]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/multi_universal/match.ma
working on 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   (current sig (nth dst (tape sig) int (niltape sig)) = None ? → outt = int) ∧
413   (∀s1.current sig (nth dst (tape sig) int (niltape sig)) = Some ? s1 → s ≠ s1 →  
414    outt = change_vec ?? int 
415           (tape_move … (nth dst ? int (niltape ?)) (Some ? 〈s1,R〉)) dst ∧ is_endc s = false) ∧  
416   (∀ls,x,xs,ci,rs,ls0,rs0. 
417     nth src ? int (niltape ?) = midtape sig ls x (xs@ci::rs) →
418     nth dst ? int (niltape ?) = midtape sig ls0 x (xs@rs0) →
419     (∀c0. memb ? c0 (x::xs) = true → is_endc c0 = false) → 
420     (∃cj,rs1.rs0 = cj::rs1 → ci ≠ cj →
421      (outt = change_vec ?? int 
422            (tape_move … (nth dst ? int (niltape ?)) (Some ? 〈x,R〉)) dst ∧ is_endc ci = false)) ∨
423     (rs0 = [ ] →
424      outt = change_vec ??
425            (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) src)
426            (mk_tape sig (reverse ? xs@x::ls0) (None ?) [ ]) dst)).
427            
428 lemma sem_test_char_multi :
429   ∀alpha,test,n,i.i ≤ n → 
430   inject_TM ? (test_char ? test) n i ⊨ 
431   [ tc_true : Rtc_multi_true alpha test n i, Rtc_multi_false alpha test n i ].
432 #alpha #test #n #i #Hin #int
433 cases (acc_sem_inject … Hin (sem_test_char alpha test) int)
434 #k * #outc * * #Hloop #Htrue #Hfalse %{k} %{outc} % [ %
435 [ @Hloop
436 | #Hqtrue lapply (Htrue Hqtrue) * * * #c *
437   #Hcur #Htestc #Hnth_i #Hnth_j %
438   [ %{c} % //
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 | #Hqfalse lapply (Hfalse Hqfalse) * * #Htestc #Hnth_i #Hnth_j %
444   [ @Htestc
445   | @(eq_vec … (niltape ?)) #i0 #Hi0
446     cases (decidable_eq_nat i0 i) #Hi0i
447     [ >Hi0i @Hnth_i
448     | @sym_eq @Hnth_j @sym_not_eq // ] ] ]
449 qed.
450
451 axiom comp_list: ∀S:DeqSet. ∀l1,l2:list S.∀is_endc. ∃l,tl1,tl2. 
452   l1 = l@tl1 ∧ l2 = l@tl2 ∧ (∀c.c ∈ l = true → is_endc c = false) ∧
453   ∀a,tla. tl1 = a::tla → is_endc a = true ∨ (∀b,tlb.tl2 = b::tlb → a≠b).
454   
455 axiom daemon : ∀X:Prop.X.
456
457 lemma sem_match_step :
458   ∀src,dst,sig,n,is_startc,is_endc.src ≠ dst → src < S n → dst < S n → 
459   match_step src dst sig n is_startc is_endc ⊨ 
460     [ inr ?? (inr ?? (inl … (inr ?? start_nop))) : 
461       R_match_step_true src dst sig n is_startc is_endc, 
462       R_match_step_false src dst sig n is_endc ].
463 #src #dst #sig #n #is_startc #is_endc #Hneq #Hsrc #Hdst 
464 @(acc_sem_seq_app sig n … (sem_compare src dst sig n is_endc Hneq Hsrc Hdst)
465     (acc_sem_if ? n … (sem_test_char_multi sig (λa.is_endc a == false) n src (le_S_S_to_le … Hsrc))
466       (sem_seq … 
467         (sem_parmoveL ???? is_startc Hneq Hsrc Hdst) 
468         (sem_inject … dst (le_S_S_to_le … Hdst) (sem_move_r ? )))
469       (sem_nop …)))
470 [#ta #tb #tc * #Hcomp1 #Hcomp2 * #td * * * #c * #Hcurtc #Hcend #Htd >Htd -Htd
471  #Htb #s #Hcurta_src #Hstart #Hnotstart % [ %
472  [#Hdst_none @daemon 
473  | #s1 #Hcurta_dst #Hneqss1
474    lapply Htb lapply Hcurtc -Htb -Hcurtc >(?:tc=ta) 
475    [|@Hcomp1 %2 % % >Hcurta_src >Hcurta_dst @(not_to_not … Hneqss1) #H destruct (H) % ]
476    #Hcurtc * #te * * #_ #Hte >Hte [2: %1 %1 %{s} % //] 
477    whd in ⊢ (%→?); * * #_ #Htbdst #Htbelse %
478    [ @(eq_vec … (niltape ?)) #i #Hi cases (decidable_eq_nat i dst) #Hidst
479      [ >Hidst >nth_change_vec // cases (current_to_midtape … Hcurta_dst)
480        #ls * #rs #Hta_mid >(Htbdst … Hta_mid) >Hta_mid cases rs //
481      | >nth_change_vec_neq [|@sym_not_eq //] @sym_eq @Htbelse @sym_not_eq // ]
482    | >Hcurtc in Hcurta_src; #H destruct (H) cases (is_endc s) in Hcend;
483      normalize #H destruct (H) // ]
484    ]
485  |#ls #x #xs #ci #rs #ls0 #rs00 #Htasrc_mid #Htadst_mid #Hnotendc 
486   cases rs00 in Htadst_mid;
487    [(* case rs empty *) #Htadst_mid %2 #_
488     cases (Hcomp2 … Htasrc_mid Htadst_mid Hnotendc) -Hcomp2 
489      [2: * #x0 * #rs1 * #H destruct (H) ]
490     * #_ #Htc cases Htb #td * * #_ #Htd >Htasrc_mid in Hcurta_src; 
491     normalize in ⊢ (%→?); #H destruct (H)  
492     >Htd [2: %2 >Htc >nth_change_vec // cases (reverse sig ?) //]
493     >Htc * * >nth_change_vec // #Htbdst #_ #Htbelse
494      @(eq_vec … (niltape ?)) #i #Hi cases (decidable_eq_nat i dst) #Hidst
495       [ >Hidst >nth_change_vec // <Htbdst // cases (reverse sig ?) //
496       |@sym_eq @Htbelse @sym_not_eq //
497       ] 
498     |#cj #rs0 #Htadst_mid % %{cj} %{rs0} #_ #Hcicj
499     cases (Hcomp2 … Htasrc_mid Htadst_mid Hnotendc) [ * #H destruct (H) ]
500     * #cj' * #rs0' * #Hcjrs0 destruct (Hcjrs0) -Hcomp2 #Hcomp2
501     lapply (Hcomp2 (or_intror ?? Hcicj)) -Hcomp2 #Htc
502     cases Htb #td * * #Htd #_ >Htasrc_mid in Hcurta_src; normalize in ⊢ (%→?);
503     #H destruct (H)
504     >(Htd ls ci (reverse ? xs) rs s ??? ls0 cj' (reverse ? xs) s rs0' (refl ??)) //
505     [| >Htc >nth_change_vec //
506     | #c0 #Hc0 @(Hnotstart c0) >Htasrc_mid 
507       cases (orb_true_l … Hc0) -Hc0 #Hc0
508       [@memb_append_l2 >(\P Hc0) @memb_hd
509       |@memb_append_l1 <(reverse_reverse …xs) @memb_reverse //
510       ]
511     | >Htc >nth_change_vec_neq [|@sym_not_eq // ] @nth_change_vec // ]
512     * * #_ #Htbdst #Htbelse %
513     [ @(eq_vec … (niltape ?)) #i #Hi cases (decidable_eq_nat i dst) #Hidst
514        [ >Hidst >nth_change_vec // >Htadst_mid >(Htbdst ls0 s (xs@cj'::rs0'))
515          [ cases xs //
516          | >nth_change_vec // ]
517        | >nth_change_vec_neq [|@sym_not_eq //]
518          <Htbelse [|@sym_not_eq // ]
519          >nth_change_vec_neq [|@sym_not_eq //]
520          cases (decidable_eq_nat i src) #Hisrc
521          [ >Hisrc >nth_change_vec // >Htasrc_mid //
522          | >nth_change_vec_neq [|@sym_not_eq //]
523            <(Htbelse i) [|@sym_not_eq // ]
524            >Htc >nth_change_vec_neq [|@sym_not_eq // ]
525            >nth_change_vec_neq [|@sym_not_eq // ] //
526          ]
527        ]
528     | >Htc in Hcurtc; >nth_change_vec_neq [|@sym_not_eq //] 
529        >nth_change_vec // whd in ⊢ (??%?→?); 
530        #H destruct (H) cases (is_endc c) in Hcend;
531        normalize #H destruct (H) // ]
532     ]
533   ]
534 |#intape #outtape #ta * #Hcomp1 #Hcomp2 * #tb * * #Hc #Htb 
535  whd in ⊢ (%→?); #Hout >Hout >Htb whd
536  #ls #c_src #xs #end #rs #Hmid_src #Hnotend #Hend
537  lapply (current_to_midtape sig (nth dst ? intape (niltape ?)))
538  cases (current … (nth dst ? intape (niltape ?))) in Hcomp1;
539   [#Hcomp1 #_ %1 % [% | @Hcomp1 %2 %2 % ]
540   |#c_dst cases (true_or_false (c_src == c_dst)) #Hceq
541     [#_ #Hmid_dst cases (Hmid_dst c_dst (refl …)) -Hmid_dst
542      #ls_dst * #rs_dst #Hmid_dst %2
543      cases (comp_list … (xs@end::rs) rs_dst is_endc) #xs1 * #rsi * #rsj * * * 
544      #Hrs_src #Hrs_dst #Hnotendxs1 #Hneq %{ls_dst} %{rsj} >Hrs_dst in Hmid_dst; #Hmid_dst
545      cut (∃r1,rs1.rsi = r1::rs1) [@daemon] * #r1 * #rs1 #Hrs1 >Hrs1 in Hrs_src;
546      #Hrs_src >Hrs_src in Hmid_src; #Hmid_src <(\P Hceq) in Hmid_dst; #Hmid_dst
547      lapply (Hcomp2 ??????? Hmid_src Hmid_dst ?) 
548      [ #c0 #Hc0 cases (orb_true_l … Hc0) -Hc0 #Hc0
549        [ >(\P Hc0) @Hnotend @memb_hd | @Hnotendxs1 //]
550      | *
551        [ * #Hrsj #Hta %
552          [ >Hta in Hc; >nth_change_vec_neq [|@sym_not_eq //] >nth_change_vec //
553            #Hc lapply (Hc ? (refl ??)) #Hendr1
554            cut (xs = xs1)
555            [ lapply Hnotendxs1 lapply Hnotend lapply Hrs_src lapply xs1
556              -Hnotendxs1 -Hnotend -Hrs_src -xs1 elim xs
557              [ * normalize in ⊢ (%→?); //
558                #x2 #xs2 normalize in ⊢ (%→?); #Heq destruct (Heq) #_ #Hnotendxs1
559                lapply (Hnotendxs1 ? (memb_hd …)) >Hend #H destruct (H)
560              | #x2 #xs2 #IH *
561                [ normalize in ⊢ (%→?); #Heq destruct (Heq) #Hnotendc
562                  >Hnotendc in Hendr1; [| @memb_cons @memb_hd ]
563                  normalize in ⊢ (%→?); #H destruct (H)
564                | #x3 #xs3 normalize in ⊢ (%→?); #Heq destruct (Heq)
565                  #Hnotendc #Hnotendcxs1 @eq_f @IH
566                  [ @(cons_injective_r … Heq)
567                  | #c0 #Hc0 @Hnotendc cases (orb_true_l … Hc0) -Hc0 #Hc0
568                    [ >(\P Hc0) @memb_hd
569                    | @memb_cons @memb_cons // ]
570                  | #c #Hc @Hnotendcxs1 @memb_cons // ]
571                ]
572              ]
573            | #Hxsxs1 >Hmid_dst >Hxsxs1 % ]
574          | #rsj0 #c >Hrsj #Hrsj0 destruct (Hrsj0) ]
575        | * #cj * #rs2 * #Hrs2 #Hta lapply (Hta ?) 
576          [ cases (Hneq … Hrs1) /2/ #H %2 @(H ?? Hrs2) ]
577          -Hta #Hta >Hta in Hc; >nth_change_vec_neq [|@sym_not_eq //] 
578          >nth_change_vec // #Hc lapply (Hc ? (refl ??)) #Hendr1
579          (* lemmatize this proof *) cut (xs = xs1)
580          [ lapply Hnotendxs1 lapply Hnotend lapply Hrs_src lapply xs1
581            -Hnotendxs1 -Hnotend -Hrs_src -xs1 elim xs
582            [ * normalize in ⊢ (%→?); //
583              #x2 #xs2 normalize in ⊢ (%→?); #Heq destruct (Heq) #_ #Hnotendxs1
584              lapply (Hnotendxs1 ? (memb_hd …)) >Hend #H destruct (H)
585            | #x2 #xs2 #IH *
586              [ normalize in ⊢ (%→?); #Heq destruct (Heq) #Hnotendc
587                >Hnotendc in Hendr1; [| @memb_cons @memb_hd ]
588                normalize in ⊢ (%→?); #H destruct (H)
589              | #x3 #xs3 normalize in ⊢ (%→?); #Heq destruct (Heq)
590                #Hnotendc #Hnotendcxs1 @eq_f @IH
591                [ @(cons_injective_r … Heq)
592                | #c0 #Hc0 @Hnotendc cases (orb_true_l … Hc0) -Hc0 #Hc0
593                  [ >(\P Hc0) @memb_hd
594                  | @memb_cons @memb_cons // ]
595                | #c #Hc @Hnotendcxs1 @memb_cons // ]
596              ]
597            ]
598          | #Hxsxs1 >Hmid_dst >Hxsxs1 % //
599            #rsj0 #c #Hcrsj destruct (Hxsxs1 Hrs2 Hcrsj) @eq_f3 //
600            @eq_f3 // lapply (append_l2_injective ?????? Hrs_src) //
601            #Hendr1 destruct (Hendr1) % ]
602        ]
603      ]
604    (* STOP *)
605    |#Hcomp1 #Hsrc cases (Hsrc ? (refl ??)) -Hsrc #ls0 * #rs0 #Hdst 
606     @False_ind lapply (Hcomp1 ?) [%2 %1 %1 >Hmid_src normalize
607     @(not_to_not ??? (\Pf Hceq)) #H destruct //] #Hintape 
608     >Hintape in Hc; >Hmid_src #Hc lapply (Hc ? (refl …)) -Hc 
609     >(Hnotend c_src) // normalize #H destruct (H)   
610    ]
611   ]
612 ]
613 qed. 
614
615 definition match_m ≝ λsrc,dst,sig,n,is_startc,is_endc.
616   whileTM … (match_step src dst sig n is_startc is_endc) 
617     (inr ?? (inr ?? (inl … (inr ?? start_nop)))).
618
619 definition R_match_m ≝ 
620   λsrc,dst,sig,n,is_startc,is_endc.λint,outt: Vector (tape sig) (S n).
621   ∀ls,x,xs,end,rs.
622   nth src ? int (niltape ?) = midtape sig ls x (xs@end::rs) →
623   is_startc x = true →
624   (∀c0. memb ? c0 (x::xs) = true → is_endc c0 = false) → is_endc end = true →
625    ((current sig (nth dst (tape sig) int (niltape sig)) = None ?) →
626      current sig (nth dst (tape sig) outt (niltape sig)) = None ?)
627      (* outt = int) *) ∧
628    (∀ls0,x0,rs0.
629     nth dst ? int (niltape ?) = midtape sig ls0 x0 rs0 →
630     (∃l,l1.x0::rs0 = l@x::xs@l1 ∧
631      ∀cj,l2.l1=cj::l2 →
632      outt = change_vec ?? 
633             (change_vec ?? int (midtape sig (reverse ? xs@x::ls) end rs) src)
634             (midtape sig ((reverse ? (l@x::xs))@ls0) cj l2) dst) ∨
635     ∀l,l1.x0::rs0 ≠ l@x::xs@l1).
636
637 (*
638 definition R_match_m ≝ 
639   λi,j,sig,n,is_startc,is_endc.λint,outt: Vector (tape sig) (S n).
640   (((∃x.current ? (nth i ? int (niltape ?)) = Some ? x ∧ is_endc x = true) ∨
641     current ? (nth i ? int (niltape ?)) = None ? ∨
642     current ? (nth j ? int (niltape ?)) = None ?) → outt = int) ∧
643   (∀ls,x,xs,ci,rs,ls0,x0,rs0.
644     (∀x. is_startc x ≠ is_endc x) → 
645     is_startc x = true → is_endc ci = true → 
646     (∀z. memb ? z (x::xs) = true → is_endc x = false) →
647     nth i ? int (niltape ?) = midtape sig ls x (xs@ci::rs) →
648     nth j ? int (niltape ?) = midtape sig ls0 x0 rs0 →
649     (∃l,l1.x0::rs0 = l@x::xs@l1 ∧
650      ∀cj,l2.l1=cj::l2 →
651      outt = change_vec ?? 
652             (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) i)
653             (midtape sig ((reverse ? (l@x::xs))@ls0) cj l2) j) ∨
654     ∀l,l1.x0::rs0 ≠ l@x::xs@l1).
655 *)
656
657 (*
658 axiom sub_list_dec: ∀A.∀l,ls:list A. 
659   ∃l1,l2. l = l1@ls@l2 ∨ ∀l1,l2. l ≠ l1@ls@l2.
660 *)
661
662 lemma wsem_match_m : ∀src,dst,sig,n,is_startc,is_endc.
663 src ≠ dst → src < S n → dst < S n → 
664   match_m src dst sig n is_startc is_endc ⊫ R_match_m src dst sig n is_startc is_endc.
665 #src #dst #sig #n #is_startc #is_endc #Hneq #Hsrc #Hdst #ta #k #outc #Hloop
666 lapply (sem_while … (sem_match_step src dst sig n is_startc is_endc Hneq Hsrc Hdst) … Hloop) //
667 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
668 [ #tc #Hfalse #ls #x #xs #end #rs #Hmid_src #Hstart #Hnotend #Hend
669   cases (Hfalse … Hmid_src Hnotend Hend) -Hfalse 
670   [(* current dest = None *) * #Hcur_dst #Houtc %
671     [#_ >Houtc //
672     |#ls0 #x0 #rs0 #Hmid_dst >Hmid_dst in Hcur_dst; 
673      normalize in ⊢ (%→?); #H destruct (H)
674     ]
675   |* #ls0 * #rs0 * #Hmid_dst #HFalse %
676     [ >Hmid_dst normalize in ⊢ (%→?); #H destruct (H)
677     |#ls1 #x1 #rs1 >Hmid_dst #H destruct (H)
678      %1 %{[ ]} %{rs0} % [%] #cj #l2 #Hnotnil 
679      >reverse_cons >associative_append @(HFalse ?? Hnotnil)
680     ]
681   ]
682 |#ta #tb #tc #Htrue #Hstar #IH #Hout lapply (IH Hout) -IH -Hout #IH whd
683  #ls #x #xs #end #rs #Hmid_src #Hstart #Hnotend #Hend 
684  lapply (refl ? (current ? (nth dst ? ta (niltape ?))))
685  cases (current ? (nth dst ? ta (niltape ?))) in ⊢ (???%→?); 
686   [#Hmid_dst % [#_ whd in Htrue; >Hmid_src in Htrue; #Htrue
687    cases (Htrue x (refl … ) Hstart ?) -Htrue [2: @daemon]
688    * #Htb #_ #_ >Htb in IH; // #IH  
689    cases (IH ls x xs end rs Hmid_src Hstart Hnotend Hend)
690     [#H @H //
691     |
692    
693   |#cur_dst #Hcur_dst %2 #ls0 #x0 #rs0 #Hmid_dst 
694    whd in Htrue; >Hmid_src in Htrue; #Htrue
695    cases (Htrue x (refl …) Hstart ?) -Htrue
696     [2: #z #membz @daemon (*aggiungere l'ipotesi*)]
697    cases (true_or_false (x==cur_dst)) #eqx
698     [#_ #Htrue cases (comp_list ? (xs@end::rs) rs0 is_endc)
699      #x1 * #tl1 * #tl2 * * * #Hxs #Hrs0 #Hnotendx1
700      cases tl1 in Hxs; 
701       [>append_nil #Hx1 @daemon (* absurd by Hxs e notendx1 *)]
702      #ci -tl1 #tl1 #Hxs #H cases (H … (refl … ))
703       [(* this is absurd, since Htrue conlcudes is_endc ci =false *)
704        #Hend_ci 
705       
706     @daemon (* lapply(Htrue … (refl …)) -Htrue *)
707     |#Htrue #_ cases(Htrue cur_dst Hcur_dst (\Pf eqx)) -Htrue #Htb #Hendx
708      whd in IH;
709      cases(IH ls x xs end rs ? Hstart Hnotend Hend)
710       [* #H1 #H2 >Htb in H1; >nth_change_vec // 
711        >Hmid_dst cases rs0 [2: #a #tl normalize in ⊢ (%→?); #H destruct (H)] 
712        #_ %2 @daemon (* si dimostra *)
713       |@daemon
714       |>Htb >nth_change_vec_neq [|@sym_not_eq //] @Hmid_src
715       ] 
716     ]
717   ]
718 ]
719 qed.
720