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 axiom num_of_state : ∀state:FinSet. state → list (unialpha × bool).
57 definition tuple_of_pair ≝ λstates: FinSet.
59 λp: (states × (option FinBool)) × (states × (option (FinBool × move))).
62 let cin ≝ match a with [ None ⇒ null | Some b ⇒ bit b ] in
63 let 〈qn,action〉 ≝ outp in
67 | Some act ⇒ let 〈na,m〉 ≝ act in
69 [ R ⇒ 〈bit na,bit true〉
70 | L ⇒ 〈bit na,bit false〉
73 let qin ≝ num_of_state states q in
74 let qout ≝ num_of_state states qn in
75 mk_tuple qin 〈cin,false〉 qout 〈cout,false〉 〈mv,false〉.
81 (* by definition, a tuple is not marked *)
82 definition tuple_TM : nat → list STape → Prop ≝
83 λn,t.∃qin,cin,qout,cout,mv.
84 no_marks qin ∧ no_marks qout ∧
85 only_bits qin ∧ only_bits qout ∧
86 bit_or_null cin = true ∧ bit_or_null cout = true ∧ bit_or_null mv = true ∧
87 (cout = null → mv = null) ∧
88 |qin| = n ∧ |qout| = n ∧
89 t = mk_tuple qin 〈cin,false〉 qout 〈cout,false〉 〈mv,false〉.
91 inductive table_TM (n:nat) : list STape → Prop ≝
92 | ttm_nil : table_TM n []
93 | ttm_cons : ∀t1,T.tuple_TM n t1 → table_TM n T → table_TM n (t1@〈bar,false〉::T).
95 inductive match_in_table (n:nat) (qin:list STape) (cin: STape)
96 (qout:list STape) (cout:STape) (mv:STape)
100 tuple_TM n (mk_tuple qin cin qout cout mv) →
101 match_in_table n qin cin qout cout mv
102 (mk_tuple qin cin qout cout mv @〈bar,false〉::tb)
104 ∀qin0,cin0,qout0,cout0,mv0,tb.
105 tuple_TM n (mk_tuple qin0 cin0 qout0 cout0 mv0) →
106 match_in_table n qin cin qout cout mv tb →
107 match_in_table n qin cin qout cout mv
108 (mk_tuple qin0 cin0 qout0 cout0 mv0@〈bar,false〉::tb).
110 axiom append_l1_injective :
111 ∀A.∀l1,l2,l3,l4:list A. |l1| = |l2| → l1@l3 = l2@l4 → l1 = l2.
112 axiom append_l2_injective :
113 ∀A.∀l1,l2,l3,l4:list A. |l1| = |l2| → l1@l3 = l2@l4 → l3 = l4.
114 axiom cons_injective_l : ∀A.∀a1,a2:A.∀l1,l2.a1::l1 = a2::l2 → a1 = a2.
115 axiom cons_injective_r : ∀A.∀a1,a2:A.∀l1,l2.a1::l1 = a2::l2 → l1 = l2.
116 axiom tuple_len : ∀n,t.tuple_TM n t → |t| = 2*n+5.
117 axiom append_eq_tech1 :
118 ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → |l1| < |l3| → ∃la:list A.l1@a::la = l3.
119 axiom append_eq_tech2 :
120 ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → memb A a l4 = false → ∃la:list A.l3 = l1@a::la.
121 (*axiom list_decompose_cases :
122 ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → ∃la,lb:list A.l3 = la@a::lb ∨ l4 = la@a::lb.
123 axiom list_decompose_l :
124 ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → memb A a l4 = false →
125 ∃la,lb.l2 = la@lb ∧ l3 = l1@a::la.
126 axiom list_decompose_r :
127 ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → memb A a l3 = false →
128 ∃la,lb.l1 = la@lb ∧ l4 = lb@a::l2.
129 axiom list_decompose_memb :
130 ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → |l1| < |l3| → memb A a l3 = true.*)
132 lemma table_invert_r : ∀n,t,T.
133 tuple_TM n t → table_TM n (t@〈bar,false〉::T) → table_TM n T.
134 #n #t #T #Htuple #Htable inversion Htable
135 [ cases t normalize [ #Hfalse | #p #t0 #Hfalse ] destruct (Hfalse)
136 | #t0 #T0 #Htuple0 #Htable0 #_ #Heq lapply (append_l2_injective ?????? Heq)
137 [ >(tuple_len … Htuple) >(tuple_len … Htuple0) % ]
138 -Heq #Heq destruct (Heq) // ]
141 lemma match_in_table_to_tuple :
142 ∀n,T,qin,cin,qout,cout,mv.
143 match_in_table n qin cin qout cout mv T → table_TM n T →
144 tuple_TM n (mk_tuple qin cin qout cout mv).
145 #n #T #qin #cin #qout #cout #mv #Hmatch elim Hmatch
147 | #qin0 #cin0 #qout0 #cout0 #mv0 #tb #Htuple #Hmatch #IH #Htable
148 @IH @(table_invert_r ???? Htable) @Htuple
152 lemma generic_match_to_match_in_table :
154 ∀qin,cin,qout,cout,mv.|qin| = n → |qout| = n →
155 only_bits qin → only_bits qout →
156 bit_or_null (\fst cin) = true → bit_or_null (\fst cout) = true →
157 bit_or_null (\fst mv) = true →
159 T = (t1@qin@cin::〈comma,false〉::qout@cout::〈comma,false〉::[mv])@t2 →
160 match_in_table n qin cin qout cout mv T.
161 #n #T #Htable #qin #cin #qout #cout #mv #Hlenqin #Hlenqout
162 #Hqinbits #Hqoutbits #Hcin #Hcout #Hmv
164 [ #t1 #t2 <associative_append cases (t1@qin) normalize in ⊢ (%→?);
165 [ #Hfalse destruct (Hfalse) | #c0 #t0 #Hfalse whd in Hfalse:(???%); destruct (Hfalse) ]
166 | #tuple #T0 #H1 #Htable0#IH #t1 #t2 #HT cases H1 #qin0 * #cin0 * #qout0 * #cout0 * #mv0
168 #Hqin0marks #Hqout0marks #Hqin0bits #Hqout0bits #Hcin0 #Hcout0 #Hmv0 #Hcout0mv0
169 #Hlenqin0 #Hlenqout0 #Htuple >Htuple in H1; #H1
170 lapply (ttm_cons … T0 H1 Htable0) #Htable
172 [ >Htuple normalize in ⊢ (??%%→?);
173 >associative_append >associative_append #HT
174 cut (qin0 = qin ∧ (〈cin0,false〉 = cin ∧ (qout0 = qout ∧
175 (〈cout0,false〉 = cout ∧ (〈mv0,false〉 = mv ∧ 〈bar,false〉::T0 = t2)))))
176 [ lapply (append_l1_injective … HT) [ >Hlenqin @Hlenqin0 ]
177 #Hqin % [ @Hqin ] -Hqin
178 lapply (append_l2_injective … HT) [ >Hlenqin @Hlenqin0 ] -HT #HT
179 lapply (cons_injective_l ????? HT) #Hcin % [ @Hcin ] -Hcin
180 lapply (cons_injective_r ????? HT) -HT #HT
181 lapply (cons_injective_r ????? HT) -HT
182 >associative_append >associative_append #HT
183 lapply (append_l1_injective … HT) [ >Hlenqout @Hlenqout0 ]
184 #Hqout % [ @Hqout ] -Hqout
185 lapply (append_l2_injective … HT) [ >Hlenqout @Hlenqout0 ] -HT normalize #HT
186 lapply (cons_injective_l ????? HT) #Hcout % [ @Hcout ] -Hcout
187 lapply (cons_injective_r ????? HT) -HT #HT
188 lapply (cons_injective_r ????? HT) -HT #HT
189 lapply (cons_injective_l ????? HT) #Hmv % [ @Hmv ] -Hmv
190 @(cons_injective_r ????? HT) ]
191 -HT * #Hqin * #Hcin * #Hqout * #Hcout * #Hmv #HT0
192 >(?:qin0@(〈cin0,false〉::〈comma,false〉::qout0@[〈cout0,false〉;〈comma,false〉;〈mv0,false〉])@〈bar,false〉::T0
193 = mk_tuple qin cin qout cout mv@〈bar,false〉::T0)
194 [|>Hqin >Hqout >Hcin >Hcout >Hmv normalize >associative_append >associative_append
195 normalize >associative_append % ]
196 % %{qin0} %{cin0} %{qout0} %{cout0} %{mv0} % // % [|@Hlenqout0] % // %
197 [ | @Hcout0mv0 ] % // % // % // % // % // % // %
198 | #c0 #cs0 #HT cut (∃cs1.c0::cs0 = tuple@〈bar,false〉::cs1)
199 [ cases (append_eq_tech1 ?????? HT ?)
200 [ -HT #ta #Hta cases (append_eq_tech2 … Hta ?)
201 [ -Hta #tb #Htb %{tb} @Htb
203 | @le_S_S >length_append >(plus_n_O (|tuple|)) >commutative_plus @le_plus
205 | >Htuple normalize >length_append >length_append @le_plus [ >Hlenqin >Hlenqin0 % ]
206 @le_S_S @le_S_S >length_append >length_append @le_plus [ >Hlenqout >Hlenqout0 % ] %] ]
208 * #cs1 #Hcs1 >Hcs1 in HT; >associative_append >associative_append #HT
209 lapply (append_l2_injective … HT) // -HT #HT
210 lapply (cons_injective_r ????? HT) -HT
211 <associative_append #HT >Htuple %2 // @(IH … HT)
217 lemma table_invert_l : ∀n,T0,qin,cin,qout,cout,mv.
218 table_TM n (mk_tuple qin cin qout cout mv@〈bar,false〉::T0) →
219 tuple_TM n (mk_tuple qin cin qout cout mv).
220 #n #T #qin #cin #qout #cout #mv #HT inversion HT
221 [ change with (append ???) in ⊢ (??(??%?)?→?);cases qin [ #Hfalse | #t0 #ts0 #Hfalse] normalize in Hfalse; destruct (Hfalse)
222 | #t0 #T0 #Ht0 #HT0 #_
225 lemma table_invert_r : ∀n,T0,qin,cin,qout,cout,mv.
226 table n (mk_tuple qin cin qout cout mv@〈bar,false〉::T0) → table n T0.
229 lemma no_grids_in_tuple : ∀n,l.tuple_TM n l → no_grids l.
230 #n #l * #qin * #cin * #qout * #cout * #mv * * * * * * * * * *
231 #_ #_ #Hqin #Hqout #Hcin #Hcout #Hmv #_ #_ #_ #Hl >Hl
232 #c #Hc normalize in Hc; cases (memb_append … Hc) -Hc #Hc
233 [ @bit_not_grid @(Hqin … Hc)
234 | cases (orb_true_l … Hc) -Hc #Hc
235 [ change with (c == 〈cin,false〉 = true) in Hc; >(\P Hc) @bit_or_null_not_grid //
236 | cases (orb_true_l … Hc) -Hc #Hc
237 [ change with (c == 〈comma,false〉 = true) in Hc; >(\P Hc) %
238 | cases (memb_append …Hc) -Hc #Hc
239 [ @bit_not_grid @(Hqout … Hc)
240 | cases (orb_true_l … Hc) -Hc #Hc
241 [ change with (c == 〈cout,false〉 = true) in Hc; >(\P Hc) @bit_or_null_not_grid //
242 | cases (orb_true_l … Hc) -Hc #Hc
243 [ change with (c == 〈comma,false〉 = true) in Hc; >(\P Hc) %
244 | >(memb_single … Hc) @bit_or_null_not_grid @Hmv
248 lemma no_marks_in_tuple : ∀n,l.tuple_TM n l → no_marks l.
249 #n #l * #qin * #cin * #qout * #cout * #mv * * * * * * * * * *
250 #Hqin #Hqout #_ #_ #_ #_ #_ #_ #_ #_ #Hl >Hl
251 #c #Hc normalize in Hc; cases (memb_append … Hc) -Hc #Hc
253 | cases (orb_true_l … Hc) -Hc #Hc
254 [ change with (c == 〈cin,false〉 = true) in Hc; >(\P Hc) %
255 | cases (orb_true_l … Hc) -Hc #Hc
256 [ change with (c == 〈comma,false〉 = true) in Hc; >(\P Hc) %
257 | cases (memb_append … Hc) -Hc #Hc
259 | cases (orb_true_l … Hc) -Hc #Hc
260 [ change with (c == 〈cout,false〉 = true) in Hc; >(\P Hc) %
261 | cases (orb_true_l … Hc) -Hc #Hc
263 [ change with (c == 〈comma,false〉 = true) in Hc; >(\P Hc) %
264 | >(memb_single … Hc) %
268 lemma no_grids_in_table: ∀n.∀l.table_TM n l → no_grids l.
270 [normalize #c #H destruct
271 |#t1 #t2 #Ht1 #Ht2 #IH lapply (no_grids_in_tuple … Ht1) -Ht1 #Ht1 #x #Hx
272 cases (memb_append … Hx) -Hx #Hx
274 | cases (orb_true_l … Hx) -Hx #Hx
279 lemma no_marks_in_table: ∀n.∀l.table_TM n l → no_marks l.
281 [normalize #c #H destruct
282 |#t1 #t2 #Ht1 #Ht2 #IH lapply (no_marks_in_tuple … Ht1) -Ht1 #Ht1 #x #Hx
283 cases (memb_append … Hx) -Hx #Hx
285 | cases (orb_true_l … Hx) -Hx #Hx
290 axiom last_of_table: ∀n,l,b.¬ table_TM n (l@[〈bar,b〉]).
293 l0 x* a l1 x0* a0 l2 ------> l0 x a* l1 x0 a0* l2
296 if current (* x *) = #
299 then move_right; ----
301 if current (* x0 *) = 0
302 then advance_mark ----
306 else x = 1 (* analogo *)
312 MARK NEXT TUPLE machine
313 (partially axiomatized)
315 marks the first character after the first bar (rightwards)
318 definition bar_or_grid ≝ λc:STape.is_bar (\fst c) ∨ is_grid (\fst c).
320 definition mark_next_tuple ≝
321 seq ? (adv_to_mark_r ? bar_or_grid)
322 (ifTM ? (test_char ? (λc:STape.is_bar (\fst c)))
323 (move_right_and_mark ?) (nop ?) 1).
325 definition R_mark_next_tuple ≝
328 (* c non può essere un separatore ... speriamo *)
329 t1 = midtape STape ls c (rs1@〈grid,false〉::rs2) →
330 no_marks rs1 → no_grids rs1 → bar_or_grid c = false →
331 (∃rs3,rs4,d,b.rs1 = rs3 @ 〈bar,false〉 :: rs4 ∧
333 Some ? 〈d,b〉 = option_hd ? (rs4@〈grid,false〉::rs2) ∧
334 t2 = midtape STape (〈bar,false〉::reverse ? rs3@c::ls) 〈d,true〉 (tail ? (rs4@〈grid,false〉::rs2)))
336 (no_bars rs1 ∧ t2 = midtape ? (reverse ? rs1@c::ls) 〈grid,false〉 rs2).
340 (∀x.memb A x l = true → f x = false) ∨
341 (∃l1,c,l2.f c = true ∧ l = l1@c::l2 ∧ ∀x.memb ? x l1 = true → f x = false).
343 [ % #x normalize #Hfalse *)
345 theorem sem_mark_next_tuple :
346 Realize ? mark_next_tuple R_mark_next_tuple.
348 lapply (sem_seq ? (adv_to_mark_r ? bar_or_grid)
349 (ifTM ? (test_char ? (λc:STape.is_bar (\fst c))) (move_right_and_mark ?) (nop ?) 1) ????)
350 [@sem_if [5: // |6: @sem_move_right_and_mark |7: // |*:skip]
352 |||#Hif cases (Hif intape) -Hif
353 #j * #outc * #Hloop * #ta * #Hleft #Hright
354 @(ex_intro ?? j) @ex_intro [|% [@Hloop] ]
356 #ls #c #rs1 #rs2 #Hrs #Hrs1 #Hrs1' #Hc
358 [ * #Hfalse >Hfalse in Hc; #Htf destruct (Htf)
359 | * #_ #Hta cases (tech_split STape (λc.is_bar (\fst c)) rs1)
360 [ #H1 lapply (Hta rs1 〈grid,false〉 rs2 (refl ??) ? ?)
361 [ * #x #b #Hx whd in ⊢ (??%?); >(Hrs1' … Hx) >(H1 … Hx) %
363 | -Hta #Hta cases Hright
364 [ * #tb * whd in ⊢ (%→?); #Hcurrent
365 @False_ind cases (Hcurrent 〈grid,false〉 ?)
366 [ normalize #Hfalse destruct (Hfalse)
368 | * #tb * whd in ⊢ (%→?); #Hcurrent
369 cases (Hcurrent 〈grid,false〉 ?)
370 [ #_ #Htb whd in ⊢ (%→?); #Houtc
373 | >Houtc >Htb >Hta % ]
377 | * #rs3 * #c0 * #rs4 * * #Hc0 #Hsplit #Hrs3
378 % @(ex_intro ?? rs3) @(ex_intro ?? rs4)
379 lapply (Hta rs3 c0 (rs4@〈grid,false〉::rs2) ???)
380 [ #x #Hrs3' whd in ⊢ (??%?); >Hsplit in Hrs1;>Hsplit in Hrs3;
381 #Hrs3 #Hrs1 >(Hrs1 …) [| @memb_append_l1 @Hrs3'|]
382 >(Hrs3 … Hrs3') @Hrs1' >Hsplit @memb_append_l1 //
383 | whd in ⊢ (??%?); >Hc0 %
384 | >Hsplit >associative_append % ] -Hta #Hta
386 [ * #tb * whd in ⊢ (%→?); #Hta'
389 [ #_ #Htb' >Htb' in Htb; #Htb
390 generalize in match Hsplit; -Hsplit
392 [ #Hta #Hsplit >(Htb … Hta)
393 >(?:c0 = 〈bar,false〉)
394 [ @(ex_intro ?? grid) @(ex_intro ?? false)
396 [(* Hsplit *) @daemon |(*Hrs3*) @daemon ] | % ] | % ]
397 | (* Hc0 *) @daemon ]
398 | #r5 #rs5 >(eq_pair_fst_snd … r5)
399 #Hta #Hsplit >(Htb … Hta)
400 >(?:c0 = 〈bar,false〉)
401 [ @(ex_intro ?? (\fst r5)) @(ex_intro ?? (\snd r5))
402 % [ % [ % [ (* Hc0, Hsplit *) @daemon | (*Hrs3*) @daemon ] | % ]
403 | % ] | (* Hc0 *) @daemon ] ] | >Hta % ]
404 | * #tb * whd in ⊢ (%→?); #Hta'
407 [ #Hfalse @False_ind >Hfalse in Hc0;
413 definition init_current_on_match ≝
415 (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
416 (seq ? (move_r ?) (mark ?)))).
418 definition R_init_current_on_match ≝ λt1,t2.
419 ∀l1,l2,c,rs. no_grids l1 → is_grid c = false →
420 t1 = midtape STape (l1@〈c,false〉::〈grid,false〉::l2) 〈grid,false〉 rs →
421 t2 = midtape STape (〈grid,false〉::l2) 〈c,true〉 ((reverse ? l1)@〈grid,false〉::rs).
423 lemma sem_init_current_on_match :
424 Realize ? init_current_on_match R_init_current_on_match.
426 cases (sem_seq ????? (sem_move_l ?)
427 (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
428 (sem_seq ????? (sem_move_r ?) (sem_mark ?))) intape)
429 #k * #outc * #Hloop #HR
430 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop] -Hloop
431 #l1 #l2 #c #rs #Hl1 #Hc #Hintape
432 cases HR -HR #ta * whd in ⊢ (%→?); #Hta lapply (Hta … Hintape) -Hta -Hintape
433 generalize in match Hl1; cases l1
434 [#Hl1 whd in ⊢ ((???(??%%%))→?); #Hta
435 * #tb * whd in ⊢ (%→?); #Htb cases (Htb … Hta) -Hta
436 [* >Hc #Htemp destruct (Htemp) ]
437 * #_ #Htc lapply (Htc [ ] 〈grid,false〉 ? (refl ??) (refl …) Hl1)
438 whd in ⊢ ((???(??%%%))→?); -Htc #Htc
439 * #td * whd in ⊢ (%→?); #Htd lapply (Htd … Htc) -Htc -Htd
440 whd in ⊢ ((???(??%%%))→?); #Htd
441 whd in ⊢ (%→?); #Houtc lapply (Houtc … Htd) -Houtc #Houtc
443 |#d #tl #Htl whd in ⊢ ((???(??%%%))→?); #Hta
444 * #tb * whd in ⊢ (%→?); #Htb cases (Htb … Hta) -Htb
445 [* >(Htl … (memb_hd …)) #Htemp destruct (Htemp)]
446 * #Hd >append_cons #Htb lapply (Htb … (refl ??) (refl …) ?)
447 [#x #membx cases (memb_append … membx) -membx #membx
448 [@Htl @memb_cons @membx | >(memb_single … membx) @Hc]]-Htb #Htb
449 * #tc * whd in ⊢ (%→?); #Htc lapply (Htc … Htb) -Htb -Htc
450 >reverse_append >associative_append whd in ⊢ ((???(??%%%))→?); #Htc
451 whd in ⊢ (%→?); #Houtc lapply (Houtc … Htc) -Houtc #Houtc
452 >Houtc >reverse_cons >associative_append %
457 definition init_current_gen ≝
458 seq ? (adv_to_mark_l ? (is_marked ?))
459 (seq ? (clear_mark ?)
461 (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
462 (seq ? (move_r ?) (mark ?))))).
464 definition R_init_current_gen ≝ λt1,t2.
465 ∀l1,c,l2,b,l3,c1,rs,c0,b0. no_marks l1 → no_grids l2 →
466 Some ? 〈c0,b0〉 = option_hd ? (reverse ? (〈c,true〉::l2)) →
467 t1 = midtape STape (l1@〈c,true〉::l2@〈grid,b〉::l3) 〈c1,false〉 rs →
468 t2 = midtape STape (〈grid,b〉::l3) 〈c0,true〉
469 ((tail ? (reverse ? (l1@〈c,false〉::l2))@〈c1,false〉::rs)).
471 lemma sem_init_current_gen : Realize ? init_current_gen R_init_current_gen.
473 cases (sem_seq ????? (sem_adv_to_mark_l ? (is_marked ?))
474 (sem_seq ????? (sem_clear_mark ?)
475 (sem_seq ????? (sem_move_l ?)
476 (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
477 (sem_seq ????? (sem_move_r ?) (sem_mark ?))))) intape)
478 #k * #outc * #Hloop #HR
479 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop] -Hloop
480 #l1 #c #l2 #b #l3 #c1 #rs #c0 #b0 #Hl1 #Hl2 #Hc #Hintape
481 cases HR -HR #ta * whd in ⊢ (%→?); #Hta cases (Hta … Hintape) -Hta -Hintape
482 [ * #Hfalse normalize in Hfalse; destruct (Hfalse) ]
483 * #_ #Hta lapply (Hta l1 〈c,true〉 ? (refl ??) ??) [@Hl1|%] -Hta #Hta
484 * #tb * whd in ⊢ (%→?); #Htb lapply (Htb … Hta) -Htb -Hta #Htb
485 * #tc * whd in ⊢ (%→?); #Htc lapply (Htc … Htb) -Htc -Htb
486 generalize in match Hc; generalize in match Hl2; cases l2
487 [#_ whd in ⊢ ((???%)→?); #Htemp destruct (Htemp)
488 whd in ⊢ ((???(??%%%))→?); #Htc
489 * #td * whd in ⊢ (%→?); #Htd cases (Htd … Htc) -Htd
490 [2: * whd in ⊢ (??%?→?); #Htemp destruct (Htemp) ]
491 * #_ #Htd >Htd in Htc; -Htd #Htd
492 * #te * whd in ⊢ (%→?); #Hte lapply (Hte … Htd) -Htd
493 >reverse_append >reverse_cons
494 whd in ⊢ ((???(??%%%))→?); #Hte
495 whd in ⊢ (%→?); #Houtc lapply (Houtc … Hte) -Houtc -Hte #Houtc
497 |#d #tl #Htl #Hc0 whd in ⊢ ((???(??%%%))→?); #Htc
498 * #td * whd in ⊢ (%→?); #Htd cases (Htd … Htc) -Htd
499 [* >(Htl … (memb_hd …)) whd in ⊢ (??%?→?); #Htemp destruct (Htemp)]
500 * #Hd #Htd lapply (Htd … (refl ??) (refl ??) ?)
501 [#x #membx @Htl @memb_cons @membx] -Htd #Htd
502 * #te * whd in ⊢ (%→?); #Hte lapply (Hte … Htd) -Htd
503 >reverse_append >reverse_cons >reverse_cons
504 >reverse_cons in Hc0; >reverse_cons cases (reverse ? tl)
505 [normalize in ⊢ (%→?); #Hc0 destruct (Hc0) #Hte
506 whd in ⊢ (%→?); #Houtc lapply (Houtc … Hte) -Houtc -Hte #Houtc
508 |* #c2 #b2 #tl2 normalize in ⊢ (%→?); #Hc0 destruct (Hc0)
509 whd in ⊢ ((???(??%%%))→?); #Hte
510 whd in ⊢ (%→?); #Houtc lapply (Houtc … Hte) -Houtc -Hte #Houtc
511 >Houtc >associative_append >associative_append >associative_append %
517 definition init_current ≝
518 seq ? (adv_to_mark_l ? (is_marked ?))
519 (seq ? (clear_mark ?)
520 (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
521 (seq ? (move_r ?) (mark ?)))).
523 definition R_init_current ≝ λt1,t2.
524 ∀l1,c,l2,b,l3,c1,rs,c0,b0. no_marks l1 → no_grids l2 → is_grid c = false →
525 Some ? 〈c0,b0〉 = option_hd ? (reverse ? (〈c,true〉::l2)) →
526 t1 = midtape STape (l1@〈c,true〉::l2@〈grid,b〉::l3) 〈c1,false〉 rs →
527 t2 = midtape STape (〈grid,b〉::l3) 〈c0,true〉
528 ((tail ? (reverse ? (l1@〈c,false〉::l2))@〈c1,false〉::rs)).
530 lemma sem_init_current : Realize ? init_current R_init_current.
532 cases (sem_seq ????? (sem_adv_to_mark_l ? (is_marked ?))
533 (sem_seq ????? (sem_clear_mark ?)
534 (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
535 (sem_seq ????? (sem_move_r ?) (sem_mark ?)))) intape)
536 #k * #outc * #Hloop #HR
537 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop]
538 cases HR -HR #ta * whd in ⊢ (%→?); #Hta
539 * #tb * whd in ⊢ (%→?); #Htb
540 * #tc * whd in ⊢ (%→?); #Htc
541 * #td * whd in ⊢ (%→%→?); #Htd #Houtc
542 #l1 #c #l2 #b #l3 #c1 #rs #c0 #b0 #Hl1 #Hl2 #Hc #Hc0 #Hintape
543 cases (Hta … Hintape) [ * #Hfalse normalize in Hfalse; destruct (Hfalse) ]
544 -Hta * #_ #Hta lapply (Hta l1 〈c,true〉 ? (refl ??) ??) [@Hl1|%]
545 -Hta #Hta lapply (Htb … Hta) -Htb #Htb cases (Htc … Htb) [ >Hc -Hc * #Hc destruct (Hc) ]
546 -Htc * #_ #Htc lapply (Htc … (refl ??) (refl ??) ?) [@Hl2]
547 -Htc #Htc lapply (Htd … Htc) -Htd
548 >reverse_append >reverse_cons
549 >reverse_cons in Hc0; cases (reverse … l2)
550 [ normalize in ⊢ (%→?); #Hc0 destruct (Hc0)
551 #Htd >(Houtc … Htd) %
552 | * #c2 #b2 #tl2 normalize in ⊢ (%→?);
553 #Hc0 #Htd >(Houtc … Htd)
554 whd in ⊢ (???%); destruct (Hc0)
555 >associative_append >associative_append %
559 definition match_tuple_step ≝
560 ifTM ? (test_char ? (λc:STape.¬ is_grid (\fst c)))
563 (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
565 (seq ? mark_next_tuple
566 (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
567 (mark ?) (seq ? (move_l ?) init_current) tc_true)) tc_true)))
570 definition R_match_tuple_step_true ≝ λt1,t2.
571 ∀ls,c,l1,l2,c1,l3,l4,rs,n.
572 bit_or_null c = true → only_bits_or_nulls l1 → no_marks l1 (* → no_grids l2 *) → bit_or_null c1 = true →
573 only_bits_or_nulls l3 → n = |l1| → |l1| = |l3| →
574 table_TM (S n) (l2@〈bar,false〉::〈c1,false〉::l3@〈comma,false〉::l4) →
575 t1 = midtape STape (〈grid,false〉::ls) 〈c,true〉
576 (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,true〉::l3@〈comma,false〉::l4@〈grid,false〉::rs) →
578 (〈c,false〉::l1 = 〈c1,false〉::l3 ∧
579 t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls) 〈grid,false〉
580 (l2@〈bar,false〉::〈c1,false〉::l3@〈comma,true〉::l4@〈grid,false〉::rs))
582 (* non facciamo match e marchiamo la prossima tupla *)
583 ((〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧
584 ∃c2,l5,l6.l4 = l5@〈bar,false〉::〈c2,false〉::l6 ∧
585 (* condizioni su l5 l6 l7 *)
586 t2 = midtape STape (〈grid,false〉::ls) 〈c,true〉
587 (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,false〉::l3@〈comma,false〉::
588 l5@〈bar,false〉::〈c2,true〉::l6@〈grid,false〉::rs))
590 (* non facciamo match e non c'è una prossima tupla:
591 non specifichiamo condizioni sul nastro di output, perché
592 non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
593 (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧ no_bars l4 ∧ current ? t2 = Some ? 〈grid,true〉)).
595 definition R_match_tuple_step_false ≝ λt1,t2.
596 ∀ls,c,rs.t1 = midtape STape ls c rs → is_grid (\fst c) = true ∧ t2 = t1.
598 include alias "basics/logic.ma".
601 lemma eq_f4: ∀A1,A2,A3,A4,B.∀f:A1 → A2 →A3 →A4 →B.
602 ∀x1,x2,x3,x4,y1,y2,y3,y4. x1 = y1 → x2 = y2 →x3=y3 →x4 = y4 →
603 f x1 x2 x3 x4 = f y1 y2 y3 y4.
607 lemma some_option_hd: ∀A.∀l:list A.∀a.∃b.
608 Some ? b = option_hd ? (l@[a]) .
609 #A #l #a cases l normalize /2/
612 axiom tech_split2 : ∀A,l1,l2,l3,l4,x.
613 memb A x l1 = false → memb ? x l3 = false →
614 l1@x::l2 = l3@x::l4 → l1 = l3 ∧ l2 = l4.
616 axiom injective_append : ∀A,l.injective … (λx.append A x l).
618 lemma sem_match_tuple_step:
619 accRealize ? match_tuple_step (inr … (inl … (inr … 0)))
620 R_match_tuple_step_true R_match_tuple_step_false.
621 @(acc_sem_if_app … (sem_test_char ? (λc:STape.¬ is_grid (\fst c))) …
622 (sem_seq … sem_compare
623 (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
625 (sem_seq … sem_mark_next_tuple
626 (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
627 (sem_mark ?) (sem_seq … (sem_move_l …) (sem_init_current …))))))
629 [(* is_grid: termination case *)
630 2:#t1 #t2 #t3 whd in ⊢ (%→?); #H #H1 whd #ls #c #rs #Ht1
631 cases (H c ?) [2: >Ht1 %] #Hgrid #Heq %
632 [@injective_notb @Hgrid | <Heq @H1]
633 |#tapea #tapeout #tapeb whd in ⊢ (%→?); #Htapea
634 * #tapec * #Hcompare #Hor
635 #ls #c #l1 #l2 #c1 #l3 #l4 #rs #n #Hc #Hl1bars #Hl1marks #Hc1 #Hl3 #eqn
636 #eqlen #Htable #Htapea1 cases (Htapea 〈c,true〉 ?) >Htapea1 [2:%]
637 #notgridc -Htapea -Htapea1 -tapea #Htapeb
638 cases (Hcompare … Htapeb) -Hcompare -Htapeb * #_ #_ #Hcompare
639 cases (Hcompare c c1 l1 l3 (l2@[〈bar,false〉]) (l4@〈grid,false〉::rs) eqlen Hl1bars Hl3 Hl1marks … (refl …) Hc ?)
641 [* #Htemp destruct (Htemp) #Htapec %1 % [%]
642 >Htapec in Hor; -Htapec *
643 [2: * #t3 * whd in ⊢ (%→?); #H @False_ind
644 cases (H … (refl …)) whd in ⊢ ((??%?)→?); #H destruct (H)
645 |* #taped * whd in ⊢ (%→?); #Htaped cases (Htaped ? (refl …)) -Htaped *
646 #Htaped whd in ⊢ (%→?); #Htapeout >Htapeout >Htaped >associative_append
649 |* #la * #c' * #d' * #lb * #lc * * * #H1 #H2 #H3 #Htapec
650 cut (〈c,false〉::l1 ≠ 〈c1,false〉::l3)
652 [@(not_to_not …H1) normalize #H destruct %
653 |#x #tl @not_to_not normalize #H destruct //
656 cut (bit_or_null d' = true)
658 [normalize in ⊢ (%→?); #H destruct //
659 |#x #tl #H @(Hl3 〈d',false〉)
660 normalize in H; destruct @memb_append_l2 @memb_hd
663 >Htapec in Hor; -Htapec *
664 [* #taped * whd in ⊢ (%→?); #H @False_ind
665 cases (H … (refl …)) >(bit_or_null_not_grid ? Hd') #Htemp destruct (Htemp)
666 |* #taped * whd in ⊢ (%→?); #H cases (H … (refl …)) -H #_
667 #Htaped * #tapee * whd in ⊢ (%→?); #Htapee
668 <(associative_append ? lc (〈comma,false〉::l4)) in Htaped; #Htaped
669 cases (Htapee … Htaped ???) -Htaped -Htapee
670 [* #rs3 * * (* we proceed by cases on rs4 *)
671 [(* rs4 is empty : the case is absurd since the tape
672 cannot end with a bar *)
673 * #d * #b * * * #Heq1 @False_ind
674 cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut
675 >Hcut in Htable; >H3 >associative_append
676 normalize >Heq1 >Hcut <associative_append >Hcut
677 <associative_append #Htable @(absurd … Htable)
680 * #d2 #b2 #rs3' * #d * #b * * * #Heq1 #Hnobars
681 cut (memb STape 〈d2,b2〉 (l2@〈bar,false〉::〈c1,false〉::l3@〈comma,false〉::l4) = true)
682 [@memb_append_l2 @memb_cons
683 cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut
684 >Hcut >H3 >associative_append @memb_append_l2
685 @memb_cons >Heq1 @memb_append_l2 @memb_cons @memb_hd] #d2intable
686 cut (is_grid d2 = false)
687 [@(no_grids_in_table … Htable … 〈d2,b2〉 d2intable)] #Hd2
689 [@(no_marks_in_table … Htable … 〈d2,b2〉 d2intable)] #Hb2
690 >Hb2 in Heq1; #Heq1 -Hb2 -b2
691 whd in ⊢ ((???%)→?); #Htemp destruct (Htemp) #Htapee >Htapee -Htapee *
692 [(* we know current is not grid *)
693 * #tapef * whd in ⊢ (%→?); #Htapef
694 cases (Htapef … (refl …)) >Hd2 #Htemp destruct (Htemp)
695 |* #tapef * whd in ⊢ (%→?); #Htapef
696 cases (Htapef … (refl …)) #_ -Htapef #Htapef
697 * #tapeg >Htapef -Htapef *
700 #H lapply (H … (refl …)) whd in ⊢ (???%→?); -H #Htapeg
703 whd in ⊢ (%→?); #Htapeout
704 %1 cases (some_option_hd ? (reverse ? (reverse ? la)) 〈c',true〉)
707 (Htapeout (reverse ? rs3 @〈d',false〉::reverse ? la@reverse ? (l2@[〈bar,false〉])@(〈grid,false〉::reverse ? lb))
708 c' (reverse ? la) false ls bar (〈d2,true〉::rs3'@〈grid,false〉::rs) c00 b00 ?????) -Htapeout
709 [whd in ⊢ (??(??%??)?); @eq_f3 [2:%|3: %]
711 generalize in match (〈c',true〉::reverse ? la@〈grid,false〉::ls); #l
712 whd in ⊢ (???(???%)); >associative_append >associative_append %
713 |>reverse_cons @Hoption
715 [normalize in ⊢ (%→?); #Htemp destruct (Htemp)
716 @injective_notb @notgridc
717 |#x #tl normalize in ⊢ (%→?); #Htemp destruct (Htemp)
718 @bit_or_null_not_grid @(Hl1bars 〈c',false〉) @memb_append_l2 @memb_hd
720 |cut (only_bits_or_nulls (la@(〈c',false〉::lb)))
721 [<H2 whd #c0 #Hmemb cases (orb_true_l … Hmemb)
722 [#eqc0 >(\P eqc0) @Hc |@Hl1bars]
723 |#Hl1' #x #Hx @bit_or_null_not_grid @Hl1'
724 @memb_append_l1 @daemon
727 |>reverse_append >reverse_cons >reverse_reverse
728 >reverse_append >reverse_reverse
729 >reverse_cons >reverse_append >reverse_reverse
730 >reverse_append >reverse_cons >reverse_reverse
732 #Htapeout % [@Hnoteq]
734 cut (∃rs32.rs3 = lc@〈comma,false〉::rs32)
735 [ (*cases (tech_split STape (λc.c == 〈bar,false〉) l4)
737 | * #l41 * * #cbar #bfalse * #l42 * * #Hbar #Hl4 #Hl41
738 @(ex_intro ?? l41) >Hl4 in Heq1; #Heq1
740 cut (sublist … lc l3)
741 [ #x #Hx cases la in H3;
742 [ normalize #H3 destruct (H3) @Hx
743 | #p #la' normalize #Hla' destruct (Hla')
744 @memb_append_l2 @memb_cons @Hx ] ] #Hsublist*)
748 (〈c1,false〉::l3@〈comma,false〉::l4= la@〈d',false〉::rs3@〈bar,false〉::〈d2,b2〉::rs3')
750 cut (l4=rs32@〈bar,false〉::〈d2,false〉::rs3')
751 [ >Hrs3 in Heq1; @daemon ] #Hl4
752 @(ex_intro … rs32) @(ex_intro … rs3') %
756 |(*>Hrs3 *)>append_cons
757 > (?:l1@〈grid,false〉::l2@〈bar,false〉::〈c1,false〉::l3@〈comma,false〉::rs32@〈bar,false〉::〈d2,true〉::rs3'@〈grid,false〉::rs
758 = (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,false〉::l3@〈comma,false〉::rs32@[〈bar,false〉])@〈d2,true〉::rs3'@〈grid,false〉::rs)
759 [|>associative_append normalize
760 >associative_append normalize
761 >associative_append normalize
762 >associative_append normalize
765 @(injective_append … (〈d2,false〉::rs3'))
766 >(?:(la@[〈c',false〉])@((((lb@[〈grid,false〉])@l2@[〈bar,false〉])@la)@[〈d',false〉])@rs3
767 =((la@〈c',false〉::lb)@([〈grid,false〉]@l2@[〈bar,false〉]@la@[〈d',false〉]@rs3)))
768 [|>associative_append >associative_append
769 >associative_append >associative_append >associative_append
770 >associative_append >associative_append % ]
771 <H2 normalize (* <Hrs3 *)
772 >associative_append >associative_append >associative_append
773 @eq_f normalize @eq_f >associative_append
774 >associative_append @eq_f normalize @eq_f
775 >(append_cons ? 〈d',false〉) >associative_append
776 <Heq1 >Hl4 <associative_append <append_cons
778 >associative_append normalize
779 >associative_append normalize %
785 |* #Hnobars #Htapee >Htapee -Htapee *
786 [whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
787 cases (Htapef … (refl …)) -Htapef #_ #Htapef >Htapef -Htapef
788 whd in ⊢ (%→?); #Htapeout %2
789 >(Htapeout … (refl …)) %
792 | whd #x #Hx @Hnobars @memb_append_l2 @memb_cons //
796 |whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
797 cases (Htapef … (refl …)) -Htapef
798 whd in ⊢ ((??%?)→?); #Htemp destruct (Htemp)
800 |(* no marks in table *)
801 #x #membx @(no_marks_in_table … Htable)
802 @memb_append_l2 @memb_cons
803 cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut >Hcut
804 >H3 >associative_append @memb_append_l2 @memb_cons @membx
805 |(* no grids in table *)
806 #x #membx @(no_grids_in_table … Htable)
807 @memb_append_l2 @memb_cons
808 cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut >Hcut
809 >H3 >associative_append @memb_append_l2 @memb_cons @membx
810 |whd in ⊢ (??%?); >(bit_or_null_not_grid … Hd') >(bit_or_null_not_bar … Hd') %
813 |#x #membx @(no_marks_in_table … Htable)
814 @memb_append_l2 @memb_cons @memb_cons @memb_append_l1 @membx
815 |#x #membx @(no_marks_in_table … Htable)
816 cases (memb_append … membx) -membx #membx
817 [@memb_append_l1 @membx | @memb_append_l2 >(memb_single … membx) @memb_hd]
818 |>associative_append %
827 scrolls through the tuples in the transition table until one matching the
828 current configuration is found
831 definition match_tuple ≝ whileTM ? match_tuple_step (inr … (inl … (inr … 0))).
833 definition R_match_tuple ≝ λt1,t2.
835 is_bit c = true → only_bits_or_nulls l1 → is_bit c1 = true → n = |l1| →
836 table_TM (S n) (〈c1,false〉::l2) →
837 t1 = midtape STape (〈grid,false〉::ls) 〈c,true〉
838 (l1@〈grid,false〉::〈c1,true〉::l2@〈grid,false〉::rs) →
841 〈c1,false〉::l2 = l3@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l4 ∧
842 t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls) 〈grid,false〉
843 (l3@〈c,false〉::l1@〈comma,true〉::newc@〈comma,false〉::mv::l4@〈grid,false〉::rs))
845 (* non facciamo match su nessuna tupla;
846 non specifichiamo condizioni sul nastro di output, perché
847 non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
848 (current ? t2 = Some ? 〈grid,true〉 ∧
850 〈c1,false〉::l2 ≠ l3@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l4).