(** printing -->* %\ensuremath{\>^*}% *)
(** printing <--> %\ensuremath{\join}% *)
(** printing --> %\ensuremath{\>}% *)
(** printing -> %\ensuremath{\Rightarrow }% *)
(** printing => %\ensuremath{\mapsto }% *)
(** printing  ++  %\ensuremath{\cdot }% *)
(** printing eps %\ensuremath{\epsilon }% *)
(** printing < %\ensuremath{< }% *)
(** printing <-> %\ensuremath{\Leftrightarrow }% *)
(** printing ` %\ensuremath{^\vee }% *)
(** printing ** %\ensuremath{^{**}}% *)

(* begin hide *)
Require Import List Arith Omega NPeano.
(* end hide *)

(** ** Définitions *)
Parameter X :Set.

Definition mot := list X.

Definition eps := @nil X.

Parameter prime : X -> X.

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

Fixpoint miroir (u : mot) : mot :=
match u with 
| nil => eps
| a::v => miroir v  ++  ((prime a)::eps)
end.
(* begin hide *)
Notation "w `" := (miroir w) (at level 10).
(* end hide *)
(** On notera $\conv w$ pour [miroir w]. *)

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

(* begin hide *)
Notation "u --> v" := (reduction u v) (at level 20).
(* end hide *)
(** On notera $u \> v$ pour [reduction u v]. *)

Inductive reduction_rt : mot -> mot -> Prop :=
| rid : forall u, reduction_rt u u
| rtrans : forall u v w, u --> v -> reduction_rt v w -> reduction_rt u w.

(* begin hide *)
Notation "u -->* v" := (reduction_rt u v) (at level 20).
(* end hide *)
(** On notera $u \>^* v$ pour [reduction_rt u v]. *)

Definition joinable (u1 u2 : mot) : Prop := 
exists v, u1 -->* v /\ u2 -->* v.

(* begin hide *)
Notation "u <--> v" := (joinable u v) (at level 20).
(* end hide *)
(** On notera $u \join v$ pour [joinable u v]. *)

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

(* begin hide *)
Notation "u ** n" := (puiss n u) (at level 30).
(* end hide *)
(** On notera $u\ ^{**}\ n$ pour [puiss n u]. *)

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.

(** ** Lemmes préliminaires *)
(** Pour ces lemmes très simples, on ne donnera pas ici de preuve. *)

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.

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 join_refl : forall u, u <--> u.
Proof.
intro u;exists u;split;apply rid.
Qed.

Lemma join_sym : forall u v, u <--> v -> v <--> u.
Proof.
intros u v h;destruct h as (v0 & h1 & h2);exists v0;split;auto.
Qed.

Lemma trans_red : forall u v w, 
u -->* v -> v -->* w -> u -->* w.
Proof.
intros u v w h1 h2.
induction h1;auto.
apply (rtrans _ v);auto.
Qed.

Lemma join_step_l : forall v1 u1 u2, u1 -->* v1 -> v1 <--> u2 -> u1 <--> u2.
Proof.
intros v1 u1 u2 h3 h1;destruct h1 as (v0 & h1 & h2);
exists v0;split;[apply (trans_red _ v1)|];auto.
Qed.

Lemma join_step_r : forall v2 u1 u2, u2 -->* v2 -> u1 <--> v2 -> u1 <--> u2.
Proof.
intros v2 u1 u2 h3 h1;destruct h1 as (v0 & h1 & h2);
exists v0;split;[|apply (trans_red _ v2)];auto.
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 decomp2 h :=
let x := fresh "t" in
let h0 :=fresh "h" in
let h1 := fresh "h" in
let h2 := fresh "h" in
case (decomposition _ _ _ _ h) as (x&[(h1,h2)|(h1,h2)]);simplmots.

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

Ltac proveredrt u1 w u2 :=
apply (rtrans _ (u1 ++ w ++ u2)); simplmots;
[provered u1 w u2| apply rid]; auto.

Ltac provered_tr u1 w u2 :=
apply (rtrans _ (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 step :=
  simplmots;
  match goal with
    | [ _:_ |- ?u -->* _ ] => (pat u provered_tr;try (apply rid))
  end.

Ltac jid := apply join_refl.

Ltac auxl l m d :=
  apply (join_step_l (l ++ m ++ d));[proveredrt l m d|].

Ltac auxr l m d :=
  apply (join_step_r (l ++ m ++ d));[proveredrt l m d|].

Ltac jstepl := 
  simplmots;
  match goal with
    | [ _:_ |- ?u <--> _ ] => (pat u auxl;simpl;simplmots;try jid)
  end.

Ltac jstepr := 
  simplmots;
  match goal with
    | [ _:_ |- _ <--> ?u ] => (pat u auxr;simpl;simplmots;try jid)
  end.

Ltac jrun :=
  repeat jstepl;repeat jstepr.

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 cntxt_red : forall v w u1 u2,
v -->* w -> (u1 ++ v ++ u2) -->* (u1 ++ w ++ u2).
Proof.
intros.
induction H.
apply rid.
apply (rtrans _ (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.
Qed.

Lemma mir_red_  : forall u v,
u --> v <-> (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 mir_red : forall u v,
u -->* v <-> u `  -->* (v ` ).
Proof.
intros u v;
split;intro h;[ | rewrite<- (mirr_inv v);rewrite<- (mirr_inv u)];
induction h;auto;try apply rid;
simplmots;
[apply (rtrans _ (v ` ))|apply (rtrans _ (v0 ` ))];auto;
apply mir_red_ in H;auto.
Qed.

Lemma join_mir : forall u v,
u ` <-->v `  -> u<-->v.
Proof.
intros u v h;destruct h as (t & h1 & h2);exists (t ` );split;
rewrite mir_red;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 ` ) \/ exists x, u = v ++ (x::eps) ++ (v ` ) /\ x=prime x).
Proof.
intro u;split;
  [|intros (v&[->|(x&->&h)]);simpl;simplmots;simpl;auto;
    rewrite<- h;simplmots;simpl;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;
  [exists eps;right;exists x;simpl;simplmots;auto|].
simplmots;apply dec_len_eq in H2 as (h2,h3);auto.
apply H in h2 as (v&[->|(z&->&h4)]);
  [| |unfold ltof;simpl;rewrite app_length;simpl;omega];exists (prime y::v);
    [|right;exists z];simpl;simplmots;auto.
Qed.

(* begin hide *)
Ltac automir h v x:=
  let hx := fresh "hx" in
    apply automir in h; destruct h as (v&[h|(x&h&hx)]).
(* end hide *)

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 w u v, w ++ w `  = v ++ u ++ u `  -> 
2*length u<= length v ->
(exists t, v=u ++ u `  ++ t ++ t ` ).
Proof.
intros w u v h1 h2.
decomp h1.
assert (t=eps);
  [apply length_eps;assert (length (u ++ u ` ) = length (t ++ t `  ++ v ` ));
    [rewrite H1;auto|repeat rewrite app_length in *;
      repeat rewrite<- mir_len in *;omega]|rewrite H in *].
exists eps;rewrite bij_mir;simpl in *;simplmots;auto.
repl_mir H1;exists (t ` );simplmots;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.

(** On introduit également un certain nombre de tactiques, on nombre desquelles on peut citer :
- [decomp] : applique le lemme de décomposition;
- [simplmots] : simplifie les expressions des mots, en appliquant l'associativité de la concaténation, en supprimant les [nil]...
- [jrun] : détecte et réduit autant que possible les motifs, pour résoudre un but de la forme [u <--> v];
- [nested] : détecte les hypothèses de la forme [u = a ++ u ++ b]. *)

(** ** Preuve de confluence *)

(** %Le théorème que l'on souhaite montrer ici est le suivant :

\begin{Theo_}[Confluence locale]
  \[\forall u,v1,v2\in X^*, u\>v_1\wedge u\>v_2\Rightarrow v_1\join v_2.\]
\end{Theo_}

Pour cela, on va faire une étude de cas exhaustive. Cette décomposition va être articulée en deux parties, correspondant aux cas suivants :
\begin{enumerate}
\item $w_1\conv w_1 w_1=u_1w_2\conv w_2 w_2 u_2$.
\item $u_1w_1\conv w_1 w_1=w_2\conv w_2 w_2 u_2$.
\end{enumerate}
Autrement représenté :
\[\begin{array}{|c|c|c|}
  \hline
  \multicolumn{3}{|c|}{ w_1\conv w_1 w_1}\\
  \hline
  u_1& w_2\conv w_2 w_2 &u_2\\
  \hline
\end{array}\ ou\ \begin{array}{|c|c|c|}
  \hline
  u_1&\multicolumn{2}{c|}{ w_1\conv w_1w_1}\\
  \hline
  \multicolumn{2}{|c|}{w_2\conv w_2 w_2}&u_2\\
  \hline
\end{array}
\]

Par symétrie de la relation $\join$, on voit que ces deux cas couvrent l'ensemble des paires critiques.%*)


(** *** Première partie *)

(** On considère tout d'abord le cas où un motif est complètement inclus dans l'autre, %c'est à dire
\[w_1\conv w_1 w_1=u_1w_2\conv w_2 w_2 u_2.\]%*)

(** On va décomposer ce cas en deux sous-cas :*)
(** %\[\begin{array}{|c|c|c|}
    \hline
    \multicolumn{2}{|c|}{w_1\conv w_1}& w_1\\
    \hline
    u_1w_2\conv w_2 w_2&a& w_1\\
    \hline
    u_1w_2\conv w_2 w_2&\multicolumn{2}{c|}{u_2}\\
    \hline
  \end{array}
  \]%*)

(** Ce premier sous cas sera lui même séparé en trois parties : 
%\begin{enumerate}
\item $\begin{array}{|c|c|c|c|}
    \hline
    \multicolumn{3}{|c|}{w_1}&\convv w_1 w_1\\
    \hline
    u_1&w_2\convv w_2 w_2&a&\convv w_1 w_1\\
    \hline
    u_1&w_2\convv w_2 w_2&\multicolumn{2}{c|}{u_2}\\
    \hline
  \end{array}$
\item $\begin{array}{|c|c|c|c|c|}
    \hline
    w_1&\multicolumn{3}{c|}{\convv w_1}& w_1\\
    \hline
    w_1&a&w_2\convv w_2 w_2&b&w_1\\
    \hline
    \multicolumn{2}{|c|}{u_1}&w_2\convv w_2 w_2&\multicolumn{2}{c|}{u_2}\\
    \hline
  \end{array}$
\item$\begin{array}{|c|c|c|c|c|}
    \hline
    \multicolumn{2}{|c|}{\quad w_1\quad }&\multicolumn{2}{c|}{\convv w_1}& w_1\\
    \hline
    u_1&a&b&c& w_1\\
    \hline
    u_1&\multicolumn{2}{c|}{w_2\convv w_2 w_2}&\multicolumn{2}{c|}{u_2}\\
    \hline
  \end{array}$
\end{enumerate}%*)

(** Les deux premiers cas sont très simples : 
%\begin{enumerate}
\item 
\begin{itemize}
\item $w_1 = u_1w_2\convv w_2 w_2a\>u_1w_2a$
\item $u_1w_2u_2 = u_1w_2a\cdot \convv w_1\cdot w_1\>^2u_1w_2a\cdot \convv{u_1w_2a}\cdot u_1w_2a\>u_1w_2a$
\end{itemize}

\item
\begin{itemize}
\item $w_1 = \convv{\convv w_1}=\convv{aw_2\convv w_2 w_2b}\>\convv{aw_2b}$
\item $u_1w_2u_2 = w_1\cdot aw_2b\cdot w_1\>^2\convv{aw_2b}\cdot aw_2b\cdot \convv{aw_2b}\>\convv{aw_2b}$
\end{itemize}
\end{enumerate}%*)

(** Le troisième cas se décompose à nouveau :
%\begin{enumerate}
\item $\begin{array}{|c|c|c|c|c|c|c|}
    \hline
    \multicolumn{4}{|c|}{\quad w_1\quad }&\multicolumn{2}{c|}{\convv w_1}& w_1\\
    \hline
    u_1&w_2&\convv w_2&a&b&c& w_1\\
    \hline
    u_1&w_2&\convv w_2 &\multicolumn{2}{c|}{w_2}&\multicolumn{2}{c|}{u_2}\\
    \hline
  \end{array}$
\[\begin{array}{rcl}
  u_1w_2\cdot u_2&=&u_1\cdot w_2\cdot  c w_1\\
  &=&u_1 a\cdot bc\cdot w_1\\
  &=&u_1 a\cdot \convv w_1\cdot w_1\\
  &=&u_1 a\cdot \convv {u_1w_2\convv w_2a}\cdot u_1w_2\convv w_2a\\
  &=&u_1 a\cdot \convv {u_1 \cdot{ab}\cdot\convv  {ab}\cdot a}\cdot u_1\cdot {ab}\cdot\convv {ab}\ a \\
  &=&u_1\cdot  a \convv a a\cdot b\ \convv {u_1 a b}\  u_1 a b \convv {ab}\ a\\
  &\>&u_1 a b\ \convv {u_1 a b}\ u_1 a b \cdot \convv {ab}\ a\\
  &\>&u_1\cdot a b\cdot \convv {ab}\ a\\
  &=&u_1w_2\convv w_2 a = w_1
\end{array}
\]
\item $\begin{array}{|c|c|c|c|c|c|c|}
    \hline
    \multicolumn{2}{|c|}{\quad w_1\quad }&\multicolumn{4}{c|}{\convv w_1}& w_1\\
    \hline
    u_1&a&b&\convv w_2&w_2&c& w_1\\
    \hline
    u_1&\multicolumn{2}{c|}{w_2}&\convv w_2 &w_2&\multicolumn{2}{c|}{u_2}\\
    \hline
  \end{array}$
\[\begin{array}{rcl}
  u_1w_2\cdot u_2&=&u_1\cdot w_2\cdot  c\cdot  w_1\\
  &=&u_1 a\cdot bc\cdot w_1\\
  &=&w_1\cdot bc\cdot w_1\\
  &=&\convv{b\convv w_2w_2 c}\cdot bc\cdot \convv{b\convv w_2w_2 c} \\
  &=&\convv {w_2\cdot c}\cdot w_2 \cdot \convv b\ bc\ \convv {w_2\cdot c}\cdot w_2\cdot  \convv b \\
  &=&\convv{abc}\ a\cdot b \convv b b \cdot c\ \convv{abc}\ {ab}  \convv b \\
  &\>&\convv{abc}\cdot a b c \cdot \convv{abc}\cdot {ab}  \convv b \\
  &\>&\convv{abc}\cdot {ab}  \convv b = \convv{w_2c}w_2 \convv b=\convv{b\convv w_2w_2 c}=w_1\\
\end{array}
\]
\item $\begin{array}{|c|c|c|c|c|c|c|}
    \hline
    \multicolumn{3}{|c|}{\quad w_1\quad }&\multicolumn{3}{c|}{\convv w_1}& w_1\\
    \hline
    u_1&w_2&a&b&w_2&c& w_1\\
    \hline
    u_1&w_2&\multicolumn{2}{c|}{\convv w_2 }&w_2&\multicolumn{2}{c|}{u_2}\\
    \hline
  \end{array}$
\end{enumerate}%*)

(** Ce dernier cas, plus délicat, fait l'objet du lemme suivant (on ne donnera pas sa preuve ici) :*)

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 ->
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 k;rewrite<- (app_ass (b ` ));
    rewrite<- mir_app;rewrite <- (app_ass t0);rewrite H1;jrun
    | rewrite<-mir_app;apply mir_len].
rewrite<- H in H1;simplmots;
assert (length (t0  ++  t0  `   ++  t  `   ++  t) = (length (x  ++  (k :: eps)  ++  x  ` )));
  [rewrite H1;auto|];
repeat (rewrite app_length in H3;simpl in H3);repeat rewrite <- mir_len in H3;omega.

rewrite<- app_ass in H3;destruct (dec_uuv b (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;jrun ]. 

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 k;
    repeat rewrite<- (app_ass t0);rewrite H0;jrun
    | repeat rewrite app_length;repeat rewrite<- mir_len;omega].
rewrite<- H in H0;simplmots;
assert (length (t0  ++  t0  `   ++  t  `   ++  t) = (length (x  ++  (k :: eps)  ++  x  ` )));
  [rewrite H0;auto|];
repeat (rewrite app_length in H1;simpl in H1);repeat rewrite <- mir_len in H1;omega.


repl_mir H0;rewrite<- app_ass in H3;destruct (dec_uuv (a ` ) 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;jrun].
Qed.

(** Une fois cela établi, on peut aisément clore le premier cas. *)

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

(** Le second sous cas est le suivant :*)
(** %\[\begin{array}{|c|c|c|c|c|}
    \hline
    \multicolumn{2}{|c|}{\quad w_1\quad }&\convv w_1& \multicolumn{2}{c|}{w_1}\\
    \hline
    u_1&a&\convv w_1&c&u_2\\
    \hline
    u_1&\multicolumn{3}{c|}{w_2\convv w_2 w_2}&u_2\\
    \hline
  \end{array}\]%*)

(** Encore une fois on le découpe en deux parties :*)

(** %\begin{enumerate}
\item $\begin{array}{|c|c|c|c|c|c|c|}
    \hline
    \multicolumn{2}{|c|}{w_1}&\multicolumn{3}{c|}{\convv w_1}& \multicolumn{2}{c|}{w_1}\\
    \hline
    u_1&a&b&\convv w_2&c&d&u_2\\
    \hline
    u_1&\multicolumn{2}{c|}{w_2}& \convv w_2&\multicolumn{2}{c|}{w_2}&u_2\\
    \hline
  \end{array}\quad
\begin{array}{rcl}u_1w_2u_2&=&u_1abu_2=\convv cw_2\convv bbu_2=\convv cab\convv bbu_2\\
&\>&\convv cabu_2=\convv c cdu_2=\convv c c \convv c w_2\convv b\> \convv c w_2 \convv b = w_1\end{array}$
\item $\begin{array}{|c|c|c|c|c|c|c|}
    \hline
    \multicolumn{2}{|c|}{w_1}&\multicolumn{2}{c|}{\convv w_1}& \multicolumn{3}{c|}{w_1}\\
    \hline
    u_1&a&b&c&d&w_2&u_2\\
    \hline
    u_1&\multicolumn{2}{c|}{w_2}&\multicolumn{2}{c|}{\convv w_2}& w_2&u_2\\
    \hline
  \end{array}\ ou\ 
  \begin{array}{|c|c|c|c|c|c|c|}
    \hline
    \multicolumn{3}{|c|}{w_1}&\multicolumn{2}{c|}{\convv w_1}& \multicolumn{2}{c|}{w_1}\\
    \hline
    u_1&w_2&a&b&c&d&u_2\\
    \hline
    u_1& w_2&\multicolumn{2}{c|}{\convv w_2}&\multicolumn{2}{c|}{w_2}&u_2\\
    \hline
  \end{array}$
\end{enumerate} %*)
(** On voit que les deux configurations sur la deuxième ligne sont équivalentes, par [join_mir]. On ne traite donc que la première, dans le lemme suivant : *)

Lemma confl_cas2aux : forall w1 w2 u1 a b c d u2, 
w1=u1 ++ a -> w1 `  = b ++ c -> w1 = d ++ w2 ++ u2 -> w2 = a ++ b -> w2 `  = c ++ d -> 
w1 <--> (u1 ++ w2 ++ u2).
Proof.
intros w1 w2 u1 a b c d u2 -> 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 u1);rewrite H0;jrun
  |repl_mir H4;repeat rewrite<- app_ass in H2;apply dec_len_eq in H2 as (H2,h);
    [symmetry in h;automir h t1 k; rewrite h in *;clear h;rewrite<-(app_ass u1);
      rewrite H0;jrun
      |symmetry;apply mir_len]].
rewrite<- H2 in H3;simplmots.
assert (length (a  ++  u2  `   ++  t1  ++  (k :: eps)  `   ++  t1 ` )=length(d  `   ++  d  ++  a  ++  u2  ` ));
  [rewrite H3;auto|].
repeat (rewrite app_length in H;simpl in H);repeat rewrite <- mir_len in H;omega.
Qed.
(** %Posons l'équation
\begin{equation}
  \convv c\convv b=w_1=dw_2u_2=(d\convv d\convv c)u_2
\end{equation}
On la décompose (parenthésé comme ci-dessus):
\begin{enumerate}
\item $\convv c = d\convv d\convv c x\wedge u_2=x\convv b$. On déduit de la première équation que $|c|=2|d|+|c|+|x|$, et donc que $d=x=\epsilon$. Par conséquent $u_2=\convv b$
  \[u_1w_2u_2=u_1ab \convv b=\convv c \convv bb \convv b\>\convv c \convv b = w_1.\] 
\item $d\convv d\convv c = \convv c x\wedge \convv b=xu_2$. En mélangeant ces deux équation et en se rappelant que $ab=\convv d\convv c$, on arrive à:
  \[da\convv u_2\convv x = dab=d\convv d\convv c = \convv c x.\]
\end{enumerate}%*)
(**  Comme $|x|=|\convv x|$, on déduit $da\convv u_2 = \convv c \wedge \convv x = x$. En repassant cela dans la seconde équation on remarque que $bu_2=\convv u_2\convv x u_2=\convv u_2x u_2=\convv u_2\convv b=\convv{bu_2}$. Le lemme [automir] nous permet d'écrire $bu_2=y\convv y$%\footnote{$d\convv d\convv c=\convv c x$, donc $|x| = 2|d|$, or $|bu_2|=2|u_2|+|x| = 2(|u_2|+|x|)$ ce qui exclue le cas $bu_2=yk\convv y$.}%, et donc :
  %\[u_1w_2u_2=u_1abu_2=dabu_2bu_2=day\convv yy\convv y\>day\convv y=dabu_2=w_1\]%
*)

(** On peut donc conclure sur le second sous-cas : *)

Lemma confl_cas2 : forall w1 u1 w2 u2 a b : mot,
w1 = u1 ++ a -> w1 = b ++ u2 -> w2 ++ w2 `  ++ w2=a ++ w1 `  ++ b ->
w1 <--> (u1 ++ w2 ++ u2).
Proof.
intros w1 u1 w2 u2 a b h h1 h2;decomp h2.
rewrite<- (app_ass t) in H1;rewrite <- (app_ass (t ++ t ` )) in H1;decomp H1.
rewrite<- (app_ass a);repl_mir H2;rewrite<- (app_ass u1);rewrite<- h;jrun;
  rewrite <- (app_ass a t u2);rewrite H3;simplmots;rewrite<- h1;jrun.
clear H3;decomp H2;[nested h1;rewrite<- h;jid|].
rewrite<- (app_ass a) in *;eapply confl_cas2aux;eauto;simplmots;auto.

decomp H1;[apply join_mir;simplmots|nested h;rewrite<- h1;jid].
decomp H3;[|repl_mir H2;nested h; nested h;jid].
rewrite bij_mir in h;rewrite bij_mir in h1;simplmots;rewrite H1 in *;
  rewrite <- (app_ass (b ` )) in *;eapply confl_cas2aux;eauto;[|rewrite H2];
    rewrite mir_app;eauto.
Qed.
 
(** Et cela nous permet de terminer la première partie : *)

Lemma confl_part1 :
forall w1 u1 w2 u2, w1 ++ w1 `  ++ w1 = u1 ++ w2 ++ w2 `  ++ w2 ++ u2 -> 
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;
        eapply confl_cas2;eauto
        |apply confl_cas1;simplmots;rewrite H2];
      auto
    | decomp H1;
      [repeat rewrite<- app_ass in H3;decomp H3;
        [apply join_mir;simplmots;apply confl_cas1;simplmots;f_equal;
          rewrite bij_mir; simplmots;rewrite H1;repeat rewrite<- app_ass;
            f_equal;auto
          |repl_mir H2]
        |];
      jrun].
Qed.

(** *** Deuxième partie *)
(** 
On étudie maintenant le cas où les deux motifs sont décalés c'est à dire 
%\[u_1w_1\convv w_1 w_1 = w_2\convv w_2 w_2 u_2.\]% 
*)

(** On a besoin d'un lemme auxiliaire :*)

Lemma confl_part2aux : 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.

(** On décompose ensuite en différents cas, les cas compliqués étant traité dans les lemmes suivants :
%\begin{enumerate}
\setcounter{enumi}{2}
\item $u_1w_1 = w_2\convv w_2 w_2x\wedge u_2=x\convv w_1 w_1$. On peut tout de suite remarquer que $u_1w_1 \>w_2x$.
  \begin{enumerate}[1]
  \item $\begin{array}{|c|c|c|c|c|}
      \hline
      \multicolumn{2}{|c|}{u_1}&\multicolumn{2}{c|}{w_1}&\convv w_1w_1\\
      \hline
      w_2\convv w_2&a&b&x&\convv w_1w_1\\
      \hline
      w_2\convv w_2&\multicolumn{2}{c|}{w_2}& \multicolumn{2}{c|}{u_2}\\
      \hline
    \end{array}\quad  w_2u_2=abx\convv x \convv b b x\>abx = w_2x.$
  \item $\begin{array}{|c|c|c|c|c|c|}
      \hline
      \multicolumn{2}{|c|}{u_1}&\multicolumn{3}{c|}{w_1}&\convv w_1w_1\\
      \hline
      w_2&a&b&w_2&x&\convv w_1w_1\\
      \hline
      w_2&\multicolumn{2}{c|}{\convv w_2}&w_2& \multicolumn{2}{c|}{u_2}\\
      \hline
    \end{array}\quad  w_2u_2 = \convv b\convv ax\convv xa b\convv b b\convv b\convv a x\>^2\convv b\convv a x=w_2 x$
  \item $\begin{array}{|c|c|c|c|c|}
      \hline
      u_1&\multicolumn{3}{c|}{w_1}&\convv w_1w_1\\
      \hline
      u_1&a&\convv w_2w_2&x&\convv w_1w_1\\
      \hline
      \multicolumn{2}{|c|}{w_2}&\convv w_2w_2& \multicolumn{2}{c|}{u_2}\\
      \hline
    \end{array}\quad w_2u_2 = u_1ax\convv x\ \convv a\ \convv u_1u_1a\convv aa\convv a\ \convv u_1u_1ax\>^3 u_1ax=w_2 x$
  \end{enumerate}


\item $w_1=xu_2 \wedge u_1w_1\convv w_1 x = w_2\convv w_2 w_2$ : 
\verb&Lemma confl_cas4&
  \begin{enumerate}[1]
  \item $\begin{array}{|c|c|c|c|c|}
      \hline
      \multicolumn{2}{|c|}{u_1}&w_1\convv w_1&\multicolumn{2}{c|}{w_1}\\
      \hline
      w_2\convv w_2&a&w_1\convv w_1&x&u_2\\
      \hline
      w_2\convv w_2&\multicolumn{3}{c|}{w_2}& u_2\\
      \hline
    \end{array}\quad \begin{array}{rcl}
      w_2u_2&=&aw_1\convv w_1 x u_2 = a w_1\convv w_1 w_1 \> a w_1\\
      u_1w_1&=& w_2\convv w_2 aw_1=a w_1\convv w_1 x\convv xw_1\convv w_1\convv a a w_1\\
      &=&a w_1\convv w_1 x\convv xxu_2\convv w_1\convv a a w_1\> a w_1\convv w_1 xu_2\convv w_1\convv a a w_1\\
      &=&a w_1\convv w_1 w_1\convv w_1\convv a a w_1\>^2 aw_1.
    \end{array}$
  \item $\begin{array}{|c|c|c|c|c|c|c|}
      \hline
      \multicolumn{2}{|c|}{u_1}&\multicolumn{2}{c|}{w_1}&\convv w_1&\multicolumn{2}{c|}{w_1}\\
      \hline
      w_2&a&b&c&\convv w_1&x&u_2\\
      \hline
      w_2&\multicolumn{2}{c|}{\convv w_2}&\multicolumn{3}{c|}{w_2}& u_2\\
      \hline
    \end{array}\quad \begin{array}{rcl}
      u_1w_1&=& w_2abc=w_2\convv w_2 c=c\convv u_2\ \convv x x \convv x x u_2\convv c c\\
      &\>&c\convv u_2\ \convv x x u_2\convv c c=c\convv w_1 bc\convv c c\>c\convv w_1 bc=c\convv w_1 xu_2=w_2 u_2.
    \end{array}$
  \item $\begin{array}{|c|c|c|c|c|c|c|}
      \hline
      \multicolumn{2}{|c|}{u_1}&w_1&\multicolumn{2}{c|}{\convv w_1}&\multicolumn{2}{c|}{w_1}\\
      \hline
      w_2&a&w_1&b&c&x&u_2\\
      \hline
      w_2&\multicolumn{3}{c|}{\convv w_2}&\multicolumn{2}{c|}{w_2}& u_2\\
      \hline
    \end{array}\quad $ \verb&Lemma confl_cas4_3&
  \item $\begin{array}{|c|c|c|c|c|c|c|}
      \hline
      u_1&\multicolumn{2}{c|}{w_1}&\multicolumn{2}{c|}{\convv w_1}&\multicolumn{2}{c|}{w_1}\\
      \hline
      u_1&a&b&c&d&x&u_2\\
      \hline
      \multicolumn{2}{|c|}{w_2}&\multicolumn{2}{c|}{\convv w_2}&\multicolumn{2}{c|}{w_2}& u_2\\
      \hline
    \end{array}\quad $ \verb&Lemma confl_cas4_4&
  \end{enumerate}


\item $w_2\convv w_2 w_2=u_1w_1x\wedge \convv w_1= xy\wedge u_2 = yw_1$:
\verb&Lemma confl_cas5&
  \begin{enumerate}[1]

  \item $\begin{array}{|c|c|c|c|c|c|c|}
      \hline
      \multicolumn{2}{|c|}{u_1}&\multicolumn{2}{c|}{w_1}&\multicolumn{2}{c|}{\convv w_1}&w_1\\
      \hline
      w_2&a&b&c&x&y&w_1\\
      \hline
      w_2& \multicolumn{2}{c|}{\convv w_2}&\multicolumn{2}{c|}{w_2}& \multicolumn{2}{c|}{u_2}\\
      \hline
    \end{array}\quad $ \verb&Lemma confl_cas5_1&

  \item $\begin{array}{|c|c|c|c|c|c|}
      \hline
      \multicolumn{2}{|c|}{u_1}&w_1&\multicolumn{2}{c|}{\convv w_1}&w_1\\
      \hline
      w_2\convv w_2&a&w_1&x&y&w_1\\
      \hline
      w_2\convv w_2&\multicolumn{3}{c|}{w_2}& \multicolumn{2}{c|}{u_2}\\
      \hline
    \end{array}\quad \begin{array}{rcl}
      w_2u_2&=&aw_1xyw_1=aw_1\convv w_1 w_1 \> aw_1\\
      u_1w_1&=&w_2\convv w_2aw_1=aw_1x\convv x\ \convv {aw_1} a w_1\\
      &=&a\convv y\ conv xx\convv x\ \convv {aw_1} a w_1\\
      &\>& a\convv y\ conv x\ \convv {aw_1} a w_1\\
      &=&aw_1\convv {aw_1} a w_1\> aw_1.
    \end{array}$

  \item $\begin{array}{|c|c|c|c|c|c|c|}
      \hline
      u_1&\multicolumn{2}{c|}{w_1}&\multicolumn{3}{c|}{\convv w_1}&w_1\\
      \hline
      u_1&a&b&c&w_2&y&w_1\\
      \hline
      \multicolumn{2}{|c|}{w_2}&\multicolumn{2}{c|}{\convv w_2}&w_2& \multicolumn{2}{c|}{u_2}\\
      \hline
    \end{array}\quad $ \verb&Lemma confl_cas5_3&

  \item $\begin{array}{|c|c|c|c|c|c|c|}
      \hline
      u_1&\multicolumn{3}{c|}{w_1}&\multicolumn{2}{c|}{\convv w_1}&w_1\\
      \hline
      u_1&a&\convv w_2&b&x&y&w_1\\
      \hline
      \multicolumn{2}{|c|}{w_2}&\convv w_2&\multicolumn{2}{c|}{w_2}& \multicolumn{2}{c|}{u_2}\\
      \hline
    \end{array}\quad \begin{array}{rcl}
      w_2u_2&=&w_2yw_1=b\ xy\ a\convv w_2b\\ 
      &=&b\convv bw_2\convv a a \convv a\convv u_1 b
      =b\convv bbx\convv a a \convv a\ \convv u_1 b\\ &\>^2& bx\convv a \convv u_1 b = u_1a\convv w_2 b =u_1 w_1.
    \end{array}$
  \end{enumerate}
\end{enumerate}%*)

Lemma confl_cas4_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 ->
(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 *; jrun].

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;jrun].

rewrite h4;rewrite bij_mir in h4;simplmots;rewrite<- (app_ass (u2 ` )) in h4;
apply dec_len_eq in h4 as (->,h4);
  [jrun|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&[->|(x&->&hx)]);jrun
  |apply mir_len].
assert (length (b  `   ++  b  ++  t0) = length (t0  ++  v  ++  (x :: eps)  ++  v  ` ));
[rewrite H2;auto|
repeat (rewrite app_length in H;simpl in H);repeat rewrite <- mir_len in H;omega].
Qed.

Lemma confl_cas4_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 ->
(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_part2aux 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 k;
    repeat(repeat rewrite<-(app_ass q p) in *;rewrite<-(app_ass _ a);rewrite h6;
      simplmots);repeat rewrite<- (app_ass p);rewrite h1;jrun
    |repeat rewrite app_length;repeat rewrite<- mir_len;omega].
rewrite h1 in h6;repeat (rewrite<- app_ass in *);rewrite h6 in h4;simplmots.
decomp h_.
repl_mir H1.
assert (length (a  ++  v  ++  (k :: eps)  ++  v  `   ++  a  `   ++  t  ` ) = length (t  `   ++  c  ++  c  ` ));
[rewrite h4;auto|repeat (rewrite app_length in H;simpl in H);repeat rewrite <- mir_len in H;omega].
assert (length (t  ++  u2  `   ++  v  ++  (k :: eps)  ++  v  `   ++  u2) = length (d  `   ++  d  ++  t));
[rewrite h4;auto|repeat (rewrite app_length in H;simpl in H);repeat rewrite <- mir_len in H;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&[->|(k&h5&hx)]);[rewrite h3;jrun|]
      |symmetry;apply mir_len].
rewrite h5 in h1.
assert (length (u1  ++  (v  ++  (k :: eps)  ++  v  ` )  ++  e) = length (c `   ++  b ` ));
[rewrite h1;auto|];
assert (length (b  `   ++  e  ` ) = length (c  ++  u1));
[rewrite h4;auto|];
repeat (rewrite app_length in *;simpl in *);repeat rewrite <- mir_len in *;omega.
Qed.

Lemma confl_cas4 : forall u1 a u2 w2 : mot, 
w2 ++ w2 `  ++ w2=u1 ++ a ++ u2 ++ (a ++ u2) `  ++ a -> 
(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;jid].

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;jid].

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;jid
    | rewrite<- (app_ass _ _ u2);eapply confl_cas4_4;eauto;[rewrite H0|rewrite<- H3];
      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;jid
    |rewrite<- (app_ass a) in H4;decomp H4]
  |jrun].

repl_mir H5;rewrite H0 in H2;rewrite<- app_ass;eapply confl_cas4_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;jrun;rewrite<- H5;
    jrun.
Qed.

Lemma confl_cas5_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 ->
(u1 ++ w1) <--> (w2 ++ u2).
Proof.
cut (forall a b c d : mot, a `  ++ a ++ b=c ++ d ++ d `  -> 
  (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_red;auto.

cut (forall a b (p: mot * mot), (fst p) `  ++ (fst p) ++ a=b ++ (snd p) ++ (snd p) `  -> 
  (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;jrun.
decomp H1;[rewrite<- app_ass in H2|clear H3 H4];decomp H2;[repl_mir H3| | |repl_mir H3];jrun.
rewrite bij_mir in H3;rewrite bij_mir in H5;simplmots;rewrite H3 in *;clear H4;
rewrite H5 in *.
decomp H1;[nested H5;jid|].
repeat (rewrite<- (app_ass (t0 ` ));rewrite H4).
cut ((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_red;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;jid|]].

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_cas5_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 ->
(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_part2aux 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| |];jrun;
            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];jrun.
Qed.

Lemma confl_cas5 : forall u1 w1 u2 w2 a b, w1 `  = a ++ b -> u2=b ++ w1 -> u1 ++ w1 ++ a = w2 ++ w2 `  ++ w2 -> 
(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;[|jrun | |nested H3;jid].

rewrite<- (app_ass w2);rewrite<- H0;
  decomp2 H3;
  [repl_mir h0;rewrite <- (app_ass t1) in h1;eapply confl_cas5_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 *;
        [eapply confl_cas5_1;eauto;simplmots;auto
          |repl_mir H2;rewrite h4 in *;simplmots;nested h4;rewrite H0;jid]].

rewrite<- (app_ass (t ` )) in H3;decomp H3;
  [rewrite<-(app_ass u1 t (b ++ t ++ t0));eapply confl_cas5_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;jrun].
Qed.

(** On peut ainsi conclure simplement ce cas : *)

Lemma confl_part2 : forall u1 w1 u2 w2, u1 ++ w1 ++ w1 `  ++ w1 = w2 ++ w2 `  ++ w2 ++ u2 -> 
(u1 ++ w1) <--> (w2 ++ u2).
(* begin show *)
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|]]];jrun
      |eapply confl_cas5;[exact H3| |];auto]
    |apply confl_cas4;simplmots;auto].
Qed.


(** *** Conclusion *)
(** La preuve du théorème est maintenant très simple : *)

Theorem confluence_locale :
forall u v1 v2 : mot, u --> v1 -> u --> v2 -> 
v1 <--> v2.
Proof.
intros u t1 t2 (u1 & v1 & w1 & h & h1) (u2 & v2 & w2 & h2 & h3); 
  unfold reduction in *;rewrite h2 in h;
    decomp h;repeat rewrite<- app_ass in H1;decomp H1;
      [apply confl_part1 in H2
        |apply confl_part2 in H2
        |apply confl_part1 in H2
        |apply confl_part2 in H2];
      destruct H2 as (v3 & h0 & h);
        [rewrite<- (app_ass t) in h3;rewrite<- (app_ass (t ++ w2)) in h3
          |rewrite<- (app_ass t) in h3;rewrite<- (app_ass w1) in h1
          |rewrite<- (app_ass t) in h1;rewrite<- (app_ass (t ++ w1)) in h1
          |rewrite<- (app_ass w2) in h3;rewrite<- (app_ass t) in h1];
        rewrite h1; rewrite h3;
          [exists (u1 ++ v3 ++ v1)
            |exists (u1 ++ v3 ++ v2)
            |exists (u2 ++ v3 ++ v2)
            |exists (u2 ++ v3 ++ v1)];
          split;apply cntxt_red;simplmots;auto.
Qed.
(* end show *)