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 ? (nth i ? int (niltape ?))
188 ≠ current ? (nth j ? int (niltape ?)) ∨
189 current ? (nth i ? int (niltape ?)) = None ? ∨
190 current ? (nth j ? int (niltape ?)) = None ?) → outt = int) ∧
191 (∀ls,x,xs,ci,rs,ls0,cj,rs0.
192 nth i ? int (niltape ?) = midtape sig ls x (xs@ci::rs) →
193 nth j ? int (niltape ?) = midtape sig ls0 x (xs@cj::rs0) → ci ≠ cj →
195 (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) i)
196 (midtape sig (reverse ? xs@x::ls0) cj rs0) j).
198 lemma wsem_compare : ∀i,j,sig,n.i ≠ j → i < S n → j < S n →
199 compare i j sig n ⊫ R_compare i j sig n.
200 #i #j #sig #n #Hneq #Hi #Hj #ta #k #outc #Hloop
201 lapply (sem_while … (sem_comp_step i j sig n Hneq Hi Hj) … Hloop) //
202 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
203 [ #tc whd in ⊢ (%→?); * * [ *
206 | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hnthi #Hnthj
207 >Hnthi in Hcicj; >Hnthj normalize in ⊢ (%→?); * #H @False_ind @H %
211 | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hnthi >Hnthi in Hci;
212 normalize in ⊢ (%→?); #H destruct (H) ] ]
215 | #ls #x #xs #ci #rs #ls0 #cj #rs0 #_ #Hnthj >Hnthj in Hcj;
216 normalize in ⊢ (%→?); #H destruct (H) ] ]
217 | #tc #td #te * #x * * #Hci #Hcj #Hd #Hstar #IH #He lapply (IH He) -IH *
219 [ >Hci >Hcj * [* [* #H @False_ind @H % | #H destruct (H)] | #H destruct (H)]
220 | #ls #c0 #xs #ci #rs #ls0 #cj #rs0 cases xs
221 [ #Hnthi #Hnthj #Hcicj >IH1
223 [ @eq_f3 // >(?:c0=x) [ >Hnthi % ]
224 >Hnthi in Hci;normalize #H destruct (H) %
225 | >(?:c0=x) [ >Hnthj % ]
226 >Hnthi in Hci;normalize #H destruct (H) % ]
227 | >Hd >nth_change_vec // >nth_change_vec_neq [|@sym_not_eq //]
228 >nth_change_vec // >Hnthi >Hnthj normalize %1 %1 @(not_to_not ??? Hcicj)
230 | #x0 #xs0 #Hnthi #Hnthj #Hcicj
231 >(IH2 (c0::ls) x0 xs0 ci rs (c0::ls0) cj rs0 … Hcicj)
232 [ >Hd >change_vec_commute in ⊢ (??%?); //
233 >change_vec_change_vec >change_vec_commute in ⊢ (??%?); //
235 | >Hd >nth_change_vec // >Hnthj normalize
236 >Hnthi in Hci;normalize #H destruct (H) %
237 | >Hd >nth_change_vec_neq [|@sym_not_eq //] >Hnthi
238 >nth_change_vec // normalize
239 >Hnthi in Hci;normalize #H destruct (H) %
244 lemma terminate_compare : ∀i,j,sig,n,t.
245 i ≠ j → i < S n → j < S n →
246 compare i j sig n ↓ t.
247 #i #j #sig #n #t #Hneq #Hi #Hj
248 @(terminate_while … (sem_comp_step …)) //
249 <(change_vec_same … t i (niltape ?))
250 cases (nth i (tape sig) t (niltape ?))
251 [ % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
252 |2,3: #a0 #al0 % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
253 | #ls #c #rs lapply c -c lapply ls -ls lapply t -t elim rs
254 [#t #ls #c % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?);
255 #H1 destruct (H1) #Hxsep >change_vec_change_vec #Ht1 %
256 #t2 * #x0 * * >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
257 >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H)
258 |#r0 #rs0 #IH #t #ls #c % #t1 * #x * * >nth_change_vec //
259 normalize in ⊢ (%→?); #H destruct (H) #Hcur
260 >change_vec_change_vec >change_vec_commute // #Ht1 >Ht1 @IH
265 lemma sem_compare : ∀i,j,sig,n.
266 i ≠ j → i < S n → j < S n →
267 compare i j sig n ⊨ R_compare i j sig n.
268 #i #j #sig #n #Hneq #Hi #Hj @WRealize_to_Realize /2/
273 |confin 0/1 confout move
284 definition match_step ≝ λsrc,dst,sig,n,is_startc,is_endc.
285 compare src dst sig n ·
286 (ifTM ?? (inject_TM ? (test_char ? (λa.is_endc a == false)) n src)
288 (parmove src dst sig n L is_startc · (inject_TM ? (move_r ?) n dst)))
292 definition Rtc_multi_true ≝
293 λalpha,test,n,i.λt1,t2:Vector ? (S n).
294 (∃c. current alpha (nth i ? t1 (niltape ?)) = Some ? c ∧ test c = true) ∧ t2 = t1.
296 definition Rtc_multi_false ≝
297 λalpha,test,n,i.λt1,t2:Vector ? (S n).
298 (∀c. current alpha (nth i ? t1 (niltape ?)) = Some ? c → test c = false) ∧ t2 = t1.
300 definition R_match_step_false ≝
301 λsrc,dst,sig,n,is_endc.λint,outt: Vector (tape sig) (S n).
302 ((current ? (nth src ? int (niltape ?)) ≠ current ? (nth dst ? int (niltape ?)) ∨
303 current sig (nth src (tape sig) int (niltape sig)) = None ? ∨
304 current sig (nth dst (tape sig) int (niltape sig)) = None ? ) ∧ outt = int) ∨
305 ∃ls,ls0,rs,rs0,x,xs. ∀rsi,rsj,end,c.
306 rs = end::rsi → rs0 = c::rsj →
308 nth src ? int (niltape ?) = midtape sig ls x (xs@rs) ∧
309 nth dst ? int (niltape ?) = midtape sig ls0 x (xs@rs0) ∧
311 (change_vec ?? int (midtape sig (reverse ? xs@x::ls) end rsi) src)
312 (midtape sig (reverse ? xs@x::ls0) c rsj) dst.
314 definition R_match_step_true ≝
315 λsrc,dst,sig,n,is_startc,is_endc.λint,outt: Vector (tape sig) (S n).
316 ∀s.current sig (nth src (tape sig) int (niltape sig)) = Some ? s →
318 (∀s1.current sig (nth dst (tape sig) int (niltape sig)) = Some ? s1 →
320 outt = change_vec ?? int
321 (tape_move … (nth dst ? int (niltape ?)) (Some ? 〈s1,R〉)) dst ∧ is_endc s = false) ∧
322 (∀ls,x,xs,ci,rs,ls0,cj,rs0.
323 nth src ? int (niltape ?) = midtape sig ls x (xs@ci::rs) →
324 nth dst ? int (niltape ?) = midtape sig ls0 x (xs@cj::rs0) → ci ≠ cj →
325 outt = change_vec ?? int
326 (tape_move … (nth dst ? int (niltape ?)) (Some ? 〈x,R〉)) dst ∧ is_endc ci = false).
328 lemma sem_test_char_multi :
329 ∀alpha,test,n,i.i ≤ n →
330 inject_TM ? (test_char ? test) n i ⊨
331 [ tc_true : Rtc_multi_true alpha test n i, Rtc_multi_false alpha test n i ].
332 #alpha #test #n #i #Hin #int
333 cases (acc_sem_inject … Hin (sem_test_char alpha test) int)
334 #k * #outc * * #Hloop #Htrue #Hfalse %{k} %{outc} % [ %
336 | #Hqtrue lapply (Htrue Hqtrue) * * * #c *
337 #Hcur #Htestc #Hnth_i #Hnth_j %
339 | @(eq_vec … (niltape ?)) #i0 #Hi0
340 cases (decidable_eq_nat i0 i) #Hi0i
342 | @sym_eq @Hnth_j @sym_not_eq // ] ] ]
343 | #Hqfalse lapply (Hfalse Hqfalse) * * #Htestc #Hnth_i #Hnth_j %
345 | @(eq_vec … (niltape ?)) #i0 #Hi0
346 cases (decidable_eq_nat i0 i) #Hi0i
348 | @sym_eq @Hnth_j @sym_not_eq // ] ] ]
351 axiom comp_list: ∀S:DeqSet. ∀l1,l2:list S. ∃l,tl1,tl2.
352 l1 = l@tl1 ∧ l2 = l@tl2 ∧ ∀a,b,tla,tlb. tl1 = a::tla → tl2 = b::tlb → a≠b.
354 axiom daemon : ∀X:Prop.X.
356 lemma sem_match_step :
357 ∀src,dst,sig,n,is_startc,is_endc.src ≠ dst → src < S n → dst < S n →
358 match_step src dst sig n is_startc is_endc ⊨
359 [ inr ?? (inr ?? (inl … (inr ?? start_nop))) :
360 R_match_step_true src dst sig n is_startc is_endc,
361 R_match_step_false src dst sig n is_endc ].
362 #src #dst #sig #n #is_startc #is_endc #Hneq #Hsrc #Hdst
363 @(acc_sem_seq_app sig n … (sem_compare src dst sig n Hneq Hsrc Hdst)
364 (acc_sem_if ? n … (sem_test_char_multi sig (λa.is_endc a == false) n src (le_S_S_to_le … Hsrc))
366 (sem_parmoveL ???? is_startc Hneq Hsrc Hdst)
367 (sem_inject … dst (le_S_S_to_le … Hdst) (sem_move_r ? )))
369 [#ta #tb #tc * #Hcomp1 #Hcomp2 * #td * * * #c * #Hcurtc #Hcend #Htd >Htd -Htd
370 #Htb #s #Hcurta_src #Hstart %
371 [ #s1 #Hcurta_dst #Hneqss1
372 lapply Htb lapply Hcurtc -Htb -Hcurtc >(?:tc=ta)
373 [|@Hcomp1 % % >Hcurta_src >Hcurta_dst @(not_to_not … Hneqss1) #H destruct (H) % ]
374 #Hcurtc * #te * * #_ #Hte >Hte // whd in ⊢ (%→?); * * #_ #Htbdst #Htbelse %
375 [ @(eq_vec … (niltape ?)) #i #Hi cases (decidable_eq_nat i dst) #Hidst
376 [ >Hidst >nth_change_vec // cases (current_to_midtape … Hcurta_dst)
377 #ls * #rs #Hta_mid >(Htbdst … Hta_mid) >Hta_mid cases rs //
378 | >nth_change_vec_neq [|@sym_not_eq //] @sym_eq @Htbelse @sym_not_eq // ]
379 | >Hcurtc in Hcurta_src; #H destruct (H) cases (is_endc s) in Hcend;
380 normalize #H destruct (H) // ]
381 |#ls #x #xs #ci #rs #ls0 #cj #rs0 #Htasrc_mid #Htadst_mid #Hcicj
382 lapply (Hcomp2 … Htasrc_mid Htadst_mid Hcicj) -Hcomp2 #Hcomp2
383 cases Htb #td * * #Htd #_ >Htasrc_mid in Hcurta_src; normalize in ⊢ (%→?);
385 >(Htd ls ci (reverse ? xs) rs s ??? ls0 cj (reverse ? xs) s rs0 (refl ??)) //
386 [| >Hcomp2 >nth_change_vec //
388 | >Hcomp2 >nth_change_vec_neq [|@sym_not_eq // ] @nth_change_vec // ]
389 * * #_ #Htbdst #Htbelse %
390 [ @(eq_vec … (niltape ?)) #i #Hi cases (decidable_eq_nat i dst) #Hidst
391 [ >Hidst >nth_change_vec // >Htadst_mid >(Htbdst ls0 s (xs@cj::rs0))
393 | >nth_change_vec // ]
394 | >nth_change_vec_neq [|@sym_not_eq //]
395 <Htbelse [|@sym_not_eq // ]
396 >nth_change_vec_neq [|@sym_not_eq //]
398 cases (decidable_eq_nat i src) #Hisrc
399 [ >Hisrc >nth_change_vec // >Htasrc_mid //
400 | >nth_change_vec_neq [|@sym_not_eq //]
401 <(Htbelse i) [|@sym_not_eq // ]
402 >Hcomp2 >nth_change_vec_neq [|@sym_not_eq // ]
403 >nth_change_vec_neq [|@sym_not_eq // ] //
406 | >Hcomp2 in Hcurtc; >nth_change_vec_neq [|@sym_not_eq //]
407 >nth_change_vec // whd in ⊢ (??%?→?);
408 #H destruct (H) cases (is_endc c) in Hcend;
409 normalize #H destruct (H) // ]
411 |#intape #outtape #ta * #Hcomp1 #Hcomp2 * #tb * * #Hc #Htb
412 whd in ⊢ (%→?); #Hout >Hout >Htb whd
413 lapply (current_to_midtape sig (nth src ? intape (niltape ?)))
414 cases (current … (nth src ? intape (niltape ?))) in Hcomp1;
415 [#Hcomp1 #_ %1 % [%1 %2 // | @Hcomp1 %1 %2 %]
416 |#c_src lapply (current_to_midtape sig (nth dst ? intape (niltape ?)))
417 cases (current … (nth dst ? intape (niltape ?)))
418 [#_ #Hcomp1 #_ %1 % [%2 % | @Hcomp1 %2 %]
419 |#c_dst cases (true_or_false (c_src == c_dst)) #Hceq
420 [#Hmid_dst cases (Hmid_dst c_dst (refl …)) -Hmid_dst
421 #ls_dst * #rs_dst #Hmid_dst #_
422 #Hmid_src cases (Hmid_src c_src (refl …)) -Hmid_src
423 #ls_src * #rs_src #Hmid_src %2
424 cases (comp_list … rs_src rs_dst) #xs * #rsi * #rsj * *
425 #Hrs_src #Hrs_dst #Hneq
426 %{ls_src} %{ls_dst} %{rsi} %{rsj} %{c_src} %{xs}
427 #rsi0 #rsj0 #end #c #Hend #Hc_dst
428 >Hrs_src in Hmid_src; >Hend #Hmid_src
429 >Hrs_dst in Hmid_dst; >Hc_dst <(\P Hceq) #Hmid_dst
430 lapply(Hcomp2 … Hmid_src Hmid_dst ?)
431 [@(Hneq … Hend Hc_dst)]
432 -Hcomp2 #Hcomp2 <Hcomp2
434 [>Hcomp2 in Hc; >nth_change_vec_neq [|@sym_not_eq //]
435 >nth_change_vec // #H lapply (H ? (refl …))
436 cases (is_endc end) normalize //
440 [% % @(not_to_not ??? (\Pf Hceq)) #H destruct (H) //
441 |@Hcomp1 %1 %1 @(not_to_not ??? (\Pf Hceq)) #H destruct (H) //