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/tuples.ma".
19 definition write_states ≝ initN 2.
21 definition write ≝ λalpha,c.
22 mk_TM alpha write_states
25 [ O ⇒ 〈1,Some ? 〈c,N〉〉
29 definition R_write ≝ λalpha,c,t1,t2.
30 ∀ls,x,rs.t1 = midtape alpha ls x rs → t2 = midtape alpha ls c rs.
32 axiom sem_write : ∀alpha,c.Realize ? (write alpha c) (R_write alpha c).
34 definition copy_step_subcase ≝
35 λalpha,c,elseM.ifTM ? (test_char ? (λx.x == 〈c,true〉))
36 (seq (FinProd alpha FinBool) (adv_mark_r …)
38 (seq ? (adv_to_mark_l … (is_marked alpha))
39 (seq ? (write ? 〈c,false〉)
42 (seq ? (move_r …) (adv_to_mark_r … (is_marked alpha)))))))))
45 definition R_copy_step_subcase ≝
46 λalpha,c,RelseM,t1,t2.
47 ∀ls,x,rs.t1 = midtape (FinProd … alpha FinBool) ls 〈x,true〉 rs →
49 ∀a,l1,x0,a0,l2,l3. (∀c.memb ? c l1 = true → is_marked ? c = false) →
50 ls = l1@〈a0,false〉::〈x0,true〉::l2 →
52 t2 = midtape ? (〈x,false〉::l1@〈a0,true〉::〈x,false〉::l2) 〈a,true〉 l3) ∨
53 (x ≠ c ∧ RelseM t1 t2).
55 axiom sem_copy_step_subcase :
56 ∀alpha,c,elseM,RelseM.
57 Realize ? (copy_step_subcase alpha c elseM) (R_copy_step_subcase alpha c RelseM).
69 else if current = 1,tt
81 definition copy_step ≝
82 ifTM ? (test_char STape (λc.is_bit (\fst c)))
83 (single_finalTM ? (copy_step_subcase FSUnialpha (bit false)
84 (copy_step_subcase FSUnialpha (bit true) (nop ?))))
88 definition R_copy_step_true ≝
90 ∀ls,c,rs.t1 = midtape (FinProd … FSUnialpha FinBool) ls 〈c,true〉 rs →
92 (∀a,l1,c0,a0,l2,l3. (∀y.memb ? y l1 = true → is_marked ? y = false) →
93 ls = l1@〈a0,false〉::〈c0,true〉::l2 →
95 t2 = midtape STape (〈bit x,false〉::l1@〈a0,true〉::〈bit x,false〉::l2) 〈a,true〉 l3).
97 definition R_copy_step_false ≝
99 ∀ls,c,rs.t1 = midtape (FinProd … FSUnialpha FinBool) ls c rs →
100 is_bit (\fst c) = false ∧ t2 = t1.
102 axiom sem_comp_step :
103 accRealize ? copy_step (inr … (inl … (inr … 0))) R_copy_step_true R_copy_step_false.
105 definition copy ≝ whileTM ? copy_step (inr … (inl … (inr … 0))).
107 definition R_copy ≝ λt1,t2.
108 ∀ls,c,rs.t1 = midtape ? ls 〈c,true〉 rs →
110 〈c,false〉::rs = l1@〈d,false〉::l2 → only_bits l1 → is_bit d = false →
111 ls = l3@l4@〈c0,true〉::l5 → |l4| = |l1@[〈d,false〉]|
115 axiom no_grids_in_table: ∀n.∀l.table_TM n l → no_grids l.
117 l0 x* a l1 x0* a0 l2 ------> l0 x a* l1 x0 a0* l2
120 if current (* x *) = #
123 then move_right; ----
125 if current (* x0 *) = 0
126 then advance_mark ----
130 else x = 1 (* analogo *)
136 MARK NEXT TUPLE machine
137 (partially axiomatized)
139 marks the first character after the first bar (rightwards)
142 definition bar_or_grid ≝ λc:STape.is_bar (\fst c) ∨ is_grid (\fst c).
144 definition mark_next_tuple ≝
145 seq ? (adv_to_mark_r ? bar_or_grid)
146 (ifTM ? (test_char ? (λc:STape.is_bar (\fst c)))
147 (move_right_and_mark ?) (nop ?) 1).
149 definition R_mark_next_tuple ≝
152 (* c non può essere un separatore ... speriamo *)
153 t1 = midtape STape ls c (rs1@〈grid,false〉::rs2) →
154 no_marks rs1 → no_grids rs1 → bar_or_grid c = false →
155 (∃rs3,rs4,d,b.rs1 = rs3 @ 〈bar,false〉 :: rs4 ∧
157 Some ? 〈d,b〉 = option_hd ? (rs4@〈grid,false〉::rs2) ∧
158 t2 = midtape STape (〈bar,false〉::reverse ? rs3@c::ls) 〈d,true〉 (tail ? (rs4@〈grid,false〉::rs2)))
160 (no_bars rs1 ∧ t2 = midtape ? (reverse ? rs1@c::ls) 〈grid,false〉 rs2).
164 (∀x.memb A x l = true → f x = false) ∨
165 (∃l1,c,l2.f c = true ∧ l = l1@c::l2 ∧ ∀x.memb ? x l1 = true → f x = false).
167 [ % #x normalize #Hfalse *)
169 theorem sem_mark_next_tuple :
170 Realize ? mark_next_tuple R_mark_next_tuple.
172 lapply (sem_seq ? (adv_to_mark_r ? bar_or_grid)
173 (ifTM ? (test_char ? (λc:STape.is_bar (\fst c))) (move_right_and_mark ?) (nop ?) 1) ????)
174 [@sem_if [5: // |6: @sem_move_right_and_mark |7: // |*:skip]
176 |||#Hif cases (Hif intape) -Hif
177 #j * #outc * #Hloop * #ta * #Hleft #Hright
178 @(ex_intro ?? j) @ex_intro [|% [@Hloop] ]
180 #ls #c #rs1 #rs2 #Hrs #Hrs1 #Hrs1' #Hc
182 [ * #Hfalse >Hfalse in Hc; #Htf destruct (Htf)
183 | * #_ #Hta cases (tech_split STape (λc.is_bar (\fst c)) rs1)
184 [ #H1 lapply (Hta rs1 〈grid,false〉 rs2 (refl ??) ? ?)
185 [ * #x #b #Hx whd in ⊢ (??%?); >(Hrs1' … Hx) >(H1 … Hx) %
187 | -Hta #Hta cases Hright
188 [ * #tb * whd in ⊢ (%→?); #Hcurrent
189 @False_ind cases (Hcurrent 〈grid,false〉 ?)
190 [ normalize #Hfalse destruct (Hfalse)
192 | * #tb * whd in ⊢ (%→?); #Hcurrent
193 cases (Hcurrent 〈grid,false〉 ?)
194 [ #_ #Htb whd in ⊢ (%→?); #Houtc
197 | >Houtc >Htb >Hta % ]
201 | * #rs3 * #c0 * #rs4 * * #Hc0 #Hsplit #Hrs3
202 % @(ex_intro ?? rs3) @(ex_intro ?? rs4)
203 lapply (Hta rs3 c0 (rs4@〈grid,false〉::rs2) ???)
204 [ #x #Hrs3' whd in ⊢ (??%?); >Hsplit in Hrs1;>Hsplit in Hrs3;
205 #Hrs3 #Hrs1 >(Hrs1 …) [| @memb_append_l1 @Hrs3'|]
206 >(Hrs3 … Hrs3') @Hrs1' >Hsplit @memb_append_l1 //
207 | whd in ⊢ (??%?); >Hc0 %
208 | >Hsplit >associative_append % ] -Hta #Hta
210 [ * #tb * whd in ⊢ (%→?); #Hta'
213 [ #_ #Htb' >Htb' in Htb; #Htb
214 generalize in match Hsplit; -Hsplit
216 [ #Hta #Hsplit >(Htb … Hta)
217 >(?:c0 = 〈bar,false〉)
218 [ @(ex_intro ?? grid) @(ex_intro ?? false)
220 [(* Hsplit *) @daemon |(*Hrs3*) @daemon ] | % ] | % ]
221 | (* Hc0 *) @daemon ]
222 | #r5 #rs5 >(eq_pair_fst_snd … r5)
223 #Hta #Hsplit >(Htb … Hta)
224 >(?:c0 = 〈bar,false〉)
225 [ @(ex_intro ?? (\fst r5)) @(ex_intro ?? (\snd r5))
226 % [ % [ % [ (* Hc0, Hsplit *) @daemon | (*Hrs3*) @daemon ] | % ]
227 | % ] | (* Hc0 *) @daemon ] ] | >Hta % ]
228 | * #tb * whd in ⊢ (%→?); #Hta'
231 [ #Hfalse @False_ind >Hfalse in Hc0;
237 definition init_current ≝
238 seq ? (adv_to_mark_l ? (is_marked ?))
239 (seq ? (clear_mark ?)
240 (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
241 (seq ? (move_r ?) (mark ?)))).
243 definition R_init_current ≝ λt1,t2.
244 ∀l1,c,l2,b,l3,c1,rs,c0,b0. no_marks l1 → no_grids l2 → is_grid c = false →
245 Some ? 〈c0,b0〉 = option_hd ? (reverse ? (〈c,true〉::l2)) →
246 t1 = midtape STape (l1@〈c,true〉::l2@〈grid,b〉::l3) 〈c1,false〉 rs →
247 t2 = midtape STape (〈grid,b〉::l3) 〈c0,true〉
248 ((tail ? (reverse ? (l1@〈c,false〉::l2))@〈c1,false〉::rs)).
250 lemma sem_init_current : Realize ? init_current R_init_current.
252 cases (sem_seq ????? (sem_adv_to_mark_l ? (is_marked ?))
253 (sem_seq ????? (sem_clear_mark ?)
254 (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
255 (sem_seq ????? (sem_move_r ?) (sem_mark ?)))) intape)
256 #k * #outc * #Hloop #HR
257 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop]
258 cases HR -HR #ta * whd in ⊢ (%→?); #Hta
259 * #tb * whd in ⊢ (%→?); #Htb
260 * #tc * whd in ⊢ (%→?); #Htc
261 * #td * whd in ⊢ (%→%→?); #Htd #Houtc
262 #l1 #c #l2 #b #l3 #c1 #rs #c0 #b0 #Hl1 #Hl2 #Hc #Hc0 #Hintape
263 cases (Hta … Hintape) [ * #Hfalse normalize in Hfalse; destruct (Hfalse) ]
264 -Hta * #_ #Hta lapply (Hta l1 〈c,true〉 ? (refl ??) ??) [@Hl1|%]
265 -Hta #Hta lapply (Htb … Hta) -Htb #Htb cases (Htc … Htb) [ >Hc -Hc * #Hc destruct (Hc) ]
266 -Htc * #_ #Htc lapply (Htc … (refl ??) (refl ??) ?) [@Hl2]
267 -Htc #Htc lapply (Htd … Htc) -Htd
268 >reverse_append >reverse_cons
269 >reverse_cons in Hc0; cases (reverse … l2)
270 [ normalize in ⊢ (%→?); #Hc0 destruct (Hc0)
271 #Htd >(Houtc … Htd) %
272 | * #c2 #b2 #tl2 normalize in ⊢ (%→?);
273 #Hc0 #Htd >(Houtc … Htd)
274 whd in ⊢ (???%); destruct (Hc0)
275 >associative_append >associative_append %
279 definition match_tuple_step ≝
280 ifTM ? (test_char ? (λc:STape.¬ is_grid (\fst c)))
283 (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
285 (seq ? mark_next_tuple
286 (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
287 (mark ?) (seq ? (move_l ?) init_current) tc_true)) tc_true)))
290 definition R_match_tuple_step_true ≝ λt1,t2.
291 ∀ls,c,l1,l2,c1,l3,l4,rs,n.
292 is_bit c = true → only_bits l1 → no_grids l2 → is_bit c1 = true →
293 only_bits l3 → n = |l1| → |l1| = |l3| →
294 table_TM (S n) (〈c1,true〉::l3@〈comma,false〉::l4) →
295 t1 = midtape STape (〈grid,false〉::ls) 〈c,true〉
296 (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,true〉::l3@〈comma,false〉::l4@〈grid,false〉::rs) →
298 (〈c,false〉::l1 = 〈c1,false〉::l3 ∧
299 t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls) 〈grid,false〉
300 (l2@〈bar,false〉::〈c1,false〉::l3@〈comma,true〉::l4@〈grid,false〉::rs))
302 (* non facciamo match e marchiamo la prossima tupla *)
303 ((〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧
304 ∃c2,l5,l6,l7.l4 = l5@〈bar,false〉::〈c2,false〉::l6@〈comma,false〉::l7 ∧
305 (* condizioni su l5 l6 l7 *)
306 t2 = midtape STape (〈grid,false〉::ls) 〈c,true〉
307 (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,true〉::l3@〈comma,false〉::
308 l5@〈bar,false〉::〈c2,true〉::l6@〈comma,false〉::l7))
310 (* non facciamo match e non c'è una prossima tupla:
311 non specifichiamo condizioni sul nastro di output, perché
312 non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
313 (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧ no_bars l4 ∧ current ? t2 = Some ? 〈grid,true〉)).
315 definition R_match_tuple_step_false ≝ λt1,t2.
316 ∀ls,c,rs.t1 = midtape STape ls c rs → is_grid (\fst c) = true ∧ t2 = t1.
318 include alias "basics/logic.ma".
321 lemma eq_f4: ∀A1,A2,A3,A4,B.∀f:A1 → A2 →A3 →A4 →B.
322 ∀x1,x2,x3,x4,y1,y2,y3,y4. x1 = y1 → x2 = y2 →x3=y3 →x4 = y4 →
323 f x1 x2 x3 x4 = f y1 y2 y3 y4.
327 lemma some_option_hd: ∀A.∀l:list A.∀a.∃b.
328 Some ? b = option_hd ? (l@[a]) .
329 #A #l #a cases l normalize /2/
332 lemma bit_not_grid: ∀d. is_bit d = true → is_grid d = false.
333 * // normalize #H destruct
336 lemma bit_not_bar: ∀d. is_bit d = true → is_bar d = false.
337 * // normalize #H destruct
340 axiom sem_match_tuple_step:
341 accRealize ? match_tuple_step (inr … (inl … (inr … 0)))
342 R_match_tuple_step_true R_match_tuple_step_false.
343 (* @(acc_sem_if_app … (sem_test_char ? (λc:STape.¬ is_grid (\fst c))) …
344 (sem_seq … sem_compare
345 (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
347 (sem_seq … sem_mark_next_tuple
348 (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
349 (sem_mark ?) (sem_seq … (sem_move_l …) (sem_init_current …))))))
351 [(* is_grid: termination case *)
352 2:#t1 #t2 #t3 whd in ⊢ (%→?); #H #H1 whd #ls #c #rs #Ht1
353 cases (H c ?) [2: >Ht1 %] #Hgrid #Heq %
354 [@injective_notb @Hgrid | <Heq @H1]
355 |#tapea #tapeout #tapeb whd in ⊢ (%→?); #Htapea
356 * #tapec * #Hcompare #Hor
357 #ls #c #l1 #l2 #c1 #l3 #l4 #rs #n #Hc #Hl1 #Hl2 #Hc1 #Hl3 #eqn
358 #eqlen #Htable #Htapea1 cases (Htapea 〈c,true〉 ?) >Htapea1 [2:%]
359 #notgridc -Htapea -Htapea1 -tapea #Htapeb
360 cases (Hcompare … Htapeb) -Hcompare -Htapeb * #_ #_ #Hcompare
361 cases (Hcompare c c1 l1 l3 (l2@[〈bar,false〉]) (l4@〈grid,false〉::rs) eqlen … (refl …) Hc ?)
363 [* #Htemp destruct (Htemp) #Htapec %1 % [%]
364 >Htapec in Hor; -Htapec *
365 [2: * #t3 * whd in ⊢ (%→?); #H @False_ind
366 cases (H … (refl …)) whd in ⊢ ((??%?)→?); #H destruct (H)
367 |* #taped * whd in ⊢ (%→?); #Htaped cases (Htaped ? (refl …)) -Htaped *
368 #Htaped whd in ⊢ (%→?); #Htapeout >Htapeout >Htaped >associative_append
371 |* #la * #c' * #d' * #lb * #lc * * * #H1 #H2 #H3 #Htapec
372 cut (〈c,false〉::l1 ≠ 〈c1,false〉::l3)
374 [@(not_to_not …H1) normalize #H destruct %
375 |#x #tl @not_to_not normalize #H destruct //
378 cut (is_bit d' = true)
380 [normalize in ⊢ (%→?); #H destruct //
381 |#x #tl #H @(Hl3 〈d',false〉)
382 normalize in H; destruct @memb_append_l2 @memb_hd
385 >Htapec in Hor; -Htapec *
386 [* #taped * whd in ⊢ (%→?); #H @False_ind
387 cases (H … (refl …)) >Hd' #Htemp destruct (Htemp)
388 |* #taped * whd in ⊢ (%→?); #H cases (H … (refl …)) -H #_
389 #Htaped * #tapee * whd in ⊢ (%→?); #Htapee
390 <(associative_append ? lc (〈comma,false〉::l4)) in Htaped; #Htaped
391 lapply (Htapee … Htaped ???) -Htaped -Htapee
392 [whd in ⊢ (??%?); >(bit_not_grid … Hd') >(bit_not_bar … Hd') %
393 |#x #Hx cases (memb_append … Hx)
394 [-Hx #Hx @bit_not_grid @Hl3 cases la in H3; normalize
395 [#H3 destruct (H3) @Hx | #y #tl #H3 destruct (H3)
396 @memb_append_l2 @memb_cons @Hx ]
397 |-Hx #Hx @(no_grids_in_table … Htable)
398 @memb_cons @memb_append_l2 @Hx
402 [* #rs3 * * (* we proceed by cases on rs4 *)
403 [* #d * #b * * * #Heq1 #Hnobars
404 whd in ⊢ ((???%)→?); #Htemp destruct (Htemp)
406 [* #tapef * whd in ⊢ (%→?); >Htapee -Htapee #Htapef
407 cases (Htapef … (refl …)) -Htapef #_ #Htapef >Htapef -Htapef
408 whd in ⊢ (%→?); #H lapply (H … ???? (refl …)) #Htapeout
413 |* #tapef * whd in ⊢ (%→?); >Htapee -Htapee #Htapef
414 cases (Htapef … (refl …)) whd in ⊢ ((??%?)→?); #Htemp destruct (Htemp)
416 |* #d2 #b2 #rs3' * #d * #b * * * #Heq1 #Hnobars
417 cut (is_grid d2 = false) [@daemon (* ??? *)] #Hd2
418 whd in ⊢ ((???%)→?); #Htemp destruct (Htemp) #Htapee >Htapee -Htapee *
419 [* #tapef * whd in ⊢ (%→?); #Htapef
420 cases (Htapef … (refl …)) >Hd2 #Htemp destruct (Htemp)
421 |* #tapef * whd in ⊢ (%→?); #Htapef
422 cases (Htapef … (refl …)) #_ -Htapef #Htapef
423 * #tapeg >Htapef -Htapef * whd in ⊢ (%→?);
424 #H lapply (H … (refl …)) whd in ⊢ (???%→?); -H #Htapeg
425 >Htapeg -Htapeg whd in ⊢ (%→?); #Htapeout
426 %1 cases (some_option_hd ? (reverse ? (reverse ? la)) 〈c',true〉)
429 (Htapeout (reverse ? rs3 @〈d',false〉::reverse ? la@reverse ? (l2@[〈bar,false〉])@(〈grid,false〉::reverse ? lb))
430 c' (reverse ? la) false ls bar (〈d2,true〉::rs3'@〈grid,false〉::rs) c00 b00 ?????) -Htapeout
431 [whd in ⊢ (??(??%??)?); @eq_f3 [2:%|3: %]
433 generalize in match (〈c',true〉::reverse ? la@〈grid,false〉::ls); #l
434 whd in ⊢ (???(???%)); >associative_append >associative_append
444 |* #Hnobars #Htapee >Htapee -Htapee *
445 [whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
446 cases (Htapef … (refl …)) -Htapef #_ #Htapef >Htapef -Htapef
447 whd in ⊢ (%→?); #Htapeout %2
448 >(Htapeout … (refl …)) %
455 |whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
456 cases (Htapef … (refl …)) -Htapef
457 whd in ⊢ ((??%?)→?); #Htemp destruct (Htemp)
466 ????? (refl …) Hc ?) -Hcompare
468 is_bit c = true → only_bits l1 → no_grids l2 → is_bit c1 = true →
469 only_bits l3 → n = |l2| → |l2| = |l3| →
470 table_TM (S n) (〈c1,true〉::l3@〈comma,false〉::l4) →#x
474 (acc_sem_if … (sem_test_char ? (λc:STape.¬ is_grid (\fst c)))
475 (sem_seq … sem_compare
476 (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
478 (sem_seq … sem_mark_next_tuple
479 (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
480 (sem_mark ?) (sem_seq … (sem_move_l …) (sem_init_current …))))))
482 #k * #outc * * #Hloop #H1 #H2
483 @(ex_intro ?? k) @(ex_intro ?? outc) %
484 [ % [@Hloop ] ] -Hloop
490 scrolls through the tuples in the transition table until one matching the
491 current configuration is found
494 definition match_tuple ≝ whileTM ? match_tuple_step (inr … (inl … (inr … 0))).
496 definition R_match_tuple ≝ λt1,t2.
498 is_bit c = true → only_bits l1 → is_bit c1 = true → n = |l1| →
499 table_TM (S n) (〈c1,true〉::l2) →
500 t1 = midtape STape (〈grid,false〉::ls) 〈c,true〉
501 (l1@〈grid,false〉::〈c1,true〉::l2@〈grid,false〉::rs) →
504 〈c1,false〉::l2 = l3@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv@l4 ∧
505 t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls) 〈grid,false〉
506 (l3@〈c,false〉::l1@〈comma,true〉::newc@〈comma,false〉::mv@l4@〈grid,false〉::rs))
508 (* non facciamo match su nessuna tupla;
509 non specifichiamo condizioni sul nastro di output, perché
510 non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
511 (current ? t2 = Some ? 〈grid,true〉 ∧
513 〈c1,false〉::l2 ≠ l3@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv@l4).