]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/mono.ma
porting of move_char_c
[helm.git] / matita / matita / lib / turing / mono.ma
1 (*
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.           
5     ||I||                                                            
6     ||T||  
7     ||A||  
8     \   /  This file is distributed under the terms of the       
9      \ /   GNU General Public License Version 2   
10       V_____________________________________________________________*)
11
12 include "basics/vectors.ma".
13 (* include "basics/relations.ma". *)
14
15 (*
16 record tape (sig:FinSet): Type[0] ≝ 
17 { left : list (option sig);
18   right: list (option sig)
19 }.
20 *)
21
22 inductive tape (sig:FinSet) : Type[0] ≝ 
23 | niltape : tape sig
24 | leftof  : sig → list sig → tape sig
25 | rightof : sig → list sig → tape sig
26 | midtape : list sig → sig → list sig → tape sig.
27
28 definition left ≝ 
29  λsig.λt:tape sig.match t with
30  [ niltape ⇒ [] 
31  | leftof _ _ ⇒ [] 
32  | rightof s l ⇒ s::l
33  | midtape l _ _ ⇒ l ].
34
35 definition right ≝ 
36  λsig.λt:tape sig.match t with
37  [ niltape ⇒ [] 
38  | leftof s r ⇒ s::r 
39  | rightof _ _ ⇒ []
40  | midtape _ _ r ⇒ r ].
41  
42 definition current ≝ 
43  λsig.λt:tape sig.match t with
44  [ midtape _ c _ ⇒ Some ? c
45  | _ ⇒ None ? ].
46  
47 definition mk_tape : 
48   ∀sig:FinSet.list sig → option sig → list sig → tape sig ≝ 
49   λsig,lt,c,rt.match c with
50   [ Some c' ⇒ midtape sig lt c' rt
51   | None ⇒ match lt with 
52     [ nil ⇒ match rt with
53       [ nil ⇒ niltape ?
54       | cons r0 rs0 ⇒ leftof ? r0 rs0 ]
55     | cons l0 ls0 ⇒ rightof ? l0 ls0 ] ].
56
57 inductive move : Type[0] ≝
58 | L : move 
59 | R : move
60 | N : move
61 .
62
63 (* We do not distinuish an input tape *)
64
65 record TM (sig:FinSet): Type[1] ≝ 
66 { states : FinSet;
67   trans : states × (option sig) → states × (option (sig × move));
68   start: states;
69   halt : states → bool
70 }.
71
72 record config (sig,states:FinSet): Type[0] ≝ 
73 { cstate : states;
74   ctape: tape sig
75 }.
76
77 (* definition option_hd ≝ λA.λl:list (option A).
78   match l with
79   [nil ⇒ None ?
80   |cons a _ ⇒ a
81   ].
82   *)
83
84 (*definition tape_write ≝ λsig.λt:tape sig.λs:sig.
85   <left ? t) s (right ? t).
86   [ None ⇒ t
87   | Some s' ⇒ midtape ? (left ? t) s' (right ? t) ].*)
88   
89 definition tape_move_left ≝ λsig:FinSet.λlt:list sig.λc:sig.λrt:list sig.
90   match lt with
91   [ nil ⇒ leftof sig c rt
92   | cons c0 lt0 ⇒ midtape sig lt0 c0 (c::rt) ].
93   
94 definition tape_move_right ≝ λsig:FinSet.λlt:list sig.λc:sig.λrt:list sig.
95   match rt with
96   [ nil ⇒ rightof sig c lt
97   | cons c0 rt0 ⇒ midtape sig (c::lt) c0 rt0 ].
98
99 definition tape_move ≝ λsig.λt: tape sig.λm:option (sig × move).
100   match m with
101   [ None ⇒ t
102   | Some m' ⇒ 
103     let 〈s,m1〉 ≝ m' in 
104     match m1 with
105       [ R ⇒ tape_move_right ? (left ? t) s (right ? t)
106       | L ⇒ tape_move_left ? (left ? t) s (right ? t)
107       | N ⇒ midtape ? (left ? t) s (right ? t)
108       ] ].
109 (*
110   (None,[]) → □
111   (None,a::[]) → □
112   (None,a::b::rs) → None::b::rs
113   (Some a,[]) → [Some a]
114   (Some a,b::rs) → Some a::rs
115   *)
116 (*
117 definition option_cons ≝ λA.λa:option A.λl.
118   match a with
119   [ None ⇒ match l with
120     [ nil ⇒ []
121     | cons _ _ ⇒ a::l ]
122   | Some _ ⇒ a::l ].
123   
124 (* definition tape_update := λsig.λt: tape sig.λs:option sig.
125   let newright ≝ 
126     match right ? t with
127     [ nil ⇒ match s with
128       [ None ⇒ [] 
129       | Some a ⇒ [Some ? a] ]
130     | cons b rs ⇒ match s with
131       [ None ⇒ match rs with
132         [ nil ⇒ [] 
133         | cons _ _ ⇒ None ?::rs ]
134       | Some a ⇒ Some ? a::rs ] ]
135   in mk_tape ? (left ? t) newright. *)
136   
137 definition tape_move ≝ λsig.λt:tape sig.λm:option sig × move.
138   let 〈s,m1〉 ≝ m in match m1 with
139     [ R ⇒ mk_tape sig (option_cons ? s (left ? t)) (tail ? (right ? t))
140     | L ⇒ mk_tape sig (tail ? (left ? t)) 
141            (option_cons ? (option_hd ? (left ? t))
142              (option_cons ? s (tail ? (right ? t))))
143     | N ⇒ mk_tape sig (left ? t) (option_cons ? s (tail ? (right ? t)))
144     ].
145 *)
146   
147 definition step ≝ λsig.λM:TM sig.λc:config sig (states sig M).
148   let current_char ≝ current ? (ctape ?? c) in
149   let 〈news,mv〉 ≝ trans sig M 〈cstate ?? c,current_char〉 in
150   mk_config ?? news (tape_move sig (ctape ?? c) mv).
151   
152 let rec loop (A:Type[0]) n (f:A→A) p a on n ≝
153   match n with 
154   [ O ⇒ None ?
155   | S m ⇒ if p a then (Some ? a) else loop A m f p (f a)
156   ].
157   
158 lemma loop_S_true : 
159   ∀A,n,f,p,a.  p a = true → 
160   loop A (S n) f p a = Some ? a.
161 #A #n #f #p #a #pa normalize >pa //
162 qed.
163
164 lemma loop_S_false : 
165   ∀A,n,f,p,a.  p a = false → 
166   loop A (S n) f p a = loop A n f p (f a).
167 normalize #A #n #f #p #a #Hpa >Hpa %
168 qed.  
169   
170 lemma loop_incr : ∀A,f,p,k1,k2,a1,a2. 
171   loop A k1 f p a1 = Some ? a2 → 
172     loop A (k2+k1) f p a1 = Some ? a2.
173 #A #f #p #k1 #k2 #a1 #a2 generalize in match a1; elim k1
174 [normalize #a0 #Hfalse destruct
175 |#k1' #IH #a0 <plus_n_Sm whd in ⊢ (??%? → ??%?);
176  cases (true_or_false (p a0)) #Hpa0 >Hpa0 whd in ⊢ (??%? → ??%?); // @IH
177 ]
178 qed.
179
180 lemma loop_merge : ∀A,f,p,q.(∀b. p b = false → q b = false) →
181  ∀k1,k2,a1,a2,a3,a4.
182    loop A k1 f p a1 = Some ? a2 → 
183      f a2 = a3 → q a2 = false → 
184        loop A k2 f q a3 = Some ? a4 →
185          loop A (k1+k2) f q a1 = Some ? a4.
186 #Sig #f #p #q #Hpq #k1 elim k1 
187   [normalize #k2 #a1 #a2 #a3 #a4 #H destruct
188   |#k1' #Hind #k2 #a1 #a2 #a3 #a4 normalize in ⊢ (%→?);
189    cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
190    [#eqa1a2 destruct #eqa2a3 #Hqa2 #H
191     whd in ⊢ (??(??%???)?); >plus_n_Sm @loop_incr
192     whd in ⊢ (??%?); >Hqa2 >eqa2a3 @H
193    |normalize >(Hpq … pa1) normalize 
194     #H1 #H2 #H3 @(Hind … H2) //
195    ]
196  ]
197 qed.
198
199 lemma loop_split : ∀A,f,p,q.(∀b. q b = true → p b = true) →
200  ∀k,a1,a2.
201    loop A k f q a1 = Some ? a2 → 
202    ∃k1,a3.
203     loop A k1 f p a1 = Some ? a3 ∧ 
204       loop A (S(k-k1)) f q a3 = Some ? a2.
205 #A #f #p #q #Hpq #k elim k
206   [#a1 #a2 normalize #Heq destruct
207   |#i #Hind #a1 #a2 normalize 
208    cases (true_or_false (q a1)) #Hqa1 >Hqa1 normalize
209     [ #Ha1a2 destruct
210      @(ex_intro … 1) @(ex_intro … a2) % 
211        [normalize >(Hpq …Hqa1) // |>Hqa1 //]
212     |#Hloop cases (true_or_false (p a1)) #Hpa1 
213        [@(ex_intro … 1) @(ex_intro … a1) % 
214          [normalize >Hpa1 // |>Hqa1 <Hloop normalize //]
215        |cases (Hind …Hloop) #k2 * #a3 * #Hloop1 #Hloop2
216         @(ex_intro … (S k2)) @(ex_intro … a3) %
217          [normalize >Hpa1 normalize // | @Hloop2 ]
218        ]
219     ]
220   ]
221 qed.
222
223 (*
224 lemma loop_split : ∀A,f,p,q.(∀b. p b = false → q b = false) →
225  ∀k1,k2,a1,a2,a3.
226    loop A k1 f p a1 = Some ? a2 → 
227      loop A k2 f q a2 = Some ? a3 →
228        loop A (k1+k2) f q a1 = Some ? a3.
229 #Sig #f #p #q #Hpq #k1 elim k1 
230   [normalize #k2 #a1 #a2 #a3 #H destruct
231   |#k1' #Hind #k2 #a1 #a2 #a3 normalize in ⊢ (%→?→?);
232    cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
233    [#eqa1a2 destruct #H @loop_incr //
234    |normalize >(Hpq … pa1) normalize 
235     #H1 #H2 @(Hind … H2) //
236    ]
237  ]
238 qed.
239 *)
240
241 definition initc ≝ λsig.λM:TM sig.λt.
242   mk_config sig (states sig M) (start sig M) t.
243
244 definition Realize ≝ λsig.λM:TM sig.λR:relation (tape sig).
245 ∀t.∃i.∃outc.
246   loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc ∧
247   R t (ctape ?? outc).
248
249 definition WRealize ≝ λsig.λM:TM sig.λR:relation (tape sig).
250 ∀t,i,outc.
251   loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc → 
252   R t (ctape ?? outc).
253
254 definition Terminate ≝ λsig.λM:TM sig.λt. ∃i,outc.
255   loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc.
256
257 lemma WRealize_to_Realize : ∀sig.∀M: TM sig.∀R.
258   (∀t.Terminate sig M t) → WRealize sig M R → Realize sig M R.
259 #sig #M #R #HT #HW #t cases (HT … t) #i * #outc #Hloop 
260 @(ex_intro … i) @(ex_intro … outc) % // @(HW … i) //
261 qed.
262
263 lemma loop_eq : ∀sig,f,q,i,j,a,x,y. 
264   loop sig i f q a = Some ? x → loop sig j f q a = Some ? y → x = y.
265 #sig #f #q #i #j @(nat_elim2 … i j)
266 [ #n #a #x #y normalize #Hfalse destruct (Hfalse)
267 | #n #a #x #y #H1 normalize #Hfalse destruct (Hfalse)
268 | #n1 #n2 #IH #a #x #y normalize cases (q a) normalize
269   [ #H1 #H2 destruct %
270   | /2/ ]
271 ]
272 qed.
273
274 theorem Realize_to_WRealize : ∀sig,M,R.Realize sig M R → WRealize sig M R.
275 #sig #M #R #H1 #inc #i #outc #Hloop
276 cases (H1 inc) #k * #outc1 * #Hloop1 #HR
277 >(loop_eq … Hloop Hloop1) //
278 qed.
279
280 definition accRealize ≝ λsig.λM:TM sig.λacc:states sig M.λRtrue,Rfalse:relation (tape sig).
281 ∀t.∃i.∃outc.
282   loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc ∧
283   (cstate ?? outc = acc → Rtrue t (ctape ?? outc)) ∧ 
284   (cstate ?? outc ≠ acc → Rfalse t (ctape ?? outc)).
285
286 (* NO OPERATION
287
288   t1 = t2
289   *)
290   
291 definition nop_states ≝ initN 1.
292 definition start_nop : initN 1 ≝ mk_Sig ?? 0 (le_n … (S 0)).
293
294 definition nop ≝ 
295   λalpha:FinSet.mk_TM alpha nop_states
296   (λp.let 〈q,a〉 ≝ p in 〈q,None ?〉)
297   start_nop (λ_.true).
298   
299 definition R_nop ≝ λalpha.λt1,t2:tape alpha.t2 = t1.
300
301 lemma sem_nop :
302   ∀alpha.Realize alpha (nop alpha) (R_nop alpha).
303 #alpha #intape @(ex_intro ?? 1) 
304 @(ex_intro … (mk_config ?? start_nop intape)) % % 
305 qed.
306
307 (* Compositions *)
308
309 definition seq_trans ≝ λsig. λM1,M2 : TM sig. 
310 λp. let 〈s,a〉 ≝ p in
311   match s with 
312   [ inl s1 ⇒ 
313       if halt sig M1 s1 then 〈inr … (start sig M2), None ?〉
314       else 
315       let 〈news1,m〉 ≝ trans sig M1 〈s1,a〉 in
316       〈inl … news1,m〉
317   | inr s2 ⇒ 
318       let 〈news2,m〉 ≝ trans sig M2 〈s2,a〉 in
319       〈inr … news2,m〉
320   ].
321  
322 definition seq ≝ λsig. λM1,M2 : TM sig. 
323   mk_TM sig 
324     (FinSum (states sig M1) (states sig M2))
325     (seq_trans sig M1 M2) 
326     (inl … (start sig M1))
327     (λs.match s with
328       [ inl _ ⇒ false | inr s2 ⇒ halt sig M2 s2]). 
329
330 definition Rcomp ≝ λA.λR1,R2:relation A.λa1,a2.
331   ∃am.R1 a1 am ∧ R2 am a2.
332
333 (*
334 definition injectRl ≝ λsig.λM1.λM2.λR.
335    λc1,c2. ∃c11,c12. 
336      inl … (cstate sig M1 c11) = cstate sig (seq sig M1 M2) c1 ∧ 
337      inl … (cstate sig M1 c12) = cstate sig (seq sig M1 M2) c2 ∧
338      ctape sig M1 c11 = ctape sig (seq sig M1 M2) c1 ∧ 
339      ctape sig M1 c12 = ctape sig (seq sig M1 M2) c2 ∧ 
340      R c11 c12.
341
342 definition injectRr ≝ λsig.λM1.λM2.λR.
343    λc1,c2. ∃c21,c22. 
344      inr … (cstate sig M2 c21) = cstate sig (seq sig M1 M2) c1 ∧ 
345      inr … (cstate sig M2 c22) = cstate sig (seq sig M1 M2) c2 ∧
346      ctape sig M2 c21 = ctape sig (seq sig M1 M2) c1 ∧ 
347      ctape sig M2 c22 = ctape sig (seq sig M1 M2) c2 ∧ 
348      R c21 c22.
349      
350 definition Rlink ≝ λsig.λM1,M2.λc1,c2.
351   ctape sig (seq sig M1 M2) c1 = ctape sig (seq sig M1 M2) c2 ∧
352   cstate sig (seq sig M1 M2) c1 = inl … (halt sig M1) ∧
353   cstate sig (seq sig M1 M2) c2 = inr … (start sig M2). *)
354   
355 interpretation "relation composition" 'compose R1 R2 = (Rcomp ? R1 R2).
356
357 definition lift_confL ≝ 
358   λsig,S1,S2,c.match c with 
359   [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inl … s) t ].
360   
361 definition lift_confR ≝ 
362   λsig,S1,S2,c.match c with
363   [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inr … s) t ].
364   
365 definition halt_liftL ≝ 
366   λS1,S2,halt.λs:FinSum S1 S2.
367   match s with
368   [ inl s1 ⇒ halt s1
369   | inr _ ⇒ true ]. (* should be vacuous in all cases we use halt_liftL *)
370
371 definition halt_liftR ≝ 
372   λS1,S2,halt.λs:FinSum S1 S2.
373   match s with
374   [ inl _ ⇒ false 
375   | inr s2 ⇒ halt s2 ].
376       
377 lemma p_halt_liftL : ∀sig,S1,S2,halt,c.
378   halt (cstate sig S1 c) =
379      halt_liftL S1 S2 halt (cstate … (lift_confL … c)).
380 #sig #S1 #S2 #halt #c cases c #s #t %
381 qed.
382
383 lemma trans_liftL : ∀sig,M1,M2,s,a,news,move.
384   halt ? M1 s = false → 
385   trans sig M1 〈s,a〉 = 〈news,move〉 → 
386   trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inl … news,move〉.
387 #sig (*#M1*) * #Q1 #T1 #init1 #halt1 #M2 #s #a #news #move
388 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
389 qed.
390
391 lemma trans_liftR : ∀sig,M1,M2,s,a,news,move.
392   halt ? M2 s = false → 
393   trans sig M2 〈s,a〉 = 〈news,move〉 → 
394   trans sig (seq sig M1 M2) 〈inr … s,a〉 = 〈inr … news,move〉.
395 #sig #M1 * #Q2 #T2 #init2 #halt2 #s #a #news #move
396 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
397 qed.
398
399 lemma config_eq : 
400   ∀sig,M,c1,c2.
401   cstate sig M c1 = cstate sig M c2 → 
402   ctape sig M c1 = ctape sig M c2 →  c1 = c2.
403 #sig #M1 * #s1 #t1 * #s2 #t2 //
404 qed.
405
406 lemma step_lift_confR : ∀sig,M1,M2,c0.
407  halt ? M2 (cstate ?? c0) = false → 
408  step sig (seq sig M1 M2) (lift_confR sig (states ? M1) (states ? M2) c0) =
409  lift_confR sig (states ? M1) (states ? M2) (step sig M2 c0).
410 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s #t
411   lapply (refl ? (trans ?? 〈s,current sig t〉))
412   cases (trans ?? 〈s,current sig t〉) in ⊢ (???% → %);
413   #s0 #m0 cases t
414   [ #Heq #Hhalt
415   | 2,3: #s1 #l1 #Heq #Hhalt 
416   |#ls #s1 #rs #Heq #Hhalt ]
417   whd in ⊢ (???(????%)); >Heq
418   whd in ⊢ (???%);
419   whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
420   >(trans_liftR … Heq) //
421 qed.
422
423 lemma step_lift_confL : ∀sig,M1,M2,c0.
424  halt ? M1 (cstate ?? c0) = false → 
425  step sig (seq sig M1 M2) (lift_confL sig (states ? M1) (states ? M2) c0) =
426  lift_confL sig ?? (step sig M1 c0).
427 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s #t
428   lapply (refl ? (trans ?? 〈s,current sig t〉))
429   cases (trans ?? 〈s,current sig t〉) in ⊢ (???% → %);
430   #s0 #m0 cases t
431   [ #Heq #Hhalt
432   | 2,3: #s1 #l1 #Heq #Hhalt 
433   |#ls #s1 #rs #Heq #Hhalt ]
434   whd in ⊢ (???(????%)); >Heq
435   whd in ⊢ (???%);
436   whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
437   >(trans_liftL … Heq) //
438 qed.
439
440 lemma loop_lift : ∀A,B,k,lift,f,g,h,hlift,c1,c2.
441   (∀x.hlift (lift x) = h x) → 
442   (∀x.h x = false → lift (f x) = g (lift x)) → 
443   loop A k f h c1 = Some ? c2 → 
444   loop B k g hlift (lift c1) = Some ? (lift … c2).
445 #A #B #k #lift #f #g #h #hlift #c1 #c2 #Hfg #Hhlift
446 generalize in match c1; elim k
447 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
448 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
449  cases (true_or_false (h c0)) #Hc0 >Hfg >Hc0
450  [ normalize #Heq destruct (Heq) %
451  | normalize <Hhlift // @IH ]
452 qed.
453
454 (* 
455 lemma loop_liftL : ∀sig,k,M1,M2,c1,c2.
456   loop ? k (step sig M1) (λc.halt sig M1 (cstate ?? c)) c1 = Some ? c2 →
457     loop ? k (step sig (seq sig M1 M2)) 
458       (λc.halt_liftL ?? (halt sig M1) (cstate ?? c)) (lift_confL … c1) = 
459     Some ? (lift_confL … c2).
460 #sig #k #M1 #M2 #c1 #c2 generalize in match c1;
461 elim k
462 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
463 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
464  cases (true_or_false (halt ?? (cstate sig (states ? M1) c0))) #Hc0 >Hc0
465  [ >(?: halt_liftL ?? (halt sig M1) (cstate sig ? (lift_confL … c0)) = true)
466    [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
467    | <Hc0 cases c0 // ]
468  | >(?: halt_liftL ?? (halt sig M1) (cstate ?? (lift_confL … c0)) = false)
469    [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
470     @step_lift_confL //
471    | <Hc0 cases c0 // ]
472 qed.
473
474 lemma loop_liftR : ∀sig,k,M1,M2,c1,c2.
475   loop ? k (step sig M2) (λc.halt sig M2 (cstate ?? c)) c1 = Some ? c2 →
476     loop ? k (step sig (seq sig M1 M2)) 
477       (λc.halt sig (seq sig M1 M2) (cstate ?? c)) (lift_confR … c1) = 
478     Some ? (lift_confR … c2).
479 #sig #k #M1 #M2 #c1 #c2 generalize in match c1;
480 elim k
481 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
482 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
483  cases (true_or_false (halt ?? (cstate sig ? c0))) #Hc0 >Hc0
484  [ >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = true)
485    [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
486    | <Hc0 cases c0 // ]
487  | >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = false)
488    [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
489     @step_lift_confR //
490    | <Hc0 cases c0 // ]
491  ]
492 qed.  
493
494 *)
495     
496 lemma loop_Some : 
497   ∀A,k,f,p,a,b.loop A k f p a = Some ? b → p b = true.
498 #A #k #f #p elim k 
499 [#a #b normalize #Hfalse destruct
500 |#k0 #IH #a #b whd in ⊢ (??%? → ?); cases (true_or_false (p a)) #Hpa
501  [ >Hpa normalize #H1 destruct //
502  | >Hpa normalize @IH
503  ]
504 ]
505 qed. 
506
507 lemma trans_liftL_true : ∀sig,M1,M2,s,a.
508   halt ? M1 s = true → 
509   trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inr … (start ? M2),None ?〉.
510 #sig #M1 #M2 #s #a
511 #Hhalt whd in ⊢ (??%?); >Hhalt %
512 qed.
513
514 lemma eq_ctape_lift_conf_L : ∀sig,S1,S2,outc.
515   ctape sig (FinSum S1 S2) (lift_confL … outc) = ctape … outc.
516 #sig #S1 #S2 #outc cases outc #s #t %
517 qed.
518   
519 lemma eq_ctape_lift_conf_R : ∀sig,S1,S2,outc.
520   ctape sig (FinSum S1 S2) (lift_confR … outc) = ctape … outc.
521 #sig #S1 #S2 #outc cases outc #s #t %
522 qed.
523
524 theorem sem_seq: ∀sig,M1,M2,R1,R2.
525   Realize sig M1 R1 → Realize sig M2 R2 → 
526     Realize sig (seq sig M1 M2) (R1 ∘ R2).
527 #sig #M1 #M2 #R1 #R2 #HR1 #HR2 #t 
528 cases (HR1 t) #k1 * #outc1 * #Hloop1 #HM1
529 cases (HR2 (ctape sig (states ? M1) outc1)) #k2 * #outc2 * #Hloop2 #HM2
530 @(ex_intro … (k1+k2)) @(ex_intro … (lift_confR … outc2))
531 %
532 [@(loop_merge ??????????? 
533    (loop_lift ??? (lift_confL sig (states sig M1) (states sig M2))
534    (step sig M1) (step sig (seq sig M1 M2)) 
535    (λc.halt sig M1 (cstate … c)) 
536    (λc.halt_liftL ?? (halt sig M1) (cstate … c)) … Hloop1))
537   [ * *
538    [ #sl #tl whd in ⊢ (??%? → ?); #Hl %
539    | #sr #tr whd in ⊢ (??%? → ?); #Hr destruct (Hr) ]
540   || #c0 #Hhalt <step_lift_confL //
541   | #x <p_halt_liftL %
542   |6:cases outc1 #s1 #t1 %
543   |7:@(loop_lift … (initc ?? (ctape … outc1)) … Hloop2) 
544     [ * #s2 #t2 %
545     | #c0 #Hhalt <step_lift_confR // ]
546   |whd in ⊢ (??(???%)?);whd in ⊢ (??%?);
547    generalize in match Hloop1; cases outc1 #sc1 #tc1 #Hloop10 
548    >(trans_liftL_true sig M1 M2 ??) 
549     [ whd in ⊢ (??%?); whd in ⊢ (???%);
550       @config_eq whd in ⊢ (???%); //
551     | @(loop_Some ?????? Hloop10) ]
552  ]
553 | @(ex_intro … (ctape ? (FinSum (states ? M1) (states ? M2)) (lift_confL … outc1)))
554   % // >eq_ctape_lift_conf_L >eq_ctape_lift_conf_R //
555 ]
556 qed.
557