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