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". *)
15 record tape (sig:FinSet): Type[0] ≝
20 inductive move : Type[0] ≝
25 (* We do not distinuish an input tape *)
27 record TM (sig:FinSet): Type[1] ≝
29 trans : states × (option sig) → states × (option (sig × move));
34 record config (sig,states:FinSet): Type[0] ≝
39 definition option_hd ≝ λA.λl:list A.
45 definition tape_move ≝ λsig.λt: tape sig.λm:option (sig × move).
50 [ R ⇒ mk_tape sig ((\fst m1)::(left ? t)) (tail ? (right ? t))
51 | L ⇒ mk_tape sig (tail ? (left ? t)) ((\fst m1)::(right ? t))
55 definition step ≝ λsig.λM:TM sig.λc:config sig (states sig M).
56 let current_char ≝ option_hd ? (right ? (ctape ?? c)) in
57 let 〈news,mv〉 ≝ trans sig M 〈cstate ?? c,current_char〉 in
58 mk_config ?? news (tape_move sig (ctape ?? c) mv).
60 let rec loop (A:Type[0]) n (f:A→A) p a on n ≝
63 | S m ⇒ if p a then (Some ? a) else loop A m f p (f a)
66 lemma loop_incr : ∀A,f,p,k1,k2,a1,a2.
67 loop A k1 f p a1 = Some ? a2 →
68 loop A (k2+k1) f p a1 = Some ? a2.
69 #A #f #p #k1 #k2 #a1 #a2 generalize in match a1; elim k1
70 [normalize #a0 #Hfalse destruct
71 |#k1' #IH #a0 <plus_n_Sm whd in ⊢ (??%? → ??%?);
72 cases (true_or_false (p a0)) #Hpa0 >Hpa0 whd in ⊢ (??%? → ??%?); // @IH
76 lemma loop_split : ∀A,f,p,q.(∀b. q b = true → p b = true) →
78 loop A k f q a1 = Some ? a2 →
80 loop A k1 f p a1 = Some ? a3 ∧
81 loop A (S(k-k1)) f q a3 = Some ? a2.
82 #A #f #p #q #Hpq #k elim k
83 [#a1 #a2 normalize #Heq destruct
84 |#i #Hind #a1 #a2 normalize
85 cases (true_or_false (q a1)) #Hqa1 >Hqa1 normalize
87 @(ex_intro … 1) @(ex_intro … a2) %
88 [normalize >(Hpq …Hqa1) // |>Hqa1 //]
89 |#Hloop cases (true_or_false (p a1)) #Hpa1
90 [@(ex_intro … 1) @(ex_intro … a1) %
91 [normalize >Hpa1 // |>Hqa1 <Hloop normalize //]
92 |cases (Hind …Hloop) #k2 * #a3 * #Hloop1 #Hloop2
93 @(ex_intro … (S k2)) @(ex_intro … a3) %
94 [normalize >Hpa1 normalize // | @Hloop2 ]
101 lemma loop_split : ∀A,f,p,q.(∀b. p b = false → q b = false) →
103 loop A k1 f p a1 = Some ? a2 →
104 f a2 = a3 → q a2 = false →
105 loop A k2 f q a3 = Some ? a4 →
106 loop A (k1+k2) f q a1 = Some ? a4.
107 #Sig #f #p #q #Hpq #k1 elim k1
108 [normalize #k2 #a1 #a2 #a3 #a4 #H destruct
109 |#k1' #Hind #k2 #a1 #a2 #a3 #a4 normalize in ⊢ (%→?);
110 cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
111 [#eqa1a2 destruct #eqa2a3 #Hqa2 #H
112 whd in ⊢ (??(??%???)?); >plus_n_Sm @loop_incr
113 whd in ⊢ (??%?); >Hqa2 >eqa2a3 @H
114 |normalize >(Hpq … pa1) normalize
115 #H1 #H2 #H3 @(Hind … H2) //
121 lemma loop_split : ∀A,f,p,q.(∀b. p b = false → q b = false) →
123 loop A k1 f p a1 = Some ? a2 →
124 loop A k2 f q a2 = Some ? a3 →
125 loop A (k1+k2) f q a1 = Some ? a3.
126 #Sig #f #p #q #Hpq #k1 elim k1
127 [normalize #k2 #a1 #a2 #a3 #H destruct
128 |#k1' #Hind #k2 #a1 #a2 #a3 normalize in ⊢ (%→?→?);
129 cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
130 [#eqa1a2 destruct #H @loop_incr //
131 |normalize >(Hpq … pa1) normalize
132 #H1 #H2 @(Hind … H2) //
138 definition initc ≝ λsig.λM:TM sig.λt.
139 mk_config sig (states sig M) (start sig M) t.
141 definition Realize ≝ λsig.λM:TM sig.λR:relation (tape sig).
143 loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc →
147 definition accRealize ≝ λsig.λM:TM sig.λacc:states sig M.λRtrue,Rfalse:relation (tape sig).
149 loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc ∧
150 (cstate ?? outc = acc → Rtrue t (ctape ?? outc)) ∧
151 (cstate ?? outc ≠ acc → Rfalse t (ctape ?? outc)).
155 definition seq_trans ≝ λsig. λM1,M2 : TM sig.
159 if halt sig M1 s1 then 〈inr … (start sig M2), None ?〉
161 let 〈news1,m〉 ≝ trans sig M1 〈s1,a〉 in
164 let 〈news2,m〉 ≝ trans sig M2 〈s2,a〉 in
168 definition seq ≝ λsig. λM1,M2 : TM sig.
170 (FinSum (states sig M1) (states sig M2))
171 (seq_trans sig M1 M2)
172 (inl … (start sig M1))
174 [ inl _ ⇒ false | inr s2 ⇒ halt sig M2 s2]).
176 definition Rcomp ≝ λA.λR1,R2:relation A.λa1,a2.
177 ∃am.R1 a1 am ∧ R2 am a2.
180 definition injectRl ≝ λsig.λM1.λM2.λR.
182 inl … (cstate sig M1 c11) = cstate sig (seq sig M1 M2) c1 ∧
183 inl … (cstate sig M1 c12) = cstate sig (seq sig M1 M2) c2 ∧
184 ctape sig M1 c11 = ctape sig (seq sig M1 M2) c1 ∧
185 ctape sig M1 c12 = ctape sig (seq sig M1 M2) c2 ∧
188 definition injectRr ≝ λsig.λM1.λM2.λR.
190 inr … (cstate sig M2 c21) = cstate sig (seq sig M1 M2) c1 ∧
191 inr … (cstate sig M2 c22) = cstate sig (seq sig M1 M2) c2 ∧
192 ctape sig M2 c21 = ctape sig (seq sig M1 M2) c1 ∧
193 ctape sig M2 c22 = ctape sig (seq sig M1 M2) c2 ∧
196 definition Rlink ≝ λsig.λM1,M2.λc1,c2.
197 ctape sig (seq sig M1 M2) c1 = ctape sig (seq sig M1 M2) c2 ∧
198 cstate sig (seq sig M1 M2) c1 = inl … (halt sig M1) ∧
199 cstate sig (seq sig M1 M2) c2 = inr … (start sig M2). *)
201 interpretation "relation composition" 'compose R1 R2 = (Rcomp ? R1 R2).
203 definition lift_confL ≝
204 λsig,S1,S2,c.match c with
205 [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inl … s) t ].
207 definition lift_confR ≝
208 λsig,S1,S2,c.match c with
209 [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inr … s) t ].
211 definition halt_liftL ≝
212 λS1,S2,halt.λs:FinSum S1 S2.
215 | inr _ ⇒ true ]. (* should be vacuous in all cases we use halt_liftL *)
217 definition halt_liftR ≝
218 λS1,S2,halt.λs:FinSum S1 S2.
221 | inr s2 ⇒ halt s2 ].
223 lemma p_halt_liftL : ∀sig,S1,S2,halt,c.
224 halt (cstate sig S1 c) =
225 halt_liftL S1 S2 halt (cstate … (lift_confL … c)).
226 #sig #S1 #S2 #halt #c cases c #s #t %
229 lemma trans_liftL : ∀sig,M1,M2,s,a,news,move.
230 halt ? M1 s = false →
231 trans sig M1 〈s,a〉 = 〈news,move〉 →
232 trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inl … news,move〉.
233 #sig (*#M1*) * #Q1 #T1 #init1 #halt1 #M2 #s #a #news #move
234 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
237 lemma trans_liftR : ∀sig,M1,M2,s,a,news,move.
238 halt ? M2 s = false →
239 trans sig M2 〈s,a〉 = 〈news,move〉 →
240 trans sig (seq sig M1 M2) 〈inr … s,a〉 = 〈inr … news,move〉.
241 #sig #M1 * #Q2 #T2 #init2 #halt2 #s #a #news #move
242 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
247 cstate sig M c1 = cstate sig M c2 →
248 ctape sig M c1 = ctape sig M c2 → c1 = c2.
249 #sig #M1 * #s1 #t1 * #s2 #t2 //
252 lemma step_lift_confR : ∀sig,M1,M2,c0.
253 halt ? M2 (cstate ?? c0) = false →
254 step sig (seq sig M1 M2) (lift_confR sig (states ? M1) (states ? M2) c0) =
255 lift_confR sig (states ? M1) (states ? M2) (step sig M2 c0).
256 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s * #lt
258 whd in ⊢ (???(????%));whd in ⊢ (???%);
259 lapply (refl ? (trans ?? 〈s,option_hd sig rs〉))
260 cases (trans ?? 〈s,option_hd sig rs〉) in ⊢ (???% → %);
261 #s0 #m0 #Heq whd in ⊢ (???%);
262 whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
267 lemma step_lift_confL : ∀sig,M1,M2,c0.
268 halt ? M1 (cstate ?? c0) = false →
269 step sig (seq sig M1 M2) (lift_confL sig (states ? M1) (states ? M2) c0) =
270 lift_confL sig ?? (step sig M1 c0).
271 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s * #lt
273 whd in ⊢ (???(????%));whd in ⊢ (???%);
274 lapply (refl ? (trans ?? 〈s,option_hd sig rs〉))
275 cases (trans ?? 〈s,option_hd sig rs〉) in ⊢ (???% → %);
276 #s0 #m0 #Heq whd in ⊢ (???%);
277 whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
282 lemma loop_lift : ∀A,B,k,lift,f,g,h,hlift,c1,c2.
283 (∀x.hlift (lift x) = h x) →
284 (∀x.h x = false → lift (f x) = g (lift x)) →
285 loop A k f h c1 = Some ? c2 →
286 loop B k g hlift (lift c1) = Some ? (lift … c2).
287 #A #B #k #lift #f #g #h #hlift #c1 #c2 #Hfg #Hhlift
288 generalize in match c1; elim k
289 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
290 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
291 cases (true_or_false (h c0)) #Hc0 >Hfg >Hc0
292 [ normalize #Heq destruct (Heq) %
293 | normalize <Hhlift // @IH ]
297 lemma loop_liftL : ∀sig,k,M1,M2,c1,c2.
298 loop ? k (step sig M1) (λc.halt sig M1 (cstate ?? c)) c1 = Some ? c2 →
299 loop ? k (step sig (seq sig M1 M2))
300 (λc.halt_liftL ?? (halt sig M1) (cstate ?? c)) (lift_confL … c1) =
301 Some ? (lift_confL … c2).
302 #sig #k #M1 #M2 #c1 #c2 generalize in match c1;
304 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
305 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
306 cases (true_or_false (halt ?? (cstate sig (states ? M1) c0))) #Hc0 >Hc0
307 [ >(?: halt_liftL ?? (halt sig M1) (cstate sig ? (lift_confL … c0)) = true)
308 [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
310 | >(?: halt_liftL ?? (halt sig M1) (cstate ?? (lift_confL … c0)) = false)
311 [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
316 lemma loop_liftR : ∀sig,k,M1,M2,c1,c2.
317 loop ? k (step sig M2) (λc.halt sig M2 (cstate ?? c)) c1 = Some ? c2 →
318 loop ? k (step sig (seq sig M1 M2))
319 (λc.halt sig (seq sig M1 M2) (cstate ?? c)) (lift_confR … c1) =
320 Some ? (lift_confR … c2).
321 #sig #k #M1 #M2 #c1 #c2 generalize in match c1;
323 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
324 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
325 cases (true_or_false (halt ?? (cstate sig ? c0))) #Hc0 >Hc0
326 [ >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = true)
327 [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
329 | >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = false)
330 [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
339 ∀A,k,f,p,a,b.loop A k f p a = Some ? b → p b = true.
341 [#a #b normalize #Hfalse destruct
342 |#k0 #IH #a #b whd in ⊢ (??%? → ?); cases (true_or_false (p a)) #Hpa
343 [ >Hpa normalize #H1 destruct //
349 lemma trans_liftL_true : ∀sig,M1,M2,s,a.
351 trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inr … (start ? M2),None ?〉.
353 #Hhalt whd in ⊢ (??%?); >Hhalt %
356 lemma eq_ctape_lift_conf_L : ∀sig,S1,S2,outc.
357 ctape sig (FinSum S1 S2) (lift_confL … outc) = ctape … outc.
358 #sig #S1 #S2 #outc cases outc #s #t %
361 lemma eq_ctape_lift_conf_R : ∀sig,S1,S2,outc.
362 ctape sig (FinSum S1 S2) (lift_confR … outc) = ctape … outc.
363 #sig #S1 #S2 #outc cases outc #s #t %
366 theorem sem_seq: ∀sig,M1,M2,R1,R2.
367 Realize sig M1 R1 → Realize sig M2 R2 →
368 Realize sig (seq sig M1 M2) (R1 ∘ R2).
369 #sig #M1 #M2 #R1 #R2 #HR1 #HR2 #t #i #outc #Hloop
370 cases (HR1 t) #k1 * #outc1 * #Hloop1 #HM1
371 cases (HR2 (ctape sig (states ? M1) outc1)) #k2 * #outc2 * #Hloop2 #HM2
372 @(ex_intro … (k1+k2)) @(ex_intro … (lift_confR … outc2))
374 [@(loop_split ???????????
375 (loop_lift ??? (lift_confL sig (states sig M1) (states sig M2))
376 (step sig M1) (step sig (seq sig M1 M2))
377 (λc.halt sig M1 (cstate … c))
378 (λc.halt_liftL ?? (halt sig M1) (cstate … c)) … Hloop1))
380 [ #sl #tl whd in ⊢ (??%? → ?); #Hl %
381 | #sr #tr whd in ⊢ (??%? → ?); #Hr destruct (Hr) ]
382 || #c0 #Hhalt <step_lift_confL //
384 |6:cases outc1 #s1 #t1 %
385 |7:@(loop_lift … (initc ?? (ctape … outc1)) … Hloop2)
387 | #c0 #Hhalt <step_lift_confR // ]
388 |whd in ⊢ (??(???%)?);whd in ⊢ (??%?);
389 generalize in match Hloop1; cases outc1 #sc1 #tc1 #Hloop10
390 >(trans_liftL_true sig M1 M2 ??)
391 [ whd in ⊢ (??%?); whd in ⊢ (???%);
392 @config_eq whd in ⊢ (???%); //
393 | @(loop_Some ?????? Hloop10) ]
395 | @(ex_intro … (ctape ? (FinSum (states ? M1) (states ? M2)) (lift_confL … outc1)))
396 % // >eq_ctape_lift_conf_L >eq_ctape_lift_conf_R //