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 no_grids ≝ λl.
25 ∀c.memb STape c l = true → is_grid (\fst c) = false.
27 definition no_bars ≝ λl.
28 ∀c.memb STape c l = true → is_bar (\fst c) = false.
30 definition no_marks ≝ λl.
31 ∀c.memb STape c l = true → is_marked ? c = false.
33 lemma bit_not_grid: ∀d. is_bit d = true → is_grid d = false.
34 * // normalize #H destruct
37 lemma bit_not_bar: ∀d. is_bit d = true → is_bar d = false.
38 * // normalize #H destruct
41 (* by definition, a tuple is not marked *)
42 definition tuple_TM : nat → list STape → Prop ≝
45 only_bits qin ∧ only_bits qout ∧ only_bits mv ∧
46 |qin| = n ∧ |qout| = n (* ∧ |mv| = ? *) ∧
47 t = qin@〈comma,false〉::qout@〈comma,false〉::mv.
49 inductive table_TM : nat → list STape → Prop ≝
50 | ttm_nil : ∀n.table_TM n []
51 | ttm_cons : ∀n,t1,T.tuple_TM n t1 → table_TM n T → table_TM n (t1@〈bar,false〉::T).
53 lemma no_grids_in_table: ∀n.∀l.table_TM n l → no_grids l.
55 [normalize #n #x #H destruct
56 |#m #t1 #t2 * #qin * #qout * #mv * * * * * *
57 #Hmarks #Hqin #Hqout #Hmv #_ #_ #Heq #Ht2 #Hind
59 cases (memb_append … membx) -membx #membx
60 [cases (memb_append … membx) -membx #membx
61 [@bit_not_grid @Hqin //
62 |cases (orb_true_l … membx) -membx #membx
64 |cases (memb_append … membx) -membx #membx
65 [@bit_not_grid @Hqout //
66 |cases (orb_true_l … membx) -membx #membx
68 |@bit_not_grid @Hmv //
73 |cases (orb_true_l … membx) -membx #membx
81 lemma no_marks_in_table: ∀n.∀l.table_TM n l → no_marks l.
83 [normalize #n #x #H destruct
84 |#m #t1 #t2 * #qin * #qout * #mv * * * * * *
85 #Hmarks #_ #_ #_ #_ #_ #_ #Ht2 #Hind
86 #x #Hx cases (memb_append … Hx) -Hx #Hx
88 |cases (orb_true_l … Hx) -Hx #Hx
98 l0 x* a l1 x0* a0 l2 ------> l0 x a* l1 x0 a0* l2
101 if current (* x *) = #
104 then move_right; ----
106 if current (* x0 *) = 0
107 then advance_mark ----
111 else x = 1 (* analogo *)
117 MARK NEXT TUPLE machine
118 (partially axiomatized)
120 marks the first character after the first bar (rightwards)
123 definition bar_or_grid ≝ λc:STape.is_bar (\fst c) ∨ is_grid (\fst c).
125 definition mark_next_tuple ≝
126 seq ? (adv_to_mark_r ? bar_or_grid)
127 (ifTM ? (test_char ? (λc:STape.is_bar (\fst c)))
128 (move_right_and_mark ?) (nop ?) 1).
130 definition R_mark_next_tuple ≝
133 (* c non può essere un separatore ... speriamo *)
134 t1 = midtape STape ls c (rs1@〈grid,false〉::rs2) →
135 no_marks rs1 → no_grids rs1 → bar_or_grid c = false →
136 (∃rs3,rs4,d,b.rs1 = rs3 @ 〈bar,false〉 :: rs4 ∧
138 Some ? 〈d,b〉 = option_hd ? (rs4@〈grid,false〉::rs2) ∧
139 t2 = midtape STape (〈bar,false〉::reverse ? rs3@c::ls) 〈d,true〉 (tail ? (rs4@〈grid,false〉::rs2)))
141 (no_bars rs1 ∧ t2 = midtape ? (reverse ? rs1@c::ls) 〈grid,false〉 rs2).
145 (∀x.memb A x l = true → f x = false) ∨
146 (∃l1,c,l2.f c = true ∧ l = l1@c::l2 ∧ ∀x.memb ? x l1 = true → f x = false).
148 [ % #x normalize #Hfalse *)
150 theorem sem_mark_next_tuple :
151 Realize ? mark_next_tuple R_mark_next_tuple.
153 lapply (sem_seq ? (adv_to_mark_r ? bar_or_grid)
154 (ifTM ? (test_char ? (λc:STape.is_bar (\fst c))) (move_right_and_mark ?) (nop ?) 1) ????)
155 [@sem_if [5: // |6: @sem_move_right_and_mark |7: // |*:skip]
157 |||#Hif cases (Hif intape) -Hif
158 #j * #outc * #Hloop * #ta * #Hleft #Hright
159 @(ex_intro ?? j) @ex_intro [|% [@Hloop] ]
161 #ls #c #rs1 #rs2 #Hrs #Hrs1 #Hrs1' #Hc
163 [ * #Hfalse >Hfalse in Hc; #Htf destruct (Htf)
164 | * #_ #Hta cases (tech_split STape (λc.is_bar (\fst c)) rs1)
165 [ #H1 lapply (Hta rs1 〈grid,false〉 rs2 (refl ??) ? ?)
166 [ * #x #b #Hx whd in ⊢ (??%?); >(Hrs1' … Hx) >(H1 … Hx) %
168 | -Hta #Hta cases Hright
169 [ * #tb * whd in ⊢ (%→?); #Hcurrent
170 @False_ind cases (Hcurrent 〈grid,false〉 ?)
171 [ normalize #Hfalse destruct (Hfalse)
173 | * #tb * whd in ⊢ (%→?); #Hcurrent
174 cases (Hcurrent 〈grid,false〉 ?)
175 [ #_ #Htb whd in ⊢ (%→?); #Houtc
178 | >Houtc >Htb >Hta % ]
182 | * #rs3 * #c0 * #rs4 * * #Hc0 #Hsplit #Hrs3
183 % @(ex_intro ?? rs3) @(ex_intro ?? rs4)
184 lapply (Hta rs3 c0 (rs4@〈grid,false〉::rs2) ???)
185 [ #x #Hrs3' whd in ⊢ (??%?); >Hsplit in Hrs1;>Hsplit in Hrs3;
186 #Hrs3 #Hrs1 >(Hrs1 …) [| @memb_append_l1 @Hrs3'|]
187 >(Hrs3 … Hrs3') @Hrs1' >Hsplit @memb_append_l1 //
188 | whd in ⊢ (??%?); >Hc0 %
189 | >Hsplit >associative_append % ] -Hta #Hta
191 [ * #tb * whd in ⊢ (%→?); #Hta'
194 [ #_ #Htb' >Htb' in Htb; #Htb
195 generalize in match Hsplit; -Hsplit
197 [ #Hta #Hsplit >(Htb … Hta)
198 >(?:c0 = 〈bar,false〉)
199 [ @(ex_intro ?? grid) @(ex_intro ?? false)
201 [(* Hsplit *) @daemon |(*Hrs3*) @daemon ] | % ] | % ]
202 | (* Hc0 *) @daemon ]
203 | #r5 #rs5 >(eq_pair_fst_snd … r5)
204 #Hta #Hsplit >(Htb … Hta)
205 >(?:c0 = 〈bar,false〉)
206 [ @(ex_intro ?? (\fst r5)) @(ex_intro ?? (\snd r5))
207 % [ % [ % [ (* Hc0, Hsplit *) @daemon | (*Hrs3*) @daemon ] | % ]
208 | % ] | (* Hc0 *) @daemon ] ] | >Hta % ]
209 | * #tb * whd in ⊢ (%→?); #Hta'
212 [ #Hfalse @False_ind >Hfalse in Hc0;
218 definition init_current ≝
219 seq ? (adv_to_mark_l ? (is_marked ?))
220 (seq ? (clear_mark ?)
221 (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
222 (seq ? (move_r ?) (mark ?)))).
224 definition R_init_current ≝ λt1,t2.
225 ∀l1,c,l2,b,l3,c1,rs,c0,b0. no_marks l1 → no_grids l2 → is_grid c = false →
226 Some ? 〈c0,b0〉 = option_hd ? (reverse ? (〈c,true〉::l2)) →
227 t1 = midtape STape (l1@〈c,true〉::l2@〈grid,b〉::l3) 〈c1,false〉 rs →
228 t2 = midtape STape (〈grid,b〉::l3) 〈c0,true〉
229 ((tail ? (reverse ? (l1@〈c,false〉::l2))@〈c1,false〉::rs)).
231 lemma sem_init_current : Realize ? init_current R_init_current.
233 cases (sem_seq ????? (sem_adv_to_mark_l ? (is_marked ?))
234 (sem_seq ????? (sem_clear_mark ?)
235 (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
236 (sem_seq ????? (sem_move_r ?) (sem_mark ?)))) intape)
237 #k * #outc * #Hloop #HR
238 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop]
239 cases HR -HR #ta * whd in ⊢ (%→?); #Hta
240 * #tb * whd in ⊢ (%→?); #Htb
241 * #tc * whd in ⊢ (%→?); #Htc
242 * #td * whd in ⊢ (%→%→?); #Htd #Houtc
243 #l1 #c #l2 #b #l3 #c1 #rs #c0 #b0 #Hl1 #Hl2 #Hc #Hc0 #Hintape
244 cases (Hta … Hintape) [ * #Hfalse normalize in Hfalse; destruct (Hfalse) ]
245 -Hta * #_ #Hta lapply (Hta l1 〈c,true〉 ? (refl ??) ??) [@Hl1|%]
246 -Hta #Hta lapply (Htb … Hta) -Htb #Htb cases (Htc … Htb) [ >Hc -Hc * #Hc destruct (Hc) ]
247 -Htc * #_ #Htc lapply (Htc … (refl ??) (refl ??) ?) [@Hl2]
248 -Htc #Htc lapply (Htd … Htc) -Htd
249 >reverse_append >reverse_cons
250 >reverse_cons in Hc0; cases (reverse … l2)
251 [ normalize in ⊢ (%→?); #Hc0 destruct (Hc0)
252 #Htd >(Houtc … Htd) %
253 | * #c2 #b2 #tl2 normalize in ⊢ (%→?);
254 #Hc0 #Htd >(Houtc … Htd)
255 whd in ⊢ (???%); destruct (Hc0)
256 >associative_append >associative_append %
260 definition match_tuple_step ≝
261 ifTM ? (test_char ? (λc:STape.¬ is_grid (\fst c)))
264 (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
266 (seq ? mark_next_tuple
267 (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
268 (mark ?) (seq ? (move_l ?) init_current) tc_true)) tc_true)))
271 definition R_match_tuple_step_true ≝ λt1,t2.
272 ∀ls,c,l1,l2,c1,l3,l4,rs,n.
273 is_bit c = true → only_bits l1 → no_marks l1 (* → no_grids l2 *) → is_bit c1 = true →
274 only_bits l3 → n = |l1| → |l1| = |l3| →
275 table_TM (S n) (l2@〈bar,false〉::〈c1,false〉::l3@〈comma,false〉::l4) →
276 t1 = midtape STape (〈grid,false〉::ls) 〈c,true〉
277 (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,true〉::l3@〈comma,false〉::l4@〈grid,false〉::rs) →
279 (〈c,false〉::l1 = 〈c1,false〉::l3 ∧
280 t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls) 〈grid,false〉
281 (l2@〈bar,false〉::〈c1,false〉::l3@〈comma,true〉::l4@〈grid,false〉::rs))
283 (* non facciamo match e marchiamo la prossima tupla *)
284 ((〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧
285 ∃c2,l5,l6,l7.l4 = l5@〈bar,false〉::〈c2,false〉::l6@〈comma,false〉::l7 ∧
286 (* condizioni su l5 l6 l7 *)
287 t2 = midtape STape (〈grid,false〉::ls) 〈c,true〉
288 (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,true〉::l3@〈comma,false〉::
289 l5@〈bar,false〉::〈c2,true〉::l6@〈comma,false〉::l7))
291 (* non facciamo match e non c'è una prossima tupla:
292 non specifichiamo condizioni sul nastro di output, perché
293 non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
294 (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧ no_bars l4 ∧ current ? t2 = Some ? 〈grid,true〉)).
296 definition R_match_tuple_step_false ≝ λt1,t2.
297 ∀ls,c,rs.t1 = midtape STape ls c rs → is_grid (\fst c) = true ∧ t2 = t1.
299 include alias "basics/logic.ma".
302 lemma eq_f4: ∀A1,A2,A3,A4,B.∀f:A1 → A2 →A3 →A4 →B.
303 ∀x1,x2,x3,x4,y1,y2,y3,y4. x1 = y1 → x2 = y2 →x3=y3 →x4 = y4 →
304 f x1 x2 x3 x4 = f y1 y2 y3 y4.
308 lemma some_option_hd: ∀A.∀l:list A.∀a.∃b.
309 Some ? b = option_hd ? (l@[a]) .
310 #A #l #a cases l normalize /2/
313 lemma sem_match_tuple_step:
314 accRealize ? match_tuple_step (inr … (inl … (inr … 0)))
315 R_match_tuple_step_true R_match_tuple_step_false.
316 @(acc_sem_if_app … (sem_test_char ? (λc:STape.¬ is_grid (\fst c))) …
317 (sem_seq … sem_compare
318 (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
320 (sem_seq … sem_mark_next_tuple
321 (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
322 (sem_mark ?) (sem_seq … (sem_move_l …) (sem_init_current …))))))
324 [(* is_grid: termination case *)
325 2:#t1 #t2 #t3 whd in ⊢ (%→?); #H #H1 whd #ls #c #rs #Ht1
326 cases (H c ?) [2: >Ht1 %] #Hgrid #Heq %
327 [@injective_notb @Hgrid | <Heq @H1]
328 |#tapea #tapeout #tapeb whd in ⊢ (%→?); #Htapea
329 * #tapec * #Hcompare #Hor
330 #ls #c #l1 #l2 #c1 #l3 #l4 #rs #n #Hc #Hl1bars #Hl1marks #Hc1 #Hl3 #eqn
331 #eqlen #Htable #Htapea1 cases (Htapea 〈c,true〉 ?) >Htapea1 [2:%]
332 #notgridc -Htapea -Htapea1 -tapea #Htapeb
333 cases (Hcompare … Htapeb) -Hcompare -Htapeb * #_ #_ #Hcompare
334 cases (Hcompare c c1 l1 l3 (l2@[〈bar,false〉]) (l4@〈grid,false〉::rs) eqlen Hl1bars Hl3 Hl1marks … (refl …) Hc ?)
336 [* #Htemp destruct (Htemp) #Htapec %1 % [%]
337 >Htapec in Hor; -Htapec *
338 [2: * #t3 * whd in ⊢ (%→?); #H @False_ind
339 cases (H … (refl …)) whd in ⊢ ((??%?)→?); #H destruct (H)
340 |* #taped * whd in ⊢ (%→?); #Htaped cases (Htaped ? (refl …)) -Htaped *
341 #Htaped whd in ⊢ (%→?); #Htapeout >Htapeout >Htaped >associative_append
344 |* #la * #c' * #d' * #lb * #lc * * * #H1 #H2 #H3 #Htapec
345 cut (〈c,false〉::l1 ≠ 〈c1,false〉::l3)
347 [@(not_to_not …H1) normalize #H destruct %
348 |#x #tl @not_to_not normalize #H destruct //
351 cut (is_bit d' = true)
353 [normalize in ⊢ (%→?); #H destruct //
354 |#x #tl #H @(Hl3 〈d',false〉)
355 normalize in H; destruct @memb_append_l2 @memb_hd
358 >Htapec in Hor; -Htapec *
359 [* #taped * whd in ⊢ (%→?); #H @False_ind
360 cases (H … (refl …)) >(bit_not_grid ? Hd') #Htemp destruct (Htemp)
361 |* #taped * whd in ⊢ (%→?); #H cases (H … (refl …)) -H #_
362 #Htaped * #tapee * whd in ⊢ (%→?); #Htapee
363 <(associative_append ? lc (〈comma,false〉::l4)) in Htaped; #Htaped
364 cases (Htapee … Htaped ???) -Htaped -Htapee
365 [* #rs3 * * (* we proceed by cases on rs4 *)
367 * #d * #b * * * #Heq1 #Hnobars
368 whd in ⊢ ((???%)→?); #Htemp destruct (Htemp)
370 [* #tapef * whd in ⊢ (%→?); >Htapee -Htapee #Htapef
371 cases (Htapef … (refl …)) -Htapef #_ #Htapef >Htapef -Htapef
372 whd in ⊢ (%→?); #H lapply (H … ???? (refl …)) #Htapeout
374 [% [@Hnoteq |@daemon]
377 |* #tapef * whd in ⊢ (%→?); >Htapee -Htapee #Htapef
378 cases (Htapef … (refl …)) whd in ⊢ ((??%?)→?); #Htemp destruct (Htemp)
381 * #d2 #b2 #rs3' * #d * #b * * * #Heq1 #Hnobars
382 cut (is_grid d2 = false) [@daemon (* no grids in table *)] #Hd2
383 whd in ⊢ ((???%)→?); #Htemp destruct (Htemp) #Htapee >Htapee -Htapee *
384 [* #tapef * whd in ⊢ (%→?); #Htapef
385 cases (Htapef … (refl …)) >Hd2 #Htemp destruct (Htemp)
386 |* #tapef * whd in ⊢ (%→?); #Htapef
387 cases (Htapef … (refl …)) #_ -Htapef #Htapef
388 * #tapeg >Htapef -Htapef * whd in ⊢ (%→?);
389 #H lapply (H … (refl …)) whd in ⊢ (???%→?); -H #Htapeg
390 >Htapeg -Htapeg whd in ⊢ (%→?); #Htapeout
391 %1 cases (some_option_hd ? (reverse ? (reverse ? la)) 〈c',true〉)
394 (Htapeout (reverse ? rs3 @〈d',false〉::reverse ? la@reverse ? (l2@[〈bar,false〉])@(〈grid,false〉::reverse ? lb))
395 c' (reverse ? la) false ls bar (〈d2,true〉::rs3'@〈grid,false〉::rs) c00 b00 ?????) -Htapeout
396 [whd in ⊢ (??(??%??)?); @eq_f3 [2:%|3: %]
398 generalize in match (〈c',true〉::reverse ? la@〈grid,false〉::ls); #l
399 whd in ⊢ (???(???%)); >associative_append >associative_append %
400 |>reverse_cons @Hoption
402 [normalize in ⊢ (%→?); #Htemp destruct (Htemp)
403 @injective_notb @notgridc
404 |#x #tl normalize in ⊢ (%→?); #Htemp destruct (Htemp)
405 @bit_not_grid @(Hl1bars 〈c',false〉) @memb_append_l2 @memb_hd
407 |cut (only_bits (la@(〈c',false〉::lb)))
408 [<H2 whd #c0 #Hmemb cases (orb_true_l … Hmemb)
409 [#eqc0 >(\P eqc0) @Hc |@Hl1bars]
410 |#Hl1' #x #Hx @bit_not_grid @Hl1'
411 @memb_append_l1 @daemon
418 |* #Hnobars #Htapee >Htapee -Htapee *
419 [whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
420 cases (Htapef … (refl …)) -Htapef #_ #Htapef >Htapef -Htapef
421 whd in ⊢ (%→?); #Htapeout %2
422 >(Htapeout … (refl …)) %
425 | whd #x #Hx @Hnobars @memb_append_l2 @memb_cons //
429 |whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
430 cases (Htapef … (refl …)) -Htapef
431 whd in ⊢ ((??%?)→?); #Htemp destruct (Htemp)
433 |@daemon (* no marks in table *)
434 |(* no grids in table *)
435 #x #Hx cases (memb_append … Hx)
436 [-Hx #Hx @bit_not_grid @Hl3 cases la in H3; normalize
437 [#H3 destruct (H3) @Hx | #y #tl #H3 destruct (H3)
438 @memb_append_l2 @memb_cons @Hx ]
439 |-Hx #Hx @(no_grids_in_table … Htable)
440 @memb_append_l2 @memb_cons @memb_cons @memb_append_l2 @Hx
442 |whd in ⊢ (??%?); >(bit_not_grid … Hd') >(bit_not_bar … Hd') %
445 |(* no marks in l3 *)
447 |(* no marks in l2@[〈bar,false〉] *) @daemon
448 |>associative_append %