2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department of the University of Bologna, Italy.
8 \ / This file is distributed under the terms of the
9 \ / GNU General Public License Version 2
10 V_____________________________________________________________*)
17 include "turing/if_machine.ma".
18 include "turing/basic_machines.ma".
19 include "turing/universal/alphabet.ma".
21 (* ADVANCE TO MARK (right)
23 sposta la testina a destra fino a raggiungere il primo carattere marcato
27 (* 0, a ≠ mark _ ⇒ 0, R
28 0, a = mark _ ⇒ 1, N *)
30 definition atm_states ≝ initN 3.
32 definition atm0 : atm_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 3 (refl …)).
33 definition atm1 : atm_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 3 (refl …)).
34 definition atm2 : atm_states ≝ mk_Sig ?? 2 (leb_true_to_le 3 3 (refl …)).
36 definition atmr_step ≝
37 λalpha:FinSet.λtest:alpha→bool.
38 mk_TM alpha atm_states
41 [ None ⇒ 〈atm1, None ?〉
44 [ true ⇒ 〈atm1,None ?〉
45 | false ⇒ 〈atm2,Some ? 〈a',R〉〉 ]])
46 atm0 (λx.notb (x == atm0)).
48 definition Ratmr_step_true ≝
51 t1 = midtape alpha ls a rs ∧ test a = false ∧
52 t2 = mk_tape alpha (a::ls) (option_hd ? rs) (tail ? rs).
54 definition Ratmr_step_false ≝
57 (current alpha t1 = None ? ∨
58 (∃a.current ? t1 = Some ? a ∧ test a = true)).
61 ∀alpha,test,ls,a0,rs. test a0 = true →
62 step alpha (atmr_step alpha test)
63 (mk_config ?? atm0 (midtape … ls a0 rs)) =
64 mk_config alpha (states ? (atmr_step alpha test)) atm1
66 #alpha #test #ls #a0 #ts #Htest whd in ⊢ (??%?);
67 whd in match (trans … 〈?,?〉); >Htest %
71 ∀alpha,test,ls,a0,rs. test a0 = false →
72 step alpha (atmr_step alpha test)
73 (mk_config ?? atm0 (midtape … ls a0 rs)) =
74 mk_config alpha (states ? (atmr_step alpha test)) atm2
75 (mk_tape … (a0::ls) (option_hd ? rs) (tail ? rs)).
76 #alpha #test #ls #a0 #ts #Htest whd in ⊢ (??%?);
77 whd in match (trans … 〈?,?〉); >Htest cases ts //
82 accRealize alpha (atmr_step alpha test)
83 atm2 (Ratmr_step_true alpha test) (Ratmr_step_false alpha test).
86 @(ex_intro ?? (mk_config ?? atm1 (niltape ?))) %
87 [ % // whd in ⊢ ((??%%)→?); #Hfalse destruct | #_ % // % % ]
88 | #a #al @(ex_intro ?? 2) @(ex_intro ?? (mk_config ?? atm1 (leftof ? a al)))
89 % [ % // whd in ⊢ ((??%%)→?); #Hfalse destruct | #_ % // % % ]
90 | #a #al @(ex_intro ?? 2) @(ex_intro ?? (mk_config ?? atm1 (rightof ? a al)))
91 % [ % // whd in ⊢ ((??%%)→?); #Hfalse destruct | #_ % // % % ]
92 | #ls #c #rs @(ex_intro ?? 2)
93 cases (true_or_false (test c)) #Htest
94 [ @(ex_intro ?? (mk_config ?? atm1 ?))
97 [ whd in ⊢ (??%?); >atmr_q0_q1 //
98 | whd in ⊢ ((??%%)→?); #Hfalse destruct ]
99 | #_ % // %2 @(ex_intro ?? c) % // ]
101 | @(ex_intro ?? (mk_config ?? atm2 (mk_tape ? (c::ls) (option_hd ? rs) (tail ? rs))))
104 [ whd in ⊢ (??%?); >atmr_q0_q2 //
105 | #_ @(ex_intro ?? ls) @(ex_intro ?? c) @(ex_intro ?? rs)
108 | #Hfalse @False_ind @(absurd ?? Hfalse) %
114 definition R_adv_to_mark_r ≝ λalpha,test,t1,t2.
116 (t1 = midtape alpha ls c rs →
117 ((test c = true ∧ t2 = t1) ∨
119 ∀rs1,b,rs2. rs = rs1@b::rs2 →
120 test b = true → (∀x.memb ? x rs1 = true → test x = false) →
121 t2 = midtape ? (reverse ? rs1@c::ls) b rs2))).
123 definition adv_to_mark_r ≝
124 λalpha,test.whileTM alpha (atmr_step alpha test) atm2.
126 lemma wsem_adv_to_mark_r :
128 WRealize alpha (adv_to_mark_r alpha test) (R_adv_to_mark_r alpha test).
129 #alpha #test #t #i #outc #Hloop
130 lapply (sem_while … (sem_atmr_step alpha test) t i outc Hloop) [%]
131 -Hloop * #t1 * #Hstar @(star_ind_l ??????? Hstar)
133 [ #H1 #ls #c #rs #H2 >H2 in H1; whd in ⊢ (??%? → ?);
134 #Hfalse destruct (Hfalse)
135 | * #a * #Ha #Htest #ls #c #rs #H2 %
136 >H2 in Ha; whd in ⊢ (??%? → ?); #Heq destruct (Heq) % //
139 | #tapea #tapeb #tapec #Hleft #Hright #IH #HRfalse
140 lapply (IH HRfalse) -IH #IH
141 #ls #c #rs #Htapea %2
142 cases Hleft #ls0 * #a0 * #rs0 * * #Htapea' #Htest #Htapeb
144 >Htapea' in Htapea; #Htapea destruct (Htapea) % // *
145 [ #b #rs2 #Hrs >Hrs in Htapeb; #Htapeb #Htestb #_
147 [ * #_ #Houtc >Houtc >Htapeb %
148 | * #Hfalse >Hfalse in Htestb; #Htestb destruct (Htestb) ]
149 | #r1 #rs1 #b #rs2 #Hrs >Hrs in Htapeb; #Htapeb #Htestb #Hmemb
151 [ * #Hfalse >(Hmemb …) in Hfalse;
152 [ #Hft destruct (Hft)
154 | * #Htestr1 #H1 >reverse_cons >associative_append
155 @H1 // #x #Hx @Hmemb @memb_cons //
160 lemma terminate_adv_to_mark_r :
162 ∀t.Terminate alpha (adv_to_mark_r alpha test) t.
164 @(terminate_while … (sem_atmr_step alpha test))
167 [ % #t1 * #ls0 * #c0 * #rs0 * * #Hfalse destruct (Hfalse)
168 |2,3: #a0 #al0 % #t1 * #ls0 * #c0 * #rs0 * * #Hfalse destruct (Hfalse)
169 | #ls #c #rs generalize in match c; -c generalize in match ls; -ls
171 [#ls #c % #t1 * #ls0 * #c0 * #rs0 * *
172 #H1 destruct (H1) #Hc0 #Ht1 normalize in Ht1;
173 % #t2 * #ls1 * #c1 * #rs1 * * >Ht1
174 normalize in ⊢ (%→?); #Hfalse destruct (Hfalse)
175 | #r0 #rs0 #IH #ls #c % #t1 * #ls0 * #c0 * #rs0 * *
176 #H1 destruct (H1) #Hc0 #Ht1 normalize in Ht1;
183 lemma sem_adv_to_mark_r :
185 Realize alpha (adv_to_mark_r alpha test) (R_adv_to_mark_r alpha test).
191 marks the current character
194 definition mark_states ≝ initN 2.
196 definition ms0 : mark_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 2 (refl …)).
197 definition ms1 : mark_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 2 (refl …)).
200 λalpha:FinSet.mk_TM (FinProd … alpha FinBool) mark_states
203 [ None ⇒ 〈ms1,None ?〉
204 | Some a' ⇒ match (pi1 … q) with
205 [ O ⇒ let 〈a'',b〉 ≝ a' in 〈ms1,Some ? 〈〈a'',true〉,N〉〉
206 | S q ⇒ 〈ms1,None ?〉 ] ])
209 definition R_mark ≝ λalpha,t1,t2.
211 t1 = midtape (FinProd … alpha FinBool) ls 〈c,b〉 rs →
212 t2 = midtape ? ls 〈c,true〉 rs.
215 ∀alpha.Realize ? (mark alpha) (R_mark alpha).
216 #alpha #intape @(ex_intro ?? 2) cases intape
218 [| % [ % | #ls #c #b #rs #Hfalse destruct ] ]
220 [| % [ % | #ls #c #b #rs #Hfalse destruct ] ]
222 [| % [ % | #ls #c #b #rs #Hfalse destruct ] ]
224 @ex_intro [| % [ % | #ls0 #c0 #b0 #rs0 #H1 destruct (H1) % ] ] ]
228 (* MOVE RIGHT AND MARK machine
230 marks the first character on the right
232 (could be rewritten using (mark; move_right))
235 definition mrm_states ≝ initN 3.
237 definition mrm0 : mrm_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 3 (refl …)).
238 definition mrm1 : mrm_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 3 (refl …)).
239 definition mrm2 : mrm_states ≝ mk_Sig ?? 2 (leb_true_to_le 3 3 (refl …)).
241 definition move_right_and_mark ≝
242 λalpha:FinSet.mk_TM (FinProd … alpha FinBool) mrm_states
245 [ None ⇒ 〈mrm2,None ?〉
246 | Some a' ⇒ match pi1 … q with
247 [ O ⇒ 〈mrm1,Some ? 〈a',R〉〉
249 [ O ⇒ let 〈a'',b〉 ≝ a' in
250 〈mrm2,Some ? 〈〈a'',true〉,N〉〉
251 | S _ ⇒ 〈mrm2,None ?〉 ] ] ])
254 definition R_move_right_and_mark ≝ λalpha,t1,t2.
256 t1 = midtape (FinProd … alpha FinBool) ls c (〈d,b〉::rs) →
257 t2 = midtape ? (c::ls) 〈d,true〉 rs.
259 lemma sem_move_right_and_mark :
260 ∀alpha.Realize ? (move_right_and_mark alpha) (R_move_right_and_mark alpha).
261 #alpha #intape @(ex_intro ?? 3) cases intape
263 [| % [ % | #ls #c #d #b #rs #Hfalse destruct ] ]
265 [| % [ % | #ls #c #d #b #rs #Hfalse destruct ] ]
267 [| % [ % | #ls #c #d #b #rs #Hfalse destruct ] ]
269 [ @ex_intro [| % [ % | #ls0 #c0 #d0 #b0 #rs0 #Hfalse destruct ] ]
270 | * #d #b #rs @ex_intro
271 [| % [ % | #ls0 #c0 #d0 #b0 #rs0 #H1 destruct (H1) % ] ] ] ]
274 (* CLEAR MARK machine
276 clears the mark in the current character
279 definition clear_mark_states ≝ initN 3.
281 definition clear0 : clear_mark_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 3 (refl …)).
282 definition clear1 : clear_mark_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 3 (refl …)).
283 definition claer2 : clear_mark_states ≝ mk_Sig ?? 2 (leb_true_to_le 3 3 (refl …)).
285 definition clear_mark ≝
286 λalpha:FinSet.mk_TM (FinProd … alpha FinBool) clear_mark_states
289 [ None ⇒ 〈clear1,None ?〉
290 | Some a' ⇒ match pi1 … q with
291 [ O ⇒ let 〈a'',b〉 ≝ a' in 〈clear1,Some ? 〈〈a'',false〉,N〉〉
292 | S q ⇒ 〈clear1,None ?〉 ] ])
293 clear0 (λq.q == clear1).
295 definition R_clear_mark ≝ λalpha,t1,t2.
297 t1 = midtape (FinProd … alpha FinBool) ls 〈c,b〉 rs →
298 t2 = midtape ? ls 〈c,false〉 rs.
300 lemma sem_clear_mark :
301 ∀alpha.Realize ? (clear_mark alpha) (R_clear_mark alpha).
302 #alpha #intape @(ex_intro ?? 2) cases intape
304 [| % [ % | #ls #c #b #rs #Hfalse destruct ] ]
306 [| % [ % | #ls #c #b #rs #Hfalse destruct ] ]
308 [| % [ % | #ls #c #b #rs #Hfalse destruct ] ]
310 @ex_intro [| % [ % | #ls0 #c0 #b0 #rs0 #H1 destruct (H1) % ] ] ]
313 (* ADVANCE MARK RIGHT machine
315 clears mark on current char,
316 moves right, and marks new current char
320 definition adv_mark_r ≝
322 seq ? (clear_mark alpha)
323 (seq ? (move_r ?) (mark alpha)).
325 definition R_adv_mark_r ≝ λalpha,t1,t2.
327 t1 = midtape (FinProd … alpha FinBool) ls 〈c,true〉 (〈d,b〉::rs) →
328 t2 = midtape ? (〈c,false〉::ls) 〈d,true〉 rs.
330 lemma sem_adv_mark_r :
331 ∀alpha.Realize ? (adv_mark_r alpha) (R_adv_mark_r alpha).
333 cases (sem_seq ????? (sem_clear_mark …)
334 (sem_seq ????? (sem_move_r ?) (sem_mark alpha)) intape)
335 #k * #outc * #Hloop whd in ⊢ (%→?);
336 * #ta * whd in ⊢ (%→?); #Hs1 * #tb * whd in ⊢ (%→%→?); #Hs2 #Hs3
337 @(ex_intro ?? k) @(ex_intro ?? outc) %
339 | -Hloop #ls #c #d #b #rs #Hintape @(Hs3 … b)
340 @(Hs2 ls 〈c,false〉 (〈d,b〉::rs))
345 (* ADVANCE TO MARK (left)
351 definition R_adv_to_mark_l ≝ λalpha,test,t1,t2.
353 (t1 = midtape alpha ls c rs →
354 ((test c = true ∧ t2 = t1) ∨
356 ∀ls1,b,ls2. ls = ls1@b::ls2 →
357 test b = true → (∀x.memb ? x ls1 = true → test x = false) →
358 t2 = midtape ? ls2 b (reverse ? ls1@c::rs)))).
360 axiom adv_to_mark_l : ∀alpha:FinSet.(alpha → bool) → TM alpha.
361 (* definition adv_to_mark_l ≝
362 λalpha,test.whileTM alpha (atml_step alpha test) 2. *)
364 axiom wsem_adv_to_mark_l :
366 WRealize alpha (adv_to_mark_l alpha test) (R_adv_to_mark_l alpha test).
368 #alpha #test #t #i #outc #Hloop
369 lapply (sem_while … (sem_atmr_step alpha test) t i outc Hloop) [%]
370 -Hloop * #t1 * #Hstar @(star_ind_l ??????? Hstar)
372 [ #H1 #ls #c #rs #H2 >H2 in H1; whd in ⊢ (??%? → ?);
373 #Hfalse destruct (Hfalse)
374 | * #a * #Ha #Htest #ls #c #rs #H2 %
375 >H2 in Ha; whd in ⊢ (??%? → ?); #Heq destruct (Heq) % //
378 | #tapea #tapeb #tapec #Hleft #Hright #IH #HRfalse
379 lapply (IH HRfalse) -IH #IH
380 #ls #c #rs #Htapea %2
381 cases Hleft #ls0 * #a0 * #rs0 * * #Htapea' #Htest #Htapeb
383 >Htapea' in Htapea; #Htapea destruct (Htapea) % // *
384 [ #b #rs2 #Hrs >Hrs in Htapeb; #Htapeb #Htestb #_
386 [ * #_ #Houtc >Houtc >Htapeb %
387 | * #Hfalse >Hfalse in Htestb; #Htestb destruct (Htestb) ]
388 | #r1 #rs1 #b #rs2 #Hrs >Hrs in Htapeb; #Htapeb #Htestb #Hmemb
390 [ * #Hfalse >(Hmemb …) in Hfalse;
391 [ #Hft destruct (Hft)
393 | * #Htestr1 #H1 >reverse_cons >associative_append
394 @H1 // #x #Hx @Hmemb @memb_cons //
400 axiom terminate_adv_to_mark_l :
402 ∀t.Terminate alpha (adv_to_mark_l alpha test) t.
405 @(terminate_while … (sem_atmr_step alpha test))
408 [ % #t1 * #ls0 * #c0 * #rs0 * * #Hfalse destruct (Hfalse)
409 |2,3: #a0 #al0 % #t1 * #ls0 * #c0 * #rs0 * * #Hfalse destruct (Hfalse)
410 | #ls #c #rs generalize in match c; -c generalize in match ls; -ls
412 [#ls #c % #t1 * #ls0 * #c0 * #rs0 * *
413 #H1 destruct (H1) #Hc0 #Ht1 normalize in Ht1;
414 % #t2 * #ls1 * #c1 * #rs1 * * >Ht1
415 normalize in ⊢ (%→?); #Hfalse destruct (Hfalse)
416 | #r0 #rs0 #IH #ls #c % #t1 * #ls0 * #c0 * #rs0 * *
417 #H1 destruct (H1) #Hc0 #Ht1 normalize in Ht1;
425 lemma sem_adv_to_mark_l :
427 Realize alpha (adv_to_mark_l alpha test) (R_adv_to_mark_l alpha test).
432 ADVANCE BOTH MARKS machine
434 l1 does not contain marks ⇒
446 definition adv_both_marks ≝
447 λalpha.seq ? (adv_mark_r alpha)
449 (seq ? (adv_to_mark_l (FinProd alpha FinBool) (is_marked alpha))
450 (adv_mark_r alpha))).
452 definition R_adv_both_marks ≝
454 ∀l0,x,a,l1,x0,a0,l2. (∀c.memb ? c l1 = true → is_marked ? c = false) →
455 t1 = midtape (FinProd … alpha FinBool)
456 (l1@〈a,false〉::〈x,true〉::l0) 〈x0,true〉 (〈a0,false〉::l2) →
457 t2 = midtape ? (〈x,false〉::l0) 〈a,true〉 (reverse ? l1@〈x0,false〉::〈a0,true〉::l2).
459 lemma sem_adv_both_marks :
460 ∀alpha.Realize ? (adv_both_marks alpha) (R_adv_both_marks alpha).
462 cases (sem_seq ????? (sem_adv_mark_r …)
463 (sem_seq ????? (sem_move_l …)
464 (sem_seq ????? (sem_adv_to_mark_l ? (is_marked ?))
465 (sem_adv_mark_r alpha))) intape)
466 #k * #outc * #Hloop whd in ⊢ (%→?);
467 * #ta * whd in ⊢ (%→?); #Hs1 * #tb * whd in ⊢ (%→?); #Hs2
468 * #tc * whd in ⊢ (%→%→?); #Hs3 #Hs4
469 @(ex_intro ?? k) @(ex_intro ?? outc) %
471 | -Hloop #l0 #x #a #l1 #x0 #a0 #l2 #Hl1 #Hintape
473 lapply (Hs1 … Hintape) #Hta
474 lapply (Hs2 … Hta) #Htb
476 [ * #Hfalse normalize in Hfalse; destruct (Hfalse)
478 lapply (Hs3 (l1@[〈a,false〉]) 〈x,true〉 l0 ???)
479 [ #x1 #Hx1 cases (memb_append … Hx1)
481 | #Hx1' >(memb_single … Hx1') % ]
483 | >associative_append %
484 | >reverse_append #Htc @Htc ]
496 l0 x a* l1 x0 a0* l2 (f(x0) == true)
498 l0 x* a l1 x0* a0 l2 (f(x0) == false)
502 definition match_and_adv ≝
503 λalpha,f.ifTM ? (test_char ? f)
504 (adv_both_marks alpha) (clear_mark ?) tc_true.
506 definition R_match_and_adv ≝
508 ∀l0,x,a,l1,x0,a0,l2. (∀c.memb ? c l1 = true → is_marked ? c = false) →
509 t1 = midtape (FinProd … alpha FinBool)
510 (l1@〈a,false〉::〈x,true〉::l0) 〈x0,true〉 (〈a0,false〉::l2) →
511 (f 〈x0,true〉 = true ∧ t2 = midtape ? (〈x,false〉::l0) 〈a,true〉 (reverse ? l1@〈x0,false〉::〈a0,true〉::l2))
512 ∨ (f 〈x0,true〉 = false ∧
513 t2 = midtape ? (l1@〈a,false〉::〈x,true〉::l0) 〈x0,false〉 (〈a0,false〉::l2)).
515 lemma sem_match_and_adv :
516 ∀alpha,f.Realize ? (match_and_adv alpha f) (R_match_and_adv alpha f).
518 cases (sem_if ? (test_char ? f) … tc_true (sem_test_char ? f) (sem_adv_both_marks alpha) (sem_clear_mark ?) intape)
519 #k * #outc * #Hloop #Hif @(ex_intro ?? k) @(ex_intro ?? outc)
522 [ * #ta * whd in ⊢ (%→%→?); #Hta #Houtc
523 #l0 #x #a #l1 #x0 #a0 #l2 #Hl1 #Hintape
524 >Hintape in Hta; #Hta cases (Hta … (refl ??)) -Hta #Hf #Hta % %
525 [ @Hf | @Houtc [ @Hl1 | @Hta ] ]
526 | * #ta * whd in ⊢ (%→%→?); #Hta #Houtc
527 #l0 #x #a #l1 #x0 #a0 #l2 #Hl1 #Hintape
528 >Hintape in Hta; #Hta cases (Hta … (refl ??)) -Hta #Hf #Hta %2 %
529 [ @Hf | >(Houtc … Hta) % ]
535 then move_right; ----
537 if current (* x0 *) = 0
538 then advance_mark ----
545 definition comp_step_subcase ≝
546 λalpha,c,elseM.ifTM ? (test_char ? (λx.x == c))
548 (seq ? (adv_to_mark_r ? (is_marked alpha))
549 (match_and_adv ? (λx.x == c))))
552 definition R_comp_step_subcase ≝
553 λalpha,c,RelseM,t1,t2.
554 ∀l0,x,rs.t1 = midtape (FinProd … alpha FinBool) l0 〈x,true〉 rs →
556 ∀a,l1,x0,a0,l2. (∀c.memb ? c l1 = true → is_marked ? c = false) →
557 rs = 〈a,false〉::l1@〈x0,true〉::〈a0,false〉::l2 →
559 t2 = midtape ? (〈x,false〉::l0) 〈a,true〉 (l1@〈x0,false〉::〈a0,true〉::l2)) ∨
561 t2 = midtape (FinProd … alpha FinBool)
562 (reverse ? l1@〈a,false〉::〈x,true〉::l0) 〈x0,false〉 (〈a0,false〉::l2)))) ∨
563 (〈x,true〉 ≠ c ∧ RelseM t1 t2).
565 lemma sem_comp_step_subcase :
566 ∀alpha,c,elseM,RelseM.
567 Realize ? elseM RelseM →
568 Realize ? (comp_step_subcase alpha c elseM)
569 (R_comp_step_subcase alpha c RelseM).
570 #alpha #c #elseM #RelseM #Helse #intape
571 cases (sem_if ? (test_char ? (λx.x == c)) … tc_true
572 (sem_test_char ? (λx.x == c))
573 (sem_seq ????? (sem_move_r …)
574 (sem_seq ????? (sem_adv_to_mark_r ? (is_marked alpha))
575 (sem_match_and_adv ? (λx.x == c)))) Helse intape)
576 #k * #outc * #Hloop #HR @(ex_intro ?? k) @(ex_intro ?? outc)
577 % [ @Hloop ] -Hloop cases HR -HR
578 [ * #ta * whd in ⊢ (%→?); #Hta * #tb * whd in ⊢ (%→?); #Htb
579 * #tc * whd in ⊢ (%→?); #Htc whd in ⊢ (%→?); #Houtc
580 #l0 #x #rs #Hintape cases (true_or_false (〈x,true〉==c)) #Hc
582 #a #l1 #x0 #a0 #l2 #Hl1 #Hrs >Hrs in Hintape; #Hintape
583 >Hintape in Hta; #Hta cases (Hta ? (refl ??)) -Hta
584 #Hx #Hta lapply (Htb … Hta) -Htb #Htb
585 cases (Htc … Htb) [ * #Hfalse normalize in Hfalse; destruct (Hfalse) ]
586 -Htc * #_ #Htc lapply (Htc l1 〈x0,true〉 (〈a0,false〉::l2) (refl ??) (refl ??) Hl1)
587 -Htc #Htc cases (Houtc ???????? Htc) -Houtc
589 % [ <(\P Hx0) in Hx; #Hx lapply (\P Hx) #Hx' destruct (Hx') %
590 | >Houtc >reverse_reverse % ]
592 % [ <(\P Hx) in Hx0; #Hx0 lapply (\Pf Hx0) @not_to_not #Hx' >Hx' %
594 | #x #membx @Hl1 <(reverse_reverse …l1) @memb_reverse @membx ]
596 >Hintape in Hta; #Hta cases (Hta ? (refl ??)) -Hta #Hx #Hta
597 >Hx in Hc;#Hc destruct (Hc) ]
598 | * #ta * whd in ⊢ (%→?); #Hta #Helse #ls #c0 #rs #Hintape %2
599 >Hintape in Hta; #Hta cases (Hta ? (refl ??)) -Hta #Hc #Hta %
600 [ @(\Pf Hc) | <Hta @Helse ]
607 + se è un bit, ho fallito il confronto della tupla corrente
608 + se è un separatore, la tupla fa match
611 ifTM ? (test_char ? is_marked)
612 (single_finalTM … (comp_step_subcase unialpha 〈bit false,true〉
613 (comp_step_subcase unialpha 〈bit true,true〉
618 definition comp_step ≝
619 ifTM ? (test_char ? (is_marked ?))
620 (single_finalTM … (comp_step_subcase FSUnialpha 〈bit false,true〉
621 (comp_step_subcase FSUnialpha 〈bit true,true〉
622 (comp_step_subcase FSUnialpha 〈null,true〉
627 definition R_comp_step_true ≝
629 ∀l0,c,rs.t1 = midtape (FinProd … FSUnialpha FinBool) l0 c rs →
631 ((bit_or_null c' = true ∧
633 rs = 〈a,false〉::l1@〈c0,true〉::〈a0,false〉::l2 →
634 (∀c.memb ? c l1 = true → is_marked ? c = false) →
636 t2 = midtape ? (〈c',false〉::l0) 〈a,true〉 (l1@〈c0,false〉::〈a0,true〉::l2)) ∨
638 t2 = midtape (FinProd … FSUnialpha FinBool)
639 (reverse ? l1@〈a,false〉::〈c',true〉::l0) 〈c0,false〉 (〈a0,false〉::l2))) ∨
640 (bit_or_null c' = false ∧ t2 = midtape ? l0 〈c',false〉 rs)).
642 definition R_comp_step_false ≝
644 ∀ls,c,rs.t1 = midtape (FinProd … FSUnialpha FinBool) ls c rs →
645 is_marked ? c = false ∧ t2 = t1.
647 lemma sem_comp_step :
648 accRealize ? comp_step (inr … (inl … (inr … start_nop)))
649 R_comp_step_true R_comp_step_false.
651 cases (acc_sem_if … (sem_test_char ? (is_marked ?))
652 (sem_comp_step_subcase FSUnialpha 〈bit false,true〉 ??
653 (sem_comp_step_subcase FSUnialpha 〈bit true,true〉 ??
654 (sem_comp_step_subcase FSUnialpha 〈null,true〉 ??
655 (sem_clear_mark …))))
657 #k * #outc * * #Hloop #H1 #H2
658 @(ex_intro ?? k) @(ex_intro ?? outc) %
659 [ % [@Hloop ] ] -Hloop
660 [ #Hstate lapply (H1 Hstate) -H1 -Hstate -H2 *
661 #ta * whd in ⊢ (%→?); #Hleft #Hright #ls #c #rs #Hintape
662 >Hintape in Hleft; #Hleft cases (Hleft ? (refl ??)) -Hleft
663 cases c in Hintape; #c' #b #Hintape whd in ⊢ (??%?→?);
664 #Hb >Hb #Hta @(ex_intro ?? c') % //
666 [ * #Hc' #H1 % % [destruct (Hc') % ]
667 #a #l1 #c0 #a0 #l2 #Hrs >Hrs in Hintape; #Hintape #Hl1
669 [ * #Htmp >Htmp -Htmp #Houtc % % // @Houtc
670 | * #Hneq #Houtc %2 %
674 | * #Hc #Helse1 cases (Helse1 … Hta)
675 [ * #Hc' #H1 % % [destruct (Hc') % ]
676 #a #l1 #c0 #a0 #l2 #Hrs >Hrs in Hintape; #Hintape #Hl1
678 [ * #Htmp >Htmp -Htmp #Houtc % % // @Houtc
679 | * #Hneq #Houtc %2 %
683 | * #Hc' #Helse2 cases (Helse2 … Hta)
684 [ * #Hc'' #H1 % % [destruct (Hc'') % ]
685 #a #l1 #c0 #a0 #l2 #Hrs >Hrs in Hintape; #Hintape #Hl1
687 [ * #Htmp >Htmp -Htmp #Houtc % % // @Houtc
688 | * #Hneq #Houtc %2 %
692 | * #Hc'' whd in ⊢ (%→?); #Helse3 %2 %
693 [ generalize in match Hc''; generalize in match Hc'; generalize in match Hc;
695 [ * [ #_ #Hfalse @False_ind @(absurd ?? Hfalse) %
696 | #Hfalse @False_ind @(absurd ?? Hfalse) % ]
697 | #_ #_ #Hfalse @False_ind @(absurd ?? Hfalse) %
704 | #Hstate lapply (H2 Hstate) -H1 -Hstate -H2 *
705 #ta * whd in ⊢ (%→%→?); #Hleft #Hright #ls #c #rs #Hintape
706 >Hintape in Hleft; #Hleft
707 cases (Hleft ? (refl ??)) -Hleft
708 #Hc #Hta % // >Hright //
713 whileTM ? comp_step (inr … (inl … (inr … start_nop))).
716 definition R_compare :=
720 ∀ls,c,b,rs.t1 = midtape ? ls 〈c,b〉 rs →
721 (b = true → rs = ....) →
725 rs = cs@l1@〈c0,true〉::cs0@l2
729 ls 〈c,b〉 cs l1 〈c0,b0〉 cs0 l2
733 ls (hd (Ls@〈grid,false〉))* (tail (Ls@〈grid,false〉)) l1 (hd (Ls@〈comma,false〉))* (tail (Ls@〈comma,false〉)) l2
734 ^^^^^^^^^^^^^^^^^^^^^^^
736 ls Ls 〈grid,false〉 l1 Ls 〈comma,true〉 l2
741 ls (hd (Ls@〈c,false〉))* (tail (Ls@〈c,false〉)) l1 (hd (Ls@〈d,false〉))* (tail (Ls@〈d,false〉)) l2
742 ^^^^^^^^^^^^^^^^^^^^^^^
745 ls Ls 〈c,true〉 l1 Ls 〈d,false〉 l2
751 (∃la,d.〈b,true〉::bs = la@[〈grid,d〉] ∧ ∀x.memb ? x la → is_bit (\fst x) = true) →
752 (∃lb,d0.〈b0,true〉::b0s = lb@[〈comma,d0〉] ∧ ∀x.memb ? x lb → is_bit (\fst x) = true) →
753 t1 = midtape ? l0 〈b,true〉 (bs@l1@〈b0,true〉::b0s@l2 →
755 mk_tape left (option current) right
757 (b = grid ∧ b0 = comma ∧ bs = [] ∧ b0s = [] ∧
758 t2 = midtape ? l0 〈grid,false〉 (l1@〈comma,true〉::l2)) ∨
759 (b = bit x ∧ b = c ∧ bs = b0s
761 definition R_compare :=
763 ∀ls,c,rs.t1 = midtape (FinProd … FSUnialpha FinBool) ls c rs →
764 (∀c'.bit_or_null c' = false → c = 〈c',true〉 → t2 = midtape ? ls 〈c',false〉 rs) ∧
765 (∀c'. c = 〈c',false〉 → t2 = t1) ∧
768 (∀c.memb (FinProd … FSUnialpha FinBool) c bs = true → bit_or_null (\fst c) = true) →
769 (∀c.memb (FinProd … FSUnialpha FinBool) c b0s = true → bit_or_null (\fst c) = true) →
770 (∀c.memb ? c bs = true → is_marked ? c = false) →
771 (∀c.memb ? c b0s = true → is_marked ? c = false) →
772 (∀c.memb ? c l1 = true → is_marked ? c = false) →
773 c = 〈b,true〉 → bit_or_null b = true →
774 rs = bs@〈grid,false〉::l1@〈b0,true〉::b0s@〈comma,false〉::l2 →
775 (〈b,true〉::bs = 〈b0,true〉::b0s ∧
776 t2 = midtape ? (reverse ? bs@〈b,false〉::ls)
777 〈grid,false〉 (l1@〈b0,false〉::b0s@〈comma,true〉::l2)) ∨
778 (∃la,c',d',lb,lc.c' ≠ d' ∧
779 〈b,false〉::bs = la@〈c',false〉::lb ∧
780 〈b0,false〉::b0s = la@〈d',false〉::lc ∧
781 t2 = midtape (FinProd … FSUnialpha FinBool) (reverse ? la@
787 〈d',false〉 (lc@〈comma,false〉::l2)).
789 lemma wsem_compare : WRealize ? compare R_compare.
791 lapply (sem_while ?????? sem_comp_step t i outc Hloop) [%]
792 -Hloop * #t1 * #Hstar @(star_ind_l ??????? Hstar)
793 [ #tapea whd in ⊢ (%→?); #Rfalse #ls #c #rs #Htapea %
795 [ #c' #Hc' #Hc lapply (Rfalse … Htapea) -Rfalse * >Hc
796 whd in ⊢ (??%?→?); #Hfalse destruct (Hfalse)
797 | #c' #Hc lapply (Rfalse … Htapea) -Rfalse * #_
799 | #b #b0 #bs #b0s #l1 #l2 #Hlen #Hbs1 #Hb0s1 #Hbs2 #Hb0s2 #Hl1 #Hc
800 cases (Rfalse … Htapea) -Rfalse >Hc whd in ⊢ (??%?→?);#Hfalse destruct (Hfalse)
802 | #tapea #tapeb #tapec #Hleft #Hright #IH #Htapec lapply (IH Htapec) -Htapec -IH #IH
803 whd in Hleft; #ls #c #rs #Htapea cases (Hleft … Htapea) -Hleft
804 #c' * #Hc >Hc cases (true_or_false (bit_or_null c')) #Hc'
806 [ * >Hc' #H @False_ind destruct (H)
807 | * #_ #Htapeb cases (IH … Htapeb) * #_ #H #_ %
809 [#c1 #Hc1 #Heqc destruct (Heqc) <Htapeb @(H c1) %
810 |#c1 #Hfalse destruct (Hfalse)
812 |#b #b0 #bs #b0s #l1 #l2 #_ #_ #_ #_ #_ #_
813 #Heq destruct (Heq) >Hc' #Hfalse @False_ind destruct (Hfalse)
818 [ #c'' #Hc'' #Heq destruct (Heq) >Hc'' in Hc'; #H destruct (H)
819 | #c0 #Hfalse destruct (Hfalse)
821 |#b #b0 #bs #b0s #l1 #l2 #Hlen #Hbs1 #Hb0s1 #Hbs2 #Hb0s2 #Hl1
822 #Heq destruct (Heq) #_ #Hrs cases Hleft -Hleft
823 [2: * >Hc' #Hfalse @False_ind destruct ] * #_
824 @(list_cases2 … Hlen)
825 [ #Hbs #Hb0s generalize in match Hrs; >Hbs in ⊢ (%→?); >Hb0s in ⊢ (%→?);
826 -Hrs #Hrs normalize in Hrs; #Hleft cases (Hleft ????? Hrs ?) -Hleft
827 [ * #Heqb #Htapeb cases (IH … Htapeb) -IH * #IH #_ #_
833 @(ex_intro … [ ]) @(ex_intro … b)
834 @(ex_intro … b0) @(ex_intro … [ ])
836 [ % [ % [@sym_not_eq //| >Hbs %] | >Hb0s %]
837 | cases (IH … Htapeb) -IH * #_ #IH #_ >(IH ? (refl ??))
841 | * #b' #bitb' * #b0' #bitb0' #bs' #b0s' #Hbs #Hb0s
842 generalize in match Hrs; >Hbs in ⊢ (%→?); >Hb0s in ⊢ (%→?);
843 cut (bit_or_null b' = true ∧ bit_or_null b0' = true ∧
844 bitb' = false ∧ bitb0' = false)
845 [ % [ % [ % [ >Hbs in Hbs1; #Hbs1 @(Hbs1 〈b',bitb'〉) @memb_hd
846 | >Hb0s in Hb0s1; #Hb0s1 @(Hb0s1 〈b0',bitb0'〉) @memb_hd ]
847 | >Hbs in Hbs2; #Hbs2 @(Hbs2 〈b',bitb'〉) @memb_hd ]
848 | >Hb0s in Hb0s2; #Hb0s2 @(Hb0s2 〈b0',bitb0'〉) @memb_hd ]
849 | * * * #Ha #Hb #Hc #Hd >Hc >Hd
851 cases (Hleft b' (bs'@〈grid,false〉::l1) b0 b0'
852 (b0s'@〈comma,false〉::l2) ??) -Hleft
853 [ 3: >Hrs normalize @eq_f >associative_append %
854 | * #Hb0 #Htapeb cases (IH …Htapeb) -IH * #_ #_ #IH
855 cases (IH b' b0' bs' b0s' (l1@[〈b0,false〉]) l2 ??????? Ha ?) -IH
857 [ >Hb0 @eq_f >Hbs in Heq; >Hb0s in ⊢ (%→?); #Heq
858 destruct (Heq) >Hb0s >Hc >Hd %
859 | >Houtc >Hbs >Hb0s >Hc >Hd >reverse_cons >associative_append
860 >associative_append %
862 | * #la * #c' * #d' * #lb * #lc * * * #H1 #H2 #H3 #H4 %2
863 @(ex_intro … (〈b,false〉::la)) @(ex_intro … c') @(ex_intro … d')
864 @(ex_intro … lb) @(ex_intro … lc)
865 % [ % [ % // >Hbs >Hc >H2 % | >Hb0s >Hd >H3 >Hb0 % ]
866 | >H4 >Hbs >Hb0s >Hc >Hd >Hb0 >reverse_append
867 >reverse_cons >reverse_cons
868 >associative_append >associative_append
869 >associative_append >associative_append %
871 | generalize in match Hlen; >Hbs >Hb0s
872 normalize #Hlen destruct (Hlen) @e0
873 | #c0 #Hc0 @Hbs1 >Hbs @memb_cons //
874 | #c0 #Hc0 @Hb0s1 >Hb0s @memb_cons //
875 | #c0 #Hc0 @Hbs2 >Hbs @memb_cons //
876 | #c0 #Hc0 @Hb0s2 >Hb0s @memb_cons //
877 | #c0 #Hc0 cases (memb_append … Hc0)
878 [ @Hl1 | #Hc0' >(memb_single … Hc0') % ]
880 | >associative_append >associative_append % ]
882 @(ex_intro … []) @(ex_intro … b) @(ex_intro … b0)
883 @(ex_intro … bs) @(ex_intro … b0s) %
884 [ % // % // @sym_not_eq //
885 | >Hbs >Hb0s >Hc >Hd >reverse_cons >associative_append
886 >reverse_append in Htapeb; >reverse_cons
887 >associative_append >associative_append
889 cases (IH … Htapeb) -Htapeb -IH * #_ #IH #_ @(IH ? (refl ??))
891 | #c1 #Hc1 cases (memb_append … Hc1) #Hyp
892 [ @Hbs2 >Hbs @memb_cons @Hyp
893 | cases (orb_true_l … Hyp)
902 axiom sem_compare : Realize ? compare R_compare.