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