Section Peq.
Load "hCoefStructure".
Load "hOrderStructure".
Load "hOrder".
Definition size := length (A:=Term A n).
Definition sizel m := match m with
| (l1, l2) => size l1 + size l2
end.
Definition lessP m1 m2 := sizel m1 < sizel m2.
Hint Unfold lessP.
red in |- *.
cut (forall (m : nat) a, sizel a < m -> Acc lessP a).
intros H' a.
apply H' with (m := S (sizel a)); auto.
intros m; elim m; clear m.
intros a H; inversion H.
intros m H' a H'0.
apply Acc_intro.
intros y H'1; apply H'.
unfold lessP in H'1.
apply lt_le_trans with (sizel a); auto with arith.
Qed.
Hint Resolve wf_lessP.
intros n1 n2 l1 l2 H' H'0; rewrite H'0; rewrite H'; auto.
Qed.
Hint Resolve pX_inj.
intros a b p q H'; inversion H'; auto.
Qed.
Hint Unfold pX.
unfold pX, pX in |- *.
intros p q a b H'; inversion H'; auto.
Qed.
split; auto.
apply olistO.
red in |- *; simpl in |- *; auto.
Qed.
intros a H; split; simpl in |- *; auto.
red in |- *; simpl in |- *; apply Relation_Operators_compat.d_one.
Qed.
Hint Resolve canonicalp1 canonical0.
Inductive eqP : list (Term A n) -> list (Term A n) -> Prop :=
| eqP0 : eqP (pO A n) (pO A n)
| eqpP1 :
forall ma mb p q,
eqTerm (A:=A) eqA (n:=n) ma mb -> eqP p q -> eqP (pX ma p) (pX mb q).
Hint Resolve eqP0 eqpP1.
red in |- *.
intros x; elim x; auto.
intros a l H; change (eqP (pX a l) (pX a l)) in |- *; apply eqpP1; auto.
Qed.
red in |- *.
intros x y H'; elim H'; auto.
intros ma mb p q H H0 H1.
apply eqpP1; auto.
apply (eqTerm_sym _ _ _ _ _ _ _ _ _ cs n); auto.
Qed.
unfold pO in |- *; intros p H'; inversion H'; auto.
Qed.
unfold pO in |- *; intros p H'; inversion H'; auto.
Qed.
intros a b p q H'; simple inversion H'; auto.
unfold pO in H0; inversion H0.
rewrite (pX_invl ma a p0 p); auto; rewrite (pX_invl mb b q0 q); auto.
Qed.
intros a b p q H'; simple inversion H'; auto.
unfold pO in H0; inversion H0.
rewrite (pX_invr p0 p ma a); auto; rewrite (pX_invr q0 q mb b); auto.
Qed.
red in |- *.
intros x; elim x; auto.
intros y z H'; inversion_clear H'.
intros H'0; inversion_clear H'0; auto.
intros a l H' y z H'0; inversion_clear H'0.
intros H4; inversion_clear H4.
change (eqP (pX a l) (pX mb0 q0)) in |- *; apply eqpP1; eauto.
apply (eqTerm_trans _ _ _ _ _ _ _ _ _ cs n) with (y := mb); auto.
Qed.
Hint Resolve eqp_refl.
Let eqTerm_imp_eqT := eqTerm_imp_eqT A eqA n.
intros a p; case p; auto.
intros a0 l H'.
apply ltP_trans with (y := pX a (pO A n)); auto.
change (ltP (A:=A) (n:=n) ltM (pX a0 l) (pX a (pO A n))) in |- *.
apply ltP_hd; auto.
apply (canonical_pX_order A A0) with (eqA := eqA) (l := l); auto.
apply ltP_tl; auto.
change (ltP (A:=A) (n:=n) ltM (pO A n) (pX a0 l)) in |- *; apply ltPO.
Qed.
intros a b p q H'; inversion_clear H'; trivial.
Qed.
intros p q r s H'; generalize r s; elim H'; clear r s H'; auto.
intros x p0 r s H H0 H1; inversion H1.
intros H4; inversion H4; apply ltPO.
intros x y p0 q0 H r s H0 H1 H2 H3; inversion_clear H2; try discriminate;
inversion_clear H3; try discriminate; auto.
apply ltP_hd.
apply ltT_eqTl with (a := y); auto.
apply ltT_eqTr with (a := x); auto.
intros x y p0 q0 H H0 H1 r s H2 H3 H4 H5; inversion_clear H4;
try discriminate; inversion_clear H5; try discriminate.
apply ltP_tl; auto.
apply (eqT_trans A n) with (y := y); auto.
apply (eqT_trans A n) with (y := x); auto.
apply (eqT_sym A n); auto.
apply H1; auto.
apply canonical_imp_canonical with (a := x); auto.
apply canonical_imp_canonical with (a := y); auto.
Qed.

Theorem eqp_imp_canonical :
forall p q, eqP p q -> canonical A0 eqA ltM p -> canonical A0 eqA ltM q.