]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/multi_universal/match.ma
8f525c9f4f7188df65acfeb91114d4d1b072b18c
[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 rs0) 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 #cj #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 #cj #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 #cj #rs0 #Hnthi >Hnthi in Hci;
253       normalize in ⊢ (%→?); #H destruct (H) ] ]
254   | #Hcj #Houtc %
255     [ #_ @Houtc
256     | #ls #x #xs #ci #rs #ls0 #cj #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 #cj #rs0 cases xs
263       [ #Hnthi #Hnthj #Hnotendc #Hcicj >IH1 
264         [ >Hd @eq_f3 // 
265           [ @eq_f3 // >(?:c0=x) [ >Hnthi % ]
266             >Hnthi in Hci;normalize #H destruct (H) %
267           | >(?:c0=x) [ >Hnthj % ]
268             >Hnthi in Hci;normalize #H destruct (H) % ]
269         | >Hd >nth_change_vec // >nth_change_vec_neq [|@sym_not_eq //]
270           >nth_change_vec // >Hnthi >Hnthj normalize 
271           cases Hcicj #Hcase 
272           [%1 %{ci} % // | %2 %1 %1 @(not_to_not ??? Hcase) #H destruct (H) % ]
273         ]
274       | #x0 #xs0 #Hnthi #Hnthj #Hnotendc #Hcicj
275         >(IH2 (c0::ls) x0 xs0 ci rs (c0::ls0) cj rs0 … Hcicj)
276         [ >Hd >change_vec_commute in ⊢ (??%?); //
277           >change_vec_change_vec >change_vec_commute in ⊢ (??%?); //
278           @sym_not_eq //
279         | #c1 #Hc1 @Hnotendc @memb_cons @Hc1
280         | >Hd >nth_change_vec // >Hnthj normalize
281           >Hnthi in Hci;normalize #H destruct (H) %
282         | >Hd >nth_change_vec_neq [|@sym_not_eq //] >Hnthi
283           >nth_change_vec // normalize
284           >Hnthi in Hci;normalize #H destruct (H) %
285         ]
286 ]]]
287 qed.      
288  
289 lemma terminate_compare :  ∀i,j,sig,n,is_endc,t.
290   i ≠ j → i < S n → j < S n → 
291   compare i j sig n is_endc ↓ t.
292 #i #j #sig #n #is_endc #t #Hneq #Hi #Hj
293 @(terminate_while … (sem_comp_step …)) //
294 <(change_vec_same … t i (niltape ?))
295 cases (nth i (tape sig) t (niltape ?))
296 [ % #t1 * #x * * * #_ >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct 
297 |2,3: #a0 #al0 % #t1 * #x * * * #_ >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
298 | #ls #c #rs lapply c -c lapply ls -ls lapply t -t elim rs
299   [#t #ls #c % #t1 * #x * * * #Hendcx >nth_change_vec // normalize in ⊢ (%→?);
300    #H1 destruct (H1) #Hxsep >change_vec_change_vec #Ht1 % 
301    #t2 * #x0 * * * #Hendcx0 >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
302    >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H)
303   |#r0 #rs0 #IH #t #ls #c % #t1 * #x * * >nth_change_vec //
304    normalize in ⊢ (%→?); #H destruct (H) #Hcur
305    >change_vec_change_vec >change_vec_commute // #Ht1 >Ht1 @IH
306   ]
307 ]
308 qed.
309
310 lemma sem_compare : ∀i,j,sig,n,is_endc.
311   i ≠ j → i < S n → j < S n → 
312   compare i j sig n is_endc ⊨ R_compare i j sig n is_endc.
313 #i #j #sig #n #is_endc #Hneq #Hi #Hj @WRealize_to_Realize /2/
314 qed.
315
316 (*
317    |conf1   $
318    |confin 0/1 confout move
319
320   match machine step ≝
321     compare;
322     if (cur(src) != $)
323       then
324         parmoveL;
325         moveR(dst);
326       else nop
327  *)
328
329 definition match_step ≝ λsrc,dst,sig,n,is_startc,is_endc.
330   compare src dst sig n is_endc ·
331     (ifTM ?? (inject_TM ? (test_char ? (λa.is_endc a == false)) n src)
332       (single_finalTM ??
333         (parmove src dst sig n L is_startc · (inject_TM ? (move_r ?) n dst)))
334       (nop …)
335       tc_true).
336       
337 definition Rtc_multi_true ≝ 
338   λalpha,test,n,i.λt1,t2:Vector ? (S n).
339    (∃c. current alpha (nth i ? t1 (niltape ?)) = Some ? c ∧ test c = true) ∧ t2 = t1.
340    
341 definition Rtc_multi_false ≝ 
342   λalpha,test,n,i.λt1,t2:Vector ? (S n).
343     (∀c. current alpha (nth i ? t1 (niltape ?)) = Some ? c → test c = false) ∧ t2 = t1.
344
345 definition R_match_step_false ≝ 
346   λsrc,dst,sig,n,is_endc.λint,outt: Vector (tape sig) (S n).
347   ∀ls,x,xs,end,rs.
348   nth src ? int (niltape ?) = midtape sig ls x (xs@end::rs) →
349   (∀c0. memb ? c0 (x::xs) = true → is_endc c0 = false) → is_endc end = true →
350    ((current sig (nth dst (tape sig) int (niltape sig)) = None ? ) ∧ outt = int) ∨
351    (∃ls0,rs0. 
352     nth dst ? int (niltape ?) = midtape sig ls0 x (xs@rs0) ∧
353     ∀rsj,end,c. 
354     rs0 = c::rsj →
355     outt = change_vec ??
356            (change_vec ?? int (midtape sig (reverse ? xs@x::ls) end rs) src)
357            (midtape sig (reverse ? xs@x::ls0) c rsj) dst).
358 (*  
359 definition R_match_step_false ≝  
360   λsrc,dst,sig,n,is_endc.λint,outt: Vector (tape sig) (S n).
361    (((∃x.current ? (nth src ? int (niltape ?)) = Some ? x ∧ is_endc x = true) ∨
362      current sig (nth src (tape sig) int (niltape sig)) = None ? ∨
363      current sig (nth dst (tape sig) int (niltape sig)) = None ? ) ∧ outt = int) ∨
364    (∃ls,ls0,rs,rs0,x,xs. 
365     nth src ? int (niltape ?) = midtape sig ls x (xs@rs) ∧ is_endc x = false ∧
366     nth dst ? int (niltape ?) = midtape sig ls0 x (xs@rs0) ∧
367     ∀rsi,rsj,end,c. 
368     rs = end::rsi → rs0 = c::rsj →
369     (∀c0. memb ? c0 (x::xs) = true → is_endc c0 = false) ∧ is_endc end = true ∧
370     nth dst ? int (niltape ?) = midtape sig ls0 x (xs@c::rsj) ∧
371     outt = change_vec ??
372            (change_vec ?? int (midtape sig (reverse ? xs@x::ls) end rsi) src)
373            (midtape sig (reverse ? xs@x::ls0) c rsj) dst).
374 *)
375
376 definition R_match_step_true ≝ 
377   λsrc,dst,sig,n,is_startc,is_endc.λint,outt: Vector (tape sig) (S n).
378   ∀s.current sig (nth src (tape sig) int (niltape sig)) = Some ? s → 
379   is_startc s = true → 
380   (∀c.c ∈ right ? (nth src (tape sig) int (niltape sig)) = true → is_startc c = false) →
381   (∀s1.current sig (nth dst (tape sig) int (niltape sig)) = Some ? s1 → s ≠ s1 →  
382    outt = change_vec ?? int 
383           (tape_move … (nth dst ? int (niltape ?)) (Some ? 〈s1,R〉)) dst ∧ is_endc s = false) ∧  
384   (∀ls,x,xs,ci,rs,ls0,cj,rs0. 
385     nth src ? int (niltape ?) = midtape sig ls x (xs@ci::rs) →
386     nth dst ? int (niltape ?) = midtape sig ls0 x (xs@cj::rs0) → ci ≠ cj → 
387     (∀c0. memb ? c0 (x::xs) = true → is_endc c0 = false) → 
388     outt = change_vec ?? int 
389            (tape_move … (nth dst ? int (niltape ?)) (Some ? 〈x,R〉)) dst ∧ is_endc ci = false).
390     
391 lemma sem_test_char_multi :
392   ∀alpha,test,n,i.i ≤ n → 
393   inject_TM ? (test_char ? test) n i ⊨ 
394   [ tc_true : Rtc_multi_true alpha test n i, Rtc_multi_false alpha test n i ].
395 #alpha #test #n #i #Hin #int
396 cases (acc_sem_inject … Hin (sem_test_char alpha test) int)
397 #k * #outc * * #Hloop #Htrue #Hfalse %{k} %{outc} % [ %
398 [ @Hloop
399 | #Hqtrue lapply (Htrue Hqtrue) * * * #c *
400   #Hcur #Htestc #Hnth_i #Hnth_j %
401   [ %{c} % //
402   | @(eq_vec … (niltape ?)) #i0 #Hi0
403     cases (decidable_eq_nat i0 i) #Hi0i
404     [ >Hi0i @Hnth_i
405     | @sym_eq @Hnth_j @sym_not_eq // ] ] ]
406 | #Hqfalse lapply (Hfalse Hqfalse) * * #Htestc #Hnth_i #Hnth_j %
407   [ @Htestc
408   | @(eq_vec … (niltape ?)) #i0 #Hi0
409     cases (decidable_eq_nat i0 i) #Hi0i
410     [ >Hi0i @Hnth_i
411     | @sym_eq @Hnth_j @sym_not_eq // ] ] ]
412 qed.
413
414 axiom comp_list: ∀S:DeqSet. ∀l1,l2:list S.∀is_endc. ∃l,tl1,tl2. 
415   l1 = l@tl1 ∧ l2 = l@tl2 ∧ (∀c.c ∈ l = true → is_endc c = false) ∧
416   ∀a,tla. tl1 = a::tla → is_endc a = true ∨ (∀b,tlb.tl2 = b::tlb → a≠b).
417   
418 axiom daemon : ∀X:Prop.X.
419
420 lemma sem_match_step :
421   ∀src,dst,sig,n,is_startc,is_endc.src ≠ dst → src < S n → dst < S n → 
422   match_step src dst sig n is_startc is_endc ⊨ 
423     [ inr ?? (inr ?? (inl … (inr ?? start_nop))) : 
424       R_match_step_true src dst sig n is_startc is_endc, 
425       R_match_step_false src dst sig n is_endc ].
426 #src #dst #sig #n #is_startc #is_endc #Hneq #Hsrc #Hdst 
427 @(acc_sem_seq_app sig n … (sem_compare src dst sig n is_endc Hneq Hsrc Hdst)
428     (acc_sem_if ? n … (sem_test_char_multi sig (λa.is_endc a == false) n src (le_S_S_to_le … Hsrc))
429       (sem_seq … 
430         (sem_parmoveL ???? is_startc Hneq Hsrc Hdst) 
431         (sem_inject … dst (le_S_S_to_le … Hdst) (sem_move_r ? )))
432       (sem_nop …)))
433 [#ta #tb #tc * #Hcomp1 #Hcomp2 * #td * * * #c * #Hcurtc #Hcend #Htd >Htd -Htd
434  #Htb #s #Hcurta_src #Hstart #Hnotstart %
435  [ #s1 #Hcurta_dst #Hneqss1
436    lapply Htb lapply Hcurtc -Htb -Hcurtc >(?:tc=ta) 
437    [|@Hcomp1 %2 % % >Hcurta_src >Hcurta_dst @(not_to_not … Hneqss1) #H destruct (H) % ]
438    #Hcurtc * #te * * #_ #Hte >Hte // whd in ⊢ (%→?); * * #_ #Htbdst #Htbelse %
439    [ @(eq_vec … (niltape ?)) #i #Hi cases (decidable_eq_nat i dst) #Hidst
440      [ >Hidst >nth_change_vec // cases (current_to_midtape … Hcurta_dst)
441        #ls * #rs #Hta_mid >(Htbdst … Hta_mid) >Hta_mid cases rs //
442      | >nth_change_vec_neq [|@sym_not_eq //] @sym_eq @Htbelse @sym_not_eq // ]
443    | >Hcurtc in Hcurta_src; #H destruct (H) cases (is_endc s) in Hcend;
444      normalize #H destruct (H) // ]
445  |#ls #x #xs #ci #rs #ls0 #cj #rs0 #Htasrc_mid #Htadst_mid #Hcicj #Hnotendc
446   lapply (Hcomp2 … Htasrc_mid Htadst_mid Hnotendc (or_intror ?? Hcicj))
447   -Hcomp2 #Hcomp2
448   cases Htb #td * * #Htd #_ >Htasrc_mid in Hcurta_src; normalize in ⊢ (%→?);
449   #H destruct (H)
450   >(Htd ls ci (reverse ? xs) rs s ??? ls0 cj (reverse ? xs) s rs0 (refl ??)) //
451   [| >Hcomp2 >nth_change_vec //
452    | #c0 #Hc0 @(Hnotstart c0) >Htasrc_mid 
453      cases (orb_true_l … Hc0) -Hc0 #Hc0
454     [@memb_append_l2 >(\P Hc0) @memb_hd
455     |@memb_append_l1 <(reverse_reverse …xs) @memb_reverse //
456     ]
457    | >Hcomp2 >nth_change_vec_neq [|@sym_not_eq // ] @nth_change_vec // ]
458   * * #_ #Htbdst #Htbelse %
459   [ @(eq_vec … (niltape ?)) #i #Hi cases (decidable_eq_nat i dst) #Hidst
460      [ >Hidst >nth_change_vec // >Htadst_mid >(Htbdst ls0 s (xs@cj::rs0))
461        [ cases xs //
462        | >nth_change_vec // ]
463      | >nth_change_vec_neq [|@sym_not_eq //]
464        <Htbelse [|@sym_not_eq // ]
465        >nth_change_vec_neq [|@sym_not_eq //]
466         (* STOP. *)
467        cases (decidable_eq_nat i src) #Hisrc
468        [ >Hisrc >nth_change_vec // >Htasrc_mid //
469        | >nth_change_vec_neq [|@sym_not_eq //]
470          <(Htbelse i) [|@sym_not_eq // ]
471          >Hcomp2 >nth_change_vec_neq [|@sym_not_eq // ]
472          >nth_change_vec_neq [|@sym_not_eq // ] //
473        ]
474      ]
475   | >Hcomp2 in Hcurtc; >nth_change_vec_neq [|@sym_not_eq //] 
476      >nth_change_vec // whd in ⊢ (??%?→?); 
477      #H destruct (H) cases (is_endc c) in Hcend;
478      normalize #H destruct (H) // ]
479   ]
480 |#intape #outtape #ta * #Hcomp1 #Hcomp2 * #tb * * #Hc #Htb 
481  whd in ⊢ (%→?); #Hout >Hout >Htb whd
482  #ls #c_src #xs #end #rs #Hmid_src #Hnotend #Hend
483  lapply (current_to_midtape sig (nth dst ? intape (niltape ?)))
484  cases (current … (nth dst ? intape (niltape ?))) in Hcomp1;
485   [#Hcomp1 #_ %1 % [% | @Hcomp1 %2 %2 % ]
486   |#c_dst cases (true_or_false (c_src == c_dst)) #Hceq
487     [#_ #Hmid_dst cases (Hmid_dst c_dst (refl …)) -Hmid_dst
488      #ls_dst * #rs_dst #Hmid_dst %2
489      cases (comp_list … (xs@end::rs) rs_dst is_endc) #xs1 * #rsi * #rsj * * * 
490      #Hrs_src #Hrs_dst #Hnotendc #Hneq    
491      %{ls_dst} %{rsj} % 
492       [<Hrs_dst >(\P Hceq) // ]]     
493          #rsi0 #rsj0 #end #c #Hend #Hc_dst
494          >Hrs_src in Hmid_src; >Hend #Hmid_src
495          >Hrs_dst in Hmid_dst; >Hc_dst <(\P Hceq) #Hmid_dst
496          cut (is_endc end = true ∨ end ≠ c)
497          [cases (Hneq … Hend) /2/ -Hneq #Hneq %2 @(Hneq … Hc_dst) ] #Hneq
498          lapply (Hcomp2 … Hmid_src Hmid_dst ? Hneq)
499           [#c0 #Hc0 cases (orb_true_l … Hc0) -Hc0 #Hc0
500             [ >(\P Hc0) //
501             | @Hnotendc // ] 
502           ]
503          -Hcomp2 #Hcomp2 <Hcomp2
504          % // % [ 
505            >Hcomp2 in Hc; >nth_change_vec_neq [|@sym_not_eq //]
506             >nth_change_vec // #H lapply (H ? (refl …)) 
507             cases (is_endc end) [|normalize #H destruct (H) ]
508             #_ % // #c0 #Hc0 cases (orb_true_l … Hc0) -Hc0 #Hc0
509               [ >(\P Hc0) // | @Hnotendc // ]
510          |@Hmid_dst] 
511          ]
512       |#_ #Hcomp1 #Hsrc cases (Hsrc ? (refl ??)) -Hsrc #ls * #rs #Hsrc
513        %1 % 
514         [% % %{c_src} % // lapply (Hc c_src) -Hc >Hcomp1
515          [| %2 % % @(not_to_not ??? (\Pf Hceq)) #H destruct (H) // ]
516          cases (is_endc c_src) //
517          >Hsrc #Hc lapply (Hc (refl ??)) normalize #H destruct (H)
518         |@Hcomp1 %2 %1 %1 @(not_to_not ??? (\Pf Hceq)) #H destruct (H) //
519         ]
520       ]
521     ]
522   ] 
523 qed.
524
525 #intape #outtape #ta * #Hcomp1 #Hcomp2 * #tb * * #Hc #Htb 
526  whd in ⊢ (%→?); #Hout >Hout >Htb whd
527  lapply (current_to_midtape sig (nth src ? intape (niltape ?)))
528  cases (current … (nth src ? intape (niltape ?))) in Hcomp1; 
529   [#Hcomp1 #_ %1 % [%1 %2 // | @Hcomp1 %2 %1 %2 %]
530   |#c_src lapply (current_to_midtape sig (nth dst ? intape (niltape ?)))
531    cases (current … (nth dst ? intape (niltape ?))) 
532     [#_ #Hcomp1 #_ %1 % [%2 % | @Hcomp1 %2 % % % #H destruct (H)]
533     |#c_dst cases (true_or_false (c_src == c_dst)) #Hceq
534       [#Hmid_dst cases (Hmid_dst c_dst (refl …)) -Hmid_dst
535        #ls_dst * #rs_dst #Hmid_dst #Hcomp1
536        #Hmid_src cases (Hmid_src c_src (refl …)) -Hmid_src
537        #ls_src * #rs_src #Hmid_src
538        cases (true_or_false (is_endc c_src)) #Hc_src
539        [ % % [ % % %{c_src} % // | @Hcomp1 % %{c_src} % // ]
540        | %2 cases (comp_list … rs_src rs_dst is_endc) #xs * #rsi * #rsj * * * 
541          #Hrs_src #Hrs_dst #Hnotendc #Hneq    
542          %{ls_src} %{ls_dst} %{rsi} %{rsj} %{c_src} %{xs} % 
543            [% [% // <Hrs_src //|<Hrs_dst >(\P Hceq) // ]]     
544          #rsi0 #rsj0 #end #c #Hend #Hc_dst
545          >Hrs_src in Hmid_src; >Hend #Hmid_src
546          >Hrs_dst in Hmid_dst; >Hc_dst <(\P Hceq) #Hmid_dst
547          cut (is_endc end = true ∨ end ≠ c)
548          [cases (Hneq … Hend) /2/ -Hneq #Hneq %2 @(Hneq … Hc_dst) ] #Hneq
549          lapply (Hcomp2 … Hmid_src Hmid_dst ? Hneq)
550           [#c0 #Hc0 cases (orb_true_l … Hc0) -Hc0 #Hc0
551             [ >(\P Hc0) //
552             | @Hnotendc // ] 
553           ]
554          -Hcomp2 #Hcomp2 <Hcomp2
555          % // % [ 
556            >Hcomp2 in Hc; >nth_change_vec_neq [|@sym_not_eq //]
557             >nth_change_vec // #H lapply (H ? (refl …)) 
558             cases (is_endc end) [|normalize #H destruct (H) ]
559             #_ % // #c0 #Hc0 cases (orb_true_l … Hc0) -Hc0 #Hc0
560               [ >(\P Hc0) // | @Hnotendc // ]
561          |@Hmid_dst] 
562          ]
563       |#_ #Hcomp1 #Hsrc cases (Hsrc ? (refl ??)) -Hsrc #ls * #rs #Hsrc
564        %1 % 
565         [% % %{c_src} % // lapply (Hc c_src) -Hc >Hcomp1
566          [| %2 % % @(not_to_not ??? (\Pf Hceq)) #H destruct (H) // ]
567          cases (is_endc c_src) //
568          >Hsrc #Hc lapply (Hc (refl ??)) normalize #H destruct (H)
569         |@Hcomp1 %2 %1 %1 @(not_to_not ??? (\Pf Hceq)) #H destruct (H) //
570         ]
571       ]
572     ]
573   ] 
574 qed.
575
576 definition match_m ≝ λsrc,dst,sig,n,is_startc,is_endc.
577   whileTM … (match_step src dst sig n is_startc is_endc) 
578     (inr ?? (inr ?? (inl … (inr ?? start_nop)))).
579
580 definition R_match_m ≝ 
581   λi,j,sig,n,is_startc,is_endc.λint,outt: Vector (tape sig) (S n).
582   (((∃x.current ? (nth i ? int (niltape ?)) = Some ? x ∧ is_endc x = true) ∨
583     current ? (nth i ? int (niltape ?)) = None ? ∨
584     current ? (nth j ? int (niltape ?)) = None ?) → outt = int) ∧
585   (∀ls,x,xs,ci,rs,ls0,x0,rs0.
586     (∀x. is_startc x ≠ is_endc x) → 
587     is_startc x = true → is_endc ci = true → 
588     (∀z. memb ? z (x::xs) = true → is_endc x = false) →
589     nth i ? int (niltape ?) = midtape sig ls x (xs@ci::rs) →
590     nth j ? int (niltape ?) = midtape sig ls0 x0 rs0 →
591     (∃l,l1.x0::rs0 = l@x::xs@l1 ∧
592      ∀cj,l2.l1=cj::l2 →
593      outt = change_vec ?? 
594             (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) i)
595             (midtape sig ((reverse ? (l@x::xs))@ls0) cj l2) j) ∨
596     ∀l,l1.x0::rs0 ≠ l@x::xs@l1).
597
598 (*
599 axiom sub_list_dec: ∀A.∀l,ls:list A. 
600   ∃l1,l2. l = l1@ls@l2 ∨ ∀l1,l2. l ≠ l1@ls@l2.
601 *)
602
603 lemma wsem_match_m : ∀src,dst,sig,n,is_startc,is_endc.
604 src ≠ dst → src < S n → dst < S n → 
605   match_m src dst sig n is_startc is_endc ⊫ R_match_m src dst sig n is_startc is_endc.
606 #src #dst #sig #n #is_startc #is_endc #Hneq #Hsrc #Hdst #ta #k #outc #Hloop
607 lapply (sem_while … (sem_match_step src dst sig n is_startc is_endc Hneq Hsrc Hdst) … Hloop) //
608 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
609 [ #tc whd in ⊢ (%→%); * 
610   [ * * [ *
611     [ * #cur_src * #H1 #H2 #Houtc %   
612       [ #_ @Houtc
613       | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hdiff #Hstartc #Hendc #Hnotend #Hnthi 
614         @False_ind
615         >Hnthi in H1; whd in ⊢ (??%?→?); #H destruct (H) cases (Hdiff cur_src)
616         #Habs @Habs //
617       ]
618     | #Hci #Houtc %
619       [ #_ @Houtc
620       | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hdiff #Hstartc #Hendc #Hnotend 
621         #Hnthi >Hnthi in Hci; normalize in ⊢ (%→?); #H destruct (H) ] ]
622     | #Hcj #Houtc %
623       [ #_ @Houtc
624       | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hdiff #Hstartc #Hendc #_ #_ #Hnthj >Hnthj in Hcj;
625         normalize in ⊢ (%→?); #H destruct (H) ] 
626     ]
627   |* #ls * #ls0 * #rs * #rs0 * #x0 * #xs * * * #Hsrc #Hx0 #Hdst #H %
628     [>Hsrc * 
629       [* [* #x * whd in ⊢ (??%?→?); #Habs destruct (Habs) >Hx0 #Habs destruct (Habs)
630                 |whd in ⊢ (??%?→?); #Habs destruct (Habs) ]
631          |>Hdst whd in ⊢ (??%?→?); #Habs destruct (Habs)  ]
632     |#ls1 #x1 #xs1 #ci #rsi #ls2 #x2 #rs2 
633      #Hdiff #Hstart #Hend #Hnotend 
634      >Hsrc #Hsrc1 destruct (Hsrc1) >Hdst #Hdst1 destruct (Hdst1) 
635      %1 %{[ ]} %{rs0} normalize in ⊢ (%→?); #Heq #cj #l2 #Hl1
636      cut (xs=xs1) 
637        [@(append_l1_injective_r … rs0 rs0 (refl …)) @(cons_injective_r …Heq)] 
638      #eqxs <eqxs 
639      whd in match (append ? [ ] (x2::xs)); >reverse_cons >associative_append
640      normalize in match (append ? [x2] ls2);
641      cases (H rsi l2 ci cj ? Hl1) 
642       [* #_ #_ #H3 @H3  
643       |>eqxs in e0; #e0 @(append_l2_injective … e0) // 
644       ] 
645     ]
646   ]
647 |#tc #td #te #Hd #Hstar #IH #He lapply (IH He) -IH *
648     #IH1 #IH2 % [@IH1]
649     
650     
651      cases (comp_list ? (x1::xs1@ci::rsi) (x2::rs2) is_endc)
652      #l * #tl1 * #tl2 * * * #H1 #H2 #H3 #H4
653