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. 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) //
96 lemma loop_split : ∀A,f,p,q.(∀b. p b = false → q b = false) →
98 loop A k1 f p a1 = Some ? a2 →
99 loop A k2 f q a2 = Some ? a3 →
100 loop A (k1+k2) f q a1 = Some ? a3.
101 #Sig #f #p #q #Hpq #k1 elim k1
102 [normalize #k2 #a1 #a2 #a3 #H destruct
103 |#k1' #Hind #k2 #a1 #a2 #a3 normalize in ⊢ (%→?→?);
104 cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
105 [#eqa1a2 destruct #H @loop_incr //
106 |normalize >(Hpq … pa1) normalize
107 #H1 #H2 @(Hind … H2) //
113 definition initc ≝ λsig.λM:TM sig.λt.
114 mk_config sig (states sig M) (start sig M) t.
116 definition Realize ≝ λsig.λM:TM sig.λR:relation (tape sig).
118 loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc ∧
122 definition accRealize ≝ λsig.λM:TM sig.λacc:states sig M.λRtrue,Rfalse:relation (tape sig).
124 loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc ∧
125 (cstate ?? outc = acc → Rtrue t (ctape ?? outc)) ∧
126 (cstate ?? outc ≠ acc → Rfalse t (ctape ?? outc)).
130 definition seq_trans ≝ λsig. λM1,M2 : TM sig.
134 if halt sig M1 s1 then 〈inr … (start sig M2), None ?〉
136 let 〈news1,m〉 ≝ trans sig M1 〈s1,a〉 in
139 let 〈news2,m〉 ≝ trans sig M2 〈s2,a〉 in
143 definition seq ≝ λsig. λM1,M2 : TM sig.
145 (FinSum (states sig M1) (states sig M2))
146 (seq_trans sig M1 M2)
147 (inl … (start sig M1))
149 [ inl _ ⇒ false | inr s2 ⇒ halt sig M2 s2]).
151 definition Rcomp ≝ λA.λR1,R2:relation A.λa1,a2.
152 ∃am.R1 a1 am ∧ R2 am a2.
155 definition injectRl ≝ λsig.λM1.λM2.λR.
157 inl … (cstate sig M1 c11) = cstate sig (seq sig M1 M2) c1 ∧
158 inl … (cstate sig M1 c12) = cstate sig (seq sig M1 M2) c2 ∧
159 ctape sig M1 c11 = ctape sig (seq sig M1 M2) c1 ∧
160 ctape sig M1 c12 = ctape sig (seq sig M1 M2) c2 ∧
163 definition injectRr ≝ λsig.λM1.λM2.λR.
165 inr … (cstate sig M2 c21) = cstate sig (seq sig M1 M2) c1 ∧
166 inr … (cstate sig M2 c22) = cstate sig (seq sig M1 M2) c2 ∧
167 ctape sig M2 c21 = ctape sig (seq sig M1 M2) c1 ∧
168 ctape sig M2 c22 = ctape sig (seq sig M1 M2) c2 ∧
171 definition Rlink ≝ λsig.λM1,M2.λc1,c2.
172 ctape sig (seq sig M1 M2) c1 = ctape sig (seq sig M1 M2) c2 ∧
173 cstate sig (seq sig M1 M2) c1 = inl … (halt sig M1) ∧
174 cstate sig (seq sig M1 M2) c2 = inr … (start sig M2). *)
176 interpretation "relation composition" 'compose R1 R2 = (Rcomp ? R1 R2).
178 definition lift_confL ≝
179 λsig,S1,S2,c.match c with
180 [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inl … s) t ].
182 definition lift_confR ≝
183 λsig,S1,S2,c.match c with
184 [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inr … s) t ].
186 definition halt_liftL ≝
187 λS1,S2,halt.λs:FinSum S1 S2.
190 | inr _ ⇒ true ]. (* should be vacuous in all cases we use halt_liftL *)
192 definition halt_liftR ≝
193 λS1,S2,halt.λs:FinSum S1 S2.
196 | inr s2 ⇒ halt s2 ].
198 lemma p_halt_liftL : ∀sig,S1,S2,halt,c.
199 halt (cstate sig S1 c) =
200 halt_liftL S1 S2 halt (cstate … (lift_confL … c)).
201 #sig #S1 #S2 #halt #c cases c #s #t %
204 lemma trans_liftL : ∀sig,M1,M2,s,a,news,move.
205 halt ? M1 s = false →
206 trans sig M1 〈s,a〉 = 〈news,move〉 →
207 trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inl … news,move〉.
208 #sig (*#M1*) * #Q1 #T1 #init1 #halt1 #M2 #s #a #news #move
209 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
212 lemma trans_liftR : ∀sig,M1,M2,s,a,news,move.
213 halt ? M2 s = false →
214 trans sig M2 〈s,a〉 = 〈news,move〉 →
215 trans sig (seq sig M1 M2) 〈inr … s,a〉 = 〈inr … news,move〉.
216 #sig #M1 * #Q2 #T2 #init2 #halt2 #s #a #news #move
217 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
222 cstate sig M c1 = cstate sig M c2 →
223 ctape sig M c1 = ctape sig M c2 → c1 = c2.
224 #sig #M1 * #s1 #t1 * #s2 #t2 //
227 lemma step_lift_confR : ∀sig,M1,M2,c0.
228 halt ? M2 (cstate ?? c0) = false →
229 step sig (seq sig M1 M2) (lift_confR sig (states ? M1) (states ? M2) c0) =
230 lift_confR sig (states ? M1) (states ? M2) (step sig M2 c0).
231 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s * #lt
233 whd in ⊢ (???(????%));whd in ⊢ (???%);
234 lapply (refl ? (trans ?? 〈s,option_hd sig rs〉))
235 cases (trans ?? 〈s,option_hd sig rs〉) in ⊢ (???% → %);
236 #s0 #m0 #Heq whd in ⊢ (???%);
237 whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
242 lemma step_lift_confL : ∀sig,M1,M2,c0.
243 halt ? M1 (cstate ?? c0) = false →
244 step sig (seq sig M1 M2) (lift_confL sig (states ? M1) (states ? M2) c0) =
245 lift_confL sig ?? (step sig M1 c0).
246 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s * #lt
248 whd in ⊢ (???(????%));whd in ⊢ (???%);
249 lapply (refl ? (trans ?? 〈s,option_hd sig rs〉))
250 cases (trans ?? 〈s,option_hd sig rs〉) in ⊢ (???% → %);
251 #s0 #m0 #Heq whd in ⊢ (???%);
252 whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
257 lemma loop_liftL : ∀sig,k,M1,M2,c1,c2.
258 loop ? k (step sig M1) (λc.halt sig M1 (cstate ?? c)) c1 = Some ? c2 →
259 loop ? k (step sig (seq sig M1 M2))
260 (λc.halt_liftL ?? (halt sig M1) (cstate ?? c)) (lift_confL … c1) =
261 Some ? (lift_confL … c2).
262 #sig #k #M1 #M2 #c1 #c2 generalize in match c1;
264 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
265 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
266 cases (true_or_false (halt ?? (cstate sig (states ? M1) c0))) #Hc0 >Hc0
267 [ >(?: halt_liftL ?? (halt sig M1) (cstate sig ? (lift_confL … c0)) = true)
268 [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
270 | >(?: halt_liftL ?? (halt sig M1) (cstate ?? (lift_confL … c0)) = false)
271 [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
276 lemma loop_liftR : ∀sig,k,M1,M2,c1,c2.
277 loop ? k (step sig M2) (λc.halt sig M2 (cstate ?? c)) c1 = Some ? c2 →
278 loop ? k (step sig (seq sig M1 M2))
279 (λc.halt sig (seq sig M1 M2) (cstate ?? c)) (lift_confR … c1) =
280 Some ? (lift_confR … c2).
281 #sig #k #M1 #M2 #c1 #c2 generalize in match c1;
283 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
284 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
285 cases (true_or_false (halt ?? (cstate sig ? c0))) #Hc0 >Hc0
286 [ >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = true)
287 [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
289 | >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = false)
290 [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
297 ∀A,k,f,p,a,b.loop A k f p a = Some ? b → p b = true.
299 [#a #b normalize #Hfalse destruct
300 |#k0 #IH #a #b whd in ⊢ (??%? → ?); cases (true_or_false (p a)) #Hpa
301 [ >Hpa normalize #H1 destruct //
307 lemma trans_liftL_true : ∀sig,M1,M2,s,a.
309 trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inr … (start ? M2),None ?〉.
311 #Hhalt whd in ⊢ (??%?); >Hhalt %
314 lemma eq_ctape_lift_conf_L : ∀sig,S1,S2,outc.
315 ctape sig (FinSum S1 S2) (lift_confL … outc) = ctape … outc.
316 #sig #S1 #S2 #outc cases outc #s #t %
319 lemma eq_ctape_lift_conf_R : ∀sig,S1,S2,outc.
320 ctape sig (FinSum S1 S2) (lift_confR … outc) = ctape … outc.
321 #sig #S1 #S2 #outc cases outc #s #t %
324 theorem sem_seq: ∀sig,M1,M2,R1,R2.
325 Realize sig M1 R1 → Realize sig M2 R2 →
326 Realize sig (seq sig M1 M2) (R1 ∘ R2).
327 #sig #M1 #M2 #R1 #R2 #HR1 #HR2 #t
328 cases (HR1 t) #k1 * #outc1 * #Hloop1 #HM1
329 cases (HR2 (ctape sig (states ? M1) outc1)) #k2 * #outc2 * #Hloop2 #HM2
330 @(ex_intro … (k1+k2)) @(ex_intro … (lift_confR … outc2))
332 [@(loop_split ??????????? (loop_liftL … Hloop1))
334 [ #sl #tl whd in ⊢ (??%? → ?); #Hl %
335 | #sr #tr whd in ⊢ (??%? → ?); #Hr destruct (Hr) ]
336 ||4:cases outc1 #s1 #t1 %
337 |5:@(loop_liftR … Hloop2)
338 |whd in ⊢ (??(???%)?);whd in ⊢ (??%?);
339 generalize in match Hloop1; cases outc1 #sc1 #tc1 #Hloop10
340 >(trans_liftL_true sig M1 M2 ??)
341 [ whd in ⊢ (??%?); whd in ⊢ (???%);
343 | @(loop_Some ?????? Hloop10) ]
345 | @(ex_intro … (ctape ? (FinSum (states ? M1) (states ? M2)) (lift_confL … outc1)))
346 % // >eq_ctape_lift_conf_L >eq_ctape_lift_conf_R //