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_____________________________________________________________*)
12 include "basics/vectors.ma".
13 (* include "basics/relations.ma". *)
16 record tape (sig:FinSet): Type[0] ≝
17 { left : list (option sig);
18 right: list (option sig)
22 inductive tape (sig:FinSet) : Type[0] ≝
24 | leftof : sig → list sig → tape sig
25 | rightof : sig → list sig → tape sig
26 | midtape : list sig → sig → list sig → tape sig.
29 λsig.λt:tape sig.match t with
33 | midtape l _ _ ⇒ l ].
36 λsig.λt:tape sig.match t with
40 | midtape _ _ r ⇒ r ].
44 λsig.λt:tape sig.match t with
45 [ midtape _ c _ ⇒ Some ? c
49 ∀sig:FinSet.list sig → option sig → list sig → tape sig ≝
50 λsig,lt,c,rt.match c with
51 [ Some c' ⇒ midtape sig lt c' rt
52 | None ⇒ match lt with
55 | cons r0 rs0 ⇒ leftof ? r0 rs0 ]
56 | cons l0 ls0 ⇒ rightof ? l0 ls0 ] ].
58 inductive move : Type[0] ≝
64 (* We do not distinuish an input tape *)
66 record TM (sig:FinSet): Type[1] ≝
68 trans : states × (option sig) → states × (option (sig × move));
73 record config (sig,states:FinSet): Type[0] ≝
78 (* definition option_hd ≝ λA.λl:list (option A).
85 (*definition tape_write ≝ λsig.λt:tape sig.λs:sig.
86 <left ? t) s (right ? t).
88 | Some s' ⇒ midtape ? (left ? t) s' (right ? t) ].*)
90 definition tape_move_left ≝ λsig:FinSet.λlt:list sig.λc:sig.λrt:list sig.
92 [ nil ⇒ leftof sig c rt
93 | cons c0 lt0 ⇒ midtape sig lt0 c0 (c::rt) ].
95 definition tape_move_right ≝ λsig:FinSet.λlt:list sig.λc:sig.λrt:list sig.
97 [ nil ⇒ rightof sig c lt
98 | cons c0 rt0 ⇒ midtape sig (c::lt) c0 rt0 ].
100 definition tape_move ≝ λsig.λt: tape sig.λm:option (sig × move).
106 [ R ⇒ tape_move_right ? (left ? t) s (right ? t)
107 | L ⇒ tape_move_left ? (left ? t) s (right ? t)
108 | N ⇒ midtape ? (left ? t) s (right ? t)
113 (None,a::b::rs) → None::b::rs
114 (Some a,[]) → [Some a]
115 (Some a,b::rs) → Some a::rs
118 definition option_cons ≝ λA.λa:option A.λl.
120 [ None ⇒ match l with
125 (* definition tape_update := λsig.λt: tape sig.λs:option sig.
130 | Some a ⇒ [Some ? a] ]
131 | cons b rs ⇒ match s with
132 [ None ⇒ match rs with
134 | cons _ _ ⇒ None ?::rs ]
135 | Some a ⇒ Some ? a::rs ] ]
136 in mk_tape ? (left ? t) newright. *)
138 definition tape_move ≝ λsig.λt:tape sig.λm:option sig × move.
139 let 〈s,m1〉 ≝ m in match m1 with
140 [ R ⇒ mk_tape sig (option_cons ? s (left ? t)) (tail ? (right ? t))
141 | L ⇒ mk_tape sig (tail ? (left ? t))
142 (option_cons ? (option_hd ? (left ? t))
143 (option_cons ? s (tail ? (right ? t))))
144 | N ⇒ mk_tape sig (left ? t) (option_cons ? s (tail ? (right ? t)))
148 definition step ≝ λsig.λM:TM sig.λc:config sig (states sig M).
149 let current_char ≝ current ? (ctape ?? c) in
150 let 〈news,mv〉 ≝ trans sig M 〈cstate ?? c,current_char〉 in
151 mk_config ?? news (tape_move sig (ctape ?? c) mv).
153 let rec loop (A:Type[0]) n (f:A→A) p a on n ≝
156 | S m ⇒ if p a then (Some ? a) else loop A m f p (f a)
159 lemma loop_incr : ∀A,f,p,k1,k2,a1,a2.
160 loop A k1 f p a1 = Some ? a2 →
161 loop A (k2+k1) f p a1 = Some ? a2.
162 #A #f #p #k1 #k2 #a1 #a2 generalize in match a1; elim k1
163 [normalize #a0 #Hfalse destruct
164 |#k1' #IH #a0 <plus_n_Sm whd in ⊢ (??%? → ??%?);
165 cases (true_or_false (p a0)) #Hpa0 >Hpa0 whd in ⊢ (??%? → ??%?); // @IH
169 lemma loop_merge : ∀A,f,p,q.(∀b. p b = false → q b = false) →
171 loop A k1 f p a1 = Some ? a2 →
172 f a2 = a3 → q a2 = false →
173 loop A k2 f q a3 = Some ? a4 →
174 loop A (k1+k2) f q a1 = Some ? a4.
175 #Sig #f #p #q #Hpq #k1 elim k1
176 [normalize #k2 #a1 #a2 #a3 #a4 #H destruct
177 |#k1' #Hind #k2 #a1 #a2 #a3 #a4 normalize in ⊢ (%→?);
178 cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
179 [#eqa1a2 destruct #eqa2a3 #Hqa2 #H
180 whd in ⊢ (??(??%???)?); >plus_n_Sm @loop_incr
181 whd in ⊢ (??%?); >Hqa2 >eqa2a3 @H
182 |normalize >(Hpq … pa1) normalize
183 #H1 #H2 #H3 @(Hind … H2) //
188 lemma loop_split : ∀A,f,p,q.(∀b. q b = true → p b = true) →
190 loop A k f q a1 = Some ? a2 →
192 loop A k1 f p a1 = Some ? a3 ∧
193 loop A (S(k-k1)) f q a3 = Some ? a2.
194 #A #f #p #q #Hpq #k elim k
195 [#a1 #a2 normalize #Heq destruct
196 |#i #Hind #a1 #a2 normalize
197 cases (true_or_false (q a1)) #Hqa1 >Hqa1 normalize
199 @(ex_intro … 1) @(ex_intro … a2) %
200 [normalize >(Hpq …Hqa1) // |>Hqa1 //]
201 |#Hloop cases (true_or_false (p a1)) #Hpa1
202 [@(ex_intro … 1) @(ex_intro … a1) %
203 [normalize >Hpa1 // |>Hqa1 <Hloop normalize //]
204 |cases (Hind …Hloop) #k2 * #a3 * #Hloop1 #Hloop2
205 @(ex_intro … (S k2)) @(ex_intro … a3) %
206 [normalize >Hpa1 normalize // | @Hloop2 ]
213 lemma loop_split : ∀A,f,p,q.(∀b. p b = false → q b = false) →
215 loop A k1 f p a1 = Some ? a2 →
216 loop A k2 f q a2 = Some ? a3 →
217 loop A (k1+k2) f q a1 = Some ? a3.
218 #Sig #f #p #q #Hpq #k1 elim k1
219 [normalize #k2 #a1 #a2 #a3 #H destruct
220 |#k1' #Hind #k2 #a1 #a2 #a3 normalize in ⊢ (%→?→?);
221 cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
222 [#eqa1a2 destruct #H @loop_incr //
223 |normalize >(Hpq … pa1) normalize
224 #H1 #H2 @(Hind … H2) //
230 definition initc ≝ λsig.λM:TM sig.λt.
231 mk_config sig (states sig M) (start sig M) t.
233 definition Realize ≝ λsig.λM:TM sig.λR:relation (tape sig).
235 loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc ∧
238 definition WRealize ≝ λsig.λM:TM sig.λR:relation (tape sig).
240 loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc →
243 lemma loop_eq : ∀sig,f,q,i,j,a,x,y.
244 loop sig i f q a = Some ? x → loop sig j f q a = Some ? y → x = y.
245 #sig #f #q #i #j @(nat_elim2 … i j)
246 [ #n #a #x #y normalize #Hfalse destruct (Hfalse)
247 | #n #a #x #y #H1 normalize #Hfalse destruct (Hfalse)
248 | #n1 #n2 #IH #a #x #y normalize cases (q a) normalize
254 theorem Realize_to_WRealize : ∀sig,M,R.Realize sig M R → WRealize sig M R.
255 #sig #M #R #H1 #inc #i #outc #Hloop
256 cases (H1 inc) #k * #outc1 * #Hloop1 #HR
257 >(loop_eq … Hloop Hloop1) //
260 definition accRealize ≝ λsig.λM:TM sig.λacc:states sig M.λRtrue,Rfalse:relation (tape sig).
262 loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc ∧
263 (cstate ?? outc = acc → Rtrue t (ctape ?? outc)) ∧
264 (cstate ?? outc ≠ acc → Rfalse t (ctape ?? outc)).
268 definition seq_trans ≝ λsig. λM1,M2 : TM sig.
272 if halt sig M1 s1 then 〈inr … (start sig M2), None ?〉
274 let 〈news1,m〉 ≝ trans sig M1 〈s1,a〉 in
277 let 〈news2,m〉 ≝ trans sig M2 〈s2,a〉 in
281 definition seq ≝ λsig. λM1,M2 : TM sig.
283 (FinSum (states sig M1) (states sig M2))
284 (seq_trans sig M1 M2)
285 (inl … (start sig M1))
287 [ inl _ ⇒ false | inr s2 ⇒ halt sig M2 s2]).
289 definition Rcomp ≝ λA.λR1,R2:relation A.λa1,a2.
290 ∃am.R1 a1 am ∧ R2 am a2.
293 definition injectRl ≝ λsig.λM1.λM2.λR.
295 inl … (cstate sig M1 c11) = cstate sig (seq sig M1 M2) c1 ∧
296 inl … (cstate sig M1 c12) = cstate sig (seq sig M1 M2) c2 ∧
297 ctape sig M1 c11 = ctape sig (seq sig M1 M2) c1 ∧
298 ctape sig M1 c12 = ctape sig (seq sig M1 M2) c2 ∧
301 definition injectRr ≝ λsig.λM1.λM2.λR.
303 inr … (cstate sig M2 c21) = cstate sig (seq sig M1 M2) c1 ∧
304 inr … (cstate sig M2 c22) = cstate sig (seq sig M1 M2) c2 ∧
305 ctape sig M2 c21 = ctape sig (seq sig M1 M2) c1 ∧
306 ctape sig M2 c22 = ctape sig (seq sig M1 M2) c2 ∧
309 definition Rlink ≝ λsig.λM1,M2.λc1,c2.
310 ctape sig (seq sig M1 M2) c1 = ctape sig (seq sig M1 M2) c2 ∧
311 cstate sig (seq sig M1 M2) c1 = inl … (halt sig M1) ∧
312 cstate sig (seq sig M1 M2) c2 = inr … (start sig M2). *)
314 interpretation "relation composition" 'compose R1 R2 = (Rcomp ? R1 R2).
316 definition lift_confL ≝
317 λsig,S1,S2,c.match c with
318 [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inl … s) t ].
320 definition lift_confR ≝
321 λsig,S1,S2,c.match c with
322 [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inr … s) t ].
324 definition halt_liftL ≝
325 λS1,S2,halt.λs:FinSum S1 S2.
328 | inr _ ⇒ true ]. (* should be vacuous in all cases we use halt_liftL *)
330 definition halt_liftR ≝
331 λS1,S2,halt.λs:FinSum S1 S2.
334 | inr s2 ⇒ halt s2 ].
336 lemma p_halt_liftL : ∀sig,S1,S2,halt,c.
337 halt (cstate sig S1 c) =
338 halt_liftL S1 S2 halt (cstate … (lift_confL … c)).
339 #sig #S1 #S2 #halt #c cases c #s #t %
342 lemma trans_liftL : ∀sig,M1,M2,s,a,news,move.
343 halt ? M1 s = false →
344 trans sig M1 〈s,a〉 = 〈news,move〉 →
345 trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inl … news,move〉.
346 #sig (*#M1*) * #Q1 #T1 #init1 #halt1 #M2 #s #a #news #move
347 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
350 lemma trans_liftR : ∀sig,M1,M2,s,a,news,move.
351 halt ? M2 s = false →
352 trans sig M2 〈s,a〉 = 〈news,move〉 →
353 trans sig (seq sig M1 M2) 〈inr … s,a〉 = 〈inr … news,move〉.
354 #sig #M1 * #Q2 #T2 #init2 #halt2 #s #a #news #move
355 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
360 cstate sig M c1 = cstate sig M c2 →
361 ctape sig M c1 = ctape sig M c2 → c1 = c2.
362 #sig #M1 * #s1 #t1 * #s2 #t2 //
365 lemma step_lift_confR : ∀sig,M1,M2,c0.
366 halt ? M2 (cstate ?? c0) = false →
367 step sig (seq sig M1 M2) (lift_confR sig (states ? M1) (states ? M2) c0) =
368 lift_confR sig (states ? M1) (states ? M2) (step sig M2 c0).
369 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s #t
370 lapply (refl ? (trans ?? 〈s,current sig t〉))
371 cases (trans ?? 〈s,current sig t〉) in ⊢ (???% → %);
374 | 2,3: #s1 #l1 #Heq #Hhalt
375 |#ls #s1 #rs #Heq #Hhalt ]
376 whd in ⊢ (???(????%)); >Heq
378 whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
379 >(trans_liftR … Heq) //
382 lemma step_lift_confL : ∀sig,M1,M2,c0.
383 halt ? M1 (cstate ?? c0) = false →
384 step sig (seq sig M1 M2) (lift_confL sig (states ? M1) (states ? M2) c0) =
385 lift_confL sig ?? (step sig M1 c0).
386 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s #t
387 lapply (refl ? (trans ?? 〈s,current sig t〉))
388 cases (trans ?? 〈s,current sig t〉) in ⊢ (???% → %);
391 | 2,3: #s1 #l1 #Heq #Hhalt
392 |#ls #s1 #rs #Heq #Hhalt ]
393 whd in ⊢ (???(????%)); >Heq
395 whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
396 >(trans_liftL … Heq) //
399 lemma loop_lift : ∀A,B,k,lift,f,g,h,hlift,c1,c2.
400 (∀x.hlift (lift x) = h x) →
401 (∀x.h x = false → lift (f x) = g (lift x)) →
402 loop A k f h c1 = Some ? c2 →
403 loop B k g hlift (lift c1) = Some ? (lift … c2).
404 #A #B #k #lift #f #g #h #hlift #c1 #c2 #Hfg #Hhlift
405 generalize in match c1; elim k
406 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
407 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
408 cases (true_or_false (h c0)) #Hc0 >Hfg >Hc0
409 [ normalize #Heq destruct (Heq) %
410 | normalize <Hhlift // @IH ]
414 lemma loop_liftL : ∀sig,k,M1,M2,c1,c2.
415 loop ? k (step sig M1) (λc.halt sig M1 (cstate ?? c)) c1 = Some ? c2 →
416 loop ? k (step sig (seq sig M1 M2))
417 (λc.halt_liftL ?? (halt sig M1) (cstate ?? c)) (lift_confL … c1) =
418 Some ? (lift_confL … c2).
419 #sig #k #M1 #M2 #c1 #c2 generalize in match c1;
421 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
422 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
423 cases (true_or_false (halt ?? (cstate sig (states ? M1) c0))) #Hc0 >Hc0
424 [ >(?: halt_liftL ?? (halt sig M1) (cstate sig ? (lift_confL … c0)) = true)
425 [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
427 | >(?: halt_liftL ?? (halt sig M1) (cstate ?? (lift_confL … c0)) = false)
428 [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
433 lemma loop_liftR : ∀sig,k,M1,M2,c1,c2.
434 loop ? k (step sig M2) (λc.halt sig M2 (cstate ?? c)) c1 = Some ? c2 →
435 loop ? k (step sig (seq sig M1 M2))
436 (λc.halt sig (seq sig M1 M2) (cstate ?? c)) (lift_confR … c1) =
437 Some ? (lift_confR … c2).
438 #sig #k #M1 #M2 #c1 #c2 generalize in match c1;
440 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
441 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
442 cases (true_or_false (halt ?? (cstate sig ? c0))) #Hc0 >Hc0
443 [ >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = true)
444 [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
446 | >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = false)
447 [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
456 ∀A,k,f,p,a,b.loop A k f p a = Some ? b → p b = true.
458 [#a #b normalize #Hfalse destruct
459 |#k0 #IH #a #b whd in ⊢ (??%? → ?); cases (true_or_false (p a)) #Hpa
460 [ >Hpa normalize #H1 destruct //
466 lemma trans_liftL_true : ∀sig,M1,M2,s,a.
468 trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inr … (start ? M2),None ?〉.
470 #Hhalt whd in ⊢ (??%?); >Hhalt %
473 lemma eq_ctape_lift_conf_L : ∀sig,S1,S2,outc.
474 ctape sig (FinSum S1 S2) (lift_confL … outc) = ctape … outc.
475 #sig #S1 #S2 #outc cases outc #s #t %
478 lemma eq_ctape_lift_conf_R : ∀sig,S1,S2,outc.
479 ctape sig (FinSum S1 S2) (lift_confR … outc) = ctape … outc.
480 #sig #S1 #S2 #outc cases outc #s #t %
483 theorem sem_seq: ∀sig,M1,M2,R1,R2.
484 Realize sig M1 R1 → Realize sig M2 R2 →
485 Realize sig (seq sig M1 M2) (R1 ∘ R2).
486 #sig #M1 #M2 #R1 #R2 #HR1 #HR2 #t
487 cases (HR1 t) #k1 * #outc1 * #Hloop1 #HM1
488 cases (HR2 (ctape sig (states ? M1) outc1)) #k2 * #outc2 * #Hloop2 #HM2
489 @(ex_intro … (k1+k2)) @(ex_intro … (lift_confR … outc2))
491 [@(loop_merge ???????????
492 (loop_lift ??? (lift_confL sig (states sig M1) (states sig M2))
493 (step sig M1) (step sig (seq sig M1 M2))
494 (λc.halt sig M1 (cstate … c))
495 (λc.halt_liftL ?? (halt sig M1) (cstate … c)) … Hloop1))
497 [ #sl #tl whd in ⊢ (??%? → ?); #Hl %
498 | #sr #tr whd in ⊢ (??%? → ?); #Hr destruct (Hr) ]
499 || #c0 #Hhalt <step_lift_confL //
501 |6:cases outc1 #s1 #t1 %
502 |7:@(loop_lift … (initc ?? (ctape … outc1)) … Hloop2)
504 | #c0 #Hhalt <step_lift_confR // ]
505 |whd in ⊢ (??(???%)?);whd in ⊢ (??%?);
506 generalize in match Hloop1; cases outc1 #sc1 #tc1 #Hloop10
507 >(trans_liftL_true sig M1 M2 ??)
508 [ whd in ⊢ (??%?); whd in ⊢ (???%);
509 @config_eq whd in ⊢ (???%); //
510 | @(loop_Some ?????? Hloop10) ]
512 | @(ex_intro … (ctape ? (FinSum (states ? M1) (states ? M2)) (lift_confL … outc1)))
513 % // >eq_ctape_lift_conf_L >eq_ctape_lift_conf_R //