Require Import List Arith Omega NPeano.

(** ** Préliminaires *)
Parameter X :Set.

Definition mot := list X.

Definition eps := @nil X.

Lemma decomposition : forall u1 u2 u3 u4 : mot, u1  ++  u2 = u3 ++ u4 -> 
exists u5 : mot, (u1=u3 ++ u5 /\ u4=u5 ++ u2) \/ (u3=u1 ++ u5 /\ u2=u5 ++ u4).
(** Ce lemme est essentiel. Toutes les preuves des lemmes ci-dessous l'utilisent intensivement. 

% Graphiquement, cela signifie que si $ab=cd$ alors:
\begin{equation}
  \begin{array}{|c|c|c|}
    \hline
    \multicolumn{2}{|c|}{a}&b\\
    \hline
    c&x&b\\
    \hline
    c&\multicolumn{2}{c|}{d}\\
    \hline
  \end{array}
  \text { ou }
  \begin{array}{|c|c|c|}
    \hline
    a&\multicolumn{2}{c|}{b}\\
    \hline
    a&x&d\\
    \hline
    \multicolumn{2}{|c|}{c}&d\\
    \hline
  \end{array}
\end{equation}% *)
Proof.
induction u1.
intros; exists u3; right;split; simpl; auto.
destruct u3.
intros; exists (a::u1); left;split; simpl; auto.
intros.
repeat rewrite<- app_comm_cons in *.
inversion H.
rewrite<- H1 in *.
apply IHu1 in H2.
destruct H2.
exists x0; case H0;intros;destruct H2;
[left|right]; rewrite H2;rewrite H3; auto.
Qed.

Lemma length_eps : forall x : mot, length x = 0 -> x = eps.
Proof.
intros x h;destruct x;[auto|simpl in h;discriminate].
Qed.

Parameter prime : X -> X.

Axiom prime_inv : forall x, prime(prime x) = x.

Axiom prime_dif : forall x, prime x <> x.

Fixpoint miroir (u : mot) : mot :=
match u with 
| nil => eps
| a::v => miroir v  ++  ((prime a)::eps)
end.
Notation "w `" := (miroir w) (at level 10).

Definition reduction (u v : mot) := 
exists u1,exists u2, exists w, u=u1 ++ w ++  (w ` )  ++ w ++ u2 /\ v=u1 ++ w ++ u2.

Inductive reduction_borne : nat -> mot -> mot -> Prop :=
| ridb : forall u, reduction_borne 0 u u
| rtransb : forall u v w n, reduction u v -> reduction_borne n v w -> 
  reduction_borne (S n) u w
| rloose : forall u v n m, (n<m) -> reduction_borne n u v -> reduction_borne m u v.

Definition joinable_borne n (u1 u2 : mot) : Prop := 
exists v, reduction_borne n u1 v /\ reduction_borne n u2 v.

Lemma mir_app : forall u v, (u ++ v) `  = v `   ++  u ` .
Proof.
induction u;intros;simpl.
rewrite<- app_nil_end;auto.
rewrite IHu;rewrite app_ass;auto.
Qed.

Lemma mirr_inv : forall u, u `  `  = u.
Proof.
induction u;intros;simpl.
auto.
rewrite mir_app;simpl.
rewrite prime_inv;f_equal;exact IHu.
Qed.

Lemma bij_mir : forall u v, u=v <-> u ` = v ` .
Proof.
intros u v;split; intro h.
rewrite h; auto.
rewrite<- mirr_inv;rewrite<- h;symmetry; apply mirr_inv. 
Qed.

Lemma mir_len : forall u, length u = length (u ` ).
Proof.
induction u; auto.
simpl;rewrite app_length.
rewrite IHu; rewrite plus_comm; auto.
Qed.

Lemma redb_refl : forall u n, reduction_borne n u u.
Proof.
intros u n;destruct n;[|apply (rloose _ _ 0);[omega|]];apply ridb.
Qed.

Lemma joinb_refl : forall u n, joinable_borne n u u.
Proof.
intro u;exists u;split;apply redb_refl. 
Qed.

Lemma joinb_sym : forall u v n, joinable_borne n u v -> joinable_borne n v u.
Proof.
intros u v n (v0 & h1 & h2);exists v0;split;auto.
Qed.

Lemma join_loose : forall n u v m, joinable_borne n u v -> n<m -> joinable_borne m u v.
Proof.
intros n u v m (v0&h1&h2) h3;exists v0;split;apply (rloose _ _ n);auto.
Qed.

Lemma trans_redb : forall u v w n m, 
reduction_borne n u v -> reduction_borne m v w -> reduction_borne (n+m) u w.
Proof.
intros u v w n m h1 h2.
induction h1;simpl;auto.
apply (rtransb _ v);auto.
apply (rloose _ _ (n+m));auto;omega.
Qed.

Lemma joinb_step_l : forall v1 u1 u2 m, reduction u1 v1 -> 
  joinable_borne m v1 u2 -> joinable_borne (S m) u1 u2.
Proof.
intros v1 u1 u2 m h3 (v0 & h1 & h2).
exists v0;split;auto.
apply (rtransb _ v1);auto.
apply (rloose _ _ m);auto;omega.
Qed.

Lemma joinb_step_r : forall v2 u1 u2 m, reduction u2 v2 -> 
  joinable_borne m u1 v2 -> joinable_borne (S m) u1 u2.
Proof.
intros v2 u1 u2 m h3 (v0 & h1 & h2);
exists v0;split;[|apply (rtransb _ v2)];auto.
apply (rloose _ _ m);auto;omega.
Qed.

Lemma dec_eps : forall w : mot, w= eps \/ w<> eps.
Proof. destruct w;[left|right;intro;discriminate];auto. Qed.

(* begin hide *)
Ltac inductionmot u :=
  induction u using (well_founded_ind (well_founded_ltof mot (@length X))).

Ltac simplmots :=
repeat rewrite<- app_nil_end in *; repeat rewrite prime_inv in *;
repeat rewrite mir_app in *; repeat rewrite mirr_inv in *; 
repeat rewrite app_ass in *.

Ltac decomp h :=
let x := fresh "t" in
let h0 :=fresh "H" in
let h1 := fresh "H" in
let h2 := fresh "H" in
case (decomposition _ _ _ _ h); intros x h0; case h0; clear h0; intro h1;
destruct h1 as (h1,h2); rewrite h1 in *;rewrite h2 in *;
simplmots;clear h.

Ltac provered u1 w u2 :=
unfold reduction; exists u1; exists u2; 
exists w; simplmots; auto.

Ltac proveredrtb u1 w u2 :=
apply (rtransb _ (u1 ++ w ++ u2)); simplmots;
[provered u1 w u2| apply redb_refl]; auto.

Ltac provered_trb u1 w u2 :=
apply (rtransb _ (u1 ++ w ++ u2)); simplmots;
[provered u1 w u2|]; auto.

Ltac pat u tac :=
  let rec reassoc l r := 
    match constr:(l,r) with
      | (eps,_) => r
      | (?a ++ ?b,eps) => reassoc a b
      | (?a ++ ?b,_) => reassoc a (b ++ r)
    end
  in
  let rec s1 l r := 
    match r with 
      | ?a ++ ?a `  ++ ?r => s2 l a r
      | ?a `  ++ ?a ++ ?r => s2 l (a ` ) r
      | ?a ++ ?r => s1 (l ++ a) r
    end
  with s2 l w r := 
    match constr:(l,w,r) with 
      | (?l ++ ?a,_,?a `  ++ ?r) => s2 l (a ++ w) r
      | (?l ++ ?a ` ,_,?a ++ ?r) => s2 l (a `  ++ w) r
      | (_,?a,?a) => s3 l a eps eps
      | (_,?a,?a ++ ?r) => s3 l a eps r
      | (_,?a ++ ?w,?a ++ ?r) => s3 l (a ++ w) w r
    end
  with s3 l m w r := 
    match constr:(w,r) with 
      | (eps,_) => let l:=reassoc l eps in (tac l m r)
      | (?a,?a) => s3 l m eps eps
      | (?a,?a ++ ?r) => s3 l m eps r
      | (?a ++ ?w,?a ++ ?r) => s3 l m w r
    end
  in s1 eps u.

Ltac stepb :=
  simplmots;
  match goal with
    | [ _:_ |- reduction_borne _ ?u _ ] => (pat u provered_trb;try (apply redb_refl))
  end.

Ltac jidb := apply joinb_refl.

Ltac auxlb l m d :=
  apply (joinb_step_l (l ++ m ++ d));[provered l m d|].

Ltac auxrb l m d :=
  apply (joinb_step_r (l ++ m ++ d));[provered l m d|].

Ltac jsteplb := 
  simplmots;
  match goal with
    | [ _:_ |- joinable_borne _ ?u _ ] => (pat u auxlb;simpl;simplmots;try jidb)
  end.

Ltac jsteprb := 
  simplmots;
  match goal with
    | [ _:_ |- joinable_borne _ _ ?u ] => (pat u auxrb;simpl;simplmots;try jidb)
  end.

Ltac jrunb :=
  repeat jsteplb;repeat jsteprb.

Ltac repl_mir H :=
rewrite bij_mir in H;simplmots;rewrite H in *;simplmots;clear H.

Ltac app_eps H :=
  let h0 := fresh "Hyp" in
    simplmots;
    repeat 
      (apply app_eq_nil in H;destruct H as (h0,H);try rewrite h0 in *;
        try repl_mir h0; try clear h0);
      try rewrite H in *; try repl_mir H; try clear H.

Ltac destruct2 H :=
  let h := fresh "h" in
    destruct H as (h,H).

Ltac replacemot w1 w2 :=
  replace w1 with w2;[|simpl;simplmots;auto].

Ltac replacemotin w1 w2 h :=
  replace w1 with w2 in h;[|simpl;simplmots;auto].
(* end hide *)

Lemma dec_trivial : forall a b c, a = b ++ a ++ c -> b=eps/\c=eps.
Proof.
intros;assert (length (b ++ c) = 0).
apply (plus_reg_l _ _(length a)). 
symmetry;rewrite H at 1; repeat rewrite app_length.
omega.
apply length_eps in H0;apply app_eq_nil in H0;auto.
Qed.

(* begin hide *)
Ltac nested H :=
  let h0 := fresh "Hyp" in
    let h1 := fresh "Hyp" in
      let rec rest u v :=
        match constr:(u,v) with
          | (?a,?a) => eps
          | (?a,?a ++ ?b) => b
          | (?a ++ ?b,?a ++ ?c) => (rest b c)
        end
        in
        let rec find acc u v :=
          match constr:(u,v) with
            | (?a,?a) => constr:(acc,eps)
            | (?a,?a ++ ?b) => constr:(acc,b)
            | (?a ++ ?b,?a ++ ?c) => let r := rest b c in constr:(acc,r)
            | (?a,?b ++ ?c) => find (acc ++ b) a c
          end 
          in
          match goal with
            | [H:?u = ?v |- _]  => 
              let e := (find eps u v) in
                replacemotin v ((fst e) ++ u ++ (snd e)) H;
                simpl in H;apply dec_trivial in H;destruct H as (h0,h1);
                  app_eps h0;
                  app_eps h1;simpl in *;simplmots
          end.
(* end hide *)

Lemma dec_trivial_r : forall a b, a = a ++ b -> b=eps.
Proof.
intros;assert (length b = 0).
apply (plus_reg_l _ _(length a)). 
symmetry;rewrite H at 1; repeat rewrite app_length.
omega.
destruct b.
auto.
simpl in H0;discriminate.
Qed.

Lemma dec_trivial_l : forall a b, a = b ++ a -> b=eps.
Proof.
intros;assert (length b = 0).
apply (plus_reg_l _ _(length a)). 
symmetry;rewrite H at 1; repeat rewrite app_length.
omega.
destruct b.
auto.
simpl in H0;discriminate.
Qed.
  
Lemma len_app_aux : 
forall a b c d : mot, a ++ b=c ++ d -> length b = length d -> length a = length c.
Proof.
intros.
apply (plus_reg_l _ _ (length b)).
rewrite plus_comm; rewrite<- app_length;rewrite H.
rewrite plus_comm; rewrite H0; apply app_length.
Qed.

Lemma mir_red_  : forall u v,
reduction u v <-> reduction (u ` ) (v ` ).
Proof.
intros u v;split;intro h;unfold reduction in h;
destruct h as (u1 & u2 & w & h & h1);[|rewrite<- mirr_inv;rewrite<- (mirr_inv u)];
rewrite h;rewrite h1;simplmots;
provered (u2 ` ) (w ` ) (u1 ` ).
Qed.

Lemma cntxt_redb : forall v w u1 u2 n,
reduction_borne n v w -> reduction_borne n (u1 ++ v ++ u2) (u1 ++ w ++ u2).
Proof.
intros.
induction H.
apply ridb.
apply (rtransb _ (u1 ++ v ++ u2));auto.
unfold reduction in H;destruct H as (x & x0 & x1 & H & H1).
exists (u1 ++ x); exists (x0 ++ u2); exists x1;rewrite H;rewrite H1;simplmots;auto.
apply (rloose _ _ n);auto.
Qed.

Lemma mir_redb : forall u v n,
reduction_borne n u v <-> reduction_borne n (u ` ) (v ` ).
Proof.
intros u v n;
split;intro h;[ | rewrite<- (mirr_inv v);rewrite<- (mirr_inv u)];
induction h;auto;try apply ridb;
simplmots;
[apply (rtransb _ (v ` ))| apply (rloose _ _ n)
  |apply (rtransb _ (v0 ` ))| apply (rloose _ _ n)];auto;
apply mir_red_ in H;auto.
Qed.

Lemma joinb_mir : forall u v n,
joinable_borne n (u ` ) (v ` ) -> joinable_borne n u v.
Proof.
intros u v n (t & h1 & h2);exists (t ` );split;
rewrite mir_redb;simplmots;auto.
Qed.

Lemma dec_len_eq : 
forall a b c d : mot, a ++ b=c ++ d -> length b = length d -> a=c/\b=d.
Proof.
induction a;intros;destruct c; auto.
apply len_app_aux in H;[discriminate|auto].
apply len_app_aux in H;[discriminate|auto].
inversion H.
apply IHa in H3;auto;destruct H3 as (h,h');rewrite h;rewrite h'; auto.
Qed.

Lemma dec_mil : forall w x y z : mot, w  ++  x = y  ++  z -> 
length w = length x -> length y = length z -> w=y /\ x=z.
Proof.
intros;apply dec_len_eq;auto.
rewrite <- Div2.div2_double;rewrite <- (Div2.div2_double (length x));f_equal.
replace 2 with (1+1);auto;rewrite mult_plus_distr_r;rewrite mult_plus_distr_r;
repeat rewrite mult_1_l.
rewrite<- H0 at 1;rewrite<- app_length;rewrite H;rewrite app_length;auto.
Qed.

Lemma automir : forall u, u=u `  <-> exists v, u=v ++ (v ` ).
Proof.
intro u;split;[|intros (v&h);rewrite h;simplmots;auto].
inductionmot u.
intro h; destruct u; [exists eps;simpl;simplmots;auto|simpl in *].

assert (forall w :mot, w=eps \/ (exists y z,w=y ++ (z::eps)));
  [intro w;set (v := w ` );assert (v=w ` );
    [|destruct v as [|a];rewrite bij_mir in H0;simpl in *;simplmots;
      [left|right;exists (v ` );exists (prime a)]];auto|].
case (H0 u) as [h1|(w&y&h1)];clear H0;rewrite h1 in *;simpl in *;simplmots;
simpl in *;inversion h;
  [symmetry in H1;apply prime_dif in H1;contradiction
    |rewrite H1 in *;clear H1;apply dec_len_eq in H2 as (h2&h3);
      [apply H in h2 as (v,h2);
        [rewrite h2;exists (prime y::v);simpl;simplmots;auto
          |unfold ltof;simpl;rewrite app_length;omega]
        |simpl;auto]].
Qed.

(* begin hide *)
Ltac automir h v :=
  apply automir in h; destruct h as (v,h).
(* end hide *)

Fixpoint puiss n u:=
match n with
| 0   => eps
| S n => u ++ (puiss n u)
end.

Notation "u ** n" := (puiss n u) (at level 30).

Definition etoile u v := exists n, v = u ** n.

Definition concat p1 p2 (u : mot) := exists v1,exists v2, u=v1 ++ v2 /\ p1 v1 /\ p2 v2.

Lemma puis_ass u n m : u ** (n+m) = u ** n ++ u** m.
Proof.
induction n;simpl;intros;auto.
rewrite IHn;symmetry;apply app_ass.
Qed.

Lemma etoile_idemp : forall u v,
concat (etoile u) (etoile u) v -> etoile u v.
Proof.
intros u v h; destruct h as (v1 & v2 & h & (n & h1) & m & h2).
unfold etoile;exists (n+m).
rewrite puis_ass;rewrite<-h1;rewrite<-h2;auto.
Qed.

Lemma puis_app : forall u n, u ++ u** n =u** (S n).
Proof.
intros;simpl;auto.
Qed.

Lemma puis_app1 : forall u v n, u ++  (v ++ u) ** n= (u ++ v)**n  ++  u.
Proof.
induction n;simpl;simplmots;auto.
rewrite IHn;auto.
Qed.

Lemma puis_app2 : forall u n,u ** n ++  u = u ** (S n) .
Proof.
induction n;simpl;simplmots;auto.
rewrite IHn;simpl;auto.
Qed.

Lemma dec_ab_eq_ca : forall a b c : mot, a ++ b=c ++ a -> c<>eps -> 
exists d,exists e : mot, concat (etoile (d ++ e)) (fun u=>u=d) a/\c=d ++ e.
Proof.
inductionmot a.
intros b c h h1; decomp h.
case (dec_eps t) as [->|h].
simpl in *;simplmots;exists b;exists eps;split;[exists eps;exists b;repeat split;
  exists 0|];simplmots;auto.
rewrite<- H2 in *;symmetry in H2;apply H in H2 as (d&e&(v1&v2&->&(n&->)&->)&h3);
  auto.
exists d;exists e;split;auto;rewrite h3;exists (d ++ e ++ (d ++ e)**n);exists d;
  repeat split;simplmots;auto;apply etoile_idemp;exists (d ++ e);exists ((d ++ e)**n);
    split;[|split;[exists 1|exists n]];simpl;simplmots;auto.
unfold ltof;assert (length c>0);
  [destruct c;[absurd (eps=eps);auto|simpl]|rewrite app_length];omega.
exists a;exists t;split;auto;exists eps;exists a;repeat split;exists 0;auto.
Qed.

Lemma dec_uuv : forall u v, u ++ u `  ++ v `  = v ++ u ++ u `  -> 
2*length u<= length v ->
(exists t, v=u ++ u `  ++ t ++ t ` ).
Proof.
intros u v h1 h2.
rewrite <- (app_ass u) in h1; decomp h1.
assert (t=eps);
  [apply length_eps;assert (length (u ++ u ` ) = length (t ++ v ` ));
    [rewrite H0;auto|repeat rewrite app_length in *;
      repeat rewrite<- mir_len in *;omega]|rewrite H in *].
exists eps;rewrite bij_mir;simpl in *;simplmots;auto.
apply dec_len_eq in H1 as (h1,h3);[|auto].
symmetry in h1;automir h1 t1;rewrite h1 in *;exists t1;auto.
Qed.

Lemma automir_eq_app : forall t t1 t2, t `  ++ t = t1  ++  t2 -> 
exists t3, (t=t3 ++ t1 `  /\ t2 =t3 `  ++ t3 ++ t1 ` ) \/ 
(t=t3 ++ t2 /\ t1 =t2 `  ++ t3 `  ++ t3).
Proof.
intros t t1 t2 h;decomp h;
[repl_mir H0;exists (t0 ` );left|exists t0;right];split;simplmots;auto.
Qed.

Lemma confl_cas5_4aux : forall a b c d : mot, a ++ b=c ++ a ++ d ->
(exists t, exists s, b = t ++ s ++ d /\ c=s ++ t /\ c ++ a = a ++ t ++ s).
Proof.
intros.
rewrite<- app_ass in H.
decomp H.
nested H1;exists eps;exists eps;simpl in *;simplmots;repeat split;auto.

clear H2;decomp H1.
exists t0;exists a;auto.

case (dec_eps c);intro h;
  [rewrite h in *;simpl in H2;apply dec_trivial_r in H2;rewrite H2 in *;
    exists eps;exists eps;simpl in *;simplmots;repeat split;auto
      | case (dec_eps t0);intro h1;[rewrite h1 in *;simpl in *;simplmots;
        exists eps;exists t;simplmots;auto|]].

assert (c ++ a=a ++ t).
rewrite H0;rewrite<- app_ass;rewrite H2;simplmots;auto.

destruct (dec_ab_eq_ca a t c);auto.
clear h h1;destruct H1 as (e & (v1 & v2 & h & (n,h1) & h2) & h0);rewrite h2 in h;
clear h2;rewrite h0 in H;rewrite h1 in h; clear h1;rewrite h in H;simplmots.
rewrite<- (app_ass x) in H;rewrite<- (app_ass (x ++ e)) in H;
rewrite puis_app in H;rewrite<- puis_app2 in H;simplmots.
rewrite bij_mir in H;simplmots;rewrite<- (app_ass (x ` ) (e ` )) in H;
apply dec_len_eq in H;auto;destruct H as (h1,H);rewrite bij_mir in h1;simplmots.

exists e;exists x;repeat split;auto;rewrite<- h1;simplmots;auto.
Qed.

Lemma confl_cas1aux : forall w1 u1 w2 u2 a b c : mot,
w1 = u1  ++  w2  ++  a -> w2 ` =a ++ b -> w1 ` =b ++ w2 ++ c -> u2 = c ++ w1 ->
joinable_borne 4 w1 (u1 ++ w2 ++ u2).
Proof.
intros w1 u1 w2 u2 a b c -> h1 h2 ->;repl_mir h1;rewrite bij_mir in h2;simplmots;decomp h2;decomp H1.

rewrite bij_mir in H3;simplmots;rewrite <- app_ass in H3;rewrite <- app_ass in H3;
apply dec_len_eq in H3;destruct H3;
  [rewrite<- mir_app in H1;automir H1 x;rewrite<- (app_ass (b ` ));
    rewrite<- mir_app;rewrite <- (app_ass t0);rewrite H1;jrunb
    | rewrite<-mir_app;apply mir_len].
rewrite<- app_ass in H3;destruct (dec_uuv (a ` ) (t0 ++ b ` )) as (t1&h1);
  [simplmots;rewrite<- H3;rewrite bij_mir;simplmots;auto
  | assert (length (b ++ b ` ) = length (t0  ++  b  `   ++  a  `   ++  a));
    [rewrite H3;simplmots;auto
      |repeat rewrite app_length in *;repeat rewrite<- mir_len in *;omega]
    |repeat rewrite <- (app_ass t0);rewrite h1;simpl;jrunb ]. 

repl_mir H0;repl_mir H2;rewrite bij_mir in H3;simplmots;rewrite <- app_ass in H3;
rewrite <- app_ass in H3;apply dec_len_eq in H3;destruct H3;
  [rewrite<- (mirr_inv a) in H0 at 2;rewrite<- mir_app in H0;automir H0 x;
    repeat rewrite<- (app_ass t0);rewrite H0;jrunb
    | repeat rewrite app_length;repeat rewrite<- mir_len;omega].

repl_mir H0;rewrite<- app_ass in H3;destruct (dec_uuv b (t0 ++ a)) as (t1,h1);
[simplmots;rewrite<- H3;rewrite bij_mir;simplmots;auto
|assert (length (a `  ++ a) = length (t0  ++  a  ++  b  ++  b ` ));
[rewrite H3;simplmots;auto
|repeat rewrite app_length in *;repeat rewrite<- mir_len in *;omega]
|repeat rewrite <- (app_ass t0);rewrite<- (app_ass (a ` ));rewrite<- mir_app;
rewrite h1;simpl;jrunb].
Qed.

Lemma confl_cas1 : forall w1 u1 w2 u2, 
w1  ++  w1 `  = u1 ++ w2 ++ w2 `  ++ w2 ++ u2 ->
joinable_borne 4 w1 (u1 ++ w2 ++ u2 ++ w1).
Proof.
intros;decomp H;[decomp H2|repl_mir H2;jrunb];
  [ rewrite bij_mir in H3;simplmots;repeat rewrite<- (app_ass u1);rewrite H3;jrunb
    | clear H0;decomp H3;
      [eapply confl_cas1aux;eauto;repl_mir H0;simplmots;auto
        |decomp H2;[repeat rewrite <- (app_ass t2);rewrite<- H4|];jrunb]].
Qed.

Lemma confl_cas3 : forall w1 w2 a b c d e f, 
w1=a ++ b -> w1 `  = c ++ d -> w1 = e ++ w2 ++ f -> w2 = b ++ c -> w2 `  = d ++ e -> 
joinable_borne 2 w1 (a ++ w2 ++ f).
Proof.
intros w1 w2 a b c d e f -> H0 H1 -> H3;simplmots.
apply bij_mir in H3;apply bij_mir in H0;simplmots;rewrite H0 in *;simplmots.
repeat rewrite<- app_ass in H1;decomp H1;
[nested H3;rewrite <- (app_ass a);rewrite H0;jrunb
  |repl_mir H4;repeat rewrite<- app_ass in H2;apply dec_len_eq in H2 as (H2,h);
    [symmetry in h;automir h t1; rewrite h in *;clear h;rewrite<-(app_ass a);
      rewrite H0;jrunb
      |symmetry;apply mir_len]].
Qed.

Lemma confl_cas23 : forall w1 u1 w2 u2 t1 t2 : mot,
w1 = u1 ++ t1 -> w1 = t2 ++ u2 -> w2 ++ w2 `  ++ w2=t1 ++ w1 `  ++ t2 ->
joinable_borne 2 w1 (u1 ++ w2 ++ u2).
Proof.
intros w1 u1 w2 u2 t1 t2 h h1 h2;decomp h2.
rewrite<- (app_ass t) in H1;rewrite <- (app_ass (t ++ t ` )) in H1;decomp H1.
rewrite<- (app_ass t1);repl_mir H2;rewrite<- (app_ass u1);rewrite<- h;jrunb;
  rewrite <- (app_ass t1 t u2);rewrite H3;simplmots;rewrite<- h1;jrunb.
clear H3;decomp H2;[nested h1;rewrite<- h;jidb|].
rewrite<- (app_ass t1) in *;eapply confl_cas3;eauto;simplmots;auto.

decomp H1;[apply joinb_mir;simplmots|nested h;rewrite<- h1;jidb].
decomp H3;[|repl_mir H2;nested h; nested h;jidb].
rewrite bij_mir in h;rewrite bij_mir in h1;simplmots;rewrite H1 in *;
  rewrite <- (app_ass (t2 ` )) in *;eapply confl_cas3;eauto;[|rewrite H2];
    rewrite mir_app;eauto.
Qed.
 

Lemma confl_part1 :
forall w1 u1 w2 u2, w1 ++ w1 `  ++ w1 = u1 ++ w2 ++ w2 `  ++ w2 ++ u2 -> 
joinable_borne 4 w1 (u1 ++ w2 ++ u2).
Proof.
intros w1 u1 w2 u2 h;decomp h;
  [repeat rewrite<- (app_ass _ _ u2) in H1;
    repeat rewrite<- (app_ass _ _ (u1 ++ t)) in H1;decomp H1;
      [rewrite <- (app_ass (t ` )) in H2;rewrite<- mir_app in H2;rewrite H3 in H2;
        apply (join_loose 2); [eapply confl_cas23;eauto|]
        |apply confl_cas1;simplmots;rewrite H2];auto
    | decomp H1;
      [repeat rewrite<- app_ass in H3;decomp H3;
        [apply joinb_mir;simplmots;apply confl_cas1;simplmots;f_equal;
          rewrite bij_mir; simplmots;rewrite H1;repeat rewrite<- app_ass;
            f_equal;auto
          |repl_mir H2]|];jrunb].
Qed.

Lemma confl_cas5_3 : forall u1 w1 u2 w2 a b c d : mot, 
u1 = w2 ++ a -> w2 `  = a ++ w1 ++ b -> w1 `  = b ++ c -> w2 = c ++ d -> w1 = d ++ u2 ->
joinable_borne 2 (u1 ++ w1) (w2 ++ u2).
Proof.
intros u1 w1 u2 w2 a b c d -> h1 h2 h3 h4;repl_mir h2;repl_mir h1;
repeat rewrite<- app_ass in h3;decomp h3;
  [clear H1;rewrite<- app_ass in H0;decomp H0;clear H2
    |rewrite h4;nested H0;repl_mir H1;repl_mir h4; simpl in *; jrunb].

apply automir_eq_app in H1 as (x&[(->&->)|(->&->)]);simplmots.

assert (x  ++  a `   ++  u2 = eps) as h;
  [apply length_eps;assert (length (c `  ++ c ++ x ` )=length (x `  ++ x ++ c `  ++ c ++ a `  ++  u2));
    [rewrite h4;auto
      |repeat rewrite app_length in *;repeat rewrite<- mir_len in *;omega]
    |app_eps h;simpl in *;simplmots;jrunb].

rewrite h4;rewrite bij_mir in h4;simplmots;rewrite<- (app_ass (u2 ` )) in h4;
apply dec_len_eq in h4 as (->,h4);
  [jrunb|repeat rewrite app_length;repeat rewrite<- mir_len;omega].

rewrite h4;rewrite bij_mir in h4;simplmots;repeat rewrite<- app_ass in h4; 
apply dec_len_eq in h4 as (h,h4);[apply automir in h4 as (v,->);jrunb|apply mir_len].
Qed.

Lemma confl_cas5_4 : forall u1 w1 u2 w2 a b c d e, 
w2 = u1 ++ a -> w2 `  = b ++ c -> w2 = d ++ e -> w1 = a ++ b -> w1 `  = c ++ d -> w1 = e ++ u2 ->
joinable_borne 2 (u1 ++ w1) (w2 ++ u2).
Proof.
intros u1 w1 u2 w2 a b c d e -> h1 h2 h3 h4 ->;rewrite bij_mir in h1;
  rewrite bij_mir in h4; simplmots; decomp h2;clear H0 H1.

symmetry in h3;apply confl_cas5_4aux in h3 as (p & q & -> & -> & h6);simplmots;
rewrite h6 in h1;rewrite<- app_ass in h1;rewrite<- (app_ass (c ` )) in h1;
apply dec_len_eq in h1;
  [destruct h1 as (h_,h1);rewrite<- mir_app in h1;automir h1 v;
    repeat(repeat rewrite<-(app_ass q p) in *;rewrite<-(app_ass _ a);rewrite h6;
      simplmots);repeat rewrite<- (app_ass p);rewrite h1;jrunb
    |repeat rewrite app_length;repeat rewrite<- mir_len;omega].

rewrite h3 in h4;rewrite bij_mir in h4;simplmots;repeat rewrite<- app_ass in h4;
  apply dec_len_eq in h4 as (h4,h2);
    [symmetry in h2;apply automir in h2 as (v&->);rewrite h3;jrunb
      |symmetry;apply mir_len].
Qed.

Lemma confl_cas5 : forall u1 a u2 w2 : mot, 
w2 ++ w2 `  ++ w2=u1 ++ a ++ u2 ++ (a ++ u2) `  ++ a ->
joinable_borne 4 (u1 ++ a ++ u2) (w2 ++ u2).
Proof.
intros u1 a u2 w2 H;decomp H.

rewrite bij_mir in H2;simplmots;rewrite<- (app_ass (t ` )) in H2;decomp H2.
repl_mir H0;assert (t0 ++ u2 ++ u1 = eps);
  [ apply length_eps;
    assert (length (u1  ++  t  ++  t  ` )=
      length (t0  ++  t0  `   ++  u1  ++  t  ++  u2  ++  u2  `   ++  t  `   ++  u1  `   ++  t0));
    [ rewrite H3;auto
      | repeat rewrite app_length in *;repeat rewrite <- mir_len in *;omega]
    | app_eps H;simpl in *;simplmots;jidb].

rewrite bij_mir in H0;simplmots;rewrite<- (app_ass u1) in H3;rewrite H0 in H3;
simplmots;rewrite<- (app_ass a) in H3;decomp H3;[|nested H4;jidb].

rewrite<- mir_app in H4;rewrite H2 in H4;simplmots;rewrite bij_mir in H4;simplmots;
rewrite<- (app_ass t0) in H4;decomp H4;
  [ repl_mir H5;symmetry in H0;nested H0;rewrite H2;jidb
    | rewrite<- (app_ass _ _ u2);apply (join_loose 2);
      [eapply confl_cas5_4;eauto;[rewrite H0|rewrite<- H3]|auto];
      rewrite mir_app;repeat rewrite mirr_inv;eauto].

decomp H2;
[rewrite bij_mir in H3;simplmots;decomp H3;
  [repl_mir H4;repl_mir H2;nested H0;jidb
    |rewrite<- (app_ass a) in H4;decomp H4]
  |jrunb].

repl_mir H5;rewrite H0 in H2;rewrite<- app_ass;apply (join_loose 2);auto;
  eapply confl_cas5_3;[|rewrite<- H3;rewrite app_ass|rewrite mir_app| |];
    eauto;apply bij_mir;simplmots;rewrite H0;auto.

rewrite bij_mir in H5;simplmots;repeat rewrite<- (app_ass a u2) in *;
  rewrite H5 in *;repl_mir H2;rewrite<- (app_ass t);rewrite<- H0;jrunb;rewrite<- H5;
    jrunb.
Qed.

Lemma confl_cas6_3 : forall u1 w1 u2 w2 a b c d : mot,
w2 = u1 ++ a -> w1 = a ++ b -> w2 `  = b ++ c -> w1 `  = c  ++  w2  ++  d -> u2 = d ++ w1 ->
joinable_borne 4 (u1 ++ w1) (w2 ++ u2).
Proof.
intros u1 w1 u2 w2 a b c d h1 h2 h3 h4 ->;repl_mir h3;
repl_mir h4;decomp h1;
[repl_mir H0;clear H1|repl_mir H1;clear H0];
rewrite bij_mir in h2;simplmots;
  symmetry in h2; 
    [replacemotin (t  `   ++  u1  `   ++  u1  ++  t  ++  b  `   ++  d) 
      ((t  `   ++  u1  `   ++  u1  ++  t)  ++  b  `   ++  d) h2
      | repeat rewrite<- (app_ass t) in h2;
        rewrite <- (app_ass c) in h2];
    apply confl_cas5_4aux in h2 as (x & y & h1 & h2 & h3);
      [rewrite <- app_ass in h2; rewrite <- mir_app in h2
        |replacemotin c (c `  ` ) h2;replacemotin (c `  `  ` ) (c ` ) h2];
      apply automir_eq_app in h2 as (z & [(h2,h4)|(h2,h4)]); rewrite h4 in *;
        clear h4;
          [rewrite bij_mir in h1;simplmots;rewrite h1 in h3
            |rewrite bij_mir in h1;simplmots;rewrite h1 in h3
            |repl_mir h2;repl_mir h1
            |repl_mir h2;repl_mir h1];simplmots;
          [replacemotin (t `  ++ u1 `  ++ u1 ++ d `  ++ y `  ++ y ++ z `  ++ z)
            ((t `  ++ u1 `  ++ u1 ++ d ` ) ++ y `  ++ y ++ z `  ++ z) h3
            |replacemotin (t `  ++ u1 `  ++ u1 ++ d `  ++ z `  ++ z ++ x ++ x ` )
              ((t `  ++ u1 `  ++ u1 ++ d ` ) ++ z `  ++ z ++ x ++ x ` ) h3
            |replacemotin (y ++ z `  ++ z ++ y `  ++ t ++ d `  ++ y `  ++ y ++ z `  ++ z)
              ((y ++ z `  ++ z ++ y `  ++ t ++ d ` ) ++ y `  ++ y ++ z `  ++ z) h3;
              replacemotin (t ++ d `  ++ y `  ++ y ++ z `  ++ z ++ z `  ++ z ++ y `  ++ y)
              ((t ++ d `  ++ y `  ++ y ++ z `  ++ z) ++ z `  ++ z ++ y `  ++ y) h3
            |replacemotin (x `  ++ z `  ++ z ++ x ++ t ++ d `  ++ z `  ++ z ++ x ++ x ` )
              ((x `  ++ z `  ++ z ++ x ++ t ++ d ` ) ++ z `  ++ z ++ x ++ x ` ) h3;
              replacemotin (t ++ d `  ++ z `  ++ z ++ x ++ x `  ++ x ++ x `  ++ z `  ++ z)
              ((t ++ d `  ++ z `  ++ z ++ x ++ x ` ) ++ x ++ x `  ++ z `  ++ z) h3];
          apply dec_len_eq in h3 as (h3 & h4);try (repeat rewrite app_length;omega);
            [symmetry in h3;repl_mir h3|symmetry in h3;repl_mir h3| |];jrunb;
            try (replacemot 
              (u1  ++  t  ++  t  `   ++  u1  `   ++  u1  ++  d  `   ++  d  ++  u1  `   ++  u1  ++  t)
              (u1  ++  (t  ++  t  `   ++  u1  `   ++  u1  ++  d  ` )  ++  d  ++  u1  `   ++  u1  ++  t);
              rewrite h1;simplmots;rewrite bij_mir in h1;simplmots;
                repeat rewrite<- app_ass in *;rewrite<- h4 in h1;simplmots);
            [replacemot (u1  ++  d  `   ++  y  `   ++  y  ++  z  `   ++  z  ++  d  ++  u1  `   ++  u1  ++  t)
              (u1  ++  d  `   ++  (y  `   ++  y  ++  z  `   ++  z  ++  d)  ++  u1  `   ++  u1  ++  t)
              |replacemot (u1  ++  d  `   ++  z  `   ++  z  ++  x  ++  x  `   ++  d  ++  u1  `   ++  u1  ++  t)
                (u1  ++  d  `   ++  (z  `   ++  z  ++  x  ++  x  `   ++  d)  ++  u1  `   ++  u1  ++  t)
              |replacemot 
                (z ++ y `  ++ t ++ d `  ++ y `  ++ y ++ z `  ++ z ++ d ++ d `  ++ z `  ++ z ++ y `  ++ y ++ d ++ t `  ++ y ++ z `  ++ z ++ y ` )
                (z ++ y `  ++ (t ++ d `  ++ y `  ++ y ++ z `  ++ z) ++ d ++ d `  ++ z `  ++ z ++ y `  ++ y ++ d ++ t `  ++ y ++ z `  ++ z ++ y ` )
              |replacemot 
                (z ++ x ++ t ++ d `  ++ z `  ++ z ++ x ++ x `  ++ d ++ d `  ++ x ++ x `  ++ z `  ++ z ++ d ++ t `  ++ x `  ++ z `  ++ z ++ x)
                (z ++ x ++ (t ++ d `  ++ z `  ++ z ++ x ++ x ` ) ++ d ++ d `  ++ x ++ x `  ++ z `  ++ z ++ d ++ t `  ++ x `  ++ z `  ++ z ++ x)
            ];
            [rewrite<- h1|rewrite<- h1|rewrite<- h3|rewrite<- h3];jrunb.
Qed.

Lemma confl_cas6_1 : forall u1 w1 u2 w2 a b c d e : mot, 
u1=w2 ++ a -> w2 `  = a ++ b -> w1 = b ++ c -> w2 = c ++ d -> w1 ` = d ++ e -> u2 = e ++ w1 ->
joinable_borne 3 (u1 ++ w1) (w2 ++ u2).
Proof.
cut (forall a b c d : mot, a `  ++ a ++ b=c ++ d ++ d `  -> 
  joinable_borne 3 (a ++ b ++ d) (a ++ c ++ d)).

intros h0 u1 w1 u2 w2 a b c d e -> h1 -> -> h2 ->;simplmots.

cut (d `  ++ d ++ e=a ++ b ++ b ` );
[intro h|rewrite<-h2;rewrite<- (app_ass a);rewrite <- h1;simplmots;auto].
apply h0 in h.
destruct h as (v & h & h');exists (c ++ v ++ c);split;rewrite<- (app_ass d) in *;
rewrite<- (app_ass _ b);apply cntxt_redb;auto.

cut (forall a b (p: mot * mot), (fst p) `  ++ (fst p) ++ a=b ++ (snd p) ++ (snd p) `  -> 
  joinable_borne 3 (fst p ++ a ++ snd p) (fst p ++ b ++ snd p)).
intros h1 a b c d h;apply (h1 b c (a,d));simpl;auto.
induction p using 
  (well_founded_ind (well_founded_ltof (mot*mot) 
    (fun p => @length X (fst p) + @length X (snd p)))).
intro h;rewrite<- (app_ass ((fst p) ` )) in h;decomp h;jrunb.
decomp H1;[rewrite<- app_ass in H2|clear H3 H4];decomp H2;[repl_mir H3| | |repl_mir H3];jrunb.
rewrite bij_mir in H3;rewrite bij_mir in H5;simplmots;rewrite H3 in *;clear H4;
rewrite H5 in *.
decomp H1;[nested H5;jidb|].
repeat (rewrite<- (app_ass (t0 ` ));rewrite H4).
cut (joinable_borne 3 (t1 ++ a ++ t0) (t1 ++ b ++ t0));
  [intros (v & h1 & h2);exists (t2 ++ v ++ t2);split;simplmots;
    repeat (rewrite<- (app_ass t1);rewrite <- (app_ass _ t0));
      apply cntxt_redb;simplmots;auto
    | case (dec_eps t2) as [h0|h0];
      [rewrite h0 in *;simpl in *;simplmots;
        rewrite<- H4 in H2;symmetry in H2;simplmots;nested H2;jidb|]].

apply (H (t1,t0)).
unfold ltof;simpl;rewrite H3;rewrite H5;repeat rewrite app_length;
cut (length t2>0);[intro h1;omega|destruct t2;[absurd (eps=eps);auto|simpl;omega]].
simpl;rewrite bij_mir;simplmots;rewrite H4;repeat rewrite<- app_ass;f_equal;auto.
Qed.

Lemma confl_cas6 : forall u1 w1 u2 w2 a b,
w1 `  = a ++ b -> u2=b ++ w1 -> u1 ++ w1 ++ a = w2 ++ w2 `  ++ w2 ->
joinable_borne 4 (u1 ++ w1) (w2 ++ u2).
Proof.
intros u1 w1 u2 w2 a b h -> h2;repl_mir h;decomp h2;
  [|rewrite<- (app_ass (b ` )) in H1;rewrite<- (app_ass (t ` )) in H1];
  decomp H1;[|jrunb | |nested H3;jidb].

rewrite<- (app_ass w2);rewrite<- H0;
 case (decomposition (b ` ) (a `   ++ a) t0 w2) as (t1&[(h0&h1)|(h0&h1)]);auto;
  [repl_mir h0;rewrite <- (app_ass t1) in h1;apply (join_loose 3);auto;
    eapply confl_cas6_1;eauto;rewrite H0;repeat rewrite mir_app;
      repeat rewrite mirr_inv;try (rewrite app_ass);eauto;rewrite <- app_ass;eauto
    |apply automir_eq_app in h1 as (y&[(->&h4)|(->&h4)]);simplmots;
      rewrite h0 in *;
        [apply (join_loose 3);auto;eapply confl_cas6_1;eauto;simplmots;auto
          |repl_mir H2;rewrite h4 in *;simplmots;nested h4;rewrite H0;jidb]].

rewrite<- (app_ass (t ` )) in H3;decomp H3;
  [rewrite<-(app_ass u1 t (b ++ t ++ t0));eapply confl_cas6_3; eauto;
    repeat rewrite mir_app;repeat rewrite mirr_inv;eauto;apply bij_mir;simplmots;auto
    |repeat rewrite<- (app_ass u1);
      repeat rewrite <- (app_ass (t ` )) in *;repeat rewrite<- mir_app in *;
        rewrite H4 in *;simplmots;rewrite bij_mir in H2;simplmots;
          rewrite<- (app_ass a b);rewrite H2;decomp H4;jrunb].
Qed.

Lemma confl_part2 : forall u1 w1 u2 w2, 
u1 ++ w1 ++ w1 `  ++ w1 = w2 ++ w2 `  ++ w2 ++ u2 ->
joinable_borne 4 (u1 ++ w1) (w2 ++ u2).
Proof.
intros u1 w1 u2 w2 h;
repeat rewrite<- app_ass in h;decomp h;
  [repeat rewrite<- app_ass in H0;decomp H0;try rewrite<- H2;
    [repeat rewrite<- app_ass in H2;decomp H2;
      [|rewrite<- (app_ass u1);rewrite<- H0;decomp H0;
        [|decomp H5;[repl_mir H0|]]];jrunb
      |eapply confl_cas6;[exact H3| |];auto]
    |apply confl_cas5;simplmots;auto].
Qed.

Theorem confluence_locale :
forall u v1 v2 : mot, reduction u v1 -> reduction u v2 -> 
joinable_borne 4 v1 v2.
Proof.
intros u t1 t2 h h2; unfold reduction in *;destruct h as (u1 & v1 & w1 & h & h1);
destruct h2 as (u2 & v2 & w2 & h2 & h3).

rewrite h2 in h.
decomp h.
repeat rewrite<- app_ass in H1;decomp H1.

apply confl_part1 in H2.
destruct H2 as (v3 & h0 & h).
rewrite<- (app_ass t) in h3;rewrite<- (app_ass (t ++ w2)) in h3;
rewrite h1; rewrite h3;exists (u1 ++ v3 ++ v1);split;apply cntxt_redb;simplmots;auto.

apply confl_part2 in H2.
destruct H2 as (v3 & h0 & h).
rewrite<- (app_ass t) in h3;rewrite<- (app_ass w1) in h1;
rewrite h1; rewrite h3;exists (u1 ++ v3 ++ v2);split;apply cntxt_redb;simplmots;auto.

repeat rewrite<- app_ass in H1;decomp H1.

apply confl_part1 in H2.
destruct H2 as (v3 & h0 & h).
rewrite<- (app_ass t) in h1;rewrite<- (app_ass (t ++ w1)) in h1;
rewrite h1; rewrite h3;exists (u2 ++ v3 ++ v2);split;apply cntxt_redb;simplmots;auto.

apply confl_part2 in H2.
destruct H2 as (v3 & h0 & h).
rewrite<- (app_ass w2) in h3;rewrite<- (app_ass t) in h1;
rewrite h1; rewrite h3;exists (u2 ++ v3 ++ v1);split;apply cntxt_redb;simplmots;auto.
Qed.
