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