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/turing.ma".
13 include "turing/inject.ma".
14 include "turing/while_multi.ma".
15 include "turing/while_machine.ma".
17 definition parmove_states ≝ initN 3.
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 …)).
25 src: a b c ... z ---→ a b c ... z
28 dst: _ _ _ ... _ ---→ a b c ... z
31 0) (x,_) → (x,_)(R,R) → 1
38 definition trans_parmove_step ≝
40 λp:parmove_states × (Vector (option sig) (S n)).
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)
49 (null_action ? n) (〈None ?,D〉) src)
52 [ O ⇒ (* 1 *) 〈parmove1,null_action ? n〉
53 | S _ ⇒ (* 2 *) 〈parmove2,null_action ? n〉 ] ].
55 definition parmove_step ≝
57 mk_mTM sig n parmove_states (trans_parmove_step src dst sig n D)
58 parmove0 (λq.q == parmove1 ∨ q == parmove2).
60 definition R_parmove_step_true ≝
61 λsrc,dst,sig,n,D.λint,outt: Vector (tape sig) (S n).
63 current ? (nth src ? int (niltape ?)) = Some ? x1 ∧
64 current ? (nth dst ? int (niltape ?)) = Some ? x2 ∧
67 (tape_move ? (nth src ? int (niltape ?)) D) src)
68 (tape_move ? (nth dst ? int (niltape ?)) D) dst.
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 ?) ∧
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 ⊢ (??%?);
85 [ whd in ⊢ (??(???%)?); >Hcurrent %
86 | whd in ⊢ (??(????(???%))?); >Hcurrent @tape_move_null_action ]
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 ⊢ (??%?);
99 [ whd in ⊢ (??(???%)?); >Hcursrc whd in ⊢ (??(???%)?); >Hcurdst %
100 | whd in ⊢ (??(????(???%))?); >Hcursrc
101 whd in ⊢ (??(????(???%))?); >Hcurdst @tape_move_null_action ]
104 lemma parmove_q0_q1 :
105 ∀src,dst,sig,n,D,v.src ≠ dst → src < S n → dst < S n →
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
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 ????);
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 //
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 ⊢ (???%→?);
140 [ whd in ⊢ (??%?); >parmove_q0_q2_null_src /2/
141 | normalize in ⊢ (%→?); #H destruct (H) ]
143 | #a #Ha lapply (refl ? (current ? (nth dst ? int (niltape ?))))
144 cases (current ? (nth dst ? int (niltape ?))) in ⊢ (???%→?);
147 [ whd in ⊢ (??%?); >(parmove_q0_q2_null_dst …) /2/
148 | normalize in ⊢ (%→?); #H destruct (H) ]
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 ?)) //
156 | #_ %{a} %{b} % // % // ]
157 | * #H @False_ind @H % ]
161 definition parmove ≝ λsrc,dst,sig,n,D.
162 whileTM … (parmove_step src dst sig n D) parmove1.
164 definition R_parmoveL ≝
165 λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n).
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 →
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 ?) →
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)
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
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)]
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)]
201 >(nth_change_vec_neq … src dst) [|@(sym_not_eq … Hneq)]
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)] %
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 //]
226 | >Hc0 >Hc1 * [ #Hc0 destruct (Hc0) | #Hc1 destruct (Hc1) ]
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
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 //]
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 …)).
270 definition mte_step ≝
271 λalpha:FinSet.λD.mk_TM alpha mte_states
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).
280 definition R_mte_step_true ≝ λalpha,D,t1,t2.
282 t1 = midtape alpha ls c rs ∧ t2 = tape_move ? t1 D.
284 definition R_mte_step_false ≝ λalpha.λt1,t2:tape alpha.
285 current ? t1 = None ? ∧ t1 = t2.
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
291 [| % [ % [ % | normalize #H destruct ] | #_ % // ] ]
293 [| % [ % [ % | normalize #H destruct ] | #_ % // ] ]
295 [| % [ % [ % | normalize #H destruct ] | #_ % // ] ]
297 @ex_intro [| % [ % [ % | #_ %{ls} %{c} %{rs} % // ]
298 | normalize in ⊢ (?(??%?)→?); * #H @False_ind /2/ ] ] ]
301 definition move_to_end ≝ λsig,D.whileTM sig (mte_step sig D) mte2.
303 definition R_move_to_end_r ≝
305 (current ? int = None ? → outt = int) ∧
306 ∀ls,c,rs.int = midtape sig ls c rs → outt = mk_tape ? (reverse ? rs@c::ls) (None ?) [ ].
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 % ] ] ]
324 lemma terminate_move_to_end_r : ∀sig,t.move_to_end sig R ↓ t.
325 #sig #t @(terminate_while … (sem_mte_step sig R …)) //
327 [ % #t1 * #ls * #c * #rs * #H destruct
328 |2,3: #a0 #al0 % #t1 * #ls * #c * #rs * #H destruct
329 | #ls #c #rs lapply c -c lapply ls -ls elim rs
330 [ #ls #c % #t1 * #ls0 * #c0 * #rs0 * #Hmid #Ht1 destruct %
331 #t2 * #ls1 * #c1 * #rs1 * normalize in ⊢ (%→?); #H destruct
332 | #r0 #rs0 #IH #ls #c % #t1 * #ls1 * #c1 * #rs1 * #Hmid #Ht1 destruct @IH
337 lemma sem_move_to_end_r : ∀sig. move_to_end sig R ⊨ R_move_to_end_r sig.
338 #sig @WRealize_to_Realize //
341 definition R_move_to_end_l ≝
343 (current ? int = None ? → outt = int) ∧
344 ∀ls,c,rs.int = midtape sig ls c rs → outt = mk_tape ? [ ] (None ?) (reverse ? ls@c::rs).
346 lemma wsem_move_to_end_l : ∀sig. move_to_end sig L ⊫ R_move_to_end_l sig.
347 #sig #ta #k #outc #Hloop
348 lapply (sem_while … (sem_mte_step sig L) … Hloop) //
349 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
350 [ * #Hcurtb #Houtc % /2/ #ls #c #rs #Htb >Htb in Hcurtb; normalize in ⊢ (%→?); #H destruct (H)
351 | #tc #td * #ls * #c * #rs * #Htc >Htc cases ls
352 [ normalize in ⊢ (%→?); #Htd >Htd #Hstar #IH whd in ⊢ (%→?); #Hfalse
353 lapply (IH Hfalse) -IH * #Htd1 #_ %
354 [ normalize in ⊢ (%→?); #H destruct (H)
355 | #ls0 #c0 #rs0 #H destruct (H) >Htd1 // ]
356 | #l0 #ls0 whd in ⊢ (???%→?); #Htd >Htd #Hstar #IH whd in ⊢ (%→?); #Hfalse
357 lapply (IH Hfalse) -IH * #_ #IH %
358 [ normalize in ⊢ (%→?); #H destruct (H)
359 | #ls1 #c1 #rs1 #H destruct (H) >reverse_cons >associative_append @IH % ] ] ]
362 lemma terminate_move_to_end_l : ∀sig,t.move_to_end sig L ↓ t.
363 #sig #t @(terminate_while … (sem_mte_step sig L …)) //
365 [ % #t1 * #ls * #c * #rs * #H destruct
366 |2,3: #a0 #al0 % #t1 * #ls * #c * #rs * #H destruct
368 [ #c #rs % #t1 * #ls0 * #c0 * #rs0 * #Hmid #Ht1 destruct %
369 #t2 * #ls1 * #c1 * #rs1 * normalize in ⊢ (%→?); #H destruct
370 | #l0 #ls0 #IH #c #rs % #t1 * #ls1 * #c1 * #rs1 * #Hmid #Ht1 destruct @IH
375 lemma sem_move_to_end_l : ∀sig. move_to_end sig L ⊨ R_move_to_end_l sig.
376 #sig @WRealize_to_Realize //