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/universal/marks.ma".
19 definition STape ≝ FinProd … FSUnialpha FinBool.
21 definition only_bits ≝ λl.
22 ∀c.memb STape c l = true → is_bit (\fst c) = true.
24 definition only_bits_or_nulls ≝ λl.
25 ∀c.memb STape c l = true → bit_or_null (\fst c) = true.
27 definition no_grids ≝ λl.
28 ∀c.memb STape c l = true → is_grid (\fst c) = false.
30 definition no_bars ≝ λl.
31 ∀c.memb STape c l = true → is_bar (\fst c) = false.
33 definition no_marks ≝ λl.
34 ∀c.memb STape c l = true → is_marked ? c = false.
36 lemma bit_not_grid: ∀d. is_bit d = true → is_grid d = false.
37 * // normalize #H destruct
40 lemma bit_or_null_not_grid: ∀d. bit_or_null d = true → is_grid d = false.
41 * // normalize #H destruct
44 lemma bit_not_bar: ∀d. is_bit d = true → is_bar d = false.
45 * // normalize #H destruct
48 lemma bit_or_null_not_bar: ∀d. bit_or_null d = true → is_bar d = false.
49 * // normalize #H destruct
52 definition mk_tuple ≝ λqin,cin,qout,cout,mv.
53 qin @ cin :: 〈comma,false〉:: qout @ cout :: 〈comma,false〉 :: [mv].
55 (* by definition, a tuple is not marked *)
56 definition tuple_TM : nat → list STape → Prop ≝
57 λn,t.∃qin,cin,qout,cout,mv.
58 no_marks qin ∧ no_marks qout ∧
59 only_bits qin ∧ only_bits qout ∧
60 bit_or_null cin = true ∧ bit_or_null cout = true ∧ bit_or_null mv = true ∧
61 (cout = null → mv = null) ∧
62 |qin| = n ∧ |qout| = n ∧
63 t = mk_tuple qin 〈cin,false〉 qout 〈cout,false〉 〈mv,false〉.
65 inductive table_TM (n:nat) : list STape → Prop ≝
66 | ttm_nil : table_TM n []
67 | ttm_cons : ∀t1,T.tuple_TM n t1 → table_TM n T → table_TM n (t1@〈bar,false〉::T).
69 inductive match_in_table (qin:list STape) (cin: STape)
70 (qout:list STape) (cout:STape) (mv:STape)
74 match_in_table qin cin qout cout mv
75 (mk_tuple qin cin qout cout mv @〈bar,false〉::tb)
77 ∀qin0,cin0,qout0,cout0,mv0,tb.
78 match_in_table qin cin qout cout mv tb →
79 match_in_table qin cin qout cout mv
80 (mk_tuple qin0 cin0 qout0 cout0 mv0@〈bar,false〉::tb).
82 axiom append_l1_injective :
83 ∀A.∀l1,l2,l3,l4:list A. |l1| = |l2| → l1@l3 = l2@l4 → l1 = l2.
84 axiom append_l2_injective :
85 ∀A.∀l1,l2,l3,l4:list A. |l1| = |l2| → l1@l3 = l2@l4 → l3 = l4.
86 axiom cons_injective_l : ∀A.∀a1,a2:A.∀l1,l2.a1::l1 = a2::l2 → a1 = a2.
87 axiom cons_injective_r : ∀A.∀a1,a2:A.∀l1,l2.a1::l1 = a2::l2 → l1 = l2.
88 axiom tuple_len : ∀n,t.tuple_TM n t → |t| = 2*n+5.
89 axiom append_eq_tech1 :
90 ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → |l1| < |l3| → ∃la:list A.l1@a::la = l3.
91 axiom append_eq_tech2 :
92 ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → memb A a l4 = false → ∃la:list A.l3 = l1@a::la.
93 (*axiom list_decompose_cases :
94 ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → ∃la,lb:list A.l3 = la@a::lb ∨ l4 = la@a::lb.
95 axiom list_decompose_l :
96 ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → memb A a l4 = false →
97 ∃la,lb.l2 = la@lb ∧ l3 = l1@a::la.
98 axiom list_decompose_r :
99 ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → memb A a l3 = false →
100 ∃la,lb.l1 = la@lb ∧ l4 = lb@a::l2.
101 axiom list_decompose_memb :
102 ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → |l1| < |l3| → memb A a l3 = true.*)
104 lemma generic_match_to_match_in_table :
106 ∀qin,cin,qout,cout,mv.|qin| = n → |qout| = n →
107 only_bits qin → only_bits qout →
108 bit_or_null (\fst cin) = true → bit_or_null (\fst cout) = true →
109 bit_or_null (\fst mv) = true →
111 T = (t1@qin@cin::〈comma,false〉::qout@cout::〈comma,false〉::[mv])@t2 →
112 match_in_table qin cin qout cout mv T.
113 #n #T #Htable #qin #cin #qout #cout #mv #Hlenqin #Hlenqout
114 #Hqinbits #Hqoutbits #Hcin #Hcout #Hmv
116 [ #t1 #t2 <associative_append cases (t1@qin) normalize
117 [ #Hfalse destruct (Hfalse) | #c0 #t0 #Hfalse destruct (Hfalse) ]
118 | #tuple #T0 * #qin0 * #cin0 * #qout0 * #cout0 * #mv0
120 #Hqin0marks #Hqout0marks #Hqin0bits #Hqout0bits #Hcin0 #Hcout0 #Hmv0 #Hcout0mv0
121 #Hlenqin0 #Hlenqout0 #Htuple #Htable0 #IH #t1 #t2 #HT
123 [ >Htuple normalize in ⊢ (??%%→?);
124 >associative_append >associative_append #HT
125 cut (qin0 = qin ∧ (〈cin0,false〉 = cin ∧ (qout0 = qout ∧
126 (〈cout0,false〉 = cout ∧ (〈mv0,false〉 = mv ∧ 〈bar,false〉::T0 = t2)))))
127 [ lapply (append_l1_injective … HT) [ >Hlenqin @Hlenqin0 ]
128 #Hqin % [ @Hqin ] -Hqin
129 lapply (append_l2_injective … HT) [ >Hlenqin @Hlenqin0 ] -HT #HT
130 lapply (cons_injective_l ????? HT) #Hcin % [ @Hcin ] -Hcin
131 lapply (cons_injective_r ????? HT) -HT #HT
132 lapply (cons_injective_r ????? HT) -HT
133 >associative_append >associative_append #HT
134 lapply (append_l1_injective … HT) [ >Hlenqout @Hlenqout0 ]
135 #Hqout % [ @Hqout ] -Hqout
136 lapply (append_l2_injective … HT) [ >Hlenqout @Hlenqout0 ] -HT normalize #HT
137 lapply (cons_injective_l ????? HT) #Hcout % [ @Hcout ] -Hcout
138 lapply (cons_injective_r ????? HT) -HT #HT
139 lapply (cons_injective_r ????? HT) -HT #HT
140 lapply (cons_injective_l ????? HT) #Hmv % [ @Hmv ] -Hmv
141 @(cons_injective_r ????? HT) ]
142 -HT * #Hqin * #Hcin * #Hqout * #Hcout * #Hmv #HT0
143 >(?:qin0@(〈cin0,false〉::〈comma,false〉::qout0@[〈cout0,false〉;〈comma,false〉;〈mv0,false〉])@〈bar,false〉::T0
144 = mk_tuple qin cin qout cout mv@〈bar,false〉::T0)
145 [|>Hqin >Hqout >Hcin >Hcout >Hmv normalize >associative_append >associative_append
146 normalize >associative_append % ]
148 | #c0 #cs0 #HT cut (∃cs1.c0::cs0 = tuple@〈bar,false〉::cs1)
149 [ cases (append_eq_tech1 ?????? HT ?)
150 [ -HT #ta #Hta cases (append_eq_tech2 … Hta ?)
151 [ -Hta #tb #Htb %{tb} @Htb
153 | @le_S_S >length_append >(plus_n_O (|tuple|)) >commutative_plus @le_plus
155 | >Htuple normalize >length_append >length_append @le_plus [ >Hlenqin >Hlenqin0 % ]
156 @le_S_S @le_S_S >length_append >length_append @le_plus [ >Hlenqout >Hlenqout0 % ] %] ]
158 * #cs1 #Hcs1 >Hcs1 in HT; >associative_append >associative_append #HT
159 lapply (append_l2_injective … HT) // -HT #HT
160 lapply (cons_injective_r ????? HT) -HT
161 <associative_append #HT >Htuple %2 @(IH ?? HT)
166 lemma no_grids_in_tuple : ∀n,l.tuple_TM n l → no_grids l.
167 #n #l * #qin * #cin * #qout * #cout * #mv * * * * * * * * * *
168 #_ #_ #Hqin #Hqout #Hcin #Hcout #Hmv #_ #_ #_ #Hl >Hl
169 #c #Hc normalize in Hc; cases (memb_append … Hc) -Hc #Hc
170 [ @bit_not_grid @(Hqin … Hc)
171 | cases (orb_true_l … Hc) -Hc #Hc
172 [ change with (c == 〈cin,false〉 = true) in Hc; >(\P Hc) @bit_or_null_not_grid //
173 | cases (orb_true_l … Hc) -Hc #Hc
174 [ change with (c == 〈comma,false〉 = true) in Hc; >(\P Hc) %
175 | cases (memb_append …Hc) -Hc #Hc
176 [ @bit_not_grid @(Hqout … Hc)
177 | cases (orb_true_l … Hc) -Hc #Hc
178 [ change with (c == 〈cout,false〉 = true) in Hc; >(\P Hc) @bit_or_null_not_grid //
179 | cases (orb_true_l … Hc) -Hc #Hc
180 [ change with (c == 〈comma,false〉 = true) in Hc; >(\P Hc) %
181 | >(memb_single … Hc) @bit_or_null_not_grid @Hmv
185 lemma no_marks_in_tuple : ∀n,l.tuple_TM n l → no_marks l.
186 #n #l * #qin * #cin * #qout * #cout * #mv * * * * * * * * * *
187 #Hqin #Hqout #_ #_ #_ #_ #_ #_ #_ #_ #Hl >Hl
188 #c #Hc normalize in Hc; cases (memb_append … Hc) -Hc #Hc
190 | cases (orb_true_l … Hc) -Hc #Hc
191 [ change with (c == 〈cin,false〉 = true) in Hc; >(\P Hc) %
192 | cases (orb_true_l … Hc) -Hc #Hc
193 [ change with (c == 〈comma,false〉 = true) in Hc; >(\P Hc) %
194 | cases (memb_append … Hc) -Hc #Hc
196 | cases (orb_true_l … Hc) -Hc #Hc
197 [ change with (c == 〈cout,false〉 = true) in Hc; >(\P Hc) %
198 | cases (orb_true_l … Hc) -Hc #Hc
199 [ change with (c == 〈comma,false〉 = true) in Hc; >(\P Hc) %
200 | >(memb_single … Hc) %
204 lemma no_grids_in_table: ∀n.∀l.table_TM n l → no_grids l.
206 [normalize #c #H destruct
207 |#t1 #t2 #Ht1 #Ht2 #IH lapply (no_grids_in_tuple … Ht1) -Ht1 #Ht1 #x #Hx
208 cases (memb_append … Hx) -Hx #Hx
210 | cases (orb_true_l … Hx) -Hx #Hx
215 lemma no_marks_in_table: ∀n.∀l.table_TM n l → no_marks l.
217 [normalize #c #H destruct
218 |#t1 #t2 #Ht1 #Ht2 #IH lapply (no_marks_in_tuple … Ht1) -Ht1 #Ht1 #x #Hx
219 cases (memb_append … Hx) -Hx #Hx
221 | cases (orb_true_l … Hx) -Hx #Hx
226 axiom last_of_table: ∀n,l,b.¬ table_TM n (l@[〈bar,b〉]).
229 l0 x* a l1 x0* a0 l2 ------> l0 x a* l1 x0 a0* l2
232 if current (* x *) = #
235 then move_right; ----
237 if current (* x0 *) = 0
238 then advance_mark ----
242 else x = 1 (* analogo *)
248 MARK NEXT TUPLE machine
249 (partially axiomatized)
251 marks the first character after the first bar (rightwards)
254 definition bar_or_grid ≝ λc:STape.is_bar (\fst c) ∨ is_grid (\fst c).
256 definition mark_next_tuple ≝
257 seq ? (adv_to_mark_r ? bar_or_grid)
258 (ifTM ? (test_char ? (λc:STape.is_bar (\fst c)))
259 (move_right_and_mark ?) (nop ?) 1).
261 definition R_mark_next_tuple ≝
264 (* c non può essere un separatore ... speriamo *)
265 t1 = midtape STape ls c (rs1@〈grid,false〉::rs2) →
266 no_marks rs1 → no_grids rs1 → bar_or_grid c = false →
267 (∃rs3,rs4,d,b.rs1 = rs3 @ 〈bar,false〉 :: rs4 ∧
269 Some ? 〈d,b〉 = option_hd ? (rs4@〈grid,false〉::rs2) ∧
270 t2 = midtape STape (〈bar,false〉::reverse ? rs3@c::ls) 〈d,true〉 (tail ? (rs4@〈grid,false〉::rs2)))
272 (no_bars rs1 ∧ t2 = midtape ? (reverse ? rs1@c::ls) 〈grid,false〉 rs2).
276 (∀x.memb A x l = true → f x = false) ∨
277 (∃l1,c,l2.f c = true ∧ l = l1@c::l2 ∧ ∀x.memb ? x l1 = true → f x = false).
279 [ % #x normalize #Hfalse *)
281 theorem sem_mark_next_tuple :
282 Realize ? mark_next_tuple R_mark_next_tuple.
284 lapply (sem_seq ? (adv_to_mark_r ? bar_or_grid)
285 (ifTM ? (test_char ? (λc:STape.is_bar (\fst c))) (move_right_and_mark ?) (nop ?) 1) ????)
286 [@sem_if [5: // |6: @sem_move_right_and_mark |7: // |*:skip]
288 |||#Hif cases (Hif intape) -Hif
289 #j * #outc * #Hloop * #ta * #Hleft #Hright
290 @(ex_intro ?? j) @ex_intro [|% [@Hloop] ]
292 #ls #c #rs1 #rs2 #Hrs #Hrs1 #Hrs1' #Hc
294 [ * #Hfalse >Hfalse in Hc; #Htf destruct (Htf)
295 | * #_ #Hta cases (tech_split STape (λc.is_bar (\fst c)) rs1)
296 [ #H1 lapply (Hta rs1 〈grid,false〉 rs2 (refl ??) ? ?)
297 [ * #x #b #Hx whd in ⊢ (??%?); >(Hrs1' … Hx) >(H1 … Hx) %
299 | -Hta #Hta cases Hright
300 [ * #tb * whd in ⊢ (%→?); #Hcurrent
301 @False_ind cases (Hcurrent 〈grid,false〉 ?)
302 [ normalize #Hfalse destruct (Hfalse)
304 | * #tb * whd in ⊢ (%→?); #Hcurrent
305 cases (Hcurrent 〈grid,false〉 ?)
306 [ #_ #Htb whd in ⊢ (%→?); #Houtc
309 | >Houtc >Htb >Hta % ]
313 | * #rs3 * #c0 * #rs4 * * #Hc0 #Hsplit #Hrs3
314 % @(ex_intro ?? rs3) @(ex_intro ?? rs4)
315 lapply (Hta rs3 c0 (rs4@〈grid,false〉::rs2) ???)
316 [ #x #Hrs3' whd in ⊢ (??%?); >Hsplit in Hrs1;>Hsplit in Hrs3;
317 #Hrs3 #Hrs1 >(Hrs1 …) [| @memb_append_l1 @Hrs3'|]
318 >(Hrs3 … Hrs3') @Hrs1' >Hsplit @memb_append_l1 //
319 | whd in ⊢ (??%?); >Hc0 %
320 | >Hsplit >associative_append % ] -Hta #Hta
322 [ * #tb * whd in ⊢ (%→?); #Hta'
325 [ #_ #Htb' >Htb' in Htb; #Htb
326 generalize in match Hsplit; -Hsplit
328 [ #Hta #Hsplit >(Htb … Hta)
329 >(?:c0 = 〈bar,false〉)
330 [ @(ex_intro ?? grid) @(ex_intro ?? false)
332 [(* Hsplit *) @daemon |(*Hrs3*) @daemon ] | % ] | % ]
333 | (* Hc0 *) @daemon ]
334 | #r5 #rs5 >(eq_pair_fst_snd … r5)
335 #Hta #Hsplit >(Htb … Hta)
336 >(?:c0 = 〈bar,false〉)
337 [ @(ex_intro ?? (\fst r5)) @(ex_intro ?? (\snd r5))
338 % [ % [ % [ (* Hc0, Hsplit *) @daemon | (*Hrs3*) @daemon ] | % ]
339 | % ] | (* Hc0 *) @daemon ] ] | >Hta % ]
340 | * #tb * whd in ⊢ (%→?); #Hta'
343 [ #Hfalse @False_ind >Hfalse in Hc0;
349 definition init_current_on_match ≝
351 (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
352 (seq ? (move_r ?) (mark ?)))).
354 definition R_init_current_on_match ≝ λt1,t2.
355 ∀l1,l2,c,rs. no_grids l1 → is_grid c = false →
356 t1 = midtape STape (l1@〈c,false〉::〈grid,false〉::l2) 〈grid,false〉 rs →
357 t2 = midtape STape (〈grid,false〉::l2) 〈c,true〉 ((reverse ? l1)@〈grid,false〉::rs).
359 lemma sem_init_current_on_match :
360 Realize ? init_current_on_match R_init_current_on_match.
362 cases (sem_seq ????? (sem_move_l ?)
363 (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
364 (sem_seq ????? (sem_move_r ?) (sem_mark ?))) intape)
365 #k * #outc * #Hloop #HR
366 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop] -Hloop
367 #l1 #l2 #c #rs #Hl1 #Hc #Hintape
368 cases HR -HR #ta * whd in ⊢ (%→?); #Hta lapply (Hta … Hintape) -Hta -Hintape
369 generalize in match Hl1; cases l1
370 [#Hl1 whd in ⊢ ((???(??%%%))→?); #Hta
371 * #tb * whd in ⊢ (%→?); #Htb cases (Htb … Hta) -Hta
372 [* >Hc #Htemp destruct (Htemp) ]
373 * #_ #Htc lapply (Htc [ ] 〈grid,false〉 ? (refl ??) (refl …) Hl1)
374 whd in ⊢ ((???(??%%%))→?); -Htc #Htc
375 * #td * whd in ⊢ (%→?); #Htd lapply (Htd … Htc) -Htc -Htd
376 whd in ⊢ ((???(??%%%))→?); #Htd
377 whd in ⊢ (%→?); #Houtc lapply (Houtc … Htd) -Houtc #Houtc
379 |#d #tl #Htl whd in ⊢ ((???(??%%%))→?); #Hta
380 * #tb * whd in ⊢ (%→?); #Htb cases (Htb … Hta) -Htb
381 [* >(Htl … (memb_hd …)) #Htemp destruct (Htemp)]
382 * #Hd >append_cons #Htb lapply (Htb … (refl ??) (refl …) ?)
383 [#x #membx cases (memb_append … membx) -membx #membx
384 [@Htl @memb_cons @membx | >(memb_single … membx) @Hc]]-Htb #Htb
385 * #tc * whd in ⊢ (%→?); #Htc lapply (Htc … Htb) -Htb -Htc
386 >reverse_append >associative_append whd in ⊢ ((???(??%%%))→?); #Htc
387 whd in ⊢ (%→?); #Houtc lapply (Houtc … Htc) -Houtc #Houtc
388 >Houtc >reverse_cons >associative_append %
393 definition init_current_gen ≝
394 seq ? (adv_to_mark_l ? (is_marked ?))
395 (seq ? (clear_mark ?)
397 (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
398 (seq ? (move_r ?) (mark ?))))).
400 definition R_init_current_gen ≝ λt1,t2.
401 ∀l1,c,l2,b,l3,c1,rs,c0,b0. no_marks l1 → no_grids l2 →
402 Some ? 〈c0,b0〉 = option_hd ? (reverse ? (〈c,true〉::l2)) →
403 t1 = midtape STape (l1@〈c,true〉::l2@〈grid,b〉::l3) 〈c1,false〉 rs →
404 t2 = midtape STape (〈grid,b〉::l3) 〈c0,true〉
405 ((tail ? (reverse ? (l1@〈c,false〉::l2))@〈c1,false〉::rs)).
407 lemma sem_init_current_gen : Realize ? init_current_gen R_init_current_gen.
409 cases (sem_seq ????? (sem_adv_to_mark_l ? (is_marked ?))
410 (sem_seq ????? (sem_clear_mark ?)
411 (sem_seq ????? (sem_move_l ?)
412 (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
413 (sem_seq ????? (sem_move_r ?) (sem_mark ?))))) intape)
414 #k * #outc * #Hloop #HR
415 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop] -Hloop
416 #l1 #c #l2 #b #l3 #c1 #rs #c0 #b0 #Hl1 #Hl2 #Hc #Hintape
417 cases HR -HR #ta * whd in ⊢ (%→?); #Hta cases (Hta … Hintape) -Hta -Hintape
418 [ * #Hfalse normalize in Hfalse; destruct (Hfalse) ]
419 * #_ #Hta lapply (Hta l1 〈c,true〉 ? (refl ??) ??) [@Hl1|%] -Hta #Hta
420 * #tb * whd in ⊢ (%→?); #Htb lapply (Htb … Hta) -Htb -Hta #Htb
421 * #tc * whd in ⊢ (%→?); #Htc lapply (Htc … Htb) -Htc -Htb
422 generalize in match Hc; generalize in match Hl2; cases l2
423 [#_ whd in ⊢ ((???%)→?); #Htemp destruct (Htemp)
424 whd in ⊢ ((???(??%%%))→?); #Htc
425 * #td * whd in ⊢ (%→?); #Htd cases (Htd … Htc) -Htd
426 [2: * whd in ⊢ (??%?→?); #Htemp destruct (Htemp) ]
427 * #_ #Htd >Htd in Htc; -Htd #Htd
428 * #te * whd in ⊢ (%→?); #Hte lapply (Hte … Htd) -Htd
429 >reverse_append >reverse_cons
430 whd in ⊢ ((???(??%%%))→?); #Hte
431 whd in ⊢ (%→?); #Houtc lapply (Houtc … Hte) -Houtc -Hte #Houtc
433 |#d #tl #Htl #Hc0 whd in ⊢ ((???(??%%%))→?); #Htc
434 * #td * whd in ⊢ (%→?); #Htd cases (Htd … Htc) -Htd
435 [* >(Htl … (memb_hd …)) whd in ⊢ (??%?→?); #Htemp destruct (Htemp)]
436 * #Hd #Htd lapply (Htd … (refl ??) (refl ??) ?)
437 [#x #membx @Htl @memb_cons @membx] -Htd #Htd
438 * #te * whd in ⊢ (%→?); #Hte lapply (Hte … Htd) -Htd
439 >reverse_append >reverse_cons >reverse_cons
440 >reverse_cons in Hc0; >reverse_cons cases (reverse ? tl)
441 [normalize in ⊢ (%→?); #Hc0 destruct (Hc0) #Hte
442 whd in ⊢ (%→?); #Houtc lapply (Houtc … Hte) -Houtc -Hte #Houtc
444 |* #c2 #b2 #tl2 normalize in ⊢ (%→?); #Hc0 destruct (Hc0)
445 whd in ⊢ ((???(??%%%))→?); #Hte
446 whd in ⊢ (%→?); #Houtc lapply (Houtc … Hte) -Houtc -Hte #Houtc
447 >Houtc >associative_append >associative_append >associative_append %
452 definition init_current ≝
453 seq ? (adv_to_mark_l ? (is_marked ?))
454 (seq ? (clear_mark ?)
455 (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
456 (seq ? (move_r ?) (mark ?)))).
458 definition R_init_current ≝ λt1,t2.
459 ∀l1,c,l2,b,l3,c1,rs,c0,b0. no_marks l1 → no_grids l2 → is_grid c = false →
460 Some ? 〈c0,b0〉 = option_hd ? (reverse ? (〈c,true〉::l2)) →
461 t1 = midtape STape (l1@〈c,true〉::l2@〈grid,b〉::l3) 〈c1,false〉 rs →
462 t2 = midtape STape (〈grid,b〉::l3) 〈c0,true〉
463 ((tail ? (reverse ? (l1@〈c,false〉::l2))@〈c1,false〉::rs)).
465 lemma sem_init_current : Realize ? init_current R_init_current.
467 cases (sem_seq ????? (sem_adv_to_mark_l ? (is_marked ?))
468 (sem_seq ????? (sem_clear_mark ?)
469 (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
470 (sem_seq ????? (sem_move_r ?) (sem_mark ?)))) intape)
471 #k * #outc * #Hloop #HR
472 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop]
473 cases HR -HR #ta * whd in ⊢ (%→?); #Hta
474 * #tb * whd in ⊢ (%→?); #Htb
475 * #tc * whd in ⊢ (%→?); #Htc
476 * #td * whd in ⊢ (%→%→?); #Htd #Houtc
477 #l1 #c #l2 #b #l3 #c1 #rs #c0 #b0 #Hl1 #Hl2 #Hc #Hc0 #Hintape
478 cases (Hta … Hintape) [ * #Hfalse normalize in Hfalse; destruct (Hfalse) ]
479 -Hta * #_ #Hta lapply (Hta l1 〈c,true〉 ? (refl ??) ??) [@Hl1|%]
480 -Hta #Hta lapply (Htb … Hta) -Htb #Htb cases (Htc … Htb) [ >Hc -Hc * #Hc destruct (Hc) ]
481 -Htc * #_ #Htc lapply (Htc … (refl ??) (refl ??) ?) [@Hl2]
482 -Htc #Htc lapply (Htd … Htc) -Htd
483 >reverse_append >reverse_cons
484 >reverse_cons in Hc0; cases (reverse … l2)
485 [ normalize in ⊢ (%→?); #Hc0 destruct (Hc0)
486 #Htd >(Houtc … Htd) %
487 | * #c2 #b2 #tl2 normalize in ⊢ (%→?);
488 #Hc0 #Htd >(Houtc … Htd)
489 whd in ⊢ (???%); destruct (Hc0)
490 >associative_append >associative_append %
494 definition match_tuple_step ≝
495 ifTM ? (test_char ? (λc:STape.¬ is_grid (\fst c)))
498 (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
500 (seq ? mark_next_tuple
501 (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
502 (mark ?) (seq ? (move_l ?) init_current) tc_true)) tc_true)))
505 definition R_match_tuple_step_true ≝ λt1,t2.
506 ∀ls,c,l1,l2,c1,l3,l4,rs,n.
507 bit_or_null c = true → only_bits_or_nulls l1 → no_marks l1 (* → no_grids l2 *) → bit_or_null c1 = true →
508 only_bits_or_nulls l3 → n = |l1| → |l1| = |l3| →
509 table_TM (S n) (l2@〈bar,false〉::〈c1,false〉::l3@〈comma,false〉::l4) →
510 t1 = midtape STape (〈grid,false〉::ls) 〈c,true〉
511 (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,true〉::l3@〈comma,false〉::l4@〈grid,false〉::rs) →
513 (〈c,false〉::l1 = 〈c1,false〉::l3 ∧
514 t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls) 〈grid,false〉
515 (l2@〈bar,false〉::〈c1,false〉::l3@〈comma,true〉::l4@〈grid,false〉::rs))
517 (* non facciamo match e marchiamo la prossima tupla *)
518 ((〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧
519 ∃c2,l5,l6.l4 = l5@〈bar,false〉::〈c2,false〉::l6 ∧
520 (* condizioni su l5 l6 l7 *)
521 t2 = midtape STape (〈grid,false〉::ls) 〈c,true〉
522 (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,false〉::l3@〈comma,false〉::
523 l5@〈bar,false〉::〈c2,true〉::l6@〈grid,false〉::rs))
525 (* non facciamo match e non c'è una prossima tupla:
526 non specifichiamo condizioni sul nastro di output, perché
527 non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
528 (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧ no_bars l4 ∧ current ? t2 = Some ? 〈grid,true〉)).
530 definition R_match_tuple_step_false ≝ λt1,t2.
531 ∀ls,c,rs.t1 = midtape STape ls c rs → is_grid (\fst c) = true ∧ t2 = t1.
533 include alias "basics/logic.ma".
536 lemma eq_f4: ∀A1,A2,A3,A4,B.∀f:A1 → A2 →A3 →A4 →B.
537 ∀x1,x2,x3,x4,y1,y2,y3,y4. x1 = y1 → x2 = y2 →x3=y3 →x4 = y4 →
538 f x1 x2 x3 x4 = f y1 y2 y3 y4.
542 lemma some_option_hd: ∀A.∀l:list A.∀a.∃b.
543 Some ? b = option_hd ? (l@[a]) .
544 #A #l #a cases l normalize /2/
547 axiom tech_split2 : ∀A,l1,l2,l3,l4,x.
548 memb A x l1 = false → memb ? x l3 = false →
549 l1@x::l2 = l3@x::l4 → l1 = l3 ∧ l2 = l4.
551 axiom injective_append : ∀A,l.injective … (λx.append A x l).
553 lemma sem_match_tuple_step:
554 accRealize ? match_tuple_step (inr … (inl … (inr … 0)))
555 R_match_tuple_step_true R_match_tuple_step_false.
556 @(acc_sem_if_app … (sem_test_char ? (λc:STape.¬ is_grid (\fst c))) …
557 (sem_seq … sem_compare
558 (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
560 (sem_seq … sem_mark_next_tuple
561 (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
562 (sem_mark ?) (sem_seq … (sem_move_l …) (sem_init_current …))))))
564 [(* is_grid: termination case *)
565 2:#t1 #t2 #t3 whd in ⊢ (%→?); #H #H1 whd #ls #c #rs #Ht1
566 cases (H c ?) [2: >Ht1 %] #Hgrid #Heq %
567 [@injective_notb @Hgrid | <Heq @H1]
568 |#tapea #tapeout #tapeb whd in ⊢ (%→?); #Htapea
569 * #tapec * #Hcompare #Hor
570 #ls #c #l1 #l2 #c1 #l3 #l4 #rs #n #Hc #Hl1bars #Hl1marks #Hc1 #Hl3 #eqn
571 #eqlen #Htable #Htapea1 cases (Htapea 〈c,true〉 ?) >Htapea1 [2:%]
572 #notgridc -Htapea -Htapea1 -tapea #Htapeb
573 cases (Hcompare … Htapeb) -Hcompare -Htapeb * #_ #_ #Hcompare
574 cases (Hcompare c c1 l1 l3 (l2@[〈bar,false〉]) (l4@〈grid,false〉::rs) eqlen Hl1bars Hl3 Hl1marks … (refl …) Hc ?)
576 [* #Htemp destruct (Htemp) #Htapec %1 % [%]
577 >Htapec in Hor; -Htapec *
578 [2: * #t3 * whd in ⊢ (%→?); #H @False_ind
579 cases (H … (refl …)) whd in ⊢ ((??%?)→?); #H destruct (H)
580 |* #taped * whd in ⊢ (%→?); #Htaped cases (Htaped ? (refl …)) -Htaped *
581 #Htaped whd in ⊢ (%→?); #Htapeout >Htapeout >Htaped >associative_append
584 |* #la * #c' * #d' * #lb * #lc * * * #H1 #H2 #H3 #Htapec
585 cut (〈c,false〉::l1 ≠ 〈c1,false〉::l3)
587 [@(not_to_not …H1) normalize #H destruct %
588 |#x #tl @not_to_not normalize #H destruct //
591 cut (bit_or_null d' = true)
593 [normalize in ⊢ (%→?); #H destruct //
594 |#x #tl #H @(Hl3 〈d',false〉)
595 normalize in H; destruct @memb_append_l2 @memb_hd
598 >Htapec in Hor; -Htapec *
599 [* #taped * whd in ⊢ (%→?); #H @False_ind
600 cases (H … (refl …)) >(bit_or_null_not_grid ? Hd') #Htemp destruct (Htemp)
601 |* #taped * whd in ⊢ (%→?); #H cases (H … (refl …)) -H #_
602 #Htaped * #tapee * whd in ⊢ (%→?); #Htapee
603 <(associative_append ? lc (〈comma,false〉::l4)) in Htaped; #Htaped
604 cases (Htapee … Htaped ???) -Htaped -Htapee
605 [* #rs3 * * (* we proceed by cases on rs4 *)
606 [(* rs4 is empty : the case is absurd since the tape
607 cannot end with a bar *)
608 * #d * #b * * * #Heq1 @False_ind
609 cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut
610 >Hcut in Htable; >H3 >associative_append
611 normalize >Heq1 >Hcut <associative_append >Hcut
612 <associative_append #Htable @(absurd … Htable)
615 * #d2 #b2 #rs3' * #d * #b * * * #Heq1 #Hnobars
616 cut (memb STape 〈d2,b2〉 (l2@〈bar,false〉::〈c1,false〉::l3@〈comma,false〉::l4) = true)
617 [@memb_append_l2 @memb_cons
618 cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut
619 >Hcut >H3 >associative_append @memb_append_l2
620 @memb_cons >Heq1 @memb_append_l2 @memb_cons @memb_hd] #d2intable
621 cut (is_grid d2 = false)
622 [@(no_grids_in_table … Htable … 〈d2,b2〉 d2intable)] #Hd2
624 [@(no_marks_in_table … Htable … 〈d2,b2〉 d2intable)] #Hb2
625 >Hb2 in Heq1; #Heq1 -Hb2 -b2
626 whd in ⊢ ((???%)→?); #Htemp destruct (Htemp) #Htapee >Htapee -Htapee *
627 [(* we know current is not grid *)
628 * #tapef * whd in ⊢ (%→?); #Htapef
629 cases (Htapef … (refl …)) >Hd2 #Htemp destruct (Htemp)
630 |* #tapef * whd in ⊢ (%→?); #Htapef
631 cases (Htapef … (refl …)) #_ -Htapef #Htapef
632 * #tapeg >Htapef -Htapef *
635 #H lapply (H … (refl …)) whd in ⊢ (???%→?); -H #Htapeg
638 whd in ⊢ (%→?); #Htapeout
639 %1 cases (some_option_hd ? (reverse ? (reverse ? la)) 〈c',true〉)
642 (Htapeout (reverse ? rs3 @〈d',false〉::reverse ? la@reverse ? (l2@[〈bar,false〉])@(〈grid,false〉::reverse ? lb))
643 c' (reverse ? la) false ls bar (〈d2,true〉::rs3'@〈grid,false〉::rs) c00 b00 ?????) -Htapeout
644 [whd in ⊢ (??(??%??)?); @eq_f3 [2:%|3: %]
646 generalize in match (〈c',true〉::reverse ? la@〈grid,false〉::ls); #l
647 whd in ⊢ (???(???%)); >associative_append >associative_append %
648 |>reverse_cons @Hoption
650 [normalize in ⊢ (%→?); #Htemp destruct (Htemp)
651 @injective_notb @notgridc
652 |#x #tl normalize in ⊢ (%→?); #Htemp destruct (Htemp)
653 @bit_or_null_not_grid @(Hl1bars 〈c',false〉) @memb_append_l2 @memb_hd
655 |cut (only_bits_or_nulls (la@(〈c',false〉::lb)))
656 [<H2 whd #c0 #Hmemb cases (orb_true_l … Hmemb)
657 [#eqc0 >(\P eqc0) @Hc |@Hl1bars]
658 |#Hl1' #x #Hx @bit_or_null_not_grid @Hl1'
659 @memb_append_l1 @daemon
662 |>reverse_append >reverse_cons >reverse_reverse
663 >reverse_append >reverse_reverse
664 >reverse_cons >reverse_append >reverse_reverse
665 >reverse_append >reverse_cons >reverse_reverse
667 #Htapeout % [@Hnoteq]
669 cut (∃rs32.rs3 = lc@〈comma,false〉::rs32)
670 [ (*cases (tech_split STape (λc.c == 〈bar,false〉) l4)
672 | * #l41 * * #cbar #bfalse * #l42 * * #Hbar #Hl4 #Hl41
673 @(ex_intro ?? l41) >Hl4 in Heq1; #Heq1
675 cut (sublist … lc l3)
676 [ #x #Hx cases la in H3;
677 [ normalize #H3 destruct (H3) @Hx
678 | #p #la' normalize #Hla' destruct (Hla')
679 @memb_append_l2 @memb_cons @Hx ] ] #Hsublist*)
683 (〈c1,false〉::l3@〈comma,false〉::l4= la@〈d',false〉::rs3@〈bar,false〉::〈d2,b2〉::rs3')
685 cut (l4=rs32@〈bar,false〉::〈d2,false〉::rs3')
686 [ >Hrs3 in Heq1; @daemon ] #Hl4
687 @(ex_intro … rs32) @(ex_intro … rs3') %
691 |(*>Hrs3 *)>append_cons
692 > (?:l1@〈grid,false〉::l2@〈bar,false〉::〈c1,false〉::l3@〈comma,false〉::rs32@〈bar,false〉::〈d2,true〉::rs3'@〈grid,false〉::rs
693 = (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,false〉::l3@〈comma,false〉::rs32@[〈bar,false〉])@〈d2,true〉::rs3'@〈grid,false〉::rs)
694 [|>associative_append normalize
695 >associative_append normalize
696 >associative_append normalize
697 >associative_append normalize
700 @(injective_append … (〈d2,false〉::rs3'))
701 >(?:(la@[〈c',false〉])@((((lb@[〈grid,false〉])@l2@[〈bar,false〉])@la)@[〈d',false〉])@rs3
702 =((la@〈c',false〉::lb)@([〈grid,false〉]@l2@[〈bar,false〉]@la@[〈d',false〉]@rs3)))
703 [|>associative_append >associative_append
704 >associative_append >associative_append >associative_append
705 >associative_append >associative_append % ]
706 <H2 normalize (* <Hrs3 *)
707 >associative_append >associative_append >associative_append
708 @eq_f normalize @eq_f >associative_append
709 >associative_append @eq_f normalize @eq_f
710 >(append_cons ? 〈d',false〉) >associative_append
711 <Heq1 >Hl4 <associative_append <append_cons
713 >associative_append normalize
714 >associative_append normalize %
720 |* #Hnobars #Htapee >Htapee -Htapee *
721 [whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
722 cases (Htapef … (refl …)) -Htapef #_ #Htapef >Htapef -Htapef
723 whd in ⊢ (%→?); #Htapeout %2
724 >(Htapeout … (refl …)) %
727 | whd #x #Hx @Hnobars @memb_append_l2 @memb_cons //
731 |whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
732 cases (Htapef … (refl …)) -Htapef
733 whd in ⊢ ((??%?)→?); #Htemp destruct (Htemp)
735 |(* no marks in table *)
736 #x #membx @(no_marks_in_table … Htable)
737 @memb_append_l2 @memb_cons
738 cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut >Hcut
739 >H3 >associative_append @memb_append_l2 @memb_cons @membx
740 |(* no grids in table *)
741 #x #membx @(no_grids_in_table … Htable)
742 @memb_append_l2 @memb_cons
743 cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut >Hcut
744 >H3 >associative_append @memb_append_l2 @memb_cons @membx
745 |whd in ⊢ (??%?); >(bit_or_null_not_grid … Hd') >(bit_or_null_not_bar … Hd') %
748 |#x #membx @(no_marks_in_table … Htable)
749 @memb_append_l2 @memb_cons @memb_cons @memb_append_l1 @membx
750 |#x #membx @(no_marks_in_table … Htable)
751 cases (memb_append … membx) -membx #membx
752 [@memb_append_l1 @membx | @memb_append_l2 >(memb_single … membx) @memb_hd]
753 |>associative_append %
762 scrolls through the tuples in the transition table until one matching the
763 current configuration is found
766 definition match_tuple ≝ whileTM ? match_tuple_step (inr … (inl … (inr … 0))).
768 definition R_match_tuple ≝ λt1,t2.
770 is_bit c = true → only_bits_or_nulls l1 → is_bit c1 = true → n = |l1| →
771 table_TM (S n) (〈c1,false〉::l2) →
772 t1 = midtape STape (〈grid,false〉::ls) 〈c,true〉
773 (l1@〈grid,false〉::〈c1,true〉::l2@〈grid,false〉::rs) →
776 〈c1,false〉::l2 = l3@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l4 ∧
777 t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls) 〈grid,false〉
778 (l3@〈c,false〉::l1@〈comma,true〉::newc@〈comma,false〉::mv::l4@〈grid,false〉::rs))
780 (* non facciamo match su nessuna tupla;
781 non specifichiamo condizioni sul nastro di output, perché
782 non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
783 (current ? t2 = Some ? 〈grid,true〉 ∧
785 〈c1,false〉::l2 ≠ l3@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l4).