]> matita.cs.unibo.it Git - helm.git/blobdiff - matita/matita/lib/turing/multi_universal/match.ma
A lot of changes
[helm.git] / matita / matita / lib / turing / multi_universal / match.ma
index d986ac4e3779147c532e095a144814b13ccd1d4a..181a3523506ba45f3d47a558f8cca5df7c7b1e03 100644 (file)
 (*                                                                        *)
 (**************************************************************************)
 
-include "turing/multi_universal/moves.ma".
-include "turing/if_multi.ma".
-include "turing/inject.ma".
-include "turing/basic_machines.ma".
+include "turing/simple_machines.ma".
+include "turing/multi_universal/compare.ma".
+include "turing/multi_universal/par_test.ma".
+include "turing/multi_universal/moves_2.ma".
 
-definition compare_states ≝ initN 3.
-
-definition comp0 : compare_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 3 (refl …)).
-definition comp1 : compare_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 3 (refl …)).
-definition comp2 : compare_states ≝ mk_Sig ?? 2 (leb_true_to_le 3 3 (refl …)).
+definition match_test ≝ λsrc,dst.λsig:DeqSet.λn.λv:Vector ? n.
+  match (nth src (option sig) v (None ?)) with 
+  [ None ⇒  false 
+  | Some x ⇒  notb (nth dst (DeqOption sig) v (None ?) == None ?) ].
+  
+definition rewind ≝ λsrc,dst,sig,n.
+  parmove src dst sig n L · mmove src sig n R · mmove dst sig n R.
 
-(*
+definition R_rewind_strong ≝ λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n).
+  (∀x,x0,xs,rs.
+    nth src ? int (niltape ?) = midtape sig (xs@[x0]) x rs → 
+    ∀ls0,y,y0,target,rs0.|xs| = |target| → 
+    nth dst ? int (niltape ?) = midtape sig (target@y0::ls0) y rs0 → 
+    outt = change_vec ?? 
+           (change_vec ?? int (midtape sig [] x0 (reverse ? xs@x::rs)) src)
+           (midtape sig ls0 y0 (reverse ? target@y::rs0)) dst) ∧
+  (∀x,x0,xs,rs.
+    nth dst ? int (niltape ?) = midtape sig (xs@[x0]) x rs → 
+    ∀ls0,y,y0,target,rs0.|xs| = |target| → 
+    nth src ? int (niltape ?) = midtape sig (target@y0::ls0) y rs0 → 
+    outt = change_vec ?? 
+           (change_vec ?? int (midtape sig [] x0 (reverse ? xs@x::rs)) dst)
+           (midtape sig ls0 y0 (reverse ? target@y::rs0)) src) ∧
+  (∀x,rs.nth src ? int (niltape ?) = midtape sig [] x rs → 
+   ∀ls0,y,rs0.nth dst ? int (niltape ?) = midtape sig ls0 y rs0 → 
+   outt = int) ∧
+  (∀x,rs.nth dst ? int (niltape ?) = midtape sig [] x rs → 
+   ∀ls0,y,rs0.nth src ? int (niltape ?) = midtape sig ls0 y rs0 → 
+   outt = int).
 
-0) (x,x) → (x,x)(R,R) → 1
-   (x,y≠x) → None 2
-1) (_,_) → None 1
-2) (_,_) → None 2
+definition R_rewind ≝ λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n).
+  (∀x,x0,xs,rs.
+    nth src ? int (niltape ?) = midtape sig (xs@[x0]) x rs → 
+    ∀ls0,y,y0,target,rs0.|xs| = |target| → 
+    nth dst ? int (niltape ?) = midtape sig (target@y0::ls0) y rs0 → 
+    outt = change_vec ?? 
+           (change_vec ?? int (midtape sig [] x0 (reverse ? xs@x::rs)) src)
+           (midtape sig ls0 y0 (reverse ? target@y::rs0)) dst) ∧
+  (∀x,rs.nth src ? int (niltape ?) = midtape sig [] x rs → 
+   ∀ls0,y,rs0.nth dst ? int (niltape ?) = midtape sig ls0 y rs0 → 
+   outt = int).
 
+(*
+theorem accRealize_to_Realize :
+  ∀sig,n.∀M:mTM sig n.∀Rtrue,Rfalse,acc.
+  M ⊨ [ acc: Rtrue, Rfalse ] →  M ⊨ Rtrue ∪ Rfalse.
+#sig #n #M #Rtrue #Rfalse #acc #HR #t
+cases (HR t) #k * #outc * * #Hloop
+#Htrue #Hfalse %{k} %{outc} % // 
+cases (true_or_false (cstate sig (states sig n M) n outc == acc)) #Hcase
+[ % @Htrue @(\P Hcase) | %2 @Hfalse @(\Pf Hcase) ]
+qed.
 *)
 
-definition trans_compare_step ≝ 
- λi,j.λsig:FinSet.λn.λis_endc.
- λp:compare_states × (Vector (option sig) (S n)).
- let 〈q,a〉 ≝ p in
- match pi1 … q with
- [ O ⇒ match nth i ? a (None ?) with
-   [ None ⇒ 〈comp2,null_action ? n〉
-   | Some ai ⇒ match nth j ? a (None ?) with 
-     [ None ⇒ 〈comp2,null_action ? n〉
-     | Some aj ⇒ if notb (is_endc ai) ∧ ai == aj 
-         then 〈comp1,change_vec ? (S n) 
-                      (change_vec ? (S n) (null_action ? n) (Some ? 〈ai,R〉) i)
-                        (Some ? 〈aj,R〉) j〉
-         else 〈comp2,null_action ? n〉 ]
-   ]
- | S q ⇒ match q with 
-   [ O ⇒ (* 1 *) 〈comp1,null_action ? n〉
-   | S _ ⇒ (* 2 *) 〈comp2,null_action ? n〉 ] ].
-
-definition compare_step ≝ 
-  λi,j,sig,n,is_endc.
-  mk_mTM sig n compare_states (trans_compare_step i j sig n is_endc) 
-    comp0 (λq.q == comp1 ∨ q == comp2).
+lemma sem_rewind_strong : ∀src,dst,sig,n.
+  src ≠ dst → src < S n → dst < S n → 
+  rewind src dst sig n ⊨ R_rewind_strong src dst sig n.
+#src #dst #sig #n #Hneq #Hsrc #Hdst
+@(sem_seq_app sig n ????? (sem_parmoveL src dst sig n Hneq Hsrc Hdst) ?)
+[| @(sem_seq_app sig n ????? (sem_move_multi … R ?) (sem_move_multi … R ?)) //
+ @le_S_S_to_le // ]
+#ta #tb * #tc * * * #Htc1 #Htc2 #_ * #td * whd in ⊢ (%→%→?); #Htd #Htb % [ % [ %
+[ #x #x0 #xs #rs #Hmidta_src #ls0 #y #y0 #target #rs0 #Hlen #Hmidta_dst
+  >(Htc1 ??? Hmidta_src ls0 y (target@[y0]) rs0 ??) in Htd;
+  [|>Hmidta_dst //
+  |>length_append >length_append >Hlen % ]
+  >change_vec_commute [|@sym_not_eq //]
+  >change_vec_change_vec
+  >nth_change_vec_neq [|@sym_not_eq //]
+  >nth_change_vec // >reverse_append >reverse_single
+  >reverse_append >reverse_single normalize in match (tape_move ???);
+  >rev_append_def >append_nil #Htd >Htd in Htb;
+  >change_vec_change_vec >nth_change_vec //
+  cases ls0 [|#l1 #ls1] normalize in match (tape_move ???); //
+| #x #x0 #xs #rs #Hmidta_dst #ls0 #y #y0 #target #rs0 #Hlen #Hmidta_src
+  >(Htc2 ??? Hmidta_dst ls0 y (target@[y0]) rs0 ??) in Htd;
+  [|>Hmidta_src //
+  |>length_append >length_append >Hlen % ]
+  >change_vec_change_vec
+  >change_vec_commute [|@sym_not_eq //]
+  >nth_change_vec // 
+  >reverse_append >reverse_single
+  >reverse_append >reverse_single
+  cases ls0 [|#l1 #ls1] normalize in match (tape_move ???);
+  #Htd >Htd in Htb; >change_vec_change_vec >nth_change_vec //
+  >rev_append_def >change_vec_commute // normalize in match (tape_move ???); // ]
+| #x #rs #Hmidta_src #ls0 #y #rs0 #Hmidta_dst
+  lapply (Htc1 … Hmidta_src … (refl ??) Hmidta_dst) -Htc1 #Htc >Htc in Htd;
+  >change_vec_commute [|@sym_not_eq //] >change_vec_change_vec
+  >nth_change_vec_neq [|@sym_not_eq //] 
+  >nth_change_vec // lapply (refl ? ls0) cases ls0 in ⊢ (???%→%);
+  [ #Hls0 #Htd >Htd in Htb; 
+    >nth_change_vec // >change_vec_change_vec 
+    whd in match (tape_move ???);whd in match (tape_move ???); <Hmidta_src
+    <Hls0 <Hmidta_dst >change_vec_same >change_vec_same //
+  | #l1 #ls1 #Hls0 #Htd >Htd in Htb;
+    >nth_change_vec // >change_vec_change_vec 
+    whd in match (tape_move ???);whd in match (tape_move ???); <Hmidta_src
+    <Hls0 <Hmidta_dst >change_vec_same >change_vec_same //
+  ] ]
+| #x #rs #Hmidta_dst #ls0 #y #rs0 #Hmidta_src
+  lapply (Htc2 … Hmidta_dst … (refl ??) Hmidta_src) -Htc2 #Htc >Htc in Htd;
+  >change_vec_change_vec >change_vec_commute [|@sym_not_eq //]
+  >nth_change_vec // lapply (refl ? ls0) cases ls0 in ⊢ (???%→%);
+  [ #Hls0 destruct (Hls0) #Htd >Htd in Htb; 
+    >nth_change_vec // >change_vec_change_vec 
+    whd in match (tape_move ???);whd in match (tape_move ???); 
+    <Hmidta_src <Hmidta_dst >change_vec_same >change_vec_same //
+  | #l1 #ls1 #Hls0 destruct (Hls0) #Htd >Htd in Htb;
+    >nth_change_vec // >change_vec_change_vec 
+    whd in match (tape_move ???); whd in match (tape_move ???); <Hmidta_src
+    <Hmidta_dst >change_vec_same >change_vec_same //
+  ]
+]
+qed.
 
-definition R_comp_step_true ≝ 
-  λi,j,sig,n,is_endc.λint,outt: Vector (tape sig) (S n).
-  ∃x.
-   is_endc x = false ∧
-   current ? (nth i ? int (niltape ?)) = Some ? x ∧
-   current ? (nth j ? int (niltape ?)) = Some ? x ∧
-   outt = change_vec ?? 
-            (change_vec ?? int
-              (tape_move ? (nth i ? int (niltape ?)) (Some ? 〈x,R〉)) i)
-            (tape_move ? (nth j ? int (niltape ?)) (Some ? 〈x,R〉)) j.
+lemma sem_rewind : ∀src,dst,sig,n.
+  src ≠ dst → src < S n → dst < S n → 
+  rewind src dst sig n ⊨ R_rewind src dst sig n.
+#src #dst #sig #n #Hneq #Hsrc #Hdst @(Realize_to_Realize … (sem_rewind_strong …)) //
+#ta #tb * * * #H1 #H2 #H3 #H4 % /2 by /
+qed.
 
-definition R_comp_step_false ≝ 
-  λi,j:nat.λsig,n,is_endc.λint,outt: Vector (tape sig) (S n).
-   ((∃x.current ? (nth i ? int (niltape ?)) = Some ? x ∧ is_endc x = true) ∨
-   current ? (nth i ? int (niltape ?)) ≠ current ? (nth j ? int (niltape ?)) ∨
-   current ? (nth i ? int (niltape ?)) = None ? ∨
-   current ? (nth j ? int (niltape ?)) = None ?) ∧ outt = int.
+definition match_step ≝ λsrc,dst,sig,n.
+  compare src dst sig n ·
+     (ifTM ?? (partest sig n (match_test src dst sig ?))
+      (single_finalTM ??
+        (rewind src dst sig n · (inject_TM ? (move_r ?) n dst)))
+      (nop …)
+      partest1).
 
-lemma comp_q0_q2_null :
-  ∀i,j,sig,n,is_endc,v.i < S n → j < S n → 
-  (nth i ? (current_chars ?? v) (None ?) = None ? ∨
-   nth j ? (current_chars ?? v) (None ?) = None ?) → 
-  step sig n (compare_step i j sig n is_endc) (mk_mconfig ??? comp0 v) 
-  = mk_mconfig ??? comp2 v.
-#i #j #sig #n #is_endc #v #Hi #Hj
-whd in ⊢ (? → ??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (?→??%?);
-* #Hcurrent
-[ @eq_f2
-  [ whd in ⊢ (??(???%)?); >Hcurrent %
-  | whd in ⊢ (??(???????(???%))?); >Hcurrent @tape_move_null_action ]
-| @eq_f2
-  [ whd in ⊢ (??(???%)?); >Hcurrent cases (nth i ?? (None sig)) //
-  | whd in ⊢ (??(???????(???%))?); >Hcurrent
-    cases (nth i ?? (None sig)) [|#x] @tape_move_null_action ] ]
-qed.
+(* we assume the src is a midtape
+   we stop
+   if the dst is out of bounds (outt = int)
+   or dst.right is shorter than src.right (outt.current → None)
+   or src.right is a prefix of dst.right (out = just right of the common prefix) *)
+definition R_match_step_false ≝ 
+  λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n).
+  ∀ls,x,xs.
+  nth src ? int (niltape ?) = midtape sig ls x xs →
+  ((current sig (nth dst (tape sig) int (niltape sig)) = None ?) ∧ outt = int) ∨
+    (∃ls0,rs0,xs0. nth dst ? int (niltape ?) = midtape sig ls0 x rs0 ∧
+      xs = rs0@xs0 ∧
+      outt = change_vec ??
+             (change_vec ?? int (mk_tape sig (reverse ? rs0@x::ls) (option_hd ? xs0) (tail ? xs0)) src)
+             (mk_tape ? (reverse ? rs0@x::ls0) (None ?) [ ]) dst) ∨
+    (∃ls0,rs0. 
+     nth dst ? int (niltape ?) = midtape sig ls0 x (xs@rs0) ∧
+     (* ∀rsj,c. 
+     rs0 = c::rsj → *)
+     outt = change_vec ??
+            (change_vec ?? int (mk_tape sig (reverse ? xs@x::ls) (None ?) [ ]) src)
+            (mk_tape sig (reverse ? xs@x::ls0) (option_hd ? rs0) (tail ? rs0)) dst).
 
-lemma comp_q0_q2_neq :
-  ∀i,j,sig,n,is_endc,v.i < S n → j < S n → 
-  ((∃x.nth i ? (current_chars ?? v) (None ?) = Some ? x ∧ is_endc x = true) ∨ 
-    nth i ? (current_chars ?? v) (None ?) ≠ nth j ? (current_chars ?? v) (None ?)) → 
-  step sig n (compare_step i j sig n is_endc) (mk_mconfig ??? comp0 v) 
-  = mk_mconfig ??? comp2 v.
-#i #j #sig #n #is_endc #v #Hi #Hj lapply (refl ? (nth i ?(current_chars ?? v)(None ?)))
-cases (nth i ?? (None ?)) in ⊢ (???%→?);
-[ #Hnth #_ @comp_q0_q2_null // % //
-| #ai #Hai lapply (refl ? (nth j ?(current_chars ?? v)(None ?)))
-  cases (nth j ?? (None ?)) in ⊢ (???%→?);
-  [ #Hnth #_ @comp_q0_q2_null // %2 //
-  | #aj #Haj *
-    [ * #c * >Hai #Heq #Hendc whd in ⊢ (??%?); 
-      >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
-      [ whd in match (trans ????); >Hai >Haj destruct (Heq) 
-        whd in ⊢ (??(???%)?); >Hendc // 
-      | whd in match (trans ????); >Hai >Haj destruct (Heq) 
-        whd in ⊢ (??(???????(???%))?); >Hendc @tape_move_null_action
-      ]
-    | #Hneq
-      whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
-      [ whd in match (trans ????); >Hai >Haj
-        whd in ⊢ (??(???%)?); cut ((¬is_endc ai∧ai==aj)=false)
-        [>(\bf ?) /2 by not_to_not/ cases (is_endc ai) // |#Hcut >Hcut //]
-        | whd in match (trans ????); >Hai >Haj
-          whd in ⊢ (??(???????(???%))?); cut ((¬is_endc ai∧ai==aj)=false)
-          [>(\bf ?) /2 by not_to_not/ cases (is_endc ai) // 
-          |#Hcut >Hcut @tape_move_null_action
+(*
+   we assume the src is a midtape [ ] s rs
+   if we iterate
+   then dst.current = Some ? s1
+   and  if s ≠ s1 then outt = int.dst.move_right()
+   and  if s = s1 
+        then int.src.right and int.dst.right have a common prefix
+        and  the heads of their suffixes are different
+        and  outt = int.dst.move_right().
+               
+ *)
+definition R_match_step_true ≝ 
+  λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n).
+  ∀s,rs.nth src ? int (niltape ?) = midtape ? [ ] s rs → 
+  outt = change_vec ?? int 
+         (tape_move_mono … (nth dst ? int (niltape ?)) (〈None ?,R〉)) dst ∧
+  (∃s0.current sig (nth dst (tape sig) int (niltape sig)) = Some ? s0 ∧
+   (s0 = s →  
+   ∃xs,ci,rs',ls0,cj,rs0.
+   rs = xs@ci::rs' ∧
+   nth dst ? int (niltape ?) = midtape sig ls0 s (xs@cj::rs0) ∧
+   ci ≠ cj)).
+  
+lemma sem_match_step :
+  ∀src,dst,sig,n.src ≠ dst → src < S n → dst < S n → 
+  match_step src dst sig n ⊨ 
+    [ inr ?? (inr ?? (inl … (inr ?? start_nop))) : 
+      R_match_step_true src dst sig n, 
+      R_match_step_false src dst sig n ].
+#src #dst #sig #n #Hneq #Hsrc #Hdst 
+@(acc_sem_seq_app sig n … (sem_compare src dst sig n Hneq Hsrc Hdst)
+    (acc_sem_if ? n … (sem_partest sig n (match_test src dst sig ?))
+      (sem_seq … 
+        (sem_rewind ???? Hneq Hsrc Hdst) 
+        (sem_inject … dst (le_S_S_to_le … Hdst) (sem_move_r ? )))
+      (sem_nop …)))
+[ #ta #tb #tc * lapply (refl ? (current ? (nth src ? ta (niltape ?))))
+  cases (current ? (nth src ? ta (niltape ?))) in ⊢ (???%→%);
+  [ #Hcurta_src #Hcomp #_ * #td * >Hcomp [| % %2 %]
+    whd in ⊢ (%→?); * whd in ⊢ (??%?→?); 
+    >nth_current_chars >Hcurta_src normalize in ⊢ (%→?); #H destruct (H)
+  | #s #Hs lapply (refl ? (current ? (nth dst ? ta (niltape ?))))
+    cases (current ? (nth dst ? ta (niltape ?))) in ⊢ (???%→%);
+    [ #Hcurta_dst #Hcomp #_ * #td * >Hcomp [| %2 %]
+      whd in ⊢ (%→?); * whd in ⊢ (??%?→?); 
+      >nth_current_chars >nth_current_chars >Hs >Hcurta_dst 
+      normalize in ⊢ (%→?); #H destruct (H)
+    | #s0 #Hs0
+      cases (current_to_midtape … Hs) #ls * #rs #Hmidta_src >Hmidta_src
+      cases (current_to_midtape … Hs0) #ls0 * #rs0 #Hmidta_dst >Hmidta_dst
+      cases (true_or_false (s == s0)) #Hss0
+      [ lapply (\P Hss0) -Hss0 #Hss0 destruct (Hss0) 
+        #_ #Hcomp cases (Hcomp ????? (refl ??) (refl ??)) -Hcomp [ *
+        [ * #rs' * #_ #Hcurtc_dst * #td * whd in ⊢ (%→?); * whd in ⊢ (??%?→?);
+          >nth_current_chars >nth_current_chars >Hcurtc_dst 
+          cases (current ? (nth src …)) 
+          [normalize in ⊢ (%→?); #H destruct (H)
+          | #x >nth_change_vec // cases (reverse ? rs0)
+            [ normalize in ⊢ (%→?); #H destruct (H)
+            | #r1 #rs1 normalize in ⊢ (%→?); #H destruct (H) ] ]
+        | * #rs0' * #_ #Hcurtc_src * #td * whd in ⊢ (%→?); * whd in ⊢ (??%?→?);
+          >(?:nth src ? (current_chars ?? tc) (None ?) = None ?) 
+          [|>nth_current_chars >Hcurtc_src >nth_change_vec_neq 
+            [>nth_change_vec [cases (append ???) // | @Hsrc] 
+            |@(not_to_not … Hneq) //
+            ]]
+          normalize in ⊢ (%→?); #H destruct (H) ]
+        | * #xs * #ci * #cj * #rs'' * #rs0' * * * #Hcicj #Hrs #Hrs0
+          #Htc * #td * * #Hmatch #Htd destruct (Htd) * #te * *
+          >Htc >change_vec_commute // >nth_change_vec //
+          >change_vec_commute [|@sym_not_eq //] >nth_change_vec // #Hte #_ #Htb
+          #s' #rs' >Hmidta_src #H destruct (H)
+          lapply (Hte … (refl ??) … (refl ??) (refl ??)) -Hte
+          >change_vec_commute // >change_vec_change_vec
+          >change_vec_commute [|@sym_not_eq //] >change_vec_change_vec #Hte
+          >Hte in Htb; * * #_ >nth_change_vec // #Htb1
+          lapply (Htb1 … (refl ??)) -Htb1 #Htb1 #Htb2 %
+          [ @(eq_vec … (niltape ?)) #i #Hi
+            cases (true_or_false ((dst : DeqNat) == i)) #Hdsti
+            [ <(\P Hdsti) >Htb1 >nth_change_vec // >Hmidta_dst
+              >Hrs0 >reverse_reverse cases xs [|#r1 #rs1] %
+            | <Htb2 [|@(\Pf Hdsti)] >nth_change_vec_neq [| @(\Pf Hdsti)]
+              >Hrs0 >reverse_reverse >nth_change_vec_neq in ⊢ (???%); 
+              <Hrs <Hmidta_src [|@(\Pf Hdsti)] >change_vec_same % ]
+          | >Hmidta_dst %{s'} % [%] #_
+            >Hrs0 %{xs} %{ci} %{rs''} %{ls0} %{cj} %{rs0'} % // % // 
           ]
         ]
+      | lapply (\Pf Hss0) -Hss0 #Hss0 #Htc cut (tc = ta) 
+        [@Htc % % @(not_to_not ??? Hss0) #H destruct (H) %]
+        -Htc #Htc destruct (Htc) #_ * #td * whd in ⊢ (%→?); * #_ 
+        #Htd destruct (Htd) * #te * * #_ #Hte * * #_ #Htb1 #Htb2
+        #s1 #rs1 >Hmidta_src #H destruct (H)
+        lapply (Hte … Hmidta_src … Hmidta_dst) -Hte #Hte destruct (Hte) %
+        [ @(eq_vec … (niltape ?)) #i #Hi
+          cases (true_or_false ((dst : DeqNat) == i)) #Hdsti
+          [ <(\P Hdsti) >(Htb1 … Hmidta_dst) >nth_change_vec // >Hmidta_dst
+            cases rs0 [|#r2 #rs2] %
+          | <Htb2 [|@(\Pf Hdsti)] >nth_change_vec_neq [| @(\Pf Hdsti)] % ]
+        | >Hs0 %{s0} % // #H destruct (H) @False_ind cases (Hss0) /2/ ]
       ]
     ]
-]
+  ]
+| #ta #tb #tc * #Hcomp1 #Hcomp2 * #td * * #Htest #Htd destruct (Htd)
+  whd in ⊢ (%→?); #Htb destruct (Htb) #ls #x #xs #Hta_src
+  lapply (refl ? (current ? (nth dst ? ta (niltape ?))))
+  cases (current ? (nth dst ? ta (niltape ?))) in ⊢ (???%→?);
+  [ #Hcurta_dst % % % // @Hcomp1 %2 //
+  | #x0 #Hcurta_dst cases (current_to_midtape … Hcurta_dst) -Hcurta_dst
+    #ls0 * #rs0 #Hta_dst cases (true_or_false (x == x0)) #Hxx0
+    [ lapply (\P Hxx0) -Hxx0 #Hxx0 destruct (Hxx0)
+    | >(?:tc=ta) in Htest; 
+      [|@Hcomp1 % % >Hta_src >Hta_dst @(not_to_not ??? (\Pf Hxx0)) normalize
+        #Hxx0' destruct (Hxx0') % ]
+      whd in ⊢ (??%?→?); 
+      >nth_current_chars >Hta_src >nth_current_chars >Hta_dst 
+      whd in ⊢ (??%?→?); #Hfalse destruct (Hfalse) ] -Hcomp1
+      cases (Hcomp2 … Hta_src Hta_dst) [ *
+      [ * #rs' * #Hxs #Hcurtc % %2 %{ls0} %{rs0} %{rs'} %
+        [ % // | >Hcurtc % ]
+      | * #rs0' * #Hxs #Htc %2 >Htc %{ls0} %{rs0'} % // ]
+      | * #xs0 * #ci * #cj * #rs' * #rs0' * * *
+        #Hci #Hxs #Hrs0 #Htc @False_ind
+        whd in Htest:(??%?); 
+        >(?:nth src ? (current_chars ?? tc) (None ?) = Some ? ci) in Htest; 
+        [|>nth_current_chars >Htc >nth_change_vec_neq [|@(not_to_not … Hneq) //]
+          >nth_change_vec //]
+        >(?:nth dst ? (current_chars ?? tc) (None ?) = Some ? cj) 
+        [|>nth_current_chars >Htc >nth_change_vec //]
+        normalize #H destruct (H) ] ] ]
 qed.
 
-lemma comp_q0_q1 :
-  ∀i,j,sig,n,is_endc,v,a.i ≠ j → i < S n → j < S n → 
-  nth i ? (current_chars ?? v) (None ?) = Some ? a → is_endc a = false →
-  nth j ? (current_chars ?? v) (None ?) = Some ? a → 
-  step sig n (compare_step i j sig n is_endc) (mk_mconfig ??? comp0 v) =
-    mk_mconfig ??? comp1 
-     (change_vec ? (S n) 
-       (change_vec ?? v
-         (tape_move ? (nth i ? v (niltape ?)) (Some ? 〈a,R〉)) i)
-       (tape_move ? (nth j ? v (niltape ?)) (Some ? 〈a,R〉)) j).
-#i #j #sig #n #is_endc #v #a #Heq #Hi #Hj #Ha1 #Hnotendc #Ha2
-whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
-[ whd in match (trans ????);
-  >Ha1 >Ha2 whd in ⊢ (??(???%)?); >Hnotendc >(\b ?) //
-| whd in match (trans ????);
-  >Ha1 >Ha2 whd in ⊢ (??(???????(???%))?); >Hnotendc >(\b ?) //
-  change with (change_vec ?????) in ⊢ (??(???????%)?);
-  <(change_vec_same … v j (niltape ?)) in ⊢ (??%?);
-  <(change_vec_same … v i (niltape ?)) in ⊢ (??%?);
-  >pmap_change >pmap_change >tape_move_null_action
-  @eq_f2 // @eq_f2 // >nth_change_vec_neq //
-]
+definition match_m ≝ λsrc,dst,sig,n.
+  whileTM … (match_step src dst sig n) 
+    (inr ?? (inr ?? (inl … (inr ?? start_nop)))).
+
+definition R_match_m ≝ 
+  λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n).
+  ∀x,rs.
+  nth src ? int (niltape ?) = midtape sig [ ] x rs →
+  (current sig (nth dst (tape sig) int (niltape sig)) = None ? → 
+   right ? (nth dst (tape sig) int (niltape sig)) = [ ] → outt = int) ∧
+  (∀ls0,x0,rs0.
+   nth dst ? int (niltape ?) = midtape sig ls0 x0 rs0 →
+   (∃l,l1.x0::rs0 = l@x::rs@l1 ∧
+    outt = change_vec ?? 
+           (change_vec ?? int 
+             (mk_tape sig (reverse ? rs@[x]) (None ?) [ ]) src)
+           (mk_tape sig ((reverse ? (l@x::rs))@ls0) (option_hd ? l1) (tail ? l1)) dst) ∨
+    ∀l,l1.x0::rs0 ≠ l@x::rs@l1).
+
+lemma not_sub_list_merge : 
+  ∀T.∀a,b:list T. (∀l1.a ≠ b@l1) → (∀t,l,l1.a ≠ t::l@b@l1) → ∀l,l1.a ≠ l@b@l1.
+#T #a #b #H1 #H2 #l elim l normalize //
 qed.
 
-lemma sem_comp_step :
-  ∀i,j,sig,n,is_endc.i ≠ j → i < S n → j < S n → 
-  compare_step i j sig n is_endc ⊨ 
-    [ comp1: R_comp_step_true i j sig n is_endc, 
-             R_comp_step_false i j sig n is_endc ].
-#i #j #sig #n #is_endc #Hneq #Hi #Hj #int
-lapply (refl ? (current ? (nth i ? int (niltape ?))))
-cases (current ? (nth i ? int (niltape ?))) in ⊢ (???%→?);
-[ #Hcuri %{2} %
-  [| % [ %
-    [ whd in ⊢ (??%?); >comp_q0_q2_null /2/ % <Hcuri in ⊢ (???%); 
-      @sym_eq @nth_vec_map
-    | normalize in ⊢ (%→?); #H destruct (H) ]
-  | #_ % // % %2 // ] ]
-| #a #Ha lapply (refl ? (current ? (nth j ? int (niltape ?))))
-  cases (current ? (nth j ? int (niltape ?))) in ⊢ (???%→?);
-  [ #Hcurj %{2} %
-    [| % [ %
-       [ whd in ⊢ (??%?); >comp_q0_q2_null /2/ %2 <Hcurj in ⊢ (???%); 
-         @sym_eq @nth_vec_map
-       | normalize in ⊢ (%→?); #H destruct (H) ]
-       | #_ % // >Ha >Hcurj % % %2 % #H destruct (H) ] ]
-  | #b #Hb %{2} 
-   cases (true_or_false (is_endc a)) #Haendc
-    [ %
-      [| % [ % 
-        [whd in ⊢  (??%?);  >comp_q0_q2_neq //
-         % %{a} % // <Ha @sym_eq @nth_vec_map
-        | normalize in ⊢ (%→?); #H destruct (H) ]
-      | #_ % // % % % >Ha %{a} % // ]
-      ]
-    |cases (true_or_false (a == b)) #Hab
-      [ %
-        [| % [ % 
-          [whd in ⊢  (??%?);  >(comp_q0_q1 … a Hneq Hi Hj) //
-            [>(\P Hab) <Hb @sym_eq @nth_vec_map
-            |<Ha @sym_eq @nth_vec_map ]
-          | #_ whd >(\P Hab) %{b} % // % // <(\P Hab) % // ]
-          | * #H @False_ind @H %
-        ] ]
-      | %
-        [| % [ % 
-          [whd in ⊢  (??%?);  >comp_q0_q2_neq //
-           <(nth_vec_map ?? (current …) i ? int (niltape ?))
-           <(nth_vec_map ?? (current …) j ? int (niltape ?)) %2 >Ha >Hb
-           @(not_to_not ??? (\Pf Hab)) #H destruct (H) %
-          | normalize in ⊢ (%→?); #H destruct (H) ]
-        | #_ % // % % %2 >Ha >Hb @(not_to_not ??? (\Pf Hab)) #H destruct (H) % ] ]
-      ]
-    ]
-  ]
-]
+lemma not_sub_list_merge_2 : 
+  ∀T:DeqSet.∀a,b:list T.∀t. (∀l1.t::a ≠ b@l1) → (∀l,l1.a ≠ l@b@l1) → ∀l,l1.t::a ≠ l@b@l1.
+#T #a #b #t #H1 #H2 #l elim l //
+#t0 #l1 #IH #l2 cases (true_or_false (t == t0)) #Htt0
+[ >(\P Htt0) % normalize #H destruct (H) cases (H2 l1 l2) /2/
+| normalize % #H destruct (H) cases (\Pf Htt0) /2/ ]
 qed.
 
-definition compare ≝ λi,j,sig,n,is_endc.
-  whileTM … (compare_step i j sig n is_endc) comp1.
 
-definition R_compare ≝ 
-  λi,j,sig,n,is_endc.λint,outt: Vector (tape sig) (S n).
-  ((∃x.current ? (nth i ? int (niltape ?)) = Some ? x ∧ is_endc x = true) ∨
-   (current ? (nth i ? int (niltape ?)) ≠ current ? (nth j ? int (niltape ?)) ∨
-    current ? (nth i ? int (niltape ?)) = None ? ∨
-    current ? (nth j ? int (niltape ?)) = None ?) → outt = int) ∧
-  (∀ls,x,xs,ci,rs,ls0,cj,rs0. 
-    nth i ? int (niltape ?) = midtape sig ls x (xs@ci::rs) →
-    nth j ? int (niltape ?) = midtape sig ls0 x (xs@cj::rs0) →
-    (∀c0. memb ? c0 (x::xs) = true → is_endc c0 = false) → 
-    (is_endc ci = true ∨ ci ≠ cj) → 
-    outt = change_vec ?? 
-           (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) i)
-           (midtape sig (reverse ? xs@x::ls0) cj rs0) j).
-          
-lemma wsem_compare : ∀i,j,sig,n,is_endc.i ≠ j → i < S n → j < S n → 
-  compare i j sig n is_endc ⊫ R_compare i j sig n is_endc.
-#i #j #sig #n #is_endc #Hneq #Hi #Hj #ta #k #outc #Hloop
-lapply (sem_while … (sem_comp_step i j sig n is_endc Hneq Hi Hj) … Hloop) //
+lemma wsem_match_m : ∀src,dst,sig,n.
+src ≠ dst → src < S n → dst < S n → 
+  match_m src dst sig n ⊫ R_match_m src dst sig n.
+#src #dst #sig #n #Hneq #Hsrc #Hdst #ta #k #outc #Hloop
+lapply (sem_while … (sem_match_step src dst sig n Hneq Hsrc Hdst) … Hloop) //
 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
-[ #tc whd in ⊢ (%→?); * * [ * [ *
-  [* #curi * #Hcuri #Hendi #Houtc %
-    [ #_ @Houtc  
-    | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hnthi #Hnthj #Hnotendc 
-      @False_ind
-      >Hnthi in Hcuri; normalize in ⊢ (%→?); #H destruct (H)
-      >(Hnotendc ? (memb_hd … )) in Hendi; #H destruct (H)
+[ #Hfalse #x #xs #Hmid_src
+  cases (Hfalse … Hmid_src) -Hfalse 
+  [(* current dest = None *) *
+    [ * #Hcur_dst #Houtc %
+      [#_ >Houtc //
+      | #ls0 #x0 #rs0 #Hmid_dst >Hmid_dst in Hcur_dst; 
+        normalize in ⊢ (%→?); #H destruct (H)
+      ]
+    | * #ls0 * #rs0 * #xs0 * * #Htc_dst #Hrs0 #HNone %
+      [ >Htc_dst normalize in ⊢ (%→?); #H destruct (H)
+      | #ls1 #x1 #rs1 >Htc_dst #H destruct (H)
+        >Hrs0 >HNone cases xs0
+        [ % %{[ ]} %{[ ]} % [ >append_nil >append_nil %]
+          @eq_f3 //
+          [ >reverse_append %
+          | >reverse_append >reverse_cons >reverse_append
+            >associative_append >associative_append % ]
+        | #x2 #xs2 %2 #l #l1 % #Habs lapply (eq_f ?? (length ?) ?? Habs)
+          >length_append whd in ⊢ (??%(??%)→?); >length_append
+          >length_append normalize >commutative_plus whd in ⊢ (???%→?);
+          #H destruct (H) lapply e0 >(plus_n_O (|rs1|)) in ⊢ (??%?→?);
+          >associative_plus >associative_plus 
+          #e1 lapply (injective_plus_r ??? e1) whd in ⊢ (???%→?);
+          #e2 destruct (e2)
+        ]
+      ]
+    ]
+  |* #ls0 * #rs0 * #Hmid_dst #Houtc %
+    [ >Hmid_dst normalize in ⊢ (%→?); #H destruct (H)
+    |#ls1 #x1 #rs1 >Hmid_dst #H destruct (H)
+     %1 %{[ ]} %{rs0} % [%] 
+     >reverse_cons >associative_append >Houtc %
+    ]
+  ]
+|-ta #ta #tc #Htrue #Hstar #IH #Hout lapply (IH Hout) -IH -Hout #IH whd
+ #x #xs #Hmidta_src
+ lapply (refl ? (current ? (nth dst ? ta (niltape ?))))
+ cases (current ? (nth dst ? ta (niltape ?))) in ⊢ (???%→?); 
+  [#Hcurta_dst % 
+    [#Hcurta_dst #Hrightta_dst whd in Htrue; >Hmidta_src in Htrue; #Htrue
+     cases (Htrue ?? (refl ??)) -Htrue #Htc
+     cut (tc = ta)
+     [ >Htc whd in match (tape_move_mono ???); whd in match (tape_write ???);
+       <(change_vec_same … ta dst (niltape ?)) in ⊢ (???%);
+       lapply Hrightta_dst lapply Hcurta_dst -Hrightta_dst -Hcurta_dst 
+       cases (nth dst ? ta (niltape ?))
+       [ #_ #_ %
+       | #r0 #rs0 #_ normalize in ⊢ (%→?); #H destruct (H)
+       | #l0 #ls0 #_ #_ %
+       | #ls #x0 #rs normalize in ⊢ (%→?); #H destruct (H) ] ]
+     -Htc #Htc destruct (Htc) #_
+     cases (IH … Hmidta_src) #Houtc #_ @Houtc //
+    |#ls0 #x0 #rs0 #Hmidta_dst >Hmidta_dst in Hcurta_dst;
+     normalize in ⊢ (%→?); #H destruct (H)
     ]
-  |#Hcicj #Houtc % 
-    [ #_ @Houtc
-    | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hnthi #Hnthj
-      >Hnthi in Hcicj; >Hnthj normalize in ⊢ (%→?); * #H @False_ind @H %
-    ]]
-  | #Hci #Houtc %
-    [ #_ @Houtc
-    | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hnthi >Hnthi in Hci;
-      normalize in ⊢ (%→?); #H destruct (H) ] ]
-  | #Hcj #Houtc %
-    [ #_ @Houtc
-    | #ls #x #xs #ci #rs #ls0 #cj #rs0 #_ #Hnthj >Hnthj in Hcj;
-      normalize in ⊢ (%→?); #H destruct (H) ] ]
-  | #tc #td #te * #x * * * #Hendcx #Hci #Hcj #Hd #Hstar #IH #He lapply (IH He) -IH *
-    #IH1 #IH2 %
-    [ >Hci >Hcj * [* #x0 * #H destruct (H) >Hendcx #H destruct (H) 
-    |* [* #H @False_ind [cases H -H #H @H % | destruct (H)] | #H destruct (H)]] 
-    | #ls #c0 #xs #ci #rs #ls0 #cj #rs0 cases xs
-      [ #Hnthi #Hnthj #Hnotendc #Hcicj >IH1 
-        [ >Hd @eq_f3 // 
-          [ @eq_f3 // >(?:c0=x) [ >Hnthi % ]
-            >Hnthi in Hci;normalize #H destruct (H) %
-          | >(?:c0=x) [ >Hnthj % ]
-            >Hnthi in Hci;normalize #H destruct (H) % ]
-        | >Hd >nth_change_vec // >nth_change_vec_neq [|@sym_not_eq //]
-          >nth_change_vec // >Hnthi >Hnthj normalize 
-          cases Hcicj #Hcase 
-          [%1 %{ci} % // | %2 %1 %1 @(not_to_not ??? Hcase) #H destruct (H) % ]
+  | #c #Hcurta_dst % [ >Hcurta_dst #H destruct (H) ]
+    #ls0 #x0 #rs0 #Hmidta_dst >Hmidta_dst in Hcurta_dst; normalize in ⊢ (%→?);
+    #H destruct (H) whd in Htrue; >Hmidta_src in Htrue; #Htrue
+    cases (Htrue ?? (refl …)) -Htrue >Hmidta_dst #Htc
+    cases (true_or_false (x==c)) #eqx
+    [ lapply (\P eqx) -eqx #eqx destruct (eqx) * #s0 * whd in ⊢ (??%?→?); #Hs0
+      destruct (Hs0) #Htrue cases (Htrue (refl ??)) -Htrue
+      #xs0 * #ci * #rs' * #ls1 * #cj * #rs1 * * #Hxs #H destruct (H) #Hcicj
+      >Htc in IH; whd in ⊢ (%→?); >nth_change_vec_neq [|@sym_not_eq //]
+      #IH cases (IH … Hmidta_src) -IH #_ >nth_change_vec //
+      cut (∃x1,xs1.xs0@cj::rs1 = x1::xs1)
+      [ cases xs0 [ %{cj} %{rs1} % | #x1 #xs1 %{x1} %{(xs1@cj::rs1)} % ] ] * #x1 * #xs1
+      #Hxs1 >Hxs1 #IH cases (IH … (refl ??)) -IH
+      [ * #l * #l1 * #Hxs1'
+        >change_vec_commute // >change_vec_change_vec
+        #Houtc % %{(s0::l)} %{l1} % 
+        [ normalize <Hxs1' %
+        | >reverse_cons >associative_append >change_vec_commute // @Houtc ]
+      | #H %2 #l #l1 >(?:l@s0::xs@l1 = l@(s0::xs)@l1) [|%]
+        @not_sub_list_merge
+        [ #l2 >Hxs <Hxs1 % normalize #H1 lapply (cons_injective_r ????? H1)
+          >associative_append #H2 lapply (append_l2_injective ????? (refl ??) H2)
+          #H3 lapply (cons_injective_l ????? H3) #H3 >H3 in Hcicj; * /2/
+        |#t #l2 #l3 % normalize #H1 lapply (cons_injective_r ????? H1)
+         -H1 #H1 cases (H l2 l3) #H2 @H2 @H1
         ]
-      | #x0 #xs0 #Hnthi #Hnthj #Hnotendc #Hcicj
-        >(IH2 (c0::ls) x0 xs0 ci rs (c0::ls0) cj rs0 … Hcicj)
-        [ >Hd >change_vec_commute in ⊢ (??%?); //
-          >change_vec_change_vec >change_vec_commute in ⊢ (??%?); //
-          @sym_not_eq //
-        | #c1 #Hc1 @Hnotendc @memb_cons @Hc1
-        | >Hd >nth_change_vec // >Hnthj normalize
-          >Hnthi in Hci;normalize #H destruct (H) %
-        | >Hd >nth_change_vec_neq [|@sym_not_eq //] >Hnthi
-          >nth_change_vec // normalize
-          >Hnthi in Hci;normalize #H destruct (H) %
+      ]
+    | #_ cases (IH x xs ?) -IH
+      [| >Htc >nth_change_vec_neq [|@sym_not_eq //] @Hmidta_src ]
+      >Htc >nth_change_vec // cases rs0
+      [ #_ #_ %2 #l #l1 cases l
+       [ normalize cases xs
+         [ cases l1
+           [ normalize % #H destruct (H) cases (\Pf eqx) /2/
+           | #tmp1 #l2 normalize % #H destruct (H) ]
+         | #tmp1 #l2 normalize % #H destruct (H) ]
+       | #tmp1 #l2 normalize % #H destruct (H)cases l2 in e0;
+         [ normalize #H1 destruct (H1)
+         | #tmp2 #l3 normalize #H1 destruct (H1) ] ]
+      | #r1 #rs1 #_ #IH cases (IH … (refl ??)) -IH
+        [ * #l * #l1 * #Hll1 #Houtc % %{(c::l)} %{l1} % [ >Hll1 % ]
+          >Houtc >change_vec_commute // >change_vec_change_vec 
+          >change_vec_commute [|@sym_not_eq //]
+          >reverse_cons >associative_append %
+        | #Hll1 %2 @(not_sub_list_merge_2 ?? (x::xs)) normalize [|@Hll1]
+         #l1 % #H destruct (H) cases (\Pf eqx) /2/
         ]
-]]]
-qed.      
-lemma terminate_compare :  ∀i,j,sig,n,is_endc,t.
-  i ≠ j → i < S n → j < S n → 
-  compare i j sig n is_endc ↓ t.
-#i #j #sig #n #is_endc #t #Hneq #Hi #Hj
-@(terminate_while … (sem_comp_step …)) //
-<(change_vec_same … t i (niltape ?))
-cases (nth i (tape sig) t (niltape ?))
-[ % #t1 * #x * * * #_ >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct 
-|2,3: #a0 #al0 % #t1 * #x * * * #_ >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 * * * #Hendcx >nth_change_vec // normalize in ⊢ (%→?);
-   #H1 destruct (H1) #Hxsep >change_vec_change_vec #Ht1 % 
-   #t2 * #x0 * * * #Hendcx0 >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
-   >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H)
-  |#r0 #rs0 #IH #t #ls #c % #t1 * #x * * >nth_change_vec //
-   normalize in ⊢ (%→?); #H destruct (H) #Hcur
-   >change_vec_change_vec >change_vec_commute // #Ht1 >Ht1 @IH
+      ]
+    ]
   ]
 ]
 qed.
 
-lemma sem_compare : ∀i,j,sig,n,is_endc.
-  i ≠ j → i < S n → j < S n → 
-  compare i j sig n is_endc ⊨ R_compare i j sig n is_endc.
-#i #j #sig #n #is_endc #Hneq #Hi #Hj @WRealize_to_Realize /2/
-qed.
+axiom daemon : ∀P:Prop.P.
 
-(*
-   |conf1   $
-   |confin 0/1 confout move
+(* XXX: move to turing (or mono) *)
+definition option_cons ≝ λsig.λc:option sig.λl.
+  match c with [ None ⇒ l | Some c0 ⇒ c0::l ].
 
-  match machine step ≝
-    compare;
-    if (cur(src) != $)
-      then
-        parmoveL;
-        moveR(dst);
-      else nop
- *)
-
-definition match_step ≝ λsrc,dst,sig,n,is_startc,is_endc.
-  compare src dst sig n is_endc ·
-    (ifTM ?? (inject_TM ? (test_char ? (λa.is_endc a == false)) n src)
-      (single_finalTM ??
-        (parmove src dst sig n L is_startc · (inject_TM ? (move_r ?) n dst)))
-      (nop …)
-      tc_true).
-      
-definition Rtc_multi_true ≝ 
-  λalpha,test,n,i.λt1,t2:Vector ? (S n).
-   (∃c. current alpha (nth i ? t1 (niltape ?)) = Some ? c ∧ test c = true) ∧ t2 = t1.
-   
-definition Rtc_multi_false ≝ 
-  λalpha,test,n,i.λt1,t2:Vector ? (S n).
-    (∀c. current alpha (nth i ? t1 (niltape ?)) = Some ? c → test c = false) ∧ t2 = t1.
-   
-definition R_match_step_false ≝  
-  λsrc,dst,sig,n,is_endc.λint,outt: Vector (tape sig) (S n).
-   (((∃x.current ? (nth src ? int (niltape ?)) = Some ? x ∧ is_endc x = true) ∨
-     (* current ? (nth src ? int (niltape ?)) ≠ current ? (nth dst ? int (niltape ?)) ∨ *)   
-     current sig (nth src (tape sig) int (niltape sig)) = None ? ∨
-     current sig (nth dst (tape sig) int (niltape sig)) = None ? ) ∧ outt = int) ∨
-   ∃ls,ls0,rs,rs0,x,xs. ∀rsi,rsj,end,c. 
-    rs = end::rsi → rs0 = c::rsj →
-    (∀c0. memb ? c0 (x::xs) = true → is_endc c0 = false) ∧ is_endc end = true ∧
-    nth src ? int (niltape ?) = midtape sig ls x (xs@rs) ∧
-    nth dst ? int (niltape ?) = midtape sig ls0 x (xs@rs0) ∧
-    outt = change_vec ??
-           (change_vec ?? int (midtape sig (reverse ? xs@x::ls) end rsi) src)
-           (midtape sig (reverse ? xs@x::ls0) c rsj) dst.
+definition R_match_step_true_naive ≝ 
+  λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n).
+  |left ? (nth src ? outt (niltape ?))| +
+  |option_cons ? (current ? (nth dst ? outt (niltape ?))) (right ? (nth dst ? outt (niltape ?)))| <
+  |left ? (nth src ? int (niltape ?))| +
+  |option_cons ? (current ? (nth dst ? int (niltape ?))) (right ? (nth dst ? int (niltape ?)))|.
 
-definition R_match_step_true ≝ 
-  λsrc,dst,sig,n,is_startc,is_endc.λint,outt: Vector (tape sig) (S n).
-  ∀s.current sig (nth src (tape sig) int (niltape sig)) = Some ? s → 
-  is_startc s = true → 
-  (∀c.c ∈ right ? (nth src (tape sig) int (niltape sig)) = true → is_startc c = false) →
-  (∀s1.current sig (nth dst (tape sig) int (niltape sig)) = Some ? s1 → s ≠ s1 →  
-   outt = change_vec ?? int 
-          (tape_move … (nth dst ? int (niltape ?)) (Some ? 〈s1,R〉)) dst ∧ is_endc s = false) ∧  
-  (∀ls,x,xs,ci,rs,ls0,cj,rs0. 
-    nth src ? int (niltape ?) = midtape sig ls x (xs@ci::rs) →
-    nth dst ? int (niltape ?) = midtape sig ls0 x (xs@cj::rs0) → ci ≠ cj → 
-    (∀c0. memb ? c0 (x::xs) = true → is_endc c0 = false) → 
-    outt = change_vec ?? int 
-           (tape_move … (nth dst ? int (niltape ?)) (Some ? 〈x,R〉)) dst ∧ is_endc ci = false).
-    
-lemma sem_test_char_multi :
-  ∀alpha,test,n,i.i ≤ n → 
-  inject_TM ? (test_char ? test) n i ⊨ 
-  [ tc_true : Rtc_multi_true alpha test n i, Rtc_multi_false alpha test n i ].
-#alpha #test #n #i #Hin #int
-cases (acc_sem_inject … Hin (sem_test_char alpha test) int)
-#k * #outc * * #Hloop #Htrue #Hfalse %{k} %{outc} % [ %
-[ @Hloop
-| #Hqtrue lapply (Htrue Hqtrue) * * * #c *
-  #Hcur #Htestc #Hnth_i #Hnth_j %
-  [ %{c} % //
-  | @(eq_vec … (niltape ?)) #i0 #Hi0
-    cases (decidable_eq_nat i0 i) #Hi0i
-    [ >Hi0i @Hnth_i
-    | @sym_eq @Hnth_j @sym_not_eq // ] ] ]
-| #Hqfalse lapply (Hfalse Hqfalse) * * #Htestc #Hnth_i #Hnth_j %
-  [ @Htestc
-  | @(eq_vec … (niltape ?)) #i0 #Hi0
-    cases (decidable_eq_nat i0 i) #Hi0i
-    [ >Hi0i @Hnth_i
-    | @sym_eq @Hnth_j @sym_not_eq // ] ] ]
-qed.
-
-axiom comp_list: ∀S:DeqSet. ∀l1,l2:list S.∀is_endc. ∃l,tl1,tl2. 
-  l1 = l@tl1 ∧ l2 = l@tl2 ∧ (∀c.c ∈ l = true → is_endc c = false) ∧
-  ∀a,tla. tl1 = a::tla → is_endc a = true ∨ (∀b,tlb.tl2 = b::tlb → a≠b).
+axiom right_mk_tape : ∀sig,ls,c,rs.right ? (mk_tape sig ls c rs) = rs.
+axiom left_mk_tape : ∀sig,ls,c,rs.left ? (mk_tape sig ls c rs) = ls.
+axiom current_mk_tape : ∀sig,ls,c,rs.current ? (mk_tape sig ls c rs) = c.
+axiom length_tail : ∀A,l.0 < |l| → |tail A l| < |l|.
+axiom lists_length_split : 
+ ∀A.∀l1,l2:list A.(∃la,lb.(|la| = |l1| ∧ l2 = la@lb) ∨ (|la| = |l2| ∧ l1 = la@lb)).
+axiom opt_cons_tail_expand : ∀A,l.l = option_cons A (option_hd ? l) (tail ? l).
   
-axiom daemon : ∀X:Prop.X.
-
-lemma sem_match_step :
-  ∀src,dst,sig,n,is_startc,is_endc.src ≠ dst → src < S n → dst < S n → 
-  match_step src dst sig n is_startc is_endc ⊨ 
+lemma sem_match_step_termination :
+  ∀src,dst,sig,n.src ≠ dst → src < S n → dst < S n → 
+  match_step src dst sig n ⊨ 
     [ inr ?? (inr ?? (inl … (inr ?? start_nop))) : 
-      R_match_step_true src dst sig n is_startc is_endc
-      R_match_step_false src dst sig n is_endc ].
-#src #dst #sig #n #is_startc #is_endc #Hneq #Hsrc #Hdst 
-@(acc_sem_seq_app sig n … (sem_compare src dst sig n is_endc Hneq Hsrc Hdst)
-    (acc_sem_if ? n … (sem_test_char_multi sig (λa.is_endc a == false) n src (le_S_S_to_le … Hsrc))
+      R_match_step_true_naive src dst sig n
+      R_match_step_false src dst sig n ].
+#src #dst #sig #n #Hneq #Hsrc #Hdst 
+@(acc_sem_seq_app sig n … (sem_compare src dst sig n Hneq Hsrc Hdst)
+    (acc_sem_if ? n … (sem_partest sig n (match_test src dst sig ?))
       (sem_seq … 
-        (sem_parmoveL ???? is_startc Hneq Hsrc Hdst) 
+        (sem_rewind_strong ???? Hneq Hsrc Hdst) 
         (sem_inject … dst (le_S_S_to_le … Hdst) (sem_move_r ? )))
       (sem_nop …)))
-[#ta #tb #tc * #Hcomp1 #Hcomp2 * #td * * * #c * #Hcurtc #Hcend #Htd >Htd -Htd
- #Htb #s #Hcurta_src #Hstart #Hnotstart %
- [ #s1 #Hcurta_dst #Hneqss1
-   lapply Htb lapply Hcurtc -Htb -Hcurtc >(?:tc=ta) 
-   [|@Hcomp1 %2 % % >Hcurta_src >Hcurta_dst @(not_to_not … Hneqss1) #H destruct (H) % ]
-   #Hcurtc * #te * * #_ #Hte >Hte // whd in ⊢ (%→?); * * #_ #Htbdst #Htbelse %
-   [ @(eq_vec … (niltape ?)) #i #Hi cases (decidable_eq_nat i dst) #Hidst
-     [ >Hidst >nth_change_vec // cases (current_to_midtape … Hcurta_dst)
-       #ls * #rs #Hta_mid >(Htbdst … Hta_mid) >Hta_mid cases rs //
-     | >nth_change_vec_neq [|@sym_not_eq //] @sym_eq @Htbelse @sym_not_eq // ]
-   | >Hcurtc in Hcurta_src; #H destruct (H) cases (is_endc s) in Hcend;
-     normalize #H destruct (H) // ]
- |#ls #x #xs #ci #rs #ls0 #cj #rs0 #Htasrc_mid #Htadst_mid #Hcicj #Hnotendc
-  lapply (Hcomp2 … Htasrc_mid Htadst_mid Hnotendc (or_intror ?? Hcicj))
-  -Hcomp2 #Hcomp2
-  cases Htb #td * * #Htd #_ >Htasrc_mid in Hcurta_src; normalize in ⊢ (%→?);
-  #H destruct (H)
-  >(Htd ls ci (reverse ? xs) rs s ??? ls0 cj (reverse ? xs) s rs0 (refl ??)) //
-  [| >Hcomp2 >nth_change_vec //
-   | #c0 #Hc0 @(Hnotstart c0) >Htasrc_mid 
-     cases (orb_true_l … Hc0) -Hc0 #Hc0
-    [@memb_append_l2 >(\P Hc0) @memb_hd
-    |@memb_append_l1 <(reverse_reverse …xs) @memb_reverse //
-    ]
-   | >Hcomp2 >nth_change_vec_neq [|@sym_not_eq // ] @nth_change_vec // ]
-  * * #_ #Htbdst #Htbelse %
-  [ @(eq_vec … (niltape ?)) #i #Hi cases (decidable_eq_nat i dst) #Hidst
-     [ >Hidst >nth_change_vec // >Htadst_mid >(Htbdst ls0 s (xs@cj::rs0))
-       [ cases xs //
-       | >nth_change_vec // ]
-     | >nth_change_vec_neq [|@sym_not_eq //]
-       <Htbelse [|@sym_not_eq // ]
-       >nth_change_vec_neq [|@sym_not_eq //]
-        (* STOP. *)
-       cases (decidable_eq_nat i src) #Hisrc
-       [ >Hisrc >nth_change_vec // >Htasrc_mid //
-       | >nth_change_vec_neq [|@sym_not_eq //]
-         <(Htbelse i) [|@sym_not_eq // ]
-         >Hcomp2 >nth_change_vec_neq [|@sym_not_eq // ]
-         >nth_change_vec_neq [|@sym_not_eq // ] //
-       ]
-     ]
-  | >Hcomp2 in Hcurtc; >nth_change_vec_neq [|@sym_not_eq //] 
-     >nth_change_vec // whd in ⊢ (??%?→?); 
-     #H destruct (H) cases (is_endc c) in Hcend;
-     normalize #H destruct (H) // ]
-  ]
-|#intape #outtape #ta * #Hcomp1 #Hcomp2 * #tb * * #Hc #Htb 
- whd in ⊢ (%→?); #Hout >Hout >Htb whd
- lapply (current_to_midtape sig (nth src ? intape (niltape ?)))
- cases (current … (nth src ? intape (niltape ?))) in Hcomp1; 
-  [#Hcomp1 #_ %1 % [%1 %2 // | @Hcomp1 %2 %1 %2 %]
-  |#c_src lapply (current_to_midtape sig (nth dst ? intape (niltape ?)))
-   cases (current … (nth dst ? intape (niltape ?))) 
-    [#_ #Hcomp1 #_ %1 % [%2 % | @Hcomp1 %2 % % % #H destruct (H)]
-    |#c_dst cases (true_or_false (c_src == c_dst)) #Hceq
-      [#Hmid_dst cases (Hmid_dst c_dst (refl …)) -Hmid_dst
-       #ls_dst * #rs_dst #Hmid_dst #Hcomp1
-       #Hmid_src cases (Hmid_src c_src (refl …)) -Hmid_src
-       #ls_src * #rs_src #Hmid_src
-       cases (true_or_false (is_endc c_src)) #Hc_src
-       [ % % [ % % %{c_src} % // | @Hcomp1 % %{c_src} % // ]
-       | %2 cases (comp_list … rs_src rs_dst is_endc) #xs * #rsi * #rsj * * * 
-         #Hrs_src #Hrs_dst #Hnotendc #Hneq    
-         %{ls_src} %{ls_dst} %{rsi} %{rsj} %{c_src} %{xs} 
-         #rsi0 #rsj0 #end #c #Hend #Hc_dst
-         >Hrs_src in Hmid_src; >Hend #Hmid_src
-         >Hrs_dst in Hmid_dst; >Hc_dst <(\P Hceq) #Hmid_dst
-         cut (is_endc end = true ∨ end ≠ c)
-         [cases (Hneq … Hend) /2/ -Hneq #Hneq %2 @(Hneq … Hc_dst) ] #Hneq
-         lapply (Hcomp2 … Hmid_src Hmid_dst ? Hneq)
-          [#c0 #Hc0 cases (orb_true_l … Hc0) -Hc0 #Hc0
-            [ >(\P Hc0) //
-            | @Hnotendc // ] 
+[ #ta #tb #tc * lapply (refl ? (current ? (nth src ? ta (niltape ?))))
+  cases (current ? (nth src ? ta (niltape ?))) in ⊢ (???%→%);
+  [ #Hcurta_src #Hcomp #_ * #td * >Hcomp [| % %2 %]
+    whd in ⊢ (%→?); * whd in ⊢ (??%?→?); 
+    >nth_current_chars >Hcurta_src normalize in ⊢ (%→?); #H destruct (H)
+  | #s #Hs lapply (refl ? (current ? (nth dst ? ta (niltape ?))))
+    cases (current ? (nth dst ? ta (niltape ?))) in ⊢ (???%→%);
+    [ #Hcurta_dst #Hcomp #_ * #td * >Hcomp [| %2 %]
+      whd in ⊢ (%→?); * whd in ⊢ (??%?→?); 
+      >nth_current_chars >nth_current_chars >Hs >Hcurta_dst 
+      normalize in ⊢ (%→?); #H destruct (H)
+    | #s0 #Hs0
+      cases (current_to_midtape … Hs) #ls * #rs #Hmidta_src >Hmidta_src
+      cases (current_to_midtape … Hs0) #ls0 * #rs0 #Hmidta_dst >Hmidta_dst
+      cases (true_or_false (s == s0)) #Hss0
+      [ lapply (\P Hss0) -Hss0 #Hss0 destruct (Hss0) 
+        #_ #Hcomp cases (Hcomp ????? (refl ??) (refl ??)) -Hcomp [ *
+        [ * #rs' * #_ #Hcurtc_dst * #td * whd in ⊢ (%→?); * whd in ⊢ (??%?→?);
+          >nth_current_chars >nth_current_chars >Hcurtc_dst 
+          cases (current ? (nth src …)) 
+          [normalize in ⊢ (%→?); #H destruct (H)
+          | #x >nth_change_vec // cases (reverse ? rs0)
+            [ normalize in ⊢ (%→?); #H destruct (H)
+            | #r1 #rs1 normalize in ⊢ (%→?); #H destruct (H) ] ]
+        | * #rs0' * #_ #Hcurtc_src * #td * whd in ⊢ (%→?); * whd in ⊢ (??%?→?);
+          >(?:nth src ? (current_chars ?? tc) (None ?) = None ?) 
+          [|>nth_current_chars >Hcurtc_src >nth_change_vec_neq 
+            [>nth_change_vec [cases (append ???) // | @Hsrc] 
+            |@(not_to_not … Hneq) //
+            ]]
+          normalize in ⊢ (%→?); #H destruct (H) ]
+        | * #xs * #ci * #cj * #rs'' * #rs0' * * * #Hcicj #Hrs #Hrs0
+          #Htc * #td * * #Hmatch #Htd destruct (Htd) * #te * * *
+          >Htc >change_vec_commute // >nth_change_vec //
+          >change_vec_commute [|@sym_not_eq //] >nth_change_vec //
+          cases (lists_length_split ? ls ls0) #lsa * #lsb * * #Hlen #Hlsalsb
+          destruct (Hlsalsb)  *
+          [ #Hte #_ #_ <(reverse_reverse … ls) in Hte; <(reverse_reverse … lsa)
+            cut (|reverse ? lsa| = |reverse ? ls|) [ // ] #Hlen' 
+            @(list_cases2 … Hlen')
+            [ #H1 #H2 >H1 >H2 -H1 -H2 normalize in match (reverse ? [ ]); #Hte #_
+              lapply (Hte … (refl ??) … (refl ??) (refl ??)) -Hte
+              >change_vec_commute // >change_vec_change_vec
+              >change_vec_commute [|@sym_not_eq //] >change_vec_change_vec #Hte
+              >Hte * * #_ >nth_change_vec // >reverse_reverse 
+              #H lapply (H … (refl ??)) -H #Htb1 #Htb2
+              cut (tb = change_vec ?? (change_vec (tape sig) (S n) ta (midtape sig [] s0 (xs@ci::rs'')) src) (mk_tape sig (s0::lsb) (option_hd sig (xs@cj::rs0')) (tail sig (xs@cj::rs0'))) dst)
+              [@daemon] -Htb1 -Htb2 #Htb >Htb whd >nth_change_vec //
+              >nth_change_vec_neq [|@sym_not_eq //] >nth_change_vec //
+              >right_mk_tape normalize in match (left ??);
+              >Hmidta_src >Hmidta_dst >current_mk_tape <opt_cons_tail_expand
+              whd in match (option_cons ???); >Hrs0
+              normalize in ⊢ (?(?%)%); //
+            | #hda #hdb #tla #tlb #H1 #H2 >H1 >H2
+              >reverse_cons >reverse_cons #Hte
+              lapply (Hte ci hdb (reverse ? xs@s0::reverse ? tlb) rs'' ?
+                       lsb cj hda (reverse ? xs@s0::reverse ? tla) rs0' ??)
+              [ /2 by cons_injective_l, nil/
+              | >length_append >length_append @eq_f @(eq_f ?? S)
+                >H1 in Hlen'; >H2 whd in ⊢ (??%%→?); #Hlen' 
+                >length_reverse >length_reverse destruct (Hlen') //
+              | /2 by refl, trans_eq/ ] -Hte
+              #Hte #_ * * #_ >Hte >nth_change_vec // #Htb1 #Htb2
+              cut (tb = change_vec ?? (change_vec (tape sig) (S n) ta               
+                        (mk_tape sig (hda::lsb) (option_hd ? (reverse sig (reverse sig xs@s0::reverse sig tla)@cj::rs0')) (tail ? (reverse sig (reverse sig xs@s0::reverse sig tla)@cj::rs0'))) dst) 
+                        (midtape ? [ ] hdb (reverse sig (reverse sig xs@s0::reverse sig tlb)@ci::rs'')) src)
+              [@daemon] -Htb1 -Htb2 #Htb >Htb whd 
+              >nth_change_vec // >nth_change_vec_neq // >nth_change_vec //
+              >right_mk_tape >Hmidta_src >Hmidta_dst 
+              whd in match (left ??); whd in match (left ??); whd in match (right ??);
+              >current_mk_tape <opt_cons_tail_expand whd in match (option_cons ???);
+              >Hrs0 >length_append whd in ⊢ (??(??%)); >length_append >length_reverse
+              >length_append >commutative_plus in match (|reverse ??| + ?);
+              whd in match (|?::?|); >length_reverse >length_reverse
+              <(length_reverse ? ls) <Hlen' >H1 normalize // ]
+         | #_ #Hte #_ <(reverse_reverse … ls0) in Hte; <(reverse_reverse … lsa)
+            cut (|reverse ? lsa| = |reverse ? ls0|) [ // ] #Hlen' 
+            @(list_cases2 … Hlen')
+            [ #H1 #H2 >H1 >H2 normalize in match (reverse ? [ ]); #Hte
+              lapply (Hte … (refl ??) … (refl ??) (refl ??)) -Hte
+              >change_vec_change_vec >change_vec_commute [|@sym_not_eq //] 
+              >change_vec_change_vec #Hte #_
+              >Hte * * #_ >nth_change_vec // >reverse_reverse 
+              #H lapply (H … (refl ??)) -H #Htb1 #Htb2
+              cut (tb = change_vec ?? (change_vec (tape sig) (S n) ta (mk_tape ? [s0] (option_hd ? (xs@cj::rs0')) (tail ? (xs@cj::rs0'))) dst)
+                           (midtape ? lsb s0 (xs@ci::rs'')) src)
+              [@daemon] -Htb1 -Htb2 #Htb >Htb whd >nth_change_vec //
+              >nth_change_vec_neq // >nth_change_vec //
+              >right_mk_tape normalize in match (left ??);
+              >Hmidta_src >Hmidta_dst >current_mk_tape <opt_cons_tail_expand >Hrs0
+              >length_append normalize >length_append >length_append
+              <(reverse_reverse ? lsa) >H1 normalize //
+            | #hda #hdb #tla #tlb #H1 #H2 >H1 >H2
+              >reverse_cons >reverse_cons #Hte
+              lapply (Hte cj hdb (reverse ? xs@s0::reverse ? tlb) rs0' ?
+                       lsb ci hda (reverse ? xs@s0::reverse ? tla) rs'' ??)
+              [ /2 by cons_injective_l, nil/
+              | >length_append >length_append @eq_f @(eq_f ?? S)
+                >H1 in Hlen'; >H2 whd in ⊢ (??%%→?); #Hlen' 
+                >length_reverse >length_reverse destruct (Hlen') //
+              | /2 by refl, trans_eq/ ] -Hte
+              #Hte #_ * * #_ >Hte >nth_change_vec_neq // >nth_change_vec // #Htb1 #Htb2
+              cut (tb = change_vec ?? (change_vec (tape sig) (S n) ta               
+                        (mk_tape sig [hdb] (option_hd ? (reverse sig (reverse sig xs@s0::reverse sig tlb)@cj::rs0')) (tail ? (reverse sig (reverse sig xs@s0::reverse sig tlb)@cj::rs0'))) dst) 
+                        (midtape ? lsb hda (reverse sig (reverse sig xs@s0::reverse sig tla)@ci::rs'')) src)
+              [@daemon] -Htb1 -Htb2 #Htb >Htb whd 
+              >nth_change_vec // >nth_change_vec_neq // >nth_change_vec //
+              >right_mk_tape >Hmidta_src >Hmidta_dst 
+              whd in match (left ??); whd in match (left ??); whd in match (right ??);
+              >current_mk_tape <opt_cons_tail_expand
+              whd in match (option_cons ???);
+              >Hrs0 >length_append whd in ⊢ (??(??%)); >length_append >length_reverse
+              >length_append >commutative_plus in match (|reverse ??| + ?);
+              whd in match (|?::?|); >length_reverse >length_reverse
+              <(length_reverse ? lsa) >Hlen' >H2 >length_append
+              normalize //
+            ]
           ]
-         -Hcomp2 #Hcomp2 <Hcomp2
-         % // % [ % 
-         [>Hcomp2 in Hc; >nth_change_vec_neq [|@sym_not_eq //]
-          >nth_change_vec // #H lapply (H ? (refl …)) 
-          cases (is_endc end) [|normalize #H destruct (H) ]
-          #_ % // #c0 #Hc0 cases (orb_true_l … Hc0) -Hc0 #Hc0
-          [ >(\P Hc0) // | @Hnotendc // ]
-         |@Hmid_src]
-         |@Hmid_dst] ]
-      |#_ #Hcomp1 #Hsrc cases (Hsrc ? (refl ??)) -Hsrc #ls * #rs #Hsrc
-       %1 % 
-        [% % %{c_src} % // lapply (Hc c_src) -Hc >Hcomp1
-         [| %2 % % @(not_to_not ??? (\Pf Hceq)) #H destruct (H) // ]
-         cases (is_endc c_src) //
-         >Hsrc #Hc lapply (Hc (refl ??)) normalize #H destruct (H)
-        |@Hcomp1 %2 %1 %1 @(not_to_not ??? (\Pf Hceq)) #H destruct (H) //
         ]
-      ]
-    ]
-  ] 
+      | lapply (\Pf Hss0) -Hss0 #Hss0 #Htc cut (tc = ta) 
+        [@Htc % % @(not_to_not ??? Hss0) #H destruct (H) %]
+        -Htc #Htc destruct (Htc) #_ * #td * whd in ⊢ (%→?); * #_ 
+        #Htd destruct (Htd) * #te * * * * >Hmidta_src >Hmidta_dst 
+        cases (lists_length_split ? ls ls0) #lsa * #lsb * * #Hlen #Hlsalsb
+        destruct (Hlsalsb)
+        [ <(reverse_reverse … ls) <(reverse_reverse … lsa)
+          cut (|reverse ? lsa| = |reverse ? ls|) [ // ] #Hlen'
+          @(list_cases2 … Hlen')
+          [ #H1 #H2 >H1 >H2 -H1 -H2 #_ #_ normalize in match (reverse ? [ ]); #Hte #_
+            lapply (Hte … (refl ??) … (refl ??)) -Hte #Hte destruct (Hte) * * #_
+            >Hmidta_dst #Htb1 lapply (Htb1 … (refl ??)) -Htb1 #Htb1 #Htb2
+            cut (tb = change_vec ?? ta (mk_tape ? (s0::lsa@lsb) (option_hd ? rs0) (tail ? rs0)) dst)
+            [@daemon] -Htb1 -Htb2 #Htb >Htb whd >nth_change_vec //
+            >nth_change_vec_neq [|@sym_not_eq //] >Hmidta_src >Hmidta_dst
+            >right_mk_tape normalize in match (left ??); normalize in match (right ??);
+            >Hmidta_src >Hmidta_dst >current_mk_tape <opt_cons_tail_expand
+            normalize //
+          | #hda #hdb #tla #tlb #H1 #H2 >H1 >H2
+            >reverse_cons >reverse_cons >associative_append #Hte
+            lapply (Hte ???? (refl ??) ? s0 ? (reverse ? tla) ?? (refl ??))
+            [ >length_reverse >length_reverse cut (|hda::tla| = |hdb::tlb|) //
+              normalize #H destruct (H) // ] #Hte #_ #_ #_
+            * * #_ >Hte >nth_change_vec // #Htb1 #Htb2
+            cut (tb = change_vec ?? (change_vec (tape sig) (S n) ta               
+                      (mk_tape sig (hda::lsb) (option_hd ? (reverse sig (reverse sig tla)@s0::rs0)) (tail ? (reverse sig (reverse sig tla)@s0::rs0))) dst) 
+                      (midtape ? [ ] hdb (reverse sig (reverse sig tlb)@s::rs)) src)
+            [@daemon] -Htb1 -Htb2 #Htb >Htb whd 
+            >nth_change_vec // >nth_change_vec_neq // >nth_change_vec //
+            >right_mk_tape >Hmidta_src >Hmidta_dst 
+            whd in match (left ??); whd in match (left ??); whd in match (right ??);
+            >current_mk_tape <opt_cons_tail_expand >length_append
+            >length_reverse >length_reverse <(length_reverse ? ls) <Hlen'
+            >H1 normalize // ]
+       | #_ <(reverse_reverse … ls0) <(reverse_reverse … lsa)
+         cut (|reverse ? lsa| = |reverse ? ls0|) [ // ] #Hlen' 
+         @(list_cases2 … Hlen')
+         [ #H1 #H2 >H1 >H2 normalize in match (reverse ? [ ]); #_ #_ #Hte
+           lapply (Hte … (refl ??) … (refl ??)) -Hte #Hte destruct (Hte)
+           * * #_ >Hmidta_dst #Htb1 lapply (Htb1 … (refl ??)) -Htb1 #Htb1 #Htb2
+           cut (tb = change_vec (tape sig) (S n) ta (mk_tape ? (s0::ls0) (option_hd ? rs0) (tail ? rs0)) dst)
+           [@daemon] -Htb1 -Htb2 #Htb >Htb whd >nth_change_vec //
+           >nth_change_vec_neq [|@sym_not_eq //] >Hmidta_src >Hmidta_dst
+           >current_mk_tape >right_mk_tape normalize in ⊢ (??%); <opt_cons_tail_expand
+           normalize //
+         | #hda #hdb #tla #tlb #H1 #H2 >H1 >H2
+           >reverse_cons >reverse_cons #Hte #_ #_
+           lapply (Hte s0 hdb (reverse ? tlb) rs0 ?
+                    lsb s hda (reverse ? tla) rs ??)
+           [ /2 by cons_injective_l, nil/
+           | >length_reverse >length_reverse cut (|hda::tla| = |hdb::tlb|) //
+             normalize #H destruct (H) //
+           | /2 by refl, trans_eq/ ] -Hte
+           #Hte * * #_ >Hte >nth_change_vec_neq // >nth_change_vec // #Htb1 #Htb2
+           cut (tb = change_vec ?? (change_vec (tape sig) (S n) ta               
+                     (mk_tape sig [hdb] (option_hd ? (reverse sig (reverse sig tlb)@s0::rs0)) (tail ? (reverse sig (reverse sig tlb)@s0::rs0))) dst) 
+                     (midtape ? lsb hda (reverse sig (reverse sig tla)@s::rs)) src)
+           [@daemon] -Htb1 -Htb2 #Htb >Htb whd 
+           >nth_change_vec // >nth_change_vec_neq // >nth_change_vec //
+           >right_mk_tape >Hmidta_src >Hmidta_dst 
+           whd in match (left ??); whd in match (left ??); whd in match (right ??);
+           >current_mk_tape <opt_cons_tail_expand >length_append
+           normalize in ⊢ (??%); >length_append >reverse_reverse
+           <(length_reverse ? lsa) >Hlen' >H2 normalize //
+         ]
+       ]
+     ]
+   ]
+ ]
+| #ta #tb #tc * #Hcomp1 #Hcomp2 * #td * * #Htest #Htd destruct (Htd)
+  whd in ⊢ (%→?); #Htb destruct (Htb) #ls #x #xs #Hta_src
+  lapply (refl ? (current ? (nth dst ? ta (niltape ?))))
+  cases (current ? (nth dst ? ta (niltape ?))) in ⊢ (???%→?);
+  [ #Hcurta_dst % % % // @Hcomp1 %2 //
+  | #x0 #Hcurta_dst cases (current_to_midtape … Hcurta_dst) -Hcurta_dst
+    #ls0 * #rs0 #Hta_dst cases (true_or_false (x == x0)) #Hxx0
+    [ lapply (\P Hxx0) -Hxx0 #Hxx0 destruct (Hxx0)
+    | >(?:tc=ta) in Htest; 
+      [|@Hcomp1 % % >Hta_src >Hta_dst @(not_to_not ??? (\Pf Hxx0)) normalize
+        #Hxx0' destruct (Hxx0') % ]
+      whd in ⊢ (??%?→?); 
+      >nth_current_chars >Hta_src >nth_current_chars >Hta_dst 
+      whd in ⊢ (??%?→?); #Hfalse destruct (Hfalse) ] -Hcomp1
+      cases (Hcomp2 … Hta_src Hta_dst) [ *
+      [ * #rs' * #Hxs #Hcurtc % %2 %{ls0} %{rs0} %{rs'} %
+        [ % // | >Hcurtc % ]
+      | * #rs0' * #Hxs #Htc %2 >Htc %{ls0} %{rs0'} % // ]
+      | * #xs0 * #ci * #cj * #rs' * #rs0' * * *
+        #Hci #Hxs #Hrs0 #Htc @False_ind
+        whd in Htest:(??%?); 
+        >(?:nth src ? (current_chars ?? tc) (None ?) = Some ? ci) in Htest; 
+        [|>nth_current_chars >Htc >nth_change_vec_neq [|@(not_to_not … Hneq) //]
+          >nth_change_vec //]
+        >(?:nth dst ? (current_chars ?? tc) (None ?) = Some ? cj) 
+        [|>nth_current_chars >Htc >nth_change_vec //]
+        normalize #H destruct (H) ] ] ]
 qed.
 
-definition match_m ≝ λsrc,dst,sig,n,is_startc,is_endc.
-  whileTM … (match_step src dst sig n is_startc is_endc) 
-    (inr ?? (inr ?? (inl … (inr ?? start_nop)))).
+axiom terminate_match_m :
+  ∀src,dst,sig,n,t.
+  src ≠ dst → src < S n → dst < S n → 
+  match_m src dst sig n ↓ t.
+(*#src #dst #sig #n #ta #Hneq #Hsrc #Hdst
+@(terminate_while … (sem_match_step_termination src dst sig n Hneq Hsrc Hdst)) // % #tb
+letin f ≝ (λt0:Vector (tape sig) (S n).|left ? (nth src (tape ?) t0 (niltape ?))|
+    +|option_cons ? (current ? (nth dst (tape ?) t0 (niltape ?)))
+      (right ? (nth dst (tape ?) t0 (niltape ?)))|)
+change with (f tb < f ta) in ⊢ (%→?); @(nat_elim1 (f tb)) 
+#x lapply (refl ? x) cases x in ⊢ (???%→%);
+[ #Hx 
+*
+#x #IH #Hx cases 
 
-definition R_match_m ≝ 
-  λi,j,sig,n,is_startc,is_endc.λint,outt: Vector (tape sig) (S n).
-  (((∃x.current ? (nth i ? int (niltape ?)) = Some ? x ∧ is_endc x = true) ∨
-    current ? (nth i ? int (niltape ?)) = None ? ∨
-    current ? (nth j ? int (niltape ?)) = None ?) → outt = int) ∧
-  (∀ls,x,xs,ci,rs,ls0,x0,rs0. 
-    is_startc x = true → is_endc ci = true → 
-    nth i ? int (niltape ?) = midtape sig ls x (xs@ci::rs) →
-    nth j ? int (niltape ?) = midtape sig ls0 x0 rs0 →
-    ∃l,cj,l1.x0::rs0 = l@x::xs@cj::l1 ∧
-    outt = change_vec ?? 
-           (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) i)
-           (midtape sig ((reverse ? (l@x::xs))@ls0) cj l1) j).
+ @IH % #tc change with (f tc < f tb) in ⊢ (%→?);
+      
+       )(|left @(nat_elim1 (|left ? (nth ? (tape ?) t (niltape ?))|
+    +|option_cons sig (current ? (nth dst (tape ?) t (niltape ?)))
+      (right ? (nth dst (tape ?) t (niltape ?)))|))
+<(change_vec_same … t dst (niltape ?))
+<(change_vec_same … t src (niltape ?)) in ⊢ (???(???%??));
+lapply (refl ? (nth dst (tape sig) t (niltape ?))) 
+cases (nth dst (tape sig) t (niltape ?)) in ⊢ (???%→?);
+[ #Htape_dst % #t1 whd in ⊢ (%→?); >nth_change_vec_neq [|@sym_not_eq //]
+  >Hmid_src #HR cases (HR ?? (refl ??)) -HR
+  >nth_change_vec // >Htape_dst #_ * #s0 * normalize in ⊢ (%→?); #H destruct (H)
+| #x0 #xs0 #Htape_dst % #t1 whd in ⊢ (%→?); >nth_change_vec_neq [|@sym_not_eq //]
+  >Hmid_src #HR cases (HR ?? (refl ??)) -HR
+  >nth_change_vec // >Htape_dst #_ normalize in ⊢ (%→?);
+  * #s0 * #H destruct (H)
+| #x0 #xs0 #Htape_dst % #t1 whd in ⊢ (%→?); >nth_change_vec_neq [|@sym_not_eq //]
+  >Hmid_src #HR cases (HR ?? (refl ??)) -HR
+  >nth_change_vec // >Htape_dst #_ normalize in ⊢ (%→?);
+  * #s0 * #H destruct (H)
+| #ls #s #rs lapply s -s lapply ls -ls lapply Hmid_src lapply t -t elim rs
+  [#t #Hmid_src #ls #s #Hmid_dst % #t1 whd in ⊢ (%→?); >nth_change_vec_neq [|@sym_not_eq //]
+   >Hmid_src >nth_change_vec // >Hmid_dst #HR cases (HR ?? (refl ??)) -HR 
+   >change_vec_change_vec #Ht1 #_ % #t2 whd in ⊢ (%→?);
+   >Ht1 >nth_change_vec_neq [|@sym_not_eq //] >Hmid_src #HR
+   cases (HR ?? (refl ??)) -HR #_
+   >nth_change_vec // * #s1 * normalize in ⊢ (%→?); #H destruct (H)
+  |#r0 #rs0 #IH #t #Hmid_src #ls #s #Hmid_dst % #t1 whd in ⊢ (%→?);
+   >nth_change_vec_neq [|@sym_not_eq //] >Hmid_src
+   #Htrue cases (Htrue ?? (refl ??)) -Htrue >change_vec_change_vec
+   >nth_change_vec // >Hmid_dst whd in match (tape_move_mono ???); #Ht1
+   * #s0 * whd in ⊢ (??%?→?); #H destruct (H) #_ >Ht1
+   lapply (IH t1 ? (s0::ls) r0 ?) 
+   [ >Ht1 >nth_change_vec //
+   | >Ht1 >nth_change_vec_neq [|@sym_not_eq //] @Hmid_src
+   | >Ht1 >nth_change_vec // ]
+ ]
+]
+qed. *)
 
-lemma wsem_match_m : ∀src,dst,sig,n,is_startc,is_endc.
+lemma sem_match_m : ∀src,dst,sig,n.
 src ≠ dst → src < S n → dst < S n → 
-  match_m src dst sig n is_startc is_endc ⊫ R_match_m src dst sig n is_startc is_endc.
-#src #dst #sig #n #is_startc #is_endc #Hneq #Hsrc #Hdst #ta #k #outc #Hloop
-lapply (sem_while … (sem_match_step src dst sig n is_startc is_endc Hneq Hsrc Hdst) … Hloop) //
--Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
-[ #tc whd in ⊢ (%→?); * 
-  [ * * [ *
-    [ * #cur_src * #H1 #H2 #Houtc %   
-      [ #_ @Houtc
-      | #ls #x #xs #ci #rs #ls0 #cj #rs0 #_ #_ #Hnthi #Hnthj
-        >Hnthi in Hcicj; >Hnthj normalize in ⊢ (%→?); * #H @False_ind @H %
-      ]
-  | #Hci #Houtc %
-    [ #_ @Houtc
-    | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hnthi >Hnthi in Hci;
-      normalize in ⊢ (%→?); #H destruct (H) ] ]
-  | #Hcj #Houtc %
-    [ #_ @Houtc
-    | #ls #x #xs #ci #rs #ls0 #cj #rs0 #_ #Hnthj >Hnthj in Hcj;
-      normalize in ⊢ (%→?); #H destruct (H) ] ]
-
-
-
-[ #tc whd in ⊢ (%→?); * * [ *
-
+  match_m src dst sig n \vDash R_match_m src dst sig n.
+#src #dst #sig #n #Hneq #Hsrc #Hdst @WRealize_to_Realize [/2/| @wsem_match_m // ]
+qed.