]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/multi_universal/match.ma
f16190491a202810abf3f2e5fb192b2d477a2884
[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/moves.ma".
16 include "turing/if_multi.ma".
17 include "turing/inject.ma".
18 include "turing/basic_machines.ma".
19
20 definition compare_states ≝ initN 3.
21
22 definition comp0 : compare_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 3 (refl …)).
23 definition comp1 : compare_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 3 (refl …)).
24 definition comp2 : compare_states ≝ mk_Sig ?? 2 (leb_true_to_le 3 3 (refl …)).
25
26 (*
27
28 0) (x,x) → (x,x)(R,R) → 1
29    (x,y≠x) → None 2
30 1) (_,_) → None 1
31 2) (_,_) → None 2
32
33 *)
34
35 definition trans_compare_step ≝ 
36  λi,j.λsig:FinSet.λn.λis_endc.
37  λp:compare_states × (Vector (option sig) (S n)).
38  let 〈q,a〉 ≝ p in
39  match pi1 … q with
40  [ O ⇒ match nth i ? a (None ?) with
41    [ None ⇒ 〈comp2,null_action ? n〉
42    | Some ai ⇒ match nth j ? a (None ?) with 
43      [ None ⇒ 〈comp2,null_action ? n〉
44      | Some aj ⇒ if notb (is_endc ai) ∧ ai == aj 
45          then 〈comp1,change_vec ? (S n) 
46                       (change_vec ? (S n) (null_action ? n) (Some ? 〈ai,R〉) i)
47                         (Some ? 〈aj,R〉) j〉
48          else 〈comp2,null_action ? n〉 ]
49    ]
50  | S q ⇒ match q with 
51    [ O ⇒ (* 1 *) 〈comp1,null_action ? n〉
52    | S _ ⇒ (* 2 *) 〈comp2,null_action ? n〉 ] ].
53
54 definition compare_step ≝ 
55   λi,j,sig,n,is_endc.
56   mk_mTM sig n compare_states (trans_compare_step i j sig n is_endc) 
57     comp0 (λq.q == comp1 ∨ q == comp2).
58
59 definition R_comp_step_true ≝ 
60   λi,j,sig,n,is_endc.λint,outt: Vector (tape sig) (S n).
61   ∃x.
62    is_endc x = false ∧
63    current ? (nth i ? int (niltape ?)) = Some ? x ∧
64    current ? (nth j ? int (niltape ?)) = Some ? x ∧
65    outt = change_vec ?? 
66             (change_vec ?? int
67               (tape_move ? (nth i ? int (niltape ?)) (Some ? 〈x,R〉)) i)
68             (tape_move ? (nth j ? int (niltape ?)) (Some ? 〈x,R〉)) j.
69
70 definition R_comp_step_false ≝ 
71   λi,j:nat.λsig,n,is_endc.λint,outt: Vector (tape sig) (S n).
72    ((∃x.current ? (nth i ? int (niltape ?)) = Some ? x ∧ is_endc x = true) ∨
73    current ? (nth i ? int (niltape ?)) ≠ current ? (nth j ? int (niltape ?)) ∨
74    current ? (nth i ? int (niltape ?)) = None ? ∨
75    current ? (nth j ? int (niltape ?)) = None ?) ∧ outt = int.
76
77 lemma comp_q0_q2_null :
78   ∀i,j,sig,n,is_endc,v.i < S n → j < S n → 
79   (nth i ? (current_chars ?? v) (None ?) = None ? ∨
80    nth j ? (current_chars ?? v) (None ?) = None ?) → 
81   step sig n (compare_step i j sig n is_endc) (mk_mconfig ??? comp0 v) 
82   = mk_mconfig ??? comp2 v.
83 #i #j #sig #n #is_endc #v #Hi #Hj
84 whd in ⊢ (? → ??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (?→??%?);
85 * #Hcurrent
86 [ @eq_f2
87   [ whd in ⊢ (??(???%)?); >Hcurrent %
88   | whd in ⊢ (??(???????(???%))?); >Hcurrent @tape_move_null_action ]
89 | @eq_f2
90   [ whd in ⊢ (??(???%)?); >Hcurrent cases (nth i ?? (None sig)) //
91   | whd in ⊢ (??(???????(???%))?); >Hcurrent
92     cases (nth i ?? (None sig)) [|#x] @tape_move_null_action ] ]
93 qed.
94
95 lemma comp_q0_q2_neq :
96   ∀i,j,sig,n,is_endc,v.i < S n → j < S n → 
97   ((∃x.nth i ? (current_chars ?? v) (None ?) = Some ? x ∧ is_endc x = true) ∨ 
98     nth i ? (current_chars ?? v) (None ?) ≠ nth j ? (current_chars ?? v) (None ?)) → 
99   step sig n (compare_step i j sig n is_endc) (mk_mconfig ??? comp0 v) 
100   = mk_mconfig ??? comp2 v.
101 #i #j #sig #n #is_endc #v #Hi #Hj lapply (refl ? (nth i ?(current_chars ?? v)(None ?)))
102 cases (nth i ?? (None ?)) in ⊢ (???%→?);
103 [ #Hnth #_ @comp_q0_q2_null // % //
104 | #ai #Hai lapply (refl ? (nth j ?(current_chars ?? v)(None ?)))
105   cases (nth j ?? (None ?)) in ⊢ (???%→?);
106   [ #Hnth #_ @comp_q0_q2_null // %2 //
107   | #aj #Haj *
108     [ * #c * >Hai #Heq #Hendc whd in ⊢ (??%?); 
109       >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
110       [ whd in match (trans ????); >Hai >Haj destruct (Heq) 
111         whd in ⊢ (??(???%)?); >Hendc // 
112       | whd in match (trans ????); >Hai >Haj destruct (Heq) 
113         whd in ⊢ (??(???????(???%))?); >Hendc @tape_move_null_action
114       ]
115     | #Hneq
116       whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
117       [ whd in match (trans ????); >Hai >Haj
118         whd in ⊢ (??(???%)?); cut ((¬is_endc ai∧ai==aj)=false)
119         [>(\bf ?) /2 by not_to_not/ cases (is_endc ai) // |#Hcut >Hcut //]
120         | whd in match (trans ????); >Hai >Haj
121           whd in ⊢ (??(???????(???%))?); cut ((¬is_endc ai∧ai==aj)=false)
122           [>(\bf ?) /2 by not_to_not/ cases (is_endc ai) // 
123           |#Hcut >Hcut @tape_move_null_action
124           ]
125         ]
126       ]
127     ]
128 ]
129 qed.
130
131 lemma comp_q0_q1 :
132   ∀i,j,sig,n,is_endc,v,a.i ≠ j → i < S n → j < S n → 
133   nth i ? (current_chars ?? v) (None ?) = Some ? a → is_endc a = false →
134   nth j ? (current_chars ?? v) (None ?) = Some ? a → 
135   step sig n (compare_step i j sig n is_endc) (mk_mconfig ??? comp0 v) =
136     mk_mconfig ??? comp1 
137      (change_vec ? (S n) 
138        (change_vec ?? v
139          (tape_move ? (nth i ? v (niltape ?)) (Some ? 〈a,R〉)) i)
140        (tape_move ? (nth j ? v (niltape ?)) (Some ? 〈a,R〉)) j).
141 #i #j #sig #n #is_endc #v #a #Heq #Hi #Hj #Ha1 #Hnotendc #Ha2
142 whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
143 [ whd in match (trans ????);
144   >Ha1 >Ha2 whd in ⊢ (??(???%)?); >Hnotendc >(\b ?) //
145 | whd in match (trans ????);
146   >Ha1 >Ha2 whd in ⊢ (??(???????(???%))?); >Hnotendc >(\b ?) //
147   change with (change_vec ?????) in ⊢ (??(???????%)?);
148   <(change_vec_same … v j (niltape ?)) in ⊢ (??%?);
149   <(change_vec_same … v i (niltape ?)) in ⊢ (??%?);
150   >pmap_change >pmap_change >tape_move_null_action
151   @eq_f2 // @eq_f2 // >nth_change_vec_neq //
152 ]
153 qed.
154
155 lemma sem_comp_step :
156   ∀i,j,sig,n,is_endc.i ≠ j → i < S n → j < S n → 
157   compare_step i j sig n is_endc ⊨ 
158     [ comp1: R_comp_step_true i j sig n is_endc, 
159              R_comp_step_false i j sig n is_endc ].
160 #i #j #sig #n #is_endc #Hneq #Hi #Hj #int
161 lapply (refl ? (current ? (nth i ? int (niltape ?))))
162 cases (current ? (nth i ? int (niltape ?))) in ⊢ (???%→?);
163 [ #Hcuri %{2} %
164   [| % [ %
165     [ whd in ⊢ (??%?); >comp_q0_q2_null /2/ % <Hcuri in ⊢ (???%); 
166       @sym_eq @nth_vec_map
167     | normalize in ⊢ (%→?); #H destruct (H) ]
168   | #_ % // % %2 // ] ]
169 | #a #Ha lapply (refl ? (current ? (nth j ? int (niltape ?))))
170   cases (current ? (nth j ? int (niltape ?))) in ⊢ (???%→?);
171   [ #Hcurj %{2} %
172     [| % [ %
173        [ whd in ⊢ (??%?); >comp_q0_q2_null /2/ %2 <Hcurj in ⊢ (???%); 
174          @sym_eq @nth_vec_map
175        | normalize in ⊢ (%→?); #H destruct (H) ]
176        | #_ % // >Ha >Hcurj % % %2 % #H destruct (H) ] ]
177   | #b #Hb %{2} 
178    cases (true_or_false (is_endc a)) #Haendc
179     [ %
180       [| % [ % 
181         [whd in ⊢  (??%?);  >comp_q0_q2_neq //
182          % %{a} % // <Ha @sym_eq @nth_vec_map
183         | normalize in ⊢ (%→?); #H destruct (H) ]
184       | #_ % // % % % >Ha %{a} % // ]
185       ]
186     |cases (true_or_false (a == b)) #Hab
187       [ %
188         [| % [ % 
189           [whd in ⊢  (??%?);  >(comp_q0_q1 … a Hneq Hi Hj) //
190             [>(\P Hab) <Hb @sym_eq @nth_vec_map
191             |<Ha @sym_eq @nth_vec_map ]
192           | #_ whd >(\P Hab) %{b} % // % // <(\P Hab) % // ]
193           | * #H @False_ind @H %
194         ] ]
195       | %
196         [| % [ % 
197           [whd in ⊢  (??%?);  >comp_q0_q2_neq //
198            <(nth_vec_map ?? (current …) i ? int (niltape ?))
199            <(nth_vec_map ?? (current …) j ? int (niltape ?)) %2 >Ha >Hb
200            @(not_to_not ??? (\Pf Hab)) #H destruct (H) %
201           | normalize in ⊢ (%→?); #H destruct (H) ]
202         | #_ % // % % %2 >Ha >Hb @(not_to_not ??? (\Pf Hab)) #H destruct (H) % ] ]
203       ]
204     ]
205   ]
206 ]
207 qed.
208
209 definition compare ≝ λi,j,sig,n,is_endc.
210   whileTM … (compare_step i j sig n is_endc) comp1.
211
212 definition R_compare ≝ 
213   λi,j,sig,n,is_endc.λint,outt: Vector (tape sig) (S n).
214   ((∃x.current ? (nth i ? int (niltape ?)) = Some ? x ∧ is_endc x = true) ∨
215    (current ? (nth i ? int (niltape ?)) ≠ current ? (nth j ? int (niltape ?)) ∨
216     current ? (nth i ? int (niltape ?)) = None ? ∨
217     current ? (nth j ? int (niltape ?)) = None ?) → outt = int) ∧
218   (∀ls,x,xs,ci,rs,ls0,rs0. 
219     nth i ? int (niltape ?) = midtape sig ls x (xs@ci::rs) →
220     nth j ? int (niltape ?) = midtape sig ls0 x (xs@rs0) →
221     (∀c0. memb ? c0 (x::xs) = true → is_endc c0 = false) → 
222     (rs0 = [ ] ∧
223      outt = change_vec ?? 
224            (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) i)
225            (mk_tape sig (reverse ? xs@x::ls0) (None ?) []) j) ∨
226     ∃cj,rs1.rs0 = cj::rs1 ∧
227     ((is_endc ci = true ∨ ci ≠ cj) → 
228     outt = change_vec ?? 
229            (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) i)
230            (midtape sig (reverse ? xs@x::ls0) cj rs1) j)).
231           
232 lemma wsem_compare : ∀i,j,sig,n,is_endc.i ≠ j → i < S n → j < S n → 
233   compare i j sig n is_endc ⊫ R_compare i j sig n is_endc.
234 #i #j #sig #n #is_endc #Hneq #Hi #Hj #ta #k #outc #Hloop
235 lapply (sem_while … (sem_comp_step i j sig n is_endc Hneq Hi Hj) … Hloop) //
236 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
237 [ #tc whd in ⊢ (%→?); * * [ * [ *
238   [* #curi * #Hcuri #Hendi #Houtc %
239     [ #_ @Houtc  
240     | #ls #x #xs #ci #rs #ls0 #rs0 #Hnthi #Hnthj #Hnotendc 
241       @False_ind
242       >Hnthi in Hcuri; normalize in ⊢ (%→?); #H destruct (H)
243       >(Hnotendc ? (memb_hd … )) in Hendi; #H destruct (H)
244     ]
245   |#Hcicj #Houtc % 
246     [ #_ @Houtc
247     | #ls #x #xs #ci #rs #ls0 #rs0 #Hnthi #Hnthj
248       >Hnthi in Hcicj; >Hnthj normalize in ⊢ (%→?); * #H @False_ind @H %
249     ]]
250   | #Hci #Houtc %
251     [ #_ @Houtc
252     | #ls #x #xs #ci #rs #ls0 #rs0 #Hnthi >Hnthi in Hci;
253       normalize in ⊢ (%→?); #H destruct (H) ] ]
254   | #Hcj #Houtc %
255     [ #_ @Houtc
256     | #ls #x #xs #ci #rs #ls0 #rs0 #_ #Hnthj >Hnthj in Hcj;
257       normalize in ⊢ (%→?); #H destruct (H) ] ]
258   | #tc #td #te * #x * * * #Hendcx #Hci #Hcj #Hd #Hstar #IH #He lapply (IH He) -IH *
259     #IH1 #IH2 %
260     [ >Hci >Hcj * [* #x0 * #H destruct (H) >Hendcx #H destruct (H) 
261     |* [* #H @False_ind [cases H -H #H @H % | destruct (H)] | #H destruct (H)]] 
262     | #ls #c0 #xs #ci #rs #ls0 #rs0 cases xs
263       [ #Hnthi #Hnthj #Hnotendc cases rs0 in Hnthj;
264         [ #Hnthj % % // >IH1
265           [ >Hd @eq_f3 //
266             [ @eq_f3 // >(?:c0=x) [ >Hnthi % ]
267               >Hnthi in Hci;normalize #H destruct (H) %
268             | >(?:c0=x) [ >Hnthj % ]
269               >Hnthi in Hci;normalize #H destruct (H) % ]
270           | >Hd %2 %2 >nth_change_vec // >Hnthj % ]
271         | #r1 #rs1 #Hnthj %2 %{r1} %{rs1} % // *
272           [ #Hendci >IH1
273             [ >Hd @eq_f3 // 
274               [ @eq_f3 // >(?:c0=x) [ >Hnthi % ]
275              >Hnthi in Hci;normalize #H destruct (H) %
276             | >(?:c0=x) [ >Hnthj % ]
277             >Hnthi in Hci;normalize #H destruct (H) % ]
278         | >Hd >nth_change_vec // >nth_change_vec_neq [|@sym_not_eq //]
279           >nth_change_vec // >Hnthi >Hnthj normalize % %{ci} % //
280         ]
281       |#Hcir1 >IH1
282         [>Hd @eq_f3 // 
283           [ @eq_f3 // >(?:c0=x) [ >Hnthi % ]
284             >Hnthi in Hci;normalize #H destruct (H) %
285           | >(?:c0=x) [ >Hnthj % ]
286             >Hnthi in Hci;normalize #H destruct (H) % ]
287         | >Hd %2 % % >nth_change_vec //
288           >nth_change_vec_neq [|@sym_not_eq //]
289           >nth_change_vec // >Hnthi >Hnthj normalize @(not_to_not … Hcir1)
290           #H destruct (H) % ]
291       ]
292     ]
293   |#x0 #xs0 #Hnthi #Hnthj #Hnotendc 
294    cut (c0 = x) [ >Hnthi in Hci; normalize #H destruct (H) // ]
295    #Hcut destruct (Hcut) cases rs0 in Hnthj;
296     [ #Hnthj % % // 
297       cases (IH2 (x::ls) x0 xs0 ci rs (x::ls0) [ ] ???) -IH2
298       [ * #_ #IH2 >IH2 >Hd >change_vec_commute in ⊢ (??%?); //
299         >change_vec_change_vec >change_vec_commute in ⊢ (??%?); //
300         @sym_not_eq //
301       | * #cj * #rs1 * #H destruct (H)
302       | >Hd >nth_change_vec_neq [|@sym_not_eq //] >nth_change_vec //
303         >Hnthi %
304       | >Hd >nth_change_vec // >Hnthj %
305       | #c0 #Hc0 @Hnotendc @memb_cons @Hc0 ]
306     | #r1 #rs1 #Hnthj %2 %{r1} %{rs1} % // #Hcir1
307       cases(IH2 (x::ls) x0 xs0 ci rs (x::ls0) (r1::rs1) ???)
308       [ * #H destruct (H)
309       | * #r1' * #rs1' * #H destruct (H) #Hc1r1 >Hc1r1 //
310         >Hd >change_vec_commute in ⊢ (??%?); //
311         >change_vec_change_vec >change_vec_commute in ⊢ (??%?); //
312           @sym_not_eq //
313       | >Hd >nth_change_vec_neq [|@sym_not_eq //] >nth_change_vec //
314         >Hnthi //
315       | >Hd >nth_change_vec // >Hnthi >Hnthj %
316       | #c0 #Hc0 @Hnotendc @memb_cons @Hc0
317 ]]]]]
318 qed.      
319  
320 lemma terminate_compare :  ∀i,j,sig,n,is_endc,t.
321   i ≠ j → i < S n → j < S n → 
322   compare i j sig n is_endc ↓ t.
323 #i #j #sig #n #is_endc #t #Hneq #Hi #Hj
324 @(terminate_while … (sem_comp_step …)) //
325 <(change_vec_same … t i (niltape ?))
326 cases (nth i (tape sig) t (niltape ?))
327 [ % #t1 * #x * * * #_ >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct 
328 |2,3: #a0 #al0 % #t1 * #x * * * #_ >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
329 | #ls #c #rs lapply c -c lapply ls -ls lapply t -t elim rs
330   [#t #ls #c % #t1 * #x * * * #Hendcx >nth_change_vec // normalize in ⊢ (%→?);
331    #H1 destruct (H1) #Hxsep >change_vec_change_vec #Ht1 % 
332    #t2 * #x0 * * * #Hendcx0 >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
333    >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H)
334   |#r0 #rs0 #IH #t #ls #c % #t1 * #x * * >nth_change_vec //
335    normalize in ⊢ (%→?); #H destruct (H) #Hcur
336    >change_vec_change_vec >change_vec_commute // #Ht1 >Ht1 @IH
337   ]
338 ]
339 qed.
340
341 lemma sem_compare : ∀i,j,sig,n,is_endc.
342   i ≠ j → i < S n → j < S n → 
343   compare i j sig n is_endc ⊨ R_compare i j sig n is_endc.
344 #i #j #sig #n #is_endc #Hneq #Hi #Hj @WRealize_to_Realize /2/
345 qed.
346
347 (*
348    |conf1   $
349    |confin 0/1 confout move
350
351   match machine step ≝
352     compare;
353     if (cur(src) != $)
354       then
355         parmoveL;
356         moveR(dst);
357       else nop
358  *)
359
360 definition Rtc_multi_true ≝ 
361   λalpha,test,n,i.λt1,t2:Vector ? (S n).
362    (∃c. current alpha (nth i ? t1 (niltape ?)) = Some ? c ∧ test c = true) ∧ t2 = t1.
363    
364 definition Rtc_multi_false ≝ 
365   λalpha,test,n,i.λt1,t2:Vector ? (S n).
366     (∀c. current alpha (nth i ? t1 (niltape ?)) = Some ? c → test c = false) ∧ t2 = t1.
367
368 lemma sem_test_char_multi :
369   ∀alpha,test,n,i.i ≤ n → 
370   inject_TM ? (test_char ? test) n i ⊨ 
371   [ tc_true : Rtc_multi_true alpha test n i, Rtc_multi_false alpha test n i ].
372 #alpha #test #n #i #Hin #int
373 cases (acc_sem_inject … Hin (sem_test_char alpha test) int)
374 #k * #outc * * #Hloop #Htrue #Hfalse %{k} %{outc} % [ %
375 [ @Hloop
376 | #Hqtrue lapply (Htrue Hqtrue) * * * #c *
377   #Hcur #Htestc #Hnth_i #Hnth_j %
378   [ %{c} % //
379   | @(eq_vec … (niltape ?)) #i0 #Hi0
380     cases (decidable_eq_nat i0 i) #Hi0i
381     [ >Hi0i @Hnth_i
382     | @sym_eq @Hnth_j @sym_not_eq // ] ] ]
383 | #Hqfalse lapply (Hfalse Hqfalse) * * #Htestc #Hnth_i #Hnth_j %
384   [ @Htestc
385   | @(eq_vec … (niltape ?)) #i0 #Hi0
386     cases (decidable_eq_nat i0 i) #Hi0i
387     [ >Hi0i @Hnth_i
388     | @sym_eq @Hnth_j @sym_not_eq // ] ] ]
389 qed.
390
391 axiom comp_list: ∀S:DeqSet. ∀l1,l2:list S.∀is_endc. ∃l,tl1,tl2. 
392   l1 = l@tl1 ∧ l2 = l@tl2 ∧ (∀c.c ∈ l = true → is_endc c = false) ∧
393   ∀a,tla. tl1 = a::tla → is_endc a = true ∨ (∀b,tlb.tl2 = b::tlb → a≠b).
394   
395 axiom daemon : ∀X:Prop.X.
396
397 (*
398 definition R_match_step_false ≝ 
399   λsrc,dst,sig,n,is_endc.λint,outt: Vector (tape sig) (S n).
400   ∀ls,x,xs,end,rs.
401   nth src ? int (niltape ?) = midtape sig ls x (xs@end::rs) →
402   (∀c0. memb ? c0 (x::xs) = true → is_endc c0 = false) → is_endc end = true →
403    ((current sig (nth dst (tape sig) int (niltape sig)) = None ?) ∧ outt = int) ∨
404    (∃ls0,rs0. 
405     nth dst ? int (niltape ?) = midtape sig ls0 x (xs@rs0) ∧
406     ∀rsj,c. 
407     rs0 = c::rsj →
408     outt = change_vec ??
409            (change_vec ?? int (midtape sig (reverse ? xs@x::ls) end rs) src)
410            (midtape sig (reverse ? xs@x::ls0) c rsj) dst).
411
412 definition R_match_step_true ≝ 
413   λsrc,dst,sig,n,is_startc,is_endc.λint,outt: Vector (tape sig) (S n).
414   ∀s.current sig (nth src (tape sig) int (niltape sig)) = Some ? s → 
415   is_startc s = true → 
416   (∀c.c ∈ right ? (nth src (tape sig) int (niltape sig)) = true → is_startc c = false) →
417   (current sig (nth dst (tape sig) int (niltape sig)) = None ? → outt = int) ∧
418   (∀s1.current sig (nth dst (tape sig) int (niltape sig)) = Some ? s1 → s ≠ s1 →  
419    outt = change_vec ?? int 
420           (tape_move … (nth dst ? int (niltape ?)) (Some ? 〈s1,R〉)) dst ∧ is_endc s = false) ∧  
421   (∀ls,x,xs,ci,rs,ls0,rs0. 
422     nth src ? int (niltape ?) = midtape sig ls x (xs@ci::rs) →
423     nth dst ? int (niltape ?) = midtape sig ls0 x (xs@rs0) →
424     (∀c0. memb ? c0 (x::xs) = true → is_endc c0 = false) → 
425     (∀cj,rs1.rs0 = cj::rs1 → ci ≠ cj →
426      (outt = change_vec ?? int 
427            (tape_move … (nth dst ? int (niltape ?)) (Some ? 〈x,R〉)) dst ∧ is_endc ci = false)) ∧
428     (rs0 = [ ] →
429      outt = change_vec ??
430            (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) src)
431            (mk_tape sig (reverse ? xs@x::ls0) (None ?) [ ]) dst)).
432            
433 lemma sem_match_step :
434   ∀src,dst,sig,n,is_startc,is_endc.src ≠ dst → src < S n → dst < S n → 
435   match_step src dst sig n is_startc is_endc ⊨ 
436     [ inr ?? (inr ?? (inl … (inr ?? start_nop))) : 
437       R_match_step_true src dst sig n is_startc is_endc, 
438       R_match_step_false src dst sig n is_endc ].
439 #src #dst #sig #n #is_startc #is_endc #Hneq #Hsrc #Hdst 
440 @(acc_sem_seq_app sig n … (sem_compare src dst sig n is_endc Hneq Hsrc Hdst)
441     (acc_sem_if ? n … (sem_test_char_multi sig (λa.is_endc a == false) n src (le_S_S_to_le … Hsrc))
442       (sem_seq … 
443         (sem_parmoveL ???? is_startc Hneq Hsrc Hdst) 
444         (sem_inject … dst (le_S_S_to_le … Hdst) (sem_move_r ? )))
445       (sem_nop …)))
446 [#ta #tb #tc * #Hcomp1 #Hcomp2 * #td * * * #c * #Hcurtc #Hcend #Htd >Htd -Htd
447  #Htb #s #Hcurta_src #Hstart #Hnotstart % [ %
448  [#Hdst_none @daemon 
449  | #s1 #Hcurta_dst #Hneqss1
450    lapply Htb lapply Hcurtc -Htb -Hcurtc >(?:tc=ta) 
451    [|@Hcomp1 %2 % % >Hcurta_src >Hcurta_dst @(not_to_not … Hneqss1) #H destruct (H) % ]
452    #Hcurtc * #te * * #_ #Hte >Hte [2: %1 %1 %{s} % //] 
453    whd in ⊢ (%→?); * * #_ #Htbdst #Htbelse %
454    [ @(eq_vec … (niltape ?)) #i #Hi cases (decidable_eq_nat i dst) #Hidst
455      [ >Hidst >nth_change_vec // cases (current_to_midtape … Hcurta_dst)
456        #ls * #rs #Hta_mid >(Htbdst … Hta_mid) >Hta_mid cases rs //
457      | >nth_change_vec_neq [|@sym_not_eq //] @sym_eq @Htbelse @sym_not_eq // ]
458    | >Hcurtc in Hcurta_src; #H destruct (H) cases (is_endc s) in Hcend;
459      normalize #H destruct (H) // ]
460    ]
461  |#ls #x #xs #ci #rs #ls0 #rs00 #Htasrc_mid #Htadst_mid #Hnotendc 
462   cases rs00 in Htadst_mid;
463    [(* case rs empty *) #Htadst_mid % [ #cj #rs1 #H destruct (H) ]
464      #_ cases (Hcomp2 … Htasrc_mid Htadst_mid Hnotendc) -Hcomp2 
465      [2: * #x0 * #rs1 * #H destruct (H) ]
466     * #_ #Htc cases Htb #td * * #_ #Htd >Htasrc_mid in Hcurta_src; 
467     normalize in ⊢ (%→?); #H destruct (H)  
468     >Htd [2: %2 >Htc >nth_change_vec // cases (reverse sig ?) //]
469     >Htc * * >nth_change_vec // #Htbdst #_ #Htbelse
470      @(eq_vec … (niltape ?)) #i #Hi cases (decidable_eq_nat i dst) #Hidst
471       [ >Hidst >nth_change_vec // <Htbdst // cases (reverse sig ?) //
472       |@sym_eq @Htbelse @sym_not_eq //
473       ] 
474     |#cj0 #rs0 #Htadst_mid % [| #H destruct (H) ]
475      #cj #rs1 #H destruct (H) #Hcicj
476      cases (Hcomp2 … Htasrc_mid Htadst_mid Hnotendc) [ * #H destruct (H) ]
477      * #cj' * #rs0' * #Hcjrs0 destruct (Hcjrs0) -Hcomp2 #Hcomp2
478      lapply (Hcomp2 (or_intror ?? Hcicj)) -Hcomp2 #Htc
479      cases Htb #td * * #Htd #_ >Htasrc_mid in Hcurta_src; normalize in ⊢ (%→?);
480      #H destruct (H)
481      >(Htd ls ci (reverse ? xs) rs s ??? ls0 cj' (reverse ? xs) s rs0' (refl ??)) //
482      [| >Htc >nth_change_vec //
483      | #c0 #Hc0 @(Hnotstart c0) >Htasrc_mid 
484       cases (orb_true_l … Hc0) -Hc0 #Hc0
485       [@memb_append_l2 >(\P Hc0) @memb_hd
486       |@memb_append_l1 <(reverse_reverse …xs) @memb_reverse //
487       ]
488     | >Htc >nth_change_vec_neq [|@sym_not_eq // ] @nth_change_vec // ]
489     * * #_ #Htbdst #Htbelse %
490     [ @(eq_vec … (niltape ?)) #i #Hi cases (decidable_eq_nat i dst) #Hidst
491       [ >Hidst >nth_change_vec // >Htadst_mid >(Htbdst ls0 s (xs@cj'::rs0'))
492         [ cases xs //
493         | >nth_change_vec // ]
494       | >nth_change_vec_neq [|@sym_not_eq //]
495         <Htbelse [|@sym_not_eq // ]
496         >nth_change_vec_neq [|@sym_not_eq //]
497         cases (decidable_eq_nat i src) #Hisrc
498         [ >Hisrc >nth_change_vec // >Htasrc_mid //
499         | >nth_change_vec_neq [|@sym_not_eq //]
500           <(Htbelse i) [|@sym_not_eq // ]
501           >Htc >nth_change_vec_neq [|@sym_not_eq // ]
502           >nth_change_vec_neq [|@sym_not_eq // ] //
503         ]
504        ] 
505     | >Htc in Hcurtc; >nth_change_vec_neq [|@sym_not_eq //] 
506       >nth_change_vec // whd in ⊢ (??%?→?); 
507       #H destruct (H) cases (is_endc c) in Hcend;
508       normalize #H destruct (H) // ]
509     ]
510   ]
511 |#intape #outtape #ta * #Hcomp1 #Hcomp2 * #tb * * #Hc #Htb 
512  whd in ⊢ (%→?); #Hout >Hout >Htb whd
513  #ls #c_src #xs #end #rs #Hmid_src #Hnotend #Hend
514  lapply (current_to_midtape sig (nth dst ? intape (niltape ?)))
515  cases (current … (nth dst ? intape (niltape ?))) in Hcomp1;
516   [#Hcomp1 #_ %1 % [% | @Hcomp1 %2 %2 % ]
517   |#c_dst cases (true_or_false (c_src == c_dst)) #Hceq
518     [#_ #Hmid_dst cases (Hmid_dst c_dst (refl …)) -Hmid_dst
519      #ls_dst * #rs_dst #Hmid_dst %2
520      cases (comp_list … (xs@end::rs) rs_dst is_endc) #xs1 * #rsi * #rsj * * * 
521      #Hrs_src #Hrs_dst #Hnotendxs1 #Hneq %{ls_dst} %{rsj} >Hrs_dst in Hmid_dst; #Hmid_dst
522      cut (∃r1,rs1.rsi = r1::rs1) [@daemon] * #r1 * #rs1 #Hrs1 >Hrs1 in Hrs_src;
523      #Hrs_src >Hrs_src in Hmid_src; #Hmid_src <(\P Hceq) in Hmid_dst; #Hmid_dst
524      lapply (Hcomp2 ??????? Hmid_src Hmid_dst ?) 
525      [ #c0 #Hc0 cases (orb_true_l … Hc0) -Hc0 #Hc0
526        [ >(\P Hc0) @Hnotend @memb_hd | @Hnotendxs1 //]
527      | *
528        [ * #Hrsj #Hta %
529          [ >Hta in Hc; >nth_change_vec_neq [|@sym_not_eq //] >nth_change_vec //
530            #Hc lapply (Hc ? (refl ??)) #Hendr1
531            cut (xs = xs1)
532            [ lapply Hnotendxs1 lapply Hnotend lapply Hrs_src lapply xs1
533              -Hnotendxs1 -Hnotend -Hrs_src -xs1 elim xs
534              [ * normalize in ⊢ (%→?); //
535                #x2 #xs2 normalize in ⊢ (%→?); #Heq destruct (Heq) #_ #Hnotendxs1
536                lapply (Hnotendxs1 ? (memb_hd …)) >Hend #H destruct (H)
537              | #x2 #xs2 #IH *
538                [ normalize in ⊢ (%→?); #Heq destruct (Heq) #Hnotendc
539                  >Hnotendc in Hendr1; [| @memb_cons @memb_hd ]
540                  normalize in ⊢ (%→?); #H destruct (H)
541                | #x3 #xs3 normalize in ⊢ (%→?); #Heq destruct (Heq)
542                  #Hnotendc #Hnotendcxs1 @eq_f @IH
543                  [ @(cons_injective_r … Heq)
544                  | #c0 #Hc0 @Hnotendc cases (orb_true_l … Hc0) -Hc0 #Hc0
545                    [ >(\P Hc0) @memb_hd
546                    | @memb_cons @memb_cons // ]
547                  | #c #Hc @Hnotendcxs1 @memb_cons // ]
548                ]
549              ]
550            | #Hxsxs1 >Hmid_dst >Hxsxs1 % ]
551          | #rsj0 #c >Hrsj #Hrsj0 destruct (Hrsj0) ]
552        | * #cj * #rs2 * #Hrs2 #Hta lapply (Hta ?) 
553          [ cases (Hneq … Hrs1) /2/ #H %2 @(H ?? Hrs2) ]
554          -Hta #Hta >Hta in Hc; >nth_change_vec_neq [|@sym_not_eq //] 
555          >nth_change_vec // #Hc lapply (Hc ? (refl ??)) #Hendr1
556          (* lemmatize this proof *) cut (xs = xs1)
557          [ lapply Hnotendxs1 lapply Hnotend lapply Hrs_src lapply xs1
558            -Hnotendxs1 -Hnotend -Hrs_src -xs1 elim xs
559            [ * normalize in ⊢ (%→?); //
560              #x2 #xs2 normalize in ⊢ (%→?); #Heq destruct (Heq) #_ #Hnotendxs1
561              lapply (Hnotendxs1 ? (memb_hd …)) >Hend #H destruct (H)
562            | #x2 #xs2 #IH *
563              [ normalize in ⊢ (%→?); #Heq destruct (Heq) #Hnotendc
564                >Hnotendc in Hendr1; [| @memb_cons @memb_hd ]
565                normalize in ⊢ (%→?); #H destruct (H)
566              | #x3 #xs3 normalize in ⊢ (%→?); #Heq destruct (Heq)
567                #Hnotendc #Hnotendcxs1 @eq_f @IH
568                [ @(cons_injective_r … Heq)
569                | #c0 #Hc0 @Hnotendc cases (orb_true_l … Hc0) -Hc0 #Hc0
570                  [ >(\P Hc0) @memb_hd
571                  | @memb_cons @memb_cons // ]
572                | #c #Hc @Hnotendcxs1 @memb_cons // ]
573              ]
574            ]
575          | #Hxsxs1 >Hmid_dst >Hxsxs1 % //
576            #rsj0 #c #Hcrsj destruct (Hxsxs1 Hrs2 Hcrsj) @eq_f3 //
577            @eq_f3 // lapply (append_l2_injective ?????? Hrs_src) //
578            #Hendr1 destruct (Hendr1) % ]
579        ]
580      ]
581    (* STOP *)
582    |#Hcomp1 #Hsrc cases (Hsrc ? (refl ??)) -Hsrc #ls0 * #rs0 #Hdst 
583     @False_ind lapply (Hcomp1 ?) [%2 %1 %1 >Hmid_src normalize
584     @(not_to_not ??? (\Pf Hceq)) #H destruct //] #Hintape 
585     >Hintape in Hc; >Hmid_src #Hc lapply (Hc ? (refl …)) -Hc 
586     >(Hnotend c_src) // normalize #H destruct (H)   
587    ]
588   ]
589 ]
590 qed. 
591 *)
592
593 definition match_step ≝ λsrc,dst,sig,n,is_startc,is_endc.
594   compare src dst sig n is_endc ·
595     (ifTM ?? (inject_TM ? (test_char ? (λa.is_endc a == false)) n src)
596       (ifTM ?? (inject_TM ? (test_null ?) n src)
597         (single_finalTM ??
598           (parmove src dst sig n L is_startc · (inject_TM ? (move_r ?) n dst)))
599         (nop …) tc_true)
600       (nop …)
601       tc_true).
602       
603 definition R_match_step_false ≝ 
604   λsrc,dst,sig,n,is_endc.λint,outt: Vector (tape sig) (S n).
605   ∀ls,x,xs,end,rs.
606   nth src ? int (niltape ?) = midtape sig ls x (xs@end::rs) →
607   (∀c0. memb ? c0 (x::xs) = true → is_endc c0 = false) → is_endc end = true →
608    ((current sig (nth dst (tape sig) int (niltape sig)) = None ?) ∧ outt = int) ∨
609    (∃ls0,rs0. 
610     nth dst ? int (niltape ?) = midtape sig ls0 x (xs@rs0) ∧
611     ∀rsj,c. 
612     rs0 = c::rsj →
613     outt = change_vec ??
614            (change_vec ?? int (midtape sig (reverse ? xs@x::ls) end rs) src)
615            (midtape sig (reverse ? xs@x::ls0) c rsj) dst).
616
617 definition R_match_step_true ≝ 
618   λsrc,dst,sig,n,is_startc,is_endc.λint,outt: Vector (tape sig) (S n).
619   ∀s.current sig (nth src (tape sig) int (niltape sig)) = Some ? s → 
620   is_startc s = true → 
621   (∀c.c ∈ right ? (nth src (tape sig) int (niltape sig)) = true → is_startc c = false) →
622   current sig (nth dst (tape sig) int (niltape sig)) ≠ None ? ∧
623   (∀s1.current sig (nth dst (tape sig) int (niltape sig)) = Some ? s1 → s ≠ s1 →  
624    outt = change_vec ?? int 
625           (tape_move … (nth dst ? int (niltape ?)) (Some ? 〈s1,R〉)) dst ∧ is_endc s = false) ∧  
626   (∀ls,x,xs,ci,rs,ls0,rs0. 
627     nth src ? int (niltape ?) = midtape sig ls x (xs@ci::rs) →
628     nth dst ? int (niltape ?) = midtape sig ls0 x (xs@rs0) →
629     (∀c0. memb ? c0 (x::xs) = true → is_endc c0 = false) → 
630     (∀cj,rs1.rs0 = cj::rs1 → ci ≠ cj →
631      (outt = change_vec ?? int 
632            (tape_move … (nth dst ? int (niltape ?)) (Some ? 〈x,R〉)) dst ∧ is_endc ci = false)) ∧
633     (rs0 = [ ] →
634      outt = change_vec ??
635            (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) src)
636            (mk_tape sig (reverse ? xs@x::ls0) (None ?) [ ]) dst)).
637            
638 lemma sem_match_step :
639   ∀src,dst,sig,n,is_startc,is_endc.src ≠ dst → src < S n → dst < S n → 
640   match_step src dst sig n is_startc is_endc ⊨ 
641     [ inr ?? (inr ?? (inl … (inr ?? (inr ?? start_nop)))) : 
642       R_match_step_true src dst sig n is_startc is_endc, 
643       R_match_step_false src dst sig n is_endc ].
644 #src #dst #sig #n #is_startc #is_endc #Hneq #Hsrc #Hdst 
645 (* test_null versione multi? *)
646 @(acc_sem_seq_app sig n … (sem_compare src dst sig n is_endc Hneq Hsrc Hdst)
647     (acc_sem_if ? n … (sem_test_char_multi sig (λa.is_endc a == false) n src (le_S_S_to_le … Hsrc))
648       (acc_sem_if ? n … (sem_test_null sig (λa.is_endc a == false) n src (le_S_S_to_le … Hsrc))
649       
650       sem_seq … 
651         (sem_parmoveL ???? is_startc Hneq Hsrc Hdst) 
652         (sem_inject … dst (le_S_S_to_le … Hdst) (sem_move_r ? )))
653       (sem_nop …)))
654 [#ta #tb #tc * #Hcomp1 #Hcomp2 * #td * * * #c * #Hcurtc #Hcend #Htd >Htd -Htd
655  #Htb #s #Hcurta_src #Hstart #Hnotstart % [ %
656  [#Hdst_none @daemon 
657  | #s1 #Hcurta_dst #Hneqss1
658    lapply Htb lapply Hcurtc -Htb -Hcurtc >(?:tc=ta) 
659    [|@Hcomp1 %2 % % >Hcurta_src >Hcurta_dst @(not_to_not … Hneqss1) #H destruct (H) % ]
660    #Hcurtc * #te * * #_ #Hte >Hte [2: %1 %1 %{s} % //] 
661    whd in ⊢ (%→?); * * #_ #Htbdst #Htbelse %
662    [ @(eq_vec … (niltape ?)) #i #Hi cases (decidable_eq_nat i dst) #Hidst
663      [ >Hidst >nth_change_vec // cases (current_to_midtape … Hcurta_dst)
664        #ls * #rs #Hta_mid >(Htbdst … Hta_mid) >Hta_mid cases rs //
665      | >nth_change_vec_neq [|@sym_not_eq //] @sym_eq @Htbelse @sym_not_eq // ]
666    | >Hcurtc in Hcurta_src; #H destruct (H) cases (is_endc s) in Hcend;
667      normalize #H destruct (H) // ]
668    ]
669  |#ls #x #xs #ci #rs #ls0 #rs00 #Htasrc_mid #Htadst_mid #Hnotendc 
670   cases rs00 in Htadst_mid;
671    [(* case rs empty *) #Htadst_mid % [ #cj #rs1 #H destruct (H) ]
672      #_ cases (Hcomp2 … Htasrc_mid Htadst_mid Hnotendc) -Hcomp2 
673      [2: * #x0 * #rs1 * #H destruct (H) ]
674     * #_ #Htc cases Htb #td * * #_ #Htd >Htasrc_mid in Hcurta_src; 
675     normalize in ⊢ (%→?); #H destruct (H)  
676     >Htd [2: %2 >Htc >nth_change_vec // cases (reverse sig ?) //]
677     >Htc * * >nth_change_vec // #Htbdst #_ #Htbelse
678      @(eq_vec … (niltape ?)) #i #Hi cases (decidable_eq_nat i dst) #Hidst
679       [ >Hidst >nth_change_vec // <Htbdst // cases (reverse sig ?) //
680       |@sym_eq @Htbelse @sym_not_eq //
681       ] 
682     |#cj0 #rs0 #Htadst_mid % [| #H destruct (H) ]
683      #cj #rs1 #H destruct (H) #Hcicj
684      cases (Hcomp2 … Htasrc_mid Htadst_mid Hnotendc) [ * #H destruct (H) ]
685      * #cj' * #rs0' * #Hcjrs0 destruct (Hcjrs0) -Hcomp2 #Hcomp2
686      lapply (Hcomp2 (or_intror ?? Hcicj)) -Hcomp2 #Htc
687      cases Htb #td * * #Htd #_ >Htasrc_mid in Hcurta_src; normalize in ⊢ (%→?);
688      #H destruct (H)
689      >(Htd ls ci (reverse ? xs) rs s ??? ls0 cj' (reverse ? xs) s rs0' (refl ??)) //
690      [| >Htc >nth_change_vec //
691      | #c0 #Hc0 @(Hnotstart c0) >Htasrc_mid 
692       cases (orb_true_l … Hc0) -Hc0 #Hc0
693       [@memb_append_l2 >(\P Hc0) @memb_hd
694       |@memb_append_l1 <(reverse_reverse …xs) @memb_reverse //
695       ]
696     | >Htc >nth_change_vec_neq [|@sym_not_eq // ] @nth_change_vec // ]
697     * * #_ #Htbdst #Htbelse %
698     [ @(eq_vec … (niltape ?)) #i #Hi cases (decidable_eq_nat i dst) #Hidst
699       [ >Hidst >nth_change_vec // >Htadst_mid >(Htbdst ls0 s (xs@cj'::rs0'))
700         [ cases xs //
701         | >nth_change_vec // ]
702       | >nth_change_vec_neq [|@sym_not_eq //]
703         <Htbelse [|@sym_not_eq // ]
704         >nth_change_vec_neq [|@sym_not_eq //]
705         cases (decidable_eq_nat i src) #Hisrc
706         [ >Hisrc >nth_change_vec // >Htasrc_mid //
707         | >nth_change_vec_neq [|@sym_not_eq //]
708           <(Htbelse i) [|@sym_not_eq // ]
709           >Htc >nth_change_vec_neq [|@sym_not_eq // ]
710           >nth_change_vec_neq [|@sym_not_eq // ] //
711         ]
712        ] 
713     | >Htc in Hcurtc; >nth_change_vec_neq [|@sym_not_eq //] 
714       >nth_change_vec // whd in ⊢ (??%?→?); 
715       #H destruct (H) cases (is_endc c) in Hcend;
716       normalize #H destruct (H) // ]
717     ]
718   ]
719 |#intape #outtape #ta * #Hcomp1 #Hcomp2 * #tb * * #Hc #Htb 
720  whd in ⊢ (%→?); #Hout >Hout >Htb whd
721  #ls #c_src #xs #end #rs #Hmid_src #Hnotend #Hend
722  lapply (current_to_midtape sig (nth dst ? intape (niltape ?)))
723  cases (current … (nth dst ? intape (niltape ?))) in Hcomp1;
724   [#Hcomp1 #_ %1 % [% | @Hcomp1 %2 %2 % ]
725   |#c_dst cases (true_or_false (c_src == c_dst)) #Hceq
726     [#_ #Hmid_dst cases (Hmid_dst c_dst (refl …)) -Hmid_dst
727      #ls_dst * #rs_dst #Hmid_dst %2
728      cases (comp_list … (xs@end::rs) rs_dst is_endc) #xs1 * #rsi * #rsj * * * 
729      #Hrs_src #Hrs_dst #Hnotendxs1 #Hneq %{ls_dst} %{rsj} >Hrs_dst in Hmid_dst; #Hmid_dst
730      cut (∃r1,rs1.rsi = r1::rs1) [@daemon] * #r1 * #rs1 #Hrs1 >Hrs1 in Hrs_src;
731      #Hrs_src >Hrs_src in Hmid_src; #Hmid_src <(\P Hceq) in Hmid_dst; #Hmid_dst
732      lapply (Hcomp2 ??????? Hmid_src Hmid_dst ?) 
733      [ #c0 #Hc0 cases (orb_true_l … Hc0) -Hc0 #Hc0
734        [ >(\P Hc0) @Hnotend @memb_hd | @Hnotendxs1 //]
735      | *
736        [ * #Hrsj #Hta %
737          [ >Hta in Hc; >nth_change_vec_neq [|@sym_not_eq //] >nth_change_vec //
738            #Hc lapply (Hc ? (refl ??)) #Hendr1
739            cut (xs = xs1)
740            [ lapply Hnotendxs1 lapply Hnotend lapply Hrs_src lapply xs1
741              -Hnotendxs1 -Hnotend -Hrs_src -xs1 elim xs
742              [ * normalize in ⊢ (%→?); //
743                #x2 #xs2 normalize in ⊢ (%→?); #Heq destruct (Heq) #_ #Hnotendxs1
744                lapply (Hnotendxs1 ? (memb_hd …)) >Hend #H destruct (H)
745              | #x2 #xs2 #IH *
746                [ normalize in ⊢ (%→?); #Heq destruct (Heq) #Hnotendc
747                  >Hnotendc in Hendr1; [| @memb_cons @memb_hd ]
748                  normalize in ⊢ (%→?); #H destruct (H)
749                | #x3 #xs3 normalize in ⊢ (%→?); #Heq destruct (Heq)
750                  #Hnotendc #Hnotendcxs1 @eq_f @IH
751                  [ @(cons_injective_r … Heq)
752                  | #c0 #Hc0 @Hnotendc cases (orb_true_l … Hc0) -Hc0 #Hc0
753                    [ >(\P Hc0) @memb_hd
754                    | @memb_cons @memb_cons // ]
755                  | #c #Hc @Hnotendcxs1 @memb_cons // ]
756                ]
757              ]
758            | #Hxsxs1 >Hmid_dst >Hxsxs1 % ]
759          | #rsj0 #c >Hrsj #Hrsj0 destruct (Hrsj0) ]
760        | * #cj * #rs2 * #Hrs2 #Hta lapply (Hta ?) 
761          [ cases (Hneq … Hrs1) /2/ #H %2 @(H ?? Hrs2) ]
762          -Hta #Hta >Hta in Hc; >nth_change_vec_neq [|@sym_not_eq //] 
763          >nth_change_vec // #Hc lapply (Hc ? (refl ??)) #Hendr1
764          (* lemmatize this proof *) cut (xs = xs1)
765          [ lapply Hnotendxs1 lapply Hnotend lapply Hrs_src lapply xs1
766            -Hnotendxs1 -Hnotend -Hrs_src -xs1 elim xs
767            [ * normalize in ⊢ (%→?); //
768              #x2 #xs2 normalize in ⊢ (%→?); #Heq destruct (Heq) #_ #Hnotendxs1
769              lapply (Hnotendxs1 ? (memb_hd …)) >Hend #H destruct (H)
770            | #x2 #xs2 #IH *
771              [ normalize in ⊢ (%→?); #Heq destruct (Heq) #Hnotendc
772                >Hnotendc in Hendr1; [| @memb_cons @memb_hd ]
773                normalize in ⊢ (%→?); #H destruct (H)
774              | #x3 #xs3 normalize in ⊢ (%→?); #Heq destruct (Heq)
775                #Hnotendc #Hnotendcxs1 @eq_f @IH
776                [ @(cons_injective_r … Heq)
777                | #c0 #Hc0 @Hnotendc cases (orb_true_l … Hc0) -Hc0 #Hc0
778                  [ >(\P Hc0) @memb_hd
779                  | @memb_cons @memb_cons // ]
780                | #c #Hc @Hnotendcxs1 @memb_cons // ]
781              ]
782            ]
783          | #Hxsxs1 >Hmid_dst >Hxsxs1 % //
784            #rsj0 #c #Hcrsj destruct (Hxsxs1 Hrs2 Hcrsj) @eq_f3 //
785            @eq_f3 // lapply (append_l2_injective ?????? Hrs_src) //
786            #Hendr1 destruct (Hendr1) % ]
787        ]
788      ]
789    (* STOP *)
790    |#Hcomp1 #Hsrc cases (Hsrc ? (refl ??)) -Hsrc #ls0 * #rs0 #Hdst 
791     @False_ind lapply (Hcomp1 ?) [%2 %1 %1 >Hmid_src normalize
792     @(not_to_not ??? (\Pf Hceq)) #H destruct //] #Hintape 
793     >Hintape in Hc; >Hmid_src #Hc lapply (Hc ? (refl …)) -Hc 
794     >(Hnotend c_src) // normalize #H destruct (H)   
795    ]
796   ]
797 ]
798 qed. 
799
800 definition match_m ≝ λsrc,dst,sig,n,is_startc,is_endc.
801   whileTM … (match_step src dst sig n is_startc is_endc) 
802     (inr ?? (inr ?? (inl … (inr ?? start_nop)))).
803
804 definition R_match_m ≝ 
805   λsrc,dst,sig,n,is_startc,is_endc.λint,outt: Vector (tape sig) (S n).
806 (*  (current sig (nth dst (tape sig) int (niltape sig)) = None ? → outt = int) ∧ *)
807   ∀ls,x,xs,end,rs.
808   nth src ? int (niltape ?) = midtape sig ls x (xs@end::rs) →
809   (∀c0. memb ? c0 (x::xs) = true → is_endc c0 = false) → is_endc end = true →
810   (current sig (nth dst (tape sig) int (niltape sig)) = None ? → outt = int) ∧
811   (is_startc x = true →
812    (∀ls0,x0,rs0.
813     nth dst ? int (niltape ?) = midtape sig ls0 x0 rs0 →
814     (∃l,l1.x0::rs0 = l@x::xs@l1 ∧
815      ∀cj,l2.l1=cj::l2 →
816      outt = change_vec ?? 
817             (change_vec ?? int (midtape sig (reverse ? xs@x::ls) end rs) src)
818             (midtape sig ((reverse ? (l@x::xs))@ls0) cj l2) dst) ∨
819     ∀l,l1.x0::rs0 ≠ l@x::xs@l1)).
820
821 (*
822 definition R_match_m ≝ 
823   λi,j,sig,n,is_startc,is_endc.λint,outt: Vector (tape sig) (S n).
824   (((∃x.current ? (nth i ? int (niltape ?)) = Some ? x ∧ is_endc x = true) ∨
825     current ? (nth i ? int (niltape ?)) = None ? ∨
826     current ? (nth j ? int (niltape ?)) = None ?) → outt = int) ∧
827   (∀ls,x,xs,ci,rs,ls0,x0,rs0.
828     (∀x. is_startc x ≠ is_endc x) → 
829     is_startc x = true → is_endc ci = true → 
830     (∀z. memb ? z (x::xs) = true → is_endc x = false) →
831     nth i ? int (niltape ?) = midtape sig ls x (xs@ci::rs) →
832     nth j ? int (niltape ?) = midtape sig ls0 x0 rs0 →
833     (∃l,l1.x0::rs0 = l@x::xs@l1 ∧
834      ∀cj,l2.l1=cj::l2 →
835      outt = change_vec ?? 
836             (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) i)
837             (midtape sig ((reverse ? (l@x::xs))@ls0) cj l2) j) ∨
838     ∀l,l1.x0::rs0 ≠ l@x::xs@l1).
839 *)
840
841 (*
842 axiom sub_list_dec: ∀A.∀l,ls:list A. 
843   ∃l1,l2. l = l1@ls@l2 ∨ ∀l1,l2. l ≠ l1@ls@l2.
844 *)
845
846 lemma wsem_match_m : ∀src,dst,sig,n,is_startc,is_endc.
847 src ≠ dst → src < S n → dst < S n → 
848   match_m src dst sig n is_startc is_endc ⊫ R_match_m src dst sig n is_startc is_endc.
849 #src #dst #sig #n #is_startc #is_endc #Hneq #Hsrc #Hdst #ta #k #outc #Hloop
850 lapply (sem_while … (sem_match_step src dst sig n is_startc is_endc Hneq Hsrc Hdst) … Hloop) //
851 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
852 [ #tc #Hfalse #ls #x #xs #end #rs #Hmid_src #Hnotend #Hend
853   cases (Hfalse … Hmid_src Hnotend Hend) -Hfalse 
854   [(* current dest = None *) * #Hcur_dst #Houtc %
855     [#_ >Houtc //
856     |#Hstart #ls0 #x0 #rs0 #Hmid_dst >Hmid_dst in Hcur_dst; 
857      normalize in ⊢ (%→?); #H destruct (H)
858     ]
859   |* #ls0 * #rs0 * #Hmid_dst #HFalse %
860     [ >Hmid_dst normalize in ⊢ (%→?); #H destruct (H)
861     | #Hstart #ls1 #x1 #rs1 >Hmid_dst #H destruct (H)
862      %1 %{[ ]} %{rs0} % [%] #cj #l2 #Hnotnil 
863      >reverse_cons >associative_append @(HFalse ?? Hnotnil)
864     ]
865   ]
866 |#ta #tb #tc #Htrue #Hstar #IH #Hout lapply (IH Hout) -IH -Hout #IH whd
867  #ls #x #xs #end #rs #Hmid_src #Hnotend #Hend 
868  lapply (refl ? (current ? (nth dst ? ta (niltape ?))))
869  cases (current ? (nth dst ? ta (niltape ?))) in ⊢ (???%→?); 
870   [#Hmid_dst % 
871     [#_ whd in Htrue; >Hmid_src in Htrue; #Htrue
872      cases (Htrue x (refl … ) Hstart ?) -Htrue [2: @daemon]
873      * #Htb #_ #_ >Htb in IH; // #IH  
874      cases (IH ls x xs end rs Hmid_src Hstart Hnotend Hend)
875      #Hcur_outc #_ @Hcur_outc //
876     |#ls0 #x0 #rs0 #Hmid_dst2 >Hmid_dst2 in Hmid_dst; normalize in ⊢ (%→?); 
877      #H destruct (H)
878     ]
879   | #c #Hcurta_dst % [ >Hcurta_dst #H destruct (H) ]
880     #ls0 #x0 #rs0 #Hmid_dst >Hmid_dst in Hcurta_dst; normalize in ⊢ (%→?);
881     #H destruct (H) whd in Htrue; >Hmid_src in Htrue; #Htrue
882     cases (Htrue x (refl …) Hstart ?) -Htrue
883     [2: #z #membz @daemon (*aggiungere l'ipotesi*)]
884     cases (true_or_false (x==c)) #eqx
885     [ #_ #Htrue cases (comp_list ? (xs@end::rs) rs0 is_endc)
886       #x1 * #tl1 * #tl2 * * * #Hxs #Hrs0 #Hnotendx1
887       cases tl1 in Hxs; 
888       [>append_nil #Hx1 @daemon (* absurd by Hx1 e notendx1 *)]
889       #ci -tl1 #tl1 #Hxs #H cases (H … (refl … ))
890       [(* this is absurd, since Htrue conlcudes is_endc ci =false *)
891        #Hend_ci @daemon (* lapply(Htrue … (refl …)) -Htrue *)
892       |#Hcomp lapply (Htrue ls x x1 ci tl1 ls0 tl2 ???)
893        [ #c0 #Hc0 cases (orb_true_l … Hc0) #Hc0
894          [ @Hnotend >(\P Hc0) @memb_hd
895          | @Hnotendx1 // ]
896        | >Hmid_dst >Hrs0 >(\P eqx) %
897        | >Hxs %
898        | *  cases tl2 in Hrs0;
899          [ >append_nil #Hrs0 #_ #Htb whd in IH;
900            lapply (IH ls x x1 ci tl1 ? Hstart ??)
901            [
902            |
903            | >Htb // >nth_change_vec_neq [|@sym_not_eq //] >nth_change_vec //
904           
905         >Hrs0 in Hmid_dst; #Hmid_dst
906        cases(Htrue ???????? Hmid_dst) -Htrue #Htb #Hendx
907        whd in IH;
908        cases(IH ls x xs end rs ? Hstart Hnotend Hend)
909        [* #H1 #H2 >Htb in H1; >nth_change_vec // 
910         >Hmid_dst cases rs0 [2: #a #tl normalize in ⊢ (%→?); #H destruct (H)] 
911         #_ %2 @daemon (* si dimostra *)
912        |@daemon
913        |>Htb >nth_change_vec_neq [|@sym_not_eq //] @Hmid_src
914        ] 
915     ]
916   ]
917 ]
918 qed.
919