]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/multi_universal/unistep_aux.ma
Yippeee! Completes proof of soundness of the universal machine
[helm.git] / matita / matita / lib / turing / multi_universal / unistep_aux.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 "turing/multi_universal/moves_2.ma".
13 include "turing/multi_universal/match.ma".
14 include "turing/multi_universal/copy.ma".
15 include "turing/multi_universal/alphabet.ma".
16 include "turing/multi_universal/tuples.ma".
17
18
19 definition obj ≝ (0:DeqNat).
20 definition cfg ≝ (1:DeqNat).
21 definition prg ≝ (2:DeqNat).
22
23 definition obj_to_cfg ≝
24   (ifTM ?? (inject_TM ? (test_null ?) 2 obj)
25     (copy_char_N obj cfg FSUnialpha 2)
26     (inject_TM ? (write FSUnialpha null) 2 cfg)
27      tc_true) ·
28   inject_TM ? (move_to_end FSUnialpha L) 2 cfg ·
29   mmove cfg FSUnialpha 2 R.
30   
31 definition R_obj_to_cfg ≝ λt1,t2:Vector (tape FSUnialpha) 3.
32   ∀c,ls.
33   nth cfg ? t1 (niltape ?) = midtape ? ls c [ ] → 
34   (∀lso,x,rso.nth obj ? t1 (niltape ?) = midtape FSUnialpha lso x rso → 
35    t2 = change_vec ?? t1 
36          (mk_tape ? [ ] (option_hd ? (reverse ? (x::ls))) (tail ? (reverse ? (x::ls)))) cfg) ∧
37   (current ? (nth obj ? t1 (niltape ?)) = None ? → 
38    t2 = change_vec ?? t1
39          (mk_tape ? [ ] (option_hd FSUnialpha (reverse ? (null::ls))) 
40            (tail ? (reverse ? (null::ls)))) cfg).
41
42 (*
43 axiom accRealize_to_Realize :
44   ∀sig,n.∀M:mTM sig n.∀Rtrue,Rfalse,acc.
45   M ⊨ [ acc: Rtrue, Rfalse ] →  M ⊨ Rtrue ∪ Rfalse.
46 *)
47   
48 lemma eq_mk_tape_rightof :
49  ∀alpha,a,al.mk_tape alpha (a::al) (None ?) [ ] = rightof ? a al.
50 #alpha #a #al %
51 qed.
52
53 lemma tape_move_mk_tape_R :
54   ∀sig,ls,c,rs.
55   (c = None ? → ls = [ ] ∨ rs = [ ]) → 
56   tape_move ? (mk_tape sig ls c rs) R =
57   mk_tape ? (option_cons ? c ls) (option_hd ? rs) (tail ? rs).
58 #sig * [ * [ * | #c * ] | #l0 #ls0 * [ *
59 [| #r0 #rs0 #H @False_ind cases (H (refl ??)) #H1 destruct (H1) ] | #c * ] ] 
60 normalize //
61 qed.
62
63 lemma None_or_Some: ∀A.∀a. a =None A ∨ ∃b. a = Some ? b.
64 #A * /2/ #a %2 %{a} %
65 qed.
66
67 lemma not_None_to_Some: ∀A.∀a. a ≠ None A → ∃b. a = Some ? b.
68 #A * /2/ * #H @False_ind @H %
69 qed. 
70
71 lemma sem_obj_to_cfg : obj_to_cfg ⊨  R_obj_to_cfg.
72 @(sem_seq_app FSUnialpha 2 ????? 
73   (sem_if ??????????
74    (sem_test_null_multi ?? obj ?)
75    (sem_copy_char_N …)
76    (sem_inject ???? cfg ? (sem_write FSUnialpha null)))
77   (sem_seq ?????? (sem_inject ???? cfg ? (sem_move_to_end_l ?))
78     (sem_move_multi ? 2 cfg R ?))) //
79 #ta #tout *
80 #tb * #Hif * #tc * #HM2 #HM3 #c #ls #Hcfg
81 (* Hif *)
82 cases Hif -Hif
83 [ * #t1 * * #Hcurta #Ht1 destruct (Ht1)
84   lapply (not_None_to_Some … Hcurta) * #curta #Hcurtaeq
85   whd in ⊢ (%→?); #Htb % [2: #Hcur @False_ind /2/]
86   #lso #xo #rso #Hobjta cases HM2 whd in ⊢ (%→?); * #_
87   #HM2 #Heq >Htb in HM2; >nth_change_vec [2: @leb_true_to_le %]
88   >Hcfg >Hcurtaeq #HM2 lapply (HM2 … (refl ??)) -HM2 
89   whd in match (left ??); whd in match (right ??);
90   >reverse_cons #Htc >HM3 @(eq_vec … (niltape ?)) #i #lei2 
91   cases (le_to_or_lt_eq … (le_S_S_to_le …lei2))
92   [#lei1 cases (le_to_or_lt_eq … (le_S_S_to_le …lei1))
93     [#lei0 lapply(le_n_O_to_eq … (le_S_S_to_le …lei0)) #eqi <eqi
94      >nth_change_vec_neq [2:@eqb_false_to_not_eq %] 
95      <(Heq 0) [2:@eqb_false_to_not_eq %] >Htb
96      >nth_change_vec_neq [2:@eqb_false_to_not_eq %]
97      >nth_change_vec_neq [%|2:@eqb_false_to_not_eq %] 
98     |#Hi >Hi >nth_change_vec // >nth_change_vec // >Htc
99      >Hobjta in Hcurtaeq; whd in ⊢ (??%?→?); #Htmp destruct(Htmp)
100      >tape_move_mk_tape_R [2: #_ %1 %] %
101     ]
102   |#Hi >Hi >nth_change_vec_neq [2:@eqb_false_to_not_eq %] 
103    >nth_change_vec_neq [2:@eqb_false_to_not_eq %] 
104    <(Heq 2) [2:@eqb_false_to_not_eq %] >Htb
105    >nth_change_vec_neq [%|2:@eqb_false_to_not_eq %] 
106   ]
107 | * #t1 * * #Hcurta #Ht1 destruct (Ht1)
108   * whd in ⊢ (%→?); #Htb lapply (Htb … Hcfg) -Htb #Htb
109   #Htbeq % 
110     [#lso #xo #rso #Hmid @False_ind >Hmid in Hcurta;
111      whd in ⊢ (??%?→?); #Htmp destruct (Htmp)]
112   #_ cases HM2 whd in ⊢ (%→?); * #_
113   #HM2 #Heq >Htb in HM2; #HM2 lapply (HM2 … (refl ??)) -HM2 
114   #Htc >HM3 @(eq_vec … (niltape ?)) #i #lei2 
115   cases (le_to_or_lt_eq … (le_S_S_to_le …lei2))
116   [#lei1 cases (le_to_or_lt_eq … (le_S_S_to_le …lei1))
117     [#lei0 lapply(le_n_O_to_eq … (le_S_S_to_le …lei0)) #eqi <eqi
118      >nth_change_vec_neq [2:@eqb_false_to_not_eq %]
119      >nth_change_vec_neq [2:@eqb_false_to_not_eq %]
120      <(Heq 0) [2:@eqb_false_to_not_eq %] >Htb
121      <(Htbeq 0) [2:@eqb_false_to_not_eq %] % 
122     |#Hi >Hi >nth_change_vec // >nth_change_vec // >Htc
123      >tape_move_mk_tape_R [2: #_ %1 %] >reverse_cons % 
124     ]
125   |#Hi >Hi >nth_change_vec_neq [2:@eqb_false_to_not_eq %] 
126    >nth_change_vec_neq [2:@eqb_false_to_not_eq %] 
127    <(Heq 2) [2:@eqb_false_to_not_eq %] 
128    <(Htbeq 2) [%|@eqb_false_to_not_eq %] 
129   ]
130 ]
131 qed.
132
133 (* another semantics for obj_to_cfg *)
134 definition low_char' ≝ λc.
135   match c with
136   [ None ⇒ null 
137   | Some b ⇒ if (is_bit b) then b else null
138   ].
139   
140 lemma low_char_option : ∀s.
141   low_char' (option_map FinBool FSUnialpha bit s) = low_char s.
142 * //
143 qed.
144
145 definition R_obj_to_cfg1 ≝ λt1,t2:Vector (tape FSUnialpha) 3.
146   ∀c,ls.
147   nth cfg ? t1 (niltape ?) = midtape ? ls c [ ] → 
148   let x ≝ current ? (nth obj ? t1 (niltape ?)) in
149   (∀b. x= Some ? b → is_bit b = true) →
150   t2 = change_vec ?? t1
151          (mk_tape ? [ ] (option_hd FSUnialpha (reverse ? (low_char' x::ls))) 
152            (tail ? (reverse ? (low_char' x::ls)))) cfg.
153
154 lemma sem_obj_to_cfg1: obj_to_cfg ⊨ R_obj_to_cfg1.
155 @(Realize_to_Realize … sem_obj_to_cfg) #t1 #t2 #Hsem
156 #c #ls #Hcfg lapply(Hsem c ls Hcfg) * #HSome #HNone #Hb 
157 cases (None_or_Some ? (current ? (nth obj ? t1 (niltape ?)))) 
158   [#Hcur >Hcur @HNone @Hcur
159   |* #b #Hb1 >Hb1
160    cut (low_char' (Some ? b) = b)  [whd in ⊢ (??%?); >(Hb b Hb1) %] #Hlow >Hlow
161    lapply(current_to_midtape … Hb1) * #lsobj * #rsobj #Hmid
162    @(HSome … Hmid)
163   ]
164 qed.
165     
166 (* test_null_char *)
167 definition test_null_char ≝ test_char FSUnialpha (λc.c == null).
168
169 definition R_test_null_char_true ≝ λt1,t2.
170   current FSUnialpha t1 = Some ? null ∧ t1 = t2.
171   
172 definition R_test_null_char_false ≝ λt1,t2.
173   current FSUnialpha t1 ≠ Some ? null ∧ t1 = t2.
174   
175 lemma sem_test_null_char :
176   test_null_char ⊨ [ tc_true : R_test_null_char_true, R_test_null_char_false].
177 #t1 cases (sem_test_char FSUnialpha (λc.c == null) t1) #k * #outc * * #Hloop #Htrue
178 #Hfalse %{k} %{outc} % [ %
179 [ @Hloop
180 | #Houtc cases (Htrue ?) [| @Houtc] * #c * #Hcurt1 #Hcnull lapply (\P Hcnull)
181   -Hcnull #H destruct (H) #Houtc1 %
182   [ @Hcurt1 | <Houtc1 % ] ]
183 | #Houtc cases (Hfalse ?) [| @Houtc] #Hc #Houtc %
184   [ % #Hcurt1 >Hcurt1 in Hc; #Hc lapply (Hc ? (refl ??)) 
185     >(?:((null:FSUnialpha) == null) = true) [|@(\b (refl ??)) ]
186     #H destruct (H)
187   | <Houtc % ] ]
188 qed.
189
190 definition cfg_to_obj ≝
191   mmove cfg FSUnialpha 2 L ·
192   (ifTM ?? (inject_TM ? test_null_char 2 cfg)
193     (nop ? 2)
194     (copy_char_N cfg obj FSUnialpha 2)
195     tc_true).
196 (* ·
197   inject_TM ? (move_to_end FSUnialpha L) 2 cfg ·
198   mmove cfg FSUnialpha 2 R. *)
199   
200 definition R_cfg_to_obj ≝ λt1,t2:Vector (tape FSUnialpha) 3.
201   ∀c,ls.
202   nth cfg ? t1 (niltape ?) = mk_tape FSUnialpha (c::ls) (None ?) [ ] → 
203   (c = null → t2 = change_vec ?? t1 (midtape ? ls c [ ]) cfg) ∧
204   (c ≠ null → 
205    t2 = change_vec ??
206           (change_vec ?? t1
207              (midtape ? (left ? (nth obj ? t1 (niltape ?))) c (right ? (nth obj ? t1 (niltape ?)))) obj)
208           (midtape ? ls c [ ]) cfg).
209           
210 lemma tape_move_mk_tape_L :
211   ∀sig,ls,c,rs.
212   (c = None ? → ls = [ ] ∨ rs = [ ]) → 
213   tape_move ? (mk_tape sig ls c rs) L =
214   mk_tape ? (tail ? ls) (option_hd ? ls) (option_cons ? c rs).
215 #sig * [ * [ * | #c * ] | #l0 #ls0 * [ *
216 [| #r0 #rs0 #H @False_ind cases (H (refl ??)) #H1 destruct (H1) ] | #c * ] ] 
217 normalize //
218 qed.
219
220 lemma sem_cfg_to_obj : cfg_to_obj ⊨  R_cfg_to_obj.
221 @(sem_seq_app FSUnialpha 2 ????? (sem_move_multi ? 2 cfg L ?)
222   (sem_if ??????????
223    (acc_sem_inject ?????? cfg ? sem_test_null_char)
224    (sem_nop …)
225    (sem_copy_char_N …)))
226 // [@sym_not_eq //]
227 #ta #tb *
228 #tc * whd in ⊢ (%→?); #Htc *
229 [ * #te * * * #Hcurtc #Hte1 #Hte2 whd in ⊢ (%→?); #Htb destruct (Htb)
230   #c #ls #Hta %
231   [ #Hc >Hta in Htc; >eq_mk_tape_rightof whd in match (tape_move ???); #Htc
232     cut (te = tc)
233     [ lapply (eq_vec_change_vec ??????? (sym_eq … Hte1) Hte2) >change_vec_same // ]
234       -Hte1 -Hte2 #Hte //
235     | #Hc >Hta in Htc; >eq_mk_tape_rightof whd in match (tape_move ???); #Htc
236       >Htc in Hcurtc; >nth_change_vec // normalize in ⊢ (%→?); 
237       #H destruct (H) @False_ind cases Hc /2/ ]
238 | * #te * * * #Hcurtc #Hte1 #Hte2
239   whd in ⊢ (%→?); #Htb
240   #c #ls #Hta % #Hc
241   [ >Htc in Hcurtc; >Hta >nth_change_vec // 
242     normalize in ⊢ (%→?); * #H @False_ind /2/
243   | cut (te = tc)
244     [ lapply (eq_vec_change_vec ??????? (sym_eq … Hte1) Hte2)
245       >change_vec_same // ] -Hte1 -Hte2 #Hte destruct (Hte)
246     >Hta in Htc; whd in match (tape_move ???); #Htc
247     >Htc in Htb; >nth_change_vec // 
248     >nth_change_vec_neq [2:@eqb_false_to_not_eq //] >Hta 
249     #Htb @Htb
250   ]
251 qed.
252
253 definition char_to_move ≝ λc.match c with
254   [ bit b ⇒ if b then R else L
255   | _ ⇒ N].
256
257 definition char_to_bit_option ≝ λc.match c with
258   [ bit b ⇒ Some ? (bit b)
259   | _ ⇒ None ?]. 
260
261 definition R_cfg_to_obj1 ≝ λt1,t2:Vector (tape FSUnialpha) 3.
262   ∀c,ls.
263   nth cfg ? t1 (niltape ?) = mk_tape FSUnialpha (c::ls) (None ?) [ ] → 
264   c ≠ bar →
265   let new_obj ≝ 
266       tape_write ? (nth obj ? t1 (niltape ?)) (char_to_bit_option c) in
267     t2 = change_vec ??
268           (change_vec ?? t1
269             (tape_write ? (nth obj ? t1 (niltape ?)) (char_to_bit_option c)) obj)
270           (midtape ? ls c [ ]) cfg.
271
272 lemma sem_cfg_to_obj1: cfg_to_obj ⊨  R_cfg_to_obj1.
273 @(Realize_to_Realize … sem_cfg_to_obj) #t1 #t2 #H #c #ls #Hcfg #Hbar
274 cases (H c ls Hcfg) cases (true_or_false (c==null)) #Hc
275   [#Ht2 #_ >(Ht2 (\P Hc)) -Ht2 @(eq_vec … (niltape ?))
276    #i #lei2 cases (le_to_or_lt_eq … (le_S_S_to_le …lei2))
277     [#lei1 cases (le_to_or_lt_eq … (le_S_S_to_le …lei1))
278       [#lei0 lapply(le_n_O_to_eq … (le_S_S_to_le …lei0)) #eqi <eqi
279        >nth_change_vec_neq [2:@eqb_false_to_not_eq %] 
280        >nth_change_vec_neq in ⊢ (???%); [2:@eqb_false_to_not_eq %]
281        >nth_change_vec // >(\P Hc) % 
282       |#Hi >Hi >nth_change_vec //
283       ] 
284     |#Hi >Hi >nth_change_vec_neq [2:@eqb_false_to_not_eq %]
285      >nth_change_vec_neq [2:@eqb_false_to_not_eq %]
286      >nth_change_vec_neq [2:@eqb_false_to_not_eq %] %
287     ]
288   |#_ #Ht2 >(Ht2 (\Pf Hc)) -Ht2 @(eq_vec … (niltape ?))
289    #i #lei2 cases (le_to_or_lt_eq … (le_S_S_to_le …lei2))
290     [#lei1 cases (le_to_or_lt_eq … (le_S_S_to_le …lei1))
291       [#lei0 lapply(le_n_O_to_eq … (le_S_S_to_le …lei0)) #eqi <eqi
292        >nth_change_vec_neq [2:@eqb_false_to_not_eq %] 
293        >nth_change_vec_neq in ⊢ (???%); [2:@eqb_false_to_not_eq %]
294        >nth_change_vec // >nth_change_vec // 
295        lapply (\bf Hbar) lapply Hc elim c //
296         [whd in ⊢ (??%?→?); #H destruct (H)
297         |#_ whd in ⊢ (??%?→?); #H destruct (H)
298         ]
299       |#Hi >Hi >nth_change_vec //
300       ] 
301     |#Hi >Hi >nth_change_vec_neq [2:@eqb_false_to_not_eq %]
302      >nth_change_vec_neq [2:@eqb_false_to_not_eq %]
303      >nth_change_vec_neq [2:@eqb_false_to_not_eq %] %
304     ]
305   ]
306 qed.
307        
308
309 (* macchina che muove il nastro obj a destra o sinistra a seconda del valore
310    del current di prg, che codifica la direzione in cui ci muoviamo *)
311       
312 definition tape_move_obj : mTM FSUnialpha 2 ≝ 
313   ifTM ?? 
314    (inject_TM ? (test_char ? (λc:FSUnialpha.c == bit false)) 2 prg)
315    (mmove obj FSUnialpha 2 L)
316    (ifTM ?? 
317     (inject_TM ? (test_char ? (λc:FSUnialpha.c == bit true)) 2 prg)
318     (mmove obj FSUnialpha 2 R)
319     (nop ??)
320     tc_true)
321    tc_true.
322
323 definition R_tape_move_obj' ≝ λt1,t2:Vector (tape FSUnialpha) 3.
324   (current ? (nth prg ? t1 (niltape ?)) = Some ? (bit false) → 
325    t2 = change_vec ?? t1 (tape_move ? (nth obj ? t1 (niltape ?)) L) obj) ∧
326   (current ? (nth prg ? t1 (niltape ?)) = Some ? (bit true) → 
327    t2 = change_vec ?? t1 (tape_move ? (nth obj ? t1 (niltape ?)) R) obj) ∧
328   (current ? (nth prg ? t1 (niltape ?)) ≠ Some ? (bit false) →
329    current ? (nth prg ? t1 (niltape ?)) ≠ Some ? (bit true) →  
330    t2 = t1).
331    
332 lemma sem_tape_move_obj' : tape_move_obj ⊨ R_tape_move_obj'.
333 #ta cases (sem_if ??????????
334   (acc_sem_inject ?????? prg ? (sem_test_char ? (λc:FSUnialpha.c == bit false)))
335   (sem_move_multi ? 2 obj L ?)
336   (sem_if ??????????
337    (acc_sem_inject ?????? prg ? (sem_test_char ? (λc:FSUnialpha.c == bit true)))
338    (sem_move_multi ? 2 obj R ?)
339    (sem_nop …)) ta) //
340 #i * #outc * #Hloop #HR %{i} %{outc} % [@Hloop] -i
341 cases HR -HR
342 [ * #tb * * * * #c * #Hcurta_prg #Hc lapply (\P Hc) -Hc #Hc #Htb1 #Htb2
343   whd in ⊢ (%→%); #Houtc >Houtc -Houtc % [ %
344   [ >Hcurta_prg #H destruct (H) >(?:tb = ta) 
345     [| lapply (eq_vec_change_vec ??????? Htb1 Htb2)
346        >change_vec_same // ] %
347   | >Hcurta_prg #H destruct (H) destruct (Hc) ]
348   | >Hcurta_prg >Hc * #H @False_ind /2/ ]
349 | * #tb * * * #Hnotfalse #Htb1 #Htb2 cut (tb = ta) 
350   [ lapply (eq_vec_change_vec ??????? Htb1 Htb2)
351      >change_vec_same // ] -Htb1 -Htb2 #Htb destruct (Htb) *
352   [ * #tc * * * * #c * #Hcurta_prg #Hc lapply (\P Hc) -Hc #Hc #Htc1 #Htc2
353     whd in ⊢ (%→%); #Houtc >Houtc -Houtc % [ %
354     [ >Hcurta_prg #H destruct (H) destruct (Hc)
355     | >Hcurta_prg #H destruct (H) >(?:tc = ta) 
356       [| lapply (eq_vec_change_vec ??????? Htc1 Htc2)
357         >change_vec_same // ] % ]
358     | >Hcurta_prg >Hc #_ * #H @False_ind /2/ ]
359   | * #tc * * * #Hnottrue #Htc1 #Htc2 cut (tc = ta) 
360     [ lapply (eq_vec_change_vec ??????? Htc1 Htc2)
361       >change_vec_same // ] -Htc1 -Htc2 
362     #Htc destruct (Htc) whd in ⊢ (%→?); #Houtc % [ %
363     [ #Hcurta_prg lapply (\Pf (Hnotfalse ? Hcurta_prg)) * #H @False_ind /2/
364     | #Hcurta_prg lapply (\Pf (Hnottrue ? Hcurta_prg)) * #H @False_ind /2/ ]
365     | #_ #_ @Houtc ]
366   ]
367 ]
368 qed.
369
370 definition R_tape_move_obj ≝ λt1,t2:Vector (tape FSUnialpha) 3.
371   ∀c. current ? (nth prg ? t1 (niltape ?)) = Some ? c → 
372   t2 = change_vec ?? t1 (tape_move ? (nth obj ? t1 (niltape ?)) (char_to_move c)) obj.
373
374 lemma sem_tape_move_obj : tape_move_obj ⊨ R_tape_move_obj.
375 @(Realize_to_Realize … sem_tape_move_obj')
376 #ta #tb * * #Htb1 #Htb2 #Htb3 * [ *
377 [ @Htb2 | @Htb1 ] 
378 | #Hcurta_prg change with (nth obj ? ta (niltape ?)) in match (tape_move ???);
379   >change_vec_same @Htb3 >Hcurta_prg % #H destruct (H)
380 | #Hcurta_prg change with (nth obj ? ta (niltape ?)) in match (tape_move ???);
381   >change_vec_same @Htb3 >Hcurta_prg % #H destruct (H)
382 ]
383 qed.
384
385 (************** list of tape ******************)
386 definition list_of_tape ≝ λsig.λt:tape sig.
387   reverse ? (left ? t)@option_cons ? (current ? t) (right ? t).
388
389 lemma list_of_midtape: ∀sig,ls,c,rs.
390   list_of_tape sig (midtape ? ls c rs) = reverse ? ls@c::rs.
391 // qed-.
392
393 lemma list_of_rightof: ∀sig,ls,c.
394   list_of_tape sig (rightof ? c ls) = reverse ? (c::ls).
395 #sig #ls #c <(append_nil ? (reverse ? (c::ls)))
396 // qed-.
397
398 lemma list_of_tape_move: ∀sig,t,m.
399   list_of_tape sig t = list_of_tape sig (tape_move ? t m).
400 #sig #t * // cases t //
401  [(* rightof, move L *) #a #l >list_of_midtape 
402   >append_cons <reverse_single <reverse_append %
403  |(* midtape, move L *) * // 
404   #a #ls #c #rs >list_of_midtape >list_of_midtape
405   >reverse_cons >associative_append %
406  |(* midtape, move R *) #ls #c * 
407    [>list_of_midtape >list_of_rightof >reverse_cons %
408    |#a #rs >list_of_midtape >list_of_midtape >reverse_cons 
409     >associative_append %
410    ]
411  ]
412 qed.
413
414 lemma list_of_tape_write: ∀sig,cond,t,c. 
415 (∀b. c = Some ? b → cond b =true) →
416 (∀x. mem ? x (list_of_tape ? t) → cond x =true ) →
417 ∀x. mem ? x (list_of_tape sig (tape_write ? t c)) → cond x =true.
418 #sig #cond #t #c #Hc #Htape #x lapply Hc cases c 
419   [(* c is None *) #_ whd in match (tape_write ???); @Htape
420   |#b #Hb lapply (Hb … (refl ??)) -Hb #Hb
421    whd in match (tape_write ???); >list_of_midtape
422    #Hx cases(mem_append ???? Hx) -Hx
423     [#Hx @Htape @mem_append_l1 @Hx
424     |* [//] 
425      #Hx @Htape @mem_append_l2 cases (current sig t)
426       [@Hx | #c1 %2 @Hx]
427     ]
428   ]
429 qed.
430    
431 lemma current_in_list: ∀sig,t,b. 
432   current sig t = Some ? b → mem ? b (list_of_tape sig t).
433 #sig #t #b cases t
434   [whd in ⊢ (??%?→?); #Htmp destruct
435   |#l #b whd in ⊢ (??%?→?); #Htmp destruct
436   |#l #b whd in ⊢ (??%?→?); #Htmp destruct
437   |#ls #c #rs whd in ⊢ (??%?→?); #Htmp destruct
438    >list_of_midtape @mem_append_l2 % %
439   ]
440 qed.
441   
442 definition restart_tape ≝ λi,n. 
443   mmove i FSUnialpha n L ·
444   inject_TM ? (move_to_end FSUnialpha L) n i ·
445   mmove i FSUnialpha n R.
446   
447 definition R_restart_tape ≝ λi,n.λint,outt:Vector (tape FSUnialpha) (S n).
448    ∀t.t = nth i ? int (niltape ?) → 
449    outt = change_vec ?? int 
450     (mk_tape ? [ ] (option_hd ? (list_of_tape ? t)) (tail ? (list_of_tape ? t))) i.
451
452 lemma sem_restart_tape : ∀i,n.i < S n → restart_tape i n ⊨ R_restart_tape i n.
453 #i #n #Hleq
454 @(sem_seq_app ??????? (sem_move_multi ? n i L ?)
455   (sem_seq ?????? (sem_inject ???? i ? (sem_move_to_end_l ?))
456    (sem_move_multi ? n i R ?))) [1,2,3:@le_S_S_to_le //]
457 #ta #tb * #tc * whd in ⊢ (%→?); #Htc
458 * #td * * * #Htd1 #Htd2 #Htd3 
459 whd in ⊢ (%→?); #Htb *
460 [ #Hta_i <Hta_i in Htc; whd in ⊢ (???(????%?)→?); #Htc
461   cut (td = tc) 
462   [ <(change_vec_same … tc … i … (niltape ?))
463     @(eq_vec_change_vec … (niltape ?))
464     [ @Htd1 >Htc >nth_change_vec //
465     | @Htd3 ] ]
466   (* >Htc in Htd1; >nth_change_vec // *) -Htd1 -Htd2 -Htd3
467   #Htd >Htd in Htb; >Htc >change_vec_change_vec >nth_change_vec //
468   #Htb >Htb %
469 | #r0 #rs0 #Hta_i <Hta_i in Htc; whd in ⊢ (???(????%?)→?); #Htc
470   cut (td = tc)
471   [ <(change_vec_same … tc … i … (niltape ?))
472     @(eq_vec_change_vec … (niltape ?))
473     [ @Htd1 >Htc >nth_change_vec //
474     | @Htd3 ] ]
475   (* >Htc in Htd1; >nth_change_vec // *) -Htd1 -Htd2 -Htd3
476   #Htd >Htd in Htb; >Htc >change_vec_change_vec >nth_change_vec //
477   #Htb >Htb %
478 | #l0 #ls0 #Hta_i <Hta_i in Htc; whd in ⊢ (???(????%?)→?); #Htc
479   cut (td = change_vec ?? tc (mk_tape ? [ ] (None ?) (reverse ? ls0@[l0])) i)
480   [ <(change_vec_same … tc … i … (niltape ?))
481     @(eq_vec_change_vec … (niltape ?))
482     [ @Htd2 >Htc >nth_change_vec //
483     | #j #Hij >nth_change_vec_neq // @Htd3 // ]]
484   #Htd >Htd in Htb; >Htc >change_vec_change_vec >change_vec_change_vec
485   >nth_change_vec // #Htb >Htb <(reverse_reverse ? ls0) in ⊢ (???%);
486   cases (reverse ? ls0)
487   [ %
488   | #l1 #ls1 >reverse_cons
489      >(?: list_of_tape ? (rightof ? l0 (reverse ? ls1@[l1])) =
490           l1::ls1@[l0])
491      [|change with (reverse ??@?) in ⊢ (??%?);
492        whd in match (left ??); >reverse_cons >reverse_append 
493        whd in ⊢ (??%?); @eq_f >reverse_reverse normalize >append_nil % ] % ]
494 | * 
495   [ #c #rs #Hta_i <Hta_i in Htc; whd in ⊢ (???(????%?)→?); #Htc
496     cut (td = tc) 
497     [ <(change_vec_same … tc … i … (niltape ?))
498     @(eq_vec_change_vec … (niltape ?))
499     [ @Htd1 >Htc >nth_change_vec //
500     | @Htd3 ] ]
501     (* >Htc in Htd1; >nth_change_vec // *) -Htd1 -Htd2 -Htd3
502     #Htd >Htd in Htb; >Htc >change_vec_change_vec >nth_change_vec //
503     #Htb >Htb %
504   | #l0 #ls0 #c #rs #Hta_i <Hta_i in Htc; whd in ⊢ (???(????%?)→?); #Htc
505     cut (td = change_vec ?? tc (mk_tape ? [ ] (None ?) (reverse ? ls0@l0::c::rs)) i)
506     [ @(eq_vec_change_vec … (niltape ?))
507       [ @Htd2 >Htc >nth_change_vec //
508       | @Htd3 ] ]
509     #Htd >Htd in Htb; >Htc >change_vec_change_vec >change_vec_change_vec
510     >nth_change_vec // #Htb >Htb <(reverse_reverse ? ls0) in ⊢ (???%);
511     cases (reverse ? ls0)
512     [ %
513     | #l1 #ls1 >reverse_cons
514       >(?: list_of_tape ? (midtape ? (l0::reverse ? ls1@[l1]) c rs) =
515             l1::ls1@l0::c::rs)
516       [|change with (reverse ??@?) in ⊢ (??%?);
517         whd in match (left ??); >reverse_cons >reverse_append 
518         whd in ⊢ (??%?); @eq_f >reverse_reverse normalize
519         >associative_append % ] % ]
520   ]
521 ]
522 qed.