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_merge : ∀A,f,p,q.(∀b. p b = false → q b = false) →
78 loop A k1 f p a1 = Some ? a2 →
79 f a2 = a3 → q a2 = false →
80 loop A k2 f q a3 = Some ? a4 →
81 loop A (k1+k2) f q a1 = Some ? a4.
82 #Sig #f #p #q #Hpq #k1 elim k1
83 [normalize #k2 #a1 #a2 #a3 #a4 #H destruct
84 |#k1' #Hind #k2 #a1 #a2 #a3 #a4 normalize in ⊢ (%→?);
85 cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
86 [#eqa1a2 destruct #eqa2a3 #Hqa2 #H
87 whd in ⊢ (??(??%???)?); >plus_n_Sm @loop_incr
88 whd in ⊢ (??%?); >Hqa2 >eqa2a3 @H
89 |normalize >(Hpq … pa1) normalize
90 #H1 #H2 #H3 @(Hind … H2) //
95 lemma loop_split : ∀A,f,p,q.(∀b. q b = true → p b = true) →
97 loop A k f q a1 = Some ? a2 →
99 loop A k1 f p a1 = Some ? a3 ∧
100 loop A (S(k-k1)) f q a3 = Some ? a2.
101 #A #f #p #q #Hpq #k elim k
102 [#a1 #a2 normalize #Heq destruct
103 |#i #Hind #a1 #a2 normalize
104 cases (true_or_false (q a1)) #Hqa1 >Hqa1 normalize
106 @(ex_intro … 1) @(ex_intro … a2) %
107 [normalize >(Hpq …Hqa1) // |>Hqa1 //]
108 |#Hloop cases (true_or_false (p a1)) #Hpa1
109 [@(ex_intro … 1) @(ex_intro … a1) %
110 [normalize >Hpa1 // |>Hqa1 <Hloop normalize //]
111 |cases (Hind …Hloop) #k2 * #a3 * #Hloop1 #Hloop2
112 @(ex_intro … (S k2)) @(ex_intro … a3) %
113 [normalize >Hpa1 normalize // | @Hloop2 ]
120 lemma loop_split : ∀A,f,p,q.(∀b. p b = false → q b = false) →
122 loop A k1 f p a1 = Some ? a2 →
123 loop A k2 f q a2 = Some ? a3 →
124 loop A (k1+k2) f q a1 = Some ? a3.
125 #Sig #f #p #q #Hpq #k1 elim k1
126 [normalize #k2 #a1 #a2 #a3 #H destruct
127 |#k1' #Hind #k2 #a1 #a2 #a3 normalize in ⊢ (%→?→?);
128 cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
129 [#eqa1a2 destruct #H @loop_incr //
130 |normalize >(Hpq … pa1) normalize
131 #H1 #H2 @(Hind … H2) //
137 definition initc ≝ λsig.λM:TM sig.λt.
138 mk_config sig (states sig M) (start sig M) t.
140 definition Realize ≝ λsig.λM:TM sig.λR:relation (tape sig).
142 loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc ∧
145 definition WRealize ≝ λsig.λM:TM sig.λR:relation (tape sig).
147 loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc →
150 lemma loop_eq : ∀sig,f,q,i,j,a,x,y.
151 loop sig i f q a = Some ? x → loop sig j f q a = Some ? y → x = y.
152 #sig #f #q #i #j @(nat_elim2 … i j)
153 [ #n #a #x #y normalize #Hfalse destruct (Hfalse)
154 | #n #a #x #y #H1 normalize #Hfalse destruct (Hfalse)
155 | #n1 #n2 #IH #a #x #y normalize cases (q a) normalize
161 theorem Realize_to_WRealize : ∀sig,M,R.Realize sig M R → WRealize sig M R.
162 #sig #M #R #H1 #inc #i #outc #Hloop
163 cases (H1 inc) #k * #outc1 * #Hloop1 #HR
164 >(loop_eq … Hloop Hloop1) //
167 definition accRealize ≝ λsig.λM:TM sig.λacc:states sig M.λRtrue,Rfalse:relation (tape sig).
169 loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc ∧
170 (cstate ?? outc = acc → Rtrue t (ctape ?? outc)) ∧
171 (cstate ?? outc ≠ acc → Rfalse t (ctape ?? outc)).
175 definition seq_trans ≝ λsig. λM1,M2 : TM sig.
179 if halt sig M1 s1 then 〈inr … (start sig M2), None ?〉
181 let 〈news1,m〉 ≝ trans sig M1 〈s1,a〉 in
184 let 〈news2,m〉 ≝ trans sig M2 〈s2,a〉 in
188 definition seq ≝ λsig. λM1,M2 : TM sig.
190 (FinSum (states sig M1) (states sig M2))
191 (seq_trans sig M1 M2)
192 (inl … (start sig M1))
194 [ inl _ ⇒ false | inr s2 ⇒ halt sig M2 s2]).
196 definition Rcomp ≝ λA.λR1,R2:relation A.λa1,a2.
197 ∃am.R1 a1 am ∧ R2 am a2.
200 definition injectRl ≝ λsig.λM1.λM2.λR.
202 inl … (cstate sig M1 c11) = cstate sig (seq sig M1 M2) c1 ∧
203 inl … (cstate sig M1 c12) = cstate sig (seq sig M1 M2) c2 ∧
204 ctape sig M1 c11 = ctape sig (seq sig M1 M2) c1 ∧
205 ctape sig M1 c12 = ctape sig (seq sig M1 M2) c2 ∧
208 definition injectRr ≝ λsig.λM1.λM2.λR.
210 inr … (cstate sig M2 c21) = cstate sig (seq sig M1 M2) c1 ∧
211 inr … (cstate sig M2 c22) = cstate sig (seq sig M1 M2) c2 ∧
212 ctape sig M2 c21 = ctape sig (seq sig M1 M2) c1 ∧
213 ctape sig M2 c22 = ctape sig (seq sig M1 M2) c2 ∧
216 definition Rlink ≝ λsig.λM1,M2.λc1,c2.
217 ctape sig (seq sig M1 M2) c1 = ctape sig (seq sig M1 M2) c2 ∧
218 cstate sig (seq sig M1 M2) c1 = inl … (halt sig M1) ∧
219 cstate sig (seq sig M1 M2) c2 = inr … (start sig M2). *)
221 interpretation "relation composition" 'compose R1 R2 = (Rcomp ? R1 R2).
223 definition lift_confL ≝
224 λsig,S1,S2,c.match c with
225 [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inl … s) t ].
227 definition lift_confR ≝
228 λsig,S1,S2,c.match c with
229 [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inr … s) t ].
231 definition halt_liftL ≝
232 λS1,S2,halt.λs:FinSum S1 S2.
235 | inr _ ⇒ true ]. (* should be vacuous in all cases we use halt_liftL *)
237 definition halt_liftR ≝
238 λS1,S2,halt.λs:FinSum S1 S2.
241 | inr s2 ⇒ halt s2 ].
243 lemma p_halt_liftL : ∀sig,S1,S2,halt,c.
244 halt (cstate sig S1 c) =
245 halt_liftL S1 S2 halt (cstate … (lift_confL … c)).
246 #sig #S1 #S2 #halt #c cases c #s #t %
249 lemma trans_liftL : ∀sig,M1,M2,s,a,news,move.
250 halt ? M1 s = false →
251 trans sig M1 〈s,a〉 = 〈news,move〉 →
252 trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inl … news,move〉.
253 #sig (*#M1*) * #Q1 #T1 #init1 #halt1 #M2 #s #a #news #move
254 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
257 lemma trans_liftR : ∀sig,M1,M2,s,a,news,move.
258 halt ? M2 s = false →
259 trans sig M2 〈s,a〉 = 〈news,move〉 →
260 trans sig (seq sig M1 M2) 〈inr … s,a〉 = 〈inr … news,move〉.
261 #sig #M1 * #Q2 #T2 #init2 #halt2 #s #a #news #move
262 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
267 cstate sig M c1 = cstate sig M c2 →
268 ctape sig M c1 = ctape sig M c2 → c1 = c2.
269 #sig #M1 * #s1 #t1 * #s2 #t2 //
272 lemma step_lift_confR : ∀sig,M1,M2,c0.
273 halt ? M2 (cstate ?? c0) = false →
274 step sig (seq sig M1 M2) (lift_confR sig (states ? M1) (states ? M2) c0) =
275 lift_confR sig (states ? M1) (states ? M2) (step sig M2 c0).
276 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s * #lt
278 whd in ⊢ (???(????%));whd in ⊢ (???%);
279 lapply (refl ? (trans ?? 〈s,option_hd sig rs〉))
280 cases (trans ?? 〈s,option_hd sig rs〉) in ⊢ (???% → %);
281 #s0 #m0 #Heq whd in ⊢ (???%);
282 whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
287 lemma step_lift_confL : ∀sig,M1,M2,c0.
288 halt ? M1 (cstate ?? c0) = false →
289 step sig (seq sig M1 M2) (lift_confL sig (states ? M1) (states ? M2) c0) =
290 lift_confL sig ?? (step sig M1 c0).
291 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s * #lt
293 whd in ⊢ (???(????%));whd in ⊢ (???%);
294 lapply (refl ? (trans ?? 〈s,option_hd sig rs〉))
295 cases (trans ?? 〈s,option_hd sig rs〉) in ⊢ (???% → %);
296 #s0 #m0 #Heq whd in ⊢ (???%);
297 whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
302 lemma loop_lift : ∀A,B,k,lift,f,g,h,hlift,c1,c2.
303 (∀x.hlift (lift x) = h x) →
304 (∀x.h x = false → lift (f x) = g (lift x)) →
305 loop A k f h c1 = Some ? c2 →
306 loop B k g hlift (lift c1) = Some ? (lift … c2).
307 #A #B #k #lift #f #g #h #hlift #c1 #c2 #Hfg #Hhlift
308 generalize in match c1; elim k
309 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
310 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
311 cases (true_or_false (h c0)) #Hc0 >Hfg >Hc0
312 [ normalize #Heq destruct (Heq) %
313 | normalize <Hhlift // @IH ]
317 lemma loop_liftL : ∀sig,k,M1,M2,c1,c2.
318 loop ? k (step sig M1) (λc.halt sig M1 (cstate ?? c)) c1 = Some ? c2 →
319 loop ? k (step sig (seq sig M1 M2))
320 (λc.halt_liftL ?? (halt sig M1) (cstate ?? c)) (lift_confL … c1) =
321 Some ? (lift_confL … 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 (states ? M1) c0))) #Hc0 >Hc0
327 [ >(?: halt_liftL ?? (halt sig M1) (cstate sig ? (lift_confL … c0)) = true)
328 [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
330 | >(?: halt_liftL ?? (halt sig M1) (cstate ?? (lift_confL … c0)) = false)
331 [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
336 lemma loop_liftR : ∀sig,k,M1,M2,c1,c2.
337 loop ? k (step sig M2) (λc.halt sig M2 (cstate ?? c)) c1 = Some ? c2 →
338 loop ? k (step sig (seq sig M1 M2))
339 (λc.halt sig (seq sig M1 M2) (cstate ?? c)) (lift_confR … c1) =
340 Some ? (lift_confR … c2).
341 #sig #k #M1 #M2 #c1 #c2 generalize in match c1;
343 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
344 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
345 cases (true_or_false (halt ?? (cstate sig ? c0))) #Hc0 >Hc0
346 [ >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = true)
347 [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
349 | >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = false)
350 [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
359 ∀A,k,f,p,a,b.loop A k f p a = Some ? b → p b = true.
361 [#a #b normalize #Hfalse destruct
362 |#k0 #IH #a #b whd in ⊢ (??%? → ?); cases (true_or_false (p a)) #Hpa
363 [ >Hpa normalize #H1 destruct //
369 lemma trans_liftL_true : ∀sig,M1,M2,s,a.
371 trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inr … (start ? M2),None ?〉.
373 #Hhalt whd in ⊢ (??%?); >Hhalt %
376 lemma eq_ctape_lift_conf_L : ∀sig,S1,S2,outc.
377 ctape sig (FinSum S1 S2) (lift_confL … outc) = ctape … outc.
378 #sig #S1 #S2 #outc cases outc #s #t %
381 lemma eq_ctape_lift_conf_R : ∀sig,S1,S2,outc.
382 ctape sig (FinSum S1 S2) (lift_confR … outc) = ctape … outc.
383 #sig #S1 #S2 #outc cases outc #s #t %
386 theorem sem_seq: ∀sig,M1,M2,R1,R2.
387 Realize sig M1 R1 → Realize sig M2 R2 →
388 Realize sig (seq sig M1 M2) (R1 ∘ R2).
389 #sig #M1 #M2 #R1 #R2 #HR1 #HR2 #t
390 cases (HR1 t) #k1 * #outc1 * #Hloop1 #HM1
391 cases (HR2 (ctape sig (states ? M1) outc1)) #k2 * #outc2 * #Hloop2 #HM2
392 @(ex_intro … (k1+k2)) @(ex_intro … (lift_confR … outc2))
394 [@(loop_merge ???????????
395 (loop_lift ??? (lift_confL sig (states sig M1) (states sig M2))
396 (step sig M1) (step sig (seq sig M1 M2))
397 (λc.halt sig M1 (cstate … c))
398 (λc.halt_liftL ?? (halt sig M1) (cstate … c)) … Hloop1))
400 [ #sl #tl whd in ⊢ (??%? → ?); #Hl %
401 | #sr #tr whd in ⊢ (??%? → ?); #Hr destruct (Hr) ]
402 || #c0 #Hhalt <step_lift_confL //
404 |6:cases outc1 #s1 #t1 %
405 |7:@(loop_lift … (initc ?? (ctape … outc1)) … Hloop2)
407 | #c0 #Hhalt <step_lift_confR // ]
408 |whd in ⊢ (??(???%)?);whd in ⊢ (??%?);
409 generalize in match Hloop1; cases outc1 #sc1 #tc1 #Hloop10
410 >(trans_liftL_true sig M1 M2 ??)
411 [ whd in ⊢ (??%?); whd in ⊢ (???%);
412 @config_eq whd in ⊢ (???%); //
413 | @(loop_Some ?????? Hloop10) ]
415 | @(ex_intro … (ctape ? (FinSum (states ? M1) (states ? M2)) (lift_confL … outc1)))
416 % // >eq_ctape_lift_conf_L >eq_ctape_lift_conf_R //