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