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