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.
8 \ / This file is distributed under the terms of the
9 \ / GNU General Public License Version 2
10 V_____________________________________________________________*)
12 include "turing/multi_universal/moves.ma".
13 include "turing/if_multi.ma".
14 include "turing/inject.ma".
15 include "turing/basic_machines.ma".
17 definition copy_states ≝ initN 3.
19 definition copy0 : copy_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 3 (refl …)).
20 definition copy1 : copy_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 3 (refl …)).
21 definition copy2 : copy_states ≝ mk_Sig ?? 2 (leb_true_to_le 3 3 (refl …)).
24 definition trans_copy_step ≝
25 λsrc,dst.λsig:FinSet.λn.
26 λp:copy_states × (Vector (option sig) (S n)).
29 [ O ⇒ match nth src ? a (None ?) with
30 [ None ⇒ 〈copy2,null_action sig n〉
31 | Some ai ⇒ match nth dst ? a (None ?) with
32 [ None ⇒ 〈copy2,null_action ? n〉
34 〈copy1,change_vec ? (S n)
35 (change_vec ? (S n) (null_action ? n) (〈None ?,R〉) src)
40 [ O ⇒ (* 1 *) 〈copy1,null_action ? n〉
41 | S _ ⇒ (* 2 *) 〈copy2,null_action ? n〉 ] ].
43 definition copy_step ≝
45 mk_mTM sig n copy_states (trans_copy_step src dst sig n)
46 copy0 (λq.q == copy1 ∨ q == copy2).
48 definition R_copy_step_true ≝
49 λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n).
51 current ? (nth src ? int (niltape ?)) = Some ? x ∧
52 current ? (nth dst ? int (niltape ?)) = Some ? y ∧
55 (tape_move_mono ? (nth src ? int (niltape ?)) 〈None ?, R〉) src)
56 (tape_move_mono ? (nth dst ? int (niltape ?)) 〈Some ? x, R〉) dst.
58 definition R_copy_step_false ≝
59 λsrc,dst:nat.λsig,n.λint,outt: Vector (tape sig) (S n).
60 (current ? (nth src ? int (niltape ?)) = None ? ∨
61 current ? (nth dst ? int (niltape ?)) = None ?) ∧ outt = int.
63 lemma copy_q0_q2_null :
64 ∀src,dst,sig,n,v.src < S n → dst < S n →
65 (nth src ? (current_chars ?? v) (None ?) = None ? ∨
66 nth dst ? (current_chars ?? v) (None ?) = None ?) →
67 step sig n (copy_step src dst sig n) (mk_mconfig ??? copy0 v)
68 = mk_mconfig ??? copy2 v.
69 #src #dst #sig #n #v #Hi #Hj
70 whd in ⊢ (? → ??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (?→??%?);
73 [ whd in ⊢ (??(???%)?); >Hcurrent %
74 | whd in ⊢ (??(????(???%))?); >Hcurrent @tape_move_null_action ]
76 [ whd in ⊢ (??(???%)?); >Hcurrent cases (nth src ?? (None sig)) //
77 | whd in ⊢ (??(????(???%))?); >Hcurrent
78 cases (nth src ?? (None sig)) [|#x] @tape_move_null_action ] ]
82 ∀src,dst,sig,n,v,a,b.src ≠ dst → src < S n → dst < S n →
83 nth src ? (current_chars ?? v) (None ?) = Some ? a →
84 nth dst ? (current_chars ?? v) (None ?) = Some ? b →
85 step sig n (copy_step src dst sig n) (mk_mconfig ??? copy0 v) =
89 (tape_move_mono ? (nth src ? v (niltape ?)) 〈None ?, R〉) src)
90 (tape_move_mono ? (nth dst ? v (niltape ?)) 〈Some ? a, R〉) dst).
91 #src #dst #sig #n #v #a #b #Heq #Hsrc #Hdst #Ha1 #Ha2
92 whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
93 [ whd in match (trans ????);
94 >Ha1 >Ha2 whd in ⊢ (??(???%)?); >(\b ?) //
95 | whd in match (trans ????);
96 >Ha1 >Ha2 whd in ⊢ (??(????(???%))?); >(\b ?) //
97 change with (change_vec ?????) in ⊢ (??(????%)?);
98 <(change_vec_same … v dst (niltape ?)) in ⊢ (??%?);
99 <(change_vec_same … v src (niltape ?)) in ⊢ (??%?);
101 >pmap_change >pmap_change <tape_move_multi_def
102 >tape_move_null_action
103 @eq_f2 // >nth_change_vec_neq //
107 lemma sem_copy_step :
108 ∀src,dst,sig,n.src ≠ dst → src < S n → dst < S n →
109 copy_step src dst sig n ⊨
110 [ copy1: R_copy_step_true src dst sig n,
111 R_copy_step_false src dst sig n ].
112 #src #dst #sig #n #Hneq #Hsrc #Hdst #int
113 lapply (refl ? (current ? (nth src ? int (niltape ?))))
114 cases (current ? (nth src ? int (niltape ?))) in ⊢ (???%→?);
117 [ whd in ⊢ (??%?); >copy_q0_q2_null /2/
118 | normalize in ⊢ (%→?); #H destruct (H) ]
120 | #a #Ha lapply (refl ? (current ? (nth dst ? int (niltape ?))))
121 cases (current ? (nth dst ? int (niltape ?))) in ⊢ (???%→?);
124 [ whd in ⊢ (??%?); >copy_q0_q2_null /2/
125 | normalize in ⊢ (%→?); #H destruct (H) ]
126 | #_ % // %2 >Hcur_dst % ] ]
129 [whd in ⊢ (??%?); >(copy_q0_q1 … a b Hneq Hsrc Hdst) //
130 | #_ %{a} %{b} % // % //]
131 | * #H @False_ind @H %
138 definition copy ≝ λsrc,dst,sig,n.
139 whileTM … (copy_step src dst sig n) copy1.
142 λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n).
143 ((current ? (nth src ? int (niltape ?)) = None ? ∨
144 current ? (nth dst ? int (niltape ?)) = None ?) → outt = int) ∧
145 (∀ls,x,x0,rs,ls0,rs0.
146 nth src ? int (niltape ?) = midtape sig ls x rs →
147 nth dst ? int (niltape ?) = midtape sig ls0 x0 rs0 →
148 (∃rs01,rs02.rs0 = rs01@rs02 ∧ |rs01| = |rs| ∧
151 (mk_tape sig (reverse sig rs@x::ls) (None sig) []) src)
152 (mk_tape sig (reverse sig rs@x::ls0) (option_hd sig rs02)
153 (tail sig rs02)) dst) ∨
154 (∃rs1,rs2.rs = rs1@rs2 ∧ |rs1| = |rs0| ∧
157 (mk_tape sig (reverse sig rs1@x::ls) (option_hd sig rs2)
159 (mk_tape sig (reverse sig rs1@x::ls0) (None sig) []) dst)).
161 lemma wsem_copy : ∀src,dst,sig,n.src ≠ dst → src < S n → dst < S n →
162 copy src dst sig n ⊫ R_copy src dst sig n.
163 #src #dst #sig #n #Hneq #Hsrc #Hdst #ta #k #outc #Hloop
164 lapply (sem_while … (sem_copy_step src dst sig n Hneq Hsrc Hdst) … Hloop) //
165 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
166 [ whd in ⊢ (%→?); * #Hnone #Hout %
168 |#ls #x #x0 #rs #ls0 #rs0 #Hsrc1 #Hdst1 @False_ind cases Hnone
169 [>Hsrc1 normalize #H destruct (H) | >Hdst1 normalize #H destruct (H)]
171 |#tc #td * #x * #y * * #Hcx #Hcy #Htd #Hstar #IH #He lapply (IH He) -IH *
173 [* [>Hcx #H destruct (H) | >Hcy #H destruct (H)]
174 |#ls #x' #y' #rs #ls0 #rs0 #Hnth_src #Hnth_dst
175 >Hnth_src in Hcx; whd in ⊢ (??%?→?); #H destruct (H)
176 >Hnth_dst in Hcy; whd in ⊢ (??%?→?); #H destruct (H)
177 >Hnth_src in Htd; >Hnth_dst -Hnth_src -Hnth_dst
179 [(* the source tape is empty after the move *)
181 [%1 >Htd >nth_change_vec_neq [2:@(not_to_not … Hneq) //] >nth_change_vec //]
182 #Hout (* whd in match (tape_move ???); *) %1 %{([])} %{rs0} %
184 |whd in match (reverse ??); whd in match (reverse ??);
185 >Hout >Htd @eq_f2 // cases rs0 //
188 [(* the dst tape is empty after the move *)
189 #Htd lapply (IH1 ?) [%2 >Htd >nth_change_vec //]
190 #Hout (* whd in match (tape_move ???); *) %2 %{[ ]} %{(c1::tl1)} %
192 |whd in match (reverse ??); whd in match (reverse ??);
195 |#c2 #tl2 whd in match (tape_move_mono ???); whd in match (tape_move_mono ???);
197 cut (nth src (tape sig) td (niltape sig)=midtape sig (x::ls) c1 tl1)
198 [>Htd >nth_change_vec_neq [2:@(not_to_not … Hneq) //] @nth_change_vec //]
200 cut (nth dst (tape sig) td (niltape sig)=midtape sig (x::ls0) c2 tl2)
201 [>Htd @nth_change_vec //]
202 #Hdst_td cases (IH2 … Hsrc_td Hdst_td) -Hsrc_td -Hdst_td
203 [* #rs01 * #rs02 * * #H1 #H2 #H3 %1
204 %{(c2::rs01)} %{rs02} % [% [@eq_f //|normalize @eq_f @H2]]
205 >Htd in H3; >change_vec_commute // >change_vec_change_vec
206 >change_vec_commute [2:@(not_to_not … Hneq) //] >change_vec_change_vec
207 #H >reverse_cons >associative_append >associative_append @H
208 |* #rs11 * #rs12 * * #H1 #H2 #H3 %2
209 %{(c1::rs11)} %{rs12} % [% [@eq_f //|normalize @eq_f @H2]]
210 >Htd in H3; >change_vec_commute // >change_vec_change_vec
211 >change_vec_commute [2:@(not_to_not … Hneq) //] >change_vec_change_vec
212 #H >reverse_cons >associative_append >associative_append @H
220 lemma terminate_copy : ∀src,dst,sig,n,t.
221 src ≠ dst → src < S n → dst < S n → copy src dst sig n ↓ t.
222 #src #dst #sig #n #t #Hneq #Hsrc #Hdts
223 @(terminate_while … (sem_copy_step …)) //
224 <(change_vec_same … t src (niltape ?))
225 cases (nth src (tape sig) t (niltape ?))
226 [ % #t1 * #x * #y * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
227 |2,3: #a0 #al0 % #t1 * #x * #y * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
228 | #ls #c #rs lapply c -c lapply ls -ls lapply t -t elim rs
229 [#t #ls #c % #t1 * #x * #y * * >nth_change_vec // normalize in ⊢ (%→?);
230 #H1 destruct (H1) #_ >change_vec_change_vec #Ht1 %
231 #t2 * #x0 * #y0 * * >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
232 >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H)
233 |#r0 #rs0 #IH #t #ls #c % #t1 * #x * #y * * >nth_change_vec //
234 normalize in ⊢ (%→?); #H destruct (H) #Hcur
235 >change_vec_change_vec >change_vec_commute // #Ht1 >Ht1 @IH
240 lemma sem_copy : ∀src,dst,sig,n.
241 src ≠ dst → src < S n → dst < S n →
242 copy src dst sig n ⊨ R_copy src dst sig n.
243 #i #j #sig #n #Hneq #Hi #Hj @WRealize_to_Realize [/2/| @wsem_copy // ]