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