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