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 ? (nth src ? int (niltape ?)) (Some ? 〈x1,R〉)) src)
69 (tape_move ? (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 ? t (Some ? 〈s,R〉)) src)
119 (tape_move ? (nth dst ? v (niltape ?)) (Some ? 〈s,R〉)) dst).
120 #src #dst #sig #n #is_sep #v #t #Hneq #Hsrc #Hdst #s #Hcurrent #Hsep
121 whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
122 [ >current_chars_change_vec // whd in match (trans ????);
123 >nth_change_vec // >Hcurrent whd in ⊢ (??(???%)?); >Hsep %
124 | >current_chars_change_vec // whd in match (trans ????);
125 >nth_change_vec // >Hcurrent whd in ⊢ (??(???????(???%))?);
126 >Hsep whd in ⊢ (??(???????(???%))?); >change_vec_commute // >pmap_change
127 >change_vec_commute // @eq_f3 //
128 <(change_vec_same ?? v dst (niltape ?)) in ⊢(??%?);
129 >pmap_change @eq_f3 //
133 lemma sem_copy_step :
134 ∀src,dst,sig,n,is_sep.src ≠ dst → src < S n → dst < S n →
135 copy_step src dst sig n is_sep ⊨
136 [ copy1: R_copy_step_true src dst sig n is_sep,
137 R_copy_step_false src dst sig n is_sep ].
138 #src #dst #sig #n #is_sep #Hneq #Hsrc #Hdst #int
139 lapply (refl ? (current ? (nth src ? int (niltape ?))))
140 cases (current ? (nth src ? int (niltape ?))) in ⊢ (???%→?);
141 [ #Hcur <(change_vec_same … int src (niltape ?)) %{2} %
143 [ whd in ⊢ (??%?); >copy_q0_q2_null /2/
144 | normalize in ⊢ (%→?); #H destruct (H) ]
145 | #_ %2 >nth_change_vec >Hcur // % // ] ]
146 | #c #Hcur cases (true_or_false (is_sep c)) #Hsep
147 [ <(change_vec_same … int src (niltape ?)) %{2} %
149 [ whd in ⊢ (??%?); >copy_q0_q2_sep /2/
150 | normalize in ⊢ (%→?); #H destruct (H) ]
151 | #_ % >nth_change_vec // %{c} % [ % /2/ | // ] ] ]
154 <(change_vec_same … int src (niltape ?)) in ⊢ (??%?);
155 >Hcur in ⊢ (??%?); whd in ⊢ (??%?); >(copy_q0_q1 … Hsep) /2/
156 | #_ whd %{c} % % /2/ ]
157 | * #Hfalse @False_ind /2/ ] ] ] ]
160 definition copy ≝ λsrc,dst,sig,n,is_sep.
161 whileTM … (copy_step src dst sig n is_sep) copy1.
164 λsrc,dst,sig,n,is_sep.λint,outt: Vector (tape sig) (S n).
166 nth src ? int (niltape ?) = midtape sig ls x (xs@sep::rs) →
167 (∀c.memb ? c (x::xs) = true → is_sep c = false) → is_sep sep = true →
168 ∀ls0,x0,target,c,rs0.|xs| = |target| →
169 nth dst ? int (niltape ?) = midtape sig ls0 x0 (target@c::rs0) →
171 (change_vec ?? int (midtape sig (reverse ? xs@x::ls) sep rs) src)
172 (midtape sig (reverse ? xs@x::ls0) c rs0) dst) ∧
173 (∀c.current ? (nth src ? int (niltape ?)) = Some ? c → is_sep c = true →
175 (current ? (nth src ? int (niltape ?)) = None ? → outt = int).
177 lemma wsem_copy : ∀src,dst,sig,n,is_sep.src ≠ dst → src < S n → dst < S n →
178 copy src dst sig n is_sep ⊫ R_copy src dst sig n is_sep.
179 #src #dst #sig #n #is_sep #Hneq #Hsrc #Hdst #ta #k #outc #Hloop
180 lapply (sem_while … (sem_copy_step src dst sig n is_sep Hneq Hsrc Hdst) … Hloop) //
181 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
182 [ #tc whd in ⊢ (%→?); *
183 [ * #x * * #Hx #Hsep #Houtc % [ %
184 [ #ls #x0 #xs #rs #sep #Hsrctc #Hnosep >Hsrctc in Hx; normalize in ⊢ (%→?);
185 #Hx0 destruct (Hx0) lapply (Hnosep ? (memb_hd …)) >Hsep
186 #Hfalse destruct (Hfalse)
187 | #c #Hc #Hsepc @Houtc ]
189 | * #Hcur #Houtc % [ %
190 [ #ls #x0 #xs #rs #sep #Hsrctc >Hsrctc in Hcur; normalize in ⊢ (%→?);
191 #Hcur destruct (Hcur)
192 | #c #Hc #Hsepc @Houtc ]
195 | #tc #td #te * #c0 * * #Hc0 #Hc0nosep #Hd #Hstar #IH #He
196 lapply (IH He) -IH * * #IH1 #IH2 #IH3 % [ %
197 [ #ls #x #xs #rs #sep #Hsrc_tc #Hnosep #Hsep #ls0 #x0 #target
198 #c #rs0 #Hlen #Hdst_tc
199 >Hsrc_tc in Hc0; normalize in ⊢ (%→?); #Hc0 destruct (Hc0)
200 <(change_vec_same … tc src (niltape ?)) in Hd:(???(???(???%??)??));
201 <(change_vec_same … tc dst (niltape ?)) in ⊢(???(???(???%??)??)→?);
202 >Hdst_tc >Hsrc_tc >change_vec_change_vec >change_vec_change_vec
203 >(change_vec_commute ?? tc ?? dst src) [|@(sym_not_eq … Hneq)]
204 >change_vec_change_vec @(list_cases2 … Hlen)
205 [ #Hxsnil #Htargetnil #Hd>(IH2 … Hsep)
206 [ >Hd -Hd >Hxsnil >Htargetnil @(eq_vec … (niltape ?))
207 #i #Hi cases (decidable_eq_nat i src) #Hisrc
208 [ >Hisrc >(nth_change_vec_neq … src dst) [|@(sym_not_eq … Hneq)]
209 >nth_change_vec // >nth_change_vec //
210 >(nth_change_vec_neq … src dst) [|@(sym_not_eq … Hneq)]
211 >nth_change_vec // whd in ⊢ (??%?); %
212 | cases (decidable_eq_nat i dst) #Hidst
213 [ >Hidst >nth_change_vec // >nth_change_vec //
214 >nth_change_vec_neq // >Hdst_tc >Htargetnil %
215 | >nth_change_vec_neq [|@(sym_not_eq … Hidst)]
216 >nth_change_vec_neq [|@(sym_not_eq … Hisrc)]
217 >nth_change_vec_neq [|@(sym_not_eq … Hidst)]
218 >nth_change_vec_neq [|@(sym_not_eq … Hisrc)] % ]
220 | >Hd >nth_change_vec_neq [|@(sym_not_eq … Hneq)]
221 >nth_change_vec // >nth_change_vec // >Hxsnil % ]
222 |#hd1 #hd2 #tl1 #tl2 #Hxs #Htarget >Hxs >Htarget #Hd
223 >(IH1 (c0::ls) hd1 tl1 rs sep ?? Hsep (c0::ls0) hd2 tl2 c rs0)
224 [ >Hd >(change_vec_commute … ?? tc ?? src dst) //
225 >change_vec_change_vec
226 >(change_vec_commute … ?? tc ?? dst src) [|@sym_not_eq //]
227 >change_vec_change_vec
228 >reverse_cons >associative_append >associative_append %
229 | >Hd >nth_change_vec // >nth_change_vec_neq // >Hdst_tc >Htarget //
230 | >Hxs in Hlen; >Htarget normalize #Hlen destruct (Hlen) //
231 | <Hxs #c1 #Hc1 @Hnosep @memb_cons //
232 | >Hd >nth_change_vec_neq [|@sym_not_eq //]
233 >nth_change_vec // >nth_change_vec // ]
235 | #c #Hc #Hsepc >Hc in Hc0; #Hcc0 destruct (Hcc0) >Hc0nosep in Hsepc;
238 | #HNone >HNone in Hc0; #Hc0 destruct (Hc0) ] ]
241 lemma terminate_copy : ∀src,dst,sig,n,is_sep,t.
242 src ≠ dst → src < S n → dst < S n →
243 copy src dst sig n is_sep ↓ t.
244 #src #dst #sig #n #is_sep #t #Hneq #Hsrc #Hdst
245 @(terminate_while … (sem_copy_step …)) //
246 <(change_vec_same … t src (niltape ?))
247 cases (nth src (tape sig) t (niltape ?))
248 [ % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
249 |2,3: #a0 #al0 % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
250 | #ls #c #rs lapply c -c lapply ls -ls lapply t -t elim rs
251 [#t #ls #c % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?);
252 #H1 destruct (H1) #Hxsep >change_vec_change_vec #Ht1 %
253 #t2 * #x0 * * >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
254 >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H)
255 |#r0 #rs0 #IH #t #ls #c % #t1 * #x * * >nth_change_vec //
256 normalize in ⊢ (%→?); #H destruct (H) #Hxsep
257 >change_vec_change_vec >change_vec_commute // #Ht1 >Ht1 @IH
262 lemma sem_copy : ∀src,dst,sig,n,is_sep.
263 src ≠ dst → src < S n → dst < S n →
264 copy src dst sig n is_sep ⊨ R_copy src dst sig n is_sep.
265 #src #dst #sig #n #is_sep #Hneq #Hsrc #Hdst @WRealize_to_Realize /2/