]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/multi_universal/match.ma
Match machine (multi)
[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.
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 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.
56   mk_mTM sig n compare_states (trans_compare_step i j sig n) 
57     comp0 (λq.q == comp1 ∨ q == comp2).
58
59 definition R_comp_step_true ≝ 
60   λi,j,sig,n.λint,outt: Vector (tape sig) (S n).
61   ∃x.
62    current ? (nth i ? int (niltape ?)) = Some ? x ∧
63    current ? (nth j ? int (niltape ?)) = Some ? x ∧
64    outt = change_vec ?? 
65             (change_vec ?? int
66               (tape_move ? (nth i ? int (niltape ?)) (Some ? 〈x,R〉)) i)
67             (tape_move ? (nth j ? int (niltape ?)) (Some ? 〈x,R〉)) j.
68
69 definition R_comp_step_false ≝ 
70   λi,j:nat.λsig,n.λint,outt: Vector (tape sig) (S n).
71   (current ? (nth i ? int (niltape ?)) ≠ current ? (nth j ? int (niltape ?)) ∨
72    current ? (nth i ? int (niltape ?)) = None ? ∨
73    current ? (nth j ? int (niltape ?)) = None ?) ∧ outt = int.
74
75 lemma comp_q0_q2_null :
76   ∀i,j,sig,n,v.i < S n → j < S n → 
77   (nth i ? (current_chars ?? v) (None ?) = None ? ∨
78    nth j ? (current_chars ?? v) (None ?) = None ?) → 
79   step sig n (compare_step i j sig n) (mk_mconfig ??? comp0 v) 
80   = mk_mconfig ??? comp2 v.
81 #i #j #sig #n #v #Hi #Hj
82 whd in ⊢ (? → ??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (?→??%?);
83 * #Hcurrent
84 [ @eq_f2
85   [ whd in ⊢ (??(???%)?); >Hcurrent %
86   | whd in ⊢ (??(???????(???%))?); >Hcurrent @tape_move_null_action ]
87 | @eq_f2
88   [ whd in ⊢ (??(???%)?); >Hcurrent cases (nth i ?? (None sig)) //
89   | whd in ⊢ (??(???????(???%))?); >Hcurrent
90     cases (nth i ?? (None sig)) [|#x] @tape_move_null_action ] ]
91 qed.
92
93 lemma comp_q0_q2_neq :
94   ∀i,j,sig,n,v.i < S n → j < S n → 
95   nth i ? (current_chars ?? v) (None ?) ≠ nth j ? (current_chars ?? v) (None ?) → 
96   step sig n (compare_step i j sig n) (mk_mconfig ??? comp0 v) 
97   = mk_mconfig ??? comp2 v.
98 #i #j #sig #n #v #Hi #Hj lapply (refl ? (nth i ?(current_chars ?? v)(None ?)))
99 cases (nth i ?? (None ?)) in ⊢ (???%→?);
100 [ #Hnth #_ @comp_q0_q2_null // % //
101 | #ai #Hai lapply (refl ? (nth j ?(current_chars ?? v)(None ?)))
102   cases (nth j ?? (None ?)) in ⊢ (???%→?);
103   [ #Hnth #_ @comp_q0_q2_null // %2 //
104   | #aj #Haj #Hneq
105     whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
106     [ whd in match (trans ????); >Hai >Haj
107       whd in ⊢ (??(???%)?); >(\bf ?) // @(not_to_not … Hneq) //
108     | whd in match (trans ????); >Hai >Haj
109       whd in ⊢ (??(???????(???%))?); >(\bf ?) /2 by not_to_not/
110       @tape_move_null_action
111 ] ]
112 qed.
113
114 lemma comp_q0_q1 :
115   ∀i,j,sig,n,v,a.i ≠ j → i < S n → j < S n → 
116   nth i ? (current_chars ?? v) (None ?) = Some ? a →
117   nth j ? (current_chars ?? v) (None ?) = Some ? a → 
118   step sig n (compare_step i j sig n) (mk_mconfig ??? comp0 v) =
119     mk_mconfig ??? comp1 
120      (change_vec ? (S n) 
121        (change_vec ?? v
122          (tape_move ? (nth i ? v (niltape ?)) (Some ? 〈a,R〉)) i)
123        (tape_move ? (nth j ? v (niltape ?)) (Some ? 〈a,R〉)) j).
124 #i #j #sig #n #v #a #Heq #Hi #Hj #Ha1 #Ha2
125 whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
126 [ whd in match (trans ????);
127   >Ha1 >Ha2 whd in ⊢ (??(???%)?); >(\b ?) //
128 | whd in match (trans ????);
129   >Ha1 >Ha2 whd in ⊢ (??(???????(???%))?); >(\b ?) //
130   change with (change_vec ?????) in ⊢ (??(???????%)?);
131   <(change_vec_same … v j (niltape ?)) in ⊢ (??%?);
132   <(change_vec_same … v i (niltape ?)) in ⊢ (??%?);
133   >pmap_change >pmap_change >tape_move_null_action
134   @eq_f2 // @eq_f2 // >nth_change_vec_neq //
135 ]
136 qed.
137
138 lemma sem_comp_step :
139   ∀i,j,sig,n.i ≠ j → i < S n → j < S n → 
140   compare_step i j sig n ⊨ 
141     [ comp1: R_comp_step_true i j sig n, 
142              R_comp_step_false i j sig n ].
143 #i #j #sig #n #Hneq #Hi #Hj #int
144 lapply (refl ? (current ? (nth i ? int (niltape ?))))
145 cases (current ? (nth i ? int (niltape ?))) in ⊢ (???%→?);
146 [ #Hcuri %{2} %
147   [| % [ %
148     [ whd in ⊢ (??%?); >comp_q0_q2_null /2/ % <Hcuri in ⊢ (???%); 
149       @sym_eq @nth_vec_map
150     | normalize in ⊢ (%→?); #H destruct (H) ]
151   | #_ % // % %2 // ] ]
152 | #a #Ha lapply (refl ? (current ? (nth j ? int (niltape ?))))
153   cases (current ? (nth j ? int (niltape ?))) in ⊢ (???%→?);
154   [ #Hcurj %{2} %
155     [| % [ %
156        [ whd in ⊢ (??%?); >comp_q0_q2_null /2/ %2 <Hcurj in ⊢ (???%); 
157          @sym_eq @nth_vec_map
158        | normalize in ⊢ (%→?); #H destruct (H) ]
159        | #_ % >Ha >Hcurj % % % #H destruct (H) ] ]
160   | #b #Hb %{2} cases (true_or_false (a == b)) #Hab
161     [ %
162       [| % [ % 
163         [whd in ⊢  (??%?);  >(comp_q0_q1 … a Hneq Hi Hj) //
164           [>(\P Hab) <Hb @sym_eq @nth_vec_map
165           |<Ha @sym_eq @nth_vec_map ]
166         | #_ whd >(\P Hab) %{b} % // % // <(\P Hab) // ]
167         | * #H @False_ind @H %
168       ] ]
169     | %
170       [| % [ % 
171         [whd in ⊢  (??%?);  >comp_q0_q2_neq //
172          <(nth_vec_map ?? (current …) i ? int (niltape ?))
173          <(nth_vec_map ?? (current …) j ? int (niltape ?)) >Ha >Hb
174          @(not_to_not ??? (\Pf Hab)) #H destruct (H) %
175         | normalize in ⊢ (%→?); #H destruct (H) ]
176       | #_ % // % % >Ha >Hb @(not_to_not ??? (\Pf Hab)) #H destruct (H) % ] ]
177     ]
178   ]
179 ]
180 qed.
181
182 definition compare ≝ λi,j,sig,n.
183   whileTM … (compare_step i j sig n) comp1.
184
185 definition R_compare ≝ 
186   λi,j,sig,n.λint,outt: Vector (tape sig) (S n).
187   (current sig (nth i (tape sig) int (niltape sig))
188    ≠current sig (nth j (tape sig) int (niltape sig)) → 
189    outt = int) ∧  
190   (∀ls,x,xs,ci,rs,ls0,cj,rs0. 
191     nth i ? int (niltape ?) = midtape sig ls x (xs@ci::rs) →
192     nth j ? int (niltape ?) = midtape sig ls0 x (xs@cj::rs0) → ci ≠ cj → 
193     outt = change_vec ?? 
194            (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) i)
195            (midtape sig (reverse ? xs@x::ls0) cj rs0) j).
196
197 lemma wsem_compare : ∀i,j,sig,n.i ≠ j → i < S n → j < S n → 
198   compare i j sig n ⊫ R_compare i j sig n.
199 #i #j #sig #n #Hneq #Hi #Hj #ta #k #outc #Hloop
200 lapply (sem_while … (sem_comp_step i j sig n Hneq Hi Hj) … Hloop) //
201 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
202 [ #tc whd in ⊢ (%→?); * * [ *
203   [ #Hcicj #Houtc %
204     [ #_ @Houtc
205     | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hnthi #Hnthj
206       >Hnthi in Hcicj; >Hnthj normalize in ⊢ (%→?); * #H @False_ind @H %
207     ]
208   | #Hci #Houtc %
209     [ #_ @Houtc
210     | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hnthi >Hnthi in Hci;
211       normalize in ⊢ (%→?); #H destruct (H) ] ]
212   | #Hcj #Houtc %
213     [ #_ @Houtc
214     | #ls #x #xs #ci #rs #ls0 #cj #rs0 #_ #Hnthj >Hnthj in Hcj;
215       normalize in ⊢ (%→?); #H destruct (H) ] ]
216   | #tc #td #te * #x * * #Hci #Hcj #Hd #Hstar #IH #He lapply (IH He) -IH *
217     #IH1 #IH2 %
218     [ >Hci >Hcj * #H @False_ind @H %
219     | #ls #c0 #xs #ci #rs #ls0 #cj #rs0 cases xs
220       [ #Hnthi #Hnthj #Hcicj >IH1 
221         [ >Hd @eq_f3 // 
222           [ @eq_f3 // >(?:c0=x) [ >Hnthi % ]
223             >Hnthi in Hci;normalize #H destruct (H) %
224           | >(?:c0=x) [ >Hnthj % ]
225             >Hnthi in Hci;normalize #H destruct (H) % ]
226         | >Hd >nth_change_vec // >nth_change_vec_neq [|@sym_not_eq //]
227           >nth_change_vec // >Hnthi >Hnthj normalize @(not_to_not ??? Hcicj)
228           #H destruct (H) % ]
229       | #x0 #xs0 #Hnthi #Hnthj #Hcicj
230         >(IH2 (c0::ls) x0 xs0 ci rs (c0::ls0) cj rs0 … Hcicj)
231         [ >Hd >change_vec_commute in ⊢ (??%?); //
232           >change_vec_change_vec >change_vec_commute in ⊢ (??%?); //
233           @sym_not_eq //
234         | >Hd >nth_change_vec // >Hnthj normalize
235           >Hnthi in Hci;normalize #H destruct (H) %
236         | >Hd >nth_change_vec_neq [|@sym_not_eq //] >Hnthi
237           >nth_change_vec // normalize
238           >Hnthi in Hci;normalize #H destruct (H) %
239         ]
240 ]]]
241 qed.      
242  
243 lemma terminate_compare :  ∀i,j,sig,n,t.
244   i ≠ j → i < S n → j < S n → 
245   compare i j sig n  ↓ t.
246 #i #j #sig #n #t #Hneq #Hi #Hj
247 @(terminate_while … (sem_comp_step …)) //
248 <(change_vec_same … t i (niltape ?))
249 cases (nth i (tape sig) t (niltape ?))
250 [ % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct 
251 |2,3: #a0 #al0 % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
252 | #ls #c #rs lapply c -c lapply ls -ls lapply t -t elim rs
253   [#t #ls #c % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?);
254    #H1 destruct (H1) #Hxsep >change_vec_change_vec #Ht1 % 
255    #t2 * #x0 * * >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
256    >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H)
257   |#r0 #rs0 #IH #t #ls #c % #t1 * #x * * >nth_change_vec //
258    normalize in ⊢ (%→?); #H destruct (H) #Hcur
259    >change_vec_change_vec >change_vec_commute // #Ht1 >Ht1 @IH
260   ]
261 ]
262 qed.
263
264 lemma sem_compare : ∀i,j,sig,n.
265   i ≠ j → i < S n → j < S n → 
266   compare i j sig n ⊨ R_compare i j sig n.
267 #i #j #sig #n #Hneq #Hi #Hj @WRealize_to_Realize /2/
268 qed.
269
270 (*
271    |conf1   $
272    |confin 0/1 confout move
273
274   match machine step ≝
275     compare;
276     if (cur(src) != $)
277       then
278         parmoveL;
279         moveR(dst);
280       else nop
281  *)
282
283 definition match_step ≝ λsrc,dst,sig,n,is_startc,is_endc.
284   compare src dst sig n ·
285     (ifTM ?? (inject_TM ? (test_char ? is_endc) n src) 
286       (single_finalTM ??
287         (parmove src dst sig n L is_startc · (inject_TM ? (move_r ?) n dst)))
288       (nop ? n)
289       tc_false).
290
291 definition R_match_step_false ≝ 
292   λsrc,dst,sig,n,is_endc.λint,outt: Vector (tape sig) (S n).
293     ∃ls,ls0,rs,rs0,x,xs,end,c.
294     is_endc end = true ∧
295     nth src ? int (niltape ?) = midtape sig ls x (xs@end::rs) ∧
296     nth dst ? int (niltape ?) = midtape sig ls0 x (xs@c::rs0) ∧
297     outt = change_vec ??
298            (change_vec ?? int (midtape sig (reverse ? xs@x::ls) end rs) src)
299            (midtape sig (reverse ? xs@x::ls0) c rs0) dst.
300
301 (*
302   src : | 
303 *)
304
305 definition R_match_step_true ≝ 
306   λsrc,dst,sig,n,is_startc,is_endc.λint,outt: Vector (tape sig) (S n).
307   ∀s.current sig (nth src (tape sig) int (niltape sig)) = Some ? s → 
308   is_startc s = true →
309   (∀s1.current sig (nth dst (tape sig) int (niltape sig)) = Some ? s1 →
310    s ≠ s1 →  
311    outt = change_vec ?? int 
312           (tape_move … (nth dst ? int (niltape ?)) (Some ? 〈s1,R〉)) dst ∧ is_endc s = false) ∧  
313   (∀ls,x,xs,ci,rs,ls0,cj,rs0. 
314     nth src ? int (niltape ?) = midtape sig ls x (xs@ci::rs) →
315     nth dst ? int (niltape ?) = midtape sig ls0 x (xs@cj::rs0) → ci ≠ cj → 
316     outt = change_vec ?? int 
317            (tape_move … (nth dst ? int (niltape ?)) (Some ? 〈x,R〉)) dst ∧ is_endc ci = false).
318
319 definition Rtc_multi_true ≝ 
320   λalpha,test,n,i.λt1,t2:Vector ? (S n).
321    (∃c. current alpha (nth i ? t1 (niltape ?)) = Some ? c ∧ test c = true) ∧ t2 = t1.
322    
323 definition Rtc_multi_false ≝ 
324   λalpha,test,n,i.λt1,t2:Vector ? (S n).
325     (∀c. current alpha (nth i ? t1 (niltape ?)) = Some ? c → test c = false) ∧ t2 = t1.
326     
327 lemma sem_test_char_multi :
328   ∀alpha,test,n,i.i ≤ n → 
329   inject_TM ? (test_char ? test) n i ⊨ 
330   [ tc_true : Rtc_multi_true alpha test n i, Rtc_multi_false alpha test n i ].
331 #alpha #test #n #i #Hin #int
332 cases (acc_sem_inject … Hin (sem_test_char alpha test) int)
333 #k * #outc * * #Hloop #Htrue #Hfalse %{k} %{outc} % [ %
334 [ @Hloop
335 | #Hqtrue lapply (Htrue Hqtrue) * * * #c *
336   #Hcur #Htestc #Hnth_i #Hnth_j %
337   [ %{c} % //
338   | @(eq_vec … (niltape ?)) #i0 #Hi0
339     cases (decidable_eq_nat i0 i) #Hi0i
340     [ >Hi0i @Hnth_i
341     | @sym_eq @Hnth_j @sym_not_eq // ] ] ]
342 | #Hqfalse lapply (Hfalse Hqfalse) * * #Htestc #Hnth_i #Hnth_j %
343   [ @Htestc
344   | @(eq_vec … (niltape ?)) #i0 #Hi0
345     cases (decidable_eq_nat i0 i) #Hi0i
346     [ >Hi0i @Hnth_i
347     | @sym_eq @Hnth_j @sym_not_eq // ] ] ]
348 qed.
349
350 lemma sem_match_step :
351   ∀src,dst,sig,n,is_startc,is_endc.src ≠ dst → src < S n → dst < S n → 
352   match_step src dst sig n is_startc is_endc ⊨ 
353     [ inr … (inr … (inr … start_nop)) : 
354       R_match_step_true src dst sig n is_startc is_endc, 
355       R_match_step_false src dst sig n is_endc ].
356 #src #dst #sig #n #is_startc #is_endc #Hneq #Hsrc #Hdst
357 @(acc_sem_seq_app … (sem_compare … Hneq Hsrc Hdst)
358     (acc_sem_if … (sem_test_char_multi ? ()
359       (sem_nop …)
360         (sem_seq … sem_mark_next_tuple 
361            (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
362              (sem_mark ?) (sem_seq … (sem_move_l …) (sem_init_current …))))))
363   (sem_nop ?) …)
364
365  #int