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