+ ∀src,dst,sig,n.src ≠ dst → src < S n → dst < S n →
+ copy_step src dst sig n ⊨
+ [ copy1: R_copy_step_true src dst sig n,
+ R_copy_step_false src dst sig n ].
+#src #dst #sig #n #Hneq #Hsrc #Hdst #int
+lapply (refl ? (current ? (nth src ? int (niltape ?))))
+cases (current ? (nth src ? int (niltape ?))) in ⊢ (???%→?);
+[ #Hcur_src %{2} %
+ [| % [ %
+ [ whd in ⊢ (??%?); >copy_q0_q2_null /2/
+ | normalize in ⊢ (%→?); #H destruct (H) ]
+ | #_ % // % // ] ]
+| #a #Ha lapply (refl ? (current ? (nth dst ? int (niltape ?))))
+ cases (current ? (nth dst ? int (niltape ?))) in ⊢ (???%→?);
+ [ #Hcur_dst %{2} %
+ [| % [ %
+ [ whd in ⊢ (??%?); >copy_q0_q2_null /2/
+ | normalize in ⊢ (%→?); #H destruct (H) ]
+ | #_ % // %2 >Hcur_dst % ] ]
+ | #b #Hb %{2} %
+ [| % [ %
+ [whd in ⊢ (??%?); >(copy_q0_q1 … a b Hneq Hsrc Hdst) //
+ | #_ %{a} %{b} % // % //]
+ | * #H @False_ind @H %
+ ]
+ ]
+ ]
+]
+qed.
+
+definition copy ≝ λsrc,dst,sig,n.
+ whileTM … (copy_step src dst sig n) copy1.
+
+definition R_copy ≝
+ λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n).
+ ((current ? (nth src ? int (niltape ?)) = None ? ∨
+ current ? (nth dst ? int (niltape ?)) = None ?) → outt = int) ∧
+ (∀ls,x,x0,rs,ls0,rs0.
+ nth src ? int (niltape ?) = midtape sig ls x rs →
+ nth dst ? int (niltape ?) = midtape sig ls0 x0 rs0 →
+ (∃rs01,rs02.rs0 = rs01@rs02 ∧ |rs01| = |rs| ∧
+ outt = change_vec ??
+ (change_vec ?? int
+ (mk_tape sig (reverse sig rs@x::ls) (None sig) []) src)
+ (mk_tape sig (reverse sig rs@x::ls0) (option_hd sig rs02)
+ (tail sig rs02)) dst) ∨
+ (∃rs1,rs2.rs = rs1@rs2 ∧ |rs1| = |rs0| ∧
+ outt = change_vec ??
+ (change_vec ?? int
+ (mk_tape sig (reverse sig rs1@x::ls) (option_hd sig rs2)
+ (tail sig rs2)) src)
+ (mk_tape sig (reverse sig rs1@x::ls0) (None sig) []) dst)).
+
+lemma wsem_copy : ∀src,dst,sig,n.src ≠ dst → src < S n → dst < S n →
+ copy src dst sig n ⊫ R_copy src dst sig n.
+#src #dst #sig #n #Hneq #Hsrc #Hdst #ta #k #outc #Hloop
+lapply (sem_while … (sem_copy_step src dst sig n Hneq Hsrc Hdst) … Hloop) //
+-Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
+[ whd in ⊢ (%→?); * #Hnone #Hout %
+ [#_ @Hout
+ |#ls #x #x0 #rs #ls0 #rs0 #Hsrc1 #Hdst1 @False_ind cases Hnone
+ [>Hsrc1 normalize #H destruct (H) | >Hdst1 normalize #H destruct (H)]
+ ]
+|#tc #td * #x * #y * * #Hcx #Hcy #Htd #Hstar #IH #He lapply (IH He) -IH *
+ #IH1 #IH2 %
+ [* [>Hcx #H destruct (H) | >Hcy #H destruct (H)]
+ |#ls #x' #y' #rs #ls0 #rs0 #Hnth_src #Hnth_dst
+ >Hnth_src in Hcx; whd in ⊢ (??%?→?); #H destruct (H)
+ >Hnth_dst in Hcy; whd in ⊢ (??%?→?); #H destruct (H)
+ >Hnth_src in Htd; >Hnth_dst -Hnth_src -Hnth_dst
+ cases rs
+ [(* the source tape is empty after the move *)
+ #Htd lapply (IH1 ?)
+ [%1 >Htd >nth_change_vec_neq [2:@(not_to_not … Hneq) //] >nth_change_vec //]
+ #Hout (* whd in match (tape_move ???); *) %1 %{([])} %{rs0} %
+ [% [// | // ]
+ |whd in match (reverse ??); whd in match (reverse ??);
+ >Hout >Htd @eq_f2 // cases rs0 //
+ ]
+ |#c1 #tl1 cases rs0
+ [(* the dst tape is empty after the move *)
+ #Htd lapply (IH1 ?) [%2 >Htd >nth_change_vec //]
+ #Hout (* whd in match (tape_move ???); *) %2 %{[ ]} %{(c1::tl1)} %
+ [% [// | // ]
+ |whd in match (reverse ??); whd in match (reverse ??);
+ >Hout >Htd @eq_f2 //
+ ]
+ |#c2 #tl2 whd in match (tape_move_mono ???); whd in match (tape_move_mono ???);
+ #Htd
+ cut (nth src (tape sig) td (niltape sig)=midtape sig (x::ls) c1 tl1)
+ [>Htd >nth_change_vec_neq [2:@(not_to_not … Hneq) //] @nth_change_vec //]
+ #Hsrc_td
+ cut (nth dst (tape sig) td (niltape sig)=midtape sig (x::ls0) c2 tl2)
+ [>Htd @nth_change_vec //]
+ #Hdst_td cases (IH2 … Hsrc_td Hdst_td) -Hsrc_td -Hdst_td
+ [* #rs01 * #rs02 * * #H1 #H2 #H3 %1
+ %{(c2::rs01)} %{rs02} % [% [@eq_f //|normalize @eq_f @H2]]
+ >Htd in H3; >change_vec_commute // >change_vec_change_vec
+ >change_vec_commute [2:@(not_to_not … Hneq) //] >change_vec_change_vec
+ #H >reverse_cons >associative_append >associative_append @H
+ |* #rs11 * #rs12 * * #H1 #H2 #H3 %2
+ %{(c1::rs11)} %{rs12} % [% [@eq_f //|normalize @eq_f @H2]]
+ >Htd in H3; >change_vec_commute // >change_vec_change_vec
+ >change_vec_commute [2:@(not_to_not … Hneq) //] >change_vec_change_vec
+ #H >reverse_cons >associative_append >associative_append @H
+ ]
+ ]
+ ]
+ ]
+qed.
+
+
+lemma terminate_copy : ∀src,dst,sig,n,t.
+ src ≠ dst → src < S n → dst < S n → copy src dst sig n ↓ t.
+#src #dst #sig #n #t #Hneq #Hsrc #Hdts
+@(terminate_while … (sem_copy_step …)) //
+<(change_vec_same … t src (niltape ?))
+cases (nth src (tape sig) t (niltape ?))
+[ % #t1 * #x * #y * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
+|2,3: #a0 #al0 % #t1 * #x * #y * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
+| #ls #c #rs lapply c -c lapply ls -ls lapply t -t elim rs
+ [#t #ls #c % #t1 * #x * #y * * >nth_change_vec // normalize in ⊢ (%→?);
+ #H1 destruct (H1) #_ >change_vec_change_vec #Ht1 %
+ #t2 * #x0 * #y0 * * >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
+ >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H)
+ |#r0 #rs0 #IH #t #ls #c % #t1 * #x * #y * * >nth_change_vec //
+ normalize in ⊢ (%→?); #H destruct (H) #Hcur
+ >change_vec_change_vec >change_vec_commute // #Ht1 >Ht1 @IH
+ ]
+]
+qed.
+
+lemma sem_copy : ∀src,dst,sig,n.
+ src ≠ dst → src < S n → dst < S n →
+ copy src dst sig n ⊨ R_copy src dst sig n.
+#i #j #sig #n #Hneq #Hi #Hj @WRealize_to_Realize [/2/| @wsem_copy // ]
+qed.