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/finset.ma".
14 include "basics/core_notation/compose_2.ma".
15 (* include "basics/relations.ma". *)
17 record tape (sig:FinSet): Type[0] ≝
22 inductive move : Type[0] ≝
27 (* We do not distinuish an input tape *)
29 record TM (sig:FinSet): Type[1] ≝
31 trans : states × (option sig) → states × (option (sig × move));
36 record config (sig,states:FinSet): Type[0] ≝
41 definition option_hd ≝ λA.λl:list A.
47 definition tape_move ≝ λsig.λt: tape sig.λm:option (sig × move).
52 [ R ⇒ mk_tape sig ((\fst m1)::(left ? t)) (tail ? (right ? t))
53 | L ⇒ mk_tape sig (tail ? (left ? t)) ((\fst m1)::(right ? t))
57 definition step ≝ λsig.λM:TM sig.λc:config sig (states sig M).
58 let current_char ≝ option_hd ? (right ? (ctape ?? c)) in
59 let 〈news,mv〉 ≝ trans sig M 〈cstate ?? c,current_char〉 in
60 mk_config ?? news (tape_move sig (ctape ?? c) mv).
62 let rec loop (A:Type[0]) n (f:A→A) p a on n ≝
65 | S m ⇒ if p a then (Some ? a) else loop A m f p (f a)
68 lemma loop_incr : ∀A,f,p,k1,k2,a1,a2.
69 loop A k1 f p a1 = Some ? a2 →
70 loop A (k2+k1) f p a1 = Some ? a2.
71 #A #f #p #k1 #k2 #a1 #a2 generalize in match a1; elim k1
72 [normalize #a0 #Hfalse destruct
73 |#k1' #IH #a0 <plus_n_Sm whd in ⊢ (??%? → ??%?);
74 cases (true_or_false (p a0)) #Hpa0 >Hpa0 whd in ⊢ (??%? → ??%?); // @IH
78 lemma loop_split : ∀A,f,p,q.(∀b. q b = true → p b = true) →
80 loop A k f q a1 = Some ? a2 →
82 loop A k1 f p a1 = Some ? a3 ∧
83 loop A (S(k-k1)) f q a3 = Some ? a2.
84 #A #f #p #q #Hpq #k elim k
85 [#a1 #a2 normalize #Heq destruct
86 |#i #Hind #a1 #a2 normalize
87 cases (true_or_false (q a1)) #Hqa1 >Hqa1 normalize
89 @(ex_intro … 1) @(ex_intro … a2) %
90 [normalize >(Hpq …Hqa1) // |>Hqa1 //]
91 |#Hloop cases (true_or_false (p a1)) #Hpa1
92 [@(ex_intro … 1) @(ex_intro … a1) %
93 [normalize >Hpa1 // |>Hqa1 <Hloop normalize //]
94 |cases (Hind …Hloop) #k2 * #a3 * #Hloop1 #Hloop2
95 @(ex_intro … (S k2)) @(ex_intro … a3) %
96 [normalize >Hpa1 normalize // | @Hloop2 ]
103 lemma loop_split : ∀A,f,p,q.(∀b. p b = false → q b = false) →
105 loop A k1 f p a1 = Some ? a2 →
106 f a2 = a3 → q a2 = false →
107 loop A k2 f q a3 = Some ? a4 →
108 loop A (k1+k2) f q a1 = Some ? a4.
109 #Sig #f #p #q #Hpq #k1 elim k1
110 [normalize #k2 #a1 #a2 #a3 #a4 #H destruct
111 |#k1' #Hind #k2 #a1 #a2 #a3 #a4 normalize in ⊢ (%→?);
112 cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
113 [#eqa1a2 destruct #eqa2a3 #Hqa2 #H
114 whd in ⊢ (??(??%???)?); >plus_n_Sm @loop_incr
115 whd in ⊢ (??%?); >Hqa2 >eqa2a3 @H
116 |normalize >(Hpq … pa1) normalize
117 #H1 #H2 #H3 @(Hind … H2) //
123 lemma loop_split : ∀A,f,p,q.(∀b. p b = false → q b = false) →
125 loop A k1 f p a1 = Some ? a2 →
126 loop A k2 f q a2 = Some ? a3 →
127 loop A (k1+k2) f q a1 = Some ? a3.
128 #Sig #f #p #q #Hpq #k1 elim k1
129 [normalize #k2 #a1 #a2 #a3 #H destruct
130 |#k1' #Hind #k2 #a1 #a2 #a3 normalize in ⊢ (%→?→?);
131 cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
132 [#eqa1a2 destruct #H @loop_incr //
133 |normalize >(Hpq … pa1) normalize
134 #H1 #H2 @(Hind … H2) //
140 definition initc ≝ λsig.λM:TM sig.λt.
141 mk_config sig (states sig M) (start sig M) t.
143 definition Realize ≝ λsig.λM:TM sig.λR:relation (tape sig).
145 loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc →
149 definition accRealize ≝ λsig.λM:TM sig.λacc:states sig M.λRtrue,Rfalse:relation (tape sig).
151 loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc ∧
152 (cstate ?? outc = acc → Rtrue t (ctape ?? outc)) ∧
153 (cstate ?? outc ≠ acc → Rfalse t (ctape ?? outc)).
157 definition seq_trans ≝ λsig. λM1,M2 : TM sig.
161 if halt sig M1 s1 then 〈inr … (start sig M2), None ?〉
163 let 〈news1,m〉 ≝ trans sig M1 〈s1,a〉 in
166 let 〈news2,m〉 ≝ trans sig M2 〈s2,a〉 in
170 definition seq ≝ λsig. λM1,M2 : TM sig.
172 (FinSum (states sig M1) (states sig M2))
173 (seq_trans sig M1 M2)
174 (inl … (start sig M1))
176 [ inl _ ⇒ false | inr s2 ⇒ halt sig M2 s2]).
178 definition Rcomp ≝ λA.λR1,R2:relation A.λa1,a2.
179 ∃am.R1 a1 am ∧ R2 am a2.
182 definition injectRl ≝ λsig.λM1.λM2.λR.
184 inl … (cstate sig M1 c11) = cstate sig (seq sig M1 M2) c1 ∧
185 inl … (cstate sig M1 c12) = cstate sig (seq sig M1 M2) c2 ∧
186 ctape sig M1 c11 = ctape sig (seq sig M1 M2) c1 ∧
187 ctape sig M1 c12 = ctape sig (seq sig M1 M2) c2 ∧
190 definition injectRr ≝ λsig.λM1.λM2.λR.
192 inr … (cstate sig M2 c21) = cstate sig (seq sig M1 M2) c1 ∧
193 inr … (cstate sig M2 c22) = cstate sig (seq sig M1 M2) c2 ∧
194 ctape sig M2 c21 = ctape sig (seq sig M1 M2) c1 ∧
195 ctape sig M2 c22 = ctape sig (seq sig M1 M2) c2 ∧
198 definition Rlink ≝ λsig.λM1,M2.λc1,c2.
199 ctape sig (seq sig M1 M2) c1 = ctape sig (seq sig M1 M2) c2 ∧
200 cstate sig (seq sig M1 M2) c1 = inl … (halt sig M1) ∧
201 cstate sig (seq sig M1 M2) c2 = inr … (start sig M2). *)
203 interpretation "relation composition" 'compose R1 R2 = (Rcomp ? R1 R2).
205 definition lift_confL ≝
206 λsig,S1,S2,c.match c with
207 [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inl … s) t ].
209 definition lift_confR ≝
210 λsig,S1,S2,c.match c with
211 [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inr … s) t ].
213 definition halt_liftL ≝
214 λS1,S2,halt.λs:FinSum S1 S2.
217 | inr _ ⇒ true ]. (* should be vacuous in all cases we use halt_liftL *)
219 definition halt_liftR ≝
220 λS1,S2,halt.λs:FinSum S1 S2.
223 | inr s2 ⇒ halt s2 ].
225 lemma p_halt_liftL : ∀sig,S1,S2,halt,c.
226 halt (cstate sig S1 c) =
227 halt_liftL S1 S2 halt (cstate … (lift_confL … c)).
228 #sig #S1 #S2 #halt #c cases c #s #t %
231 lemma trans_liftL : ∀sig,M1,M2,s,a,news,move.
232 halt ? M1 s = false →
233 trans sig M1 〈s,a〉 = 〈news,move〉 →
234 trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inl … news,move〉.
235 #sig (*#M1*) * #Q1 #T1 #init1 #halt1 #M2 #s #a #news #move
236 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
239 lemma trans_liftR : ∀sig,M1,M2,s,a,news,move.
240 halt ? M2 s = false →
241 trans sig M2 〈s,a〉 = 〈news,move〉 →
242 trans sig (seq sig M1 M2) 〈inr … s,a〉 = 〈inr … news,move〉.
243 #sig #M1 * #Q2 #T2 #init2 #halt2 #s #a #news #move
244 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
249 cstate sig M c1 = cstate sig M c2 →
250 ctape sig M c1 = ctape sig M c2 → c1 = c2.
251 #sig #M1 * #s1 #t1 * #s2 #t2 //
254 lemma step_lift_confR : ∀sig,M1,M2,c0.
255 halt ? M2 (cstate ?? c0) = false →
256 step sig (seq sig M1 M2) (lift_confR sig (states ? M1) (states ? M2) c0) =
257 lift_confR sig (states ? M1) (states ? M2) (step sig M2 c0).
258 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s * #lt
260 whd in ⊢ (???(????%));whd in ⊢ (???%);
261 lapply (refl ? (trans ?? 〈s,option_hd sig rs〉))
262 cases (trans ?? 〈s,option_hd sig rs〉) in ⊢ (???% → %);
263 #s0 #m0 #Heq whd in ⊢ (???%);
264 whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
269 lemma step_lift_confL : ∀sig,M1,M2,c0.
270 halt ? M1 (cstate ?? c0) = false →
271 step sig (seq sig M1 M2) (lift_confL sig (states ? M1) (states ? M2) c0) =
272 lift_confL sig ?? (step sig M1 c0).
273 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s * #lt
275 whd in ⊢ (???(????%));whd in ⊢ (???%);
276 lapply (refl ? (trans ?? 〈s,option_hd sig rs〉))
277 cases (trans ?? 〈s,option_hd sig rs〉) in ⊢ (???% → %);
278 #s0 #m0 #Heq whd in ⊢ (???%);
279 whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
284 lemma loop_lift : ∀A,B,k,lift,f,g,h,hlift,c1,c2.
285 (∀x.hlift (lift x) = h x) →
286 (∀x.h x = false → lift (f x) = g (lift x)) →
287 loop A k f h c1 = Some ? c2 →
288 loop B k g hlift (lift c1) = Some ? (lift … c2).
289 #A #B #k #lift #f #g #h #hlift #c1 #c2 #Hfg #Hhlift
290 generalize in match c1; elim k
291 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
292 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
293 cases (true_or_false (h c0)) #Hc0 >Hfg >Hc0
294 [ normalize #Heq destruct (Heq) %
295 | normalize <Hhlift // @IH ]
299 lemma loop_liftL : ∀sig,k,M1,M2,c1,c2.
300 loop ? k (step sig M1) (λc.halt sig M1 (cstate ?? c)) c1 = Some ? c2 →
301 loop ? k (step sig (seq sig M1 M2))
302 (λc.halt_liftL ?? (halt sig M1) (cstate ?? c)) (lift_confL … c1) =
303 Some ? (lift_confL … c2).
304 #sig #k #M1 #M2 #c1 #c2 generalize in match c1;
306 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
307 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
308 cases (true_or_false (halt ?? (cstate sig (states ? M1) c0))) #Hc0 >Hc0
309 [ >(?: halt_liftL ?? (halt sig M1) (cstate sig ? (lift_confL … c0)) = true)
310 [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
312 | >(?: halt_liftL ?? (halt sig M1) (cstate ?? (lift_confL … c0)) = false)
313 [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
318 lemma loop_liftR : ∀sig,k,M1,M2,c1,c2.
319 loop ? k (step sig M2) (λc.halt sig M2 (cstate ?? c)) c1 = Some ? c2 →
320 loop ? k (step sig (seq sig M1 M2))
321 (λc.halt sig (seq sig M1 M2) (cstate ?? c)) (lift_confR … c1) =
322 Some ? (lift_confR … c2).
323 #sig #k #M1 #M2 #c1 #c2 generalize in match c1;
325 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
326 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
327 cases (true_or_false (halt ?? (cstate sig ? c0))) #Hc0 >Hc0
328 [ >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = true)
329 [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
331 | >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = false)
332 [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
341 ∀A,k,f,p,a,b.loop A k f p a = Some ? b → p b = true.
343 [#a #b normalize #Hfalse destruct
344 |#k0 #IH #a #b whd in ⊢ (??%? → ?); cases (true_or_false (p a)) #Hpa
345 [ >Hpa normalize #H1 destruct //
351 lemma trans_liftL_true : ∀sig,M1,M2,s,a.
353 trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inr … (start ? M2),None ?〉.
355 #Hhalt whd in ⊢ (??%?); >Hhalt %
358 lemma eq_ctape_lift_conf_L : ∀sig,S1,S2,outc.
359 ctape sig (FinSum S1 S2) (lift_confL … outc) = ctape … outc.
360 #sig #S1 #S2 #outc cases outc #s #t %
363 lemma eq_ctape_lift_conf_R : ∀sig,S1,S2,outc.
364 ctape sig (FinSum S1 S2) (lift_confR … outc) = ctape … outc.
365 #sig #S1 #S2 #outc cases outc #s #t %
368 axiom daemon :∀P:Prop.P.
370 theorem sem_seq: ∀sig,M1,M2,R1,R2.
371 Realize sig M1 R1 → Realize sig M2 R2 →
372 Realize sig (seq sig M1 M2) (R1 ∘ R2).
373 @daemon (* this no longer works: TODO *) (*
374 #sig #M1 #M2 #R1 #R2 #HR1 #HR2 #t #i #outc #Hloop
375 cases (HR1 t) #k1 * #outc1 * #Hloop1 #HM1
376 cases (HR2 (ctape sig (states ? M1) outc1)) #k2 * #outc2 * #Hloop2 #HM2
377 @(ex_intro … (k1+k2)) @(ex_intro … (lift_confR … outc2))
379 [@(loop_split ???????????
380 (loop_lift ??? (lift_confL sig (states sig M1) (states sig M2))
381 (step sig M1) (step sig (seq sig M1 M2))
382 (λc.halt sig M1 (cstate … c))
383 (λc.halt_liftL ?? (halt sig M1) (cstate … c)) … Hloop1))
385 [ #sl #tl whd in ⊢ (??%? → ?); #Hl %
386 | #sr #tr whd in ⊢ (??%? → ?); #Hr destruct (Hr) ]
387 || #c0 #Hhalt <step_lift_confL //
389 |6:cases outc1 #s1 #t1 %
390 |7:@(loop_lift … (initc ?? (ctape … outc1)) … Hloop2)
392 | #c0 #Hhalt <step_lift_confR // ]
393 |whd in ⊢ (??(???%)?);whd in ⊢ (??%?);
394 generalize in match Hloop1; cases outc1 #sc1 #tc1 #Hloop10
395 >(trans_liftL_true sig M1 M2 ??)
396 [ whd in ⊢ (??%?); whd in ⊢ (???%);
397 @config_eq whd in ⊢ (???%); //
398 | @(loop_Some ?????? Hloop10) ]
400 | @(ex_intro … (ctape ? (FinSum (states ? M1) (states ? M2)) (lift_confL … outc1)))
401 % // >eq_ctape_lift_conf_L >eq_ctape_lift_conf_R //