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