]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/multi_universal/match.ma
wsem_match finished
[helm.git] / matita / matita / lib / turing / multi_universal / match.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/multi_universal/compare.ma".
16 include "turing/multi_universal/par_test.ma".
17 include "turing/multi_universal/moves_2.ma".
18
19 definition Rtc_multi_true ≝ 
20   λalpha,test,n,i.λt1,t2:Vector ? (S n).
21    (∃c. current alpha (nth i ? t1 (niltape ?)) = Some ? c ∧ test c = true) ∧ t2 = t1.
22    
23 definition Rtc_multi_false ≝ 
24   λalpha,test,n,i.λt1,t2:Vector ? (S n).
25     (∀c. current alpha (nth i ? t1 (niltape ?)) = Some ? c → test c = false) ∧ t2 = t1.
26
27 lemma sem_test_char_multi :
28   ∀alpha,test,n,i.i ≤ n → 
29   inject_TM ? (test_char ? test) n i ⊨ 
30   [ tc_true : Rtc_multi_true alpha test n i, Rtc_multi_false alpha test n i ].
31 #alpha #test #n #i #Hin #int
32 cases (acc_sem_inject … Hin (sem_test_char alpha test) int)
33 #k * #outc * * #Hloop #Htrue #Hfalse %{k} %{outc} % [ %
34 [ @Hloop
35 | #Hqtrue lapply (Htrue Hqtrue) * * * #c *
36   #Hcur #Htestc #Hnth_i #Hnth_j %
37   [ %{c} % //
38   | @(eq_vec … (niltape ?)) #i0 #Hi0
39     cases (decidable_eq_nat i0 i) #Hi0i
40     [ >Hi0i @Hnth_i
41     | @sym_eq @Hnth_j @sym_not_eq // ] ] ]
42 | #Hqfalse lapply (Hfalse Hqfalse) * * #Htestc #Hnth_i #Hnth_j %
43   [ @Htestc
44   | @(eq_vec … (niltape ?)) #i0 #Hi0
45     cases (decidable_eq_nat i0 i) #Hi0i
46     [ >Hi0i @Hnth_i
47     | @sym_eq @Hnth_j @sym_not_eq // ] ] ]
48 qed.
49
50 definition Rm_test_null_true ≝ 
51   λalpha,n,i.λt1,t2:Vector ? (S n).
52    current alpha (nth i ? t1 (niltape ?)) ≠ None ? ∧ t2 = t1.
53    
54 definition Rm_test_null_false ≝ 
55   λalpha,n,i.λt1,t2:Vector ? (S n).
56     current alpha (nth i ? t1 (niltape ?)) = None ? ∧ t2 = t1.
57
58 lemma sem_test_null_multi : ∀alpha,n,i.i ≤ n → 
59   inject_TM ? (test_null ?) n i ⊨ 
60     [ tc_true : Rm_test_null_true alpha n i, Rm_test_null_false alpha n i ].
61 #alpha #n #i #Hin #int
62 cases (acc_sem_inject … Hin (sem_test_null alpha) int)
63 #k * #outc * * #Hloop #Htrue #Hfalse %{k} %{outc} % [ %
64 [ @Hloop
65 | #Hqtrue lapply (Htrue Hqtrue) * * #Hcur #Hnth_i #Hnth_j % //
66   @(eq_vec … (niltape ?)) #i0 #Hi0 cases (decidable_eq_nat i0 i) #Hi0i
67   [ >Hi0i @sym_eq @Hnth_i | @sym_eq @Hnth_j @sym_not_eq // ] ] 
68 | #Hqfalse lapply (Hfalse Hqfalse) * * #Hcur #Hnth_i #Hnth_j %
69   [ @Hcur
70   | @(eq_vec … (niltape ?)) #i0 #Hi0 cases (decidable_eq_nat i0 i) // 
71     #Hi0i @sym_eq @Hnth_j @sym_not_eq // ] ] 
72 qed.
73
74 definition match_test ≝ λsrc,dst.λsig:DeqSet.λn.λv:Vector ? n.
75   match (nth src (option sig) v (None ?)) with 
76   [ None ⇒  false 
77   | Some x ⇒  notb (nth dst (DeqOption sig) v (None ?) == None ?) ].
78
79 definition mmove_states ≝ initN 2.
80
81 definition mmove0 : mmove_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 2 (refl …)).
82 definition mmove1 : mmove_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 2 (refl …)).
83
84 definition trans_mmove ≝ 
85  λi,sig,n,D.
86  λp:mmove_states × (Vector (option sig) (S n)).
87  let 〈q,a〉 ≝ p in match (pi1 … q) with
88  [ O ⇒ 〈mmove1,change_vec ? (S n) (null_action ? n) (〈None ?,D〉) i〉
89  | S _ ⇒ 〈mmove1,null_action sig n〉 ].
90
91 definition mmove ≝ 
92   λi,sig,n,D.
93   mk_mTM sig n mmove_states (trans_mmove i sig n D) 
94     mmove0 (λq.q == mmove1).
95     
96 definition Rm_multi ≝ 
97   λalpha,n,i,D.λt1,t2:Vector ? (S n).
98   t2 = change_vec ? (S n) t1 (tape_move alpha (nth i ? t1 (niltape ?)) D) i.
99    
100 lemma sem_move_multi :
101   ∀alpha,n,i,D.i ≤ n → 
102   mmove i alpha n D ⊨ Rm_multi alpha n i D.
103 #alpha #n #i #D #Hin #int %{2}
104 %{(mk_mconfig ? mmove_states n mmove1 ?)} 
105 [| %
106  [ whd in ⊢ (??%?); @eq_f whd in ⊢ (??%?); @eq_f %
107  | whd >tape_move_multi_def
108    <(change_vec_same … (ctapes …) i (niltape ?))
109    >pmap_change <tape_move_multi_def >tape_move_null_action % ] ]
110  qed.
111   
112 definition rewind ≝ λsrc,dst,sig,n.
113   parmove src dst sig n L · mmove src sig n R · mmove dst sig n R.
114
115 definition R_rewind ≝ λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n).
116   (∀x,x0,xs,rs.
117     nth src ? int (niltape ?) = midtape sig (xs@[x0]) x rs → 
118     ∀ls0,y,y0,target,rs0.|xs| = |target| → 
119     nth dst ? int (niltape ?) = midtape sig (target@y0::ls0) y rs0 → 
120     outt = change_vec ?? 
121            (change_vec ?? int (midtape sig [] x0 (reverse ? xs@x::rs)) src)
122            (midtape sig ls0 y0 (reverse ? target@y::rs0)) dst) ∧
123   (∀x,rs.nth src ? int (niltape ?) = midtape sig [] x rs → 
124    ∀ls0,y,rs0.nth dst ? int (niltape ?) = midtape sig ls0 y rs0 → 
125    outt = int).
126
127 (*
128 theorem accRealize_to_Realize :
129   ∀sig,n.∀M:mTM sig n.∀Rtrue,Rfalse,acc.
130   M ⊨ [ acc: Rtrue, Rfalse ] →  M ⊨ Rtrue ∪ Rfalse.
131 #sig #n #M #Rtrue #Rfalse #acc #HR #t
132 cases (HR t) #k * #outc * * #Hloop
133 #Htrue #Hfalse %{k} %{outc} % // 
134 cases (true_or_false (cstate sig (states sig n M) n outc == acc)) #Hcase
135 [ % @Htrue @(\P Hcase) | %2 @Hfalse @(\Pf Hcase) ]
136 qed.
137 *)
138
139 lemma sem_rewind : ∀src,dst,sig,n.
140   src ≠ dst → src < S n → dst < S n → 
141   rewind src dst sig n ⊨ R_rewind src dst sig n.
142 #src #dst #sig #n #Hneq #Hsrc #Hdst
143 @(sem_seq_app sig n ????? (sem_parmoveL src dst sig n Hneq Hsrc Hdst) ?)
144 [| @(sem_seq_app sig n ????? (sem_move_multi … R ?) (sem_move_multi … R ?)) //
145  @le_S_S_to_le // ]
146 #ta #tb * #tc * * #Htc #_ * #td * whd in ⊢ (%→%→?); #Htd #Htb %
147 [ #x #x0 #xs #rs #Hmidta_src #ls0 #y #y0 #target #rs0 #Hlen #Hmidta_dst
148   >(Htc ??? Hmidta_src ls0 y (target@[y0]) rs0 ??) in Htd;
149   [|>Hmidta_dst //
150   |>length_append >length_append >Hlen % ]
151   >change_vec_commute [|@sym_not_eq //]
152   >change_vec_change_vec
153   >nth_change_vec_neq [|@sym_not_eq //]
154   >nth_change_vec // >reverse_append >reverse_single
155   >reverse_append >reverse_single normalize in match (tape_move ???);
156   >rev_append_def >append_nil #Htd >Htd in Htb;
157   >change_vec_change_vec >nth_change_vec //
158   cases ls0 [|#l1 #ls1] normalize in match (tape_move ???); //
159 | #x #rs #Hmidta_src #ls0 #y #rs0 #Hmidta_dst
160   lapply (Htc … Hmidta_src … (refl ??) Hmidta_dst) -Htc #Htc >Htc in Htd;
161   >change_vec_commute [|@sym_not_eq //] >change_vec_change_vec
162   >nth_change_vec_neq [|@sym_not_eq //] 
163   >nth_change_vec // lapply (refl ? ls0) cases ls0 in ⊢ (???%→%);
164   [ #Hls0 #Htd >Htd in Htb; 
165     >nth_change_vec // >change_vec_change_vec 
166     whd in match (tape_move ???);whd in match (tape_move ???); <Hmidta_src
167     <Hls0 <Hmidta_dst >change_vec_same >change_vec_same //
168   | #l1 #ls1 #Hls0 #Htd >Htd in Htb;
169     >nth_change_vec // >change_vec_change_vec 
170     whd in match (tape_move ???);whd in match (tape_move ???); <Hmidta_src
171     <Hls0 <Hmidta_dst >change_vec_same >change_vec_same //
172 ]]
173 qed.
174
175 definition match_step ≝ λsrc,dst,sig,n.
176   compare src dst sig n ·
177      (ifTM ?? (partest sig n (match_test src dst sig ?))
178       (single_finalTM ??
179         (rewind src dst sig n · (inject_TM ? (move_r ?) n dst)))
180       (nop …)
181       partest1).
182
183 (* we assume the src is a midtape
184    we stop
185    if the dst is out of bounds (outt = int)
186    or dst.right is shorter than src.right (outt.current → None)
187    or src.right is a prefix of dst.right (out = just right of the common prefix) *)
188 definition R_match_step_false ≝ 
189   λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n).
190   ∀ls,x,xs.
191   nth src ? int (niltape ?) = midtape sig ls x xs →
192   ((current sig (nth dst (tape sig) int (niltape sig)) = None ?) ∧ outt = int) ∨
193     (∃ls0,rs0,xs0. nth dst ? int (niltape ?) = midtape sig ls0 x rs0 ∧
194       xs = rs0@xs0 ∧
195       outt = change_vec ??
196              (change_vec ?? int (mk_tape sig (reverse ? rs0@x::ls) (option_hd ? xs0) (tail ? xs0)) src)
197              (mk_tape ? (reverse ? rs0@x::ls0) (None ?) [ ]) dst) ∨
198     (∃ls0,rs0. 
199      nth dst ? int (niltape ?) = midtape sig ls0 x (xs@rs0) ∧
200      (* ∀rsj,c. 
201      rs0 = c::rsj → *)
202      outt = change_vec ??
203             (change_vec ?? int (mk_tape sig (reverse ? xs@x::ls) (None ?) [ ]) src)
204             (mk_tape sig (reverse ? xs@x::ls0) (option_hd ? rs0) (tail ? rs0)) dst).
205
206 (*
207    we assume the src is a midtape [ ] s rs
208    if we iterate
209    then dst.current = Some ? s1
210    and  if s ≠ s1 then outt = int.dst.move_right()
211    and  if s = s1 
212         then int.src.right and int.dst.right have a common prefix
213         and  the heads of their suffixes are different
214         and  outt = int.dst.move_right().
215                
216  *)
217 definition R_match_step_true ≝ 
218   λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n).
219   ∀s,rs.nth src ? int (niltape ?) = midtape ? [ ] s rs → 
220   outt = change_vec ?? int 
221          (tape_move_mono … (nth dst ? int (niltape ?)) (〈None ?,R〉)) dst ∧
222   (current sig (nth dst (tape sig) int (niltape sig)) = Some ? s → 
223    ∃xs,ci,rs',ls0,cj,rs0.
224    rs = xs@ci::rs' ∧
225    nth dst ? int (niltape ?) = midtape sig ls0 s (xs@cj::rs0) ∧
226    ci ≠ cj).
227   
228 axiom daemon : ∀X:Prop.X.
229       
230 lemma sem_match_step :
231   ∀src,dst,sig,n.src ≠ dst → src < S n → dst < S n → 
232   match_step src dst sig n ⊨ 
233     [ inr ?? (inr ?? (inl … (inr ?? start_nop))) : 
234       R_match_step_true src dst sig n, 
235       R_match_step_false src dst sig n ].
236 #src #dst #sig #n #Hneq #Hsrc #Hdst 
237 @(acc_sem_seq_app sig n … (sem_compare src dst sig n Hneq Hsrc Hdst)
238     (acc_sem_if ? n … (sem_partest sig n (match_test src dst sig ?))
239       (sem_seq … 
240         (sem_rewind ???? Hneq Hsrc Hdst) 
241         (sem_inject … dst (le_S_S_to_le … Hdst) (sem_move_r ? )))
242       (sem_nop …)))
243 [ #ta #tb #tc * lapply (refl ? (current ? (nth src ? ta (niltape ?))))
244   cases (current ? (nth src ? ta (niltape ?))) in ⊢ (???%→%);
245   [ #Hcurta_src #Hcomp #_ * #td * >Hcomp [| % %2 %]
246     whd in ⊢ (%→?); * whd in ⊢ (??%?→?); 
247     >nth_current_chars >Hcurta_src normalize in ⊢ (%→?); #H destruct (H)
248   | #s #Hs lapply (refl ? (current ? (nth dst ? ta (niltape ?))))
249     cases (current ? (nth dst ? ta (niltape ?))) in ⊢ (???%→%);
250     [ #Hcurta_dst #Hcomp #_ * #td * >Hcomp [| %2 %]
251       whd in ⊢ (%→?); * whd in ⊢ (??%?→?); 
252       >nth_current_chars >nth_current_chars >Hs >Hcurta_dst 
253       normalize in ⊢ (%→?); #H destruct (H)
254     | #s0 #Hs0
255       cases (current_to_midtape … Hs) #ls * #rs #Hmidta_src >Hmidta_src
256       cases (current_to_midtape … Hs0) #ls0 * #rs0 #Hmidta_dst >Hmidta_dst
257       cases (true_or_false (s == s0)) #Hss0
258       [ lapply (\P Hss0) -Hss0 #Hss0 destruct (Hss0) 
259         #_ #Hcomp cases (Hcomp ????? (refl ??) (refl ??)) -Hcomp [ *
260         [ * #rs' * #_ #Hcurtc_dst * #td * whd in ⊢ (%→?); * whd in ⊢ (??%?→?);
261           >nth_current_chars >nth_current_chars >Hcurtc_dst 
262           cases (current ? (nth src …)) 
263           [normalize in ⊢ (%→?); #H destruct (H)
264           | #x >nth_change_vec // cases (reverse ? rs0)
265             [ normalize in ⊢ (%→?); #H destruct (H)
266             | #r1 #rs1 normalize in ⊢ (%→?); #H destruct (H) ] ]
267         | * #rs0' * #_ #Hcurtc_src * #td * whd in ⊢ (%→?); * whd in ⊢ (??%?→?);
268           >(?:nth src ? (current_chars ?? tc) (None ?) = None ?) 
269           [|>nth_current_chars >Hcurtc_src >nth_change_vec_neq 
270             [>nth_change_vec [cases (append ???) // | @Hsrc] 
271             |@(not_to_not … Hneq) //
272             ]]
273           normalize in ⊢ (%→?); #H destruct (H) ]
274         | * #xs * #ci * #cj * #rs'' * #rs0' * * * #Hcicj #Hrs #Hrs0
275           #Htc * #td * * #Hmatch #Htd destruct (Htd) * #te * *
276           >Htc >change_vec_commute // >nth_change_vec //
277           >change_vec_commute [|@sym_not_eq //] >nth_change_vec // #Hte #_ #Htb
278           #s' #rs' >Hmidta_src #H destruct (H)
279           lapply (Hte … (refl ??) … (refl ??) (refl ??)) -Hte
280           >change_vec_commute // >change_vec_change_vec
281           >change_vec_commute [|@sym_not_eq //] >change_vec_change_vec #Hte
282           >Hte in Htb; * * #_ >nth_change_vec // #Htb1
283           lapply (Htb1 … (refl ??)) -Htb1 #Htb1 #Htb2 %
284           [ @(eq_vec … (niltape ?)) #i #Hi
285             cases (true_or_false ((dst : DeqNat) == i)) #Hdsti
286             [ <(\P Hdsti) >Htb1 >nth_change_vec // >Hmidta_dst
287               >Hrs0 >reverse_reverse cases xs [|#r1 #rs1] %
288             | <Htb2 [|@(\Pf Hdsti)] >nth_change_vec_neq [| @(\Pf Hdsti)]
289               >Hrs0 >reverse_reverse >nth_change_vec_neq in ⊢ (???%); 
290               <Hrs <Hmidta_src [|@(\Pf Hdsti)] >change_vec_same % ]
291           | #_ >Hmidta_dst >Hrs0
292             %{xs} %{ci} %{rs''} %{ls0} %{cj} %{rs0'} % // % // 
293           ]
294         ]
295       | lapply (\Pf Hss0) -Hss0 #Hss0 #Htc cut (tc = ta) 
296         [@Htc % % @(not_to_not ??? Hss0) #H destruct (H) %]
297         -Htc #Htc destruct (Htc) #_ * #td * whd in ⊢ (%→?); * #_ 
298         #Htd destruct (Htd) * #te * * #_ #Hte * * #_ #Htb1 #Htb2
299         #s1 #rs1 >Hmidta_src #H destruct (H)
300         lapply (Hte … Hmidta_src … Hmidta_dst) -Hte #Hte destruct (Hte) %
301         [ @(eq_vec … (niltape ?)) #i #Hi
302           cases (true_or_false ((dst : DeqNat) == i)) #Hdsti
303           [ <(\P Hdsti) >(Htb1 … Hmidta_dst) >nth_change_vec // >Hmidta_dst
304             cases rs0 [|#r2 #rs2] %
305           | <Htb2 [|@(\Pf Hdsti)] >nth_change_vec_neq [| @(\Pf Hdsti)] % ]
306         | >Hs0 #H destruct (H) @False_ind cases (Hss0) /2/ ]
307       ]
308     ]
309   ]
310 | #ta #tb #tc * #Hcomp1 #Hcomp2 * #td * * #Htest #Htd destruct (Htd)
311   whd in ⊢ (%→?); #Htb destruct (Htb) #ls #x #xs #Hta_src
312   lapply (refl ? (current ? (nth dst ? ta (niltape ?))))
313   cases (current ? (nth dst ? ta (niltape ?))) in ⊢ (???%→?);
314   [ #Hcurta_dst % % % // @Hcomp1 %2 //
315   | #x0 #Hcurta_dst cases (current_to_midtape … Hcurta_dst) -Hcurta_dst
316     #ls0 * #rs0 #Hta_dst cases (true_or_false (x == x0)) #Hxx0
317     [ lapply (\P Hxx0) -Hxx0 #Hxx0 destruct (Hxx0)
318     | >(?:tc=ta) in Htest; 
319       [|@Hcomp1 % % >Hta_src >Hta_dst @(not_to_not ??? (\Pf Hxx0)) normalize
320         #Hxx0' destruct (Hxx0') % ]
321       whd in ⊢ (??%?→?); 
322       >nth_current_chars >Hta_src >nth_current_chars >Hta_dst 
323       whd in ⊢ (??%?→?); #Hfalse destruct (Hfalse) ] -Hcomp1
324       cases (Hcomp2 … Hta_src Hta_dst) [ *
325       [ * #rs' * #Hxs #Hcurtc % %2 %{ls0} %{rs0} %{rs'} %
326         [ % // | >Hcurtc % ]
327       | * #rs0' * #Hxs #Htc %2 >Htc %{ls0} %{rs0'} % // ]
328       | * #xs0 * #ci * #cj * #rs' * #rs0' * * *
329         #Hci #Hxs #Hrs0 #Htc @False_ind
330         whd in Htest:(??%?); 
331         >(?:nth src ? (current_chars ?? tc) (None ?) = Some ? ci) in Htest; 
332         [|>nth_current_chars >Htc >nth_change_vec_neq [|@(not_to_not … Hneq) //]
333           >nth_change_vec //]
334         >(?:nth dst ? (current_chars ?? tc) (None ?) = Some ? cj) 
335         [|>nth_current_chars >Htc >nth_change_vec //]
336         normalize #H destruct (H) ] ] ]
337 qed.
338
339 definition match_m ≝ λsrc,dst,sig,n.
340   whileTM … (match_step src dst sig n) 
341     (inr ?? (inr ?? (inl … (inr ?? start_nop)))).
342
343 definition R_match_m ≝ 
344   λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n).
345   ∀x,rs.
346   nth src ? int (niltape ?) = midtape sig [ ] x rs →
347   (current sig (nth dst (tape sig) int (niltape sig)) = None ? → outt = int) ∧
348   (∀ls0,x0,rs0.
349    nth dst ? int (niltape ?) = midtape sig ls0 x0 rs0 →
350    (∃l,l1.x0::rs0 = l@x::rs@l1 ∧
351     outt = change_vec ?? 
352            (change_vec ?? int 
353              (mk_tape sig (reverse ? rs@[x]) (None ?) [ ]) src)
354            (mk_tape sig ((reverse ? (l@x::rs))@ls0) (option_hd ? l1) (tail ? l1)) dst) ∨
355     ∀l,l1.x0::rs0 ≠ l@x::rs@l1).
356
357 lemma not_sub_list_merge : 
358   ∀T.∀a,b:list T. (∀l1.a ≠ b@l1) → (∀t,l,l1.a ≠ t::l@b@l1) → ∀l,l1.a ≠ l@b@l1.
359 #T #a #b #H1 #H2 #l elim l normalize //
360 qed.
361
362 lemma not_sub_list_merge_2 : 
363   ∀T:DeqSet.∀a,b:list T.∀t. (∀l1.t::a ≠ b@l1) → (∀l,l1.a ≠ l@b@l1) → ∀l,l1.t::a ≠ l@b@l1.
364 #T #a #b #t #H1 #H2 #l elim l //
365 #t0 #l1 #IH #l2 cases (true_or_false (t == t0)) #Htt0
366 [ >(\P Htt0) % normalize #H destruct (H) cases (H2 l1 l2) /2/
367 | normalize % #H destruct (H) cases (\Pf Htt0) /2/ ]
368 qed.
369
370
371 lemma wsem_match_m : ∀src,dst,sig,n.
372 src ≠ dst → src < S n → dst < S n → 
373   match_m src dst sig n ⊫ R_match_m src dst sig n.
374 #src #dst #sig #n #Hneq #Hsrc #Hdst #ta #k #outc #Hloop
375 lapply (sem_while … (sem_match_step src dst sig n Hneq Hsrc Hdst) … Hloop) //
376 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
377 [ #Hfalse #x #xs #Hmid_src
378   cases (Hfalse … Hmid_src) -Hfalse 
379   [(* current dest = None *) *
380     [ * #Hcur_dst #Houtc %
381       [#_ >Houtc //
382       | #ls0 #x0 #rs0 #Hmid_dst >Hmid_dst in Hcur_dst; 
383         normalize in ⊢ (%→?); #H destruct (H)
384       ]
385     | * #ls0 * #rs0 * #xs0 * * #Htc_dst #Hrs0 #HNone %
386       [ >Htc_dst normalize in ⊢ (%→?); #H destruct (H)
387       | #ls1 #x1 #rs1 >Htc_dst #H destruct (H)
388         >Hrs0 >HNone cases xs0
389         [ % %{[ ]} %{[ ]} % [ >append_nil >append_nil %]
390           @eq_f3 //
391           [ >reverse_append %
392           | >reverse_append >reverse_cons >reverse_append
393             >associative_append >associative_append % ]
394         | #x2 #xs2 %2 #l #l1 % #Habs lapply (eq_f ?? (length ?) ?? Habs)
395           >length_append whd in ⊢ (??%(??%)→?); >length_append
396           >length_append normalize >commutative_plus whd in ⊢ (???%→?);
397           #H destruct (H) lapply e0 >(plus_n_O (|rs1|)) in ⊢ (??%?→?);
398           >associative_plus >associative_plus 
399           #e1 lapply (injective_plus_r ??? e1) whd in ⊢ (???%→?);
400           #e2 destruct (e2)
401         ]
402       ]
403     ]
404   |* #ls0 * #rs0 * #Hmid_dst #Houtc %
405     [ >Hmid_dst normalize in ⊢ (%→?); #H destruct (H)
406     |#ls1 #x1 #rs1 >Hmid_dst #H destruct (H)
407      %1 %{[ ]} %{rs0} % [%] 
408      >reverse_cons >associative_append >Houtc %
409     ]
410   ]
411 |-ta #ta #tc #Htrue #Hstar #IH #Hout lapply (IH Hout) -IH -Hout #IH whd
412  #x #xs #Hmidta_src
413  lapply (refl ? (current ? (nth dst ? ta (niltape ?))))
414  cases (current ? (nth dst ? ta (niltape ?))) in ⊢ (???%→?); 
415   [#Hcurta_dst % 
416     [#_ whd in Htrue; >Hmidta_src in Htrue; #Htrue
417      cases (Htrue ?? (refl ??)) -Htrue >Hcurta_dst
418      (* dovremmo sapere che ta.dst è sul margine destro, da cui la move non 
419         ha effetto *) #_ cut (tc = ta) [@daemon] #Htc destruct (Htc) #_
420      cases (IH … Hmidta_src) #Houtc #_ @Houtc //
421     |#ls0 #x0 #rs0 #Hmidta_dst >Hmidta_dst in Hcurta_dst;
422      normalize in ⊢ (%→?); #H destruct (H)
423     ]
424   | #c #Hcurta_dst % [ >Hcurta_dst #H destruct (H) ]
425     #ls0 #x0 #rs0 #Hmidta_dst >Hmidta_dst in Hcurta_dst; normalize in ⊢ (%→?);
426     #H destruct (H) whd in Htrue; >Hmidta_src in Htrue; #Htrue
427     cases (Htrue ?? (refl …)) -Htrue >Hmidta_dst #Htc
428     cases (true_or_false (x==c)) #eqx
429     [ lapply (\P eqx) -eqx #eqx destruct (eqx)
430       #Htrue cases (Htrue (refl ??)) -Htrue
431       #xs0 * #ci * #rs' * #ls1 * #cj * #rs1 * * #Hxs #H destruct (H) #Hcicj
432       >Htc in IH; whd in ⊢ (%→?); >nth_change_vec_neq [|@sym_not_eq //]
433       #IH cases (IH … Hmidta_src) -IH #_ >nth_change_vec //
434       cut (∃x1,xs1.xs0@cj::rs1 = x1::xs1)
435       [ cases xs0 [ %{cj} %{rs1} % | #x1 #xs1 %{x1} %{(xs1@cj::rs1)} % ] ] * #x1 * #xs1
436       #Hxs1 >Hxs1 #IH cases (IH … (refl ??)) -IH
437       [ * #l * #l1 * #Hxs1'
438         >change_vec_commute // >change_vec_change_vec
439         #Houtc % %{(c::l)} %{l1} % 
440         [ normalize <Hxs1' %
441         | >reverse_cons >associative_append >change_vec_commute // @Houtc ]
442       | #H %2 #l #l1 >(?:l@c::xs@l1 = l@(c::xs)@l1) [|%]
443         @not_sub_list_merge
444         [ #l2 >Hxs <Hxs1 % normalize #H1 lapply (cons_injective_r ????? H1)
445           >associative_append #H2 lapply (append_l2_injective ????? (refl ??) H2)
446           #H3 lapply (cons_injective_l ????? H3) #H3 >H3 in Hcicj; * /2/
447         |#t #l2 #l3 % normalize #H1 lapply (cons_injective_r ????? H1)
448          -H1 #H1 cases (H l2 l3) #H2 @H2 @H1
449         ]
450       ]
451     | #_ cases (IH x xs ?) -IH
452       [| >Htc >nth_change_vec_neq [|@sym_not_eq //] @Hmidta_src ]
453       >Htc >nth_change_vec // cases rs0
454       [ #_ #_ %2 #l #l1 cases l
455        [ normalize cases xs
456          [ cases l1
457            [ normalize % #H destruct (H) cases (\Pf eqx) /2/
458            | #tmp1 #l2 normalize % #H destruct (H) ]
459          | #tmp1 #l2 normalize % #H destruct (H) ]
460        | #tmp1 #l2 normalize % #H destruct (H)cases l2 in e0;
461          [ normalize #H1 destruct (H1)
462          | #tmp2 #l3 normalize #H1 destruct (H1) ] ]
463       | #r1 #rs1 #_ #IH cases (IH … (refl ??)) -IH
464         [ * #l * #l1 * #Hll1 #Houtc % %{(c::l)} %{l1} % [ >Hll1 % ]
465           >Houtc >change_vec_commute // >change_vec_change_vec 
466           >change_vec_commute [|@sym_not_eq //]
467           >reverse_cons >associative_append %
468         | #Hll1 %2 @(not_sub_list_merge_2 ?? (x::xs)) normalize [|@Hll1]
469          #l1 % #H destruct (H) cases (\Pf eqx) /2/
470         ]
471       ]
472     ]
473   ]
474 ]
475 qed.
476
477 definition Pre_match_m ≝ 
478   λsrc,sig,n.λt: Vector (tape sig) (S n).
479   ∃x,xs.
480   nth src (tape sig) t (niltape sig) = midtape ? [] x xs.
481   
482 lemma terminate_match_m :
483   ∀src,dst,sig,n,t.
484   src ≠ dst → src < S n → dst < S n → 
485   Pre_match_m src sig n t → 
486   match_m src dst sig n ↓ t.
487 #src #dst #sig #n #t #Hneq #Hsrc #Hdst * #start * #xs
488 #Hmid_src
489 @(terminate_while … (sem_match_step src dst sig n Hneq Hsrc Hdst)) //
490 <(change_vec_same … t dst (niltape ?))
491 lapply (refl ? (nth dst (tape sig) t (niltape ?))) 
492 cases (nth dst (tape sig) t (niltape ?)) in ⊢ (???%→?);
493 [ #Htape_dst % #t1 whd in ⊢ (%→?); >nth_change_vec_neq [|@sym_not_eq //]
494   >Hmid_src #HR cases (HR ?? (refl ??)) -HR
495   >nth_change_vec // >Htape_dst
496 | #x0 #xs0 #Htape_dst % #t1 whd in ⊢ (%→?); >nth_change_vec_neq [|@sym_not_eq //]
497   >Hmid_src #HR cases (HR ? (refl ??)) -HR
498   >nth_change_vec // >Htape_dst normalize in ⊢ (%→?);
499   * #H @False_ind @H %
500 | #x0 #xs0 #Htape_dst % #t1 whd in ⊢ (%→?); >nth_change_vec_neq [|@sym_not_eq //]
501   >Hmid_src #HR cases (HR ? (refl ??)) -HR
502   >nth_change_vec // >Htape_dst normalize in ⊢ (%→?);
503   * #H @False_ind @H %
504 | #ls #s #rs lapply s -s lapply ls -ls lapply Hmid_src lapply t -t elim rs
505   [#t #Hmid_src #ls #s #Hmid_dst % #t1 whd in ⊢ (%→?); >nth_change_vec_neq [|@sym_not_eq //]
506    >Hmid_src >nth_change_vec // >Hmid_dst #HR cases (HR ? (refl ??)) -HR #_
507    #HR cases (HR Hstart Hnotstart)
508    cases (true_or_false (start == s)) #Hs
509    [ lapply (\P Hs) -Hs #Hs <Hs #_ #Htrue
510      cut (∃ci,xs1.xs@[end] = ci::xs1)
511      [ cases xs
512        [ %{end} %{[]} %
513        | #x1 #xs1 %{x1} %{(xs1@[end])} % ] ] * #ci * #xs1 #Hxs
514      >Hxs in Htrue; #Htrue
515      cases (Htrue [ ] start [ ] ? xs1 ? [ ] (refl ??) (refl ??) ?)
516      [ * #_ * #H @False_ind @H % ]
517      #c0 #Hc0 @Hnotend >(memb_single … Hc0) @memb_hd
518    | lapply (\Pf Hs) -Hs #Hs #Htrue #_
519      cases (Htrue ? (refl ??) Hs) -Htrue #Ht1 #_ %
520      #t2 whd in ⊢ (%→?); #HR cases (HR start ?)
521      [ >Ht1 >nth_change_vec // normalize in ⊢ (%→?); * #H @False_ind @H %
522      | >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
523        >nth_change_vec_neq [|@sym_not_eq //] >Hmid_src % ]
524    ]
525   |#r0 #rs0 #IH #t #Hmid_src #ls #s #Hmid_dst % #t1 whd in ⊢ (%→?);
526    >nth_change_vec_neq [|@sym_not_eq //] >Hmid_src
527    #Htrue cases (Htrue ? (refl ??)) -Htrue #_ #Htrue
528    <(change_vec_same … t1 dst (niltape ?))
529    cases (Htrue Hstart Hnotstart) -Htrue
530    cases (true_or_false (start == s)) #Hs
531    [ lapply (\P Hs) -Hs #Hs <Hs #_ #Htrue
532     cut (∃ls0,xs0,ci,rs,rs0.
533       nth src ? t (niltape ?) = midtape sig [ ] start (xs0@ci::rs) ∧
534       nth dst ? t (niltape ?) = midtape sig ls0 s (xs0@rs0) ∧
535       (is_endc ci = true ∨ (is_endc ci = false ∧ (∀b,tlb.rs0 = b::tlb → ci ≠ b))))
536     [cases (comp_list ? (xs@[end]) (r0::rs0) is_endc) #xs0 * #xs1 * #xs2
537       * * * #Hxs #Hrs #Hxs0notend #Hcomp >Hrs
538       cut (∃y,ys. xs1 = y::ys)
539       [ lapply Hxs0notend lapply Hxs lapply xs0 elim xs
540         [ *
541           [ normalize #Hxs1 <Hxs1 #_ %{end} %{[]} %
542           | #z #zs normalize in ⊢ (%→?); #H destruct (H) #H
543             lapply (H ? (memb_hd …)) -H >Hend #H1 destruct (H1)
544           ]
545         | #y #ys #IH0 * 
546           [ normalize in ⊢ (%→?); #Hxs1 <Hxs1 #_ %{y} %{(ys@[end])} %
547           | #z #zs normalize in ⊢ (%→?); #H destruct (H) #Hmemb
548             @(IH0 ? e0 ?) #c #Hc @Hmemb @memb_cons // ] ] ] * #y * #ys #Hxs1
549       >Hxs1 in Hxs; #Hxs >Hmid_src >Hmid_dst >Hxs >Hrs
550       %{ls} %{xs0} %{y} %{ys} %{xs2}
551       % [ % // | @Hcomp // ] ]
552     * #ls0 * #xs0 * #ci * #rs * #rs0 * * #Hmid_src' #Hmid_dst' #Hcomp
553     <Hmid_src in Htrue; >nth_change_vec // >Hs #Htrue destruct (Hs)
554     lapply (Htrue ??????? Hmid_src' Hmid_dst' ?) -Htrue
555     [ #c0 #Hc0 @Hnotend cases (orb_true_l … Hc0) -Hc0 #Hc0
556       [ whd in ⊢ (??%?); >Hc0 %
557       | @memb_cons >Hmid_src in Hmid_src'; #Hmid_src' destruct (Hmid_src')
558         lapply e0 -e0 @(list_elim_left … rs)
559         [ #e0 destruct (e0) lapply (append_l1_injective_r ?????? e0) //
560         | #x1 #xs1 #_ >append_cons in ⊢ (???%→?);
561           <associative_append #e0 lapply (append_l1_injective_r ?????? e0) //
562           #e1 >e1 @memb_append_l1 @memb_append_l1 // ] ]
563     | * * #Hciendc cases rs0 in Hcomp;
564       [ #_ * #H @False_ind @H %
565       | #r1 #rs1 * [ >Hciendc #H destruct (H) ]
566         * #_ #Hcomp lapply (Hcomp ?? (refl ??)) -Hcomp #Hcomp #_ #Htrue
567         cases (Htrue ?? (refl ??) Hcomp) #Ht1 #_ >Ht1 @(IH ?? (s::ls) r0)
568         [ >nth_change_vec_neq [|@sym_not_eq //]
569           >nth_change_vec_neq [|@sym_not_eq //] @Hmid_src
570         | >nth_change_vec // >Hmid_dst % ] ] ]
571   | >Hmid_dst >nth_change_vec // lapply (\Pf Hs) -Hs #Hs #Htrue #_
572     cases (Htrue ? (refl ??) Hs) #Ht1 #_ >Ht1 @(IH ?? (s::ls) r0)
573     [ >nth_change_vec_neq [|@sym_not_eq //]
574       >nth_change_vec_neq [|@sym_not_eq //] @Hmid_src
575     | >nth_change_vec // ] ] ] ]
576 qed.