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) ∧
174 nth dst ? int (niltape ?) = midtape sig xs x rs →
175 ∀ls0,x0,target,rs0.|xs| = |target| →
176 nth src ? int (niltape ?) = midtape sig (target@ls0) x0 rs0 →
178 (change_vec ?? int (mk_tape sig [] (None ?) (reverse ? xs@x::rs)) dst)
179 (mk_tape sig (tail ? ls0) (option_hd ? ls0) (reverse ? target@x0::rs0)) src) ∧
180 ((current ? (nth src ? int (niltape ?)) = None ? ∨
181 current ? (nth dst ? int (niltape ?)) = None ?) →
184 lemma wsem_parmoveL : ∀src,dst,sig,n.src ≠ dst → src < S n → dst < S n →
185 parmove src dst sig n L ⊫ R_parmoveL src dst sig n.
186 #src #dst #sig #n #Hneq #Hsrc #Hdst #ta #k #outc #Hloop
187 lapply (sem_while … (sem_parmove_step src dst sig n L Hneq Hsrc Hdst) … Hloop) //
188 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
189 [ whd in ⊢ (%→?); * #H #Houtc % [2: #_ @Houtc ] cases H #Hcurtb
191 [ #x #xs #rs #Hsrctb >Hsrctb in Hcurtb; normalize in ⊢ (%→?);
192 #Hfalse destruct (Hfalse)
193 | #x #xs #rs #Hdsttb #ls0 #x0 #target #rs0 #Hlen #Hsrctb >Hsrctb in Hcurtb;
194 normalize in ⊢ (%→?); #H destruct (H)
197 [ #x #xs #rs #Hsrctb #ls0 #x0 #target
198 #rs0 #Hlen #Hdsttb >Hdsttb in Hcurtb; normalize in ⊢ (%→?); #H destruct (H)
199 | #x #xs #rs #Hdsttb >Hdsttb in Hcurtb; normalize in ⊢ (%→?);
200 #Hfalse destruct (Hfalse)
203 | #td #te * #c0 * #c1 * * #Hc0 #Hc1 #Hd #Hstar #IH #He
204 lapply (IH He) -IH * * #IH1a #IH1b #IH2 % [ %
205 [ #x #xs #rs #Hsrc_td #ls0 #x0 #target
207 >Hsrc_td in Hc0; normalize in ⊢ (%→?); #Hc0 destruct (Hc0)
208 >Hdst_td in Hd; >Hsrc_td @(list_cases2 … Hlen)
209 [ #Hxsnil #Htargetnil >Hxsnil >Htargetnil #Hd >IH2
210 [2: %1 >Hd >nth_change_vec_neq [|@(sym_not_eq … Hneq)]
212 >Hd -Hd @(eq_vec … (niltape ?))
213 #i #Hi cases (decidable_eq_nat i src) #Hisrc
214 [ >Hisrc >(nth_change_vec_neq … src dst) [|@(sym_not_eq … Hneq)]
216 >(nth_change_vec_neq … src dst) [|@(sym_not_eq … Hneq)]
218 | cases (decidable_eq_nat i dst) #Hidst
219 [ >Hidst >nth_change_vec // >nth_change_vec //
220 >Hdst_td in Hc1; >Htargetnil
221 normalize in ⊢ (%→?); #Hc1 destruct (Hc1) cases ls0 //
222 | >nth_change_vec_neq [|@(sym_not_eq … Hidst)]
223 >nth_change_vec_neq [|@(sym_not_eq … Hisrc)]
224 >nth_change_vec_neq [|@(sym_not_eq … Hidst)]
225 >nth_change_vec_neq [|@(sym_not_eq … Hisrc)] %
228 | #hd1 #hd2 #tl1 #tl2 #Hxs #Htarget >Hxs >Htarget #Hd
229 >(IH1a hd1 tl1 (c0::rs) ? ls0 hd2 tl2 (x0::rs0))
230 [ >Hd >(change_vec_commute … ?? td ?? src dst) //
231 >change_vec_change_vec
232 >(change_vec_commute … ?? td ?? dst src) [|@sym_not_eq //]
233 >change_vec_change_vec
234 >reverse_cons >associative_append
235 >reverse_cons >associative_append %
236 | >Hd >nth_change_vec //
237 | >Hxs in Hlen; >Htarget normalize #Hlen destruct (Hlen) //
238 | >Hd >nth_change_vec_neq [|@sym_not_eq //]
241 | #x #xs #rs #Hdst_td #ls0 #x0 #target
243 >Hdst_td in Hc0; normalize in ⊢ (%→?); #Hc0 destruct (Hc0)
244 >Hsrc_td in Hd; >Hdst_td @(list_cases2 … Hlen)
245 [ #Hxsnil #Htargetnil >Hxsnil >Htargetnil #Hd >IH2
246 [2: %2 >Hd >nth_change_vec //]
247 >Hd -Hd @(eq_vec … (niltape ?))
248 #i #Hi cases (decidable_eq_nat i dst) #Hidst
249 [ >Hidst >(nth_change_vec_neq … dst src) //
250 >nth_change_vec // >nth_change_vec //
251 | cases (decidable_eq_nat i src) #Hisrc
252 [ >Hisrc >nth_change_vec // >(nth_change_vec_neq …) [|@sym_not_eq //]
253 >Hsrc_td in Hc1; >Htargetnil
254 normalize in ⊢ (%→?); #Hc1 destruct (Hc1) >nth_change_vec //
256 | >nth_change_vec_neq [|@(sym_not_eq … Hidst)]
257 >nth_change_vec_neq [|@(sym_not_eq … Hisrc)]
258 >nth_change_vec_neq [|@(sym_not_eq … Hisrc)]
259 >nth_change_vec_neq [|@(sym_not_eq … Hidst)] %
262 | #hd1 #hd2 #tl1 #tl2 #Hxs #Htarget >Hxs >Htarget #Hd
263 >(IH1b hd1 tl1 (x::rs) ? ls0 hd2 tl2 (x0::rs0))
264 [ >Hd >(change_vec_commute … ?? td ?? dst src) [|@sym_not_eq //]
265 >change_vec_change_vec
266 >(change_vec_commute … ?? td ?? src dst) //
267 >change_vec_change_vec
268 >reverse_cons >associative_append
269 >reverse_cons >associative_append
270 >change_vec_commute [|@sym_not_eq //] %
271 | >Hd >nth_change_vec_neq [|@sym_not_eq //] >nth_change_vec //
272 | >Hxs in Hlen; >Htarget normalize #Hlen destruct (Hlen) //
273 | >Hd >nth_change_vec // ]
276 | >Hc0 >Hc1 * [ #Hc0 destruct (Hc0) | #Hc1 destruct (Hc1) ]
280 lemma terminate_parmoveL : ∀src,dst,sig,n,t.
281 src ≠ dst → src < S n → dst < S n →
282 parmove src dst sig n L ↓ t.
283 #src #dst #sig #n #t #Hneq #Hsrc #Hdst
284 @(terminate_while … (sem_parmove_step …)) //
285 <(change_vec_same … t src (niltape ?))
286 cases (nth src (tape sig) t (niltape ?))
287 [ % #t1 * #x1 * #x2 * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
288 |2,3: #a0 #al0 % #t1 * #x1 * #x2 * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
289 | #ls lapply t -t elim ls
290 [#t #c #rs % #t1 * #x1 * #x2 * * >nth_change_vec // normalize in ⊢ (%→?);
291 #H1 destruct (H1) #Hcurdst >change_vec_change_vec #Ht1 %
292 #t2 * #y1 * #y2 * * >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
293 >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H)
294 |#l0 #ls0 #IH #t #c #rs % #t1 * #x1 * #x2 * * >nth_change_vec //
295 normalize in ⊢ (%→?); #H destruct (H) #Hcurdst
296 >change_vec_change_vec >change_vec_commute // #Ht1 >Ht1 @IH
301 lemma sem_parmoveL : ∀src,dst,sig,n.
302 src ≠ dst → src < S n → dst < S n →
303 parmove src dst sig n L ⊨ R_parmoveL src dst sig n.
304 #src #dst #sig #n #Hneq #Hsrc #Hdst @WRealize_to_Realize
305 [/2/ | @wsem_parmoveL //]
315 definition mte_states ≝ initN 3.
316 definition mte0 : mte_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 3 (refl …)).
317 definition mte1 : mte_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 3 (refl …)).
318 definition mte2 : mte_states ≝ mk_Sig ?? 2 (leb_true_to_le 3 3 (refl …)).
320 definition mte_step ≝
321 λalpha:FinSet.λD.mk_TM alpha mte_states
324 [ None ⇒ 〈mte1,None ?,N〉
325 | Some a' ⇒ match (pi1 … q) with
326 [ O ⇒ 〈mte2,Some ? a',D〉
327 | S q ⇒ 〈mte2,None ?,N〉 ] ])
328 mte0 (λq.q == mte1 ∨ q == mte2).
330 definition R_mte_step_true ≝ λalpha,D,t1,t2.
332 t1 = midtape alpha ls c rs ∧ t2 = tape_move ? t1 D.
334 definition R_mte_step_false ≝ λalpha.λt1,t2:tape alpha.
335 current ? t1 = None ? ∧ t1 = t2.
338 ∀alpha,D.mte_step alpha D ⊨ [ mte2 : R_mte_step_true alpha D, R_mte_step_false alpha ] .
339 #alpha #D #intape @(ex_intro ?? 2) cases intape
341 [| % [ % [ % | normalize #H destruct ] | #_ % // ] ]
343 [| % [ % [ % | normalize #H destruct ] | #_ % // ] ]
345 [| % [ % [ % | normalize #H destruct ] | #_ % // ] ]
347 @ex_intro [| % [ % [ % | #_ %{ls} %{c} %{rs} % // ]
348 | normalize in ⊢ (?(??%?)→?); * #H @False_ind /2/ ] ] ]
351 definition move_to_end ≝ λsig,D.whileTM sig (mte_step sig D) mte2.
353 definition R_move_to_end_r ≝
355 (current ? int = None ? → outt = int) ∧
356 ∀ls,c,rs.int = midtape sig ls c rs → outt = mk_tape ? (reverse ? rs@c::ls) (None ?) [ ].
358 lemma wsem_move_to_end_r : ∀sig. move_to_end sig R ⊫ R_move_to_end_r sig.
359 #sig #ta #k #outc #Hloop
360 lapply (sem_while … (sem_mte_step sig R) … Hloop) //
361 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
362 [ * #Hcurtb #Houtc % /2/ #ls #c #rs #Htb >Htb in Hcurtb; normalize in ⊢ (%→?); #H destruct (H)
363 | #tc #td * #ls * #c * #rs * #Htc >Htc cases rs
364 [ normalize in ⊢ (%→?); #Htd >Htd #Hstar #IH whd in ⊢ (%→?); #Hfalse
365 lapply (IH Hfalse) -IH * #Htd1 #_ %
366 [ normalize in ⊢ (%→?); #H destruct (H)
367 | #ls0 #c0 #rs0 #H destruct (H) >Htd1 // ]
368 | #r0 #rs0 whd in ⊢ (???%→?); #Htd >Htd #Hstar #IH whd in ⊢ (%→?); #Hfalse
369 lapply (IH Hfalse) -IH * #_ #IH %
370 [ normalize in ⊢ (%→?); #H destruct (H)
371 | #ls1 #c1 #rs1 #H destruct (H) >reverse_cons >associative_append @IH % ] ] ]
374 lemma terminate_move_to_end_r : ∀sig,t.move_to_end sig R ↓ t.
375 #sig #t @(terminate_while … (sem_mte_step sig R …)) //
377 [ % #t1 * #ls * #c * #rs * #H destruct
378 |2,3: #a0 #al0 % #t1 * #ls * #c * #rs * #H destruct
379 | #ls #c #rs lapply c -c lapply ls -ls elim rs
380 [ #ls #c % #t1 * #ls0 * #c0 * #rs0 * #Hmid #Ht1 destruct %
381 #t2 * #ls1 * #c1 * #rs1 * normalize in ⊢ (%→?); #H destruct
382 | #r0 #rs0 #IH #ls #c % #t1 * #ls1 * #c1 * #rs1 * #Hmid #Ht1 destruct @IH
387 lemma sem_move_to_end_r : ∀sig. move_to_end sig R ⊨ R_move_to_end_r sig.
388 #sig @WRealize_to_Realize //
391 definition R_move_to_end_l ≝
393 (current ? int = None ? → outt = int) ∧
394 ∀ls,c,rs.int = midtape sig ls c rs → outt = mk_tape ? [ ] (None ?) (reverse ? ls@c::rs).
396 lemma wsem_move_to_end_l : ∀sig. move_to_end sig L ⊫ R_move_to_end_l sig.
397 #sig #ta #k #outc #Hloop
398 lapply (sem_while … (sem_mte_step sig L) … Hloop) //
399 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
400 [ * #Hcurtb #Houtc % /2/ #ls #c #rs #Htb >Htb in Hcurtb; normalize in ⊢ (%→?); #H destruct (H)
401 | #tc #td * #ls * #c * #rs * #Htc >Htc cases ls
402 [ normalize in ⊢ (%→?); #Htd >Htd #Hstar #IH whd in ⊢ (%→?); #Hfalse
403 lapply (IH Hfalse) -IH * #Htd1 #_ %
404 [ normalize in ⊢ (%→?); #H destruct (H)
405 | #ls0 #c0 #rs0 #H destruct (H) >Htd1 // ]
406 | #l0 #ls0 whd in ⊢ (???%→?); #Htd >Htd #Hstar #IH whd in ⊢ (%→?); #Hfalse
407 lapply (IH Hfalse) -IH * #_ #IH %
408 [ normalize in ⊢ (%→?); #H destruct (H)
409 | #ls1 #c1 #rs1 #H destruct (H) >reverse_cons >associative_append @IH % ] ] ]
412 lemma terminate_move_to_end_l : ∀sig,t.move_to_end sig L ↓ t.
413 #sig #t @(terminate_while … (sem_mte_step sig L …)) //
415 [ % #t1 * #ls * #c * #rs * #H destruct
416 |2,3: #a0 #al0 % #t1 * #ls * #c * #rs * #H destruct
418 [ #c #rs % #t1 * #ls0 * #c0 * #rs0 * #Hmid #Ht1 destruct %
419 #t2 * #ls1 * #c1 * #rs1 * normalize in ⊢ (%→?); #H destruct
420 | #l0 #ls0 #IH #c #rs % #t1 * #ls1 * #c1 * #rs1 * #Hmid #Ht1 destruct @IH
425 lemma sem_move_to_end_l : ∀sig. move_to_end sig L ⊨ R_move_to_end_l sig.
426 #sig @WRealize_to_Realize //