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