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
48 inductive move : Type[0] ≝
54 (* We do not distinuish an input tape *)
56 record TM (sig:FinSet): Type[1] ≝
58 trans : states × (option sig) → states × (option (sig × move));
63 record config (sig,states:FinSet): Type[0] ≝
68 (* definition option_hd ≝ λA.λl:list (option A).
75 (*definition tape_write ≝ λsig.λt:tape sig.λs:sig.
76 <left ? t) s (right ? t).
78 | Some s' ⇒ midtape ? (left ? t) s' (right ? t) ].*)
80 definition tape_move_left ≝ λsig:FinSet.λlt:list sig.λc:sig.λrt:list sig.
82 [ nil ⇒ leftof sig c rt
83 | cons c0 lt0 ⇒ midtape sig lt0 c0 (c::rt) ].
85 definition tape_move_right ≝ λsig:FinSet.λlt:list sig.λc:sig.λrt:list sig.
87 [ nil ⇒ rightof sig c lt
88 | cons c0 rt0 ⇒ midtape sig (c::lt) c0 rt0 ].
90 definition tape_move ≝ λsig.λt: tape sig.λm:option (sig × move).
96 [ R ⇒ tape_move_right ? (left ? t) s (right ? t)
97 | L ⇒ tape_move_left ? (left ? t) s (right ? t)
98 | N ⇒ midtape ? (left ? t) s (right ? t)
103 (None,a::b::rs) → None::b::rs
104 (Some a,[]) → [Some a]
105 (Some a,b::rs) → Some a::rs
108 definition option_cons ≝ λA.λa:option A.λl.
110 [ None ⇒ match l with
115 (* definition tape_update := λsig.λt: tape sig.λs:option sig.
120 | Some a ⇒ [Some ? a] ]
121 | cons b rs ⇒ match s with
122 [ None ⇒ match rs with
124 | cons _ _ ⇒ None ?::rs ]
125 | Some a ⇒ Some ? a::rs ] ]
126 in mk_tape ? (left ? t) newright. *)
128 definition tape_move ≝ λsig.λt:tape sig.λm:option sig × move.
129 let 〈s,m1〉 ≝ m in match m1 with
130 [ R ⇒ mk_tape sig (option_cons ? s (left ? t)) (tail ? (right ? t))
131 | L ⇒ mk_tape sig (tail ? (left ? t))
132 (option_cons ? (option_hd ? (left ? t))
133 (option_cons ? s (tail ? (right ? t))))
134 | N ⇒ mk_tape sig (left ? t) (option_cons ? s (tail ? (right ? t)))
138 definition step ≝ λsig.λM:TM sig.λc:config sig (states sig M).
139 let current_char ≝ current ? (ctape ?? c) in
140 let 〈news,mv〉 ≝ trans sig M 〈cstate ?? c,current_char〉 in
141 mk_config ?? news (tape_move sig (ctape ?? c) mv).
143 let rec loop (A:Type[0]) n (f:A→A) p a on n ≝
146 | S m ⇒ if p a then (Some ? a) else loop A m f p (f a)
149 lemma loop_incr : ∀A,f,p,k1,k2,a1,a2.
150 loop A k1 f p a1 = Some ? a2 →
151 loop A (k2+k1) f p a1 = Some ? a2.
152 #A #f #p #k1 #k2 #a1 #a2 generalize in match a1; elim k1
153 [normalize #a0 #Hfalse destruct
154 |#k1' #IH #a0 <plus_n_Sm whd in ⊢ (??%? → ??%?);
155 cases (true_or_false (p a0)) #Hpa0 >Hpa0 whd in ⊢ (??%? → ??%?); // @IH
159 lemma loop_merge : ∀A,f,p,q.(∀b. p b = false → q b = false) →
161 loop A k1 f p a1 = Some ? a2 →
162 f a2 = a3 → q a2 = false →
163 loop A k2 f q a3 = Some ? a4 →
164 loop A (k1+k2) f q a1 = Some ? a4.
165 #Sig #f #p #q #Hpq #k1 elim k1
166 [normalize #k2 #a1 #a2 #a3 #a4 #H destruct
167 |#k1' #Hind #k2 #a1 #a2 #a3 #a4 normalize in ⊢ (%→?);
168 cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
169 [#eqa1a2 destruct #eqa2a3 #Hqa2 #H
170 whd in ⊢ (??(??%???)?); >plus_n_Sm @loop_incr
171 whd in ⊢ (??%?); >Hqa2 >eqa2a3 @H
172 |normalize >(Hpq … pa1) normalize
173 #H1 #H2 #H3 @(Hind … H2) //
178 lemma loop_split : ∀A,f,p,q.(∀b. q b = true → p b = true) →
180 loop A k f q a1 = Some ? a2 →
182 loop A k1 f p a1 = Some ? a3 ∧
183 loop A (S(k-k1)) f q a3 = Some ? a2.
184 #A #f #p #q #Hpq #k elim k
185 [#a1 #a2 normalize #Heq destruct
186 |#i #Hind #a1 #a2 normalize
187 cases (true_or_false (q a1)) #Hqa1 >Hqa1 normalize
189 @(ex_intro … 1) @(ex_intro … a2) %
190 [normalize >(Hpq …Hqa1) // |>Hqa1 //]
191 |#Hloop cases (true_or_false (p a1)) #Hpa1
192 [@(ex_intro … 1) @(ex_intro … a1) %
193 [normalize >Hpa1 // |>Hqa1 <Hloop normalize //]
194 |cases (Hind …Hloop) #k2 * #a3 * #Hloop1 #Hloop2
195 @(ex_intro … (S k2)) @(ex_intro … a3) %
196 [normalize >Hpa1 normalize // | @Hloop2 ]
203 lemma loop_split : ∀A,f,p,q.(∀b. p b = false → q b = false) →
205 loop A k1 f p a1 = Some ? a2 →
206 loop A k2 f q a2 = Some ? a3 →
207 loop A (k1+k2) f q a1 = Some ? a3.
208 #Sig #f #p #q #Hpq #k1 elim k1
209 [normalize #k2 #a1 #a2 #a3 #H destruct
210 |#k1' #Hind #k2 #a1 #a2 #a3 normalize in ⊢ (%→?→?);
211 cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
212 [#eqa1a2 destruct #H @loop_incr //
213 |normalize >(Hpq … pa1) normalize
214 #H1 #H2 @(Hind … H2) //
220 definition initc ≝ λsig.λM:TM sig.λt.
221 mk_config sig (states sig M) (start sig M) t.
223 definition Realize ≝ λsig.λM:TM sig.λR:relation (tape sig).
225 loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc ∧
228 definition WRealize ≝ λsig.λM:TM sig.λR:relation (tape sig).
230 loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc →
233 lemma loop_eq : ∀sig,f,q,i,j,a,x,y.
234 loop sig i f q a = Some ? x → loop sig j f q a = Some ? y → x = y.
235 #sig #f #q #i #j @(nat_elim2 … i j)
236 [ #n #a #x #y normalize #Hfalse destruct (Hfalse)
237 | #n #a #x #y #H1 normalize #Hfalse destruct (Hfalse)
238 | #n1 #n2 #IH #a #x #y normalize cases (q a) normalize
244 theorem Realize_to_WRealize : ∀sig,M,R.Realize sig M R → WRealize sig M R.
245 #sig #M #R #H1 #inc #i #outc #Hloop
246 cases (H1 inc) #k * #outc1 * #Hloop1 #HR
247 >(loop_eq … Hloop Hloop1) //
250 definition accRealize ≝ λsig.λM:TM sig.λacc:states sig M.λRtrue,Rfalse:relation (tape sig).
252 loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc ∧
253 (cstate ?? outc = acc → Rtrue t (ctape ?? outc)) ∧
254 (cstate ?? outc ≠ acc → Rfalse t (ctape ?? outc)).
258 definition seq_trans ≝ λsig. λM1,M2 : TM sig.
262 if halt sig M1 s1 then 〈inr … (start sig M2), None ?〉
264 let 〈news1,m〉 ≝ trans sig M1 〈s1,a〉 in
267 let 〈news2,m〉 ≝ trans sig M2 〈s2,a〉 in
271 definition seq ≝ λsig. λM1,M2 : TM sig.
273 (FinSum (states sig M1) (states sig M2))
274 (seq_trans sig M1 M2)
275 (inl … (start sig M1))
277 [ inl _ ⇒ false | inr s2 ⇒ halt sig M2 s2]).
279 definition Rcomp ≝ λA.λR1,R2:relation A.λa1,a2.
280 ∃am.R1 a1 am ∧ R2 am a2.
283 definition injectRl ≝ λsig.λM1.λM2.λR.
285 inl … (cstate sig M1 c11) = cstate sig (seq sig M1 M2) c1 ∧
286 inl … (cstate sig M1 c12) = cstate sig (seq sig M1 M2) c2 ∧
287 ctape sig M1 c11 = ctape sig (seq sig M1 M2) c1 ∧
288 ctape sig M1 c12 = ctape sig (seq sig M1 M2) c2 ∧
291 definition injectRr ≝ λsig.λM1.λM2.λR.
293 inr … (cstate sig M2 c21) = cstate sig (seq sig M1 M2) c1 ∧
294 inr … (cstate sig M2 c22) = cstate sig (seq sig M1 M2) c2 ∧
295 ctape sig M2 c21 = ctape sig (seq sig M1 M2) c1 ∧
296 ctape sig M2 c22 = ctape sig (seq sig M1 M2) c2 ∧
299 definition Rlink ≝ λsig.λM1,M2.λc1,c2.
300 ctape sig (seq sig M1 M2) c1 = ctape sig (seq sig M1 M2) c2 ∧
301 cstate sig (seq sig M1 M2) c1 = inl … (halt sig M1) ∧
302 cstate sig (seq sig M1 M2) c2 = inr … (start sig M2). *)
304 interpretation "relation composition" 'compose R1 R2 = (Rcomp ? R1 R2).
306 definition lift_confL ≝
307 λsig,S1,S2,c.match c with
308 [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inl … s) t ].
310 definition lift_confR ≝
311 λsig,S1,S2,c.match c with
312 [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inr … s) t ].
314 definition halt_liftL ≝
315 λS1,S2,halt.λs:FinSum S1 S2.
318 | inr _ ⇒ true ]. (* should be vacuous in all cases we use halt_liftL *)
320 definition halt_liftR ≝
321 λS1,S2,halt.λs:FinSum S1 S2.
324 | inr s2 ⇒ halt s2 ].
326 lemma p_halt_liftL : ∀sig,S1,S2,halt,c.
327 halt (cstate sig S1 c) =
328 halt_liftL S1 S2 halt (cstate … (lift_confL … c)).
329 #sig #S1 #S2 #halt #c cases c #s #t %
332 lemma trans_liftL : ∀sig,M1,M2,s,a,news,move.
333 halt ? M1 s = false →
334 trans sig M1 〈s,a〉 = 〈news,move〉 →
335 trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inl … news,move〉.
336 #sig (*#M1*) * #Q1 #T1 #init1 #halt1 #M2 #s #a #news #move
337 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
340 lemma trans_liftR : ∀sig,M1,M2,s,a,news,move.
341 halt ? M2 s = false →
342 trans sig M2 〈s,a〉 = 〈news,move〉 →
343 trans sig (seq sig M1 M2) 〈inr … s,a〉 = 〈inr … news,move〉.
344 #sig #M1 * #Q2 #T2 #init2 #halt2 #s #a #news #move
345 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
350 cstate sig M c1 = cstate sig M c2 →
351 ctape sig M c1 = ctape sig M c2 → c1 = c2.
352 #sig #M1 * #s1 #t1 * #s2 #t2 //
355 lemma step_lift_confR : ∀sig,M1,M2,c0.
356 halt ? M2 (cstate ?? c0) = false →
357 step sig (seq sig M1 M2) (lift_confR sig (states ? M1) (states ? M2) c0) =
358 lift_confR sig (states ? M1) (states ? M2) (step sig M2 c0).
359 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s #t
360 lapply (refl ? (trans ?? 〈s,current sig t〉))
361 cases (trans ?? 〈s,current sig t〉) in ⊢ (???% → %);
364 | 2,3: #s1 #l1 #Heq #Hhalt
365 |#ls #s1 #rs #Heq #Hhalt ]
366 whd in ⊢ (???(????%)); >Heq
368 whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
369 >(trans_liftR … Heq) //
372 lemma step_lift_confL : ∀sig,M1,M2,c0.
373 halt ? M1 (cstate ?? c0) = false →
374 step sig (seq sig M1 M2) (lift_confL sig (states ? M1) (states ? M2) c0) =
375 lift_confL sig ?? (step sig M1 c0).
376 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s #t
377 lapply (refl ? (trans ?? 〈s,current sig t〉))
378 cases (trans ?? 〈s,current sig t〉) in ⊢ (???% → %);
381 | 2,3: #s1 #l1 #Heq #Hhalt
382 |#ls #s1 #rs #Heq #Hhalt ]
383 whd in ⊢ (???(????%)); >Heq
385 whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
386 >(trans_liftL … Heq) //
389 lemma loop_lift : ∀A,B,k,lift,f,g,h,hlift,c1,c2.
390 (∀x.hlift (lift x) = h x) →
391 (∀x.h x = false → lift (f x) = g (lift x)) →
392 loop A k f h c1 = Some ? c2 →
393 loop B k g hlift (lift c1) = Some ? (lift … c2).
394 #A #B #k #lift #f #g #h #hlift #c1 #c2 #Hfg #Hhlift
395 generalize in match c1; elim k
396 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
397 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
398 cases (true_or_false (h c0)) #Hc0 >Hfg >Hc0
399 [ normalize #Heq destruct (Heq) %
400 | normalize <Hhlift // @IH ]
404 lemma loop_liftL : ∀sig,k,M1,M2,c1,c2.
405 loop ? k (step sig M1) (λc.halt sig M1 (cstate ?? c)) c1 = Some ? c2 →
406 loop ? k (step sig (seq sig M1 M2))
407 (λc.halt_liftL ?? (halt sig M1) (cstate ?? c)) (lift_confL … c1) =
408 Some ? (lift_confL … c2).
409 #sig #k #M1 #M2 #c1 #c2 generalize in match c1;
411 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
412 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
413 cases (true_or_false (halt ?? (cstate sig (states ? M1) c0))) #Hc0 >Hc0
414 [ >(?: halt_liftL ?? (halt sig M1) (cstate sig ? (lift_confL … c0)) = true)
415 [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
417 | >(?: halt_liftL ?? (halt sig M1) (cstate ?? (lift_confL … c0)) = false)
418 [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
423 lemma loop_liftR : ∀sig,k,M1,M2,c1,c2.
424 loop ? k (step sig M2) (λc.halt sig M2 (cstate ?? c)) c1 = Some ? c2 →
425 loop ? k (step sig (seq sig M1 M2))
426 (λc.halt sig (seq sig M1 M2) (cstate ?? c)) (lift_confR … c1) =
427 Some ? (lift_confR … c2).
428 #sig #k #M1 #M2 #c1 #c2 generalize in match c1;
430 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
431 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
432 cases (true_or_false (halt ?? (cstate sig ? c0))) #Hc0 >Hc0
433 [ >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = true)
434 [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
436 | >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = false)
437 [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
446 ∀A,k,f,p,a,b.loop A k f p a = Some ? b → p b = true.
448 [#a #b normalize #Hfalse destruct
449 |#k0 #IH #a #b whd in ⊢ (??%? → ?); cases (true_or_false (p a)) #Hpa
450 [ >Hpa normalize #H1 destruct //
456 lemma trans_liftL_true : ∀sig,M1,M2,s,a.
458 trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inr … (start ? M2),None ?〉.
460 #Hhalt whd in ⊢ (??%?); >Hhalt %
463 lemma eq_ctape_lift_conf_L : ∀sig,S1,S2,outc.
464 ctape sig (FinSum S1 S2) (lift_confL … outc) = ctape … outc.
465 #sig #S1 #S2 #outc cases outc #s #t %
468 lemma eq_ctape_lift_conf_R : ∀sig,S1,S2,outc.
469 ctape sig (FinSum S1 S2) (lift_confR … outc) = ctape … outc.
470 #sig #S1 #S2 #outc cases outc #s #t %
473 theorem sem_seq: ∀sig,M1,M2,R1,R2.
474 Realize sig M1 R1 → Realize sig M2 R2 →
475 Realize sig (seq sig M1 M2) (R1 ∘ R2).
476 #sig #M1 #M2 #R1 #R2 #HR1 #HR2 #t
477 cases (HR1 t) #k1 * #outc1 * #Hloop1 #HM1
478 cases (HR2 (ctape sig (states ? M1) outc1)) #k2 * #outc2 * #Hloop2 #HM2
479 @(ex_intro … (k1+k2)) @(ex_intro … (lift_confR … outc2))
481 [@(loop_merge ???????????
482 (loop_lift ??? (lift_confL sig (states sig M1) (states sig M2))
483 (step sig M1) (step sig (seq sig M1 M2))
484 (λc.halt sig M1 (cstate … c))
485 (λc.halt_liftL ?? (halt sig M1) (cstate … c)) … Hloop1))
487 [ #sl #tl whd in ⊢ (??%? → ?); #Hl %
488 | #sr #tr whd in ⊢ (??%? → ?); #Hr destruct (Hr) ]
489 || #c0 #Hhalt <step_lift_confL //
491 |6:cases outc1 #s1 #t1 %
492 |7:@(loop_lift … (initc ?? (ctape … outc1)) … Hloop2)
494 | #c0 #Hhalt <step_lift_confR // ]
495 |whd in ⊢ (??(???%)?);whd in ⊢ (??%?);
496 generalize in match Hloop1; cases outc1 #sc1 #tc1 #Hloop10
497 >(trans_liftL_true sig M1 M2 ??)
498 [ whd in ⊢ (??%?); whd in ⊢ (???%);
499 @config_eq whd in ⊢ (???%); //
500 | @(loop_Some ?????? Hloop10) ]
502 | @(ex_intro … (ctape ? (FinSum (states ? M1) (states ? M2)) (lift_confL … outc1)))
503 % // >eq_ctape_lift_conf_L >eq_ctape_lift_conf_R //