]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/multi_universal/copy.ma
e4931202652279a6abe6745086cf2b99444d1508
[helm.git] / matita / matita / lib / turing / multi_universal / copy.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||         The HELM team.                                      *)
8 (*      ||A||         http://helm.cs.unibo.it                             *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU General Public License Version 2                  *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 include "turing/turing.ma".
16 include "turing/inject.ma".
17 include "turing/while_multi.ma".
18
19 definition copy_states ≝ initN 3.
20
21 definition copy0 : copy_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 3 (refl …)).
22 definition copy1 : copy_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 3 (refl …)).
23 definition copy2 : copy_states ≝ mk_Sig ?? 2 (leb_true_to_le 3 3 (refl …)).
24
25 (*
26
27 src: a b c ... z # ---→ a b c ... z #
28      ^                              ^
29
30 dst: _ _ _ ... _ d ---→ a b c ... z d
31      ^                              ^
32
33 0) (x ≠ sep,_) → (x,x)(R,R) → 1
34    (sep,d) → None 2
35 1) (_,_) → None 1
36 2) (_,_) → None 2
37
38 *)
39
40 definition trans_copy_step ≝ 
41  λsrc,dst,sig,n,is_sep.
42  λp:copy_states × (Vector (option sig) (S n)).
43  let 〈q,a〉 ≝ p in
44  match pi1 … q with
45  [ O ⇒ match nth src ? a (None ?) with
46    [ None ⇒ 〈copy2,null_action ? n〉
47    | Some a0 ⇒ if is_sep a0 then 〈copy2,null_action ? n〉
48                   else 〈copy1,change_vec ? (S n)
49                           (change_vec ?(S n)
50                            (null_action ? n) (Some ? 〈a0,R〉) src)
51                           (Some ? 〈a0,R〉) dst〉 ]
52  | S q ⇒ match q with 
53    [ O ⇒ (* 1 *) 〈copy1,null_action ? n〉
54    | S _ ⇒ (* 2 *) 〈copy2,null_action ? n〉 ] ].
55
56 definition copy_step ≝ 
57   λsrc,dst,sig,n,is_sep.
58   mk_mTM sig n copy_states (trans_copy_step src dst sig n is_sep) 
59     copy0 (λq.q == copy1 ∨ q == copy2).
60
61 definition R_copy_step_true ≝ 
62   λsrc,dst,sig,n,is_sep.λint,outt: Vector (tape sig) (S n).
63   ∃x1.
64    current ? (nth src ? int (niltape ?)) = Some ? x1 ∧
65    is_sep x1 = false ∧
66    outt = change_vec ?? 
67             (change_vec ?? int
68               (tape_move ? (nth src ? int (niltape ?)) (Some ? 〈x1,R〉)) src)
69             (tape_move ? (nth dst ? int (niltape ?)) (Some ? 〈x1,R〉)) dst.
70
71 definition R_copy_step_false ≝ 
72   λsrc,dst:nat.λsig,n,is_sep.λint,outt: Vector (tape sig) (S n).
73   (∃x1.
74    current ? (nth src ? int (niltape ?)) = Some ? x1 ∧
75    is_sep x1 = true ∧ outt = int) ∨
76    current ? (nth src ? int (niltape ?)) = None ? ∧
77    outt = int.
78
79 lemma copy_q0_q2_null :
80   ∀src,dst,sig,n,is_sep,v,t.src < S n → dst < S n → 
81   current ? t = None ? → 
82   step sig n (copy_step src dst sig n is_sep)
83     (mk_mconfig ??? copy0 (change_vec ? (S n) v t src)) =
84     mk_mconfig ??? copy2 (change_vec ? (S n) v t src).
85 #src #dst #sig #n #is_sep #v #t #Hsrc #Hdst #Hcurrent
86 whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
87 [ >current_chars_change_vec // whd in match (trans ????);
88   >nth_change_vec // >Hcurrent %
89 | >current_chars_change_vec // whd in match (trans ????);
90   >nth_change_vec // >Hcurrent @tape_move_null_action
91 ]
92 qed.
93
94 lemma copy_q0_q2_sep :
95   ∀src,dst,sig,n,is_sep,v,t.src < S n → dst < S n → 
96   ∀s.current ? t = Some ? s → is_sep s = true → 
97   step sig n (copy_step src dst sig n is_sep)
98     (mk_mconfig ??? copy0 (change_vec ? (S n) v t src)) =
99     mk_mconfig ??? copy2 (change_vec ? (S n) v t src).
100 #src #dst #sig #n #is_sep #v #t #Hsrc #Hdst #s #Hcurrent #Hsep
101 whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
102 [ >current_chars_change_vec // whd in match (trans ????);
103   >nth_change_vec // >Hcurrent whd in ⊢ (??(???%)?); >Hsep %
104 | >current_chars_change_vec // whd in match (trans ????);
105   >nth_change_vec // >Hcurrent whd in ⊢ (??(???????(???%))?);
106   >Hsep @tape_move_null_action
107 ]
108 qed.
109
110 lemma change_vec_commute : ∀A,n,v,a,b,i,j. i ≠ j → 
111   change_vec A n (change_vec A n v a i) b j
112   = change_vec A n (change_vec A n v b j) a i.
113 #A #n #v #a #b #i #j #Hij @(eq_vec … a)
114 #k #Hk cases (decidable_eq_nat k i) #Hki
115 [ >Hki >nth_change_vec // >(nth_change_vec_neq ??????? (sym_not_eq … Hij))
116   >nth_change_vec //
117 | cases (decidable_eq_nat k j) #Hkj
118   [ >Hkj >nth_change_vec // >(nth_change_vec_neq ??????? Hij) >nth_change_vec //
119   | >(nth_change_vec_neq ??????? (sym_not_eq … Hki))
120     >(nth_change_vec_neq ??????? (sym_not_eq … Hkj))
121     >(nth_change_vec_neq ??????? (sym_not_eq … Hki))
122     >(nth_change_vec_neq ??????? (sym_not_eq … Hkj)) //
123   ]
124 ]
125 qed.
126
127 lemma copy_q0_q1 :
128   ∀src,dst,sig,n,is_sep,v,t.src ≠ dst → src < S n → dst < S n → 
129   ∀s.current ? t = Some ? s → is_sep s = false → 
130   step sig n (copy_step src dst sig n is_sep)
131     (mk_mconfig ??? copy0 (change_vec ? (S n) v t src)) =
132     mk_mconfig ??? copy1 
133      (change_vec ? (S n) 
134        (change_vec ?? v
135          (tape_move ? t (Some ? 〈s,R〉)) src)
136        (tape_move ? (nth dst ? v (niltape ?)) (Some ? 〈s,R〉)) dst).
137 #src #dst #sig #n #is_sep #v #t #Hneq #Hsrc #Hdst #s #Hcurrent #Hsep
138 whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
139 [ >current_chars_change_vec // whd in match (trans ????);
140   >nth_change_vec // >Hcurrent whd in ⊢ (??(???%)?); >Hsep %
141 | >current_chars_change_vec // whd in match (trans ????);
142   >nth_change_vec // >Hcurrent whd in ⊢ (??(???????(???%))?);
143   >Hsep whd in ⊢ (??(???????(???%))?); >change_vec_commute // >pmap_change
144   >change_vec_commute // @eq_f3 //
145   <(change_vec_same ?? v dst (niltape ?)) in ⊢(??%?);
146   >pmap_change @eq_f3 //
147 ]
148 qed.
149
150 lemma sem_copy_step :
151   ∀src,dst,sig,n,is_sep.src ≠ dst → src < S n → dst < S n → 
152   copy_step src dst sig n is_sep ⊨ 
153     [ copy1: R_copy_step_true src dst sig n is_sep, 
154              R_copy_step_false src dst sig n is_sep ].
155 #src #dst #sig #n #is_sep #Hneq #Hsrc #Hdst #int
156 lapply (refl ? (current ? (nth src ? int (niltape ?))))
157 cases (current ? (nth src ? int (niltape ?))) in ⊢ (???%→?);
158 [ #Hcur <(change_vec_same … int src (niltape ?)) %{2} %
159   [| % [ % 
160     [ whd in ⊢ (??%?); >copy_q0_q2_null /2/ 
161     | normalize in ⊢ (%→?); #H destruct (H) ]
162     | #_ %2 >nth_change_vec >Hcur // % // ] ]
163 | #c #Hcur cases (true_or_false (is_sep c)) #Hsep
164   [ <(change_vec_same … int src (niltape ?)) %{2} % 
165     [| % [ %
166       [ whd in ⊢ (??%?); >copy_q0_q2_sep /2/
167       | normalize in ⊢ (%→?); #H destruct (H) ]
168       | #_ % >nth_change_vec // %{c} % [ % /2/ | // ] ] ]
169   | %{2} % [| % [ %
170     [ whd in ⊢ (??%?);
171       <(change_vec_same … int src (niltape ?)) in ⊢ (??%?);
172       >Hcur in ⊢ (??%?); whd in ⊢ (??%?); >(copy_q0_q1 … Hsep) /2/
173     | #_ whd %{c} % % /2/ ]
174     | * #Hfalse @False_ind /2/ ] ] ] ]
175 qed.
176
177 definition copy ≝ λsrc,dst,sig,n,is_sep.
178   whileTM … (copy_step src dst sig n is_sep) copy1.
179
180 definition R_copy ≝ 
181   λsrc,dst,sig,n,is_sep.λint,outt: Vector (tape sig) (S n).
182   (∀ls,x,xs,rs,sep.
183     nth src ? int (niltape ?) = midtape sig ls x (xs@sep::rs) →
184     (∀c.memb ? c (x::xs) = true → is_sep c = false) → is_sep sep = true → 
185     ∀ls0,x0,target,c,rs0.|xs| = |target| → 
186     nth dst ? int (niltape ?) = midtape sig ls0 x0 (target@c::rs0) → 
187     outt = change_vec ?? 
188            (change_vec ?? int (midtape sig (reverse ? xs@x::ls) sep rs) src)
189            (midtape sig (reverse ? xs@x::ls0) c rs0) dst) ∧
190   (∀c.current ? (nth src ? int (niltape ?)) = Some ? c → is_sep c = true → 
191    outt = int) ∧
192   (current ? (nth src ? int (niltape ?)) = None ? → outt = int).
193   
194 lemma change_vec_change_vec : ∀A,n,v,a,b,i. 
195   change_vec A n (change_vec A n v a i) b i = change_vec A n v b i.
196 #A #n #v #a #b #i @(eq_vec … a) #i0 #Hi0
197 cases (decidable_eq_nat i i0) #Hii0
198 [ >Hii0 >nth_change_vec // >nth_change_vec //
199 | >nth_change_vec_neq // >nth_change_vec_neq //
200   >nth_change_vec_neq // ]
201 qed.
202
203 lemma wsem_copy : ∀src,dst,sig,n,is_sep.src ≠ dst → src < S n → dst < S n → 
204   copy src dst sig n is_sep ⊫ R_copy src dst sig n is_sep.
205 #src #dst #sig #n #is_sep #Hneq #Hsrc #Hdst #ta #k #outc #Hloop
206 lapply (sem_while … (sem_copy_step src dst sig n is_sep Hneq Hsrc Hdst) … Hloop) //
207 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
208 [ #tc whd in ⊢ (%→?); *
209   [ * #x * * #Hx #Hsep #Houtc % [ %
210     [ #ls #x0 #xs #rs #sep #Hsrctc #Hnosep >Hsrctc in Hx; normalize in ⊢ (%→?);
211       #Hx0 destruct (Hx0) lapply (Hnosep ? (memb_hd …)) >Hsep
212       #Hfalse destruct (Hfalse)
213     | #c #Hc #Hsepc @Houtc ]
214     | #_ @Houtc ]
215   | * #Hcur #Houtc % [ %
216     [ #ls #x0 #xs #rs #sep #Hsrctc >Hsrctc in Hcur; normalize in ⊢ (%→?); 
217       #Hcur destruct (Hcur)
218     | #c #Hc #Hsepc @Houtc ]
219     | #_ @Houtc ]
220   ]
221 | #tc #td #te * #c0 * * #Hc0 #Hc0nosep #Hd #Hstar #IH #He 
222   lapply (IH He) -IH * * #IH1 #IH2 #IH3 % [ %
223   [ #ls #x #xs #rs #sep #Hsrc_tc #Hnosep #Hsep #ls0 #x0 #target
224     #c #rs0 #Hlen #Hdst_tc
225     >Hsrc_tc in Hc0; normalize in ⊢ (%→?); #Hc0 destruct (Hc0)
226     <(change_vec_same … tc src (niltape ?)) in Hd:(???(???(???%??)??));
227     <(change_vec_same … tc dst (niltape ?)) in ⊢(???(???(???%??)??)→?);
228     >Hdst_tc >Hsrc_tc >change_vec_change_vec >change_vec_change_vec
229     >(change_vec_commute ?? tc ?? dst src) [|@(sym_not_eq … Hneq)]
230     >change_vec_change_vec @(list_cases2 … Hlen)
231     [ #Hxsnil #Htargetnil #Hd>(IH2 … Hsep)
232       [ >Hd -Hd >Hxsnil >Htargetnil @(eq_vec … (niltape ?))
233         #i #Hi cases (decidable_eq_nat i src) #Hisrc
234         [ >Hisrc >(nth_change_vec_neq … src dst) [|@(sym_not_eq … Hneq)]
235           >nth_change_vec // >nth_change_vec //
236           >(nth_change_vec_neq … src dst) [|@(sym_not_eq … Hneq)]
237           >nth_change_vec // whd in ⊢ (??%?); %
238         | cases (decidable_eq_nat i dst) #Hidst
239           [ >Hidst >nth_change_vec // >nth_change_vec //
240             >nth_change_vec_neq // >Hdst_tc >Htargetnil %
241           | >nth_change_vec_neq [|@(sym_not_eq … Hidst)]
242             >nth_change_vec_neq [|@(sym_not_eq … Hisrc)]
243             >nth_change_vec_neq [|@(sym_not_eq … Hidst)]
244             >nth_change_vec_neq [|@(sym_not_eq … Hisrc)] % ]
245          ]
246       | >Hd >nth_change_vec_neq [|@(sym_not_eq … Hneq)]
247         >nth_change_vec // >nth_change_vec // >Hxsnil % ]
248     |#hd1 #hd2 #tl1 #tl2 #Hxs #Htarget >Hxs >Htarget #Hd
249      >(IH1 (c0::ls) hd1 tl1 rs sep ?? Hsep (c0::ls0) hd2 tl2 c rs0)
250      [ >Hd >(change_vec_commute … ?? tc ?? src dst) //
251        >change_vec_change_vec
252        >(change_vec_commute … ?? tc ?? dst src) [|@sym_not_eq //]
253        >change_vec_change_vec
254        >reverse_cons >associative_append >associative_append % 
255      | >Hd >nth_change_vec // >nth_change_vec_neq // >Hdst_tc >Htarget //
256      | >Hxs in Hlen; >Htarget normalize #Hlen destruct (Hlen) //
257      | <Hxs #c1 #Hc1 @Hnosep @memb_cons //
258      | >Hd >nth_change_vec_neq [|@sym_not_eq //]
259        >nth_change_vec // >nth_change_vec // ]
260    ]
261  | #c #Hc #Hsepc >Hc in Hc0; #Hcc0 destruct (Hcc0) >Hc0nosep in Hsepc;
262    #H destruct (H)
263  ]
264 | #HNone >HNone in Hc0; #Hc0 destruct (Hc0) ] ]
265 qed.
266  
267 lemma terminate_copy :  ∀src,dst,sig,n,is_sep,t.
268   src ≠ dst → src < S n → dst < S n → 
269   copy src dst sig n is_sep ↓ t.
270 #src #dst #sig #n #is_sep #t #Hneq #Hsrc #Hdst
271 @(terminate_while … (sem_copy_step …)) //
272 <(change_vec_same … t src (niltape ?))
273 cases (nth src (tape sig) t (niltape ?))
274 [ % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct 
275 |2,3: #a0 #al0 % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
276 | #ls #c #rs lapply c -c lapply ls -ls lapply t -t elim rs
277   [#t #ls #c % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?);
278    #H1 destruct (H1) #Hxsep >change_vec_change_vec #Ht1 % 
279    #t2 * #x0 * * >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
280    >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H)
281   |#r0 #rs0 #IH #t #ls #c % #t1 * #x * * >nth_change_vec //
282    normalize in ⊢ (%→?); #H destruct (H) #Hxsep
283    >change_vec_change_vec >change_vec_commute // #Ht1 >Ht1 @IH
284   ]
285 ]
286 qed.