]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/multi_universal/moves_2.ma
unistep_aux
[helm.git] / matita / matita / lib / turing / multi_universal / moves_2.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/turing.ma".
13 include "turing/inject.ma".
14 include "turing/while_multi.ma".
15 include "turing/while_machine.ma".
16
17 definition parmove_states ≝ initN 3.
18
19 definition parmove0 : parmove_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 3 (refl …)).
20 definition parmove1 : parmove_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 3 (refl …)).
21 definition parmove2 : parmove_states ≝ mk_Sig ?? 2 (leb_true_to_le 3 3 (refl …)).
22
23 (*
24
25 src: a b c ... z ---→ a b c ... z 
26      ^                            ^
27
28 dst: _ _ _ ... _ ---→ a b c ... z 
29      ^                            ^
30
31 0) (x,_) → (x,_)(R,R) → 1
32    (None,_) → None 2
33 1) (_,_) → None 1
34 2) (_,_) → None 2
35
36 *)
37
38 definition trans_parmove_step ≝ 
39  λsrc,dst,sig,n,D.
40  λp:parmove_states × (Vector (option sig) (S n)).
41  let 〈q,a〉 ≝ p in
42  match pi1 … q with
43  [ O ⇒ match nth src ? a (None ?) with
44    [ None ⇒ 〈parmove2,null_action sig n〉
45    | Some a0 ⇒ match nth dst ? a (None ?) with
46      [ None ⇒ 〈parmove2,null_action ? n〉
47      | Some a1 ⇒ 〈parmove1,change_vec ? (S n)
48                           (change_vec ?(S n)
49                            (null_action ? n) (〈None ?,D〉) src)
50                           (〈None ?,D〉) dst〉 ] ]
51  | S q ⇒ match q with 
52    [ O ⇒ (* 1 *) 〈parmove1,null_action ? n〉
53    | S _ ⇒ (* 2 *) 〈parmove2,null_action ? n〉 ] ].
54
55 definition parmove_step ≝ 
56   λsrc,dst,sig,n,D.
57   mk_mTM sig n parmove_states (trans_parmove_step src dst sig n D) 
58     parmove0 (λq.q == parmove1 ∨ q == parmove2).
59
60 definition R_parmove_step_true ≝ 
61   λsrc,dst,sig,n,D.λint,outt: Vector (tape sig) (S n).
62   ∃x1,x2.
63    current ? (nth src ? int (niltape ?)) = Some ? x1 ∧
64    current ? (nth dst ? int (niltape ?)) = Some ? x2 ∧
65    outt = change_vec ?? 
66             (change_vec ?? int
67               (tape_move ? (nth src ? int (niltape ?)) D) src)
68             (tape_move ? (nth dst ? int (niltape ?)) D) dst.
69
70 definition R_parmove_step_false ≝ 
71   λsrc,dst:nat.λsig,n.λint,outt: Vector (tape sig) (S n).
72   (current ? (nth src ? int (niltape ?)) = None ?  ∨
73    current ? (nth dst ? int (niltape ?)) = None ?) ∧
74    outt = int.
75
76 lemma parmove_q0_q2_null_src :
77   ∀src,dst,sig,n,D,v.src < S n → dst < S n → 
78   nth src ? (current_chars ?? v) (None ?) = None ? → 
79   step sig n (parmove_step src dst sig n D)
80     (mk_mconfig ??? parmove0 v) =
81     mk_mconfig ??? parmove2 v.
82 #src #dst #sig #n #D #v #Hsrc #Hdst #Hcurrent
83 whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?);
84 @eq_f2
85 [ whd in ⊢ (??(???%)?); >Hcurrent %
86 | whd in ⊢ (??(????(???%))?); >Hcurrent @tape_move_null_action ]
87 qed.
88
89 lemma parmove_q0_q2_null_dst :
90   ∀src,dst,sig,n,D,v,s.src < S n → dst < S n → 
91   nth src ? (current_chars ?? v) (None ?) = Some ? s → 
92   nth dst ? (current_chars ?? v) (None ?) = None ? → 
93   step sig n (parmove_step src dst sig n D)
94     (mk_mconfig ??? parmove0 v) =
95     mk_mconfig ??? parmove2 v.
96 #src #dst #sig #n #D #v #s #Hsrc #Hdst #Hcursrc #Hcurdst
97 whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?);
98 @eq_f2
99 [ whd in ⊢ (??(???%)?); >Hcursrc whd in ⊢ (??(???%)?); >Hcurdst %
100 | whd in ⊢ (??(????(???%))?); >Hcursrc
101   whd in ⊢ (??(????(???%))?); >Hcurdst @tape_move_null_action ]
102 qed.
103
104 lemma parmove_q0_q1 :
105   ∀src,dst,sig,n,D,v.src ≠ dst → src < S n → dst < S n → 
106   ∀a1,a2.
107   nth src ? (current_chars ?? v) (None ?) = Some ? a1 →
108   nth dst ? (current_chars ?? v) (None ?) = Some ? a2 → 
109   step sig n (parmove_step src dst sig n D)
110     (mk_mconfig ??? parmove0 v) =
111     mk_mconfig ??? parmove1 
112      (change_vec ? (S n) 
113        (change_vec ?? v
114          (tape_move ? (nth src ? v (niltape ?)) D) src)
115        (tape_move ? (nth dst ? v (niltape ?)) D) dst).
116 #src #dst #sig #n #D #v #Hneq #Hsrc #Hdst
117 #a1 #a2 #Hcursrc #Hcurdst
118 whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
119 [ whd in match (trans ????);
120   >Hcursrc >Hcurdst %
121 | whd in match (trans ????);
122   >Hcursrc >Hcurdst whd in ⊢ (??(????(???%))?); 
123   >tape_move_multi_def <(change_vec_same ?? v dst (niltape ?)) in ⊢ (??%?);
124   >pmap_change <(change_vec_same ?? v src (niltape ?)) in ⊢(??%?);
125   >pmap_change <tape_move_multi_def >tape_move_null_action
126   @eq_f2 // >nth_change_vec_neq //
127 ]
128 qed.
129
130 lemma sem_parmove_step :
131   ∀src,dst,sig,n,D.src ≠ dst → src < S n → dst < S n → 
132   parmove_step src dst sig n D ⊨ 
133     [ parmove1: R_parmove_step_true src dst sig n D, 
134              R_parmove_step_false src dst sig n ].
135 #src #dst #sig #n #D #Hneq #Hsrc #Hdst #int
136 lapply (refl ? (current ? (nth src ? int (niltape ?))))
137 cases (current ? (nth src ? int (niltape ?))) in ⊢ (???%→?);
138 [ #Hcursrc %{2} %
139   [| % [ %
140     [ whd in ⊢ (??%?); >parmove_q0_q2_null_src /2/
141     | normalize in ⊢ (%→?); #H destruct (H) ]
142     | #_ % // % // ] ]
143 | #a #Ha lapply (refl ? (current ? (nth dst ? int (niltape ?))))
144   cases (current ? (nth dst ? int (niltape ?))) in ⊢ (???%→?);
145   [ #Hcurdst %{2} %
146     [| % [ %
147       [ whd in ⊢ (??%?); >(parmove_q0_q2_null_dst …) /2/ 
148       | normalize in ⊢ (%→?); #H destruct (H) ]
149       | #_ % // %2 // ] ]
150   | #b #Hb %{2} %
151     [| % [ % 
152       [whd in ⊢  (??%?); >(parmove_q0_q1 … Hneq Hsrc Hdst ? b ??)
153         [2: <(nth_vec_map ?? (current …) dst ? int (niltape ?)) //
154         |3: <(nth_vec_map ?? (current …) src ? int (niltape ?)) //
155         | // ]
156       | #_ %{a} %{b} % // % // ]
157       | * #H @False_ind @H % ]
158 ]]]
159 qed.
160
161 definition parmove ≝ λsrc,dst,sig,n,D.
162   whileTM … (parmove_step src dst sig n D) parmove1.
163
164 definition R_parmoveL ≝ 
165   λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n).
166   (∀x,xs,rs.
167     nth src ? int (niltape ?) = midtape sig xs x rs → 
168     ∀ls0,x0,target,rs0.|xs| = |target| → 
169     nth dst ? int (niltape ?) = midtape sig (target@ls0) x0 rs0 → 
170     outt = change_vec ?? 
171            (change_vec ?? int (mk_tape sig [] (None ?) (reverse ? xs@x::rs)) src)
172            (mk_tape sig (tail ? ls0) (option_hd ? ls0) (reverse ? target@x0::rs0)) dst) ∧
173   ((current ? (nth src ? int (niltape ?)) = None ? ∨
174     current ? (nth dst ? int (niltape ?)) = None ?) →
175     outt = int).
176   
177 lemma wsem_parmoveL : ∀src,dst,sig,n.src ≠ dst → src < S n → dst < S n → 
178   parmove src dst sig n L ⊫ R_parmoveL src dst sig n.
179 #src #dst #sig #n #Hneq #Hsrc #Hdst #ta #k #outc #Hloop
180 lapply (sem_while … (sem_parmove_step src dst sig n L Hneq Hsrc Hdst) … Hloop) //
181 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
182 [ whd in ⊢ (%→?); * #H #Houtc % [2: #_ @Houtc ] cases H
183   [ #Hcurtb #x #xs #rs #Hsrctb >Hsrctb in Hcurtb; normalize in ⊢ (%→?);
184     #Hfalse destruct (Hfalse)
185   | #Hcur_dst #x #xs #rs #Hsrctb #ls0 #x0 #target 
186     #rs0 #Hlen #Hdsttb >Hdsttb in Hcur_dst; normalize in ⊢ (%→?); #H destruct (H)
187   ]  
188 | #td #te * #c0 * #c1 * * #Hc0 #Hc1 #Hd #Hstar #IH #He 
189   lapply (IH He) -IH * #IH1 #IH2 %
190   [ #x #xs #rs #Hsrc_td #ls0 #x0 #target
191     #rs0 #Hlen #Hdst_td
192     >Hsrc_td in Hc0; normalize in ⊢ (%→?); #Hc0 destruct (Hc0)
193     >Hdst_td in Hd; >Hsrc_td @(list_cases2 … Hlen)
194     [ #Hxsnil #Htargetnil >Hxsnil >Htargetnil #Hd >IH2 
195       [2: %1 >Hd >nth_change_vec_neq [|@(sym_not_eq … Hneq)]
196       >nth_change_vec //]  
197       >Hd -Hd @(eq_vec … (niltape ?))
198       #i #Hi cases (decidable_eq_nat i src) #Hisrc
199       [ >Hisrc >(nth_change_vec_neq … src dst) [|@(sym_not_eq … Hneq)]
200         >nth_change_vec //
201         >(nth_change_vec_neq … src dst) [|@(sym_not_eq … Hneq)]
202         >nth_change_vec //
203       | cases (decidable_eq_nat i dst) #Hidst
204         [ >Hidst >nth_change_vec // >nth_change_vec //
205           >Hdst_td in Hc1; >Htargetnil
206           normalize in ⊢ (%→?); #Hc1 destruct (Hc1) cases ls0 //
207         | >nth_change_vec_neq [|@(sym_not_eq … Hidst)]
208           >nth_change_vec_neq [|@(sym_not_eq … Hisrc)]
209           >nth_change_vec_neq [|@(sym_not_eq … Hidst)]
210           >nth_change_vec_neq [|@(sym_not_eq … Hisrc)] % 
211         ]
212       ]
213     | #hd1 #hd2 #tl1 #tl2 #Hxs #Htarget >Hxs >Htarget #Hd
214       >(IH1 hd1 tl1 (c0::rs) ? ls0 hd2 tl2 (x0::rs0))
215       [ >Hd >(change_vec_commute … ?? td ?? src dst) //
216        >change_vec_change_vec
217        >(change_vec_commute … ?? td ?? dst src) [|@sym_not_eq //]
218        >change_vec_change_vec
219        >reverse_cons >associative_append
220        >reverse_cons >associative_append % 
221      | >Hd >nth_change_vec //
222      | >Hxs in Hlen; >Htarget normalize #Hlen destruct (Hlen) //
223      | >Hd >nth_change_vec_neq [|@sym_not_eq //]
224        >nth_change_vec // ]
225    ]
226  | >Hc0 >Hc1 * [ #Hc0 destruct (Hc0) | #Hc1 destruct (Hc1) ]
227  ] ]
228 qed.
229  
230 lemma terminate_parmoveL :  ∀src,dst,sig,n,t.
231   src ≠ dst → src < S n → dst < S n → 
232   parmove src dst sig n L ↓ t.
233 #src #dst #sig #n #t #Hneq #Hsrc #Hdst
234 @(terminate_while … (sem_parmove_step …)) //
235 <(change_vec_same … t src (niltape ?))
236 cases (nth src (tape sig) t (niltape ?))
237 [ % #t1 * #x1 * #x2 * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct 
238 |2,3: #a0 #al0 % #t1 * #x1 * #x2 * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
239 | #ls lapply t -t elim ls
240   [#t #c #rs % #t1 * #x1 * #x2 * * >nth_change_vec // normalize in ⊢ (%→?);
241    #H1 destruct (H1) #Hcurdst >change_vec_change_vec #Ht1 % 
242    #t2 * #y1 * #y2 * * >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
243    >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H)
244   |#l0 #ls0 #IH #t #c #rs % #t1 * #x1 * #x2 * * >nth_change_vec //
245    normalize in ⊢ (%→?); #H destruct (H) #Hcurdst
246    >change_vec_change_vec >change_vec_commute // #Ht1 >Ht1 @IH
247   ]
248 ]
249 qed.
250
251 lemma sem_parmoveL : ∀src,dst,sig,n.
252   src ≠ dst → src < S n → dst < S n → 
253   parmove src dst sig n L ⊨ R_parmoveL src dst sig n.
254 #src #dst #sig #n #Hneq #Hsrc #Hdst @WRealize_to_Realize 
255 [/2/ | @wsem_parmoveL //]
256 qed.
257
258 (* while {
259      if current != null 
260         then move_r
261         else nop
262      }
263  *)
264  
265 definition mte_states ≝ initN 3.
266 definition mte0 : mte_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 3 (refl …)).
267 definition mte1 : mte_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 3 (refl …)).
268 definition mte2 : mte_states ≝ mk_Sig ?? 2 (leb_true_to_le 3 3 (refl …)).
269
270 definition mte_step ≝ 
271   λalpha:FinSet.λD.mk_TM alpha mte_states
272   (λp.let 〈q,a〉 ≝ p in
273     match a with
274     [ None ⇒ 〈mte1,None ?,N〉
275     | Some a' ⇒ match (pi1 … q) with
276       [ O ⇒ 〈mte2,Some ? a',D〉
277       | S q ⇒ 〈mte2,None ?,N〉 ] ])
278   mte0 (λq.q == mte1 ∨ q == mte2).
279   
280 definition R_mte_step_true ≝ λalpha,D,t1,t2.
281   ∃ls,c,rs.
282     t1 = midtape alpha ls c rs ∧ t2 = tape_move ? t1 D.
283
284 definition R_mte_step_false ≝ λalpha.λt1,t2:tape alpha.
285   current ? t1 = None ? ∧ t1 = t2.
286
287 lemma sem_mte_step :
288   ∀alpha,D.mte_step alpha D ⊨ [ mte2 : R_mte_step_true alpha D, R_mte_step_false alpha ] .
289 #alpha #D #intape @(ex_intro ?? 2) cases intape
290 [ @ex_intro
291   [| % [ % [ % | normalize #H destruct ] | #_ % // ] ]
292 |#a #al @ex_intro
293   [| % [ % [ % | normalize #H destruct ] | #_ % // ] ]
294 |#a #al @ex_intro
295   [| % [ % [ % | normalize #H destruct ] | #_ % // ] ]
296 | #ls #c #rs
297   @ex_intro [| % [ % [ % | #_ %{ls} %{c} %{rs} % // ]
298                      | normalize in ⊢ (?(??%?)→?); * #H @False_ind /2/ ] ] ]
299 qed.
300
301 definition move_to_end ≝ λsig,D.whileTM sig (mte_step sig D) mte2.
302
303 definition R_move_to_end_r ≝ 
304   λsig,int,outt.
305   (current ? int = None ? → outt = int) ∧
306   ∀ls,c,rs.int = midtape sig ls c rs → outt = mk_tape ? (reverse ? rs@c::ls) (None ?) [ ].
307   
308 lemma wsem_move_to_end_r : ∀sig. move_to_end sig R ⊫ R_move_to_end_r sig.
309 #sig #ta #k #outc #Hloop
310 lapply (sem_while … (sem_mte_step sig R) … Hloop) //
311 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
312 [ * #Hcurtb #Houtc % /2/ #ls #c #rs #Htb >Htb in Hcurtb; normalize in ⊢ (%→?); #H destruct (H)
313 | #tc #td * #ls * #c * #rs * #Htc >Htc cases rs
314   [ normalize in ⊢ (%→?); #Htd >Htd #Hstar #IH whd in ⊢ (%→?); #Hfalse
315     lapply (IH Hfalse) -IH * #Htd1 #_ %
316     [ normalize in ⊢ (%→?); #H destruct (H)
317     | #ls0 #c0 #rs0 #H destruct (H) >Htd1 // ]
318   | #r0 #rs0 whd in ⊢ (???%→?); #Htd >Htd #Hstar #IH whd in ⊢ (%→?); #Hfalse
319     lapply (IH Hfalse) -IH * #_ #IH %
320     [ normalize in ⊢ (%→?); #H destruct (H)
321     | #ls1 #c1 #rs1 #H destruct (H) >reverse_cons >associative_append @IH % ] ] ]
322 qed.
323
324 definition R_move_to_end_l ≝ 
325   λsig,int,outt.
326   (current ? int = None ? → outt = int) ∧
327   ∀ls,c,rs.int = midtape sig ls c rs → outt = mk_tape ? [ ] (None ?) (reverse ? ls@c::rs).
328   
329 lemma wsem_move_to_end_l : ∀sig. move_to_end sig L ⊫ R_move_to_end_l sig.
330 #sig #ta #k #outc #Hloop
331 lapply (sem_while … (sem_mte_step sig L) … Hloop) //
332 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
333 [ * #Hcurtb #Houtc % /2/ #ls #c #rs #Htb >Htb in Hcurtb; normalize in ⊢ (%→?); #H destruct (H)
334 | #tc #td * #ls * #c * #rs * #Htc >Htc cases ls
335   [ normalize in ⊢ (%→?); #Htd >Htd #Hstar #IH whd in ⊢ (%→?); #Hfalse
336     lapply (IH Hfalse) -IH * #Htd1 #_ %
337     [ normalize in ⊢ (%→?); #H destruct (H)
338     | #ls0 #c0 #rs0 #H destruct (H) >Htd1 // ]
339   | #l0 #ls0 whd in ⊢ (???%→?); #Htd >Htd #Hstar #IH whd in ⊢ (%→?); #Hfalse
340     lapply (IH Hfalse) -IH * #_ #IH %
341     [ normalize in ⊢ (%→?); #H destruct (H)
342     | #ls1 #c1 #rs1 #H destruct (H) >reverse_cons >associative_append @IH % ] ] ]
343 qed.