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/relations.ma". *)
16 record tape (sig:FinSet): Type[0] ≝
21 inductive move : Type[0] ≝
26 (* We do not distinuish an input tape *)
28 record TM (sig:FinSet): Type[1] ≝
30 trans : states × (option sig) → states × (option (sig × move));
35 record config (sig,states:FinSet): Type[0] ≝
40 definition option_hd ≝ λA.λl:list A.
46 definition tape_move ≝ λsig.λt: tape sig.λm:option (sig × move).
51 [ R ⇒ mk_tape sig ((\fst m1)::(left ? t)) (tail ? (right ? t))
52 | L ⇒ mk_tape sig (tail ? (left ? t)) ((\fst m1)::(right ? t))
56 definition step ≝ λsig.λM:TM sig.λc:config sig (states sig M).
57 let current_char ≝ option_hd ? (right ? (ctape ?? c)) in
58 let 〈news,mv〉 ≝ trans sig M 〈cstate ?? c,current_char〉 in
59 mk_config ?? news (tape_move sig (ctape ?? c) mv).
61 let rec loop (A:Type[0]) n (f:A→A) p a on n ≝
64 | S m ⇒ if p a then (Some ? a) else loop A m f p (f a)
67 lemma loop_incr : ∀A,f,p,k1,k2,a1,a2.
68 loop A k1 f p a1 = Some ? a2 →
69 loop A (k2+k1) f p a1 = Some ? a2.
70 #A #f #p #k1 #k2 #a1 #a2 generalize in match a1; elim k1
71 [normalize #a0 #Hfalse destruct
72 |#k1' #IH #a0 <plus_n_Sm whd in ⊢ (??%? → ??%?);
73 cases (true_or_false (p a0)) #Hpa0 >Hpa0 whd in ⊢ (??%? → ??%?); // @IH
77 lemma loop_split : ∀A,f,p,q.(∀b. q b = true → p b = true) →
79 loop A k f q a1 = Some ? a2 →
81 loop A k1 f p a1 = Some ? a3 ∧
82 loop A (S(k-k1)) f q a3 = Some ? a2.
83 #A #f #p #q #Hpq #k elim k
84 [#a1 #a2 normalize #Heq destruct
85 |#i #Hind #a1 #a2 normalize
86 cases (true_or_false (q a1)) #Hqa1 >Hqa1 normalize
88 @(ex_intro … 1) @(ex_intro … a2) %
89 [normalize >(Hpq …Hqa1) // |>Hqa1 //]
90 |#Hloop cases (true_or_false (p a1)) #Hpa1
91 [@(ex_intro … 1) @(ex_intro … a1) %
92 [normalize >Hpa1 // |>Hqa1 <Hloop normalize //]
93 |cases (Hind …Hloop) #k2 * #a3 * #Hloop1 #Hloop2
94 @(ex_intro … (S k2)) @(ex_intro … a3) %
95 [normalize >Hpa1 normalize // | @Hloop2 ]
102 lemma loop_split : ∀A,f,p,q.(∀b. p b = false → q b = false) →
104 loop A k1 f p a1 = Some ? a2 →
105 f a2 = a3 → q a2 = false →
106 loop A k2 f q a3 = Some ? a4 →
107 loop A (k1+k2) f q a1 = Some ? a4.
108 #Sig #f #p #q #Hpq #k1 elim k1
109 [normalize #k2 #a1 #a2 #a3 #a4 #H destruct
110 |#k1' #Hind #k2 #a1 #a2 #a3 #a4 normalize in ⊢ (%→?);
111 cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
112 [#eqa1a2 destruct #eqa2a3 #Hqa2 #H
113 whd in ⊢ (??(??%???)?); >plus_n_Sm @loop_incr
114 whd in ⊢ (??%?); >Hqa2 >eqa2a3 @H
115 |normalize >(Hpq … pa1) normalize
116 #H1 #H2 #H3 @(Hind … H2) //
122 lemma loop_split : ∀A,f,p,q.(∀b. p b = false → q b = false) →
124 loop A k1 f p a1 = Some ? a2 →
125 loop A k2 f q a2 = Some ? a3 →
126 loop A (k1+k2) f q a1 = Some ? a3.
127 #Sig #f #p #q #Hpq #k1 elim k1
128 [normalize #k2 #a1 #a2 #a3 #H destruct
129 |#k1' #Hind #k2 #a1 #a2 #a3 normalize in ⊢ (%→?→?);
130 cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
131 [#eqa1a2 destruct #H @loop_incr //
132 |normalize >(Hpq … pa1) normalize
133 #H1 #H2 @(Hind … H2) //
139 definition initc ≝ λsig.λM:TM sig.λt.
140 mk_config sig (states sig M) (start sig M) t.
142 definition Realize ≝ λsig.λM:TM sig.λR:relation (tape sig).
144 loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc →
148 definition accRealize ≝ λsig.λM:TM sig.λacc:states sig M.λRtrue,Rfalse:relation (tape sig).
150 loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc ∧
151 (cstate ?? outc = acc → Rtrue t (ctape ?? outc)) ∧
152 (cstate ?? outc ≠ acc → Rfalse t (ctape ?? outc)).
156 definition seq_trans ≝ λsig. λM1,M2 : TM sig.
160 if halt sig M1 s1 then 〈inr … (start sig M2), None ?〉
162 let 〈news1,m〉 ≝ trans sig M1 〈s1,a〉 in
165 let 〈news2,m〉 ≝ trans sig M2 〈s2,a〉 in
169 definition seq ≝ λsig. λM1,M2 : TM sig.
171 (FinSum (states sig M1) (states sig M2))
172 (seq_trans sig M1 M2)
173 (inl … (start sig M1))
175 [ inl _ ⇒ false | inr s2 ⇒ halt sig M2 s2]).
177 definition Rcomp ≝ λA.λR1,R2:relation A.λa1,a2.
178 ∃am.R1 a1 am ∧ R2 am a2.
181 definition injectRl ≝ λsig.λM1.λM2.λR.
183 inl … (cstate sig M1 c11) = cstate sig (seq sig M1 M2) c1 ∧
184 inl … (cstate sig M1 c12) = cstate sig (seq sig M1 M2) c2 ∧
185 ctape sig M1 c11 = ctape sig (seq sig M1 M2) c1 ∧
186 ctape sig M1 c12 = ctape sig (seq sig M1 M2) c2 ∧
189 definition injectRr ≝ λsig.λM1.λM2.λR.
191 inr … (cstate sig M2 c21) = cstate sig (seq sig M1 M2) c1 ∧
192 inr … (cstate sig M2 c22) = cstate sig (seq sig M1 M2) c2 ∧
193 ctape sig M2 c21 = ctape sig (seq sig M1 M2) c1 ∧
194 ctape sig M2 c22 = ctape sig (seq sig M1 M2) c2 ∧
197 definition Rlink ≝ λsig.λM1,M2.λc1,c2.
198 ctape sig (seq sig M1 M2) c1 = ctape sig (seq sig M1 M2) c2 ∧
199 cstate sig (seq sig M1 M2) c1 = inl … (halt sig M1) ∧
200 cstate sig (seq sig M1 M2) c2 = inr … (start sig M2). *)
202 interpretation "relation composition" 'compose R1 R2 = (Rcomp ? R1 R2).
204 definition lift_confL ≝
205 λsig,S1,S2,c.match c with
206 [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inl … s) t ].
208 definition lift_confR ≝
209 λsig,S1,S2,c.match c with
210 [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inr … s) t ].
212 definition halt_liftL ≝
213 λS1,S2,halt.λs:FinSum S1 S2.
216 | inr _ ⇒ true ]. (* should be vacuous in all cases we use halt_liftL *)
218 definition halt_liftR ≝
219 λS1,S2,halt.λs:FinSum S1 S2.
222 | inr s2 ⇒ halt s2 ].
224 lemma p_halt_liftL : ∀sig,S1,S2,halt,c.
225 halt (cstate sig S1 c) =
226 halt_liftL S1 S2 halt (cstate … (lift_confL … c)).
227 #sig #S1 #S2 #halt #c cases c #s #t %
230 lemma trans_liftL : ∀sig,M1,M2,s,a,news,move.
231 halt ? M1 s = false →
232 trans sig M1 〈s,a〉 = 〈news,move〉 →
233 trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inl … news,move〉.
234 #sig (*#M1*) * #Q1 #T1 #init1 #halt1 #M2 #s #a #news #move
235 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
238 lemma trans_liftR : ∀sig,M1,M2,s,a,news,move.
239 halt ? M2 s = false →
240 trans sig M2 〈s,a〉 = 〈news,move〉 →
241 trans sig (seq sig M1 M2) 〈inr … s,a〉 = 〈inr … news,move〉.
242 #sig #M1 * #Q2 #T2 #init2 #halt2 #s #a #news #move
243 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
248 cstate sig M c1 = cstate sig M c2 →
249 ctape sig M c1 = ctape sig M c2 → c1 = c2.
250 #sig #M1 * #s1 #t1 * #s2 #t2 //
253 lemma step_lift_confR : ∀sig,M1,M2,c0.
254 halt ? M2 (cstate ?? c0) = false →
255 step sig (seq sig M1 M2) (lift_confR sig (states ? M1) (states ? M2) c0) =
256 lift_confR sig (states ? M1) (states ? M2) (step sig M2 c0).
257 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s * #lt
259 whd in ⊢ (???(????%));whd in ⊢ (???%);
260 lapply (refl ? (trans ?? 〈s,option_hd sig rs〉))
261 cases (trans ?? 〈s,option_hd sig rs〉) in ⊢ (???% → %);
262 #s0 #m0 #Heq whd in ⊢ (???%);
263 whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
268 lemma step_lift_confL : ∀sig,M1,M2,c0.
269 halt ? M1 (cstate ?? c0) = false →
270 step sig (seq sig M1 M2) (lift_confL sig (states ? M1) (states ? M2) c0) =
271 lift_confL sig ?? (step sig M1 c0).
272 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s * #lt
274 whd in ⊢ (???(????%));whd in ⊢ (???%);
275 lapply (refl ? (trans ?? 〈s,option_hd sig rs〉))
276 cases (trans ?? 〈s,option_hd sig rs〉) in ⊢ (???% → %);
277 #s0 #m0 #Heq whd in ⊢ (???%);
278 whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
283 lemma loop_lift : ∀A,B,k,lift,f,g,h,hlift,c1,c2.
284 (∀x.hlift (lift x) = h x) →
285 (∀x.h x = false → lift (f x) = g (lift x)) →
286 loop A k f h c1 = Some ? c2 →
287 loop B k g hlift (lift c1) = Some ? (lift … c2).
288 #A #B #k #lift #f #g #h #hlift #c1 #c2 #Hfg #Hhlift
289 generalize in match c1; elim k
290 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
291 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
292 cases (true_or_false (h c0)) #Hc0 >Hfg >Hc0
293 [ normalize #Heq destruct (Heq) %
294 | normalize <Hhlift // @IH ]
298 lemma loop_liftL : ∀sig,k,M1,M2,c1,c2.
299 loop ? k (step sig M1) (λc.halt sig M1 (cstate ?? c)) c1 = Some ? c2 →
300 loop ? k (step sig (seq sig M1 M2))
301 (λc.halt_liftL ?? (halt sig M1) (cstate ?? c)) (lift_confL … c1) =
302 Some ? (lift_confL … c2).
303 #sig #k #M1 #M2 #c1 #c2 generalize in match c1;
305 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
306 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
307 cases (true_or_false (halt ?? (cstate sig (states ? M1) c0))) #Hc0 >Hc0
308 [ >(?: halt_liftL ?? (halt sig M1) (cstate sig ? (lift_confL … c0)) = true)
309 [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
311 | >(?: halt_liftL ?? (halt sig M1) (cstate ?? (lift_confL … c0)) = false)
312 [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
317 lemma loop_liftR : ∀sig,k,M1,M2,c1,c2.
318 loop ? k (step sig M2) (λc.halt sig M2 (cstate ?? c)) c1 = Some ? c2 →
319 loop ? k (step sig (seq sig M1 M2))
320 (λc.halt sig (seq sig M1 M2) (cstate ?? c)) (lift_confR … c1) =
321 Some ? (lift_confR … c2).
322 #sig #k #M1 #M2 #c1 #c2 generalize in match c1;
324 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
325 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
326 cases (true_or_false (halt ?? (cstate sig ? c0))) #Hc0 >Hc0
327 [ >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = true)
328 [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
330 | >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = false)
331 [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
340 ∀A,k,f,p,a,b.loop A k f p a = Some ? b → p b = true.
342 [#a #b normalize #Hfalse destruct
343 |#k0 #IH #a #b whd in ⊢ (??%? → ?); cases (true_or_false (p a)) #Hpa
344 [ >Hpa normalize #H1 destruct //
350 lemma trans_liftL_true : ∀sig,M1,M2,s,a.
352 trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inr … (start ? M2),None ?〉.
354 #Hhalt whd in ⊢ (??%?); >Hhalt %
357 lemma eq_ctape_lift_conf_L : ∀sig,S1,S2,outc.
358 ctape sig (FinSum S1 S2) (lift_confL … outc) = ctape … outc.
359 #sig #S1 #S2 #outc cases outc #s #t %
362 lemma eq_ctape_lift_conf_R : ∀sig,S1,S2,outc.
363 ctape sig (FinSum S1 S2) (lift_confR … outc) = ctape … outc.
364 #sig #S1 #S2 #outc cases outc #s #t %
367 axiom daemon :∀P:Prop.P.
369 theorem sem_seq: ∀sig,M1,M2,R1,R2.
370 Realize sig M1 R1 → Realize sig M2 R2 →
371 Realize sig (seq sig M1 M2) (R1 ∘ R2).
372 @daemon (* this no longer works: TODO *) (*
373 #sig #M1 #M2 #R1 #R2 #HR1 #HR2 #t #i #outc #Hloop
374 cases (HR1 t) #k1 * #outc1 * #Hloop1 #HM1
375 cases (HR2 (ctape sig (states ? M1) outc1)) #k2 * #outc2 * #Hloop2 #HM2
376 @(ex_intro … (k1+k2)) @(ex_intro … (lift_confR … outc2))
378 [@(loop_split ???????????
379 (loop_lift ??? (lift_confL sig (states sig M1) (states sig M2))
380 (step sig M1) (step sig (seq sig M1 M2))
381 (λc.halt sig M1 (cstate … c))
382 (λc.halt_liftL ?? (halt sig M1) (cstate … c)) … Hloop1))
384 [ #sl #tl whd in ⊢ (??%? → ?); #Hl %
385 | #sr #tr whd in ⊢ (??%? → ?); #Hr destruct (Hr) ]
386 || #c0 #Hhalt <step_lift_confL //
388 |6:cases outc1 #s1 #t1 %
389 |7:@(loop_lift … (initc ?? (ctape … outc1)) … Hloop2)
391 | #c0 #Hhalt <step_lift_confR // ]
392 |whd in ⊢ (??(???%)?);whd in ⊢ (??%?);
393 generalize in match Hloop1; cases outc1 #sc1 #tc1 #Hloop10
394 >(trans_liftL_true sig M1 M2 ??)
395 [ whd in ⊢ (??%?); whd in ⊢ (???%);
396 @config_eq whd in ⊢ (???%); //
397 | @(loop_Some ?????? Hloop10) ]
399 | @(ex_intro … (ctape ? (FinSum (states ? M1) (states ? M2)) (lift_confL … outc1)))
400 % // >eq_ctape_lift_conf_L >eq_ctape_lift_conf_R //