]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/multi_universal/copy.ma
wsem_match finished
[helm.git] / matita / matita / lib / turing / multi_universal / copy.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||         The HELM team.                                      *)
8 (*      ||A||         http://helm.cs.unibo.it                             *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU General Public License Version 2                  *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 include "turing/turing.ma".
16 include "turing/inject.ma".
17 include "turing/while_multi.ma".
18
19 definition copy_states ≝ initN 3.
20
21 definition copy0 : copy_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 3 (refl …)).
22 definition copy1 : copy_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 3 (refl …)).
23 definition copy2 : copy_states ≝ mk_Sig ?? 2 (leb_true_to_le 3 3 (refl …)).
24
25 (*
26
27 src: a b c ... z # ---→ a b c ... z #
28      ^                              ^
29
30 dst: _ _ _ ... _ d ---→ a b c ... z d
31      ^                              ^
32
33 0) (x ≠ sep,_) → (x,x)(R,R) → 1
34    (sep,d) → None 2
35 1) (_,_) → None 1
36 2) (_,_) → None 2
37
38 *)
39
40 definition trans_copy_step ≝ 
41  λsrc,dst,sig,n,is_sep.
42  λp:copy_states × (Vector (option sig) (S n)).
43  let 〈q,a〉 ≝ p in
44  match pi1 … q with
45  [ O ⇒ match nth src ? a (None ?) with
46    [ None ⇒ 〈copy2,null_action ? n〉
47    | Some a0 ⇒ if is_sep a0 then 〈copy2,null_action ? n〉
48                   else 〈copy1,change_vec ? (S n)
49                           (change_vec ?(S n)
50                            (null_action ? n) (〈Some ? a0,R〉) src)
51                           (〈Some ? a0,R〉) dst〉 ]
52  | S q ⇒ match q with 
53    [ O ⇒ (* 1 *) 〈copy1,null_action ? n〉
54    | S _ ⇒ (* 2 *) 〈copy2,null_action ? n〉 ] ].
55
56 definition copy_step ≝ 
57   λsrc,dst,sig,n,is_sep.
58   mk_mTM sig n copy_states (trans_copy_step src dst sig n is_sep) 
59     copy0 (λq.q == copy1 ∨ q == copy2).
60
61 definition R_copy_step_true ≝ 
62   λsrc,dst,sig,n,is_sep.λint,outt: Vector (tape sig) (S n).
63   ∃x1.
64    current ? (nth src ? int (niltape ?)) = Some ? x1 ∧
65    is_sep x1 = false ∧
66    outt = change_vec ?? 
67             (change_vec ?? int
68               (tape_move_mono ? (nth src ? int (niltape ?)) (〈Some ? x1,R〉)) src)
69             (tape_move_mono ? (nth dst ? int (niltape ?)) (〈Some ? x1,R〉)) dst.
70
71 definition R_copy_step_false ≝ 
72   λsrc,dst:nat.λsig,n,is_sep.λint,outt: Vector (tape sig) (S n).
73   (∃x1.
74    current ? (nth src ? int (niltape ?)) = Some ? x1 ∧
75    is_sep x1 = true ∧ outt = int) ∨
76    current ? (nth src ? int (niltape ?)) = None ? ∧
77    outt = int.
78
79 lemma copy_q0_q2_null :
80   ∀src,dst,sig,n,is_sep,v,t.src < S n → dst < S n → 
81   current ? t = None ? → 
82   step sig n (copy_step src dst sig n is_sep)
83     (mk_mconfig ??? copy0 (change_vec ? (S n) v t src)) =
84     mk_mconfig ??? copy2 (change_vec ? (S n) v t src).
85 #src #dst #sig #n #is_sep #v #t #Hsrc #Hdst #Hcurrent
86 whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
87 [ >current_chars_change_vec // whd in match (trans ????);
88   >nth_change_vec // >Hcurrent %
89 | >current_chars_change_vec // whd in match (trans ????);
90   >nth_change_vec // >Hcurrent @tape_move_null_action
91 ]
92 qed.
93
94 lemma copy_q0_q2_sep :
95   ∀src,dst,sig,n,is_sep,v,t.src < S n → dst < S n → 
96   ∀s.current ? t = Some ? s → is_sep s = true → 
97   step sig n (copy_step src dst sig n is_sep)
98     (mk_mconfig ??? copy0 (change_vec ? (S n) v t src)) =
99     mk_mconfig ??? copy2 (change_vec ? (S n) v t src).
100 #src #dst #sig #n #is_sep #v #t #Hsrc #Hdst #s #Hcurrent #Hsep
101 whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
102 [ >current_chars_change_vec // whd in match (trans ????);
103   >nth_change_vec // >Hcurrent whd in ⊢ (??(???%)?); >Hsep %
104 | >current_chars_change_vec // whd in match (trans ????);
105   >nth_change_vec // >Hcurrent whd in ⊢ (??(????(???%))?);
106   >Hsep @tape_move_null_action
107 ]
108 qed.
109
110 axiom copy_q0_q1 :
111   ∀src,dst,sig,n,is_sep,v,t.src ≠ dst → src < S n → dst < S n → 
112   ∀s.current ? t = Some ? s → is_sep s = false → 
113   step sig n (copy_step src dst sig n is_sep)
114     (mk_mconfig ??? copy0 (change_vec ? (S n) v t src)) =
115     mk_mconfig ??? copy1 
116      (change_vec ? (S n) 
117        (change_vec ?? v
118          (tape_move_mono ? t (〈Some ? s,R〉)) src)
119        (tape_move_mono ? (nth dst ? v (niltape ?)) (〈Some ? s,R〉)) dst).
120 (*       
121 #src #dst #sig #n #is_sep #v #t #Hneq #Hsrc #Hdst #s #Hcurrent #Hsep
122 whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
123 [ >current_chars_change_vec // whd in match (trans ????);
124   >nth_change_vec // >Hcurrent whd in ⊢ (??(???%)?); >Hsep %
125 | >current_chars_change_vec // whd in match (trans ????);
126   >nth_change_vec // >Hcurrent whd in ⊢ (??(????(???%))?);
127   >Hsep whd in ⊢ (??(????(???%))?); >change_vec_commute // >pmap_change
128   >change_vec_commute // @eq_f3 //
129   <(change_vec_same ?? v dst (niltape ?)) in ⊢(??%?);
130   >pmap_change @eq_f3 //
131 ]
132 qed.*)
133
134 lemma sem_copy_step :
135   ∀src,dst,sig,n,is_sep.src ≠ dst → src < S n → dst < S n → 
136   copy_step src dst sig n is_sep ⊨ 
137     [ copy1: R_copy_step_true src dst sig n is_sep, 
138              R_copy_step_false src dst sig n is_sep ].
139 #src #dst #sig #n #is_sep #Hneq #Hsrc #Hdst #int
140 lapply (refl ? (current ? (nth src ? int (niltape ?))))
141 cases (current ? (nth src ? int (niltape ?))) in ⊢ (???%→?);
142 [ #Hcur <(change_vec_same … int src (niltape ?)) %{2} %
143   [| % [ % 
144     [ whd in ⊢ (??%?); >copy_q0_q2_null /2/ 
145     | normalize in ⊢ (%→?); #H destruct (H) ]
146     | #_ %2 >nth_change_vec >Hcur // % // ] ]
147 | #c #Hcur cases (true_or_false (is_sep c)) #Hsep
148   [ <(change_vec_same … int src (niltape ?)) %{2} % 
149     [| % [ %
150       [ whd in ⊢ (??%?); >copy_q0_q2_sep /2/
151       | normalize in ⊢ (%→?); #H destruct (H) ]
152       | #_ % >nth_change_vec // %{c} % [ % /2/ | // ] ] ]
153   | %{2} % [| % [ %
154     [ whd in ⊢ (??%?);
155       <(change_vec_same … int src (niltape ?)) in ⊢ (??%?);
156       >Hcur in ⊢ (??%?); whd in ⊢ (??%?); >(copy_q0_q1 … Hsep) /2/
157     | #_ whd %{c} % % /2/ ]
158     | * #Hfalse @False_ind /2/ ] ] ] ]
159 qed.
160
161 definition copy ≝ λsrc,dst,sig,n,is_sep.
162   whileTM … (copy_step src dst sig n is_sep) copy1.
163
164 definition R_copy ≝ 
165   λsrc,dst,sig,n,is_sep.λint,outt: Vector (tape sig) (S n).
166   (∀ls,x,xs,rs,sep.
167     nth src ? int (niltape ?) = midtape sig ls x (xs@sep::rs) →
168     (∀c.memb ? c (x::xs) = true → is_sep c = false) → is_sep sep = true → 
169     ∀ls0,x0,target,c,rs0.|xs| = |target| → 
170     nth dst ? int (niltape ?) = midtape sig ls0 x0 (target@c::rs0) → 
171     outt = change_vec ?? 
172            (change_vec ?? int (midtape sig (reverse ? xs@x::ls) sep rs) src)
173            (midtape sig (reverse ? xs@x::ls0) c rs0) dst) ∧
174   (∀c.current ? (nth src ? int (niltape ?)) = Some ? c → is_sep c = true → 
175    outt = int) ∧
176   (current ? (nth src ? int (niltape ?)) = None ? → outt = int).
177   
178 lemma wsem_copy : ∀src,dst,sig,n,is_sep.src ≠ dst → src < S n → dst < S n → 
179   copy src dst sig n is_sep ⊫ R_copy src dst sig n is_sep.
180 #src #dst #sig #n #is_sep #Hneq #Hsrc #Hdst #ta #k #outc #Hloop
181 lapply (sem_while … (sem_copy_step src dst sig n is_sep Hneq Hsrc Hdst) … Hloop) //
182 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar -ta
183 [ whd in ⊢ (%→?); *
184   [ * #x * * #Hx #Hsep #Houtc % [ %
185     [ #ls #x0 #xs #rs #sep #Hsrctc #Hnosep >Hsrctc in Hx; normalize in ⊢ (%→?);
186       #Hx0 destruct (Hx0) lapply (Hnosep ? (memb_hd …)) >Hsep
187       #Hfalse destruct (Hfalse)
188     | #c #Hc #Hsepc @Houtc ]
189     | #_ @Houtc ]
190   | * #Hcur #Houtc % [ %
191     [ #ls #x0 #xs #rs #sep #Hsrctc >Hsrctc in Hcur; normalize in ⊢ (%→?); 
192       #Hcur destruct (Hcur)
193     | #c #Hc #Hsepc @Houtc ]
194     | #_ @Houtc ]
195   ]
196 | #td #te * #c0 * * #Hc0 #Hc0nosep #Hd #Hstar #IH #He 
197   lapply (IH He) -IH * * #IH1 #IH2 #IH3 % [ %
198   [ #ls #x #xs #rs #sep #Hsrc_tc #Hnosep #Hsep #ls0 #x0 #target
199     #c #rs0 #Hlen #Hdst_tc
200     >Hsrc_tc in Hc0; normalize in ⊢ (%→?); #Hc0 destruct (Hc0)
201     <(change_vec_same … td src (niltape ?)) in Hd:(???(???(???%??)??));
202     <(change_vec_same … td dst (niltape ?)) in ⊢(???(???(???%??)??)→?);
203     >Hdst_tc >Hsrc_tc >(change_vec_change_vec ?) >change_vec_change_vec
204     >(change_vec_commute ?? td ?? dst src) [|@(sym_not_eq … Hneq)]
205     >change_vec_change_vec @(list_cases2 … Hlen)
206     [ #Hxsnil #Htargetnil #Hd>(IH2 … Hsep)
207       [ >Hd -Hd >Hxsnil >Htargetnil @(eq_vec … (niltape ?))
208         #i #Hi cases (decidable_eq_nat i src) #Hisrc
209         [ >Hisrc >(nth_change_vec_neq … src dst) [|@(sym_not_eq … Hneq)]
210           >nth_change_vec // >nth_change_vec //
211           >(nth_change_vec_neq … src dst) [|@(sym_not_eq … Hneq)]
212           >nth_change_vec // whd in ⊢ (??%?); %
213         | cases (decidable_eq_nat i dst) #Hidst
214           [ >Hidst >nth_change_vec // >nth_change_vec //
215             >nth_change_vec_neq // >Hdst_tc >Htargetnil %
216           | >nth_change_vec_neq [|@(sym_not_eq … Hidst)]
217             >nth_change_vec_neq [|@(sym_not_eq … Hisrc)]
218             >nth_change_vec_neq [|@(sym_not_eq … Hidst)]
219             >nth_change_vec_neq [|@(sym_not_eq … Hisrc)] % ]
220          ]
221       | >Hd >nth_change_vec_neq [|@(sym_not_eq … Hneq)]
222         >nth_change_vec // >nth_change_vec // >Hxsnil % ]
223     |#hd1 #hd2 #tl1 #tl2 #Hxs #Htarget >Hxs >Htarget #Hd
224      >(IH1 (c0::ls) hd1 tl1 rs sep ?? Hsep (c0::ls0) hd2 tl2 c rs0)
225      [ >Hd >(change_vec_commute … ?? td ?? src dst) //
226        >change_vec_change_vec
227        >(change_vec_commute … ?? td ?? dst src) [|@sym_not_eq //]
228        >change_vec_change_vec
229        >reverse_cons >associative_append >associative_append % 
230      | >Hd >nth_change_vec // >nth_change_vec_neq // >Hdst_tc >Htarget //
231      | >Hxs in Hlen; >Htarget normalize #Hlen destruct (Hlen) //
232      | <Hxs #c1 #Hc1 @Hnosep @memb_cons //
233      | >Hd >nth_change_vec_neq [|@sym_not_eq //]
234        >nth_change_vec // >nth_change_vec // ]
235    ]
236  | #c #Hc #Hsepc >Hc in Hc0; #Hcc0 destruct (Hcc0) >Hc0nosep in Hsepc;
237    #H destruct (H)
238  ]
239 | #HNone >HNone in Hc0; #Hc0 destruct (Hc0) ] ]
240 qed.
241  
242 lemma terminate_copy :  ∀src,dst,sig,n,is_sep,t.
243   src ≠ dst → src < S n → dst < S n → 
244   copy src dst sig n is_sep ↓ t.
245 #src #dst #sig #n #is_sep #t #Hneq #Hsrc #Hdst
246 @(terminate_while … (sem_copy_step …)) //
247 <(change_vec_same … t src (niltape ?))
248 cases (nth src (tape sig) t (niltape ?))
249 [ % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct 
250 |2,3: #a0 #al0 % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
251 | #ls #c #rs lapply c -c lapply ls -ls lapply t -t elim rs
252   [#t #ls #c % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?);
253    #H1 destruct (H1) #Hxsep >change_vec_change_vec #Ht1 % 
254    #t2 * #x0 * * >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
255    >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H)
256   |#r0 #rs0 #IH #t #ls #c % #t1 * #x * * >nth_change_vec //
257    normalize in ⊢ (%→?); #H destruct (H) #Hxsep
258    >change_vec_change_vec >change_vec_commute // #Ht1 >Ht1 @IH
259   ]
260 ]
261 qed.
262
263 lemma sem_copy : ∀src,dst,sig,n,is_sep.
264   src ≠ dst → src < S n → dst < S n → 
265   copy src dst sig n is_sep ⊨ R_copy src dst sig n is_sep.
266 #src #dst #sig #n #is_sep #Hneq #Hsrc #Hdst @WRealize_to_Realize /2/
267 qed.