]> matita.cs.unibo.it Git - helm.git/blobdiff - matita/matita/lib/turing/multi_universal/match.ma
work in progress
[helm.git] / matita / matita / lib / turing / multi_universal / match.ma
index 5e1b501ed7476bea4ab351a6bb62328c9ac5c821..8f525c9f4f7188df65acfeb91114d4d1b072b18c 100644 (file)
@@ -33,7 +33,7 @@ definition comp2 : compare_states ≝ mk_Sig ?? 2 (leb_true_to_le 3 3 (refl …)
 *)
 
 definition trans_compare_step ≝ 
- λi,j.λsig:FinSet.λn.
+ λi,j.λsig:FinSet.λn.λis_endc.
  λp:compare_states × (Vector (option sig) (S n)).
  let 〈q,a〉 ≝ p in
  match pi1 … q with
@@ -41,7 +41,7 @@ definition trans_compare_step ≝
    [ None ⇒ 〈comp2,null_action ? n〉
    | Some ai ⇒ match nth j ? a (None ?) with 
      [ None ⇒ 〈comp2,null_action ? n〉
-     | Some aj ⇒ if ai == aj 
+     | 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〉
@@ -52,13 +52,14 @@ definition trans_compare_step ≝
    | S _ ⇒ (* 2 *) 〈comp2,null_action ? n〉 ] ].
 
 definition compare_step ≝ 
-  λi,j,sig,n.
-  mk_mTM sig n compare_states (trans_compare_step i j sig n) 
+  λ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).
 
 definition R_comp_step_true ≝ 
-  λi,j,sig,n.λint,outt: Vector (tape sig) (S n).
+  λ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 ?? 
@@ -67,18 +68,19 @@ definition R_comp_step_true ≝
             (tape_move ? (nth j ? int (niltape ?)) (Some ? 〈x,R〉)) j.
 
 definition R_comp_step_false ≝ 
-  λi,j:nat.λsig,n.λint,outt: Vector (tape sig) (S n).
-  (current ? (nth i ? int (niltape ?)) ≠ current ? (nth j ? int (niltape ?)) ∨
+  λ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.
 
 lemma comp_q0_q2_null :
-  ∀i,j,sig,n,v.i < S n → j < S n → 
+  ∀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) (mk_mconfig ??? comp0 v) 
+  step sig n (compare_step i j sig n is_endc) (mk_mconfig ??? comp0 v) 
   = mk_mconfig ??? comp2 v.
-#i #j #sig #n #v #Hi #Hj
+#i #j #sig #n #is_endc #v #Hi #Hj
 whd in ⊢ (? → ??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (?→??%?);
 * #Hcurrent
 [ @eq_f2
@@ -91,42 +93,57 @@ whd in ⊢ (? → ??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (?→??%
 qed.
 
 lemma comp_q0_q2_neq :
-  ∀i,j,sig,n,v.i < S n → j < S n → 
-  nth i ? (current_chars ?? v) (None ?) ≠ nth j ? (current_chars ?? v) (None ?) → 
-  step sig n (compare_step i j sig n) (mk_mconfig ??? comp0 v) 
+  ∀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 #v #Hi #Hj lapply (refl ? (nth i ?(current_chars ?? v)(None ?)))
+#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 #Hneq
-    whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
-    [ whd in match (trans ????); >Hai >Haj
-      whd in ⊢ (??(???%)?); >(\bf ?) // @(not_to_not … Hneq) //
-    | whd in match (trans ????); >Hai >Haj
-      whd in ⊢ (??(???????(???%))?); >(\bf ?) /2 by not_to_not/
-      @tape_move_null_action
-] ]
+  | #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
+          ]
+        ]
+      ]
+    ]
+]
 qed.
 
 lemma comp_q0_q1 :
-  ∀i,j,sig,n,v,a.i ≠ j → i < S n → j < S n → 
-  nth i ? (current_chars ?? v) (None ?) = Some ? a →
+  ∀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) (mk_mconfig ??? comp0 v) =
+  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 #v #a #Heq #Hi #Hj #Ha1 #Ha2
+#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 ⊢ (??(???%)?); >(\b ?) //
+  >Ha1 >Ha2 whd in ⊢ (??(???%)?); >Hnotendc >(\b ?) //
 | whd in match (trans ????);
-  >Ha1 >Ha2 whd in ⊢ (??(???????(???%))?); >(\b ?) //
+  >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 ⊢ (??%?);
@@ -136,11 +153,11 @@ whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
 qed.
 
 lemma sem_comp_step :
-  ∀i,j,sig,n.i ≠ j → i < S n → j < S n → 
-  compare_step i j sig n ⊨ 
-    [ comp1: R_comp_step_true i j sig n, 
-             R_comp_step_false i j sig n ].
-#i #j #sig #n #Hneq #Hi #Hj #int
+  ∀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} %
@@ -156,56 +173,80 @@ cases (current ? (nth i ? int (niltape ?))) in ⊢ (???%→?);
        [ whd in ⊢ (??%?); >comp_q0_q2_null /2/ %2 <Hcurj in ⊢ (???%); 
          @sym_eq @nth_vec_map
        | normalize in ⊢ (%→?); #H destruct (H) ]
-       | #_ % >Ha >Hcurj % % % #H destruct (H) ] ]
-  | #b #Hb %{2} cases (true_or_false (a == b)) #Hab
+       | #_ % // >Ha >Hcurj % % %2 % #H destruct (H) ] ]
+  | #b #Hb %{2} 
+   cases (true_or_false (is_endc a)) #Haendc
     [ %
-      [| % [ % 
-        [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 ?)) >Ha >Hb
-         @(not_to_not ??? (\Pf Hab)) #H destruct (H) %
+         % %{a} % // <Ha @sym_eq @nth_vec_map
         | normalize in ⊢ (%→?); #H destruct (H) ]
-      | #_ % // % % >Ha >Hb @(not_to_not ??? (\Pf Hab)) #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) % ] ]
+      ]
     ]
   ]
 ]
 qed.
 
-definition compare ≝ λi,j,sig,n.
-  whileTM … (compare_step i j sig n) comp1.
+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.λint,outt: Vector (tape sig) (S n).
-  ((current ? (nth i ? int (niltape ?)) 
-    ≠ current ? (nth j ? int (niltape ?)) ∨
+  λ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. 
+  (∀ls,x,xs,ci,rs,ls0,rs0. 
     nth i ? int (niltape ?) = midtape sig ls x (xs@ci::rs) →
-    nth j ? int (niltape ?) = midtape sig ls0 x (xs@cj::rs0) → ci ≠ cj → 
+    nth j ? int (niltape ?) = midtape sig ls0 x (xs@rs0) →
+    (∀c0. memb ? c0 (x::xs) = true → is_endc c0 = false) → 
+    (rs0 = [ ] → 
+     outt = change_vec ?? 
+           (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) i)
+           (mk_tape sig (reverse ? xs@x::ls0) (None ?) []) j) ∨
+    ∀cj,rs1.rs0 = cj::rs1 →
+    (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.i ≠ j → i < S n → j < S n → 
-  compare i j sig n ⊫ R_compare i j sig n.
-#i #j #sig #n #Hneq #Hi #Hj #ta #k #outc #Hloop
-lapply (sem_while … (sem_comp_step i j sig n Hneq Hi Hj) … Hloop) //
+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) //
 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
-[ #tc whd in ⊢ (%→?); * * [ *
-  [ #Hcicj #Houtc % 
+[ #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)
+    ]
+  |#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;
@@ -214,24 +255,28 @@ lapply (sem_while … (sem_comp_step i j sig n Hneq Hi Hj) … Hloop) //
     [ #_ @Houtc
     | #ls #x #xs #ci #rs #ls0 #cj #rs0 #_ #Hnthj >Hnthj in Hcj;
       normalize in ⊢ (%→?); #H destruct (H) ] ]
-  | #tc #td #te * #x * * #Hci #Hcj #Hd #Hstar #IH #He lapply (IH He) -IH *
+  | #tc #td #te * #x * * * #Hendcx #Hci #Hcj #Hd #Hstar #IH #He lapply (IH He) -IH *
     #IH1 #IH2 %
-    [ >Hci >Hcj * [* [* #H @False_ind @H % | #H destruct (H)] | #H destruct (H)] 
+    [ >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 #Hcicj >IH1 
+      [ #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 %1 %1 @(not_to_not ??? Hcicj)
-          #H destruct (H) % ]
-      | #x0 #xs0 #Hnthi #Hnthj #Hcicj
+          >nth_change_vec // >Hnthi >Hnthj normalize 
+          cases Hcicj #Hcase 
+          [%1 %{ci} % // | %2 %1 %1 @(not_to_not ??? Hcase) #H destruct (H) % ]
+        ]
+      | #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
@@ -241,19 +286,19 @@ lapply (sem_while … (sem_comp_step i j sig n Hneq Hi Hj) … Hloop) //
 ]]]
 qed.      
  
-lemma terminate_compare :  ∀i,j,sig,n,t.
+lemma terminate_compare :  ∀i,j,sig,n,is_endc,t.
   i ≠ j → i < S n → j < S n → 
-  compare i j sig n  ↓ t.
-#i #j #sig #n #t #Hneq #Hi #Hj
+  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
+[ % #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 * * >nth_change_vec // normalize in ⊢ (%→?);
+  [#t #ls #c % #t1 * #x * * * #Hendcx >nth_change_vec // normalize in ⊢ (%→?);
    #H1 destruct (H1) #Hxsep >change_vec_change_vec #Ht1 % 
-   #t2 * #x0 * * >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
+   #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
@@ -262,10 +307,10 @@ cases (nth i (tape sig) t (niltape ?))
 ]
 qed.
 
-lemma sem_compare : ∀i,j,sig,n.
+lemma sem_compare : ∀i,j,sig,n,is_endc.
   i ≠ j → i < S n → j < S n → 
-  compare i j sig n ⊨ R_compare i j sig n.
-#i #j #sig #n #Hneq #Hi #Hj @WRealize_to_Realize /2/
+  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.
 
 (*
@@ -282,7 +327,7 @@ qed.
  *)
 
 definition match_step ≝ λsrc,dst,sig,n,is_startc,is_endc.
-  compare src dst sig n ·
+  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)))
@@ -296,32 +341,50 @@ definition Rtc_multi_true ≝
 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).
+  ∀ls,x,xs,end,rs.
+  nth src ? int (niltape ?) = midtape sig ls x (xs@end::rs) →
+  (∀c0. memb ? c0 (x::xs) = true → is_endc c0 = false) → is_endc end = true →
+   ((current sig (nth dst (tape sig) int (niltape sig)) = None ? ) ∧ outt = int) ∨
+   (∃ls0,rs0. 
+    nth dst ? int (niltape ?) = midtape sig ls0 x (xs@rs0) ∧
+    ∀rsj,end,c. 
+    rs0 = c::rsj →
+    outt = change_vec ??
+           (change_vec ?? int (midtape sig (reverse ? xs@x::ls) end rs) src)
+           (midtape sig (reverse ? xs@x::ls0) c rsj) dst).
+(*  
 definition R_match_step_false ≝  
   λsrc,dst,sig,n,is_endc.λint,outt: Vector (tape sig) (S n).
-   ((current ? (nth src ? int (niltape ?)) ≠ current ? (nth dst ? int (niltape ?)) ∨   
+   (((∃x.current ? (nth src ? int (niltape ?)) = Some ? x ∧ is_endc x = true) ∨
      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 →
-    is_endc end = true ∧
-    nth src ? int (niltape ?) = midtape sig ls x (xs@rs) ∧
+   (∃ls,ls0,rs,rs0,x,xs. 
+    nth src ? int (niltape ?) = midtape sig ls x (xs@rs) ∧ is_endc x = false ∧
     nth dst ? int (niltape ?) = midtape sig ls0 x (xs@rs0) ∧
+    ∀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 dst ? int (niltape ?) = midtape sig ls0 x (xs@c::rsj) ∧
     outt = change_vec ??
            (change_vec ?? int (midtape sig (reverse ? xs@x::ls) end rsi) src)
-           (midtape sig (reverse ? xs@x::ls0) c rsj) dst.
+           (midtape sig (reverse ? xs@x::ls0) c rsj) dst).
+*)
 
 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 →
-  (∀s1.current sig (nth dst (tape sig) int (niltape sig)) = Some ? s1 →
-   s ≠ s1 →  
+  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).
     
@@ -348,11 +411,12 @@ cases (acc_sem_inject … Hin (sem_test_char alpha test) int)
     | @sym_eq @Hnth_j @sym_not_eq // ] ] ]
 qed.
 
-axiom comp_list: ∀S:DeqSet. ∀l1,l2:list S. ∃l,tl1,tl2. 
-  l1 = l@tl1 ∧ l2 = l@tl2 ∧ ∀a,b,tla,tlb. tl1 = a::tla → tl2 = b::tlb → a≠b.
+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 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 ⊨ 
@@ -360,17 +424,17 @@ lemma sem_match_step :
       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 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))
       (sem_seq … 
         (sem_parmoveL ???? is_startc 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 %
+ #Htb #s #Hcurta_src #Hstart #Hnotstart %
  [ #s1 #Hcurta_dst #Hneqss1
    lapply Htb lapply Hcurtc -Htb -Hcurtc >(?:tc=ta) 
-   [|@Hcomp1 % % >Hcurta_src >Hcurta_dst @(not_to_not … Hneqss1) #H destruct (H) % ]
+   [|@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)
@@ -378,13 +442,18 @@ lemma sem_match_step :
      | >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
-  lapply (Hcomp2 … Htasrc_mid Htadst_mid Hcicj) -Hcomp2 #Hcomp2
+ |#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 //
-   | @daemon
+   | #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
@@ -394,53 +463,191 @@ lemma sem_match_step :
      | >nth_change_vec_neq [|@sym_not_eq //]
        <Htbelse [|@sym_not_eq // ]
        >nth_change_vec_neq [|@sym_not_eq //]
-       STOP.
-       
-       >nth_change_vec in Htbdst; // #Htbdst >(Htbdst
-     … Htadst_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;
+        (* 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
+ #ls #c_src #xs #end #rs #Hmid_src #Hnotend #Hend
+ lapply (current_to_midtape sig (nth dst ? intape (niltape ?)))
+ cases (current … (nth dst ? intape (niltape ?))) in Hcomp1;
+  [#Hcomp1 #_ %1 % [% | @Hcomp1 %2 %2 % ]
+  |#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 %2
+     cases (comp_list … (xs@end::rs) rs_dst is_endc) #xs1 * #rsi * #rsj * * * 
+     #Hrs_src #Hrs_dst #Hnotendc #Hneq    
+     %{ls_dst} %{rsj} % 
+      [<Hrs_dst >(\P Hceq) // ]]     
+         #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 // ] 
+          ]
+         -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_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) //
+        ]
+      ]
+    ]
+  ] 
+qed.
+
+#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 %1 %2 %]
+  [#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 %]
+    [#_ #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 #
+       #ls_dst * #rs_dst #Hmid_dst #Hcomp1
        #Hmid_src cases (Hmid_src c_src (refl …)) -Hmid_src
-       #ls_src * #rs_src #Hmid_src %2 
-       cases (comp_list … rs_src rs_dst) #xs * #rsi * #rsj * * 
-       #Hrs_src #Hrs_dst #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
-       lapply(Hcomp2 … Hmid_src Hmid_dst ?)
-        [@(Hneq … Hend Hc_dst)]
-       -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 //
-          |@Hmid_src]
-          |@Hmid_dst]
-      |#_ #Hcomp1 #_ %1 % 
-        [% % @(not_to_not ??? (\Pf Hceq)) #H destruct (H) //
-        |@Hcomp1 %1 %1 @(not_to_not ??? (\Pf Hceq)) #H destruct (H) //
+       #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} % 
+           [% [% // <Hrs_src //|<Hrs_dst >(\P Hceq) // ]]     
+         #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 // ] 
+          ]
+         -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_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) //
         ]
       ]
     ]
   ] 
+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)))).
 
-2:#t1 #t2 #t3 whd in ⊢ (%→?); * #Hc #H #H1 whd #ls #c #rs #Ht1 %
-  [lapply(Hc c ?) [>Ht1 %] #Hgrid @injective_notb @Hgrid |>H1 @H]
+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.
+    (∀x. is_startc x ≠ is_endc x) → 
+    is_startc x = true → is_endc ci = true → 
+    (∀z. memb ? z (x::xs) = true → is_endc x = false) →
+    nth i ? int (niltape ?) = midtape sig ls x (xs@ci::rs) →
+    nth j ? int (niltape ?) = midtape sig ls0 x0 rs0 →
+    (∃l,l1.x0::rs0 = l@x::xs@l1 ∧
+     ∀cj,l2.l1=cj::l2 →
+     outt = change_vec ?? 
+            (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) i)
+            (midtape sig ((reverse ? (l@x::xs))@ls0) cj l2) j) ∨
+    ∀l,l1.x0::rs0 ≠ l@x::xs@l1).
 
+(*
+axiom sub_list_dec: ∀A.∀l,ls:list A. 
+  ∃l1,l2. l = l1@ls@l2 ∨ ∀l1,l2. l ≠ l1@ls@l2.
+*)
 
+lemma wsem_match_m : ∀src,dst,sig,n,is_startc,is_endc.
+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 #Hdiff #Hstartc #Hendc #Hnotend #Hnthi 
+        @False_ind
+        >Hnthi in H1; whd in ⊢ (??%?→?); #H destruct (H) cases (Hdiff cur_src)
+        #Habs @Habs //
+      ]
+    | #Hci #Houtc %
+      [ #_ @Houtc
+      | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hdiff #Hstartc #Hendc #Hnotend 
+        #Hnthi >Hnthi in Hci; normalize in ⊢ (%→?); #H destruct (H) ] ]
+    | #Hcj #Houtc %
+      [ #_ @Houtc
+      | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hdiff #Hstartc #Hendc #_ #_ #Hnthj >Hnthj in Hcj;
+        normalize in ⊢ (%→?); #H destruct (H) ] 
+    ]
+  |* #ls * #ls0 * #rs * #rs0 * #x0 * #xs * * * #Hsrc #Hx0 #Hdst #H %
+    [>Hsrc * 
+      [* [* #x * whd in ⊢ (??%?→?); #Habs destruct (Habs) >Hx0 #Habs destruct (Habs)
+                |whd in ⊢ (??%?→?); #Habs destruct (Habs) ]
+         |>Hdst whd in ⊢ (??%?→?); #Habs destruct (Habs)  ]
+    |#ls1 #x1 #xs1 #ci #rsi #ls2 #x2 #rs2 
+     #Hdiff #Hstart #Hend #Hnotend 
+     >Hsrc #Hsrc1 destruct (Hsrc1) >Hdst #Hdst1 destruct (Hdst1) 
+     %1 %{[ ]} %{rs0} normalize in ⊢ (%→?); #Heq #cj #l2 #Hl1
+     cut (xs=xs1) 
+       [@(append_l1_injective_r … rs0 rs0 (refl …)) @(cons_injective_r …Heq)] 
+     #eqxs <eqxs 
+     whd in match (append ? [ ] (x2::xs)); >reverse_cons >associative_append
+     normalize in match (append ? [x2] ls2);
+     cases (H rsi l2 ci cj ? Hl1) 
+      [* #_ #_ #H3 @H3  
+      |>eqxs in e0; #e0 @(append_l2_injective … e0) // 
+      ] 
+    ]
+  ]
+|#tc #td #te #Hd #Hstar #IH #He lapply (IH He) -IH *
+    #IH1 #IH2 % [@IH1]
+    
+    
+     cases (comp_list ? (x1::xs1@ci::rsi) (x2::rs2) is_endc)
+     #l * #tl1 * #tl2 * * * #H1 #H2 #H3 #H4