(** ** TM to SR with finite types *)

Require Import singleTM .

(* ** More preliminary lemmas *)

Lemma inductive_count (S:eqType) (Y:eqType) A (s:S) n (f: S -> Y):
Injective f -> count A s = n -> count (map (fun a:S => f a) A) (f s) = n.
Proof.
(* Goal: forall (_ : @Injective (eqType_X S) (eqType_X Y) f) (_ : @eq nat (@count (eqType_X S) (@eqType_dec S) A s) n), @eq nat (@count (eqType_X Y) (@eqType_dec Y) (@map (eqType_X S) (eqType_X Y) (fun a : eqType_X S => f a) A) (f s)) n *)
intros inj. revert n. induction A; intros n HC.
- destruct n; try discriminate HC. reflexivity.
- simpl in *. decide (s=a). rewrite e in *. destruct n.  discriminate HC. inversion HC.
(* Goal: @eq nat (if @Dec (@eq (eqType_X Y) (f a) (f a)) (@eqType_dec Y (f a) (f a)) then Datatypes.S (@count (eqType_X Y) (@eqType_dec Y) (@map (eqType_X S) (eqType_X Y) (fun a : eqType_X S => f a) A) (f a)) else @count (eqType_X Y) (@eqType_dec Y) (@map (eqType_X S) (eqType_X Y) (fun a : eqType_X S => f a) A) (f a)) (Datatypes.S (@count (eqType_X S) (@eqType_dec S) A a)) *)
(* Goal: @eq nat (if @Dec (@eq (eqType_X Y) (f s) (f a)) (@eqType_dec Y (f s) (f a)) then Datatypes.S (@count (eqType_X Y) (@eqType_dec Y) (@map (eqType_X S) (eqType_X Y) (fun a : eqType_X S => f a) A) (f s)) else @count (eqType_X Y) (@eqType_dec Y) (@map (eqType_X S) (eqType_X Y) (fun a : eqType_X S => f a) A) (f s)) n *)
  decide (f a = f a); try contradiction. rewrite (IHA n H0). now rewrite H0.
(* Goal: @eq nat (if @Dec (@eq (eqType_X Y) (f s) (f a)) (@eqType_dec Y (f s) (f a)) then Datatypes.S (@count (eqType_X Y) (@eqType_dec Y) (@map (eqType_X S) (eqType_X Y) (fun a : eqType_X S => f a) A) (f s)) else @count (eqType_X Y) (@eqType_dec Y) (@map (eqType_X S) (eqType_X Y) (fun a : eqType_X S => f a) A) (f s)) n *)
  decide (f s = f a). apply inj in e. contradiction. now apply IHA.
Qed.

Lemma diff_constructors_count_zero (F X Y:eqType) (A: list F) (f: F -> X) (g: Y -> X) x:
(forall a b, f a <> g b) -> count (map (fun (a:F) => f a) A) (g x) = 0.
Proof.
(* Goal: forall _ : forall (a : eqType_X F) (b : eqType_X Y), not (@eq (eqType_X X) (f a) (g b)), @eq nat (@count (eqType_X X) (@eqType_dec X) (@map (eqType_X F) (eqType_X X) (fun a : eqType_X F => f a) A) (g x)) O *)
intros dis. induction A. auto. cbn. decide (g x = f a).
(* Goal: and (@eq (list rsig) (@cons rsig (state q2) A) (@nil rsig)) (and (@eq (list rsig) B (@app rsig A (@cons rsig (state q1) B))) (@eq (eqType_X (type (@states sig T))) q1 q2)) *)
exfalso. apply (dis a x). symmetry. assumption. assumption.
Qed.

Lemma notInZero (X: eqType) (x: X) A :
  not (x el A) <-> count A x = 0.
Proof.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma A)) (@map (eqType_X (type sig)) rsig sym A) *)
  split; induction A.
  -  reflexivity.
  - intros H. cbn in *. dec.
(* Goal: and (@eq (list rsig) (@cons rsig (state q2) A) (@nil rsig)) (and (@eq (list rsig) B (@app rsig A (@cons rsig (state q1) B))) (@eq (eqType_X (type (@states sig T))) q1 q2)) *)
    + exfalso. apply H. left. congruence.
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
    + apply IHA. intros F. apply H. now right.
  - tauto.
  - cbn. dec.
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
    + subst a. omega.
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
    + intros H [E | E].
      * now symmetry in E.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
      * tauto.
Qed.


Lemma in_sing {X} (x y:X) :
    x el [y] -> x = y.
Proof.
(* Goal: @eq (eqType_X (type (@states sig M))) (@cstate sig (@states sig M) x) (@cstate sig (@states sig M) x) *)
(* Goal: @eq (eqType_X (type (@states sig M))) x0 (@cstate sig (@states sig M) x) *)
    cbn. intros [[]|[]]. reflexivity.
Qed.

Lemma in_concat_iff A l (a:A) : a el concat l <-> exists l', a el l' /\ l' el l.
Proof.
(* Goal: iff (SR_fin (@existT finType (fun sig : finType => prod (prod (srs (eqType_X (type sig))) (list (eqType_X (type sig)))) (list (eqType_X (type sig)))) Sigma (@pair (prod (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma)))) (list (eqType_X (type Sigma))) (@pair (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma))) R x0) y))) (SR (@pair (prod (list (prod (list nat) (list nat))) (list nat)) (list nat) (@pair (list (prod (list nat) (list nat))) (list nat) (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0)) (@map (eqType_X (type Sigma)) nat (@index Sigma) y))) *)
  induction l; cbn.
  - intuition. now destruct H.
  - rewrite in_app_iff, IHl. firstorder subst. auto.
Qed.

(* Definition of String rewriting with finite types *)


(*  String Rewriting **)
Section string_rewriting.

  Variable sig: Type.
  Definition rule : Type := list sig * list sig. 
  Definition srs := list rule.

  Implicit Types (a b c: sig) (x y z u v: list sig)
                 (r: list sig* list sig) (R S: srs).

  Inductive rew' : srs -> list sig -> list sig -> Prop:=
  |rewR R x u y v : (u,v) el R -> rew' R (x++u++y) (x++v++y).
  Hint Constructors rew'. 

  Inductive rewt' (S: srs) (x: list sig) : list sig -> Prop :=
  |rewt'Refl : rewt' S x x
  |rewt'Rule y z : rew' S x y -> rewt' S y z -> rewt' S x z.
  Hint Constructors rewt'.

  Instance rewt'Trans R :
    PreOrder (rewt' R).
  Proof.
(* Goal: iff (@rewt' (eqType_X (type Sigma)) R x0 y) (rewt (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0) (@map (eqType_X (type Sigma)) nat (@index Sigma) y)) *)
    split.
    - econstructor.
    - intros ? ? ? ? ?.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
      induction H. auto. now apply rewt'Rule with (y:= y), IHrewt'. 
  Qed.

  Lemma subset_rewriting S R x y:
    S <<= R -> rewt' S x y -> rewt' R x y.
  Proof.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
    intros Sub H. induction H. auto. apply rewt'Rule with (y:=y).
(* Goal: @eq (eqType_X (type (@states sig M))) x0 (@cstate sig (@states sig M) x) *)
    inv H. apply rewR with (x:=x0) (u:=u) (v:=v). now apply Sub. exact IHrewt'.  
  Qed.

  Lemma rewrite_app R x y z:
    rewt' R x y -> rewt' R (z++x) (z++y). 
  Proof.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
    induction 1. auto. inv H. revert IHrewt'. apply rewt'Rule.
(* Goal: rew' R (@app sig z (@app sig x0 (@app sig u y0))) (@app sig z (@app sig x0 (@app sig v y0))) *)
    rewrite app_assoc. setoid_rewrite app_assoc at 2. now apply rewR.
  Qed.

  Lemma rewrite_right R x y z :
    rewt' R x y -> rew' R y z -> rewt' R x z.
  Proof.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
    intros. induction H; eauto.
  Qed.

  Lemma rewt'_ind_left :
    forall (S : srs) x (P : list sig -> Prop),
      (P x) ->
      (forall y z : list sig, rewt' S x y -> rew' S y z -> P y -> P z) -> forall l : list sig, rewt' S x l -> P l.
  Proof.
(* Goal: forall _ : rewt (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0) (@map (eqType_X (type Sigma)) nat (@index Sigma) y), @rewt' (eqType_X (type Sigma)) R x0 y *)
    intros. induction H1; eauto 10 using rewt'.
  Qed.  
    
End string_rewriting.
  
Definition SR_fin (S: sigT (fun (sig:finType) => (prod (prod (srs sig) (list sig)) (list sig)))) :=
   match (projT2 S) with
    |(rules,x,y) => rewt' rules x y
   end.

Section Fix_TM.
  Variable sig : finType.
  Variable T : (sTM sig).
  Definition conf :Type := mconfig sig (states T).                                        
 
  Instance eq_dec_states : eq_dec (states T). 
(* Goal: forall x y : eqType_X (type (@states sig T)), dec (@eq (eqType_X (type (@states sig T))) x y) *)
  Proof. exact _. Defined.

  Inductive rsig' : Type := hash | dollar | sigma (s: sig).

  Instance eq_dec_rsig' : eq_dec rsig'.
  Proof.
(* Goal: forall _ : rewt (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0) (@map (eqType_X (type Sigma)) nat (@index Sigma) y), @rewt' (eqType_X (type Sigma)) R x0 y *)
    intros. hnf. decide equality. apply eq_dec_sig.
  Defined.

  Instance finType_rsig': finTypeC (EqType rsig').
  Proof.
(* Goal: finTypeC (@EqType rsig' eq_dec_rsig') *)
    exists (hash::dollar::(List.map (fun s => sigma s) (elem sig))).
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
    intros x. destruct x; cbn. induction (elem sig); auto. induction (elem sig); auto.  
(* Goal: @eq nat (@count rsig eq_dec_rsig (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig (fun s0 : eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig')) => symb s0) (elem (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) (symb s)) (S O) *)
    apply inductive_count. intros x y; now inversion 1. apply (@enum_ok sig (class sig) s). 
  Defined.

  
  Inductive rsig : Type := state (q: states T) | symb (s: (FinType (EqType rsig'))).
  Definition sym : sig -> rsig := fun s => symb (sigma s). 
  Notation "#" := (symb hash).
  Notation "$" := (symb dollar).

  Global Instance eq_dec_rsig : eq_dec rsig.
  Proof.
(* Goal: forall _ : rewt (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0) (@map (eqType_X (type Sigma)) nat (@index Sigma) y), @rewt' (eqType_X (type Sigma)) R x0 y *)
    intros. hnf. decide equality. apply eq_dec_states.
(* Goal: sumbool (@eq (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) s s0) (not (@eq (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) s s0)) *)
    apply eq_dec_rsig'.
  Defined.

  Lemma state_count_one q:
    count (map (fun s : states T => state s) (elem (states T))) (state q) = 1.
  Proof.
(* Goal: @eq nat (@count rsig eq_dec_rsig (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig (fun s0 : eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig')) => symb s0) (elem (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) (symb s)) (S O) *)
    apply inductive_count. intros x y; now inversion 1.
(* Goal: @eq nat (@count (eqType_X (type (@states sig T))) (@eqType_dec (type (@states sig T))) (elem (@states sig T)) q) (S O) *)
    apply (@enum_ok (states T) (class (states T)) q).
  Qed.

  Lemma symb_count_one s:
    count (map (fun s0 : FinType (EqType rsig') => symb s0)
               (elem (FinType (EqType rsig')))) (symb s) = 1.
  Proof.
(* Goal: @eq nat (@count rsig eq_dec_rsig (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig (fun s0 : eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig')) => symb s0) (elem (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) (symb s)) (S O) *)
    apply inductive_count. intros x y; now inversion 1.
(* Goal: @eq nat (@count (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) (@eqType_dec (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) (elem (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig')) s) (S O) *)
    apply (@enum_ok (FinType (EqType rsig')) (class (FinType (EqType rsig'))) s).
  Qed.

    
  Global Instance finType_rsig: finTypeC (EqType rsig).
  Proof.
    exists ((map (fun s => state s) (elem (states T)))
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
         ++ (map (fun s => symb s) (elem (FinType (EqType rsig'))))). intros x. destruct x.
    - rewrite <- countSplit. rewrite state_count_one.
      now rewrite (@diff_constructors_count_zero (FinType (EqType rsig'))
                  (EqType rsig) (states T) (elem (FinType (EqType (rsig')))) symb state).
    - rewrite <- countSplit. rewrite symb_count_one. 
(* Goal: @eq nat (Init.Nat.add (@count (eqType_X (@eqType_CS rsig eq_dec_rsig)) (@eqType_dec (@eqType_CS rsig eq_dec_rsig)) (@map (eqType_X (type (@states sig T))) rsig (fun s : eqType_X (type (@states sig T)) => state s) (elem (@states sig T))) (symb s)) (S O)) (S O) *)
      now rewrite (@diff_constructors_count_zero (states T)).
  Defined.

  Definition rsig_finType : finType := (@FinType (EqType rsig) finType_rsig).


  Definition halting_states : list (states T) :=
    List.filter (fun s => halt s) (elem (states T)).

  Definition not_halting_states : list (states T) :=
    List.filter (fun s => negb (halt s)) (elem (states T)).

  Lemma not_halting (q: states T) :
    halt q = false <-> q el not_halting_states.
  Proof.
(* Goal: iff (@rewt' (eqType_X (type Sigma)) R x0 y) (rewt (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0) (@map (eqType_X (type Sigma)) nat (@index Sigma) y)) *)
    split. unfold not_halting_states. rewrite filter_In.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
    intros H. split. auto. now rewrite H. unfold not_halting_states.
(* Goal: forall _ : @In (eqType_X (type (@states sig T))) q (@filter (eqType_X (type (@states sig T))) (fun s : eqType_X (type (@states sig T)) => @halt sig T s) (elem (@states sig T))), @eq bool (@halt sig T q) true *)
    rewrite filter_In. intros [H1 H2]. now destruct (halt q).  
  Qed.

  Lemma halting (q: states T) :
    halt q = true <-> q el halting_states.
  Proof.
(* Goal: iff (@rewt' (eqType_X (type Sigma)) R x0 y) (rewt (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0) (@map (eqType_X (type Sigma)) nat (@index Sigma) y)) *)
    split. unfold halting_states. rewrite filter_In.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
    intros H. split. auto. now rewrite H. unfold halting_states.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
    rewrite filter_In. intros []. auto. 
  Qed.
  

  (*  ** Encoding of Configurations **)
  Definition mk_srconf (c: conf): list rsig :=
    match (ctape c) with
    | niltape _ => [state (cstate c);#;$]
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
    | leftof s r => [state(cstate c); #]++ (List.map sym (s::r))++[$]
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
    | rightof s l => [#]++(List.map sym (List.rev (s::l)))++[state(cstate c);$]
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
    | midtape l s r => [#]++(List.map sym (List.rev l))++ [(state(cstate c))]
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
                                ++ (List.map sym (s::r))++[$]
    end.
         
  Lemma sym_inj : Injective sym. 
  Proof.
(* Goal: @Injective (eqType_X (type sig)) rsig sym *)
    intros x y. now inversion 1.
  Qed.

  Lemma state_not_sym A B q e l:
    A++[state q] = B ++sym e::(List.map sym l) -> False.
  Proof.
(* Goal: @eq (eqType_X (type (@states sig M))) x0 (@cstate sig (@states sig M) x) *)
    revert B e. induction l; intros B e H; cbn in *.
    - apply last_app_eq in H as [H HE]. inv HE.
    - apply (IHl (B++[sym e]) a). now rewrite <- app_assoc.
  Qed.
  
  Lemma midtape_state A B q1 q2 l r:
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
    A ++ (state q1)::B = List.map sym l++(state q2)::(List.map sym r)
            -> A = List.map sym l /\ B = (List.map sym r) /\ q1 = q2.
  Proof.
(* Goal: @eq (eqType_X (type (@states sig M))) x0 (@cstate sig (@states sig M) x) *)
    revert r A B. induction l; intros r A B H; cbn in *. 
    - destruct A. rewrite app_nil_l in H. inv H. now split. inv H. exfalso.
      assert ((state q1) el (A++(state q1)::B) -> False). intros H. rewrite H2 in H.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
      apply in_map_iff in H as [x [? ?]]. inv H. apply H. auto. 
    - destruct A. rewrite app_nil_l in H. inv H. inv H. destruct (IHl r A B H2).
(* Goal: iff (@rewt' (eqType_X (type Sigma)) R x0 y) (rewt (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0) (@map (eqType_X (type Sigma)) nat (@index Sigma) y)) *)
      subst. now split. 
  Qed.

  Lemma mk_srconf_inj (c1 c2: conf) :
    mk_srconf c1 = mk_srconf c2 <-> c1 = c2.
  Proof.
(* Goal: iff (@rewt' (eqType_X (type Sigma)) R x0 y) (rewt (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0) (@map (eqType_X (type Sigma)) nat (@index Sigma) y)) *)
    split.
    - unfold mk_srconf. destruct (ctape c1) eqn: H,(ctape c2) eqn: H'; try inversion 1;
(* Goal: @eq (eqType_X (type (@states sig M))) (@cstate sig (@states sig M) x) (@cstate sig (@states sig M) x) *)
(* Goal: @eq (eqType_X (type (@states sig M))) x0 (@cstate sig (@states sig M) x) *)
                         subst; destruct c1,c2; cbn in *; subst; try reflexivity. 
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
      + apply app_inv_tail in H4. apply map_inj in H4. now subst. apply sym_inj. 
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
      + change [state cstate;$] with ([state cstate]++[$]) in H2.
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
        change [state cstate0;$] with ([state cstate0]++[$]) in H2. 
(* Goal: @eq conf (@mk_mconfig sig (@states sig T) cstate (@midtape sig l e l0)) (@mk_mconfig sig (@states sig T) cstate0 (@midtape sig l1 e0 l2)) *)
        rewrite !app_assoc in H2. apply app_inv_tail in H2.
(* Goal: @eq conf (@mk_mconfig sig (@states sig T) cstate (@rightof sig e l)) (@mk_mconfig sig (@states sig T) cstate0 (@rightof sig e0 l0)) *)
        apply last_app_eq in H2 as [H3 H2]. repeat rewrite map_app in H3.
(* Goal: @eq conf (@mk_mconfig sig (@states sig T) cstate0 (@rightof sig e l)) (@mk_mconfig sig (@states sig T) cstate0 (@rightof sig e0 l0)) *)
        apply last_app_eq in H3 as [H3 HE]. inv H2. inv HE. rewrite map_inj in H3.
(* Goal: @rewt' (eqType_X (type Sigma)) R x0 y *)
(* Goal: @eq (list (eqType_X (type Sigma))) x y *)
        rewrite rev_eq in H3. now subst. apply sym_inj. 
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
      + change [state cstate; $] with ([state cstate]++[$]) in H2.
(* Goal: @eq conf (@mk_mconfig sig (@states sig T) cstate (@midtape sig l e l0)) (@mk_mconfig sig (@states sig T) cstate0 (@rightof sig e0 l1)) *)
        rewrite !app_assoc, !app_comm_cons in H2. rewrite app_assoc in H2.
(* Goal: and (@eq (list rsig) (@cons rsig (state q2) A) (@nil rsig)) (and (@eq (list rsig) B (@app rsig A (@cons rsig (state q1) B))) (@eq (eqType_X (type (@states sig T))) q1 q2)) *)
        apply last_app_eq in H2 as [H2 _]. exfalso.
        change (state cstate0::sym e0::List.map sym l1)
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
                   with ([state cstate0]++sym e0::List.map sym l1) in H2.
(* Goal: @eq conf (@mk_mconfig sig (@states sig T) cstate (@midtape sig l e l0)) (@mk_mconfig sig (@states sig T) cstate0 (@rightof sig e0 l1)) *)
        rewrite app_assoc in H2.  apply (state_not_sym H2).
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
      + change [state cstate0; $] with ([state cstate0]++[$]) in H2.
(* Goal: @eq conf (@mk_mconfig sig (@states sig T) cstate (@midtape sig l e l0)) (@mk_mconfig sig (@states sig T) cstate0 (@rightof sig e0 l1)) *)
        rewrite !app_assoc, !app_comm_cons in H2. rewrite app_assoc in H2.
(* Goal: @eq conf (@mk_mconfig sig (@states sig T) cstate (@midtape sig l e l0)) (@mk_mconfig sig (@states sig T) cstate0 (@midtape sig l1 e0 l2)) *)
        apply last_app_eq in H2 as [H2 _]. symmetry in H2.
        change (state cstate::sym e::List.map sym l0)
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
                          with ([state cstate]++sym e::List.map sym l0) in H2.
(* Goal: and (@eq (list rsig) (@cons rsig (state q2) A) (@nil rsig)) (and (@eq (list rsig) B (@app rsig A (@cons rsig (state q1) B))) (@eq (eqType_X (type (@states sig T))) q1 q2)) *)
        rewrite app_assoc in H2. exfalso. apply (state_not_sym H2).
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
      + rewrite !app_comm_cons in H2. rewrite! app_assoc in H2. apply last_app_eq in H2 as [H2 _].
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
        assert (List.map sym (List.rev l) ++ state cstate :: List.map sym (e::l0) =
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
                List.map sym (List.rev l1) ++ state cstate0 :: List.map sym (e0::l2)) as H3 by auto. 
(* Goal: @eq conf (@mk_mconfig sig (@states sig T) cstate (@midtape sig l e l0)) (@mk_mconfig sig (@states sig T) cstate0 (@midtape sig l1 e0 l2)) *)
        apply midtape_state in H3 as [H3 [H4 H5]]. inv H4. apply map_inj in H6.
(* Goal: @rewt' (eqType_X (type Sigma)) R x0 y *)
(* Goal: @eq (list (eqType_X (type Sigma))) x y *)
        subst. apply map_inj in H3. rewrite rev_eq in H3. now subst. apply sym_inj. apply sym_inj. 
    - now intros <-.
  Qed.
  

  

  (* ** Definition of Rewrite Rules Simulating a Transition **)
  
   Definition get_rules_left (q1 q2: states T) (old new: option sig) : list (list rsig * list rsig):=
    match old,new with
    |None,None => List.map (fun c => ([sym c; state q1; $], [state q2; sym c; $])) (elem sig)
    |None,Some b => List.map (fun c => ([sym c; state q1; $], [state q2; sym c; sym b; $])) (elem sig)
    |Some a,None => List.map (fun c => ([sym c; state q1; sym a], [state q2; sym c; sym a])) (elem sig)
    |Some a,Some b => List.map (fun c => ([sym c; state q1; sym a], [state q2; sym c; sym b])) (elem sig)
    end.
     
   Definition get_rules_right (q1 q2: states T) : list (list rsig * list rsig) :=
     List.map (fun a => ([state q1; #; sym a],[#; state q2; sym a])) (elem sig).
   

   Definition get_rules (q1 q2: states T) (old new: option sig) (m: move) :
     list (list rsig * list rsig) :=
    match old,new,m with
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
    |None,None,L => [([state q1; #],[state q2; #])] ++ (get_rules_left q1 q2 None None)
    |None,None,N => [([state q1; #],[state q2; #]);([state q1; $],[state q2; $])]
    |None,None,R => [([state q1; #;$],[state q2; #;$]); ([state q1; $],[state q2; $])]
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
                     ++ (get_rules_right q1 q2)
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
    |None,Some b,L => [([state q1; #],[state q2; #; sym b])] ++ (get_rules_left q1 q2 None (Some b))
    |None,Some b,N => [([state q1; #],[#; state q2; sym b]); ([state q1; $],[state q2; sym b; $])]
    |None,Some b,R => [([state q1; #],[#; sym b; state q2]); ([state q1; $],[sym b; state q2; $])]
    |Some a,None,L => [([#;state q1; sym a],[state q2; #; sym a])]
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
                       ++ (get_rules_left q1 q2 (Some a) None)
    |Some a,None,N => [([state q1; sym a],[state q2; sym a])]
    |Some a,None,R => [([state q1; sym a],[sym a; state q2])]
    |Some a,Some b,L => [([#; state q1; sym a],[state q2; #; sym b])]
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
                         ++ (get_rules_left q1 q2 (Some a) (Some b))
    |Some a,Some b,N => [([state q1; sym a],[state q2; sym b])]
    |Some a,Some b,R => [([state q1; sym a],[sym b; state q2])]
    end.


 
   Definition TMrules : list (list rsig * list rsig) :=
     List.concat ((List.map (fun q1 => match trans (q1,None) with
                      |(q2,(o,m)) => get_rules q1 q2 None o m
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
                                   end) not_halting_states) ++
                 (List.map (fun (P: states T * sig) => let (q1,s) := P in match trans (q1,Some s) with
                                                    |(q2,(o,m)) => get_rules q1 q2 (Some s) o m
                                                    end)
                           (list_prod not_halting_states (elem sig)))).
   

   (* ** Correctness Proof **)
   
   Lemma get_rules_el_TMrules (q1 q2: states T) (old new: option sig) (m: move) :
     halt q1 = false -> (trans (q1,old) = (q2,(new,m)))
     -> get_rules q1 q2 old new m <<= TMrules.
   Proof.
(* Goal: forall (_ : @eq bool (@halt sig T q1) false) (_ : @eq (prod (eqType_X (type (@states sig T))) (prod (option (eqType_X (type sig))) move)) (@trans sig T (@pair (eqType_X (type (@states sig T))) (option (eqType_X (type sig))) q1 old)) (@pair (eqType_X (type (@states sig T))) (prod (option (eqType_X (type sig))) move) q2 (@pair (option (eqType_X (type sig))) move new m))), @incl (prod (list rsig) (list rsig)) (get_rules q1 q2 old new m) TMrules *)
     intros HF HT. intros s HS. apply in_concat_iff. exists (get_rules q1 q2 old new m).
(* Goal: @eq bool (@halt sig T (@cstate sig (@states sig T) c1)) false *)
     split. assumption. apply in_app_iff. destruct old.
     - right. apply in_map_iff. exists (q1,e). rewrite HT. split. reflexivity.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
       apply in_prod_iff. split. now apply not_halting. auto. 
     - left. apply in_map_iff. exists q1. rewrite HT. split. auto. now apply not_halting.
   Qed.

   Lemma rewrite_step_conf (c1: conf) :
    (halt (cstate c1) = false) -> rew' TMrules (mk_srconf c1) (mk_srconf (step c1)).
   Proof.
(* Goal: iff (SR_fin (@existT finType (fun sig : finType => prod (prod (srs (eqType_X (type sig))) (list (eqType_X (type sig)))) (list (eqType_X (type sig)))) Sigma (@pair (prod (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma)))) (list (eqType_X (type Sigma))) (@pair (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma))) R x0) y))) (SR (@pair (prod (list (prod (list nat) (list nat))) (list nat)) (list nat) (@pair (list (prod (list nat) (list nat))) (list nat) (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0)) (@map (eqType_X (type Sigma)) nat (@index Sigma) y))) *)
     intros H. unfold mk_srconf. destruct (ctape c1) eqn: CT; unfold step; rewrite CT; cbn.
     - destruct (trans (cstate c1, None)) as (q2,(new,mov)) eqn: TC;
         assert (Sub: get_rules (cstate c1) q2 None new mov <<= TMrules)
(* Goal: iff (SR_fin (@existT finType (fun sig : finType => prod (prod (srs (eqType_X (type sig))) (list (eqType_X (type sig)))) (list (eqType_X (type sig)))) Sigma (@pair (prod (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma)))) (list (eqType_X (type Sigma))) (@pair (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma))) R x0) y))) (SR (@pair (prod (list (prod (list nat) (list nat))) (list nat)) (list nat) (@pair (list (prod (list nat) (list nat))) (list nat) (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0)) (@map (eqType_X (type Sigma)) nat (@index Sigma) y))) *)
         by (now apply get_rules_el_TMrules). destruct new as [new| ]; destruct mov; cbn.
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
       + apply rewR with (x:=[]) (u:= [state (cstate c1);#]) (v:=[state q2;#;sym new]).
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
         firstorder. 
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
       + apply rewR with (x:=[]) (u:= [state (cstate c1);#]) (v:= [#;sym new; state q2]).
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
         firstorder. 
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
       + apply rewR with (x:=[]) (u:= [state (cstate c1); #]) (v:=[#; state q2; sym new]).
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
         firstorder. 
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
       + apply rewR with (x:=[]) (u:= [state (cstate c1); #]) (v:=[state q2; #]).
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
         firstorder. 
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
       + apply rewR with (x:=[]) (u:= [state (cstate c1); #; $])(v:=[state q2; #; $]).
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
         firstorder. 
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
       + apply rewR with (x:=[]) (u:= [state (cstate c1); #]) (v:=[state q2; #]).
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
         firstorder.
     - destruct (trans (cstate c1, None)) as (q2,(new,mov)) eqn: TC;
         assert (Sub: get_rules (cstate c1) q2 None new mov <<= TMrules)
(* Goal: iff (SR_fin (@existT finType (fun sig : finType => prod (prod (srs (eqType_X (type sig))) (list (eqType_X (type sig)))) (list (eqType_X (type sig)))) Sigma (@pair (prod (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma)))) (list (eqType_X (type Sigma))) (@pair (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma))) R x0) y))) (SR (@pair (prod (list (prod (list nat) (list nat))) (list nat)) (list nat) (@pair (list (prod (list nat) (list nat))) (list nat) (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0)) (@map (eqType_X (type Sigma)) nat (@index Sigma) y))) *)
         by (now apply get_rules_el_TMrules). destruct new as [new| ]; destruct mov; cbn.
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
       + apply rewR with (x:=[]) (u:= [state (cstate c1); #]) (v:=[state q2; #; sym new]).
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
         firstorder. 
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
       + apply rewR with (x:=[]) (u:= [state (cstate c1); #]) (v:=[#; sym new; state q2]).
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
         firstorder.
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
       + apply rewR with (x:=[]) (u:= [state (cstate c1); #]) (v:=[#; state q2; sym new]).
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
         firstorder. 
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
       + apply rewR with (x:=[]) (u:= [state (cstate c1); #])(v:=[state q2; #]).
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
         firstorder. 
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
       + apply rewR with (x:=[]) (u:= [state (cstate c1); #; sym e]) (v:=[#; state q2; sym e]).
(* Goal: iff (SR_fin (@existT finType (fun sig : finType => prod (prod (srs (eqType_X (type sig))) (list (eqType_X (type sig)))) (list (eqType_X (type sig)))) Sigma (@pair (prod (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma)))) (list (eqType_X (type Sigma))) (@pair (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma))) R x0) y))) (SR (@pair (prod (list (prod (list nat) (list nat))) (list nat)) (list nat) (@pair (list (prod (list nat) (list nat))) (list nat) (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0)) (@map (eqType_X (type Sigma)) nat (@index Sigma) y))) *)
         apply Sub. cbn. right. right. unfold get_rules_right. apply in_map_iff. exists e. now split.
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
       + apply rewR with (x:=[]) (u:= [state (cstate c1); #]) (v:=[state q2; #]).
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
         firstorder.
     - destruct (trans (cstate c1, None)) as (q2,(new,mov)) eqn: TC;
         assert (Sub: get_rules (cstate c1) q2 None new mov <<= TMrules)
(* Goal: iff (SR_fin (@existT finType (fun sig : finType => prod (prod (srs (eqType_X (type sig))) (list (eqType_X (type sig)))) (list (eqType_X (type sig)))) Sigma (@pair (prod (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma)))) (list (eqType_X (type Sigma))) (@pair (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma))) R x0) y))) (SR (@pair (prod (list (prod (list nat) (list nat))) (list nat)) (list nat) (@pair (list (prod (list nat) (list nat))) (list nat) (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0)) (@map (eqType_X (type Sigma)) nat (@index Sigma) y))) *)
         by (now apply get_rules_el_TMrules). destruct new as [new| ]; destruct mov; cbn.
       + rewrite map_app. rewrite <- app_assoc.
         apply rewR with (x:=#::List.map sym (List.rev l)) (u:= [sym e; state (cstate c1); $])
                       (v:=[state q2; sym e; sym new;$]). 
(* Goal: iff (SR_fin (@existT finType (fun sig : finType => prod (prod (srs (eqType_X (type sig))) (list (eqType_X (type sig)))) (list (eqType_X (type sig)))) Sigma (@pair (prod (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma)))) (list (eqType_X (type Sigma))) (@pair (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma))) R x0) y))) (SR (@pair (prod (list (prod (list nat) (list nat))) (list nat)) (list nat) (@pair (list (prod (list nat) (list nat))) (list nat) (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0)) (@map (eqType_X (type Sigma)) nat (@index Sigma) y))) *)
         apply Sub. cbn. right. apply in_map_iff. exists e. now split.
       + rewrite !map_app. setoid_rewrite <- app_assoc at 2. 
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
         apply rewR with (x:=#::List.map sym (List.rev l) ++[sym e]) (u:= [state (cstate c1); $])
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
                       (v:=[sym new; state q2;$]). firstorder. 
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
       + apply rewR with (x:=#::List.map sym (List.rev l++[e])) (u:= [state (cstate c1); $])
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
                       (v:=[state q2; sym new; $]). firstorder. 
       + rewrite map_app. rewrite <- app_assoc.
         apply rewR with (x:=#::List.map sym (List.rev l)) (u:= [sym e; state (cstate c1); $])
                       (v:=[state q2; sym e;$]).
(* Goal: iff (SR_fin (@existT finType (fun sig : finType => prod (prod (srs (eqType_X (type sig))) (list (eqType_X (type sig)))) (list (eqType_X (type sig)))) Sigma (@pair (prod (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma)))) (list (eqType_X (type Sigma))) (@pair (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma))) R x0) y))) (SR (@pair (prod (list (prod (list nat) (list nat))) (list nat)) (list nat) (@pair (list (prod (list nat) (list nat))) (list nat) (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0)) (@map (eqType_X (type Sigma)) nat (@index Sigma) y))) *)
         apply Sub. cbn. right. apply in_map_iff. exists e. now split.
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
       + apply rewR with (x:=#::List.map sym (List.rev l++[e])) (u:= [state (cstate c1); $])
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
                       (v:=[state q2; $]). firstorder. 
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
       + apply rewR with (x:=#::List.map sym (List.rev l++[e])) (u:= [state (cstate c1); $])
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
                       (v:=[state q2; $]). firstorder. 
     - destruct (trans (cstate c1, Some e)) as (q2,(new,mov)) eqn: TC;
         assert (Sub: get_rules (cstate c1) q2 (Some e) new mov <<= TMrules)
(* Goal: iff (SR_fin (@existT finType (fun sig : finType => prod (prod (srs (eqType_X (type sig))) (list (eqType_X (type sig)))) (list (eqType_X (type sig)))) Sigma (@pair (prod (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma)))) (list (eqType_X (type Sigma))) (@pair (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma))) R x0) y))) (SR (@pair (prod (list (prod (list nat) (list nat))) (list nat)) (list nat) (@pair (list (prod (list nat) (list nat))) (list nat) (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0)) (@map (eqType_X (type Sigma)) nat (@index Sigma) y))) *)
         by (now apply get_rules_el_TMrules). destruct new as [new| ]; destruct mov; cbn.
(* Goal: iff (SR_fin (@existT finType (fun sig : finType => prod (prod (srs (eqType_X (type sig))) (list (eqType_X (type sig)))) (list (eqType_X (type sig)))) Sigma (@pair (prod (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma)))) (list (eqType_X (type Sigma))) (@pair (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma))) R x0) y))) (SR (@pair (prod (list (prod (list nat) (list nat))) (list nat)) (list nat) (@pair (list (prod (list nat) (list nat))) (list nat) (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0)) (@map (eqType_X (type Sigma)) nat (@index Sigma) y))) *)
       + destruct l; cbn. 
         * apply rewR with (x:=[]) (u:= [#; state (cstate c1) ; sym e]) (v:=[state q2; #; sym new]).
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
           firstorder. 
         * rewrite map_app. rewrite <- app_assoc.
           apply rewR with (x:=#::List.map sym (List.rev l))
                           (u:= [sym e0; state (cstate c1) ; sym e])
                           (v:=[state q2; sym e0; sym new]).
(* Goal: iff (SR_fin (@existT finType (fun sig : finType => prod (prod (srs (eqType_X (type sig))) (list (eqType_X (type sig)))) (list (eqType_X (type sig)))) Sigma (@pair (prod (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma)))) (list (eqType_X (type Sigma))) (@pair (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma))) R x0) y))) (SR (@pair (prod (list (prod (list nat) (list nat))) (list nat)) (list nat) (@pair (list (prod (list nat) (list nat))) (list nat) (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0)) (@map (eqType_X (type Sigma)) nat (@index Sigma) y))) *)
           apply Sub. cbn. right. apply in_map_iff. exists e0. now split.
(* Goal: iff (SR_fin (@existT finType (fun sig : finType => prod (prod (srs (eqType_X (type sig))) (list (eqType_X (type sig)))) (list (eqType_X (type sig)))) Sigma (@pair (prod (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma)))) (list (eqType_X (type Sigma))) (@pair (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma))) R x0) y))) (SR (@pair (prod (list (prod (list nat) (list nat))) (list nat)) (list nat) (@pair (list (prod (list nat) (list nat))) (list nat) (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0)) (@map (eqType_X (type Sigma)) nat (@index Sigma) y))) *)
       + destruct l0; cbn. 
         * rewrite map_app. rewrite <- app_assoc.
           apply rewR with (x:= # :: List.map sym (List.rev l))
                           (u:= [state (cstate c1) ; sym e]) (y:=[$])
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
                           (v:=[sym new; state q2]). firstorder. 
         * rewrite map_app. rewrite <- app_assoc.
           apply rewR with (x:=#::List.map sym (List.rev l))
                           (u:= [state (cstate c1) ; sym e])
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
                           (v:=[sym new; state q2]). firstorder. 
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
       + apply rewR with (x:=# :: List.map sym (List.rev l)) (u:= [state (cstate c1); sym e])
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
                         (v:=[state q2; sym new]). firstorder.
(* Goal: iff (SR_fin (@existT finType (fun sig : finType => prod (prod (srs (eqType_X (type sig))) (list (eqType_X (type sig)))) (list (eqType_X (type sig)))) Sigma (@pair (prod (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma)))) (list (eqType_X (type Sigma))) (@pair (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma))) R x0) y))) (SR (@pair (prod (list (prod (list nat) (list nat))) (list nat)) (list nat) (@pair (list (prod (list nat) (list nat))) (list nat) (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0)) (@map (eqType_X (type Sigma)) nat (@index Sigma) y))) *)
       + destruct l; cbn. 
         * apply rewR with (x:=[])
                           (u:= [#;state (cstate c1) ; sym e])
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
                           (v:=[state q2; #; sym e]). firstorder. 
         * rewrite map_app. rewrite <- app_assoc.
           apply rewR with (x:=#::List.map sym (List.rev l))
                           (u:= [sym e0; state (cstate c1) ; sym e])
                           (v:=[state q2; sym e0; sym e]).
(* Goal: iff (SR_fin (@existT finType (fun sig : finType => prod (prod (srs (eqType_X (type sig))) (list (eqType_X (type sig)))) (list (eqType_X (type sig)))) Sigma (@pair (prod (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma)))) (list (eqType_X (type Sigma))) (@pair (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma))) R x0) y))) (SR (@pair (prod (list (prod (list nat) (list nat))) (list nat)) (list nat) (@pair (list (prod (list nat) (list nat))) (list nat) (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0)) (@map (eqType_X (type Sigma)) nat (@index Sigma) y))) *)
           apply Sub. cbn. right. apply in_map_iff. exists e0. now split.
(* Goal: iff (SR_fin (@existT finType (fun sig : finType => prod (prod (srs (eqType_X (type sig))) (list (eqType_X (type sig)))) (list (eqType_X (type sig)))) Sigma (@pair (prod (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma)))) (list (eqType_X (type Sigma))) (@pair (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma))) R x0) y))) (SR (@pair (prod (list (prod (list nat) (list nat))) (list nat)) (list nat) (@pair (list (prod (list nat) (list nat))) (list nat) (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0)) (@map (eqType_X (type Sigma)) nat (@index Sigma) y))) *)
       + destruct l0; cbn. 
         * rewrite map_app. rewrite <- app_assoc.
            apply rewR with (x:=# :: List.map sym (List.rev l))
                            (u:= [state (cstate c1) ; sym e])
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
                            (v:=[sym e; state q2]). firstorder. 
         * rewrite map_app. rewrite <- app_assoc.
           apply rewR with (x:=#::List.map sym (List.rev l))
                           (u:= [state (cstate c1) ; sym e])
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
                           (v:=[sym e; state q2]). firstorder. 
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
       + apply rewR with (x:=# :: List.map sym (List.rev l))
                         (u:= [state (cstate c1); sym e])
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
                       (v:=[state q2; sym e]). firstorder. 
  Qed.   
  

  Lemma state_eqlist A B C D q1 q2:
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
    A ++(state q1)::B = C++(state q2)::D
    -> (exists (E F: list rsig'), C = List.map symb E /\ D = List.map symb F)
    -> A = C /\ B = D /\ q1 = q2.
  Proof.
(* Goal: forall (_ : @eq (list rsig) (@app rsig A (@cons rsig (state q1) B)) (@app rsig C (@cons rsig (state q2) D))) (_ : @ex (list rsig') (fun E : list rsig' => @ex (list rsig') (fun F : list rsig' => and (@eq (list rsig) C (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb E)) (@eq (list rsig) D (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb F))))), and (@eq (list rsig) A C) (and (@eq (list rsig) B D) (@eq (eqType_X (type (@states sig T))) q1 q2)) *)
    revert C. induction A; intros C H HA; destruct C; try rewrite !app_nil_l in H.
    - inv H. now split.
    - inv H. destruct HA as [E [F [H1 H2]]]. destruct E; inv H1.
    - rewrite <- app_comm_cons in H. inv H. destruct HA as [E [F [H1 H2]]].
      exfalso. assert ((state q1) el (A++(state q1)::B) -> False). intros H.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
      rewrite H2 in H. apply in_map_iff in H as [x [? ?]]. inv H. apply H. auto.
    - inv H. specialize (IHA C H2). destruct HA as [E [F [H3 H4]]]. destruct E,F.
      inv H3. inv H3. cbn in H3. inv H3. destruct IHA as [<- [<- <-]]. exists E, []. now split.
      now split. cbn in H3. inv H3. destruct IHA as [<- [<- <-]]. exists E, (r1::F). now split.
(* Goal: iff (@rewt' (eqType_X (type Sigma)) R x0 y) (rewt (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0) (@map (eqType_X (type Sigma)) nat (@index Sigma) y)) *)
      now split.
  Qed.

  Fact map_symb_sigma A :
    List.map symb (List.map sigma A) = List.map sym A.
  Proof.
    induction A. auto. cbn. now rewrite <- IHA.
  Qed.

  (* Automation for rewrite_step_halt Lemma. ListInv mainly eliminates contradictory
     assumptions while state_inv also solves cases *)

  Ltac listInv :=
    cbn in *;
    match goal with
    | [H: sym _ = sym _ |- _ ] => inv H
    | [H: [] = _ ::_  |- _ ] => inv H
    | [H: _::_ = _::_ |- _ ] => inv H 
    | [H: []++ _ = _ |- _ ] => rewrite app_nil_l in H
    | [H: _ ++[] = _ |- _ ] => rewrite app_nil_r in H
    | [H: _ = []++ _ |- _ ] => rewrite app_nil_l in H
    | [H: _ = _ ++[] |- _ ] => rewrite app_nil_r in H
    | [H: [] = List.map sym ?L |- _ ] => destruct L eqn: ?
    | [H: List.rev ?L = [] |- _] => apply rev_nil in H        
    | [H: _ ++ _::_ = [] |- _ ] => symmetry in H; now apply app_cons_not_nil in H               
    | [H: [] = List.map _ (List.rev (_) ++ ?e) |- _ ] =>
      apply map_app in H; now apply app_cons_not_nil in H
    | [H: _ ++ [#] = List.map sym (List.rev _ ++ ?e) |- _ ] =>
      rewrite map_app in H; let E:= fresh "E" in apply last_app_eq in H as [_ E]; inv E      
    | [H: ?x ++ [_] = #::(List.map sym ?L) |- _ ] => destruct x eqn: ?
    | [H: _ ++ [#] = List.map sym (List.rev ?L) |- _ ] => destruct L eqn: ?
    | [H: _ ++ [sym _] = List.map sym (List.rev ?L) |- _ ] => destruct L eqn:?
    | [H: _ ++ [sym _] = List.map sym (_ ++ [_]) |- _ ] =>
      rewrite map_app in H; apply last_app_eq in H as [? ?]
    | [H: ?A ++ _::_ = _::_ |- _ ] => destruct A eqn: ?                    
(* Goal: @rewt' (eqType_X (type Sigma)) R x0 y *)
(* Goal: @eq (list (eqType_X (type Sigma))) x y *)
    end; subst.
  Ltac rep_listInv := repeat listInv.


  Ltac confeq :=
    unfold step; unfold mk_srconf;
    match goal with
      | [ |- (_::_ = _::_) /\ _ ] => split
      | [ |- (_::_ = _::_)] => reflexivity                            
      | [H: ctape _ = _ |- (_ = _) /\ _ ] => rewrite H
      | [H: trans(_,_) = _ |- _ ] => rewrite H          
      | [H: _ |- mk_mconfig _ _ = mk_mconfig _  _ /\ _ ] => split
      | [ |- mk_mconfig _ _ = mk_mconfig _ _ ] => reflexivity
      | [H: (?q,_) el list_prod not_halting_states _ |- halt ?q = false ] =>
        apply in_prod_iff in H as [? _]; now apply not_halting
      | [H: _ el not_halting_states |- halt _ = false] => now apply not_halting
        end; subst; cbn in *; try now rewrite !map_app, <- !app_assoc. 
  Ltac rep_confeq := repeat confeq.


  Ltac state_inv :=
    unfold get_rules_right in *;
    match goal with
    |[H: ?x ++ ?z::(state ?q)::?y = ?a::(?A++(state _ )::_) |- _] =>
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
     cbn in H; change (x++z::state q::y) with (x++[z]++(state q)::y) in H; rewrite app_assoc in H;
     rewrite app_comm_cons in H; eapply state_eqlist in H as [? [? ?]]
    |[H: ?x ++ state _ :: ?y = state _ ::_ |- _ = _ /\ _ ] =>
     apply state_eqlist with (A:= x) (B:= y) (C:= []) in H as [? [? ?]]
    |[H: ?x ++ ?z::state ?q :: ?y = state _ ::_ |- _ = _ /\ _ ] =>
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
     change (x++z::state q::y) with (x++[z]++state q::y) in H; rewrite app_assoc in H;
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
     apply state_eqlist with (A:=x++[z]) (B:= y) (C:= []) in H as [? [? ?]]
    |[H: ?x ++(state _ )::?y = ?a::(?A ++ (state _ )::_) |- _ ] =>
     cbn in H; rewrite app_comm_cons in H; eapply state_eqlist in H as [? [? ?]]
    |[H: (_,_) el List.map _ _ |- (_ = _) /\ _] => apply in_map_iff in H as [? [? ?]]
    |[H: (_,_) = (_,_) |- _ ] => inv H
    |[H: _ |- exists E F, [] = _ /\ [#;$] = _ ] => exists [],[hash; dollar]; now split
    |[H: _ |- exists E F, [] = _ /\ #::sym ?e::List.map sym ?L++[$] = _ ] =>
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
     exists [],(hash::sigma e::List.map sigma L++[dollar]); split; auto; cbn;
     rewrite map_app, map_symb_sigma; now cbn
    |[H: _ |- exists E F, #::(List.map sym ?A) = _ /\ [$] = _ ] =>
     exists (hash::(List.map sigma A)), [dollar]; split; auto; cbn;
     rewrite !map_app, map_symb_sigma; now cbn
    |[H: _ |- exists E F, #::(List.map sym ?A) = _ /\ sym ?e::(List.map sym ?L)++[$] = _ ] =>
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
     exists (hash::(List.map sigma A)), (sigma e::(List.map sigma L)++[dollar]);
     split; cbn; try rewrite map_app; now rewrite map_symb_sigma
    end.
  
  Ltac solve_inv := repeat (state_inv || confeq || listInv).

  Lemma rewrite_step_halt (c1: conf) y:
    rew' TMrules (mk_srconf c1) y -> (y = (mk_srconf (step c1)) /\ (halt (cstate c1) = false)).
  Proof.
(* Goal: forall _ : @rewt' rsig TMrules y b, @reach sig T c1 c2 *)
    intros H. remember (mk_srconf c1) as a. inv H.
(* Goal: and (@eq (list rsig) (@app rsig x (@app rsig v y0)) (mk_srconf (@step sig T c1))) (@eq bool (@halt sig T (@cstate sig (@states sig T) c1)) false) *)
    apply in_concat_iff in H0 as [L [EL HL]]. unfold mk_srconf in H2. 
    destruct (ctape c1) eqn: CT1; cbn in *; apply in_app_iff in HL as [HL|HL];
      try apply in_map_iff in HL as [z [HZ ZF]]; try (destruct z as (q1, old));
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
        try (destruct (trans(z,None)) as (qz,(o,m)) eqn: TZ +
            destruct (trans (q1, Some old)) as (q2, (o, m)) eqn: TZ);
        destruct o as [new| ]; destruct m; cbn in HZ; subst; destruct EL;
          try apply in_sing in H; try inv H; solve_inv; destruct l0; cbn; rep_confeq.
  Qed.
  
  Lemma rewrite_steps_halt (c1: conf) y:
    rewt' TMrules (mk_srconf c1) y -> exists c2, y = (mk_srconf c2).
  Proof.
(* Goal: forall _ : @rewt' rsig TMrules y b, @reach sig T c1 c2 *)
    intros H. induction H using rewt'_ind_left. 
    - eauto.
    - destruct IHrewt' as [? ->]. eapply rewrite_step_halt in H0 as [-> ?]. eauto.
  Qed.

  Theorem reach_rewrite (c1 c2:conf):
    reach c1 c2 <-> rewt' TMrules (mk_srconf c1) (mk_srconf c2).
  Proof.
(* Goal: iff (@rewt' (eqType_X (type Sigma)) R x0 y) (rewt (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0) (@map (eqType_X (type Sigma)) nat (@index Sigma) y)) *)
    split.
    - induction 1. constructor. apply rewt'Rule with (y:=(mk_srconf (step c))).
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
      + now apply rewrite_step_conf.
(* Goal: @eq bool (@halt sig T (@cstate sig (@states sig T) c1)) false *)
      + assumption.
    - remember (mk_srconf c1) as y. remember (mk_srconf c2) as b.
(* Goal: forall _ : @rewt' rsig TMrules y b, @reach sig T c1 c2 *)
       intros H. revert Heqy. revert c1. induction H; intros c1 Heqy; subst. 
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
       + apply mk_srconf_inj in Heqy. subst. constructor.
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
       + apply rewrite_step_halt in H as [H1 H]. subst.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
         apply reachS. apply IHrewt'; auto. assumption.
  Qed.

  Definition reduction_reach_rewrite (p: conf * conf) :=
      let (c1,c2) := p in (TMrules, mk_srconf c1, mk_srconf c2). 

End Fix_TM.

Require Import PCP.Definitions.

Theorem reduction_reach_sr : Reach ⪯ SR_fin.
Proof. exists (fun S
                      => existT (fun (sig:finType) => (prod (prod (srs sig) (list sig)) (list sig)))
                               (rsig_finType (projT1 (projT2 S)))
                               (reduction_reach_rewrite (projT2 (projT2 S)))).
(* Goal: iff (SR_fin (@existT finType (fun sig : finType => prod (prod (srs (eqType_X (type sig))) (list (eqType_X (type sig)))) (list (eqType_X (type sig)))) Sigma (@pair (prod (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma)))) (list (eqType_X (type Sigma))) (@pair (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma))) R x0) y))) (SR (@pair (prod (list (prod (list nat) (list nat))) (list nat)) (list nat) (@pair (list (prod (list nat) (list nat))) (list nat) (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0)) (@map (eqType_X (type Sigma)) nat (@index Sigma) y))) *)
       intros [sig [M [c1 c2]]]. unfold Reach, SR.  cbn. apply reach_rewrite.
Qed.

(** ** SR with finite types to SR *)

Definition trans_R (Sigma : finType) (R : srs Sigma) :=
  map (fun '(x,y) => (map (@index Sigma) x, map (@index Sigma) y)) R.

Lemma red_rew     (Sigma : finType) (R : srs Sigma) (x0 y : list Sigma) :
    rew' R x0 y <-> rew (trans_R R) (map (index (X:=Sigma)) x0) (map (index (X:=Sigma)) y).
Proof.
(* Goal: iff (@rewt' (eqType_X (type Sigma)) R x0 y) (rewt (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0) (@map (eqType_X (type Sigma)) nat (@index Sigma) y)) *)
  split.
  - intros []. cbn. autorewrite with list. econstructor.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
    eapply in_map_iff. exists (u/v). eauto.
  - inversion 1. unfold trans_R in H. 
    symmetry in H0. eapply map_app_inv in H0 as (x0' & x0'' & -> & <- & (y' & y'' & -> & <- & <-) % map_app_inv ).
(* Goal: @rew' (eqType_X (type Sigma)) R (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) y' y'')) y *)
    unfold trans_R in H2. eapply in_map_iff in H2 as ([] & ? & ?). inv H0.
    rewrite <- !map_app in H1. enough (l = y' /\ x0' ++ l0 ++ y'' =  y) as [-> <-].
(* Goal: rew (@trans_R Sigma R0) (@app nat (@map (eqType_X (type Sigma)) nat (@index Sigma) x) (@app nat (@map (eqType_X (type Sigma)) nat (@index Sigma) u) (@map (eqType_X (type Sigma)) nat (@index Sigma) y0))) (@app nat (@map (eqType_X (type Sigma)) nat (@index Sigma) x) (@app nat (@map (eqType_X (type Sigma)) nat (@index Sigma) v) (@map (eqType_X (type Sigma)) nat (@index Sigma) y0))) *)
    now econstructor.
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
    + split; eapply map_inj; eauto using inj_index.
Qed.    


Lemma rew_inv    (Sigma : finType) (R : srs Sigma) (x0 : list Sigma) y' :
    rew (trans_R R) (map (index (X:=Sigma)) x0) y' -> exists y, y' = (map (index (X:=Sigma)) y).
Proof.
(* Goal: @rewt' (eqType_X (type Sigma)) R x0 y *)
(* Goal: @eq (list (eqType_X (type Sigma))) x y *)
  inversion 1; subst. unfold trans_R in H1. eapply in_map_iff in H1 as ([] & ? & ?). inv H1.
(* Goal: @rewt' (eqType_X (type Sigma)) R x0 y *)
(* Goal: @eq (list (eqType_X (type Sigma))) x y *)
  symmetry in H0. eapply map_app_inv in H0 as (x0' & x0'' & ? & ? & (y' & y'' & ? & ? & ?) % map_app_inv ); subst.
(* Goal: @rewt' (eqType_X (type Sigma)) R x0 y *)
(* Goal: @eq (list (eqType_X (type Sigma))) x y *)
  rewrite map_inj in H4; eauto using inj_index. subst.
  eexists. rewrite <- !map_app. reflexivity.
Qed.

Lemma nrewt_ind_left :
  forall (R : SRS) s (P : string -> Prop),
    (P s) ->
    (forall y z : string, rewt R s y -> rew R y z -> P y -> P z) -> forall s0 : string, rewt R s s0 -> P s0.
Proof.
(* Goal: forall _ : rewt (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0) (@map (eqType_X (type Sigma)) nat (@index Sigma) y), @rewt' (eqType_X (type Sigma)) R x0 y *)
  intros. induction H1; eauto 10 using rewt.
Qed.

Lemma rewt_inv    (Sigma : finType) (R : srs Sigma) (x0 : list Sigma) y' :
    rewt (trans_R R) (map (index (X:=Sigma)) x0) y' -> exists y, y' = (map (index (X:=Sigma)) y) /\ rewt' R x0 y.
Proof.
(* Goal: or (@In (list (prod (list rsig) (list rsig))) (get_rules q1 q2 (@None (eqType_X (type sig))) new m) (@map (eqType_X (type (@states sig T))) (list (prod (list rsig) (list rsig))) (fun q1 : eqType_X (type (@states sig T)) => let (q2, p) := @trans sig T (@pair (eqType_X (type (@states sig T))) (option (eqType_X (type sig))) q1 (@None (eqType_X (type sig)))) in let (o, m) := p in get_rules q1 q2 (@None (eqType_X (type sig))) o m) not_halting_states)) (@In (list (prod (list rsig) (list rsig))) (get_rules q1 q2 (@None (eqType_X (type sig))) new m) (@map (prod (eqType_X (type (@states sig T))) (eqType_X (type sig))) (list (prod (list rsig) (list rsig))) (fun P : prod (eqType_X (type (@states sig T))) (eqType_X (type sig)) => let (q1, s) := P in let (q2, p) := @trans sig T (@pair (eqType_X (type (@states sig T))) (option (eqType_X (type sig))) q1 (@Some (eqType_X (type sig)) s)) in let (o, m) := p in get_rules q1 q2 (@Some (eqType_X (type sig)) s) o m) (@list_prod (eqType_X (type (@states sig T))) (eqType_X (type sig)) not_halting_states (elem sig)))) *)
  intros. induction H using nrewt_ind_left.
  - eauto using rewt'. 
  - destruct IHrewt as (? & -> & ?). pose proof H0. eapply rew_inv in H0 as [? ->].
(* Goal: @eq (eqType_X (type (@states sig M))) (@cstate sig (@states sig M) x) (@cstate sig (@states sig M) x) *)
(* Goal: @eq (eqType_X (type (@states sig M))) x0 (@cstate sig (@states sig M) x) *)
    exists x1. eapply red_rew in H2. split; try reflexivity. eapply rewrite_right; eauto.
Qed.

Lemma red_rewt    (Sigma : finType) (R : srs Sigma) (x0 y : list Sigma) :
    rewt' R x0 y <-> rewt (trans_R R) (map (index (X:=Sigma)) x0) (map (index (X:=Sigma)) y).
Proof.
(* Goal: iff (@rewt' (eqType_X (type Sigma)) R x0 y) (rewt (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0) (@map (eqType_X (type Sigma)) nat (@index Sigma) y)) *)
  split.
  - induction 1. reflexivity.
(* Goal: rewt (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x) (@map (eqType_X (type Sigma)) nat (@index Sigma) z) *)
    eapply red_rew in H. eauto using rewt.
  - intros. eapply rewt_inv in H as (? & ? & ?). enough (x = y). subst. eassumption.
(* Goal: @eq (list (eqType_X (type Sigma))) x y *)
    eapply map_inj; eauto using inj_index.
Qed.

Lemma reduction : SR_fin ⪯ SR.
Proof.
(* Goal: @reduces (@sigT finType (fun sig : finType => prod (prod (srs (eqType_X (type sig))) (list (eqType_X (type sig)))) (list (eqType_X (type sig))))) (prod (prod SRS string) string) SR_fin SR *)
  exists (fun '(existT _ sig (Rs,x,y)) => (trans_R Rs, map (@index sig) x, map (@index sig) y)).
(* Goal: iff (SR_fin (@existT finType (fun sig : finType => prod (prod (srs (eqType_X (type sig))) (list (eqType_X (type sig)))) (list (eqType_X (type sig)))) Sigma (@pair (prod (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma)))) (list (eqType_X (type Sigma))) (@pair (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma))) R x0) y))) (SR (@pair (prod (list (prod (list nat) (list nat))) (list nat)) (list nat) (@pair (list (prod (list nat) (list nat))) (list nat) (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0)) (@map (eqType_X (type Sigma)) nat (@index Sigma) y))) *)
  intros [Sigma ((R, x0),  y)]. cbn. eapply red_rewt.
Qed.

(** ** TM to SRH' *)

Definition SRH' '(Rs, x, A) := exists y, rewt Rs x y /\ exists a, a el A /\ a el y.

Lemma mk_srconf_state:
  forall (sig : finType) (M : sTM sig) (x : conf M) (x0 : states M), state x0 el mk_srconf x -> x0 = cstate x.
Proof.
  intros sig M x x0 H2. unfold mk_srconf in *. destruct _; cbn in *; rewrite ?in_app_iff in *.
  - intuition; congruence.
  - intuition; try congruence. inv H. eapply in_map_iff in H0 as (? & ? & ?). inv H. inv H0. inv H. inv H.
  - intuition; try congruence. eapply in_map_iff in H0 as (? & ? & ?). inv H. inv H0. inv H. reflexivity. inv H. inv H0. inv H0.
  - intuition; try congruence. eapply in_map_iff in H0 as (? & ? & ?). inv H. cbn in *. intuition. now inv H. inv H0.
(* Goal: @eq (eqType_X (type (@states sig M))) x0 (@cstate sig (@states sig M) x) *)
    eapply in_app_iff in H0. intuition. eapply in_map_iff in H as (? & ? & ?). inv H. inv H. inv H0. inv H0.
Qed.

Lemma halt_SRH' : Halt ⪯ SRH'.
Proof.
  exists (fun '(existT2 _ _ sig M t) => (trans_R (@TMrules sig M), map (@index (FinType (EqType (rsig M)))) (@mk_srconf sig M (@initc sig M t)), map (@index (FinType (EqType (rsig M)))) (map (@state sig M) (@halting_states sig M)))).
(* Goal: iff (SR_fin (@existT finType (fun sig : finType => prod (prod (srs (eqType_X (type sig))) (list (eqType_X (type sig)))) (list (eqType_X (type sig)))) Sigma (@pair (prod (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma)))) (list (eqType_X (type Sigma))) (@pair (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma))) R x0) y))) (SR (@pair (prod (list (prod (list nat) (list nat))) (list nat)) (list nat) (@pair (list (prod (list nat) (list nat))) (list nat) (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0)) (@map (eqType_X (type Sigma)) nat (@index Sigma) y))) *)
  intros [sig M]. cbn. unfold Halt, Halt. cbn. split; intros []; intuition.
  - eapply reach_rewrite in H1.
(* Goal: @eq bool (@halt sig T (@cstate sig (@states sig T) c1)) false *)
    eapply red_rewt in H1. eexists. split. eassumption. exists (index (state (cstate x))).
    rewrite !in_map_iff. intuition; eexists.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
    + rewrite in_map_iff. intuition. eexists; intuition.  eapply halting. eauto.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
    + intuition.  unfold mk_srconf. destruct _; cbn; eauto.
  - destruct H1. intuition. destruct (rewt_inv H0) as [? [-> ?]].
    pose proof H.
    eapply rewrite_steps_halt in H as [? ->].
    eapply in_map_iff in H1 as (? & <- & ?). eapply in_map_iff in H2 as (? & ? & ?).
    eapply inj_index in H1 as ->. eapply in_map_iff in H as (? & <- & ?).
(* Goal: @rewt' (eqType_X (type Sigma)) R x0 y *)
(* Goal: @eq (list (eqType_X (type Sigma))) x y *)
    eapply halting in H. eapply mk_srconf_state in H2. subst.
(* Goal: iff (SR_fin (@existT finType (fun sig : finType => prod (prod (srs (eqType_X (type sig))) (list (eqType_X (type sig)))) (list (eqType_X (type sig)))) Sigma (@pair (prod (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma)))) (list (eqType_X (type Sigma))) (@pair (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma))) R x0) y))) (SR (@pair (prod (list (prod (list nat) (list nat))) (list nat)) (list nat) (@pair (list (prod (list nat) (list nat))) (list nat) (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0)) (@map (eqType_X (type Sigma)) nat (@index Sigma) y))) *)
    exists x. cbn. intuition. 
(* Goal: @eq bool (@halt sig T (@cstate sig (@states sig T) c1)) false *)
    eapply reach_rewrite. eassumption.
Qed.

(** ** SRH' to SRH *)

Lemma rewt_R_fresh (R : SRS) (x : string) (A l : list symbol) :
  rewt (R ++ map (fun a : symbol => [a] / [fresh (sym R ++ x)]) A) x l -> ~ fresh (sym R ++ x) el l -> rewt R x l.
Proof.
(* Goal: forall _ : rewt (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0) (@map (eqType_X (type Sigma)) nat (@index Sigma) y), @rewt' (eqType_X (type Sigma)) R x0 y *)
  induction 1 using nrewt_ind_left; intros.
  - reflexivity.
  - inv H0. eapply in_app_iff in H2 as [|].
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
    + eapply rewt_left. eapply IHrewt.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
      * intros [ | [|] % in_app_iff ] %in_app_iff; eapply H1; eauto.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
        eapply sym_word_l in H2; eauto. edestruct fresh_spec with (l := sym R ++ x); eauto.
      * eauto using rew.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
    + eapply in_map_iff in H0 as (? & ? & ?). inv H0. destruct H1; eauto.
Qed.

Lemma SRH'_SRH : SRH' ⪯ SRH.
Proof.
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
  exists (fun '(P, x, A) => (P ++ map (fun a => [a] / [fresh (sym P ++ x)]) A, x, fresh (sym P ++ x))).
(* Goal: iff (SR_fin (@existT finType (fun sig : finType => prod (prod (srs (eqType_X (type sig))) (list (eqType_X (type sig)))) (list (eqType_X (type sig)))) Sigma (@pair (prod (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma)))) (list (eqType_X (type Sigma))) (@pair (srs (eqType_X (type Sigma))) (list (eqType_X (type Sigma))) R x0) y))) (SR (@pair (prod (list (prod (list nat) (list nat))) (list nat)) (list nat) (@pair (list (prod (list nat) (list nat))) (list nat) (@trans_R Sigma R) (@map (eqType_X (type Sigma)) nat (@index Sigma) x0)) (@map (eqType_X (type Sigma)) nat (@index Sigma) y))) *)
  intros [ [R x] A]. cbn. firstorder.
  - eapply in_split in H1 as (? & ? & ->). exists (x2 ++ [fresh (sym R ++ x)] ++ x3). split.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
    + transitivity (x2 ++ [x1] ++ x3). eapply rewt_subset. eassumption. eauto.
      eauto 9 using rew, rewt.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
    + eauto.
  -  revert H0. induction H using nrewt_ind_left; intros.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
    + edestruct fresh_spec with (l := sym R ++ x); eauto.
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
    + edestruct (@in_dec nat (ltac:(intros; decide equality)) (fresh (sym R ++ x)) y).
(* Goal: @In (prod (list rsig) (list rsig)) (@pair (list rsig) (list rsig) (@cons rsig (state (@cstate sig (@states sig T) c1)) (@cons rsig (sym e) (@nil rsig))) (@cons rsig (state q2) (@cons rsig (sym e) (@nil rsig)))) TMrules *)
      firstorder. inv H0. eapply in_app_iff in H2 as [|].
(* Goal: @eq nat (if @Dec (@eq (eqType_X Y) (f a) (f a)) (@eqType_dec Y (f a) (f a)) then Datatypes.S (@count (eqType_X Y) (@eqType_dec Y) (@map (eqType_X S) (eqType_X Y) (fun a : eqType_X S => f a) A) (f a)) else @count (eqType_X Y) (@eqType_dec Y) (@map (eqType_X S) (eqType_X Y) (fun a : eqType_X S => f a) A) (f a)) n *)
(* Goal: @eq nat (if @Dec (@eq (eqType_X Y) (f s) (f a)) (@eqType_dec Y (f s) (f a)) then Datatypes.S (@count (eqType_X Y) (@eqType_dec Y) (@map (eqType_X S) (eqType_X Y) (fun a : eqType_X S => f a) A) (f s)) else @count (eqType_X Y) (@eqType_dec Y) (@map (eqType_X S) (eqType_X Y) (fun a : eqType_X S => f a) A) (f s)) n *)
      * destruct n. rewrite !in_app_iff in H1. intuition; eauto.
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
        eapply sym_word_R in H1; eauto. edestruct fresh_spec with (l := sym R ++ x); eauto.
(* Goal: and (@eq (list (eqType_X (type Sigma))) l y') (@eq (list (eqType_X (type Sigma))) (@app (eqType_X (type Sigma)) x0' (@app (eqType_X (type Sigma)) l0 y'')) y) *)
      * eapply in_map_iff in H0 as (? & ? & ?); inv H0. exists (x0 ++ [x1] ++ y0).
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@nil (eqType_X (type sig))))) (@map (eqType_X (type sig)) rsig sym (@nil (eqType_X (type sig)))) *)
(* Goal: @eq (list rsig) (@map (eqType_X (type (@FinType (@EqType rsig' eq_dec_rsig') finType_rsig'))) rsig symb (@map (eqType_X (type sig)) rsig' sigma (@cons (eqType_X (type sig)) a A))) (@map (eqType_X (type sig)) rsig sym (@cons (eqType_X (type sig)) a A)) *)
        split; firstorder. eapply rewt_R_fresh; eauto.
Qed.

Lemma Halt_SRH : Halt ⪯ SRH.
Proof.
  eapply reduces_transitive. eapply halt_SRH'. eapply SRH'_SRH.
Qed.

