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