1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| The HELM team. *)
8 (* ||A|| http://helm.cs.unibo.it *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU General Public License Version 2 *)
13 (**************************************************************************)
15 include "turing/turing.ma".
16 include "turing/inject.ma".
17 include "turing/while_multi.ma".
19 definition copy_states ≝ initN 3.
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 …)).
27 src: a b c ... z # ---→ a b c ... z #
30 dst: _ _ _ ... _ d ---→ a b c ... z d
33 0) (x ≠ sep,_) → (x,x)(R,R) → 1
40 definition trans_copy_step ≝
41 λsrc,dst,sig,n,is_sep.
42 λp:copy_states × (Vector (option sig) (S n)).
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)
50 (null_action ? n) (〈Some ? a0,R〉) src)
51 (〈Some ? a0,R〉) dst〉 ]
53 [ O ⇒ (* 1 *) 〈copy1,null_action ? n〉
54 | S _ ⇒ (* 2 *) 〈copy2,null_action ? n〉 ] ].
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).
61 definition R_copy_step_true ≝
62 λsrc,dst,sig,n,is_sep.λint,outt: Vector (tape sig) (S n).
64 current ? (nth src ? int (niltape ?)) = Some ? x1 ∧
68 (tape_move_mono ? (nth src ? int (niltape ?)) (〈Some ? x1,R〉)) src)
69 (tape_move_mono ? (nth dst ? int (niltape ?)) (〈Some ? x1,R〉)) dst.
71 definition R_copy_step_false ≝
72 λsrc,dst:nat.λsig,n,is_sep.λint,outt: Vector (tape sig) (S n).
74 current ? (nth src ? int (niltape ?)) = Some ? x1 ∧
75 is_sep x1 = true ∧ outt = int) ∨
76 current ? (nth src ? int (niltape ?)) = None ? ∧
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
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
111 ∀src,dst,sig,n,is_sep,v,t.src ≠ dst → src < S n → dst < S n →
112 ∀s.current ? t = Some ? s → is_sep s = false →
113 step sig n (copy_step src dst sig n is_sep)
114 (mk_mconfig ??? copy0 (change_vec ? (S n) v t src)) =
118 (tape_move_mono ? t (〈Some ? s,R〉)) src)
119 (tape_move_mono ? (nth dst ? v (niltape ?)) (〈Some ? s,R〉)) dst).
121 #src #dst #sig #n #is_sep #v #t #Hneq #Hsrc #Hdst #s #Hcurrent #Hsep
122 whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
123 [ >current_chars_change_vec // whd in match (trans ????);
124 >nth_change_vec // >Hcurrent whd in ⊢ (??(???%)?); >Hsep %
125 | >current_chars_change_vec // whd in match (trans ????);
126 >nth_change_vec // >Hcurrent whd in ⊢ (??(????(???%))?);
127 >Hsep whd in ⊢ (??(????(???%))?); >change_vec_commute // >pmap_change
128 >change_vec_commute // @eq_f3 //
129 <(change_vec_same ?? v dst (niltape ?)) in ⊢(??%?);
130 >pmap_change @eq_f3 //
134 lemma sem_copy_step :
135 ∀src,dst,sig,n,is_sep.src ≠ dst → src < S n → dst < S n →
136 copy_step src dst sig n is_sep ⊨
137 [ copy1: R_copy_step_true src dst sig n is_sep,
138 R_copy_step_false src dst sig n is_sep ].
139 #src #dst #sig #n #is_sep #Hneq #Hsrc #Hdst #int
140 lapply (refl ? (current ? (nth src ? int (niltape ?))))
141 cases (current ? (nth src ? int (niltape ?))) in ⊢ (???%→?);
142 [ #Hcur <(change_vec_same … int src (niltape ?)) %{2} %
144 [ whd in ⊢ (??%?); >copy_q0_q2_null /2/
145 | normalize in ⊢ (%→?); #H destruct (H) ]
146 | #_ %2 >nth_change_vec >Hcur // % // ] ]
147 | #c #Hcur cases (true_or_false (is_sep c)) #Hsep
148 [ <(change_vec_same … int src (niltape ?)) %{2} %
150 [ whd in ⊢ (??%?); >copy_q0_q2_sep /2/
151 | normalize in ⊢ (%→?); #H destruct (H) ]
152 | #_ % >nth_change_vec // %{c} % [ % /2/ | // ] ] ]
155 <(change_vec_same … int src (niltape ?)) in ⊢ (??%?);
156 >Hcur in ⊢ (??%?); whd in ⊢ (??%?); >(copy_q0_q1 … Hsep) /2/
157 | #_ whd %{c} % % /2/ ]
158 | * #Hfalse @False_ind /2/ ] ] ] ]
161 definition copy ≝ λsrc,dst,sig,n,is_sep.
162 whileTM … (copy_step src dst sig n is_sep) copy1.
165 λsrc,dst,sig,n,is_sep.λint,outt: Vector (tape sig) (S n).
167 nth src ? int (niltape ?) = midtape sig ls x (xs@sep::rs) →
168 (∀c.memb ? c (x::xs) = true → is_sep c = false) → is_sep sep = true →
169 ∀ls0,x0,target,c,rs0.|xs| = |target| →
170 nth dst ? int (niltape ?) = midtape sig ls0 x0 (target@c::rs0) →
172 (change_vec ?? int (midtape sig (reverse ? xs@x::ls) sep rs) src)
173 (midtape sig (reverse ? xs@x::ls0) c rs0) dst) ∧
174 (∀c.current ? (nth src ? int (niltape ?)) = Some ? c → is_sep c = true →
176 (current ? (nth src ? int (niltape ?)) = None ? → outt = int).
178 lemma wsem_copy : ∀src,dst,sig,n,is_sep.src ≠ dst → src < S n → dst < S n →
179 copy src dst sig n is_sep ⊫ R_copy src dst sig n is_sep.
180 #src #dst #sig #n #is_sep #Hneq #Hsrc #Hdst #ta #k #outc #Hloop
181 lapply (sem_while … (sem_copy_step src dst sig n is_sep Hneq Hsrc Hdst) … Hloop) //
182 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar -ta
184 [ * #x * * #Hx #Hsep #Houtc % [ %
185 [ #ls #x0 #xs #rs #sep #Hsrctc #Hnosep >Hsrctc in Hx; normalize in ⊢ (%→?);
186 #Hx0 destruct (Hx0) lapply (Hnosep ? (memb_hd …)) >Hsep
187 #Hfalse destruct (Hfalse)
188 | #c #Hc #Hsepc @Houtc ]
190 | * #Hcur #Houtc % [ %
191 [ #ls #x0 #xs #rs #sep #Hsrctc >Hsrctc in Hcur; normalize in ⊢ (%→?);
192 #Hcur destruct (Hcur)
193 | #c #Hc #Hsepc @Houtc ]
196 | #td #te * #c0 * * #Hc0 #Hc0nosep #Hd #Hstar #IH #He
197 lapply (IH He) -IH * * #IH1 #IH2 #IH3 % [ %
198 [ #ls #x #xs #rs #sep #Hsrc_tc #Hnosep #Hsep #ls0 #x0 #target
199 #c #rs0 #Hlen #Hdst_tc
200 >Hsrc_tc in Hc0; normalize in ⊢ (%→?); #Hc0 destruct (Hc0)
201 <(change_vec_same … td src (niltape ?)) in Hd:(???(???(???%??)??));
202 <(change_vec_same … td dst (niltape ?)) in ⊢(???(???(???%??)??)→?);
203 >Hdst_tc >Hsrc_tc >(change_vec_change_vec ?) >change_vec_change_vec
204 >(change_vec_commute ?? td ?? dst src) [|@(sym_not_eq … Hneq)]
205 >change_vec_change_vec @(list_cases2 … Hlen)
206 [ #Hxsnil #Htargetnil #Hd>(IH2 … Hsep)
207 [ >Hd -Hd >Hxsnil >Htargetnil @(eq_vec … (niltape ?))
208 #i #Hi cases (decidable_eq_nat i src) #Hisrc
209 [ >Hisrc >(nth_change_vec_neq … src dst) [|@(sym_not_eq … Hneq)]
210 >nth_change_vec // >nth_change_vec //
211 >(nth_change_vec_neq … src dst) [|@(sym_not_eq … Hneq)]
212 >nth_change_vec // whd in ⊢ (??%?); %
213 | cases (decidable_eq_nat i dst) #Hidst
214 [ >Hidst >nth_change_vec // >nth_change_vec //
215 >nth_change_vec_neq // >Hdst_tc >Htargetnil %
216 | >nth_change_vec_neq [|@(sym_not_eq … Hidst)]
217 >nth_change_vec_neq [|@(sym_not_eq … Hisrc)]
218 >nth_change_vec_neq [|@(sym_not_eq … Hidst)]
219 >nth_change_vec_neq [|@(sym_not_eq … Hisrc)] % ]
221 | >Hd >nth_change_vec_neq [|@(sym_not_eq … Hneq)]
222 >nth_change_vec // >nth_change_vec // >Hxsnil % ]
223 |#hd1 #hd2 #tl1 #tl2 #Hxs #Htarget >Hxs >Htarget #Hd
224 >(IH1 (c0::ls) hd1 tl1 rs sep ?? Hsep (c0::ls0) hd2 tl2 c rs0)
225 [ >Hd >(change_vec_commute … ?? td ?? src dst) //
226 >change_vec_change_vec
227 >(change_vec_commute … ?? td ?? dst src) [|@sym_not_eq //]
228 >change_vec_change_vec
229 >reverse_cons >associative_append >associative_append %
230 | >Hd >nth_change_vec // >nth_change_vec_neq // >Hdst_tc >Htarget //
231 | >Hxs in Hlen; >Htarget normalize #Hlen destruct (Hlen) //
232 | <Hxs #c1 #Hc1 @Hnosep @memb_cons //
233 | >Hd >nth_change_vec_neq [|@sym_not_eq //]
234 >nth_change_vec // >nth_change_vec // ]
236 | #c #Hc #Hsepc >Hc in Hc0; #Hcc0 destruct (Hcc0) >Hc0nosep in Hsepc;
239 | #HNone >HNone in Hc0; #Hc0 destruct (Hc0) ] ]
242 lemma terminate_copy : ∀src,dst,sig,n,is_sep,t.
243 src ≠ dst → src < S n → dst < S n →
244 copy src dst sig n is_sep ↓ t.
245 #src #dst #sig #n #is_sep #t #Hneq #Hsrc #Hdst
246 @(terminate_while … (sem_copy_step …)) //
247 <(change_vec_same … t src (niltape ?))
248 cases (nth src (tape sig) t (niltape ?))
249 [ % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
250 |2,3: #a0 #al0 % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
251 | #ls #c #rs lapply c -c lapply ls -ls lapply t -t elim rs
252 [#t #ls #c % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?);
253 #H1 destruct (H1) #Hxsep >change_vec_change_vec #Ht1 %
254 #t2 * #x0 * * >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
255 >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H)
256 |#r0 #rs0 #IH #t #ls #c % #t1 * #x * * >nth_change_vec //
257 normalize in ⊢ (%→?); #H destruct (H) #Hxsep
258 >change_vec_change_vec >change_vec_commute // #Ht1 >Ht1 @IH
263 lemma sem_copy : ∀src,dst,sig,n,is_sep.
264 src ≠ dst → src < S n → dst < S n →
265 copy src dst sig n is_sep ⊨ R_copy src dst sig n is_sep.
266 #src #dst #sig #n #is_sep #Hneq #Hsrc #Hdst @WRealize_to_Realize /2/