]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/multi_universal/unistep_aux.ma
7d0b63a3a9aeb259301ad45f1877fd8b6c9b06d6
[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     (inject_TM ? (write FSUnialpha null) 2 cfg)
64      tc_true) ·
65   inject_TM ? (move_to_end FSUnialpha L) 2 cfg ·
66   mmove cfg FSUnialpha 2 R.
67   
68 definition R_obj_to_cfg ≝ λt1,t2:Vector (tape FSUnialpha) 3.
69   ∀c,ls.
70   nth cfg ? t1 (niltape ?) = mk_tape FSUnialpha (c::ls) (None ?) [ ] → 
71   (∀lso,x,rso.nth obj ? t1 (niltape ?) = midtape FSUnialpha lso x rso → 
72    t2 = change_vec ?? t1 
73          (mk_tape ? [ ] (option_hd ? (reverse ? (c::ls))) (tail ? (reverse ? (c::ls)))) cfg) ∧
74   (current ? (nth obj ? t1 (niltape ?)) = None ? → 
75    t2 = change_vec ?? t1
76          (mk_tape ? [ ] (option_hd FSUnialpha (reverse ? (null::ls))) 
77            (tail ? (reverse ? (null::ls)))) cfg).
78            
79 axiom sem_move_to_end_l : ∀sig. move_to_end sig L ⊨ R_move_to_end_l sig.
80 axiom accRealize_to_Realize :
81   ∀sig,n.∀M:mTM sig n.∀Rtrue,Rfalse,acc.
82   M ⊨ [ acc: Rtrue, Rfalse ] →  M ⊨ Rtrue ∪ Rfalse.
83   
84 lemma eq_mk_tape_rightof :
85  ∀alpha,a,al.mk_tape alpha (a::al) (None ?) [ ] = rightof ? a al.
86 #alpha #a #al %
87 qed.
88
89 axiom daemon : ∀P:Prop.P.
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 sem_obj_to_cfg : obj_to_cfg ⊨  R_obj_to_cfg.
105 @(sem_seq_app FSUnialpha 2 ????? (sem_move_multi ? 2 cfg L ?)
106    (sem_seq ??????
107     (sem_if ??????????
108      (sem_test_null_multi ?? obj ?)
109       (sem_seq ?????? (accRealize_to_Realize … (sem_copy_step …))
110        (sem_move_multi ? 2 cfg L ?))
111       (sem_inject ???? cfg ? (sem_write FSUnialpha null)))
112      (sem_seq ?????? (sem_inject ???? cfg ? (sem_move_to_end_l ?))
113        (sem_move_multi ? 2 cfg R ?)))) //
114 #ta #tb *
115 #tc * whd in ⊢ (%→?); #Htc *
116 #td * *
117 [ * #te * * #Hcurtc #Hte
118   * destruct (Hte) #te * *
119   [ whd in ⊢ (%→%→?); * #x * #y * * -Hcurtc #Hcurtc1 #Hcurtc2 #Hte #Htd
120     * #tf * * * whd in ⊢ (%→%→%→%→?); #Htf1 #Htf2 #Htf3 #Htb
121     #c #ls #Hta1 %
122     [ #lso #x0 #rso #Hta2 >Hta1 in Htc; >eq_mk_tape_rightof 
123       whd in match (tape_move ???); #Htc
124       cut (tf = change_vec ?? tc (mk_tape ? [ ] (None ?) (reverse ? ls@[x])) cfg)
125       [@daemon] -Htf1 -Htf2 -Htf3 #Htf destruct (Htf Hte Htd Htc Htb)
126       >change_vec_change_vec >change_vec_change_vec >change_vec_change_vec
127       >nth_change_vec // >tape_move_mk_tape_R
128       @daemon
129     | #Hta2 >Htc in Hcurtc1; >nth_change_vec_neq [| @sym_not_eq //]
130       >Hta2 #H destruct (H)
131     ]
132   | * #Hcurtc0 #Hte #_ #_ #c #ls #Hta1 >Hta1 in Htc; >eq_mk_tape_rightof
133     whd in match (tape_move ???); #Htc >Htc in Hcurtc0; *
134     [ >Htc in Hcurtc; >nth_change_vec_neq [|@sym_not_eq //]
135       #Hcurtc #Hcurtc0 >Hcurtc0 in Hcurtc; * #H @False_ind @H %
136     | >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H) ]
137   ]
138 | * #te * * #Hcurtc #Hte
139   * whd in ⊢ (%→%→?); #Htd1 #Htd2
140   * #tf * * * #Htf1 #Htf2 #Htf3 whd in ⊢ (%→?); #Htb
141   #c #ls #Hta1 %
142   [ #lso #x #rso #Hta2 >Htc in Hcurtc; >nth_change_vec_neq [|@sym_not_eq //] 
143     >Hta2 normalize in ⊢ (%→?); #H destruct (H)
144   | #_ >Hta1 in Htc; >eq_mk_tape_rightof whd in match (tape_move ???); #Htc
145     destruct (Hte) cut (td = change_vec ?? tc (midtape ? ls null []) cfg)
146     [@daemon] -Htd1 -Htd2 #Htd
147     -Htf1 cut (tf = change_vec ?? td (mk_tape ? [ ] (None ?) (reverse ? ls@[null])) cfg)
148     [@daemon] -Htf2 -Htf3 #Htf destruct (Htf Htd Htc Htb)
149     >change_vec_change_vec >change_vec_change_vec >change_vec_change_vec
150     >change_vec_change_vec >change_vec_change_vec >nth_change_vec //
151     >reverse_cons >tape_move_mk_tape_R /2/ ]
152 ]
153 qed.
154
155 definition test_null_char ≝ test_char FSUnialpha (λc.c == null).
156
157 definition R_test_null_char_true ≝ λt1,t2.
158   current FSUnialpha t1 = Some ? null ∧ t1 = t2.
159   
160 definition R_test_null_char_false ≝ λt1,t2.
161   current FSUnialpha t1 ≠ Some ? null ∧ t1 = t2.
162   
163 lemma sem_test_null_char :
164   test_null_char ⊨ [ tc_true : R_test_null_char_true, R_test_null_char_false].
165 #t1 cases (sem_test_char FSUnialpha (λc.c == null) t1) #k * #outc * * #Hloop #Htrue
166 #Hfalse %{k} %{outc} % [ %
167 [ @Hloop
168 | #Houtc cases (Htrue ?) [| @Houtc] * #c * #Hcurt1 #Hcnull lapply (\P Hcnull)
169   -Hcnull #H destruct (H) #Houtc1 %
170   [ @Hcurt1 | <Houtc1 % ] ]
171 | #Houtc cases (Hfalse ?) [| @Houtc] #Hc #Houtc %
172   [ % #Hcurt1 >Hcurt1 in Hc; #Hc lapply (Hc ? (refl ??)) 
173     >(?:((null:FSUnialpha) == null) = true) [|@(\b (refl ??)) ]
174     #H destruct (H)
175   | <Houtc % ] ]
176 qed.
177
178 definition cfg_to_obj ≝
179   mmove cfg FSUnialpha 2 L ·
180   (ifTM ?? (inject_TM ? test_null_char 2 cfg)
181     (nop ? 2)
182     (copy_step cfg obj FSUnialpha 2 ·
183      mmove cfg FSUnialpha 2 L) 
184      tc_true) ·
185   inject_TM ? (move_to_end FSUnialpha L) 2 cfg ·
186   mmove cfg FSUnialpha 2 R.
187   
188 definition R_cfg_to_obj ≝ λt1,t2:Vector (tape FSUnialpha) 3.
189   ∀c,ls.
190   nth cfg ? t1 (niltape ?) = mk_tape FSUnialpha (c::ls) (None ?) [ ] → 
191   (c = null → 
192    t2 = change_vec ?? t1
193          (mk_tape ? [ ] (option_hd FSUnialpha (reverse ? (c::ls))) 
194            (tail ? (reverse ? (c::ls)))) cfg) ∧
195   (c ≠ null → 
196    t2 = change_vec ??
197           (change_vec ?? t1
198              (midtape ? (left ? (nth obj ? t1 (niltape ?))) c (right ? (nth obj ? t1 (niltape ?)))) obj)
199           (mk_tape ? [ ] (option_hd ? (reverse ? (c::ls))) (tail ? (reverse ? (c::ls)))) cfg).
200
201 axiom sem_cfg_to_obj : obj_to_cfg ⊨  R_obj_to_cfg.
202 (*@(sem_seq_app FSUnialpha 2 ????? (sem_move_multi ? 2 cfg L ?)
203    (sem_seq ??????
204     (sem_if ??????????
205      (sem_test_null_multi ?? obj ?)
206       (sem_seq ?????? (accRealize_to_Realize … (sem_copy_step …))
207        (sem_move_multi ? 2 cfg L ?))
208       (sem_inject ???? cfg ? (sem_write FSUnialpha null)))
209      (sem_seq ?????? (sem_inject ???? cfg ? (sem_move_to_end_l ?))
210        (sem_move_multi ? 2 cfg R ?)))) //
211 #ta #tb *
212 #tc * whd in ⊢ (%→?); #Htc *
213 #td * *
214 [ * #te * * #Hcurtc #Hte
215   * destruct (Hte) #te * *
216   [ whd in ⊢ (%→%→?); * #x * #y * * -Hcurtc #Hcurtc1 #Hcurtc2 #Hte #Htd
217     * #tf * * * whd in ⊢ (%→%→%→%→?); #Htf1 #Htf2 #Htf3 #Htb
218     #c #ls #Hta1 %
219     [ #lso #x0 #rso #Hta2 >Hta1 in Htc; >eq_mk_tape_rightof 
220       whd in match (tape_move ???); #Htc
221       cut (tf = change_vec ?? tc (mk_tape ? [ ] (None ?) (reverse ? ls@[x])) cfg)
222       [@daemon] -Htf1 -Htf2 -Htf3 #Htf destruct (Htf Hte Htd Htc Htb)
223       >change_vec_change_vec >change_vec_change_vec >change_vec_change_vec
224       >nth_change_vec // >tape_move_mk_tape_R
225       @daemon
226     | #Hta2 >Htc in Hcurtc1; >nth_change_vec_neq [| @sym_not_eq //]
227       >Hta2 #H destruct (H)
228     ]
229   | * #Hcurtc0 #Hte #_ #_ #c #ls #Hta1 >Hta1 in Htc; >eq_mk_tape_rightof
230     whd in match (tape_move ???); #Htc >Htc in Hcurtc0; *
231     [ >Htc in Hcurtc; >nth_change_vec_neq [|@sym_not_eq //]
232       #Hcurtc #Hcurtc0 >Hcurtc0 in Hcurtc; * #H @False_ind @H %
233     | >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H) ]
234   ]
235 | * #te * * #Hcurtc #Hte
236   * whd in ⊢ (%→%→?); #Htd1 #Htd2
237   * #tf * * * #Htf1 #Htf2 #Htf3 whd in ⊢ (%→?); #Htb
238   #c #ls #Hta1 %
239   [ #lso #x #rso #Hta2 >Htc in Hcurtc; >nth_change_vec_neq [|@sym_not_eq //] 
240     >Hta2 normalize in ⊢ (%→?); #H destruct (H)
241   | #_ >Hta1 in Htc; >eq_mk_tape_rightof whd in match (tape_move ???); #Htc
242     destruct (Hte) cut (td = change_vec ?? tc (midtape ? ls null []) cfg)
243     [@daemon] -Htd1 -Htd2 #Htd
244     -Htf1 cut (tf = change_vec ?? td (mk_tape ? [ ] (None ?) (reverse ? ls@[null])) cfg)
245     [@daemon] -Htf2 -Htf3 #Htf destruct (Htf Htd Htc Htb)
246     >change_vec_change_vec >change_vec_change_vec >change_vec_change_vec
247     >change_vec_change_vec >change_vec_change_vec >nth_change_vec //
248     >reverse_cons >tape_move_mk_tape_R /2/ ]
249 ]
250 qed.
251 *)
252
253 (* macchina che muove il nastro obj a destra o sinistra a seconda del valore
254    del current di prg, che codifica la direzione in cui ci muoviamo *)
255
256 axiom tape_move_obj : mTM FSUnialpha 2.
257
258 definition unistep ≝ 
259   obj_to_cfg · match_m cfg prg FSUnialpha 2 · copy prg cfg FSUnialpha 2 ·
260    cfg_to_obj · tape_move_obj.