]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/mono.ma
748f08612442cc6e8aa5a240b2037f14e20e51c7
[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 (******************************** tape ****************************************)
16
17 (* A tape is essentially a triple 〈left,current,right〉 where however the current 
18 symbol could be missing. This may happen for three different reasons: both tapes 
19 are empty; we are on the left extremity of a non-empty tape (left overflow), or 
20 we are on the right extremity of a non-empty tape (right overflow). *)
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 ⇒ [] | leftof _ _ ⇒ [] | rightof s l ⇒ s::l | midtape l _ _ ⇒ l ].
31
32 definition right ≝ 
33  λsig.λt:tape sig.match t with
34  [ niltape ⇒ [] | leftof s r ⇒ s::r | rightof _ _ ⇒ []| midtape _ _ r ⇒ r ].
35  
36 definition current ≝ 
37  λsig.λt:tape sig.match t with
38  [ midtape _ c _ ⇒ Some ? c | _ ⇒ None ? ].
39  
40 definition mk_tape : 
41   ∀sig:FinSet.list sig → option sig → list sig → tape sig ≝ 
42   λsig,lt,c,rt.match c with
43   [ Some c' ⇒ midtape sig lt c' rt
44   | None ⇒ match lt with 
45     [ nil ⇒ match rt with
46       [ nil ⇒ niltape ?
47       | cons r0 rs0 ⇒ leftof ? r0 rs0 ]
48     | cons l0 ls0 ⇒ rightof ? l0 ls0 ] ].
49
50 lemma right_mk_tape : 
51   ∀sig,ls,c,rs.(c = None ? → ls = [ ] ∨ rs = [ ]) → right ? (mk_tape sig ls c rs) = rs.
52 #sig #ls #c #rs cases c // cases ls 
53 [ cases rs // 
54 | #l0 #ls0 #H normalize cases (H (refl ??)) #H1 [ destruct (H1) | >H1 % ] ]
55 qed-.
56
57 lemma left_mk_tape : ∀sig,ls,c,rs.left ? (mk_tape sig ls c rs) = ls.
58 #sig #ls #c #rs cases c // cases ls // cases rs //
59 qed.
60
61 lemma current_mk_tape : ∀sig,ls,c,rs.current ? (mk_tape sig ls c rs) = c.
62 #sig #ls #c #rs cases c // cases ls // cases rs //
63 qed.
64
65 lemma current_to_midtape: ∀sig,t,c. current sig t = Some ? c →
66   ∃ls,rs. t = midtape ? ls c rs.
67 #sig *
68   [#c whd in ⊢ ((??%?)→?); #Hfalse destruct
69   |#a #l #c whd in ⊢ ((??%?)→?); #Hfalse destruct
70   |#a #l #c whd in ⊢ ((??%?)→?); #Hfalse destruct
71   |#ls #a #rs #c whd in ⊢ ((??%?)→?); #H destruct 
72    @(ex_intro … ls) @(ex_intro … rs) //
73   ]
74 qed.
75
76 (*********************************** moves ************************************)
77
78 inductive move : Type[0] ≝
79   | L : move | R : move | N : move.
80
81 (********************************** machine ***********************************)
82
83 record TM (sig:FinSet): Type[1] ≝ 
84 { states : FinSet;
85   trans : states × (option sig) → states × (option sig) × move;
86   start: states;
87   halt : states → bool
88 }.
89
90 definition tape_move_left ≝ λsig:FinSet.λt:tape sig.
91   match t with 
92   [ niltape ⇒ niltape sig
93   | leftof _ _ ⇒ t
94   | rightof a ls ⇒ midtape sig ls a [ ]
95   | midtape ls a rs ⇒ 
96     match ls with 
97     [ nil ⇒ leftof sig a rs
98     | cons a0 ls0 ⇒ midtape sig ls0 a0 (a::rs)
99     ]
100   ]. 
101   
102 definition tape_move_right ≝ λsig:FinSet.λt:tape sig.
103   match t with 
104   [ niltape ⇒ niltape sig
105   | rightof _ _ ⇒ t
106   | leftof a rs ⇒ midtape sig [ ] a rs
107   | midtape ls a rs ⇒ 
108     match rs with 
109     [ nil ⇒ rightof sig a ls
110     | cons a0 rs0 ⇒ midtape sig (a::ls) a0 rs0
111     ]
112   ]. 
113   
114 definition tape_write ≝ λsig.λt: tape sig.λs:option sig.
115   match s with 
116   [ None ⇒ t
117   | Some s0 ⇒ midtape ? (left ? t) s0 (right ? t)
118   ].
119
120 definition tape_move ≝ λsig.λt: tape sig.λm:move.
121   match m with
122     [ R ⇒ tape_move_right ? t
123     | L ⇒ tape_move_left ? t
124     | N ⇒ t
125     ].
126
127 definition tape_move_mono ≝ 
128   λsig,t,mv.
129   tape_move sig (tape_write sig t (\fst mv)) (\snd mv).
130
131 record config (sig,states:FinSet): Type[0] ≝ 
132 { cstate : states;
133   ctape: tape sig
134 }.
135
136 lemma config_expand: ∀sig,Q,c. 
137   c = mk_config sig Q (cstate ?? c) (ctape ?? c).
138 #sig #Q * // 
139 qed.
140   
141 lemma config_eq : ∀sig,M,c1,c2.
142   cstate sig M c1 = cstate sig M c2 → 
143     ctape sig M c1 = ctape sig M c2 →  c1 = c2.
144 #sig #M1 * #s1 #t1 * #s2 #t2 //
145 qed.
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,a,mv〉 ≝ trans sig M 〈cstate ?? c,current_char〉 in
150   mk_config ?? news (tape_move sig (tape_write ? (ctape ?? c) a) mv).
151
152 (*
153 lemma step_eq : ∀sig,M,c. 
154   let current_char ≝ current ? (ctape ?? c) in
155   let 〈news,a,mv〉 ≝ trans sig M 〈cstate ?? c,current_char〉 in
156   step sig M c = 
157     mk_config ?? news (tape_move sig (tape_write ? (ctape ?? c) a) mv).
158 #sig #M #c  
159  whd in match (tape_move_mono sig ??);
160 *)
161   
162 (******************************** loop ****************************************)
163 let rec loop (A:Type[0]) n (f:A→A) p a on n ≝
164   match n with 
165   [ O ⇒ None ?
166   | S m ⇒ if p a then (Some ? a) else loop A m f p (f a)
167   ].
168   
169 lemma loop_S_true : 
170   ∀A,n,f,p,a. p a = true → 
171     loop A (S n) f p a = Some ? a.
172 #A #n #f #p #a #pa normalize >pa //
173 qed.
174
175 lemma loop_S_false : 
176   ∀A,n,f,p,a.  p a = false → 
177     loop A (S n) f p a = loop A n f p (f a).
178 normalize #A #n #f #p #a #Hpa >Hpa %
179 qed.  
180   
181 lemma loop_incr : ∀A,f,p,k1,k2,a1,a2. 
182   loop A k1 f p a1 = Some ? a2 → 
183     loop A (k2+k1) f p a1 = Some ? a2.
184 #A #f #p #k1 #k2 #a1 #a2 generalize in match a1; elim k1
185 [normalize #a0 #Hfalse destruct
186 |#k1' #IH #a0 <plus_n_Sm whd in ⊢ (??%? → ??%?);
187  cases (true_or_false (p a0)) #Hpa0 >Hpa0 whd in ⊢ (??%? → ??%?); // @IH
188 ]
189 qed.
190
191 lemma loop_merge : ∀A,f,p,q.(∀b. p b = false → q b = false) →
192  ∀k1,k2,a1,a2,a3,a4.
193    loop A k1 f p a1 = Some ? a2 → 
194      f a2 = a3 → q a2 = false → 
195        loop A k2 f q a3 = Some ? a4 →
196          loop A (k1+k2) f q a1 = Some ? a4.
197 #Sig #f #p #q #Hpq #k1 elim k1 
198   [normalize #k2 #a1 #a2 #a3 #a4 #H destruct
199   |#k1' #Hind #k2 #a1 #a2 #a3 #a4 normalize in ⊢ (%→?);
200    cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
201    [#eqa1a2 destruct #eqa2a3 #Hqa2 #H
202     whd in ⊢ (??(??%???)?); >plus_n_Sm @loop_incr
203     whd in ⊢ (??%?); >Hqa2 >eqa2a3 @H
204    |normalize >(Hpq … pa1) normalize #H1 #H2 #H3 @(Hind … H2) //
205    ]
206  ]
207 qed.
208
209 lemma loop_split : ∀A,f,p,q.(∀b. q b = true → p b = true) →
210  ∀k,a1,a2.
211    loop A k f q a1 = Some ? a2 → 
212    ∃k1,a3.
213     loop A k1 f p a1 = Some ? a3 ∧ 
214       loop A (S(k-k1)) f q a3 = Some ? a2.
215 #A #f #p #q #Hpq #k elim k
216   [#a1 #a2 normalize #Heq destruct
217   |#i #Hind #a1 #a2 normalize 
218    cases (true_or_false (q a1)) #Hqa1 >Hqa1 normalize
219     [ #Ha1a2 destruct
220      @(ex_intro … 1) @(ex_intro … a2) % 
221        [normalize >(Hpq …Hqa1) // |>Hqa1 //]
222     |#Hloop cases (true_or_false (p a1)) #Hpa1 
223        [@(ex_intro … 1) @(ex_intro … a1) % 
224          [normalize >Hpa1 // |>Hqa1 <Hloop normalize //]
225        |cases (Hind …Hloop) #k2 * #a3 * #Hloop1 #Hloop2
226         @(ex_intro … (S k2)) @(ex_intro … a3) %
227          [normalize >Hpa1 normalize // | @Hloop2 ]
228        ]
229     ]
230   ]
231 qed.
232
233 lemma loop_eq : ∀sig,f,q,i,j,a,x,y. 
234   loop sig i f q a = Some ? x → loop sig j f q a = Some ? y → x = y.
235 #sig #f #q #i #j @(nat_elim2 … i j)
236 [ #n #a #x #y normalize #Hfalse destruct (Hfalse)
237 | #n #a #x #y #H1 normalize #Hfalse destruct (Hfalse)
238 | #n1 #n2 #IH #a #x #y normalize cases (q a) normalize
239   [ #H1 #H2 destruct %
240   | /2/ ]
241 ]
242 qed.
243
244 lemma loop_p_true : 
245   ∀A,k,f,p,a.p a = true → loop A (S k) f p a = Some ? a.
246 #A #k #f #p #a #Ha normalize >Ha %
247 qed.
248
249 lemma loop_Some : 
250   ∀A,k,f,p,a,b.loop A k f p a = Some ? b → p b = true.
251 #A #k #f #p elim k 
252   [#a #b normalize #Hfalse destruct
253   |#k0 #IH #a #b whd in ⊢ (??%? → ?); cases (true_or_false (p a)) #Hpa
254     [ >Hpa normalize #H1 destruct // | >Hpa normalize @IH ]
255   ]
256 qed. 
257
258 lemma loop_lift : ∀A,B,k,lift,f,g,h,hlift,c1,c2.
259   (∀x.hlift (lift x) = h x) → 
260   (∀x.h x = false → lift (f x) = g (lift x)) → 
261   loop A k f h c1 = Some ? c2 → 
262   loop B k g hlift (lift c1) = Some ? (lift … c2).
263 #A #B #k #lift #f #g #h #hlift #c1 #c2 #Hfg #Hhlift
264 generalize in match c1; elim k
265 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
266 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
267  cases (true_or_false (h c0)) #Hc0 >Hfg >Hc0 normalize
268  [ #Heq destruct (Heq) % | <Hhlift // @IH ]
269 qed.
270
271 (************************** Realizability *************************************)
272 definition loopM ≝ λsig,M,i,cin.
273   loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) cin.
274
275 lemma loopM_unfold : ∀sig,M,i,cin.
276   loopM sig M i cin = loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) cin.
277 // qed.
278
279 definition initc ≝ λsig.λM:TM sig.λt.
280   mk_config sig (states sig M) (start sig M) t.
281
282 definition Realize ≝ λsig.λM:TM sig.λR:relation (tape sig).
283 ∀t.∃i.∃outc.
284   loopM sig M i (initc sig M t) = Some ? outc ∧ R t (ctape ?? outc).
285
286 definition WRealize ≝ λsig.λM:TM sig.λR:relation (tape sig).
287 ∀t,i,outc.
288   loopM sig M i (initc sig M t) = Some ? outc → R t (ctape ?? outc).
289
290 definition Terminate ≝ λsig.λM:TM sig.λt. ∃i,outc.
291   loopM sig M i (initc sig M t) = Some ? outc.
292   
293 notation "M \vDash R" non associative with precedence 45 for @{ 'models $M $R}.
294 interpretation "realizability" 'models M R = (Realize ? M R).
295
296 notation "M \VDash R" non associative with precedence 45 for @{ 'wmodels $M $R}.
297 interpretation "weak realizability" 'wmodels M R = (WRealize ? M R).
298
299 interpretation "termination" 'fintersects M t = (Terminate ? M t).
300
301 lemma WRealize_to_Realize : ∀sig.∀M: TM sig.∀R.
302   (∀t.M ↓ t) → M ⊫ R → M ⊨ R.
303 #sig #M #R #HT #HW #t cases (HT … t) #i * #outc #Hloop 
304 @(ex_intro … i) @(ex_intro … outc) % // @(HW … i) //
305 qed.
306
307 theorem Realize_to_WRealize : ∀sig.∀M:TM sig.∀R.
308   M ⊨ R → M ⊫ R.
309 #sig #M #R #H1 #inc #i #outc #Hloop 
310 cases (H1 inc) #k * #outc1 * #Hloop1 #HR >(loop_eq … Hloop Hloop1) //
311 qed.
312
313 definition accRealize ≝ λsig.λM:TM sig.λacc:states sig M.λRtrue,Rfalse.
314 ∀t.∃i.∃outc.
315   loopM sig M i (initc sig M t) = Some ? outc ∧
316     (cstate ?? outc = acc → Rtrue t (ctape ?? outc)) ∧ 
317     (cstate ?? outc ≠ acc → Rfalse t (ctape ?? outc)).
318     
319 notation "M ⊨ [q: R1,R2]" non associative with precedence 45 for @{ 'cmodels $M $q $R1 $R2}.
320 interpretation "conditional realizability" 'cmodels M q R1 R2 = (accRealize ? M q R1 R2).
321
322 (*************************** guarded realizablity *****************************)
323 definition GRealize ≝ λsig.λM:TM sig.λPre:tape sig →Prop.λR:relation (tape sig).
324 ∀t.Pre t → ∃i.∃outc.
325   loopM sig M i (initc sig M t) = Some ? outc ∧ R t (ctape ?? outc).
326   
327 definition accGRealize ≝ λsig.λM:TM sig.λacc:states sig M.
328 λPre: tape sig → Prop.λRtrue,Rfalse.
329 ∀t.Pre t → ∃i.∃outc.
330   loopM sig M i (initc sig M t) = Some ? outc ∧
331     (cstate ?? outc = acc → Rtrue t (ctape ?? outc)) ∧ 
332     (cstate ?? outc ≠ acc → Rfalse t (ctape ?? outc)).
333     
334 lemma WRealize_to_GRealize : ∀sig.∀M: TM sig.∀Pre,R.
335   (∀t.Pre t → M ↓ t) → M ⊫ R → GRealize sig M Pre R.
336 #sig #M #Pre #R #HT #HW #t #HPre cases (HT … t HPre) #i * #outc #Hloop 
337 @(ex_intro … i) @(ex_intro … outc) % // @(HW … i) //
338 qed.
339
340 lemma Realize_to_GRealize : ∀sig,M.∀P,R. 
341   M ⊨ R → GRealize sig M P R.
342 #alpha #M #Pre #R #HR #t #HPre
343 cases (HR t) -HR #k * #outc * #Hloop #HR 
344 @(ex_intro ?? k) @(ex_intro ?? outc) % 
345   [ @Hloop | @HR ]
346 qed.
347
348 lemma acc_Realize_to_acc_GRealize: ∀sig,M.∀q:states sig M.∀P,R1,R2. 
349   M ⊨ [q:R1,R2] → accGRealize sig M q P R1 R2.
350 #alpha #M #q #Pre #R1 #R2 #HR #t #HPre
351 cases (HR t) -HR #k * #outc * * #Hloop #HRtrue #HRfalse 
352 @(ex_intro ?? k) @(ex_intro ?? outc) % 
353   [ % [@Hloop] @HRtrue | @HRfalse]
354 qed.
355
356 (******************************** monotonicity ********************************)
357 lemma Realize_to_Realize : ∀alpha,M,R1,R2.
358   R1 ⊆ R2 → Realize alpha M R1 → Realize alpha M R2.
359 #alpha #M #R1 #R2 #Himpl #HR1 #intape
360 cases (HR1 intape) -HR1 #k * #outc * #Hloop #HR1
361 @(ex_intro ?? k) @(ex_intro ?? outc) % /2/
362 qed.
363
364 lemma WRealize_to_WRealize: ∀sig,M,R1,R2.
365   R1 ⊆ R2 → WRealize sig M R1 → WRealize ? M R2.
366 #alpha #M #R1 #R2 #Hsub #HR1 #intape #i #outc #Hloop
367 @Hsub @(HR1 … i) @Hloop
368 qed.
369
370 lemma GRealize_to_GRealize : ∀alpha,M,P,R1,R2.
371   R1 ⊆ R2 → GRealize alpha M P R1 → GRealize alpha M P R2.
372 #alpha #M #P #R1 #R2 #Himpl #HR1 #intape #HP
373 cases (HR1 intape HP) -HR1 #k * #outc * #Hloop #HR1
374 @(ex_intro ?? k) @(ex_intro ?? outc) % /2/
375 qed.
376
377 lemma GRealize_to_GRealize_2 : ∀alpha,M,P1,P2,R1,R2.
378   P2 ⊆ P1 → R1 ⊆ R2 → GRealize alpha M P1 R1 → GRealize alpha M P2 R2.
379 #alpha #M #P1 #P2 #R1 #R2 #Himpl1 #Himpl2 #H1 #intape #HP
380 cases (H1 intape (Himpl1 … HP)) -H1 #k * #outc * #Hloop #H1
381 @(ex_intro ?? k) @(ex_intro ?? outc) % /2/
382 qed.
383
384 lemma acc_Realize_to_acc_Realize: ∀sig,M.∀q:states sig M.∀R1,R2,R3,R4. 
385   R1 ⊆ R3 → R2 ⊆ R4 → M ⊨ [q:R1,R2] → M ⊨ [q:R3,R4].
386 #alpha #M #q #R1 #R2 #R3 #R4 #Hsub13 #Hsub24 #HRa #intape
387 cases (HRa intape) -HRa #k * #outc * * #Hloop #HRtrue #HRfalse 
388 @(ex_intro ?? k) @(ex_intro ?? outc) % 
389   [ % [@Hloop] #Hq @Hsub13 @HRtrue // | #Hq @Hsub24 @HRfalse //]
390 qed.
391
392 (**************************** A canonical relation ****************************)
393
394 definition R_TM ≝ λsig.λM:TM sig.λq.λt1,t2.
395 ∃i,outc.
396   loopM ? M i (mk_config ?? q t1) = Some ? outc ∧ 
397   t2 = (ctape ?? outc).
398   
399 lemma R_TM_to_R: ∀sig,M,R. ∀t1,t2. 
400   M ⊫ R → R_TM ? M (start sig M) t1 t2 → R t1 t2.
401 #sig #M #R #t1 #t2 whd in ⊢ (%→?); #HMR * #i * #outc *
402 #Hloop #Ht2 >Ht2 @(HMR … Hloop)
403 qed.
404
405 (******************************** NOP Machine *********************************)
406
407 (* NO OPERATION
408    t1 = t2 *)
409   
410 definition nop_states ≝ initN 1.
411 definition start_nop : initN 1 ≝ mk_Sig ?? 0 (le_n … 1).
412
413 definition nop ≝ 
414   λalpha:FinSet.mk_TM alpha nop_states
415   (λp.let 〈q,a〉 ≝ p in 〈q,None ?,N〉)
416   start_nop (λ_.true).
417   
418 definition R_nop ≝ λalpha.λt1,t2:tape alpha.t2 = t1.
419
420 lemma sem_nop :
421   ∀alpha.nop alpha ⊨ R_nop alpha.
422 #alpha #intape @(ex_intro ?? 1) 
423 @(ex_intro … (mk_config ?? start_nop intape)) % % 
424 qed.
425
426 lemma nop_single_state: ∀sig.∀q1,q2:states ? (nop sig). q1 = q2.
427 normalize #sig * #n #ltn1 * #m #ltm1 
428 generalize in match ltn1; generalize in match ltm1;
429 <(le_n_O_to_eq … (le_S_S_to_le … ltn1)) <(le_n_O_to_eq … (le_S_S_to_le … ltm1)) 
430 // qed.
431
432 (************************** Sequential Composition ****************************)
433
434 definition seq_trans ≝ λsig. λM1,M2 : TM sig. 
435 λp. let 〈s,a〉 ≝ p in
436   match s with 
437   [ inl s1 ⇒ 
438       if halt sig M1 s1 then 〈inr … (start sig M2), None ?,N〉
439       else let 〈news1,newa,m〉 ≝ trans sig M1 〈s1,a〉 in 〈inl … news1,newa,m〉
440   | inr s2 ⇒ let 〈news2,newa,m〉 ≝ trans sig M2 〈s2,a〉 in 〈inr … news2,newa,m〉
441   ].
442  
443 definition seq ≝ λsig. λM1,M2 : TM sig. 
444   mk_TM sig 
445     (FinSum (states sig M1) (states sig M2))
446     (seq_trans sig M1 M2) 
447     (inl … (start sig M1))
448     (λs.match s with 
449       [ inl _ ⇒ false | inr s2 ⇒ halt sig M2 s2]). 
450
451 notation "a · b" right associative with precedence 65 for @{ 'middot $a $b}.
452 interpretation "sequential composition" 'middot a b = (seq ? a b).
453
454 definition lift_confL ≝ 
455   λsig,S1,S2,c.match c with 
456   [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inl … s) t ].
457   
458 definition lift_confR ≝ 
459   λsig,S1,S2,c.match c with
460   [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inr … s) t ].
461   
462 definition halt_liftL ≝ 
463   λS1,S2,halt.λs:FinSum S1 S2.
464   match s with
465   [ inl s1 ⇒ halt s1
466   | inr _ ⇒ true ]. (* should be vacuous in all cases we use halt_liftL *)
467
468 definition halt_liftR ≝ 
469   λS1,S2,halt.λs:FinSum S1 S2.
470   match s with
471   [ inl _ ⇒ false 
472   | inr s2 ⇒ halt s2 ].
473       
474 lemma p_halt_liftL : ∀sig,S1,S2,halt,c.
475   halt (cstate sig S1 c) =
476      halt_liftL S1 S2 halt (cstate … (lift_confL … c)).
477 #sig #S1 #S2 #halt #c cases c #s #t %
478 qed.
479
480 lemma trans_seq_liftL : ∀sig,M1,M2,s,a,news,newa,move.
481   halt ? M1 s = false → 
482   trans sig M1 〈s,a〉 = 〈news,newa,move〉 → 
483   trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inl … news,newa,move〉.
484 #sig (*#M1*) * #Q1 #T1 #init1 #halt1 #M2 #s #a #news #newa #move
485 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
486 qed.
487
488 lemma trans_seq_liftR : ∀sig,M1,M2,s,a,news,newa,move.
489   halt ? M2 s = false → 
490   trans sig M2 〈s,a〉 = 〈news,newa,move〉 → 
491   trans sig (seq sig M1 M2) 〈inr … s,a〉 = 〈inr … news,newa,move〉.
492 #sig #M1 * #Q2 #T2 #init2 #halt2 #s #a #news #newa #move
493 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
494 qed.
495
496 lemma step_seq_liftR : ∀sig,M1,M2,c0.
497  halt ? M2 (cstate ?? c0) = false → 
498  step sig (seq sig M1 M2) (lift_confR sig (states ? M1) (states ? M2) c0) =
499  lift_confR sig (states ? M1) (states ? M2) (step sig M2 c0).
500 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s #t
501   lapply (refl ? (trans ?? 〈s,current sig t〉))
502   cases (trans ?? 〈s,current sig t〉) in ⊢ (???% → %);
503   * #s0 #a0 #m0 cases t
504   [ #Heq #Hhalt
505   | 2,3: #s1 #l1 #Heq #Hhalt 
506   |#ls #s1 #rs #Heq #Hhalt ]
507   whd in ⊢ (???(????%)); >Heq whd in ⊢ (???%);
508   whd in ⊢ (??(???%)?); whd in ⊢ (??%?); >(trans_seq_liftR … Heq) //
509 qed.
510
511 lemma step_seq_liftL : ∀sig,M1,M2,c0.
512  halt ? M1 (cstate ?? c0) = false → 
513  step sig (seq sig M1 M2) (lift_confL sig (states ? M1) (states ? M2) c0) =
514  lift_confL sig ?? (step sig M1 c0).
515 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s #t
516   lapply (refl ? (trans ?? 〈s,current sig t〉))
517   cases (trans ?? 〈s,current sig t〉) in ⊢ (???% → %);
518   * #s0 #a0 #m0 cases t
519   [ #Heq #Hhalt
520   | 2,3: #s1 #l1 #Heq #Hhalt 
521   |#ls #s1 #rs #Heq #Hhalt ]
522   whd in ⊢ (???(????%)); >Heq whd in ⊢ (???%);
523   whd in ⊢ (??(???%)?); whd in ⊢ (??%?); >(trans_seq_liftL … Heq) //
524 qed.
525
526 lemma trans_liftL_true : ∀sig,M1,M2,s,a.
527   halt ? M1 s = true → 
528   trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inr … (start ? M2),None ?,N〉.
529 #sig #M1 #M2 #s #a #Hhalt whd in ⊢ (??%?); >Hhalt %
530 qed.
531
532 lemma eq_ctape_lift_conf_L : ∀sig,S1,S2,outc.
533   ctape sig (FinSum S1 S2) (lift_confL … outc) = ctape … outc.
534 #sig #S1 #S2 #outc cases outc #s #t %
535 qed.
536   
537 lemma eq_ctape_lift_conf_R : ∀sig,S1,S2,outc.
538   ctape sig (FinSum S1 S2) (lift_confR … outc) = ctape … outc.
539 #sig #S1 #S2 #outc cases outc #s #t %
540 qed.
541
542 theorem sem_seq: ∀sig.∀M1,M2:TM sig.∀R1,R2.
543   M1 ⊨ R1 → M2 ⊨ R2 → M1 · M2 ⊨ R1 ∘ R2.
544 #sig #M1 #M2 #R1 #R2 #HR1 #HR2 #t 
545 cases (HR1 t) #k1 * #outc1 * #Hloop1 #HM1
546 cases (HR2 (ctape sig (states ? M1) outc1)) #k2 * #outc2 * #Hloop2 #HM2
547 @(ex_intro … (k1+k2)) @(ex_intro … (lift_confR … outc2))
548 %
549 [@(loop_merge ??????????? 
550    (loop_lift ??? (lift_confL sig (states sig M1) (states sig M2))
551    (step sig M1) (step sig (seq sig M1 M2)) 
552    (λc.halt sig M1 (cstate … c)) 
553    (λc.halt_liftL ?? (halt sig M1) (cstate … c)) … Hloop1))
554   [ * *
555    [ #sl #tl whd in ⊢ (??%? → ?); #Hl %
556    | #sr #tr whd in ⊢ (??%? → ?); #Hr destruct (Hr) ]
557   || #c0 #Hhalt <step_seq_liftL //
558   | #x <p_halt_liftL %
559   |6:cases outc1 #s1 #t1 %
560   |7:@(loop_lift … (initc ?? (ctape … outc1)) … Hloop2) 
561     [ * #s2 #t2 %
562     | #c0 #Hhalt <step_seq_liftR // ]
563   |whd in ⊢ (??(???%)?);whd in ⊢ (??%?);
564    generalize in match Hloop1; cases outc1 #sc1 #tc1 #Hloop10 
565    >(trans_liftL_true sig M1 M2 ??) 
566     [ whd in ⊢ (??%?); whd in ⊢ (???%);
567       @config_eq whd in ⊢ (???%); //
568     | @(loop_Some ?????? Hloop10) ]
569  ]
570 | @(ex_intro … (ctape ? (FinSum (states ? M1) (states ? M2)) (lift_confL … outc1)))
571   % // >eq_ctape_lift_conf_L >eq_ctape_lift_conf_R //
572 ]
573 qed.
574
575 theorem sem_seq_app: ∀sig.∀M1,M2:TM sig.∀R1,R2,R3.
576   M1 ⊨ R1 → M2 ⊨ R2 → R1 ∘ R2 ⊆ R3 → M1 · M2 ⊨ R3.
577 #sig #M1 #M2 #R1 #R2 #R3 #HR1 #HR2 #Hsub
578 #t cases (sem_seq … HR1 HR2 t)
579 #k * #outc * #Hloop #Houtc @(ex_intro … k) @(ex_intro … outc)
580 % [@Hloop |@Hsub @Houtc]
581 qed.
582
583 (* composition with guards *)
584 theorem sem_seq_guarded: ∀sig.∀M1,M2:TM sig.∀Pre1,Pre2,R1,R2.
585   GRealize sig M1 Pre1 R1 → GRealize sig M2 Pre2 R2 → 
586   (∀t1,t2.Pre1 t1 → R1 t1 t2 → Pre2 t2) → 
587   GRealize sig (M1 · M2) Pre1 (R1 ∘ R2).
588 #sig #M1 #M2 #Pre1 #Pre2 #R1 #R2 #HGR1 #HGR2 #Hinv #t1 #HPre1
589 cases (HGR1 t1 HPre1) #k1 * #outc1 * #Hloop1 #HM1
590 cases (HGR2 (ctape sig (states ? M1) outc1) ?) 
591   [2: @(Hinv … HPre1 HM1)]  
592 #k2 * #outc2 * #Hloop2 #HM2
593 @(ex_intro … (k1+k2)) @(ex_intro … (lift_confR … outc2))
594 %
595 [@(loop_merge ??????????? 
596    (loop_lift ??? (lift_confL sig (states sig M1) (states sig M2))
597    (step sig M1) (step sig (seq sig M1 M2)) 
598    (λc.halt sig M1 (cstate … c)) 
599    (λc.halt_liftL ?? (halt sig M1) (cstate … c)) … Hloop1))
600   [ * *
601    [ #sl #tl whd in ⊢ (??%? → ?); #Hl %
602    | #sr #tr whd in ⊢ (??%? → ?); #Hr destruct (Hr) ]
603   || #c0 #Hhalt <step_seq_liftL //
604   | #x <p_halt_liftL %
605   |6:cases outc1 #s1 #t1 %
606   |7:@(loop_lift … (initc ?? (ctape … outc1)) … Hloop2) 
607     [ * #s2 #t2 %
608     | #c0 #Hhalt <step_seq_liftR // ]
609   |whd in ⊢ (??(???%)?);whd in ⊢ (??%?);
610    generalize in match Hloop1; cases outc1 #sc1 #tc1 #Hloop10 
611    >(trans_liftL_true sig M1 M2 ??) 
612     [ whd in ⊢ (??%?); whd in ⊢ (???%);
613       @config_eq whd in ⊢ (???%); //
614     | @(loop_Some ?????? Hloop10) ]
615  ]
616 | @(ex_intro … (ctape ? (FinSum (states ? M1) (states ? M2)) (lift_confL … outc1)))
617   % // >eq_ctape_lift_conf_L >eq_ctape_lift_conf_R //
618 ]
619 qed.
620
621 theorem sem_seq_app_guarded: ∀sig.∀M1,M2:TM sig.∀Pre1,Pre2,R1,R2,R3.
622   GRealize sig M1 Pre1 R1 → GRealize sig M2 Pre2 R2 → 
623   (∀t1,t2.Pre1 t1 → R1 t1 t2 → Pre2 t2) → R1 ∘ R2 ⊆ R3 →
624   GRealize sig (M1 · M2) Pre1 R3.
625 #sig #M1 #M2 #Pre1 #Pre2 #R1 #R2 #R3 #HR1 #HR2 #Hinv #Hsub
626 #t #HPre1 cases (sem_seq_guarded … HR1 HR2 Hinv t HPre1)
627 #k * #outc * #Hloop #Houtc @(ex_intro … k) @(ex_intro … outc)
628 % [@Hloop |@Hsub @Houtc]
629 qed.