1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| The HELM team. *)
8 (* ||A|| http://helm.cs.unibo.it *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU General Public License Version 2 *)
13 (**************************************************************************)
15 include "turing/multi_universal/moves.ma".
16 include "turing/if_multi.ma".
17 include "turing/inject.ma".
18 include "turing/basic_machines.ma".
20 definition compare_states ≝ initN 3.
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 …)).
28 0) (x,x) → (x,x)(R,R) → 1
35 definition trans_compare_step ≝
37 λp:compare_states × (Vector (option sig) (S n)).
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)
48 else 〈comp2,null_action ? n〉 ]
51 [ O ⇒ (* 1 *) 〈comp1,null_action ? n〉
52 | S _ ⇒ (* 2 *) 〈comp2,null_action ? n〉 ] ].
54 definition compare_step ≝
56 mk_mTM sig n compare_states (trans_compare_step i j sig n)
57 comp0 (λq.q == comp1 ∨ q == comp2).
59 definition R_comp_step_true ≝
60 λi,j,sig,n.λint,outt: Vector (tape sig) (S n).
62 current ? (nth i ? int (niltape ?)) = Some ? x ∧
63 current ? (nth j ? int (niltape ?)) = Some ? x ∧
66 (tape_move ? (nth i ? int (niltape ?)) (Some ? 〈x,R〉)) i)
67 (tape_move ? (nth j ? int (niltape ?)) (Some ? 〈x,R〉)) j.
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.
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 ⊢ (?→??%?);
85 [ whd in ⊢ (??(???%)?); >Hcurrent %
86 | whd in ⊢ (??(???????(???%))?); >Hcurrent @tape_move_null_action ]
88 [ whd in ⊢ (??(???%)?); >Hcurrent cases (nth i ?? (None sig)) //
89 | whd in ⊢ (??(???????(???%))?); >Hcurrent
90 cases (nth i ?? (None sig)) [|#x] @tape_move_null_action ] ]
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 //
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
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) =
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 //
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 ⊢ (???%→?);
148 [ whd in ⊢ (??%?); >comp_q0_q2_null /2/ % <Hcuri in ⊢ (???%);
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 ⊢ (???%→?);
156 [ whd in ⊢ (??%?); >comp_q0_q2_null /2/ %2 <Hcurj in ⊢ (???%);
158 | normalize in ⊢ (%→?); #H destruct (H) ]
159 | #_ % >Ha >Hcurj % % % #H destruct (H) ] ]
160 | #b #Hb %{2} cases (true_or_false (a == b)) #Hab
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 %
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) % ] ]
182 definition compare ≝ λi,j,sig,n.
183 whileTM … (compare_step i j sig n) comp1.
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)) →
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 →
194 (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) i)
195 (midtape sig (reverse ? xs@x::ls0) cj rs0) j).
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 ⊢ (%→?); * * [ *
205 | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hnthi #Hnthj
206 >Hnthi in Hcicj; >Hnthj normalize in ⊢ (%→?); * #H @False_ind @H %
210 | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hnthi >Hnthi in Hci;
211 normalize in ⊢ (%→?); #H destruct (H) ] ]
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 *
218 [ >Hci >Hcj * #H @False_ind @H %
219 | #ls #c0 #xs #ci #rs #ls0 #cj #rs0 cases xs
220 [ #Hnthi #Hnthj #Hcicj >IH1
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)
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 ⊢ (??%?); //
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) %
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
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/
272 |confin 0/1 confout move
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)
287 (parmove src dst sig n L is_startc · (inject_TM ? (move_r ?) n dst)))
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.
295 nth src ? int (niltape ?) = midtape sig ls x (xs@end::rs) ∧
296 nth dst ? int (niltape ?) = midtape sig ls0 x (xs@c::rs0) ∧
298 (change_vec ?? int (midtape sig (reverse ? xs@x::ls) end rs) src)
299 (midtape sig (reverse ? xs@x::ls0) c rs0) dst.
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 →
309 (∀s1.current sig (nth dst (tape sig) int (niltape sig)) = Some ? 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).
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.
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.
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} % [ %
335 | #Hqtrue lapply (Htrue Hqtrue) * * * #c *
336 #Hcur #Htestc #Hnth_i #Hnth_j %
338 | @(eq_vec … (niltape ?)) #i0 #Hi0
339 cases (decidable_eq_nat i0 i) #Hi0i
341 | @sym_eq @Hnth_j @sym_not_eq // ] ] ]
342 | #Hqfalse lapply (Hfalse Hqfalse) * * #Htestc #Hnth_i #Hnth_j %
344 | @(eq_vec … (niltape ?)) #i0 #Hi0
345 cases (decidable_eq_nat i0 i) #Hi0i
347 | @sym_eq @Hnth_j @sym_not_eq // ] ] ]
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 ? ()
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 …))))))