]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/universal/marks.ma
Modifications and refactoring
[helm.git] / matita / matita / lib / turing / universal / marks.ma
1 (*
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.           
5     ||I||                                                            
6     ||T||  
7     ||A||  
8     \   /  This file is distributed under the terms of the       
9      \ /   GNU General Public License Version 2   
10       V_____________________________________________________________*)
11
12
13 (* COMPARE BIT
14
15 *)
16
17 include "turing/if_machine.ma".
18 include "turing/basic_machines.ma".
19 include "turing/universal/alphabet.ma".
20
21 (* ADVANCE TO MARK (right)
22
23    sposta la testina a destra fino a raggiungere il primo carattere marcato 
24    
25 *)
26
27 (* 0, a ≠ mark _ ⇒ 0, R
28 0, a = mark _ ⇒ 1, N *)
29
30 definition atm_states ≝ initN 3.
31
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 …)).
35
36 definition atmr_step ≝ 
37   λalpha:FinSet.λtest:alpha→bool.
38   mk_TM alpha atm_states
39   (λp.let 〈q,a〉 ≝ p in
40    match a with
41    [ None ⇒ 〈atm1, None ?〉
42    | Some a' ⇒ 
43      match test a' with
44      [ true ⇒ 〈atm1,None ?〉
45      | false ⇒ 〈atm2,Some ? 〈a',R〉〉 ]])
46   atm0 (λx.notb (x == atm0)).
47
48 definition Ratmr_step_true ≝ 
49   λalpha,test,t1,t2.
50    ∃ls,a,rs.
51    t1 = midtape alpha ls a rs ∧ test a = false ∧ 
52    t2 = mk_tape alpha (a::ls) (option_hd ? rs) (tail ? rs).
53    
54 definition Ratmr_step_false ≝ 
55   λalpha,test,t1,t2.
56     t1 = t2 ∧
57     (current alpha t1 = None ? ∨
58      (∃a.current ? t1 = Some ? a ∧ test a = true)).
59      
60 lemma atmr_q0_q1 :
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
65     (midtape … ls a0 rs).
66 #alpha #test #ls #a0 #ts #Htest whd in ⊢ (??%?);
67 whd in match (trans … 〈?,?〉); >Htest %
68 qed.
69      
70 lemma atmr_q0_q2 :
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 //
78 qed.
79
80 lemma sem_atmr_step :
81   ∀alpha,test.
82   accRealize alpha (atmr_step alpha test) 
83     atm2 (Ratmr_step_true alpha test) (Ratmr_step_false alpha test).
84 #alpha #test *
85 [ @(ex_intro ?? 2)
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 ?))
95     [| % 
96       [ % 
97         [ whd in ⊢ (??%?); >atmr_q0_q1 //
98         | whd in ⊢ ((??%%)→?); #Hfalse destruct ]
99       | #_ % // %2 @(ex_intro ?? c) % // ]
100     ]
101   | @(ex_intro ?? (mk_config ?? atm2 (mk_tape ? (c::ls) (option_hd ? rs) (tail ? rs))))
102     % 
103     [ %
104       [ whd in ⊢ (??%?); >atmr_q0_q2 //
105       | #_ @(ex_intro ?? ls) @(ex_intro ?? c) @(ex_intro ?? rs)
106         % // % //
107       ]
108     | #Hfalse @False_ind @(absurd ?? Hfalse) %
109     ]
110   ]
111 ]
112 qed.
113
114 definition R_adv_to_mark_r ≝ λalpha,test,t1,t2.
115   ∀ls,c,rs.
116   (t1 = midtape alpha ls c rs  → 
117   ((test c = true ∧ t2 = t1) ∨
118    (test c = false ∧
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))).
122      
123 definition adv_to_mark_r ≝ 
124   λalpha,test.whileTM alpha (atmr_step alpha test) atm2.
125
126 lemma wsem_adv_to_mark_r :
127   ∀alpha,test.
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)
132 [ #tapea * #Htapea *
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) % //
137     <Htapea //
138   ]
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
143   
144   >Htapea' in Htapea; #Htapea destruct (Htapea) % // *
145   [ #b #rs2 #Hrs >Hrs in Htapeb; #Htapeb #Htestb #_
146     cases (IH … Htapeb)
147     [ * #_ #Houtc >Houtc >Htapeb %
148     | * #Hfalse >Hfalse in Htestb; #Htestb destruct (Htestb) ]
149   | #r1 #rs1 #b #rs2 #Hrs >Hrs in Htapeb; #Htapeb #Htestb #Hmemb
150     cases (IH … Htapeb)
151     [ * #Hfalse >(Hmemb …) in Hfalse;
152       [ #Hft destruct (Hft)
153       | @memb_hd ]
154     | * #Htestr1 #H1 >reverse_cons >associative_append
155       @H1 // #x #Hx @Hmemb @memb_cons //
156     ]
157   ]
158 qed.
159
160 lemma terminate_adv_to_mark_r :
161   ∀alpha,test.
162   ∀t.Terminate alpha (adv_to_mark_r alpha test) t.
163 #alpha #test #t
164 @(terminate_while … (sem_atmr_step alpha test))
165   [ %
166   | cases t
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
170       elim rs
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;
177         >Ht1 @IH
178       ]
179     ]
180   ]
181 qed.
182
183 lemma sem_adv_to_mark_r :
184   ∀alpha,test.
185   Realize alpha (adv_to_mark_r alpha test) (R_adv_to_mark_r alpha test).
186 /2/
187 qed.
188
189 (* MARK machine
190
191    marks the current character 
192  *)
193  
194 definition mark_states ≝ initN 2.
195
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 …)).
198
199 definition mark ≝ 
200   λalpha:FinSet.mk_TM (FinProd … alpha FinBool) mark_states
201   (λp.let 〈q,a〉 ≝ p in
202     match a with
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 ?〉 ] ])
207   ms0 (λq.q == ms1).
208   
209 definition R_mark ≝ λalpha,t1,t2.
210   ∀ls,c,b,rs.
211   t1 = midtape (FinProd … alpha FinBool) ls 〈c,b〉 rs → 
212   t2 = midtape ? ls 〈c,true〉 rs.
213     
214 lemma sem_mark :
215   ∀alpha.Realize ? (mark alpha) (R_mark alpha).
216 #alpha #intape @(ex_intro ?? 2) cases intape
217 [ @ex_intro
218   [| % [ % | #ls #c #b #rs #Hfalse destruct ] ]
219 |#a #al @ex_intro
220   [| % [ % | #ls #c #b #rs #Hfalse destruct ] ]
221 |#a #al @ex_intro
222   [| % [ % | #ls #c #b #rs #Hfalse destruct ] ]
223 | #ls * #c #b #rs
224   @ex_intro [| % [ % | #ls0 #c0 #b0 #rs0 #H1 destruct (H1) % ] ] ]
225 qed.
226
227
228 (* MOVE RIGHT AND MARK machine
229
230    marks the first character on the right
231    
232    (could be rewritten using (mark; move_right))
233  *)
234  
235 definition mrm_states ≝ initN 3.
236
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 …)).
240
241 definition move_right_and_mark ≝ 
242   λalpha:FinSet.mk_TM (FinProd … alpha FinBool) mrm_states
243   (λp.let 〈q,a〉 ≝ p in
244     match a with
245     [ None ⇒ 〈mrm2,None ?〉
246     | Some a' ⇒ match pi1 … q with
247       [ O ⇒ 〈mrm1,Some ? 〈a',R〉〉
248       | S q ⇒ match q with
249         [ O ⇒ let 〈a'',b〉 ≝ a' in
250               〈mrm2,Some ? 〈〈a'',true〉,N〉〉
251         | S _ ⇒ 〈mrm2,None ?〉 ] ] ])
252   mrm0 (λq.q == mrm2).
253   
254 definition R_move_right_and_mark ≝ λalpha,t1,t2.
255   ∀ls,c,d,b,rs.
256   t1 = midtape (FinProd … alpha FinBool) ls c (〈d,b〉::rs) → 
257   t2 = midtape ? (c::ls) 〈d,true〉 rs.
258     
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
262 [ @ex_intro
263   [| % [ % | #ls #c #d #b #rs #Hfalse destruct ] ]
264 |#a #al @ex_intro
265   [| % [ % | #ls #c #d #b #rs #Hfalse destruct ] ]
266 |#a #al @ex_intro
267   [| % [ % | #ls #c #d #b #rs #Hfalse destruct ] ]
268 | #ls #c *
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) % ] ] ] ]
272 qed.
273
274 (* CLEAR MARK machine
275
276    clears the mark in the current character 
277  *)
278  
279 definition clear_mark_states ≝ initN 3.
280
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 …)).
284
285 definition clear_mark ≝ 
286   λalpha:FinSet.mk_TM (FinProd … alpha FinBool) clear_mark_states
287   (λp.let 〈q,a〉 ≝ p in
288     match a with
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).
294   
295 definition R_clear_mark ≝ λalpha,t1,t2.
296   ∀ls,c,b,rs.
297   t1 = midtape (FinProd … alpha FinBool) ls 〈c,b〉 rs → 
298   t2 = midtape ? ls 〈c,false〉 rs.
299     
300 lemma sem_clear_mark :
301   ∀alpha.Realize ? (clear_mark alpha) (R_clear_mark alpha).
302 #alpha #intape @(ex_intro ?? 2) cases intape
303 [ @ex_intro
304   [| % [ % | #ls #c #b #rs #Hfalse destruct ] ]
305 |#a #al @ex_intro
306   [| % [ % | #ls #c #b #rs #Hfalse destruct ] ]
307 |#a #al @ex_intro
308   [| % [ % | #ls #c #b #rs #Hfalse destruct ] ]
309 | #ls * #c #b #rs
310   @ex_intro [| % [ % | #ls0 #c0 #b0 #rs0 #H1 destruct (H1) % ] ] ]
311 qed.
312
313 (* ADVANCE MARK RIGHT machine
314
315    clears mark on current char,
316    moves right, and marks new current char
317    
318 *)
319
320 definition adv_mark_r ≝ 
321   λalpha:FinSet.
322     seq ? (clear_mark alpha)
323       (seq ? (move_r ?) (mark alpha)).
324       
325 definition R_adv_mark_r ≝ λalpha,t1,t2.
326   ∀ls,c,d,b,rs.
327   t1 = midtape (FinProd … alpha FinBool) ls 〈c,true〉 (〈d,b〉::rs) → 
328   t2 = midtape ? (〈c,false〉::ls) 〈d,true〉 rs.
329   
330 lemma sem_adv_mark_r : 
331   ∀alpha.Realize ? (adv_mark_r alpha) (R_adv_mark_r alpha).
332 #alpha #intape
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) %
338 [ @Hloop
339 | -Hloop #ls #c #d #b #rs #Hintape @(Hs3 … b)
340   @(Hs2 ls 〈c,false〉 (〈d,b〉::rs))
341   @(Hs1 … Hintape)
342 ]
343 qed.
344
345 (* ADVANCE TO MARK (left)
346
347 axiomatized
348
349 *)
350
351 definition R_adv_to_mark_l ≝ λalpha,test,t1,t2.
352   ∀ls,c,rs.
353   (t1 = midtape alpha ls c rs  → 
354   ((test c = true ∧ t2 = t1) ∨
355    (test c = false ∧
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)))).
359
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. *)
363
364 axiom wsem_adv_to_mark_l :
365   ∀alpha,test.
366   WRealize alpha (adv_to_mark_l alpha test) (R_adv_to_mark_l alpha test).
367 (*
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)
371 [ #tapea * #Htapea *
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) % //
376     <Htapea //
377   ]
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
382   
383   >Htapea' in Htapea; #Htapea destruct (Htapea) % // *
384   [ #b #rs2 #Hrs >Hrs in Htapeb; #Htapeb #Htestb #_
385     cases (IH … Htapeb)
386     [ * #_ #Houtc >Houtc >Htapeb %
387     | * #Hfalse >Hfalse in Htestb; #Htestb destruct (Htestb) ]
388   | #r1 #rs1 #b #rs2 #Hrs >Hrs in Htapeb; #Htapeb #Htestb #Hmemb
389     cases (IH … Htapeb)
390     [ * #Hfalse >(Hmemb …) in Hfalse;
391       [ #Hft destruct (Hft)
392       | @memb_hd ]
393     | * #Htestr1 #H1 >reverse_cons >associative_append
394       @H1 // #x #Hx @Hmemb @memb_cons //
395     ]
396   ]
397 qed.
398 *)
399
400 axiom terminate_adv_to_mark_l :
401   ∀alpha,test.
402   ∀t.Terminate alpha (adv_to_mark_l alpha test) t.
403 (*
404 #alpha #test #t
405 @(terminate_while … (sem_atmr_step alpha test))
406   [ %
407   | cases t
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
411       elim rs
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;
418         >Ht1 @IH
419       ]
420     ]
421   ]
422 qed.
423 *)
424
425 lemma sem_adv_to_mark_l :
426   ∀alpha,test.
427   Realize alpha (adv_to_mark_l alpha test) (R_adv_to_mark_l alpha test).
428 /2/
429 qed.
430
431 (*
432    ADVANCE BOTH MARKS machine
433    
434    l1 does not contain marks ⇒
435    
436
437    input:
438    l0 x* a l1 x0* a0 l2
439               ^
440    
441    output:
442    l0 x a* l1 x0 a0* l2
443         ^
444 *)
445
446 definition adv_both_marks ≝ 
447   λalpha.seq ? (adv_mark_r alpha)
448     (seq ? (move_l ?)
449      (seq ? (adv_to_mark_l (FinProd alpha FinBool) (is_marked alpha))
450        (adv_mark_r alpha))).
451
452 definition R_adv_both_marks ≝ 
453   λalpha,t1,t2.
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).
458
459 lemma sem_adv_both_marks :
460   ∀alpha.Realize ? (adv_both_marks alpha) (R_adv_both_marks alpha).    
461 #alpha #intape
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) %
470 [ @Hloop
471 | -Hloop #l0 #x #a #l1 #x0 #a0 #l2 #Hl1 #Hintape
472   @(Hs4 … false) -Hs4
473   lapply (Hs1 … Hintape) #Hta
474   lapply (Hs2 … Hta) #Htb
475   cases (Hs3 … Htb)
476   [ * #Hfalse normalize in Hfalse; destruct (Hfalse)
477   | * #_ -Hs3 #Hs3 
478     lapply (Hs3 (l1@[〈a,false〉]) 〈x,true〉 l0 ???)
479     [ #x1 #Hx1 cases (memb_append … Hx1)
480       [ @Hl1
481       | #Hx1' >(memb_single … Hx1') % ]
482     | % 
483     | >associative_append %
484     | >reverse_append #Htc @Htc ]
485   ]
486 qed.
487
488 (* 
489    MATCH AND ADVANCE(f)
490    
491    input:
492    l0 x* a l1 x0* a0 l2
493               ^
494     
495    output:
496    l0 x a* l1 x0 a0* l2   (f(x0) == true)
497         ^
498    l0 x* a l1 x0* a0 l2   (f(x0) == false)
499               ^
500 *)
501
502 definition match_and_adv ≝ 
503   λalpha,f.ifTM ? (test_char ? f)
504      (adv_both_marks alpha) (clear_mark ?) tc_true.
505
506 definition R_match_and_adv ≝ 
507   λalpha,f,t1,t2.
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)).
514
515 lemma sem_match_and_adv : 
516   ∀alpha,f.Realize ? (match_and_adv alpha f) (R_match_and_adv alpha f).
517 #alpha #f #intape
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)
520 % [ @Hloop ] -Hloop
521 cases Hif
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) % ]
530 ]
531 qed.
532
533 (*
534  if x = c
535       then move_right; ----
536            adv_to_mark_r;
537            if current (* x0 *) = 0
538               then advance_mark ----
539                    adv_to_mark_l;
540                    advance_mark
541               else STOP
542       else M
543 *)
544
545 definition comp_step_subcase ≝ 
546   λalpha,c,elseM.ifTM ? (test_char ? (λx.x == c))
547     (seq ? (move_r …)
548       (seq ? (adv_to_mark_r ? (is_marked alpha)) 
549       (match_and_adv ? (λx.x == c))))
550     elseM tc_true.
551
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 → 
555     (〈x,true〉 = c ∧
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 → 
558      ((x = x0 ∧
559       t2 = midtape ? (〈x,false〉::l0) 〈a,true〉 (l1@〈x0,false〉::〈a0,true〉::l2)) ∨
560       (x ≠ x0 ∧
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).
564
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
581   [ % % [ @(\P 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
588     [ * #Hx0 #Houtc % 
589       % [ <(\P Hx0) in Hx; #Hx lapply (\P Hx) #Hx' destruct (Hx') %
590         | >Houtc >reverse_reverse % ]
591     | * #Hx0 #Houtc %2
592       % [ <(\P Hx) in Hx0; #Hx0 lapply (\Pf Hx0) @not_to_not #Hx' >Hx' %
593         | >Houtc % ]
594     | #x #membx @Hl1 <(reverse_reverse …l1) @memb_reverse @membx ]
595   | %2 % [ @(\Pf Hc) ]
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 ]
601 ]
602 qed.
603
604 (* 
605 - se marcato, itero
606 - se non è marcato
607   + se è un bit, ho fallito il confronto della tupla corrente
608   + se è un separatore, la tupla fa match
609
610
611 ifTM ? (test_char ? is_marked)
612   (single_finalTM … (comp_step_subcase unialpha 〈bit false,true〉
613     (comp_step_subcase unialpha 〈bit true,true〉
614       (clear_mark …))))
615   (nop ?)
616 *)
617
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〉
623         (clear_mark …)))))
624   (nop ?)
625   tc_true.
626   
627 definition R_comp_step_true ≝ 
628   λt1,t2.
629     ∀l0,c,rs.t1 = midtape (FinProd … FSUnialpha FinBool) l0 c rs → 
630     ∃c'. c = 〈c',true〉 ∧
631     ((bit_or_null c' = true ∧
632      ∀a,l1,c0,a0,l2.
633       rs = 〈a,false〉::l1@〈c0,true〉::〈a0,false〉::l2 → 
634       (∀c.memb ? c l1 = true → is_marked ? c = false) → 
635       (c0 = c' ∧
636        t2 = midtape ? (〈c',false〉::l0) 〈a,true〉 (l1@〈c0,false〉::〈a0,true〉::l2)) ∨
637       (c0 ≠ c' ∧
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)).
641
642 definition R_comp_step_false ≝ 
643   λt1,t2.
644    ∀ls,c,rs.t1 = midtape (FinProd … FSUnialpha FinBool) ls c rs → 
645    is_marked ? c = false ∧ t2 = t1.
646    
647 lemma sem_comp_step : 
648   accRealize ? comp_step (inr … (inl … (inr … start_nop))) 
649     R_comp_step_true R_comp_step_false.
650 #intape
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 …))))
656         (sem_nop …) intape)
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') % //
665   cases (Hright … Hta)
666   [ * #Hc' #H1 % % [destruct (Hc') % ]
667     #a #l1 #c0 #a0 #l2 #Hrs >Hrs in Hintape; #Hintape #Hl1
668     cases (H1 … Hl1 Hrs)
669     [ * #Htmp >Htmp -Htmp #Houtc % % // @Houtc
670     | * #Hneq #Houtc %2 %
671       [ @sym_not_eq //
672       | @Houtc ]
673     ]
674   | * #Hc #Helse1 cases (Helse1 … Hta)
675     [ * #Hc' #H1 % % [destruct (Hc') % ]
676       #a #l1 #c0 #a0 #l2 #Hrs >Hrs in Hintape; #Hintape #Hl1
677       cases (H1 … Hl1 Hrs)
678       [ * #Htmp >Htmp -Htmp #Houtc % % // @Houtc
679       | * #Hneq #Houtc %2 %
680         [ @sym_not_eq //
681         | @Houtc ]
682       ]
683     | * #Hc' #Helse2 cases (Helse2 … Hta)
684       [ * #Hc'' #H1 % % [destruct (Hc'') % ]
685         #a #l1 #c0 #a0 #l2 #Hrs >Hrs in Hintape; #Hintape #Hl1
686         cases (H1 … Hl1 Hrs)
687         [ * #Htmp >Htmp -Htmp #Houtc % % // @Houtc
688         | * #Hneq #Houtc %2 %
689           [ @sym_not_eq //
690           | @Houtc ]
691         ]
692       | * #Hc'' whd in ⊢ (%→?); #Helse3 %2 %
693         [ generalize in match Hc''; generalize in match Hc'; generalize in match Hc;
694           cases c'
695           [ * [ #_ #Hfalse @False_ind @(absurd ?? Hfalse) %
696               | #Hfalse @False_ind @(absurd ?? Hfalse) % ]
697           | #_ #_ #Hfalse @False_ind @(absurd ?? Hfalse) %
698           |*: #_ #_ #_ % ]
699         | @(Helse3 … Hta)
700         ]
701       ]
702     ]
703   ]
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 //
709 ]
710 qed.
711
712 definition compare ≝ 
713   whileTM ? comp_step (inr … (inl … (inr … start_nop))).
714
715 (*
716 definition R_compare :=
717   λt1,t2.
718   (t
719   
720   ∀ls,c,b,rs.t1 = midtape ? ls 〈c,b〉 rs →
721   (b = true → rs = ....) → 
722   (b = false ∧ ....) ∨
723   (b = true ∧ 
724    
725    rs = cs@l1@〈c0,true〉::cs0@l2
726    (
727  
728   
729   ls 〈c,b〉 cs l1 〈c0,b0〉 cs0 l2
730   
731
732 ACCETTAZIONE:  
733   ls (hd (Ls@〈grid,false〉))* (tail (Ls@〈grid,false〉)) l1 (hd (Ls@〈comma,false〉))* (tail (Ls@〈comma,false〉)) l2
734      ^^^^^^^^^^^^^^^^^^^^^^^
735   
736   ls Ls 〈grid,false〉 l1 Ls 〈comma,true〉 l2
737         ^^^^^^^^^^^^
738
739 RIFIUTO: c ≠ d
740   
741   ls (hd (Ls@〈c,false〉))* (tail (Ls@〈c,false〉)) l1 (hd (Ls@〈d,false〉))* (tail (Ls@〈d,false〉)) l2
742      ^^^^^^^^^^^^^^^^^^^^^^^
743   
744   
745   ls Ls 〈c,true〉 l1 Ls 〈d,false〉 l2
746                        ^^^^^^^^
747   
748   ).
749   
750   |bs| = |b0s| → 
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 → 
754   
755   mk_tape left (option current) right
756   
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
760   *)
761 definition R_compare :=
762   λt1,t2.
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) ∧
766   ∀b,b0,bs,b0s,l1,l2.
767   |bs| = |b0s| → 
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@
782                     reverse ? l1@
783                     〈grid,false〉::
784                     reverse ? lb@
785                     〈c',true〉::
786                     reverse ? la@ls)
787                     〈d',false〉 (lc@〈comma,false〉::l2)).
788                     
789 lemma wsem_compare : WRealize ? compare R_compare.
790 #t #i #outc #Hloop
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 %
794   [ %
795     [ #c' #Hc' #Hc lapply (Rfalse … Htapea) -Rfalse * >Hc
796       whd in ⊢ (??%?→?); #Hfalse destruct (Hfalse)
797     | #c' #Hc lapply (Rfalse … Htapea) -Rfalse * #_
798       #Htrue @Htrue ]
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)
801   ]
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'
805   [2: * 
806     [ * >Hc' #H @False_ind destruct (H)
807     | * #_ #Htapeb cases (IH … Htapeb) * #_ #H #_ %
808       [% 
809         [#c1 #Hc1 #Heqc destruct (Heqc) <Htapeb @(H c1) %
810         |#c1 #Hfalse destruct (Hfalse)
811         ]
812       |#b #b0 #bs #b0s #l1 #l2 #_ #_ #_ #_ #_ #_
813        #Heq destruct (Heq) >Hc' #Hfalse @False_ind destruct (Hfalse)
814       ]
815     ]
816  |#Hleft %
817     [ %
818       [ #c'' #Hc'' #Heq destruct (Heq) >Hc'' in Hc'; #H destruct (H) 
819       | #c0 #Hfalse destruct (Hfalse)
820       ]
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 #_ #_
828           % %
829             [ >Heqb >Hbs >Hb0s %
830             | >Hbs >Hb0s @IH %
831             ] 
832          |* #Hneqb #Htapeb %2
833           @(ex_intro … [ ]) @(ex_intro … b)
834           @(ex_intro … b0) @(ex_intro … [ ]) 
835           @(ex_intro … [ ]) %
836             [ % [ % [@sym_not_eq //| >Hbs %] | >Hb0s %]
837             | cases (IH … Htapeb) -IH * #_ #IH #_ >(IH ? (refl ??))
838               @Htapeb
839             ]
840          | @Hl1 ]
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
850           #Hrs #Hleft 
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
856             [ * #Heq #Houtc % %
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 %
861               ]
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 %
870                 ]
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') % ]
879             | %
880             | >associative_append >associative_append % ]
881          | * #Hneq #Htapeb %2
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
888              #Htapeb <Htapeb
889              cases (IH … Htapeb) -Htapeb -IH * #_ #IH #_ @(IH ? (refl ??))
890            ]
891          | #c1 #Hc1 cases (memb_append … Hc1) #Hyp
892            [ @Hbs2 >Hbs @memb_cons @Hyp
893            | cases (orb_true_l … Hyp)
894              [ #Hyp2 >(\P Hyp2) %
895              | @Hl1
896              ]
897            ]
898          ]
899 ]]]]]
900 qed.       
901
902 axiom sem_compare : Realize ? compare R_compare.