]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/multi_universal/match.ma
match
[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 match_step ≝ λsrc,dst,sig,n,is_startc,is_endc.
361   compare src dst sig n is_endc ·
362     (ifTM ?? (inject_TM ? (test_char ? (λa.is_endc a == false)) n src)
363       (single_finalTM ??
364         (parmove src dst sig n L is_startc · (inject_TM ? (move_r ?) n dst)))
365       (nop …)
366       tc_true).
367       
368 definition Rtc_multi_true ≝ 
369   λalpha,test,n,i.λt1,t2:Vector ? (S n).
370    (∃c. current alpha (nth i ? t1 (niltape ?)) = Some ? c ∧ test c = true) ∧ t2 = t1.
371    
372 definition Rtc_multi_false ≝ 
373   λalpha,test,n,i.λt1,t2:Vector ? (S n).
374     (∀c. current alpha (nth i ? t1 (niltape ?)) = Some ? c → test c = false) ∧ t2 = t1.
375
376 definition R_match_step_false ≝ 
377   λsrc,dst,sig,n,is_endc.λint,outt: Vector (tape sig) (S n).
378   ∀ls,x,xs,end,rs.
379   nth src ? int (niltape ?) = midtape sig ls x (xs@end::rs) →
380   (∀c0. memb ? c0 (x::xs) = true → is_endc c0 = false) → is_endc end = true →
381    ((current sig (nth dst (tape sig) int (niltape sig)) = None ? ) ∧ outt = int) ∨
382    (∃ls0,rs0. 
383     nth dst ? int (niltape ?) = midtape sig ls0 x (xs@rs0) ∧
384     ∀rsj,c. 
385     rs0 = c::rsj →
386     outt = change_vec ??
387            (change_vec ?? int (midtape sig (reverse ? xs@x::ls) end rs) src)
388            (midtape sig (reverse ? xs@x::ls0) c rsj) dst).
389 (*  
390 definition R_match_step_false ≝  
391   λsrc,dst,sig,n,is_endc.λint,outt: Vector (tape sig) (S n).
392    (((∃x.current ? (nth src ? int (niltape ?)) = Some ? x ∧ is_endc x = true) ∨
393      current sig (nth src (tape sig) int (niltape sig)) = None ? ∨
394      current sig (nth dst (tape sig) int (niltape sig)) = None ? ) ∧ outt = int) ∨
395    (∃ls,ls0,rs,rs0,x,xs. 
396     nth src ? int (niltape ?) = midtape sig ls x (xs@rs) ∧ is_endc x = false ∧
397     nth dst ? int (niltape ?) = midtape sig ls0 x (xs@rs0) ∧
398     ∀rsi,rsj,end,c. 
399     rs = end::rsi → rs0 = c::rsj →
400     (∀c0. memb ? c0 (x::xs) = true → is_endc c0 = false) ∧ is_endc end = true ∧
401     nth dst ? int (niltape ?) = midtape sig ls0 x (xs@c::rsj) ∧
402     outt = change_vec ??
403            (change_vec ?? int (midtape sig (reverse ? xs@x::ls) end rsi) src)
404            (midtape sig (reverse ? xs@x::ls0) c rsj) dst).
405 *)
406
407 definition R_match_step_true ≝ 
408   λsrc,dst,sig,n,is_startc,is_endc.λint,outt: Vector (tape sig) (S n).
409   ∀s.current sig (nth src (tape sig) int (niltape sig)) = Some ? s → 
410   is_startc s = true → 
411   (∀c.c ∈ right ? (nth src (tape sig) int (niltape sig)) = true → is_startc c = false) →
412   (∀s1.current sig (nth dst (tape sig) int (niltape sig)) = Some ? s1 → s ≠ s1 →  
413    outt = change_vec ?? int 
414           (tape_move … (nth dst ? int (niltape ?)) (Some ? 〈s1,R〉)) dst ∧ is_endc s = false) ∧  
415   (∀ls,x,xs,ci,rs,ls0,cj,rs0. 
416     nth src ? int (niltape ?) = midtape sig ls x (xs@ci::rs) →
417     nth dst ? int (niltape ?) = midtape sig ls0 x (xs@cj::rs0) → ci ≠ cj → 
418     (∀c0. memb ? c0 (x::xs) = true → is_endc c0 = false) → 
419     outt = change_vec ?? int 
420            (tape_move … (nth dst ? int (niltape ?)) (Some ? 〈x,R〉)) dst ∧ is_endc ci = false).
421     
422 lemma sem_test_char_multi :
423   ∀alpha,test,n,i.i ≤ n → 
424   inject_TM ? (test_char ? test) n i ⊨ 
425   [ tc_true : Rtc_multi_true alpha test n i, Rtc_multi_false alpha test n i ].
426 #alpha #test #n #i #Hin #int
427 cases (acc_sem_inject … Hin (sem_test_char alpha test) int)
428 #k * #outc * * #Hloop #Htrue #Hfalse %{k} %{outc} % [ %
429 [ @Hloop
430 | #Hqtrue lapply (Htrue Hqtrue) * * * #c *
431   #Hcur #Htestc #Hnth_i #Hnth_j %
432   [ %{c} % //
433   | @(eq_vec … (niltape ?)) #i0 #Hi0
434     cases (decidable_eq_nat i0 i) #Hi0i
435     [ >Hi0i @Hnth_i
436     | @sym_eq @Hnth_j @sym_not_eq // ] ] ]
437 | #Hqfalse lapply (Hfalse Hqfalse) * * #Htestc #Hnth_i #Hnth_j %
438   [ @Htestc
439   | @(eq_vec … (niltape ?)) #i0 #Hi0
440     cases (decidable_eq_nat i0 i) #Hi0i
441     [ >Hi0i @Hnth_i
442     | @sym_eq @Hnth_j @sym_not_eq // ] ] ]
443 qed.
444
445 axiom comp_list: ∀S:DeqSet. ∀l1,l2:list S.∀is_endc. ∃l,tl1,tl2. 
446   l1 = l@tl1 ∧ l2 = l@tl2 ∧ (∀c.c ∈ l = true → is_endc c = false) ∧
447   ∀a,tla. tl1 = a::tla → is_endc a = true ∨ (∀b,tlb.tl2 = b::tlb → a≠b).
448   
449 axiom daemon : ∀X:Prop.X.
450
451 lemma sem_match_step :
452   ∀src,dst,sig,n,is_startc,is_endc.src ≠ dst → src < S n → dst < S n → 
453   match_step src dst sig n is_startc is_endc ⊨ 
454     [ inr ?? (inr ?? (inl … (inr ?? start_nop))) : 
455       R_match_step_true src dst sig n is_startc is_endc, 
456       R_match_step_false src dst sig n is_endc ].
457 #src #dst #sig #n #is_startc #is_endc #Hneq #Hsrc #Hdst 
458 @(acc_sem_seq_app sig n … (sem_compare src dst sig n is_endc Hneq Hsrc Hdst)
459     (acc_sem_if ? n … (sem_test_char_multi sig (λa.is_endc a == false) n src (le_S_S_to_le … Hsrc))
460       (sem_seq … 
461         (sem_parmoveL ???? is_startc Hneq Hsrc Hdst) 
462         (sem_inject … dst (le_S_S_to_le … Hdst) (sem_move_r ? )))
463       (sem_nop …)))
464 [#ta #tb #tc * #Hcomp1 #Hcomp2 * #td * * * #c * #Hcurtc #Hcend #Htd >Htd -Htd
465  #Htb #s #Hcurta_src #Hstart #Hnotstart %
466  [ #s1 #Hcurta_dst #Hneqss1
467    lapply Htb lapply Hcurtc -Htb -Hcurtc >(?:tc=ta) 
468    [|@Hcomp1 %2 % % >Hcurta_src >Hcurta_dst @(not_to_not … Hneqss1) #H destruct (H) % ]
469    #Hcurtc * #te * * #_ #Hte >Hte // whd in ⊢ (%→?); * * #_ #Htbdst #Htbelse %
470    [ @(eq_vec … (niltape ?)) #i #Hi cases (decidable_eq_nat i dst) #Hidst
471      [ >Hidst >nth_change_vec // cases (current_to_midtape … Hcurta_dst)
472        #ls * #rs #Hta_mid >(Htbdst … Hta_mid) >Hta_mid cases rs //
473      | >nth_change_vec_neq [|@sym_not_eq //] @sym_eq @Htbelse @sym_not_eq // ]
474    | >Hcurtc in Hcurta_src; #H destruct (H) cases (is_endc s) in Hcend;
475      normalize #H destruct (H) // ]
476  |#ls #x #xs #ci #rs #ls0 #cj #rs0 #Htasrc_mid #Htadst_mid #Hcicj #Hnotendc
477   cases (Hcomp2 … Htasrc_mid Htadst_mid Hnotendc) [ * #H destruct (H) ]
478   * #cj' * #rs0' * #Hcjrs0 destruct (Hcjrs0) -Hcomp2 #Hcomp2
479   lapply (Hcomp2 (or_intror ?? Hcicj)) -Hcomp2 #Htc
480   cases Htb #td * * #Htd #_ >Htasrc_mid in Hcurta_src; normalize in ⊢ (%→?);
481   #H destruct (H)
482   >(Htd ls ci (reverse ? xs) rs s ??? ls0 cj' (reverse ? xs) s rs0' (refl ??)) //
483   [| >Htc >nth_change_vec //
484    | #c0 #Hc0 @(Hnotstart c0) >Htasrc_mid 
485      cases (orb_true_l … Hc0) -Hc0 #Hc0
486     [@memb_append_l2 >(\P Hc0) @memb_hd
487     |@memb_append_l1 <(reverse_reverse …xs) @memb_reverse //
488     ]
489    | >Htc >nth_change_vec_neq [|@sym_not_eq // ] @nth_change_vec // ]
490   * * #_ #Htbdst #Htbelse %
491   [ @(eq_vec … (niltape ?)) #i #Hi cases (decidable_eq_nat i dst) #Hidst
492      [ >Hidst >nth_change_vec // >Htadst_mid >(Htbdst ls0 s (xs@cj'::rs0'))
493        [ cases xs //
494        | >nth_change_vec // ]
495      | >nth_change_vec_neq [|@sym_not_eq //]
496        <Htbelse [|@sym_not_eq // ]
497        >nth_change_vec_neq [|@sym_not_eq //]
498        cases (decidable_eq_nat i src) #Hisrc
499        [ >Hisrc >nth_change_vec // >Htasrc_mid //
500        | >nth_change_vec_neq [|@sym_not_eq //]
501          <(Htbelse i) [|@sym_not_eq // ]
502          >Htc >nth_change_vec_neq [|@sym_not_eq // ]
503          >nth_change_vec_neq [|@sym_not_eq // ] //
504        ]
505      ]
506   | >Htc in Hcurtc; >nth_change_vec_neq [|@sym_not_eq //] 
507      >nth_change_vec // whd in ⊢ (??%?→?); 
508      #H destruct (H) cases (is_endc c) in Hcend;
509      normalize #H destruct (H) // ]
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 #Hsrc %2
583      %1 % 
584       [% % %{c_src} % // lapply (Hc c_src) -Hc >Hcomp1
585        [| %2 % % @(not_to_not ??? (\Pf Hceq)) #H destruct (H) // ]
586        cases (is_endc c_src) //
587        >Hsrc #Hc lapply (Hc (refl ??)) normalize #H destruct (H)
588       |@Hcomp1 %2 %1 %1 @(not_to_not ??? (\Pf Hceq)) #H destruct (H) //
589       ]
590     ]
591   ]
592
593 qed.
594
595 #intape #outtape #ta * #Hcomp1 #Hcomp2 * #tb * * #Hc #Htb 
596  whd in ⊢ (%→?); #Hout >Hout >Htb whd
597  lapply (current_to_midtape sig (nth src ? intape (niltape ?)))
598  cases (current … (nth src ? intape (niltape ?))) in Hcomp1; 
599   [#Hcomp1 #_ %1 % [%1 %2 // | @Hcomp1 %2 %1 %2 %]
600   |#c_src lapply (current_to_midtape sig (nth dst ? intape (niltape ?)))
601    cases (current … (nth dst ? intape (niltape ?))) 
602     [#_ #Hcomp1 #_ %1 % [%2 % | @Hcomp1 %2 % % % #H destruct (H)]
603     |#c_dst cases (true_or_false (c_src == c_dst)) #Hceq
604       [#Hmid_dst cases (Hmid_dst c_dst (refl …)) -Hmid_dst
605        #ls_dst * #rs_dst #Hmid_dst #Hcomp1
606        #Hmid_src cases (Hmid_src c_src (refl …)) -Hmid_src
607        #ls_src * #rs_src #Hmid_src
608        cases (true_or_false (is_endc c_src)) #Hc_src
609        [ % % [ % % %{c_src} % // | @Hcomp1 % %{c_src} % // ]
610        | %2 cases (comp_list … rs_src rs_dst is_endc) #xs * #rsi * #rsj * * * 
611          #Hrs_src #Hrs_dst #Hnotendc #Hneq    
612          %{ls_src} %{ls_dst} %{rsi} %{rsj} %{c_src} %{xs} % 
613            [% [% // <Hrs_src //|<Hrs_dst >(\P Hceq) // ]]     
614          #rsi0 #rsj0 #end #c #Hend #Hc_dst
615          >Hrs_src in Hmid_src; >Hend #Hmid_src
616          >Hrs_dst in Hmid_dst; >Hc_dst <(\P Hceq) #Hmid_dst
617          cut (is_endc end = true ∨ end ≠ c)
618          [cases (Hneq … Hend) /2/ -Hneq #Hneq %2 @(Hneq … Hc_dst) ] #Hneq
619          lapply (Hcomp2 … Hmid_src Hmid_dst ? Hneq)
620           [#c0 #Hc0 cases (orb_true_l … Hc0) -Hc0 #Hc0
621             [ >(\P Hc0) //
622             | @Hnotendc // ] 
623           ]
624          -Hcomp2 #Hcomp2 <Hcomp2
625          % // % [ 
626            >Hcomp2 in Hc; >nth_change_vec_neq [|@sym_not_eq //]
627             >nth_change_vec // #H lapply (H ? (refl …)) 
628             cases (is_endc end) [|normalize #H destruct (H) ]
629             #_ % // #c0 #Hc0 cases (orb_true_l … Hc0) -Hc0 #Hc0
630               [ >(\P Hc0) // | @Hnotendc // ]
631          |@Hmid_dst] 
632          ]
633       |#_ #Hcomp1 #Hsrc cases (Hsrc ? (refl ??)) -Hsrc #ls * #rs #Hsrc
634        %1 % 
635         [% % %{c_src} % // lapply (Hc c_src) -Hc >Hcomp1
636          [| %2 % % @(not_to_not ??? (\Pf Hceq)) #H destruct (H) // ]
637          cases (is_endc c_src) //
638          >Hsrc #Hc lapply (Hc (refl ??)) normalize #H destruct (H)
639         |@Hcomp1 %2 %1 %1 @(not_to_not ??? (\Pf Hceq)) #H destruct (H) //
640         ]
641       ]
642     ]
643   ] 
644 qed.
645
646 definition match_m ≝ λsrc,dst,sig,n,is_startc,is_endc.
647   whileTM … (match_step src dst sig n is_startc is_endc) 
648     (inr ?? (inr ?? (inl … (inr ?? start_nop)))).
649
650 definition R_match_m ≝ 
651   λi,j,sig,n,is_startc,is_endc.λint,outt: Vector (tape sig) (S n).
652   (((∃x.current ? (nth i ? int (niltape ?)) = Some ? x ∧ is_endc x = true) ∨
653     current ? (nth i ? int (niltape ?)) = None ? ∨
654     current ? (nth j ? int (niltape ?)) = None ?) → outt = int) ∧
655   (∀ls,x,xs,ci,rs,ls0,x0,rs0.
656     (∀x. is_startc x ≠ is_endc x) → 
657     is_startc x = true → is_endc ci = true → 
658     (∀z. memb ? z (x::xs) = true → is_endc x = false) →
659     nth i ? int (niltape ?) = midtape sig ls x (xs@ci::rs) →
660     nth j ? int (niltape ?) = midtape sig ls0 x0 rs0 →
661     (∃l,l1.x0::rs0 = l@x::xs@l1 ∧
662      ∀cj,l2.l1=cj::l2 →
663      outt = change_vec ?? 
664             (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) i)
665             (midtape sig ((reverse ? (l@x::xs))@ls0) cj l2) j) ∨
666     ∀l,l1.x0::rs0 ≠ l@x::xs@l1).
667
668 (*
669 axiom sub_list_dec: ∀A.∀l,ls:list A. 
670   ∃l1,l2. l = l1@ls@l2 ∨ ∀l1,l2. l ≠ l1@ls@l2.
671 *)
672
673 lemma wsem_match_m : ∀src,dst,sig,n,is_startc,is_endc.
674 src ≠ dst → src < S n → dst < S n → 
675   match_m src dst sig n is_startc is_endc ⊫ R_match_m src dst sig n is_startc is_endc.
676 #src #dst #sig #n #is_startc #is_endc #Hneq #Hsrc #Hdst #ta #k #outc #Hloop
677 lapply (sem_while … (sem_match_step src dst sig n is_startc is_endc Hneq Hsrc Hdst) … Hloop) //
678 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
679 [ #tc whd in ⊢ (%→%); * 
680   [ * * [ *
681     [ * #cur_src * #H1 #H2 #Houtc %   
682       [ #_ @Houtc
683       | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hdiff #Hstartc #Hendc #Hnotend #Hnthi 
684         @False_ind
685         >Hnthi in H1; whd in ⊢ (??%?→?); #H destruct (H) cases (Hdiff cur_src)
686         #Habs @Habs //
687       ]
688     | #Hci #Houtc %
689       [ #_ @Houtc
690       | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hdiff #Hstartc #Hendc #Hnotend 
691         #Hnthi >Hnthi in Hci; normalize in ⊢ (%→?); #H destruct (H) ] ]
692     | #Hcj #Houtc %
693       [ #_ @Houtc
694       | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hdiff #Hstartc #Hendc #_ #_ #Hnthj >Hnthj in Hcj;
695         normalize in ⊢ (%→?); #H destruct (H) ] 
696     ]
697   |* #ls * #ls0 * #rs * #rs0 * #x0 * #xs * * * #Hsrc #Hx0 #Hdst #H %
698     [>Hsrc * 
699       [* [* #x * whd in ⊢ (??%?→?); #Habs destruct (Habs) >Hx0 #Habs destruct (Habs)
700                 |whd in ⊢ (??%?→?); #Habs destruct (Habs) ]
701          |>Hdst whd in ⊢ (??%?→?); #Habs destruct (Habs)  ]
702     |#ls1 #x1 #xs1 #ci #rsi #ls2 #x2 #rs2 
703      #Hdiff #Hstart #Hend #Hnotend 
704      >Hsrc #Hsrc1 destruct (Hsrc1) >Hdst #Hdst1 destruct (Hdst1) 
705      %1 %{[ ]} %{rs0} normalize in ⊢ (%→?); #Heq #cj #l2 #Hl1
706      cut (xs=xs1) 
707        [@(append_l1_injective_r … rs0 rs0 (refl …)) @(cons_injective_r …Heq)] 
708      #eqxs <eqxs 
709      whd in match (append ? [ ] (x2::xs)); >reverse_cons >associative_append
710      normalize in match (append ? [x2] ls2);
711      cases (H rsi l2 ci cj ? Hl1) 
712       [* #_ #_ #H3 @H3  
713       |>eqxs in e0; #e0 @(append_l2_injective … e0) // 
714       ] 
715     ]
716   ]
717 |#tc #td #te #Hd #Hstar #IH #He lapply (IH He) -IH *
718     #IH1 #IH2 % [@IH1]
719     
720     
721      cases (comp_list ? (x1::xs1@ci::rsi) (x2::rs2) is_endc)
722      #l * #tl1 * #tl2 * * * #H1 #H2 #H3 #H4
723