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 definition tuple_TM : nat → list STape → Prop ≝
34 λn,t.∃qin,qout,mv,b1,b2.
35 only_bits qin ∧ only_bits qout ∧ only_bits mv ∧
36 |qin| = n ∧ |qout| = n (* ∧ |mv| = ? *) ∧
37 t = qin@〈comma,b1〉::qout@〈comma,b2〉::mv.
39 inductive table_TM : nat → list STape → Prop ≝
40 | ttm_nil : ∀n.table_TM n []
41 | ttm_cons : ∀n,t1,T,b.tuple_TM n t1 → table_TM n T → table_TM n (t1@〈bar,b〉::T).
43 axiom no_grids_in_table: ∀n.∀l.table_TM n l → no_grids l.
45 l0 x* a l1 x0* a0 l2 ------> l0 x a* l1 x0 a0* l2
48 if current (* x *) = #
53 if current (* x0 *) = 0
54 then advance_mark ----
58 else x = 1 (* analogo *)
64 MARK NEXT TUPLE machine
65 (partially axiomatized)
67 marks the first character after the first bar (rightwards)
70 definition bar_or_grid ≝ λc:STape.is_bar (\fst c) ∨ is_grid (\fst c).
72 definition mark_next_tuple ≝
73 seq ? (adv_to_mark_r ? bar_or_grid)
74 (ifTM ? (test_char ? (λc:STape.is_bar (\fst c)))
75 (move_right_and_mark ?) (nop ?) 1).
77 definition R_mark_next_tuple ≝
80 (* c non può essere un separatore ... speriamo *)
81 t1 = midtape STape ls c (rs1@〈grid,false〉::rs2) →
82 no_marks rs1 → no_grids rs1 → bar_or_grid c = false →
83 (∃rs3,rs4,d,b.rs1 = rs3 @ 〈bar,false〉 :: rs4 ∧
85 Some ? 〈d,b〉 = option_hd ? (rs4@〈grid,false〉::rs2) ∧
86 t2 = midtape STape (〈bar,false〉::reverse ? rs3@c::ls) 〈d,true〉 (tail ? (rs4@〈grid,false〉::rs2)))
88 (no_bars rs1 ∧ t2 = midtape ? (reverse ? rs1@c::ls) 〈grid,false〉 rs2).
92 (∀x.memb A x l = true → f x = false) ∨
93 (∃l1,c,l2.f c = true ∧ l = l1@c::l2 ∧ ∀x.memb ? x l1 = true → f x = false).
95 [ % #x normalize #Hfalse *)
97 theorem sem_mark_next_tuple :
98 Realize ? mark_next_tuple R_mark_next_tuple.
100 lapply (sem_seq ? (adv_to_mark_r ? bar_or_grid)
101 (ifTM ? (test_char ? (λc:STape.is_bar (\fst c))) (move_right_and_mark ?) (nop ?) 1) ????)
102 [@sem_if [5: // |6: @sem_move_right_and_mark |7: // |*:skip]
104 |||#Hif cases (Hif intape) -Hif
105 #j * #outc * #Hloop * #ta * #Hleft #Hright
106 @(ex_intro ?? j) @ex_intro [|% [@Hloop] ]
108 #ls #c #rs1 #rs2 #Hrs #Hrs1 #Hrs1' #Hc
110 [ * #Hfalse >Hfalse in Hc; #Htf destruct (Htf)
111 | * #_ #Hta cases (tech_split STape (λc.is_bar (\fst c)) rs1)
112 [ #H1 lapply (Hta rs1 〈grid,false〉 rs2 (refl ??) ? ?)
113 [ * #x #b #Hx whd in ⊢ (??%?); >(Hrs1' … Hx) >(H1 … Hx) %
115 | -Hta #Hta cases Hright
116 [ * #tb * whd in ⊢ (%→?); #Hcurrent
117 @False_ind cases (Hcurrent 〈grid,false〉 ?)
118 [ normalize #Hfalse destruct (Hfalse)
120 | * #tb * whd in ⊢ (%→?); #Hcurrent
121 cases (Hcurrent 〈grid,false〉 ?)
122 [ #_ #Htb whd in ⊢ (%→?); #Houtc
125 | >Houtc >Htb >Hta % ]
129 | * #rs3 * #c0 * #rs4 * * #Hc0 #Hsplit #Hrs3
130 % @(ex_intro ?? rs3) @(ex_intro ?? rs4)
131 lapply (Hta rs3 c0 (rs4@〈grid,false〉::rs2) ???)
132 [ #x #Hrs3' whd in ⊢ (??%?); >Hsplit in Hrs1;>Hsplit in Hrs3;
133 #Hrs3 #Hrs1 >(Hrs1 …) [| @memb_append_l1 @Hrs3'|]
134 >(Hrs3 … Hrs3') @Hrs1' >Hsplit @memb_append_l1 //
135 | whd in ⊢ (??%?); >Hc0 %
136 | >Hsplit >associative_append % ] -Hta #Hta
138 [ * #tb * whd in ⊢ (%→?); #Hta'
141 [ #_ #Htb' >Htb' in Htb; #Htb
142 generalize in match Hsplit; -Hsplit
144 [ #Hta #Hsplit >(Htb … Hta)
145 >(?:c0 = 〈bar,false〉)
146 [ @(ex_intro ?? grid) @(ex_intro ?? false)
148 [(* Hsplit *) @daemon |(*Hrs3*) @daemon ] | % ] | % ]
149 | (* Hc0 *) @daemon ]
150 | #r5 #rs5 >(eq_pair_fst_snd … r5)
151 #Hta #Hsplit >(Htb … Hta)
152 >(?:c0 = 〈bar,false〉)
153 [ @(ex_intro ?? (\fst r5)) @(ex_intro ?? (\snd r5))
154 % [ % [ % [ (* Hc0, Hsplit *) @daemon | (*Hrs3*) @daemon ] | % ]
155 | % ] | (* Hc0 *) @daemon ] ] | >Hta % ]
156 | * #tb * whd in ⊢ (%→?); #Hta'
159 [ #Hfalse @False_ind >Hfalse in Hc0;
165 definition init_current ≝
166 seq ? (adv_to_mark_l ? (is_marked ?))
167 (seq ? (clear_mark ?)
168 (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
169 (seq ? (move_r ?) (mark ?)))).
171 definition R_init_current ≝ λt1,t2.
172 ∀l1,c,l2,b,l3,c1,rs,c0,b0. no_marks l1 → no_grids l2 → is_grid c = false →
173 Some ? 〈c0,b0〉 = option_hd ? (reverse ? (〈c,true〉::l2)) →
174 t1 = midtape STape (l1@〈c,true〉::l2@〈grid,b〉::l3) 〈c1,false〉 rs →
175 t2 = midtape STape (〈grid,b〉::l3) 〈c0,true〉
176 ((tail ? (reverse ? (l1@〈c,false〉::l2))@〈c1,false〉::rs)).
178 lemma sem_init_current : Realize ? init_current R_init_current.
180 cases (sem_seq ????? (sem_adv_to_mark_l ? (is_marked ?))
181 (sem_seq ????? (sem_clear_mark ?)
182 (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
183 (sem_seq ????? (sem_move_r ?) (sem_mark ?)))) intape)
184 #k * #outc * #Hloop #HR
185 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop]
186 cases HR -HR #ta * whd in ⊢ (%→?); #Hta
187 * #tb * whd in ⊢ (%→?); #Htb
188 * #tc * whd in ⊢ (%→?); #Htc
189 * #td * whd in ⊢ (%→%→?); #Htd #Houtc
190 #l1 #c #l2 #b #l3 #c1 #rs #c0 #b0 #Hl1 #Hl2 #Hc #Hc0 #Hintape
191 cases (Hta … Hintape) [ * #Hfalse normalize in Hfalse; destruct (Hfalse) ]
192 -Hta * #_ #Hta lapply (Hta l1 〈c,true〉 ? (refl ??) ??) [@Hl1|%]
193 -Hta #Hta lapply (Htb … Hta) -Htb #Htb cases (Htc … Htb) [ >Hc -Hc * #Hc destruct (Hc) ]
194 -Htc * #_ #Htc lapply (Htc … (refl ??) (refl ??) ?) [@Hl2]
195 -Htc #Htc lapply (Htd … Htc) -Htd
196 >reverse_append >reverse_cons
197 >reverse_cons in Hc0; cases (reverse … l2)
198 [ normalize in ⊢ (%→?); #Hc0 destruct (Hc0)
199 #Htd >(Houtc … Htd) %
200 | * #c2 #b2 #tl2 normalize in ⊢ (%→?);
201 #Hc0 #Htd >(Houtc … Htd)
202 whd in ⊢ (???%); destruct (Hc0)
203 >associative_append >associative_append %
207 definition match_tuple_step ≝
208 ifTM ? (test_char ? (λc:STape.¬ is_grid (\fst c)))
211 (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
213 (seq ? mark_next_tuple
214 (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
215 (mark ?) (seq ? (move_l ?) init_current) tc_true)) tc_true)))
218 definition R_match_tuple_step_true ≝ λt1,t2.
219 ∀ls,c,l1,l2,c1,l3,l4,rs,n.
220 is_bit c = true → only_bits l1 → no_grids l2 → is_bit c1 = true →
221 only_bits l3 → n = |l1| → |l1| = |l3| →
222 table_TM (S n) (〈c1,true〉::l3@〈comma,false〉::l4) →
223 t1 = midtape STape (〈grid,false〉::ls) 〈c,true〉
224 (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,true〉::l3@〈comma,false〉::l4@〈grid,false〉::rs) →
226 (〈c,false〉::l1 = 〈c1,false〉::l3 ∧
227 t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls) 〈grid,false〉
228 (l2@〈bar,false〉::〈c1,false〉::l3@〈comma,true〉::l4@〈grid,false〉::rs))
230 (* non facciamo match e marchiamo la prossima tupla *)
231 ((〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧
232 ∃c2,l5,l6,l7.l4 = l5@〈bar,false〉::〈c2,false〉::l6@〈comma,false〉::l7 ∧
233 (* condizioni su l5 l6 l7 *)
234 t2 = midtape STape (〈grid,false〉::ls) 〈c,true〉
235 (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,true〉::l3@〈comma,false〉::
236 l5@〈bar,false〉::〈c2,true〉::l6@〈comma,false〉::l7))
238 (* non facciamo match e non c'è una prossima tupla:
239 non specifichiamo condizioni sul nastro di output, perché
240 non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
241 (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧ no_bars l4 ∧ current ? t2 = Some ? 〈grid,true〉)).
243 definition R_match_tuple_step_false ≝ λt1,t2.
244 ∀ls,c,rs.t1 = midtape STape ls c rs → is_grid (\fst c) = true ∧ t2 = t1.
246 include alias "basics/logic.ma".
249 lemma eq_f4: ∀A1,A2,A3,A4,B.∀f:A1 → A2 →A3 →A4 →B.
250 ∀x1,x2,x3,x4,y1,y2,y3,y4. x1 = y1 → x2 = y2 →x3=y3 →x4 = y4 →
251 f x1 x2 x3 x4 = f y1 y2 y3 y4.
255 lemma some_option_hd: ∀A.∀l:list A.∀a.∃b.
256 Some ? b = option_hd ? (l@[a]) .
257 #A #l #a cases l normalize /2/
260 lemma bit_not_grid: ∀d. is_bit d = true → is_grid d = false.
261 * // normalize #H destruct
264 lemma bit_not_bar: ∀d. is_bit d = true → is_bar d = false.
265 * // normalize #H destruct
268 lemma sem_match_tuple_step:
269 accRealize ? match_tuple_step (inr … (inl … (inr … 0)))
270 R_match_tuple_step_true R_match_tuple_step_false.
271 @(acc_sem_if_app … (sem_test_char ? (λc:STape.¬ is_grid (\fst c))) …
272 (sem_seq … sem_compare
273 (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
275 (sem_seq … sem_mark_next_tuple
276 (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
277 (sem_mark ?) (sem_seq … (sem_move_l …) (sem_init_current …))))))
279 [(* is_grid: termination case *)
280 2:#t1 #t2 #t3 whd in ⊢ (%→?); #H #H1 whd #ls #c #rs #Ht1
281 cases (H c ?) [2: >Ht1 %] #Hgrid #Heq %
282 [@injective_notb @Hgrid | <Heq @H1]
283 |#tapea #tapeout #tapeb whd in ⊢ (%→?); #Htapea
284 * #tapec * #Hcompare #Hor
285 #ls #c #l1 #l2 #c1 #l3 #l4 #rs #n #Hc #Hl1 #Hl2 #Hc1 #Hl3 #eqn
286 #eqlen #Htable #Htapea1 cases (Htapea 〈c,true〉 ?) >Htapea1 [2:%]
287 #notgridc -Htapea -Htapea1 -tapea #Htapeb
288 cases (Hcompare … Htapeb) -Hcompare -Htapeb * #_ #_ #Hcompare
289 cases (Hcompare c c1 l1 l3 (l2@[〈bar,false〉]) (l4@〈grid,false〉::rs) eqlen Hl1 … (refl …) Hc ?)
291 [* #Htemp destruct (Htemp) #Htapec %1 % [%]
292 >Htapec in Hor; -Htapec *
293 [2: * #t3 * whd in ⊢ (%→?); #H @False_ind
294 cases (H … (refl …)) whd in ⊢ ((??%?)→?); #H destruct (H)
295 |* #taped * whd in ⊢ (%→?); #Htaped cases (Htaped ? (refl …)) -Htaped *
296 #Htaped whd in ⊢ (%→?); #Htapeout >Htapeout >Htaped >associative_append
299 |* #la * #c' * #d' * #lb * #lc * * * #H1 #H2 #H3 #Htapec
300 cut (〈c,false〉::l1 ≠ 〈c1,false〉::l3)
302 [@(not_to_not …H1) normalize #H destruct %
303 |#x #tl @not_to_not normalize #H destruct //
306 cut (is_bit d' = true)
308 [normalize in ⊢ (%→?); #H destruct //
309 |#x #tl #H @(Hl3 〈d',false〉)
310 normalize in H; destruct @memb_append_l2 @memb_hd
313 >Htapec in Hor; -Htapec *
314 [* #taped * whd in ⊢ (%→?); #H @False_ind
315 cases (H … (refl …)) >(bit_not_grid ? Hd') #Htemp destruct (Htemp)
316 |* #taped * whd in ⊢ (%→?); #H cases (H … (refl …)) -H #_
317 #Htaped * #tapee * whd in ⊢ (%→?); #Htapee
318 <(associative_append ? lc (〈comma,false〉::l4)) in Htaped; #Htaped
319 cases (Htapee … Htaped ???) -Htaped -Htapee
320 [* #rs3 * * (* we proceed by cases on rs4 *)
322 * #d * #b * * * #Heq1 #Hnobars
323 whd in ⊢ ((???%)→?); #Htemp destruct (Htemp)
325 [* #tapef * whd in ⊢ (%→?); >Htapee -Htapee #Htapef
326 cases (Htapef … (refl …)) -Htapef #_ #Htapef >Htapef -Htapef
327 whd in ⊢ (%→?); #H lapply (H … ???? (refl …)) #Htapeout
329 [% [@Hnoteq |@daemon]
332 |* #tapef * whd in ⊢ (%→?); >Htapee -Htapee #Htapef
333 cases (Htapef … (refl …)) whd in ⊢ ((??%?)→?); #Htemp destruct (Htemp)
336 * #d2 #b2 #rs3' * #d * #b * * * #Heq1 #Hnobars
337 cut (is_grid d2 = false) [@daemon (* no grids in table *)] #Hd2
338 whd in ⊢ ((???%)→?); #Htemp destruct (Htemp) #Htapee >Htapee -Htapee *
339 [* #tapef * whd in ⊢ (%→?); #Htapef
340 cases (Htapef … (refl …)) >Hd2 #Htemp destruct (Htemp)
341 |* #tapef * whd in ⊢ (%→?); #Htapef
342 cases (Htapef … (refl …)) #_ -Htapef #Htapef
343 * #tapeg >Htapef -Htapef * whd in ⊢ (%→?);
344 #H lapply (H … (refl …)) whd in ⊢ (???%→?); -H #Htapeg
345 >Htapeg -Htapeg whd in ⊢ (%→?); #Htapeout
346 %1 cases (some_option_hd ? (reverse ? (reverse ? la)) 〈c',true〉)
349 (Htapeout (reverse ? rs3 @〈d',false〉::reverse ? la@reverse ? (l2@[〈bar,false〉])@(〈grid,false〉::reverse ? lb))
350 c' (reverse ? la) false ls bar (〈d2,true〉::rs3'@〈grid,false〉::rs) c00 b00 ?????) -Htapeout
351 [whd in ⊢ (??(??%??)?); @eq_f3 [2:%|3: %]
353 generalize in match (〈c',true〉::reverse ? la@〈grid,false〉::ls); #l
354 whd in ⊢ (???(???%)); >associative_append >associative_append %
355 |>reverse_cons @Hoption
357 [normalize in ⊢ (%→?); #Htemp destruct (Htemp)
358 @injective_notb @notgridc
359 |#x #tl normalize in ⊢ (%→?); #Htemp destruct (Htemp)
360 @bit_not_grid @(Hl1 〈c',false〉) @memb_append_l2 @memb_hd
362 |cut (only_bits (la@(〈c',false〉::lb)))
363 [<H2 whd #c0 #Hmemb cases (orb_true_l … Hmemb)
364 [#eqc0 >(\P eqc0) @Hc |@Hl1]
365 |#Hl1' #x #Hx @bit_not_grid @Hl1'
366 @memb_append_l1 @daemon
373 |* #Hnobars #Htapee >Htapee -Htapee *
374 [whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
375 cases (Htapef … (refl …)) -Htapef #_ #Htapef >Htapef -Htapef
376 whd in ⊢ (%→?); #Htapeout %2
377 >(Htapeout … (refl …)) %
380 | whd #x #Hx @Hnobars @memb_append_l2 @memb_cons //
384 |whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
385 cases (Htapef … (refl …)) -Htapef
386 whd in ⊢ ((??%?)→?); #Htemp destruct (Htemp)
388 |@daemon (* no marks in table *)
389 |(* no grids in table *)
390 #x #Hx cases (memb_append … Hx)
391 [-Hx #Hx @bit_not_grid @Hl3 cases la in H3; normalize
392 [#H3 destruct (H3) @Hx | #y #tl #H3 destruct (H3)
393 @memb_append_l2 @memb_cons @Hx ]
394 |-Hx #Hx @(no_grids_in_table … Htable)
395 @memb_cons @memb_append_l2 @Hx
397 |whd in ⊢ (??%?); >(bit_not_grid … Hd') >(bit_not_bar … Hd') %
404 |>associative_append %