]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/multi_universal/unistep_aux.ma
4f37564c0ea459d8aa8365813dec2084917e13e5
[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
20   in.obj : ...  x ...
21                 ^
22   in.cfg : ...  ? ? ...
23                     ^
24                 
25   out.cfg : ... 1 x ...
26                   ^
27                   
28   ---------------------
29   current (in.obj) = None
30   
31   in.cfg : ...  ? ? ...
32                     ^
33
34   out.cfg : ... 0 0 ...
35                   ^
36                   
37   obj_to_cfg ≝ 
38     move_l(cfg);
39     move_l(cfg);
40     (if (current(in.obj)) == None
41        then write(0,cfg);
42             move_r(cfg);
43             write(0,cfg);
44        else write(1,cfg);
45             move_r(cfg);
46             copy_step(obj,cfg);
47             move_l(obj);)
48     move_to_end_l(cfg);
49     move_r(cfg);
50        
51   
52   cfg_to_obj
53 *)
54
55 definition copy_char_states ≝ initN 3.
56
57 definition cc0 : copy_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 3 (refl …)).
58 definition cc1 : copy_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 3 (refl …)).
59
60 definition trans_copy_char ≝ 
61  λsrc,dst.λsig:FinSet.λn.
62  λp:copy_char_states × (Vector (option sig) (S n)).
63  let 〈q,a〉 ≝ p in
64  match pi1 … q with
65  [ O ⇒ 〈cc1,change_vec ? (S n) 
66            (change_vec ? (S n) (null_action ? n) (〈None ?,R〉) src)
67            (〈nth src ? a (None ?),R〉) dst〉
68  | S _ ⇒ 〈cc1,null_action ? n〉 ].
69
70 definition copy_char ≝ 
71   λsrc,dst,sig,n.
72   mk_mTM sig n copy_char_states (trans_copy_char src dst sig n) 
73     cc0 (λq.q == cc1).
74
75 definition R_copy_char ≝ 
76   λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n).
77   outt = change_vec ?? 
78          (change_vec ?? int
79           (tape_move_mono ? (nth src ? int (niltape ?)) 〈None ?, R〉) src)
80           (tape_move_mono ? (nth dst ? int (niltape ?)) 
81            〈current ? (nth src ? int (niltape ?)), R〉) dst.
82
83 lemma copy_char_q0_q1 :
84   ∀src,dst,sig,n,v.src ≠ dst → src < S n → dst < S n → 
85   step sig n (copy_char src dst sig n) (mk_mconfig ??? cc0 v) =
86     mk_mconfig ??? cc1 
87      (change_vec ? (S n) 
88        (change_vec ?? v
89          (tape_move_mono ? (nth src ? v (niltape ?)) 〈None ?, R〉) src)
90             (tape_move_mono ? (nth dst ? v (niltape ?)) 〈current ? (nth src ? v (niltape ?)), R〉) dst).
91 #src #dst #sig #n #v #Heq #Hsrc #Hdst
92 whd in ⊢ (??%?);
93 <(change_vec_same … v dst (niltape ?)) in ⊢ (??%?);
94 <(change_vec_same … v src (niltape ?)) in ⊢ (??%?);
95 >tape_move_multi_def @eq_f2 //
96 >pmap_change >pmap_change <tape_move_multi_def
97 >tape_move_null_action @eq_f2 // @eq_f2
98 [ >change_vec_same %
99 | >change_vec_same >change_vec_same // ]
100 qed.
101
102 lemma sem_copy_char:
103   ∀src,dst,sig,n.src ≠ dst → src < S n → dst < S n → 
104   copy_char src dst sig n ⊨ R_copy_char src dst sig n.
105 #src #dst #sig #n #Hneq #Hsrc #Hdst #int
106 %{2} % [| % [ % | whd >copy_char_q0_q1 // ]]
107 qed.
108
109 definition obj ≝ (0:DeqNat).
110 definition cfg ≝ (1:DeqNat).
111 definition prg ≝ (2:DeqNat).
112
113 definition obj_to_cfg ≝
114   mmove cfg FSUnialpha 2 L ·
115   (ifTM ?? (inject_TM ? (test_null ?) 2 obj)
116     (copy_char obj cfg FSUnialpha 2 ·
117      mmove cfg FSUnialpha 2 L ·
118      mmove obj FSUnialpha 2 L) 
119     (inject_TM ? (write FSUnialpha null) 2 cfg)
120      tc_true) ·
121   inject_TM ? (move_to_end FSUnialpha L) 2 cfg ·
122   mmove cfg FSUnialpha 2 R.
123   
124 definition R_obj_to_cfg ≝ λt1,t2:Vector (tape FSUnialpha) 3.
125   ∀c,ls.
126   nth cfg ? t1 (niltape ?) = mk_tape FSUnialpha (c::ls) (None ?) [ ] → 
127   (∀lso,x,rso.nth obj ? t1 (niltape ?) = midtape FSUnialpha lso x rso → 
128    t2 = change_vec ?? t1 
129          (mk_tape ? [ ] (option_hd ? (reverse ? (x::ls))) (tail ? (reverse ? (x::ls)))) cfg) ∧
130   (current ? (nth obj ? t1 (niltape ?)) = None ? → 
131    t2 = change_vec ?? t1
132          (mk_tape ? [ ] (option_hd FSUnialpha (reverse ? (null::ls))) 
133            (tail ? (reverse ? (null::ls)))) cfg).
134            
135 axiom accRealize_to_Realize :
136   ∀sig,n.∀M:mTM sig n.∀Rtrue,Rfalse,acc.
137   M ⊨ [ acc: Rtrue, Rfalse ] →  M ⊨ Rtrue ∪ Rfalse.
138   
139 lemma eq_mk_tape_rightof :
140  ∀alpha,a,al.mk_tape alpha (a::al) (None ?) [ ] = rightof ? a al.
141 #alpha #a #al %
142 qed.
143
144 definition option_cons ≝ λsig.λc:option sig.λl.
145   match c with [ None ⇒ l | Some c0 ⇒ c0::l ].
146
147 lemma tape_move_mk_tape_R :
148   ∀sig,ls,c,rs.
149   (c = None ? → ls = [ ] ∨ rs = [ ]) → 
150   tape_move ? (mk_tape sig ls c rs) R =
151   mk_tape ? (option_cons ? c ls) (option_hd ? rs) (tail ? rs).
152 #sig * [ * [ * | #c * ] | #l0 #ls0 * [ *
153 [| #r0 #rs0 #H @False_ind cases (H (refl ??)) #H1 destruct (H1) ] | #c * ] ] 
154 normalize //
155 qed.
156
157 lemma eq_vec_change_vec : ∀sig,n.∀v1,v2:Vector sig n.∀i,t,d.
158   nth i ? v2 d = t → 
159   (∀j.i ≠ j → nth j ? v1 d = nth j ? v2 d) → 
160   v2 = change_vec ?? v1 t i.
161 #sig #n #v1 #v2 #i #t #d #H1 #H2 @(eq_vec … d)
162 #i0 #Hlt cases (decidable_eq_nat i0 i) #Hii0
163 [ >Hii0 >nth_change_vec //
164 | >nth_change_vec_neq [|@sym_not_eq //] @sym_eq @H2 @sym_not_eq // ]
165 qed.
166
167 lemma sem_obj_to_cfg : obj_to_cfg ⊨  R_obj_to_cfg.
168 @(sem_seq_app FSUnialpha 2 ????? (sem_move_multi ? 2 cfg L ?)
169    (sem_seq ??????
170     (sem_if ??????????
171      (sem_test_null_multi ?? obj ?)
172       (sem_seq ?????? (sem_copy_char …)
173        (sem_seq ?????? (sem_move_multi ? 2 cfg L ?)
174         (sem_move_multi ? 2 obj L ?)))
175       (sem_inject ???? cfg ? (sem_write FSUnialpha null)))
176      (sem_seq ?????? (sem_inject ???? cfg ? (sem_move_to_end_l ?))
177        (sem_move_multi ? 2 cfg R ?)))) //
178 #ta #tb *
179 #tc * whd in ⊢ (%→?); #Htc *
180 #td * *
181 [ * #te * * #Hcurtc #Hte
182   * destruct (Hte) #te * whd in ⊢ (%→?); #Hte
183   cut (∃x.current ? (nth obj ? tc (niltape ?)) = Some ? x)
184   [ cases (current ? (nth obj ? tc (niltape ?))) in Hcurtc;
185     [ * #H @False_ind /2/ | #x #_ %{x} % ] ] * #x #Hcurtc'
186 (*  [ whd in ⊢ (%→%→?); * #x * #y * * -Hcurtc #Hcurtc1 #Hcurtc2 #Hte *)
187     * #tf * whd in ⊢ (%→%→?); #Htf #Htd
188     * #tg * * * whd in ⊢ (%→%→%→%→?); #Htg1 #Htg2 #Htg3 #Htb
189     #c #ls #Hta1 %
190     [ #lso #x0 #rso #Hta2 >Hta1 in Htc; >eq_mk_tape_rightof 
191       whd in match (tape_move ???); #Htc
192       cut (tg = change_vec ?? td (mk_tape ? [ ] (None ?) (reverse ? ls@[x])) cfg)
193       [ lapply (eq_vec_change_vec ??????? (Htg2 ls x [ ] ?) Htg3) //
194         >Htd >nth_change_vec_neq // >Htf >nth_change_vec //
195         >Hte >Hcurtc' >nth_change_vec // >Htc >nth_change_vec // ] 
196       -Htg1 -Htg2 -Htg3 #Htg destruct 
197       >change_vec_change_vec >change_vec_change_vec
198       >change_vec_commute // >change_vec_change_vec
199       >change_vec_commute [|@sym_not_eq //] >change_vec_change_vec
200       >change_vec_commute // >change_vec_change_vec
201       >nth_change_vec // >nth_change_vec_neq [|@sym_not_eq //] 
202       >nth_change_vec // >nth_change_vec_neq [|@sym_not_eq //]
203       >change_vec_commute [|@sym_not_eq //] @eq_f3 //
204       [ >Hta2 cases rso in Hta2; whd in match (tape_move_mono ???);
205         [ #Hta2 whd in match (tape_move ???); <Hta2 @change_vec_same
206         | #r1 #rs1 #Hta2 whd in match (tape_move ???); <Hta2 @change_vec_same ]
207       | >tape_move_mk_tape_R [| #_ % %] >reverse_cons
208         >nth_change_vec_neq in Hcurtc'; [|@sym_not_eq //] >Hta2
209         normalize in ⊢ (%→?); #H destruct (H) %
210       ]
211     | #Hta2 >Htc in Hcurtc'; >nth_change_vec_neq [| @sym_not_eq //]
212       >Hta2 #H destruct (H)
213     ]
214 | * #te * * #Hcurtc #Hte
215   * whd in ⊢ (%→%→?); #Htd1 #Htd2
216   * #tf * * * #Htf1 #Htf2 #Htf3 whd in ⊢ (%→?); #Htb
217   #c #ls #Hta1 %
218   [ #lso #x #rso #Hta2 >Htc in Hcurtc; >nth_change_vec_neq [|@sym_not_eq //] 
219     >Hta2 normalize in ⊢ (%→?); #H destruct (H)
220   | #_ >Hta1 in Htc; >eq_mk_tape_rightof whd in match (tape_move ???); #Htc
221     destruct (Hte) cut (td = change_vec ?? tc (midtape ? ls null []) cfg)
222     [ lapply (eq_vec_change_vec ??????? (Htd1 ls c [ ] ?) Htd2) // 
223       >Htc >nth_change_vec // ] -Htd1 -Htd2 #Htd
224     -Htf1 cut (tf = change_vec ?? td (mk_tape ? [ ] (None ?) (reverse ? ls@[null])) cfg)
225     [ lapply (eq_vec_change_vec ??????? (Htf2 ls null [ ] ?) Htf3) //
226       >Htd >nth_change_vec // ] -Htf2 -Htf3 #Htf destruct (Htf Htd Htc Htb)
227     >change_vec_change_vec >change_vec_change_vec >change_vec_change_vec
228     >change_vec_change_vec >change_vec_change_vec >nth_change_vec //
229     >reverse_cons >tape_move_mk_tape_R /2/ ]
230 ]
231 qed.
232
233 definition test_null_char ≝ test_char FSUnialpha (λc.c == null).
234
235 definition R_test_null_char_true ≝ λt1,t2.
236   current FSUnialpha t1 = Some ? null ∧ t1 = t2.
237   
238 definition R_test_null_char_false ≝ λt1,t2.
239   current FSUnialpha t1 ≠ Some ? null ∧ t1 = t2.
240   
241 lemma sem_test_null_char :
242   test_null_char ⊨ [ tc_true : R_test_null_char_true, R_test_null_char_false].
243 #t1 cases (sem_test_char FSUnialpha (λc.c == null) t1) #k * #outc * * #Hloop #Htrue
244 #Hfalse %{k} %{outc} % [ %
245 [ @Hloop
246 | #Houtc cases (Htrue ?) [| @Houtc] * #c * #Hcurt1 #Hcnull lapply (\P Hcnull)
247   -Hcnull #H destruct (H) #Houtc1 %
248   [ @Hcurt1 | <Houtc1 % ] ]
249 | #Houtc cases (Hfalse ?) [| @Houtc] #Hc #Houtc %
250   [ % #Hcurt1 >Hcurt1 in Hc; #Hc lapply (Hc ? (refl ??)) 
251     >(?:((null:FSUnialpha) == null) = true) [|@(\b (refl ??)) ]
252     #H destruct (H)
253   | <Houtc % ] ]
254 qed.
255
256 definition cfg_to_obj ≝
257   mmove cfg FSUnialpha 2 L ·
258   (ifTM ?? (inject_TM ? test_null_char 2 cfg)
259     (nop ? 2)
260     (copy_char cfg obj FSUnialpha 2 ·
261      mmove cfg FSUnialpha 2 L ·
262      mmove obj FSUnialpha 2 L) 
263      tc_true) ·
264   inject_TM ? (move_to_end FSUnialpha L) 2 cfg ·
265   mmove cfg FSUnialpha 2 R.
266   
267 definition R_cfg_to_obj ≝ λt1,t2:Vector (tape FSUnialpha) 3.
268   ∀c,ls.
269   nth cfg ? t1 (niltape ?) = mk_tape FSUnialpha (c::ls) (None ?) [ ] → 
270   (c = null → 
271    t2 = change_vec ?? t1
272          (mk_tape ? [ ] (option_hd FSUnialpha (reverse ? (c::ls))) 
273            (tail ? (reverse ? (c::ls)))) cfg) ∧
274   (c ≠ null → 
275    t2 = change_vec ??
276           (change_vec ?? t1
277              (midtape ? (left ? (nth obj ? t1 (niltape ?))) c (right ? (nth obj ? t1 (niltape ?)))) obj)
278           (mk_tape ? [ ] (option_hd ? (reverse ? (c::ls))) (tail ? (reverse ? (c::ls)))) cfg).
279           
280 lemma tape_move_mk_tape_L :
281   ∀sig,ls,c,rs.
282   (c = None ? → ls = [ ] ∨ rs = [ ]) → 
283   tape_move ? (mk_tape sig ls c rs) L =
284   mk_tape ? (tail ? ls) (option_hd ? ls) (option_cons ? c rs).
285 #sig * [ * [ * | #c * ] | #l0 #ls0 * [ *
286 [| #r0 #rs0 #H @False_ind cases (H (refl ??)) #H1 destruct (H1) ] | #c * ] ] 
287 normalize //
288 qed.
289
290 lemma sem_cfg_to_obj : cfg_to_obj ⊨  R_cfg_to_obj.
291 @(sem_seq_app FSUnialpha 2 ????? (sem_move_multi ? 2 cfg L ?)
292   (sem_seq ??????
293    (sem_if ??????????
294     (acc_sem_inject ?????? cfg ? sem_test_null_char)
295     (sem_nop …)
296     (sem_seq ?????? (sem_copy_char …)
297      (sem_seq ?????? (sem_move_multi ? 2 cfg L ?) (sem_move_multi ? 2 obj L ?))))
298    (sem_seq ?????? (sem_inject ???? cfg ? (sem_move_to_end_l ?))
299     (sem_move_multi ? 2 cfg R ?)))) // [@sym_not_eq //]
300 #ta #tb *
301 #tc * whd in ⊢ (%→?); #Htc *
302 #td * *
303 [ * #te * * * #Hcurtc #Hte1 #Hte2 whd in ⊢ (%→?); #Htd destruct (Htd)
304   * #tf * * * #Htf1 #Htf2 #Htf3
305   whd in ⊢ (%→?); #Htb
306   #c #ls #Hta %
307   [ #Hc >Hta in Htc; >eq_mk_tape_rightof whd in match (tape_move ???); #Htc
308     cut (te = tc)
309     [ lapply (eq_vec_change_vec ??????? (sym_eq … Hte1) Hte2) >change_vec_same // ]
310     -Hte1 -Hte2 #Hte
311     cut (tf = change_vec ? 3 te (mk_tape ? [ ] (None ?) (reverse ? ls@[c])) cfg)
312     [ lapply (eq_vec_change_vec ??????? (Htf2 ls c [ ] ?) Htf3) //
313       >Hte >Htc >nth_change_vec // ] -Htf1 -Htf2 -Htf3 #Htf
314     destruct (Htf Hte Htc Htb)
315     >change_vec_change_vec >change_vec_change_vec >change_vec_change_vec
316     >nth_change_vec // >tape_move_mk_tape_R [| #_ % % ] 
317     >reverse_cons %
318   | #Hc >Hta in Htc; >eq_mk_tape_rightof whd in match (tape_move ???); #Htc
319     >Htc in Hcurtc; >nth_change_vec // normalize in ⊢ (%→?); 
320     #H destruct (H) @False_ind cases Hc /2/ ]
321   * #tf * *
322 | * #te * * * #Hcurtc #Hte1 #Hte2
323   * #tf * whd in ⊢ (%→?); #Htf
324   * #tg * whd in ⊢ (%→%→?); #Htg #Htd
325   * #th * * * #Hth1 #Hth2 #Hth3
326   whd in ⊢ (%→?); #Htb 
327   #c #ls #Hta % #Hc
328   [ >Htc in Hcurtc; >Hta >nth_change_vec // >tape_move_mk_tape_L //
329     >Hc normalize in ⊢ (%→?); * #H @False_ind /2/
330   | cut (te = tc)
331     [ lapply (eq_vec_change_vec ??????? (sym_eq … Hte1) Hte2)
332       >change_vec_same // ] -Hte1 -Hte2 #Hte
333     cut (th = change_vec ?? td (mk_tape ? [ ] (None ?) (reverse ? ls@[c])) cfg)
334     [ lapply (eq_vec_change_vec ??????? (Hth2 ls c [ ] ?) Hth3) //
335       >Htd >nth_change_vec_neq // >Htg >nth_change_vec //
336       >Htf >nth_change_vec_neq // >nth_change_vec // 
337       >Hte >Htc >nth_change_vec // >Hta // ] -Hth1 -Hth2 -Hth3 #Hth
338     destruct (Hth Hte Hta Htb Htd Htg Htc Htf) 
339     >change_vec_change_vec >change_vec_change_vec
340     >change_vec_commute // >change_vec_change_vec
341     >change_vec_commute [|@sym_not_eq //] >change_vec_change_vec
342     >change_vec_commute // >change_vec_change_vec    
343     >nth_change_vec // >nth_change_vec_neq [|@sym_not_eq //] 
344     >nth_change_vec // >nth_change_vec_neq [|@sym_not_eq //]
345     >change_vec_commute [|@sym_not_eq //]
346     @eq_f3 //
347     [ >Hta >tape_move_mk_tape_L // >nth_change_vec // whd in match (current ??);
348       @eq_f2 // cases (nth obj ? ta (niltape ?))
349       [| #r0 #rs0 | #l0 #ls0 | #ls0 #c0 #rs0 ] try %
350       cases rs0 //
351     | >reverse_cons >tape_move_mk_tape_R // #_ % % ]
352   ]
353 ]
354 qed.
355
356 (* macchina che muove il nastro obj a destra o sinistra a seconda del valore
357    del current di prg, che codifica la direzione in cui ci muoviamo *)
358
359 definition char_to_move ≝ λc.match c with
360   [ bit b ⇒ if b then R else L
361   | _ ⇒ N].
362
363 definition char_to_bit_option ≝ λc.match c with
364   [ bit b ⇒ Some ? (bit b)
365   | _ ⇒ None ?]. 
366  
367 definition tape_move_obj : mTM FSUnialpha 2 ≝ 
368   ifTM ?? 
369    (inject_TM ? (test_char ? (λc:FSUnialpha.c == bit false)) 2 prg)
370    (mmove obj FSUnialpha 2 L)
371    (ifTM ?? 
372     (inject_TM ? (test_char ? (λc:FSUnialpha.c == bit true)) 2 prg)
373     (mmove obj FSUnialpha 2 R)
374     (nop ??)
375     tc_true)
376    tc_true.
377
378 definition R_tape_move_obj' ≝ λt1,t2:Vector (tape FSUnialpha) 3.
379   (current ? (nth prg ? t1 (niltape ?)) = Some ? (bit false) → 
380    t2 = change_vec ?? t1 (tape_move ? (nth obj ? t1 (niltape ?)) L) obj) ∧
381   (current ? (nth prg ? t1 (niltape ?)) = Some ? (bit true) → 
382    t2 = change_vec ?? t1 (tape_move ? (nth obj ? t1 (niltape ?)) R) obj) ∧
383   (current ? (nth prg ? t1 (niltape ?)) ≠ Some ? (bit false) →
384    current ? (nth prg ? t1 (niltape ?)) ≠ Some ? (bit true) →  
385    t2 = t1).
386    
387 lemma sem_tape_move_obj' : tape_move_obj ⊨ R_tape_move_obj'.
388 #ta cases (sem_if ??????????
389   (acc_sem_inject ?????? prg ? (sem_test_char ? (λc:FSUnialpha.c == bit false)))
390   (sem_move_multi ? 2 obj L ?)
391   (sem_if ??????????
392    (acc_sem_inject ?????? prg ? (sem_test_char ? (λc:FSUnialpha.c == bit true)))
393    (sem_move_multi ? 2 obj R ?)
394    (sem_nop …)) ta) //
395 #i * #outc * #Hloop #HR %{i} %{outc} % [@Hloop] -i
396 cases HR -HR
397 [ * #tb * * * * #c * #Hcurta_prg #Hc lapply (\P Hc) -Hc #Hc #Htb1 #Htb2
398   whd in ⊢ (%→%); #Houtc >Houtc -Houtc % [ %
399   [ >Hcurta_prg #H destruct (H) >(?:tb = ta) 
400     [| lapply (eq_vec_change_vec ??????? Htb1 Htb2)
401        >change_vec_same // ] %
402   | >Hcurta_prg #H destruct (H) destruct (Hc) ]
403   | >Hcurta_prg >Hc * #H @False_ind /2/ ]
404 | * #tb * * * #Hnotfalse #Htb1 #Htb2 cut (tb = ta) 
405   [ lapply (eq_vec_change_vec ??????? Htb1 Htb2)
406      >change_vec_same // ] -Htb1 -Htb2 #Htb destruct (Htb) *
407   [ * #tc * * * * #c * #Hcurta_prg #Hc lapply (\P Hc) -Hc #Hc #Htc1 #Htc2
408     whd in ⊢ (%→%); #Houtc >Houtc -Houtc % [ %
409     [ >Hcurta_prg #H destruct (H) destruct (Hc)
410     | >Hcurta_prg #H destruct (H) >(?:tc = ta) 
411       [| lapply (eq_vec_change_vec ??????? Htc1 Htc2)
412         >change_vec_same // ] % ]
413     | >Hcurta_prg >Hc #_ * #H @False_ind /2/ ]
414   | * #tc * * * #Hnottrue #Htc1 #Htc2 cut (tc = ta) 
415     [ lapply (eq_vec_change_vec ??????? Htc1 Htc2)
416       >change_vec_same // ] -Htc1 -Htc2 
417     #Htc destruct (Htc) whd in ⊢ (%→?); #Houtc % [ %
418     [ #Hcurta_prg lapply (\Pf (Hnotfalse ? Hcurta_prg)) * #H @False_ind /2/
419     | #Hcurta_prg lapply (\Pf (Hnottrue ? Hcurta_prg)) * #H @False_ind /2/ ]
420     | #_ #_ @Houtc ]
421   ]
422 ]
423 qed.
424
425 definition R_tape_move_obj ≝ λt1,t2:Vector (tape FSUnialpha) 3.
426   ∀c. current ? (nth prg ? t1 (niltape ?)) = Some ? c → 
427   t2 = change_vec ?? t1 (tape_move ? (nth obj ? t1 (niltape ?)) (char_to_move c)) obj.
428
429 lemma sem_tape_move_obj : tape_move_obj ⊨ R_tape_move_obj.
430 @(Realize_to_Realize … sem_tape_move_obj')
431 #ta #tb * * #Htb1 #Htb2 #Htb3 * [ *
432 [ @Htb2 | @Htb1 ] 
433 | #Hcurta_prg change with (nth obj ? ta (niltape ?)) in match (tape_move ???);
434   >change_vec_same @Htb3 >Hcurta_prg % #H destruct (H)
435 | #Hcurta_prg change with (nth obj ? ta (niltape ?)) in match (tape_move ???);
436   >change_vec_same @Htb3 >Hcurta_prg % #H destruct (H)
437 ]
438 qed.
439
440 definition restart_tape ≝ λi. 
441   inject_TM ? (move_to_end FSUnialpha L) 2 i ·
442   mmove i FSUnialpha 2 R. 
443
444 definition unistep ≝ 
445   match_m cfg prg FSUnialpha 2 · 
446   restart_tape cfg · copy prg cfg FSUnialpha 2 ·
447   cfg_to_obj · tape_move_obj · restart_tape prg · obj_to_cfg.
448
449 (*
450 definition legal_tape ≝ λn,l,h,t.
451   ∃state,char,table.
452   nth cfg ? t1 (niltape ?) = midtape ? [ ] bar (state@[char]) →
453   is_config n (bar::state@[char]) →  
454   nth prg ? t1 (niltape ?) = midtape ? [ ] bar table →
455   bar::table = table_TM n l h → *)
456
457 definition list_of_tape ≝ λsig,t. 
458   left sig t@option_cons ? (current ? t) (right ? t).
459
460 definition low_char' ≝ λc.
461   match c with
462   [ None ⇒ null 
463   | Some b ⇒ if (is_bit b) then b else null
464   ].
465   
466 lemma low_char_option : ∀s.
467   low_char' (option_map FinBool FSUnialpha bit s) = low_char s.
468 * //
469 qed.
470
471 definition R_unistep ≝ λn,l,h.λt1,t2: Vector ? 3.
472   ∀state,char,table.
473   (* cfg *)
474   nth cfg ? t1 (niltape ?) = midtape ? [ ] bar (state@[char]) →
475   is_config n (bar::state@[char]) →  
476   (* prg *)
477   nth prg ? t1 (niltape ?) = midtape ? [ ] bar table →
478   bar::table = table_TM n l h →
479   (* obj *)
480   only_bits (list_of_tape ? (nth obj ? t1 (niltape ?))) →
481   let conf ≝ (bar::state@[char]) in
482   (∃ll,lr.bar::table = ll@conf@lr) →
483 (*
484     ∃nstate,nchar,m,t. tuple_encoding n h t = (conf@nstate@[nchar;m]) ∧ 
485     mem ? t l ∧  *)
486     ∀nstate,nchar,m,t. 
487     tuple_encoding n h t = (conf@nstate@[nchar;m])→ 
488     mem ? t l →
489     let new_obj ≝ 
490      tape_move_mono ? (nth obj ? t1 (niltape ?)) 
491        〈char_to_bit_option nchar,char_to_move m〉 in
492     let next_char ≝ low_char' (current ? new_obj) in
493     t2 = 
494       change_vec ??
495         (change_vec ?? t1 (midtape ? [ ] bar (nstate@[next_char])) cfg)
496         new_obj obj.
497
498 definition tape_map ≝ λA,B:FinSet.λf:A→B.λt.
499   mk_tape B (map ?? f (left ? t)) 
500     (option_map ?? f (current ? t)) 
501     (map ?? f (right ? t)).
502     
503 lemma map_list_of_tape: ∀A,B,f,t.
504   list_of_tape B (tape_map ?? f t) = map ?? f (list_of_tape A t).
505 #A #B #f * // normalize // #ls #c #rs <map_append %
506 qed.
507
508 lemma low_char_current : ∀t.
509   low_char' (current FSUnialpha (tape_map FinBool FSUnialpha bit t))
510   = low_char (current FinBool t).
511 * // qed.
512
513 definition low_tapes: ∀M:normalTM.∀c:nconfig (no_states M).Vector ? 3 ≝ 
514 λM:normalTM.λc:nconfig (no_states M).Vector_of_list ?
515   [tape_map ?? bit (ctape ?? c);
516    midtape ? [ ] bar 
517     ((bits_of_state ? (nhalt M) (cstate ?? c))@[low_char (current ? (ctape ?? c))]);
518    midtape ? [ ] bar (tail ? (table_TM ? (graph_enum ?? (ntrans M)) (nhalt M)))
519   ].
520
521 lemma obj_low_tapes: ∀M,c.
522   nth obj ? (low_tapes M c) (niltape ?) = tape_map ?? bit (ctape ?? c).
523 // qed.
524
525 lemma cfg_low_tapes: ∀M,c.
526   nth cfg ? (low_tapes M c) (niltape ?) = 
527   midtape ? [ ] bar 
528     ((bits_of_state ? (nhalt M) (cstate ?? c))@[low_char (current ? (ctape ?? c))]).
529 // qed.
530
531 lemma prg_low_tapes: ∀M,c.
532   nth prg ? (low_tapes M c) (niltape ?) = 
533   midtape ? [ ] bar (tail ? (table_TM ? (graph_enum ?? (ntrans M)) (nhalt M))).
534 // qed.
535
536 (* commutation lemma for write *)
537 lemma map_write: ∀t,cout.
538  tape_write ? (tape_map FinBool ? bit t) (char_to_bit_option (low_char cout))
539   = tape_map ?? bit (tape_write ? t cout).
540 #t * // #b whd in match (char_to_bit_option ?);
541 whd in ⊢ (??%%); @eq_f3 [elim t // | // | elim t //]
542 qed.
543
544 (* commutation lemma for moves *)
545 lemma map_move: ∀t,m.
546  tape_move ? (tape_map FinBool ? bit t) (char_to_move (low_mv m))
547   = tape_map ?? bit (tape_move ? t m).
548 #t * // whd in match (char_to_move ?);
549   [cases t // * // | cases t // #ls #a * //]
550 qed.
551   
552 (* commutation lemma for actions *)
553 lemma map_action: ∀t,cout,m.
554  tape_move ? (tape_write ? (tape_map FinBool ? bit t)
555     (char_to_bit_option (low_char cout))) (char_to_move (low_mv m)) 
556  = tape_map ?? bit (tape_move ? (tape_write ? t cout) m).
557 #t #cout #m >map_write >map_move % 
558 qed. 
559
560 lemma map_move_mono: ∀t,cout,m.
561  tape_move_mono ? (tape_map FinBool ? bit t)
562   〈char_to_bit_option (low_char cout), char_to_move (low_mv m)〉
563  = tape_map ?? bit (tape_move_mono ? t 〈cout,m〉).
564 @map_action
565 qed. 
566
567 definition R_unistep_high ≝ λM:normalTM.λt1,t2.
568 ∀c:nconfig (no_states M).
569   t1 = low_tapes M c → 
570   t2 = low_tapes M (step ? M c). 
571
572 lemma R_unistep_equiv : ∀M,t1,t2. 
573   R_unistep (no_states M) (graph_enum ?? (ntrans M)) (nhalt M) t1 t2 →
574   R_unistep_high M t1 t2.
575 #M #t1 #t2 #H whd whd in match (nconfig ?); #c #Ht1
576 lapply (initial_bar ? (nhalt M) (graph_enum ?? (ntrans M)) (nTM_nog ?)) #Htable
577 (* tup = current tuple *)
578 cut (∃t.t = 〈〈cstate … c,current ? (ctape … c)〉, 
579              ntrans M 〈cstate … c,current ? (ctape … c)〉〉) [% //] * #tup #Htup
580 (* tup is in the graph *)
581 cut (mem ? tup (graph_enum ?? (ntrans M)))
582   [@memb_to_mem >Htup @(graph_enum_complete … (ntrans M)) %] #Hingraph
583 (* tupe target = 〈qout,cout,m〉 *)
584 lapply (decomp_target ? (ntrans M 〈cstate … c,current ? (ctape … c)〉))
585 * #qout * #cout * #m #Htg >Htg in Htup; #Htup
586 (* new config *)
587 cut (step FinBool M c = mk_config ?? qout (tape_move ? (tape_write ? (ctape … c) cout) m))
588   [>(config_expand … c) whd in ⊢ (??%?); (* >Htg ?? why not?? *)
589    cut (trans ? M 〈cstate  … c, current ? (ctape … c)〉 = 〈qout,cout,m〉) [<Htg %] #Heq1 
590    >Heq1 %] #Hstep
591 (* new state *)
592 cut (cstate ?? (step FinBool M c) = qout) [>Hstep %] #Hnew_state
593 (* new tape *)
594 cut (ctape ?? (step FinBool M c) = tape_move ? (tape_write ? (ctape … c) cout) m)
595   [>Hstep %] #Hnew_tape
596 lapply(H (bits_of_state ? (nhalt M) (cstate ?? c)) 
597          (low_char (current ? (ctape ?? c)))
598          (tail ? (table_TM ? (graph_enum ?? (ntrans M)) (nhalt M)))
599          ??????)
600 [<Htable
601  lapply(list_to_table … (nhalt M) …Hingraph) * #ll * #lr #Htable1 %{ll} 
602  %{(((bits_of_state ? (nhalt M) qout)@[low_char cout;low_mv m])@lr)} 
603  >Htable1 @eq_f <associative_append @eq_f2 // >Htup
604  whd in ⊢ (??%?); @eq_f >associative_append %
605 |>Ht1 >obj_low_tapes >map_list_of_tape elim (list_of_tape ??) 
606   [#b @False_ind | #b #tl #Hind #a * [#Ha >Ha //| @Hind]]
607 |@sym_eq @Htable
608 |>Ht1 %
609 |%{(bits_of_state ? (nhalt M) (cstate ?? c))} %{(low_char (current ? (ctape ?? c)))}
610  % [% [% [// | cases (current ??) normalize [|#b] % #Hd destruct (Hd)]
611       |>length_map whd in match (length ??); @eq_f //]
612    |//]
613 |>Ht1 >cfg_low_tapes //] -H #H 
614 lapply(H (bits_of_state … (nhalt M) qout) (low_char … cout) 
615          (low_mv … m) tup ? Hingraph)
616   [>Htup whd in ⊢ (??%?); @eq_f >associative_append %] -H
617 #Ht2 @(eq_vec ? 3 ?? (niltape ?) ?) >Ht2 #i #Hi 
618 cases (le_to_or_lt_eq … (le_S_S_to_le … Hi)) -Hi #Hi
619   [cases (le_to_or_lt_eq … (le_S_S_to_le … Hi)) -Hi #Hi
620     [cases (le_to_or_lt_eq … (le_S_S_to_le … Hi)) -Hi #Hi
621       [@False_ind /2/
622       |>Hi >obj_low_tapes >nth_change_vec //
623        >Ht1 >obj_low_tapes >Hstep @map_action 
624       ]
625     |>Hi >cfg_low_tapes >nth_change_vec_neq 
626       [|% whd in ⊢ (??%?→?);  #H destruct (H)]
627      >nth_change_vec // >Hnew_state @eq_f @eq_f >Hnew_tape 
628      @eq_f2 [|2:%] >Ht1 >obj_low_tapes >map_move_mono >low_char_current %
629     ]
630   |(* program tapes do not change *)
631    >Hi >prg_low_tapes 
632    >nth_change_vec_neq [|% whd in ⊢ (??%?→?);  #H destruct (H)]
633    >nth_change_vec_neq [|% whd in ⊢ (??%?→?);  #H destruct (H)]
634    >Ht1 >prg_low_tapes //
635   ]
636 qed.