]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/turing.ma
7e9c708d0bda3a345b39122a191446a54a0704f5
[helm.git] / matita / matita / lib / turing / turing.ma
1 include "turing/mono.ma".
2 include "basics/vectors.ma".
3
4 (* We do not distinuish an input tape *)
5
6 (* tapes_no = number of ADDITIONAL working tapes *)
7
8 record mTM (sig:FinSet) (tapes_no:nat) : Type[1] ≝ 
9 { states : FinSet;
10   trans : states × (Vector (option sig) (S tapes_no)) → 
11     states  × (Vector ((option sig) × move) (S tapes_no));
12   start: states;
13   halt : states → bool
14 }.
15
16 record mconfig (sig,states:FinSet) (n:nat): Type[0] ≝
17 { cstate : states;
18   ctapes : Vector (tape sig) (S n)
19 }.
20
21 lemma mconfig_expand: ∀sig,n,Q,c. 
22   c = mk_mconfig sig Q n (cstate ??? c) (ctapes ??? c).
23 #sig #n #Q * // 
24 qed.
25   
26 lemma mconfig_eq : ∀sig,n,M,c1,c2.
27   cstate sig n M c1 = cstate sig n M c2 → 
28     ctapes sig n M c1 = ctapes sig n M c2 →  c1 = c2.
29 #sig #n #M1 * #s1 #t1 * #s2 #t2 //
30 qed.
31
32 definition current_chars ≝ λsig.λn.λtapes.
33   vec_map ?? (current sig) (S n) tapes.
34
35 definition tape_move_multi ≝ 
36   λsig,n,ts,mvs.
37   pmap_vec ??? (tape_move sig) ? 
38     (pmap_vec ??? (tape_write sig) n ts (vec_map ?? (λx.\fst x) ? mvs))
39         (vec_map ?? (λx.\snd x) ? mvs).
40
41 definition step ≝ λsig.λn.λM:mTM sig n.λc:mconfig sig (states ?? M) n.
42   let 〈news,mvs〉 ≝ trans sig n M 〈cstate ??? c,current_chars ?? (ctapes ??? c)〉 in
43   mk_mconfig ??? news (tape_move_multi sig ? (ctapes ??? c) mvs).
44
45 definition empty_tapes ≝ λsig.λn.
46 mk_Vector ? n (make_list (tape sig) (niltape sig) n) ?.
47 elim n // normalize //
48 qed.
49
50 (************************** Realizability *************************************)
51 definition loopM ≝ λsig,n.λM:mTM sig n.λi,cin.
52   loop ? i (step sig n M) (λc.halt sig n M (cstate ??? c)) cin.
53
54 lemma loopM_unfold : ∀sig,n,M,i,cin.
55   loopM sig n M i cin = loop ? i (step sig n M) (λc.halt sig n M (cstate ??? c)) cin.
56 // qed.
57
58 definition initc ≝ λsig,n.λM:mTM sig n.λtapes.
59   mk_mconfig sig (states sig n M) n (start sig n M) tapes.
60
61 definition Realize ≝ λsig,n.λM:mTM sig n.λR:relation (Vector (tape sig) ?).
62 ∀t.∃i.∃outc.
63   loopM sig n M i (initc sig n M t) = Some ? outc ∧ R t (ctapes ??? outc).
64
65 definition WRealize ≝ λsig,n.λM:mTM sig n.λR:relation (Vector (tape sig) ?).
66 ∀t,i,outc.
67   loopM sig n M i (initc sig n M t) = Some ? outc → R t (ctapes ??? outc).
68
69 definition Terminate ≝ λsig,n.λM:mTM sig n.λt. ∃i,outc.
70   loopM sig n M i (initc sig n M t) = Some ? outc.
71   
72 (* notation "M \vDash R" non associative with precedence 45 for @{ 'models $M $R}. *)
73 interpretation "multi realizability" 'models M R = (Realize ?? M R).
74
75 (* notation "M \VDash R" non associative with precedence 45 for @{ 'wmodels $M $R}. *)
76 interpretation "weak multi realizability" 'wmodels M R = (WRealize ?? M R).
77
78 interpretation "multi termination" 'fintersects M t = (Terminate ?? M t).
79
80 lemma WRealize_to_Realize : ∀sig,n .∀M: mTM sig n.∀R.
81   (∀t.M ↓ t) → M ⊫ R → M ⊨ R.
82 #sig #n #M #R #HT #HW #t cases (HT … t) #i * #outc #Hloop 
83 @(ex_intro … i) @(ex_intro … outc) % // @(HW … i) //
84 qed.
85
86 theorem Realize_to_WRealize : ∀sig,n.∀M:mTM sig n.∀R.
87   M ⊨ R → M ⊫ R.
88 #sig #n #M #R #H1 #inc #i #outc #Hloop 
89 cases (H1 inc) #k * #outc1 * #Hloop1 #HR >(loop_eq … Hloop Hloop1) //
90 qed.
91
92 definition accRealize ≝ λsig,n.λM:mTM sig n.λacc:states sig n M.λRtrue,Rfalse.
93 ∀t.∃i.∃outc.
94   loopM sig n M i (initc sig n M t) = Some ? outc ∧
95     (cstate ??? outc = acc → Rtrue t (ctapes ??? outc)) ∧ 
96     (cstate ??? outc ≠ acc → Rfalse t (ctapes ??? outc)).
97     
98 (* notation "M ⊨ [q: R1,R2]" non associative with precedence 45 for @{ 'cmodels $M $q $R1 $R2}. *)
99 interpretation "conditional multi realizability" 'cmodels M q R1 R2 = (accRealize ?? M q R1 R2).
100
101 (*************************** guarded realizablity *****************************)
102 definition GRealize ≝ λsig,n.λM:mTM sig n.
103  λPre:Vector (tape sig) ? →Prop.λR:relation (Vector (tape sig) ?).
104   ∀t.Pre t → ∃i.∃outc.
105    loopM sig n M i (initc sig n M t) = Some ? outc ∧ R t (ctapes ??? outc).
106   
107 definition accGRealize ≝ λsig,n.λM:mTM sig n.λacc:states sig n M.
108 λPre: Vector (tape sig) ? → Prop.λRtrue,Rfalse.
109 ∀t.Pre t → ∃i.∃outc.
110   loopM sig n M i (initc sig n M t) = Some ? outc ∧
111     (cstate ??? outc = acc → Rtrue t (ctapes ??? outc)) ∧ 
112     (cstate ??? outc ≠ acc → Rfalse t (ctapes ??? outc)).
113     
114 lemma WRealize_to_GRealize : ∀sig,n.∀M: mTM sig n.∀Pre,R.
115   (∀t.Pre t → M ↓ t) → M ⊫ R → GRealize sig n M Pre R.
116 #sig #n #M #Pre #R #HT #HW #t #HPre cases (HT … t HPre) #i * #outc #Hloop 
117 @(ex_intro … i) @(ex_intro … outc) % // @(HW … i) //
118 qed.
119
120 lemma Realize_to_GRealize : ∀sig,n.∀M: mTM sig n.∀P,R. 
121   M ⊨ R → GRealize sig n M P R.
122 #alpha #n #M #Pre #R #HR #t #HPre
123 cases (HR t) -HR #k * #outc * #Hloop #HR 
124 @(ex_intro ?? k) @(ex_intro ?? outc) % 
125   [ @Hloop | @HR ]
126 qed.
127
128 lemma acc_Realize_to_acc_GRealize: ∀sig,n.∀M:mTM sig n.∀q:states sig n M.∀P,R1,R2. 
129   M ⊨ [q:R1,R2] → accGRealize sig n M q P R1 R2.
130 #alpha #n #M #q #Pre #R1 #R2 #HR #t #HPre
131 cases (HR t) -HR #k * #outc * * #Hloop #HRtrue #HRfalse 
132 @(ex_intro ?? k) @(ex_intro ?? outc) % 
133   [ % [@Hloop] @HRtrue | @HRfalse]
134 qed.
135
136 (******************************** monotonicity ********************************)
137 lemma Realize_to_Realize : ∀sig,n.∀M:mTM sig n.∀R1,R2.
138   R1 ⊆ R2 → M ⊨ R1 → M ⊨ R2.
139 #alpha #n #M #R1 #R2 #Himpl #HR1 #intape
140 cases (HR1 intape) -HR1 #k * #outc * #Hloop #HR1
141 @(ex_intro ?? k) @(ex_intro ?? outc) % /2/
142 qed.
143
144 lemma WRealize_to_WRealize: ∀sig,n.∀M:mTM sig n.∀R1,R2.
145   R1 ⊆ R2 → WRealize sig n M R1 → WRealize sig n M R2.
146 #alpha #n #M #R1 #R2 #Hsub #HR1 #intape #i #outc #Hloop
147 @Hsub @(HR1 … i) @Hloop
148 qed.
149
150 lemma GRealize_to_GRealize : ∀sig,n.∀M:mTM sig n.∀P,R1,R2.
151   R1 ⊆ R2 → GRealize sig n M P R1 → GRealize sig n M P R2.
152 #alpha #n #M #P #R1 #R2 #Himpl #HR1 #intape #HP
153 cases (HR1 intape HP) -HR1 #k * #outc * #Hloop #HR1
154 @(ex_intro ?? k) @(ex_intro ?? outc) % /2/
155 qed.
156
157 lemma GRealize_to_GRealize_2 : ∀sig,n.∀M:mTM sig n.∀P1,P2,R1,R2.
158   P2 ⊆ P1 → R1 ⊆ R2 → GRealize sig n M P1 R1 → GRealize sig n M P2 R2.
159 #alpha #n #M #P1 #P2 #R1 #R2 #Himpl1 #Himpl2 #H1 #intape #HP
160 cases (H1 intape (Himpl1 … HP)) -H1 #k * #outc * #Hloop #H1
161 @(ex_intro ?? k) @(ex_intro ?? outc) % /2/
162 qed.
163
164 lemma acc_Realize_to_acc_Realize: ∀sig,n.∀M:mTM sig n.∀q:states sig n M.
165  ∀R1,R2,R3,R4. 
166   R1 ⊆ R3 → R2 ⊆ R4 → M ⊨ [q:R1,R2] → M ⊨ [q:R3,R4].
167 #alpha #n #M #q #R1 #R2 #R3 #R4 #Hsub13 #Hsub24 #HRa #intape
168 cases (HRa intape) -HRa #k * #outc * * #Hloop #HRtrue #HRfalse 
169 @(ex_intro ?? k) @(ex_intro ?? outc) % 
170   [ % [@Hloop] #Hq @Hsub13 @HRtrue // | #Hq @Hsub24 @HRfalse //]
171 qed.
172
173 (**************************** A canonical relation ****************************)
174
175 definition R_mTM ≝ λsig,n.λM:mTM sig n.λq.λt1,t2.
176 ∃i,outc.
177   loopM ? n M i (mk_mconfig ??? q t1) = Some ? outc ∧ 
178   t2 = (ctapes ??? outc).
179   
180 lemma R_mTM_to_R: ∀sig,n.∀M:mTM sig n.∀R. ∀t1,t2. 
181   M ⊫ R → R_mTM ?? M (start sig n M) t1 t2 → R t1 t2.
182 #sig #n #M #R #t1 #t2 whd in ⊢ (%→?); #HMR * #i * #outc *
183 #Hloop #Ht2 >Ht2 @(HMR … Hloop)
184 qed.
185
186 (******************************** NOP Machine *********************************)
187
188 (* NO OPERATION
189    t1 = t2 
190   
191 definition nop_states ≝ initN 1.
192 definition start_nop : initN 1 ≝ mk_Sig ?? 0 (le_n … 1). *)
193
194 definition nop ≝ 
195   λalpha:FinSet.λn.mk_mTM alpha n nop_states
196   (λp.let 〈q,a〉 ≝ p in 〈q,mk_Vector ? (S n) (make_list ? (〈None ?,N〉) (S n)) ?〉)
197   start_nop (λ_.true).
198 elim n normalize //
199 qed.
200   
201 definition R_nop ≝ λalpha,n.λt1,t2:Vector (tape alpha) (S n).t2 = t1.
202
203 lemma sem_nop :
204   ∀alpha,n.nop alpha n⊨ R_nop alpha n.
205 #alpha #n #intapes @(ex_intro ?? 1) 
206 @(ex_intro … (mk_mconfig ??? start_nop intapes)) % % 
207 qed.
208
209 lemma nop_single_state: ∀sig,n.∀q1,q2:states ? n (nop sig n). q1 = q2.
210 normalize #sig #n0 * #n #ltn1 * #m #ltm1 
211 generalize in match ltn1; generalize in match ltm1;
212 <(le_n_O_to_eq … (le_S_S_to_le … ltn1)) <(le_n_O_to_eq … (le_S_S_to_le … ltm1)) 
213 // qed.
214
215 (************************** Sequential Composition ****************************)
216 definition null_action ≝ λsig.λn.
217 mk_Vector ? (S n) (make_list (option sig × move) (〈None ?,N〉) (S n)) ?.
218 elim (S n) // normalize //
219 qed.
220
221 lemma tape_move_null_action: ∀sig,n,tapes.
222   tape_move_multi sig (S n) tapes (null_action sig n) = tapes.
223 #sig #n #tapes cases tapes -tapes #tapes whd in match (null_action ??);
224 #Heq @Vector_eq <Heq -Heq elim tapes //
225 #a #tl #Hind whd in ⊢ (??%?); @eq_f2 // @Hind
226 qed.
227
228 definition seq_trans ≝ λsig,n. λM1,M2 : mTM sig n. 
229 λp. let 〈s,a〉 ≝ p in
230   match s with 
231   [ inl s1 ⇒ 
232       if halt sig n M1 s1 then 〈inr … (start sig n M2), null_action sig n〉
233       else let 〈news1,m〉 ≝ trans sig n M1 〈s1,a〉 in 〈inl … news1,m〉
234   | inr s2 ⇒ let 〈news2,m〉 ≝ trans sig n M2 〈s2,a〉 in 〈inr … news2,m〉
235   ].
236  
237 definition seq ≝ λsig,n. λM1,M2 : mTM sig n. 
238   mk_mTM sig n
239     (FinSum (states sig n M1) (states sig n M2))
240     (seq_trans sig n M1 M2) 
241     (inl … (start sig n M1))
242     (λs.match s with 
243       [ inl _ ⇒ false | inr s2 ⇒ halt sig n M2 s2]). 
244
245 (* notation "a · b" right associative with precedence 65 for @{ 'middot $a $b}. *)
246 interpretation "sequential composition" 'middot a b = (seq ?? a b).
247
248 definition lift_confL ≝ 
249   λsig,n,S1,S2,c.match c with 
250   [ mk_mconfig s t ⇒ mk_mconfig sig (FinSum S1 S2) n (inl … s) t ].
251   
252 definition lift_confR ≝ 
253   λsig,n,S1,S2,c.match c with
254   [ mk_mconfig s t ⇒ mk_mconfig sig (FinSum S1 S2) n (inr … s) t ].
255
256 (* 
257 definition halt_liftL ≝ 
258   λS1,S2,halt.λs:FinSum S1 S2.
259   match s with
260   [ inl s1 ⇒ halt s1
261   | inr _ ⇒ true ]. (* should be vacuous in all cases we use halt_liftL *)
262
263 definition halt_liftR ≝ 
264   λS1,S2,halt.λs:FinSum S1 S2.
265   match s with
266   [ inl _ ⇒ false 
267   | inr s2 ⇒ halt s2 ]. *)
268       
269 lemma p_halt_liftL : ∀sig,n,S1,S2,halt,c.
270   halt (cstate sig S1 n c) =
271      halt_liftL S1 S2 halt (cstate … (lift_confL … c)).
272 #sig #n #S1 #S2 #halt #c cases c #s #t %
273 qed.
274
275 lemma trans_seq_liftL : ∀sig,n,M1,M2,s,a,news,move.
276   halt ?? M1 s = false → 
277   trans sig n M1 〈s,a〉 = 〈news,move〉 → 
278   trans sig n (seq sig n M1 M2) 〈inl … s,a〉 = 〈inl … news,move〉.
279 #sig #n (*#M1*) * #Q1 #T1 #init1 #halt1 #M2 #s #a #news #move
280 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
281 qed.
282
283 lemma trans_seq_liftR : ∀sig,n,M1,M2,s,a,news,move.
284   halt ?? M2 s = false → 
285   trans sig n M2 〈s,a〉 = 〈news,move〉 → 
286   trans sig n (seq sig n M1 M2) 〈inr … s,a〉 = 〈inr … news,move〉.
287 #sig #n #M1 * #Q2 #T2 #init2 #halt2 #s #a #news #move
288 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
289 qed.
290
291 lemma step_seq_liftR : ∀sig,n,M1,M2,c0.
292  halt ?? M2 (cstate ??? c0) = false → 
293  step sig n (seq sig n M1 M2) (lift_confR sig n (states ?? M1) (states ?? M2) c0) =
294  lift_confR sig n (states ?? M1) (states ?? M2) (step sig n M2 c0).
295 #sig #n #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s #t
296 lapply (refl ? (trans ??? 〈s,current_chars sig n t〉))
297 cases (trans ??? 〈s,current_chars sig n t〉) in ⊢ (???% → %);
298 #s0 #m0 #Heq #Hhalt whd in ⊢ (???(?????%)); >Heq  whd in ⊢ (???%);
299 whd in ⊢ (??(????%)?); whd in ⊢ (??%?); >(trans_seq_liftR … Heq) //
300 qed.
301
302 lemma step_seq_liftL : ∀sig,n,M1,M2,c0.
303  halt ?? M1 (cstate ??? c0) = false → 
304  step sig n (seq sig n M1 M2) (lift_confL sig n (states ?? M1) (states ?? M2) c0) =
305  lift_confL sig n ?? (step sig n M1 c0).
306 #sig #n #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s #t
307   lapply (refl ? (trans ??? 〈s,current_chars sig n t〉))
308   cases (trans ??? 〈s,current_chars sig n t〉) in ⊢ (???% → %);
309   #s0 #m0 #Heq #Hhalt
310   whd in ⊢ (???(?????%)); >Heq whd in ⊢ (???%);
311   whd in ⊢ (??(????%)?); whd in ⊢ (??%?); >(trans_seq_liftL … Heq) //
312 qed.
313
314 lemma trans_liftL_true : ∀sig,n,M1,M2,s,a.
315   halt ?? M1 s = true → 
316   trans sig n (seq sig n M1 M2) 〈inl … s,a〉 = 〈inr … (start ?? M2),null_action sig n〉.
317 #sig #n #M1 #M2 #s #a #Hhalt whd in ⊢ (??%?); >Hhalt %
318 qed.
319
320 lemma eq_ctape_lift_conf_L : ∀sig,n,S1,S2,outc.
321   ctapes sig (FinSum S1 S2) n (lift_confL … outc) = ctapes … outc.
322 #sig #n #S1 #S2 #outc cases outc #s #t %
323 qed.
324   
325 lemma eq_ctape_lift_conf_R : ∀sig,n,S1,S2,outc.
326   ctapes sig (FinSum S1 S2) n (lift_confR … outc) = ctapes … outc.
327 #sig #n #S1 #S2 #outc cases outc #s #t %
328 qed.
329
330 theorem sem_seq: ∀sig,n.∀M1,M2:mTM sig n.∀R1,R2.
331   M1 ⊨ R1 → M2 ⊨ R2 → M1 · M2 ⊨ R1 ∘ R2.
332 #sig #n #M1 #M2 #R1 #R2 #HR1 #HR2 #t 
333 cases (HR1 t) #k1 * #outc1 * #Hloop1 #HM1
334 cases (HR2 (ctapes sig (states ?? M1) n outc1)) #k2 * #outc2 * #Hloop2 #HM2
335 @(ex_intro … (k1+k2)) @(ex_intro … (lift_confR … outc2))
336 %
337 [@(loop_merge ??????????? 
338    (loop_lift ??? (lift_confL sig n (states sig n M1) (states sig n M2))
339    (step sig n M1) (step sig n (seq sig n M1 M2)) 
340    (λc.halt sig n M1 (cstate … c)) 
341    (λc.halt_liftL ?? (halt sig n M1) (cstate … c)) … Hloop1))
342   [ * *
343    [ #sl #tl whd in ⊢ (??%? → ?); #Hl %
344    | #sr #tr whd in ⊢ (??%? → ?); #Hr destruct (Hr) ]
345   || #c0 #Hhalt <step_seq_liftL //
346   | #x <p_halt_liftL %
347   |6:cases outc1 #s1 #t1 %
348   |7:@(loop_lift … (initc ??? (ctapes … outc1)) … Hloop2) 
349     [ * #s2 #t2 %
350     | #c0 #Hhalt <step_seq_liftR // ]
351   |whd in ⊢ (??(????%)?);whd in ⊢ (??%?);
352    generalize in match Hloop1; cases outc1 #sc1 #tc1 #Hloop10 
353    >(trans_liftL_true sig n M1 M2 ??) 
354     [ whd in ⊢ (??%?); whd in ⊢ (???%);
355       @mconfig_eq whd in ⊢ (???%); // 
356     | @(loop_Some ?????? Hloop10) ]
357  ]
358 | @(ex_intro … (ctapes ? (FinSum (states ?? M1) (states ?? M2)) ? (lift_confL … outc1)))
359   % // >eq_ctape_lift_conf_L >eq_ctape_lift_conf_R //
360 ]
361 qed.
362
363 theorem sem_seq_app: ∀sig,n.∀M1,M2:mTM sig n.∀R1,R2,R3.
364   M1 ⊨ R1 → M2 ⊨ R2 → R1 ∘ R2 ⊆ R3 → M1 · M2 ⊨ R3.
365 #sig #n #M1 #M2 #R1 #R2 #R3 #HR1 #HR2 #Hsub
366 #t cases (sem_seq … HR1 HR2 t)
367 #k * #outc * #Hloop #Houtc @(ex_intro … k) @(ex_intro … outc)
368 % [@Hloop |@Hsub @Houtc]
369 qed.
370
371 (* composition with guards *)
372 theorem sem_seq_guarded: ∀sig,n.∀M1,M2:mTM sig n.∀Pre1,Pre2,R1,R2.
373   GRealize sig n M1 Pre1 R1 → GRealize sig n M2 Pre2 R2 → 
374   (∀t1,t2.Pre1 t1 → R1 t1 t2 → Pre2 t2) → 
375   GRealize sig n (M1 · M2) Pre1 (R1 ∘ R2).
376 #sig #n #M1 #M2 #Pre1 #Pre2 #R1 #R2 #HGR1 #HGR2 #Hinv #t1 #HPre1
377 cases (HGR1 t1 HPre1) #k1 * #outc1 * #Hloop1 #HM1
378 cases (HGR2 (ctapes sig (states ?? M1) n outc1) ?) 
379   [2: @(Hinv … HPre1 HM1)]  
380 #k2 * #outc2 * #Hloop2 #HM2
381 @(ex_intro … (k1+k2)) @(ex_intro … (lift_confR … outc2))
382 %
383 [@(loop_merge ??????????? 
384    (loop_lift ??? (lift_confL sig n (states sig n M1) (states sig n M2))
385    (step sig n M1) (step sig n (seq sig n M1 M2)) 
386    (λc.halt sig n M1 (cstate … c)) 
387    (λc.halt_liftL ?? (halt sig n M1) (cstate … c)) … Hloop1))
388   [ * *
389    [ #sl #tl whd in ⊢ (??%? → ?); #Hl %
390    | #sr #tr whd in ⊢ (??%? → ?); #Hr destruct (Hr) ]
391   || #c0 #Hhalt <step_seq_liftL //
392   | #x <p_halt_liftL %
393   |6:cases outc1 #s1 #t1 %
394   |7:@(loop_lift … (initc ??? (ctapes … outc1)) … Hloop2) 
395     [ * #s2 #t2 %
396     | #c0 #Hhalt <step_seq_liftR // ]
397   |whd in ⊢ (??(????%)?);whd in ⊢ (??%?);
398    generalize in match Hloop1; cases outc1 #sc1 #tc1 #Hloop10 
399    >(trans_liftL_true sig n M1 M2 ??) 
400     [ whd in ⊢ (??%?); whd in ⊢ (???%);
401       @mconfig_eq whd in ⊢ (???%); //
402     | @(loop_Some ?????? Hloop10) ]
403  ]
404 | @(ex_intro … (ctapes ? (FinSum (states ?? M1) (states ?? M2)) n (lift_confL … outc1)))
405   % // >eq_ctape_lift_conf_L >eq_ctape_lift_conf_R //
406 ]
407 qed.
408
409 theorem sem_seq_app_guarded: ∀sig,n.∀M1,M2:mTM sig n.∀Pre1,Pre2,R1,R2,R3.
410   GRealize sig n M1 Pre1 R1 → GRealize sig n M2 Pre2 R2 → 
411   (∀t1,t2.Pre1 t1 → R1 t1 t2 → Pre2 t2) → R1 ∘ R2 ⊆ R3 →
412   GRealize sig n (M1 · M2) Pre1 R3.
413 #sig #n #M1 #M2 #Pre1 #Pre2 #R1 #R2 #R3 #HR1 #HR2 #Hinv #Hsub
414 #t #HPre1 cases (sem_seq_guarded … HR1 HR2 Hinv t HPre1)
415 #k * #outc * #Hloop #Houtc @(ex_intro … k) @(ex_intro … outc)
416 % [@Hloop |@Hsub @Houtc]
417 qed.
418
419 theorem acc_sem_seq : ∀sig,n.∀M1,M2:mTM sig n.∀R1,Rtrue,Rfalse,acc.
420   M1 ⊨ R1 → M2 ⊨ [ acc: Rtrue, Rfalse ] → 
421   M1 · M2 ⊨ [ inr … acc: R1 ∘ Rtrue, R1 ∘ Rfalse ].
422 #sig #n #M1 #M2 #R1 #Rtrue #Rfalse #acc #HR1 #HR2 #t 
423 cases (HR1 t) #k1 * #outc1 * #Hloop1 #HM1
424 cases (HR2 (ctapes sig (states ?? M1) n outc1)) #k2 * #outc2 * * #Hloop2 
425 #HMtrue #HMfalse
426 @(ex_intro … (k1+k2)) @(ex_intro … (lift_confR … outc2))
427 % [ %
428 [@(loop_merge ??????????? 
429    (loop_lift ??? (lift_confL sig n (states sig n M1) (states sig n M2))
430    (step sig n M1) (step sig n (seq sig n M1 M2)) 
431    (λc.halt sig n M1 (cstate … c)) 
432    (λc.halt_liftL ?? (halt sig n M1) (cstate … c)) … Hloop1))
433   [ * *
434    [ #sl #tl whd in ⊢ (??%? → ?); #Hl %
435    | #sr #tr whd in ⊢ (??%? → ?); #Hr destruct (Hr) ]
436   || #c0 #Hhalt <step_seq_liftL //
437   | #x <p_halt_liftL %
438   |6:cases outc1 #s1 #t1 %
439   |7:@(loop_lift … (initc ??? (ctapes … outc1)) … Hloop2) 
440     [ * #s2 #t2 %
441     | #c0 #Hhalt <step_seq_liftR // ]
442   |whd in ⊢ (??(????%)?);whd in ⊢ (??%?);
443    generalize in match Hloop1; cases outc1 #sc1 #tc1 #Hloop10 
444    >(trans_liftL_true sig n M1 M2 ??) 
445     [ whd in ⊢ (??%?); whd in ⊢ (???%);
446       @mconfig_eq whd in ⊢ (???%); // 
447     | @(loop_Some ?????? Hloop10) ]
448  ]
449 | >(mconfig_expand … outc2) in ⊢ (%→?); whd in ⊢ (??%?→?); 
450   #Hqtrue destruct (Hqtrue)
451   @(ex_intro … (ctapes ? (FinSum (states ?? M1) (states ?? M2)) ? (lift_confL … outc1)))
452   % // >eq_ctape_lift_conf_L >eq_ctape_lift_conf_R /2/ ]
453 | >(mconfig_expand … outc2) in ⊢ (%→?); whd in ⊢ (?(??%?)→?); #Hqfalse
454   @(ex_intro … (ctapes ? (FinSum (states ?? M1) (states ?? M2)) ? (lift_confL … outc1)))
455   % // >eq_ctape_lift_conf_L >eq_ctape_lift_conf_R @HMfalse
456   @(not_to_not … Hqfalse) //
457 ]
458 qed.
459
460 lemma acc_sem_seq_app : ∀sig,n.∀M1,M2:mTM sig n.∀R1,Rtrue,Rfalse,R2,R3,acc.
461   M1 ⊨ R1 → M2 ⊨ [acc: Rtrue, Rfalse] → 
462     (∀t1,t2,t3. R1 t1 t3 → Rtrue t3 t2 → R2 t1 t2) → 
463     (∀t1,t2,t3. R1 t1 t3 → Rfalse t3 t2 → R3 t1 t2) → 
464     M1 · M2 ⊨ [inr … acc : R2, R3].    
465 #sig #n #M1 #M2 #R1 #Rtrue #Rfalse #R2 #R3 #acc
466 #HR1 #HRacc #Hsub1 #Hsub2 
467 #t cases (acc_sem_seq … HR1 HRacc t)
468 #k * #outc * * #Hloop #Houtc1 #Houtc2 @(ex_intro … k) @(ex_intro … outc)
469 % [% [@Hloop
470      |#H cases (Houtc1 H) #t3 * #Hleft #Hright @Hsub1 // ]
471   |#H cases (Houtc2 H) #t3 * #Hleft #Hright @Hsub2 // ]
472 qed.