]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/multi_universal/unistep_aux.ma
progress
[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
17 (*
18
19   in.obj : ...  x ...
20                 ^
21   in.cfg : ...  ? ? ...
22                     ^
23                 
24   out.cfg : ... 1 x ...
25                   ^
26                   
27   ---------------------
28   current (in.obj) = None
29   
30   in.cfg : ...  ? ? ...
31                     ^
32
33   out.cfg : ... 0 0 ...
34                   ^
35                   
36   obj_to_cfg ≝ 
37     move_l(cfg);
38     move_l(cfg);
39     (if (current(in.obj)) == None
40        then write(0,cfg);
41             move_r(cfg);
42             write(0,cfg);
43        else write(1,cfg);
44             move_r(cfg);
45             copy_step(obj,cfg);
46             move_l(obj);)
47     move_to_end_l(cfg);
48     move_r(cfg);
49        
50   
51   cfg_to_obj
52 *)
53
54 definition obj ≝ (0:DeqNat).
55 definition cfg ≝ (1:DeqNat).
56 definition prg ≝ (2:DeqNat).
57
58 definition obj_to_cfg ≝
59   mmove cfg FSUnialpha 2 L ·
60   (ifTM ?? (inject_TM ? (test_null ?) 2 obj)
61     (copy_step obj cfg FSUnialpha 2 ·
62      mmove cfg FSUnialpha 2 L ·
63      mmove obj FSUnialpha 2 L) 
64     (inject_TM ? (write FSUnialpha null) 2 cfg)
65      tc_true) ·
66   inject_TM ? (move_to_end FSUnialpha L) 2 cfg ·
67   mmove cfg FSUnialpha 2 R.
68   
69 definition R_obj_to_cfg ≝ λt1,t2:Vector (tape FSUnialpha) 3.
70   ∀c,ls.
71   nth cfg ? t1 (niltape ?) = mk_tape FSUnialpha (c::ls) (None ?) [ ] → 
72   (∀lso,x,rso.nth obj ? t1 (niltape ?) = midtape FSUnialpha lso x rso → 
73    t2 = change_vec ?? t1 
74          (mk_tape ? [ ] (option_hd ? (reverse ? (x::ls))) (tail ? (reverse ? (x::ls)))) cfg) ∧
75   (current ? (nth obj ? t1 (niltape ?)) = None ? → 
76    t2 = change_vec ?? t1
77          (mk_tape ? [ ] (option_hd FSUnialpha (reverse ? (null::ls))) 
78            (tail ? (reverse ? (null::ls)))) cfg).
79            
80 axiom sem_move_to_end_l : ∀sig. move_to_end sig L ⊨ R_move_to_end_l sig.
81 axiom accRealize_to_Realize :
82   ∀sig,n.∀M:mTM sig n.∀Rtrue,Rfalse,acc.
83   M ⊨ [ acc: Rtrue, Rfalse ] →  M ⊨ Rtrue ∪ Rfalse.
84   
85 lemma eq_mk_tape_rightof :
86  ∀alpha,a,al.mk_tape alpha (a::al) (None ?) [ ] = rightof ? a al.
87 #alpha #a #al %
88 qed.
89
90 axiom daemon : ∀P:Prop.P.
91
92 definition option_cons ≝ λsig.λc:option sig.λl.
93   match c with [ None ⇒ l | Some c0 ⇒ c0::l ].
94
95 lemma tape_move_mk_tape_R :
96   ∀sig,ls,c,rs.
97   (c = None ? → ls = [ ] ∨ rs = [ ]) → 
98   tape_move ? (mk_tape sig ls c rs) R =
99   mk_tape ? (option_cons ? c ls) (option_hd ? rs) (tail ? rs).
100 #sig * [ * [ * | #c * ] | #l0 #ls0 * [ *
101 [| #r0 #rs0 #H @False_ind cases (H (refl ??)) #H1 destruct (H1) ] | #c * ] ] 
102 normalize //
103 qed.
104
105 lemma sem_obj_to_cfg : obj_to_cfg ⊨  R_obj_to_cfg.
106 @(sem_seq_app FSUnialpha 2 ????? (sem_move_multi ? 2 cfg L ?)
107    (sem_seq ??????
108     (sem_if ??????????
109      (sem_test_null_multi ?? obj ?)
110       (sem_seq ?????? (accRealize_to_Realize … (sem_copy_step …))
111        (sem_seq ?????? (sem_move_multi ? 2 cfg L ?)
112         (sem_move_multi ? 2 obj L ?)))
113       (sem_inject ???? cfg ? (sem_write FSUnialpha null)))
114      (sem_seq ?????? (sem_inject ???? cfg ? (sem_move_to_end_l ?))
115        (sem_move_multi ? 2 cfg R ?)))) //
116 #ta #tb *
117 #tc * whd in ⊢ (%→?); #Htc *
118 #td * *
119 [ * #te * * #Hcurtc #Hte
120   * destruct (Hte) #te * *
121   [ whd in ⊢ (%→%→?); * #x * #y * * -Hcurtc #Hcurtc1 #Hcurtc2 #Hte
122     * #tf * whd in ⊢ (%→%→?); #Htf #Htd
123     * #tg * * * whd in ⊢ (%→%→%→%→?); #Htg1 #Htg2 #Htg3 #Htb
124     #c #ls #Hta1 %
125     [ #lso #x0 #rso #Hta2 >Hta1 in Htc; >eq_mk_tape_rightof 
126       whd in match (tape_move ???); #Htc
127       cut (tg = change_vec ?? td (mk_tape ? [ ] (None ?) (reverse ? ls@[x])) cfg)
128       [@daemon] -Htg1 -Htg2 -Htg3 #Htg destruct (Htg Htf Hte Htd Htc Htb)
129       >change_vec_change_vec >change_vec_change_vec
130       >change_vec_commute // >change_vec_change_vec
131       >change_vec_commute [|@sym_not_eq //] >change_vec_change_vec
132       >change_vec_commute // >change_vec_change_vec
133       >nth_change_vec // >nth_change_vec_neq [|@sym_not_eq //] 
134       >nth_change_vec // >nth_change_vec_neq [|@sym_not_eq //]
135       >change_vec_commute [|@sym_not_eq //] @eq_f3 //
136       [ >Hta2 cases rso in Hta2; whd in match (tape_move_mono ???);
137         [ #Hta2 whd in match (tape_move ???); <Hta2 @change_vec_same
138         | #r1 #rs1 #Hta2 whd in match (tape_move ???); <Hta2 @change_vec_same ]
139       | >tape_move_mk_tape_R [| #_ % %] >reverse_cons
140         >nth_change_vec_neq in Hcurtc1; [|@sym_not_eq //] >Hta2
141         normalize in ⊢ (%→?); #H destruct (H) %
142       ]
143     | #Hta2 >Htc in Hcurtc1; >nth_change_vec_neq [| @sym_not_eq //]
144       >Hta2 #H destruct (H)
145     ]
146   | * #Hcurtc0 #Hte #_ #_ #c #ls #Hta1 >Hta1 in Htc; >eq_mk_tape_rightof
147     whd in match (tape_move ???); #Htc >Htc in Hcurtc0; *
148     [ >Htc in Hcurtc; >nth_change_vec_neq [|@sym_not_eq //]
149       #Hcurtc #Hcurtc0 >Hcurtc0 in Hcurtc; * #H @False_ind @H %
150     | >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H) ]
151   ]
152 | * #te * * #Hcurtc #Hte
153   * whd in ⊢ (%→%→?); #Htd1 #Htd2
154   * #tf * * * #Htf1 #Htf2 #Htf3 whd in ⊢ (%→?); #Htb
155   #c #ls #Hta1 %
156   [ #lso #x #rso #Hta2 >Htc in Hcurtc; >nth_change_vec_neq [|@sym_not_eq //] 
157     >Hta2 normalize in ⊢ (%→?); #H destruct (H)
158   | #_ >Hta1 in Htc; >eq_mk_tape_rightof whd in match (tape_move ???); #Htc
159     destruct (Hte) cut (td = change_vec ?? tc (midtape ? ls null []) cfg)
160     [@daemon] -Htd1 -Htd2 #Htd
161     -Htf1 cut (tf = change_vec ?? td (mk_tape ? [ ] (None ?) (reverse ? ls@[null])) cfg)
162     [@daemon] -Htf2 -Htf3 #Htf destruct (Htf Htd Htc Htb)
163     >change_vec_change_vec >change_vec_change_vec >change_vec_change_vec
164     >change_vec_change_vec >change_vec_change_vec >nth_change_vec //
165     >reverse_cons >tape_move_mk_tape_R /2/ ]
166 ]
167 qed.
168
169 definition test_null_char ≝ test_char FSUnialpha (λc.c == null).
170
171 definition R_test_null_char_true ≝ λt1,t2.
172   current FSUnialpha t1 = Some ? null ∧ t1 = t2.
173   
174 definition R_test_null_char_false ≝ λt1,t2.
175   current FSUnialpha t1 ≠ Some ? null ∧ t1 = t2.
176   
177 lemma sem_test_null_char :
178   test_null_char ⊨ [ tc_true : R_test_null_char_true, R_test_null_char_false].
179 #t1 cases (sem_test_char FSUnialpha (λc.c == null) t1) #k * #outc * * #Hloop #Htrue
180 #Hfalse %{k} %{outc} % [ %
181 [ @Hloop
182 | #Houtc cases (Htrue ?) [| @Houtc] * #c * #Hcurt1 #Hcnull lapply (\P Hcnull)
183   -Hcnull #H destruct (H) #Houtc1 %
184   [ @Hcurt1 | <Houtc1 % ] ]
185 | #Houtc cases (Hfalse ?) [| @Houtc] #Hc #Houtc %
186   [ % #Hcurt1 >Hcurt1 in Hc; #Hc lapply (Hc ? (refl ??)) 
187     >(?:((null:FSUnialpha) == null) = true) [|@(\b (refl ??)) ]
188     #H destruct (H)
189   | <Houtc % ] ]
190 qed.
191
192 definition cfg_to_obj ≝
193   mmove cfg FSUnialpha 2 L ·
194   (ifTM ?? (inject_TM ? test_null_char 2 cfg)
195     (nop ? 2)
196     (copy_step cfg obj FSUnialpha 2 ·
197      mmove cfg FSUnialpha 2 L ·
198      mmove obj FSUnialpha 2 L) 
199      tc_true) ·
200   inject_TM ? (move_to_end FSUnialpha L) 2 cfg ·
201   mmove cfg FSUnialpha 2 R.
202   
203 definition R_cfg_to_obj ≝ λt1,t2:Vector (tape FSUnialpha) 3.
204   ∀c,ls.
205   nth cfg ? t1 (niltape ?) = mk_tape FSUnialpha (c::ls) (None ?) [ ] → 
206   (c = null → 
207    t2 = change_vec ?? t1
208          (mk_tape ? [ ] (option_hd FSUnialpha (reverse ? (c::ls))) 
209            (tail ? (reverse ? (c::ls)))) cfg) ∧
210   (c ≠ null → 
211    t2 = change_vec ??
212           (change_vec ?? t1
213              (midtape ? (left ? (nth obj ? t1 (niltape ?))) c (right ? (nth obj ? t1 (niltape ?)))) obj)
214           (mk_tape ? [ ] (option_hd ? (reverse ? (c::ls))) (tail ? (reverse ? (c::ls)))) cfg).
215
216 axiom sem_cfg_to_obj : cfg_to_obj ⊨  R_cfg_to_obj.
217 (*@(sem_seq_app FSUnialpha 2 ????? (sem_move_multi ? 2 cfg L ?)
218    (sem_seq ??????
219     (sem_if ??????????
220      (sem_test_null_multi ?? obj ?)
221       (sem_seq ?????? (accRealize_to_Realize … (sem_copy_step …))
222        (sem_move_multi ? 2 cfg L ?))
223       (sem_inject ???? cfg ? (sem_write FSUnialpha null)))
224      (sem_seq ?????? (sem_inject ???? cfg ? (sem_move_to_end_l ?))
225        (sem_move_multi ? 2 cfg R ?)))) //
226 #ta #tb *
227 #tc * whd in ⊢ (%→?); #Htc *
228 #td * *
229 [ * #te * * #Hcurtc #Hte
230   * destruct (Hte) #te * *
231   [ whd in ⊢ (%→%→?); * #x * #y * * -Hcurtc #Hcurtc1 #Hcurtc2 #Hte #Htd
232     * #tf * * * whd in ⊢ (%→%→%→%→?); #Htf1 #Htf2 #Htf3 #Htb
233     #c #ls #Hta1 %
234     [ #lso #x0 #rso #Hta2 >Hta1 in Htc; >eq_mk_tape_rightof 
235       whd in match (tape_move ???); #Htc
236       cut (tf = change_vec ?? tc (mk_tape ? [ ] (None ?) (reverse ? ls@[x])) cfg)
237       [@daemon] -Htf1 -Htf2 -Htf3 #Htf destruct (Htf Hte Htd Htc Htb)
238       >change_vec_change_vec >change_vec_change_vec >change_vec_change_vec
239       >nth_change_vec // >tape_move_mk_tape_R
240       @daemon
241     | #Hta2 >Htc in Hcurtc1; >nth_change_vec_neq [| @sym_not_eq //]
242       >Hta2 #H destruct (H)
243     ]
244   | * #Hcurtc0 #Hte #_ #_ #c #ls #Hta1 >Hta1 in Htc; >eq_mk_tape_rightof
245     whd in match (tape_move ???); #Htc >Htc in Hcurtc0; *
246     [ >Htc in Hcurtc; >nth_change_vec_neq [|@sym_not_eq //]
247       #Hcurtc #Hcurtc0 >Hcurtc0 in Hcurtc; * #H @False_ind @H %
248     | >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H) ]
249   ]
250 | * #te * * #Hcurtc #Hte
251   * whd in ⊢ (%→%→?); #Htd1 #Htd2
252   * #tf * * * #Htf1 #Htf2 #Htf3 whd in ⊢ (%→?); #Htb
253   #c #ls #Hta1 %
254   [ #lso #x #rso #Hta2 >Htc in Hcurtc; >nth_change_vec_neq [|@sym_not_eq //] 
255     >Hta2 normalize in ⊢ (%→?); #H destruct (H)
256   | #_ >Hta1 in Htc; >eq_mk_tape_rightof whd in match (tape_move ???); #Htc
257     destruct (Hte) cut (td = change_vec ?? tc (midtape ? ls null []) cfg)
258     [@daemon] -Htd1 -Htd2 #Htd
259     -Htf1 cut (tf = change_vec ?? td (mk_tape ? [ ] (None ?) (reverse ? ls@[null])) cfg)
260     [@daemon] -Htf2 -Htf3 #Htf destruct (Htf Htd Htc Htb)
261     >change_vec_change_vec >change_vec_change_vec >change_vec_change_vec
262     >change_vec_change_vec >change_vec_change_vec >nth_change_vec //
263     >reverse_cons >tape_move_mk_tape_R /2/ ]
264 ]
265 qed.
266 *)
267
268 (* macchina che muove il nastro obj a destra o sinistra a seconda del valore
269    del current di prg, che codifica la direzione in cui ci muoviamo *)
270    
271 definition tape_move_obj : mTM FSUnialpha 2 ≝ 
272   ifTM ?? 
273    (inject_TM ? (test_char ? (λc:FSUnialpha.c == bit false)) 2 prg)
274    (mmove obj FSUnialpha 2 L)
275    (ifTM ?? 
276     (inject_TM ? (test_char ? (λc:FSUnialpha.c == bit true)) 2 prg)
277     (mmove obj FSUnialpha 2 R)
278     (nop ??)
279     tc_true)
280    tc_true.
281
282 definition unistep ≝ 
283   obj_to_cfg · match_m cfg prg FSUnialpha 2 · 
284   inject_TM ? (move_to_end FSUnialpha L) 2 cfg ·
285   mmove cfg FSUnialpha 2 R · copy prg cfg FSUnialpha 2 ·
286   cfg_to_obj · tape_move_obj.