Library NumAnalysis.Subset.Subsets.FF.Lex

This file is part of the Rocq Numerical Analysis library
Copyright (C) Boldo, Clément, Martin, Mayero
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the COPYING file for more details.

Brief description

Support for lexicographic orders on finite families.

Description

Lexicographic orders are generalizations, and variants, of the alphabetical order in dictionaries to finite families of any type.
Let T be any type. Let R : T → T → Prop be an homogeneous binary relation on T. Let n : nat.
  • lex R is the binary relation defined for all x y : 'T^n.+1 by R (x i) (y i) for the first index i such that x i ≠ y i, and lex R x y for all x y : 'T^0 iff R is reflexive.
  • colex R is the binary relation defined for all x y : 'T^n.+1 by R (x i) (y i) for the last index i such that x i ≠ y i, ie words of the dictionary are sorted from their last letter, and colex R x y for all x y : 'T^0 iff R is reflexive.
  • symlex R is the converse of lex R, ie it is defined for all x y : 'T^n.+1 by R (y i) (x i) for the first index i such that x i ≠ y i.
  • revlex R is the converse of colex R, ie it is defined for all x y : 'T^n.+1 by R (y i) (x i) for the last index i such that x i ≠ y i.

Used logic axiom

Usage

This module may be used through the import of NumAnalysis.Subset.Subsets.Subsets, NumAnalysis.Subset.Subsets.Subsets_wDep, NumAnalysis.Algebra.Algebra_wDep, NumAnalysis.Lebesgue.Lebesgue_p_wDep, NumAnalysis.Lebesgue.Bochner.Bochner_wDep, NumAnalysis.LM.LM_wDep, or NumAnalysis.FEM.FEM_wDep, where it is exported.

From NumAnalysis Require Import stdlib.

From NumAnalysis Require Import ssr_wMC.

From NumAnalysis Require Import Numbers_wDep.
From NumAnalysis Require Import Function Binary_relation.
From NumAnalysis Require Import ord_compl Finite_family.

Section Homogeneous_binary_relation_FF_Def.

Context {T : Type}.
Context {n : nat}.
Variable Rn : 'T^n → 'T^n → Prop.

Definition reverse (x y : 'T^n) : Prop := Rn (revF x) (revF y).

End Homogeneous_binary_relation_FF_Def.

Section Homogeneous_binary_relation_FF_Facts1.

Context {T : Type}.
Context {n : nat}.

Lemma rev_invol : involutive (@reverse T n).
Proof. intros Rn; unfold reverse; fun_ext2; rewrite !revF_invol; easy. Qed.

Lemma rev_eq : reverse eq = @eq 'T^n.
Proof. unfold reverse; fun_ext2; apply prop_ext, revF_ext. Qed.

Lemma rev_neq : reverse neq = @neq 'T^n.
Proof. unfold reverse; fun_ext2; apply prop_ext, revF_next. Qed.

Lemma rev_conv_eq :
  ∀ (Rn : 'T^n → 'T^n → Prop),
    reverse (converse Rn) = converse (reverse Rn).
Proof. easy. Qed.

Lemma conv_rev_eq :
  ∀ (Rn : 'T^n → 'T^n → Prop),
    converse (reverse Rn) = reverse (converse Rn).
Proof. easy. Qed.

Lemma rev_br_and :
  ∀ {n} {Rn1 Rn2 : 'T^n → 'T^n → Prop},
    reverse (br_and Rn1 Rn2) = br_and (reverse Rn1) (reverse Rn2).
Proof. easy. Qed.

Lemma rev_br_or :
  ∀ {n} {Rn1 Rn2 : 'T^n → 'T^n → Prop},
    reverse (br_or Rn1 Rn2) = br_or (reverse Rn1) (reverse Rn2).
Proof. easy. Qed.

End Homogeneous_binary_relation_FF_Facts1.

Section Homogeneous_binary_relation_FF_Facts2.

Context {T : Type}.
Context {n : nat}.

Lemma rev_br_and_neq :
  ∀ {n} {Rn : 'T^n → 'T^n → Prop},
    reverse (br_and_neq Rn) = br_and_neq (reverse Rn).
Proof. intros; rewrite rev_br_and rev_neq; easy. Qed.

Lemma rev_br_or_eq :
  ∀ {n} {Rn : 'T^n → 'T^n → Prop},
    reverse (br_or_eq Rn) = br_or_eq (reverse Rn).
Proof. intros; rewrite rev_br_or rev_eq; easy. Qed.

End Homogeneous_binary_relation_FF_Facts2.

Section Lex_orders_Def.

Context {T : Type}.
Hypothesis HT0 : inhabited T.
Variable R : T → T → Prop.

Lexicographic order on 'T^n.

Fixpoint lex {n} (x y : 'T^n) : Prop :=
  match n as p return (n = p → _) with
  | 0 ⇒ fun⇒ reflexive R
  | S m ⇒ fun H ⇒
      castF H x ord0 ≠ castF H y ord0 ∧
        R (castF H x ord0) (castF H y ord0) ∨
      castF H x ord0 = castF H y ord0 ∧
        lex (skipF0 (castF H x)) (skipF0 (castF H y))
  end erefl.

Co-lexicographic order on 'T^n.
Definition colex {n} : 'T^n → 'T^n → Prop := reverse lex.

Symmetric, or converse, lexicographic order on 'T^n.
Definition symlex {n} : 'T^n → 'T^n → Prop := converse lex.

Reverse lexicographic order on 'T^n.
Definition revlex {n} : 'T^n → 'T^n → Prop := converse colex.

Correctness lemmas.

Lemma lex_rev_eq : ∀ {n}, @reverse _ n lex = colex.
Proof. easy. Qed.

Lemma colex_rev_eq : ∀ {n}, @reverse _ n colex = lex.
Proof. intros; rewrite rev_invol; easy. Qed.

Lemma symlex_rev_eq : ∀ {n}, @reverse _ n symlex = revlex.
Proof. intros; apply rev_conv_eq. Qed.

Lemma revlex_rev_eq : ∀ {n}, @reverse _ n revlex = symlex.
Proof. intros; rewrite rev_conv_eq rev_invol; easy. Qed.

Lemma lex_revF_equiv :
  ∀ {n} (x y : 'T^n), lex (revF x) (revF y) ↔ colex x y.
Proof. easy. Qed.

Lemma colex_revF_equiv :
  ∀ {n} (x y : 'T^n), colex (revF x) (revF y) ↔ lex x y.
Proof. intros; rewrite -colex_rev_eq; easy. Qed.

Lemma symlex_revF_equiv :
  ∀ {n} (x y : 'T^n), symlex (revF x) (revF y) ↔ revlex x y.
Proof. intros; rewrite -symlex_rev_eq; easy. Qed.

Lemma revlex_revF_equiv :
  ∀ {n} (x y : 'T^n), revlex (revF x) (revF y) ↔ symlex x y.
Proof. intros; rewrite -revlex_rev_eq; easy. Qed.

Lemma lex_nil : ∀ (x y : 'T^0), lex x y ↔ reflexive R.
Proof. easy. Qed.

Lemma colex_nil : ∀ (x y : 'T^0), colex x y ↔ reflexive R.
Proof. easy. Qed.

Lemma symlex_nil : ∀ (x y : 'T^0), symlex x y ↔ reflexive R.
Proof. easy. Qed.

Lemma revlex_nil : ∀ (x y : 'T^0), revlex x y ↔ reflexive R.
Proof. easy. Qed.

Lemma lex_S :
  ∀ {n} {x y : 'T^n.+1},
    lex x y ↔
    x ord0 ≠ y ord0 ∧ R (x ord0) (y ord0) ∨
    x ord0 = y ord0 ∧ lex (skipF0 x) (skipF0 y).
Proof. intros; simpl; repeat rewrite castF_refl; easy. Qed.

Lemma colex_S :
  ∀ {n} {x y : 'T^n.+1},
    colex x y ↔
    x ord_max ≠ y ord_max ∧ R (x ord_max) (y ord_max) ∨
    x ord_max = y ord_max ∧ colex (skipFmax x) (skipFmax y).
Proof.
intros; unfold colex, reverse; rewrite -!(revF_S_0 x) -!(revF_S_0 y)
    -rev_ord_0 -!skipF_revF; apply lex_S.
Qed.

Lemma symlex_S :
  ∀ {n} {x y : 'T^n.+1},
    symlex x y ↔
    x ord0 ≠ y ord0 ∧ R (y ord0) (x ord0) ∨
    x ord0 = y ord0 ∧ symlex (skipF0 x) (skipF0 y).
Proof. move=>>; unfold symlex; rewrite eq_sym_equiv; apply lex_S. Qed.

Lemma revlex_S :
  ∀ {n} {x y : 'T^n.+1},
    revlex x y ↔
    x ord_max ≠ y ord_max ∧ R (y ord_max) (x ord_max) ∨
    x ord_max = y ord_max ∧ revlex (skipFmax x) (skipFmax y).
Proof. move=>>; unfold revlex; rewrite eq_sym_equiv; apply colex_S. Qed.

Lemma lex_irrefl_S :
  ∀ {n} {x y : 'T^n.+1},
    irreflexive R →
    lex x y ↔ R (x ord0) (y ord0) ∨
      x ord0 = y ord0 ∧ lex (skipF0 x) (skipF0 y).
Proof.
move=>> /irrefl_equiv HR; rewrite lex_S; split;
    (intros [H | H]; [left | right]); try split; try easy.
contradict H; apply HR; easy.
Qed.

Lemma colex_irrefl_S :
  ∀ {n} {x y : 'T^n.+1},
    irreflexive R →
    colex x y ↔ R (x ord_max) (y ord_max) ∨
      x ord_max = y ord_max ∧ colex (skipFmax x) (skipFmax y).
Proof.
intros; unfold colex, reverse;
    rewrite lex_irrefl_S// !revF_S_0 !skipF0_revF; easy.
Qed.

Lemma symlex_irrefl_S :
  ∀ {n} {x y : 'T^n.+1},
    irreflexive R →
    symlex x y ↔ R (y ord0) (x ord0) ∨
      x ord0 = y ord0 ∧ symlex (skipF0 x) (skipF0 y).
Proof. move=>>; unfold symlex; rewrite eq_sym_equiv; apply lex_irrefl_S. Qed.

Lemma revlex_irrefl_S :
  ∀ {n} {x y : 'T^n.+1},
    irreflexive R →
    revlex x y ↔ R (y ord_max) (x ord_max) ∨
      x ord_max = y ord_max ∧ revlex (skipFmax x) (skipFmax y).
Proof. move=>>; unfold revlex; rewrite eq_sym_equiv; apply colex_irrefl_S. Qed.

Lemma lex_1 :
  ∀ {x y : 'T^1},
    lex x y ↔
    x ord0 ≠ y ord0 ∧ R (x ord0) (y ord0) ∨ x ord0 = y ord0 ∧ reflexive R.
Proof. intros; rewrite lex_S lex_nil; easy. Qed.

Lemma colex_1 :
  ∀ {x y : 'T^1},
    colex x y ↔
    x ord_max ≠ y ord_max ∧ R (x ord_max) (y ord_max) ∨
    x ord_max = y ord_max ∧ reflexive R.
Proof. intros; rewrite colex_S colex_nil; easy. Qed.

Lemma symlex_1 :
  ∀ {x y : 'T^1},
    symlex x y ↔
    x ord0 ≠ y ord0 ∧ R (y ord0) (x ord0) ∨ x ord0 = y ord0 ∧ reflexive R.
Proof. intros; rewrite symlex_S symlex_nil; easy. Qed.

Lemma revlex_1 :
  ∀ {x y : 'T^1},
    revlex x y ↔
    x ord_max ≠ y ord_max ∧ R (y ord_max) (x ord_max) ∨
    x ord_max = y ord_max ∧ reflexive R.
Proof. intros; rewrite revlex_S revlex_nil; easy. Qed.

Lemma lex_irrefl_1 :
  irreflexive R → ∀ {x y : 'T^1}, lex x y ↔ R (x ord0) (y ord0).
Proof.
move⇒ HR =>>; rewrite lex_1; split.
intros [H | H]; [| apply irrefl_not_refl in HR]; easy.
intros H; left; split;
    [move: HR ⇒ /irrefl_equiv ⇒ HR; contradict H; apply HR |]; easy.
Qed.

Lemma colex_irrefl_1 :
  irreflexive R →
  ∀ {x y : 'T^1}, colex x y ↔ R (x ord_max) (y ord_max).
Proof.
intros; unfold colex, reverse; rewrite lex_irrefl_1// !revF_S_0; easy.
Qed.

Lemma symlex_irrefl_1 :
  ∀ {x y : 'T^1}, irreflexive R → symlex x y ↔ R (y ord0) (x ord0).
Proof. move=>> HR; unfold symlex; apply lex_irrefl_1; easy. Qed.

Lemma revlex_irrefl_1 :
  ∀ {x y : 'T^1},
    irreflexive R → revlex x y ↔ R (y ord_max) (x ord_max).
Proof. move=>> HR; unfold revlex; apply colex_irrefl_1; easy. Qed.

"w_eq" stands for "when equal".
Lemma lex_equiv_w_eq :
  ∀ {n} {x y : 'T^n}, x = y → lex x y ↔ reflexive R.
Proof.
intros n; induction n; [easy | intros; subst; rewrite lex_S IHn; tauto].
Qed.

"w_neq" stands for "when not equal".
Lemma lex_equiv_w_neq :
  ∀ {n} {x y : 'T^n}, x ≠ y → lex x y ↔
    ∃ (i : [0..n)),
      (∀ (j : [0..n)), (j < i)%coq_nat → x j = y j) ∧
      x i ≠ y i ∧ R (x i) (y i).
Proof.
intros n; induction n; intros x y H; [contradict H; apply hat0F_unit |];
    split; rewrite lex_S.
intros [[H0 H1] | [H0 H1]]; [∃ ord0; easy |].
destruct (neqxF_compat ord0 H) as [| H2]; [easy |].
destruct (IHn _ _ (skipF_neqxF_compat H2)) as [H3 _].
destruct (H3 H1) as [i [Hi1 Hi2]]; ∃ (lift_S i); split;
    [| rewrite !skipF_first in Hi2; easy].
intros j; destruct (ord_eq_dec j ord0) as [-> | Hj1]; [easy | intros Hj2].
specialize (Hi1 (lower_S Hj1)); rewrite !skipF_first !liftF_lower_S in Hi1.
apply Hi1; rewrite -(lower_lift_S (lift_S_not_first i));
    apply lower_S_incrS; easy.
intros [i [Hi1 Hi2]].
destruct (ord_eq_dec i ord0) as [-> | Hi0]; [left; easy | right].
assert (H0 : x ord0 = y ord0) by now apply Hi1, ord_n0_gt_equiv.
split; [easy | apply IHn]; [contradict H; apply (extF_skipF ord0); easy |].
∃ (lower_S Hi0); split; [| rewrite !skipF_first !liftF_lower_S; easy].
intros j Hj; apply Hi1; rewrite skip_ord_first -(lift_lower_S Hi0).
apply lift_S_incrF; easy.
Qed.

"w_eq" stands for "when equal".
Lemma colex_equiv_w_eq :
  ∀ {n} {x y : 'T^n}, x = y → colex x y ↔ reflexive R.
Proof. intros; subst; unfold colex; apply lex_equiv_w_eq; easy. Qed.

"w_neq" stands for "when not equal".
Lemma colex_equiv_w_neq :
  ∀ {n} {x y : 'T^n}, x ≠ y → colex x y ↔
    ∃ (i : [0..n)),
      (∀ (j : [0..n)), (i < j)%coq_nat → x j = y j) ∧
      x i ≠ y i ∧ R (x i) (y i).
Proof.
intros; unfold colex, reverse;
    rewrite lex_equiv_w_neq; [| apply revF_next; easy].
split; intros [i [Hi1 Hi2]]; ∃ (rev_ord i); split; [| easy |..].
apply revF_first_rev; rewrite rev_ordK; easy.
apply revF_first; rewrite rev_ordK; easy.
rewrite -!revF_correct !revF_invol; easy.
Qed.

"w_eq" stands for "when equal".
Lemma symlex_equiv_w_eq :
  ∀ {n} {x y : 'T^n}, x = y → symlex x y ↔ reflexive R.
Proof. intros; apply lex_equiv_w_eq; easy. Qed.

"w_neq" stands for "when not equal".
Lemma symlex_equiv_w_neq :
  ∀ {n} {x y : 'T^n},
    x ≠ y → symlex x y ↔
    ∃ (i : [0..n)),
      (∀ (j : [0..n)), (j < i)%coq_nat → x j = y j) ∧
      x i ≠ y i ∧ R (y i) (x i).
Proof.
move=>> H; unfold symlex, converse; rewrite lex_equiv_w_neq;
    [| apply not_eq_sym; easy].
split; intros [i Hi]; ∃ i;
    (split; [intro |]); rewrite eq_sym_equiv; apply Hi.
Qed.

"w_eq" stands for "when equal".
Lemma revlex_equiv_w_eq :
  ∀ {n} {x y : 'T^n}, x = y → revlex x y ↔ reflexive R.
Proof. intros; apply colex_equiv_w_eq; easy. Qed.

"w_neq" stands for "when not equal".
Lemma revlex_equiv_w_neq :
  ∀ {n} {x y : 'T^n},
    x ≠ y → revlex x y ↔
    ∃ (i : [0..n)),
      (∀ (j : [0..n)), (i < j)%coq_nat → x j = y j) ∧
      x i ≠ y i ∧ R (y i) (x i).
Proof.
move=>> H; unfold revlex, converse; rewrite colex_equiv_w_neq;
    [| apply not_eq_sym; easy].
split; intros [i Hi]; ∃ i;
    (split; [intro |]); rewrite eq_sym_equiv; apply Hi.
Qed.

End Lex_orders_Def.

Section Lex_orders_Facts1.

Context {T : Type}.
Hypothesis HT0 : inhabited T.

Compatibility results with operators.
With the converse / complementary / conv_compl operators.

Lemma lex_conv :
  ∀ {R : T → T → Prop} {n}, @lex _ (converse R) n = converse (lex R).
Proof.
unfold converse; intros R n; fun_ext2; apply prop_ext; induction n; [easy |].
rewrite !lex_S IHn neq_sym_equiv eq_sym_equiv; easy.
Qed.

Lemma colex_conv :
  ∀ {R : T → T → Prop} {n},
    @colex _ (converse R) n = converse (colex R).
Proof. intros; unfold colex; rewrite lex_conv; easy. Qed.

Lemma symlex_conv :
  ∀ {R : T → T → Prop} {n},
    @symlex _ (converse R) n = converse (symlex R).
Proof. intros; unfold symlex; rewrite lex_conv; easy. Qed.

Lemma revlex_conv :
  ∀ {R : T → T → Prop} {n},
    @revlex _ (converse R) n = converse (revlex R).
Proof. intros; unfold revlex; rewrite colex_conv; easy. Qed.

Lemma lex_compl :
  ∀ {R : T → T → Prop} {n},
    reflexive R ∨ irreflexive R →
    @lex _ (complementary R) n = complementary (lex R).
Proof.
intros R n H; fun_ext2 x y; apply prop_ext;
    induction n; rewrite compl_alt.
+ rewrite !lex_nil compl_refl_equiv; split;
      [apply irrefl_not_refl | destruct H]; easy.
+ rewrite !lex_S IHn not_or_equiv !not_and_equiv !NNPP_equiv; split.
  - intros [H1 | H1]; split; [right | left | left | right]; easy.
  - intros [[H1 | H1] [H2 | H2]]; [| right | left |]; try easy.
    destruct (eq_dec (x ord0) (y ord0)); [right | left]; easy.
Qed.

Lemma colex_compl :
  ∀ {R : T → T → Prop} {n},
    reflexive R ∨ irreflexive R →
    @colex _ (complementary R) n = complementary (colex R).
Proof. intros; unfold colex; rewrite lex_compl; easy. Qed.

Lemma symlex_compl :
  ∀ {R : T → T → Prop} {n},
    reflexive R ∨ irreflexive R →
    @symlex _ (complementary R) n = complementary (symlex R).
Proof. intros; unfold symlex; rewrite lex_compl; easy. Qed.

Lemma revlex_compl :
  ∀ {R : T → T → Prop} {n},
    reflexive R ∨ irreflexive R →
    @revlex _ (complementary R) n = complementary (revlex R).
Proof. intros; unfold revlex; rewrite colex_compl; easy. Qed.

Lemma lex_conv_compl :
  ∀ {R : T → T → Prop} {n},
    reflexive R ∨ irreflexive R →
    @lex _ (conv_compl R) n = conv_compl (lex R).
Proof. intros; rewrite lex_conv lex_compl; easy. Qed.

Lemma colex_conv_compl :
  ∀ {R : T → T → Prop} {n},
    reflexive R ∨ irreflexive R →
    @colex _ (conv_compl R) n = conv_compl (colex R).
Proof. intros; rewrite colex_conv colex_compl; easy. Qed.

Lemma symlex_conv_compl :
  ∀ {R : T → T → Prop} {n},
    reflexive R ∨ irreflexive R →
    @symlex _ (conv_compl R) n = conv_compl (symlex R).
Proof. intros; rewrite symlex_conv symlex_compl; easy. Qed.

Lemma revlex_conv_compl :
  ∀ {R : T → T → Prop} {n},
    reflexive R ∨ irreflexive R →
    @revlex _ (conv_compl R) n = conv_compl (revlex R).
Proof. intros; rewrite revlex_conv revlex_compl; easy. Qed.

With the br_and / br_or operators.

Lemma lex_br_and :
  ∀ {R1 R2 : T → T → Prop} {n},
    @lex _ (br_and R1 R2) n = br_and (lex R1) (lex R2).
Proof.
unfold br_and; intros R1 R2 n; fun_ext2; apply prop_ext; induction n.
rewrite !lex_nil; apply br_and_refl_equiv.
rewrite !lex_S IHn; split.
intros [H | H]; split; [left | left | right | right]; easy.
intros [[H1 | H1] [H2 | H2]]; [left |..| right]; easy.
Qed.

Lemma colex_br_and :
  ∀ {R1 R2 : T → T → Prop} {n},
    @colex _ (br_and R1 R2) n = br_and (colex R1) (colex R2).
Proof. intros; unfold colex; rewrite br_and_conv lex_br_and; easy. Qed.

Lemma symlex_br_and :
  ∀ {R1 R2 : T → T → Prop} {n},
    @symlex _ (br_and R1 R2) n = br_and (symlex R1) (symlex R2).
Proof. intros; unfold symlex; rewrite br_and_conv lex_br_and; easy. Qed.

Lemma revlex_br_and :
  ∀ {R1 R2 : T → T → Prop} {n},
    @revlex _ (br_and R1 R2) n = br_and (revlex R1) (revlex R2).
Proof. intros; unfold revlex; rewrite br_and_conv colex_br_and; easy. Qed.

Since reflexivity of a disjunction is not equivalent to the disjunction of reflexivities, the base case cannot be fullfilled in inductions, and *lex orders do not commute with operator br_or.
"w_neq" stands for "when not equal".
Lemma lex_br_or_w_neq :
  ∀ {R1 R2 : T → T → Prop} {n} {x y : 'T^n},
    x ≠ y → lex (br_or R1 R2) x y ↔ br_or (lex R1) (lex R2) x y.
Proof.
unfold br_or at 2; intros R1 R2 n x y H1; induction n;
    [contradict H1; apply hat0F_unit; easy |].
destruct (nextF_skipF ord0 H1) as [H2 | H2];
    rewrite !lex_S; [| rewrite (IHn _ _ H2)]; split.
intros [[H3 [H4 | H4]] | H3]; [left; left | right; left |]; easy.
intros [[H3 | H3] | [H3 | H3]];
    [left; split; [| left] | | left; split; [| right] |]; easy.
intros [[H3 [H4 | H4]] | [H3 [H4 | H4]]];
    [left; left | right; left | left; right | right; right]; easy.
intros [[H3 | H3] | [H3 | H3]];
    [left; split; [| left] | right; split; [| left] |
     left; split; [| right] | right; split; [| right]]; easy.
Qed.

"w_neq" stands for "when not equal".
Lemma colex_br_or_w_neq :
  ∀ {R1 R2 : T → T → Prop} {n} {x y : 'T^n},
    x ≠ y → colex (br_or R1 R2) x y ↔ br_or (colex R1) (colex R2) x y.
Proof.
intros; unfold colex, reverse;
    rewrite lex_br_or_w_neq; [| apply revF_next]; easy.
Qed.

"w_neq" stands for "when not equal".
Lemma symlex_br_or_w_neq :
  ∀ {R1 R2 : T → T → Prop} {n} {x y : 'T^n},
    x ≠ y → symlex (br_or R1 R2) x y ↔ br_or (symlex R1) (symlex R2) x y.
Proof.
intros; unfold symlex; rewrite -!lex_conv -br_or_conv lex_br_or_w_neq; easy.
Qed.

"w_neq" stands for "when not equal".
Lemma revlex_br_or_w_neq :
  ∀ {R1 R2 : T → T → Prop} {n} {x y : 'T^n},
    x ≠ y → revlex (br_or R1 R2) x y ↔ br_or (revlex R1) (revlex R2) x y.
Proof.
intros; unfold revlex;
    rewrite -!colex_conv -br_or_conv colex_br_or_w_neq; easy.
Qed.

With the br_and_neq / br_or_eq operators.

Lemma lex_eq : ∀ {n}, lex eq = @eq 'T^n.
Proof.
intros n; induction n; fun_ext2 x y; apply prop_ext.
rewrite lex_nil (hat0F_unit x _ y); easy.
rewrite lex_S IHn (extF_skipF_equiv ord0); tauto.
Qed.

Lemma lex_neq : ∀ {n}, lex neq = @neq 'T^n.
Proof.
intros n; induction n; fun_ext2 x y; apply prop_ext.
rewrite lex_nil (hat0F_unit x _ y); split; intros H; contradict H; [| easy].
apply (irrefl_not_refl HT0); easy.
unfold neq; rewrite lex_S IHn; rewrite (nextF_skipF_equiv ord0); tauto.
Qed.

Lemma colex_eq : ∀ {n}, colex eq = @eq 'T^n.
Proof.
intros; unfold colex; rewrite lex_eq; fun_ext2; apply prop_ext, revF_ext.
Qed.

Lemma colex_neq : ∀ {n}, colex neq = @neq 'T^n.
Proof.
intros; unfold colex; rewrite lex_neq; fun_ext2; apply prop_ext, revF_next.
Qed.

Lemma symlex_eq : ∀ {n}, symlex eq = @eq 'T^n.
Proof. intros; unfold symlex; rewrite lex_eq; apply conv_eq. Qed.

Lemma symlex_neq : ∀ {n}, symlex neq = @neq 'T^n.
Proof. intros n; unfold symlex; rewrite lex_neq; apply conv_neq. Qed.

Lemma revlex_eq : ∀ {n}, revlex eq = @eq 'T^n.
Proof. intros; unfold revlex; rewrite colex_eq; apply conv_eq. Qed.

Lemma revlex_neq : ∀ {n}, revlex neq = @neq 'T^n.
Proof. intros n; unfold revlex; rewrite colex_neq; apply conv_neq. Qed.

Lemma lex_br_and_neq :
  ∀ {R : T → T → Prop} {n},
    @lex _ (br_and_neq R) n = br_and_neq (lex R).
Proof. intros; rewrite !br_and_neq_eq lex_br_and lex_neq; easy. Qed.

Lemma colex_br_and_neq :
  ∀ {R : T → T → Prop} {n},
    @colex _ (br_and_neq R) n = br_and_neq (colex R).
Proof. intros; rewrite !br_and_neq_eq colex_br_and colex_neq; easy. Qed.

Lemma symlex_br_and_neq :
  ∀ {R : T → T → Prop} {n},
    @symlex _ (br_and_neq R) n = br_and_neq (symlex R).
Proof.
intros; unfold symlex; rewrite br_and_neq_conv lex_br_and_neq; easy.
Qed.

Lemma revlex_br_and_neq :
  ∀ {R : T → T → Prop} {n},
    @revlex _ (br_and_neq R) n = br_and_neq (revlex R).
Proof.
intros; unfold revlex; rewrite br_and_neq_conv colex_br_and_neq; easy.
Qed.

"w_neq" stands for "when not equal".
Lemma lex_br_or_eq_w_neq :
  ∀ {R : T → T → Prop} {n} {x y : 'T^n},
    x ≠ y → lex (br_or_eq R) x y ↔ br_or_eq (lex R) x y.
Proof. move=>> H; rewrite !br_or_eq_eq (lex_br_or_w_neq H) lex_eq; easy. Qed.

"w_neq" stands for "when not equal".
Lemma colex_br_or_eq_w_neq :
  ∀ {R : T → T → Prop} {n} {x y : 'T^n},
    x ≠ y → colex (br_or_eq R) x y ↔ br_or_eq (colex R) x y.
Proof.
move=>> H; rewrite !br_or_eq_eq (colex_br_or_w_neq H) colex_eq; easy.
Qed.

"w_neq" stands for "when not equal".
Lemma symlex_br_or_eq_w_neq :
  ∀ {R : T → T → Prop} {n} {x y : 'T^n},
    x ≠ y → symlex (br_or_eq R) x y ↔ br_or_eq (symlex R) x y.
Proof.
intros; unfold symlex;
    rewrite -!lex_conv -br_or_eq_conv lex_br_or_eq_w_neq; easy.
Qed.

"w_neq" stands for "when not equal".
Lemma revlex_br_or_eq_w_neq :
  ∀ {R : T → T → Prop} {n} {x y : 'T^n},
    x ≠ y → revlex (br_or_eq R) x y ↔ br_or_eq (revlex R) x y.
Proof.
intros; unfold revlex;
    rewrite -!colex_conv -br_or_eq_conv colex_br_or_eq_w_neq; easy.
Qed.

With the strict / equivalent operators.

Lemma lex_strict :
  ∀ {R : T → T → Prop} {n},
    reflexive R ∨ irreflexive R →
    @lex _ (strict R) n = strict (lex R).
Proof. intros; rewrite !strict_eq lex_br_and lex_conv_compl; easy. Qed.

Lemma colex_strict :
  ∀ {R : T → T → Prop} {n},
    reflexive R ∨ irreflexive R →
    @colex _ (strict R) n = strict (colex R).
Proof. intros; unfold colex; rewrite lex_strict; easy. Qed.

Lemma symlex_strict :
  ∀ {R : T → T → Prop} {n},
    reflexive R ∨ irreflexive R →
    @symlex _ (strict R) n = strict (symlex R).
Proof. intros; unfold symlex; rewrite lex_strict; easy. Qed.

Lemma revlex_strict :
  ∀ {R : T → T → Prop} {n},
    reflexive R ∨ irreflexive R →
    @revlex _ (strict R) n = strict (revlex R).
Proof. intros; unfold revlex; rewrite colex_strict; easy. Qed.

Lemma lex_equivalent :
  ∀ {R : T → T → Prop} {n},
    @lex _ (equivalent R) n = equivalent (lex R).
Proof. intros; rewrite !equivalent_eq lex_br_and lex_conv; easy. Qed.

Lemma colex_equivalent :
  ∀ {R : T → T → Prop} {n},
    @colex _ (equivalent R) n = equivalent (colex R).
Proof. intros; unfold colex; rewrite lex_equivalent; easy. Qed.

Lemma symlex_equivalent :
  ∀ {R : T → T → Prop} {n},
    @symlex _ (equivalent R) n = equivalent (symlex R).
Proof. intros; unfold symlex; rewrite lex_equivalent; easy. Qed.

Lemma revlex_equivalent :
  ∀ {R : T → T → Prop} {n},
    @revlex _ (equivalent R) n = equivalent (revlex R).
Proof. intros; unfold revlex; rewrite colex_equivalent; easy. Qed.

End Lex_orders_Facts1.

Section Lex_orders_Facts2.

Context {T : Type}.
Hypothesis HT0 : inhabited T.

Compatibility results with elementary properties.
With the transitive / negatively_transitive elementary properties.

Lemma rev_trans :
  ∀ {n} {Rn : 'T^n → 'T^n → Prop},
    transitive Rn → transitive (reverse Rn).
Proof. move=>> HRn x y z; apply HRn. Qed.

Lemma lex_trans :
  ∀ {R : T → T → Prop},
    antisymmetric R → transitive R → ∀ {n}, transitive (@lex _ R n).
Proof.
intros R H1 H2 n x y z; induction n; [easy |].
rewrite !lex_S; intros [[H3a H3b] | [H3 H4]] [[H5a H5b] | [H5 H6]].
left; split; [| apply H2 with (y ord0); easy].
destruct (eq_dec (x ord0) (z ord0)) as [H4 |]; [exfalso | easy].
rewrite H4 in H3b; contradict H5a; apply H1; easy.
rewrite -H5; left; easy.
rewrite H3; left; easy.
right; split; [rewrite H3 | apply IHn with (skipF0 y)]; easy.
Qed.

Lemma colex_trans :
  ∀ {R : T → T → Prop},
    antisymmetric R → transitive R → ∀ {n}, transitive (@colex _ R n).
Proof. intros; apply rev_trans, lex_trans; easy. Qed.

Lemma symlex_trans :
  ∀ {R : T → T → Prop},
    antisymmetric R → transitive R → ∀ {n}, transitive (@symlex _ R n).
Proof. intros; apply conv_trans, lex_trans; easy. Qed.

Lemma revlex_trans :
  ∀ {R : T → T → Prop},
    antisymmetric R → transitive R → ∀ {n}, transitive (@revlex _ R n).
Proof. intros; apply conv_trans, colex_trans; easy. Qed.

Lemma rev_neg_trans :
  ∀ {n} {Rn : 'T^n → 'T^n → Prop},
    negatively_transitive Rn → negatively_transitive (reverse Rn).
Proof. move=>> HRn x y z; apply HRn. Qed.

Lemma lex_neg_trans :
  ∀ {R : T → T → Prop},
    reflexive R ∨ irreflexive R → connected R →
    negatively_transitive R → ∀ {n}, negatively_transitive (@lex _ R n).
Proof.
intros R; rewrite -compl_antisym_equiv.
move⇒ H1 H2 /neg_trans_equiv /(lex_trans H2) H3 n.
rewrite neg_trans_equiv -(lex_compl HT0 H1); easy.
Qed.

Lemma colex_neg_trans :
  ∀ {R : T → T → Prop},
    reflexive R ∨ irreflexive R → connected R →
    negatively_transitive R →
    ∀ {n}, negatively_transitive (@colex _ R n).
Proof. intros; apply rev_neg_trans, lex_neg_trans; easy. Qed.

Lemma symlex_neg_trans :
  ∀ {R : T → T → Prop},
    reflexive R ∨ irreflexive R → connected R →
    negatively_transitive R →
    ∀ {n}, negatively_transitive (@symlex _ R n).
Proof. intros; apply conv_neg_trans, lex_neg_trans; easy. Qed.

Lemma revlex_neg_trans :
  ∀ {R : T → T → Prop},
    reflexive R ∨ irreflexive R → connected R →
    negatively_transitive R →
    ∀ {n}, negatively_transitive (@revlex _ R n).
Proof. intros; apply conv_neg_trans, colex_neg_trans; easy. Qed.

With the reflexive / irreflexive elementary properties.

Lemma rev_refl :
  ∀ {n} {Rn : 'T^n → 'T^n → Prop},
    reflexive Rn → reflexive (reverse Rn).
Proof. move=>> HRn x; apply HRn. Qed.

Lemma lex_refl :
  ∀ {R : T → T → Prop},
    reflexive R → ∀ {n}, reflexive (@lex _ R n).
Proof.
intros R H n; induction n; intros x; [easy |].
rewrite lex_S; right; split; [easy | apply IHn].
Qed.

Lemma colex_refl :
  ∀ {R : T → T → Prop},
    reflexive R → ∀ {n}, reflexive (@colex _ R n).
Proof. intros; apply rev_refl, lex_refl; easy. Qed.

Lemma symlex_refl :
  ∀ {R : T → T → Prop},
    reflexive R → ∀ {n}, reflexive (@symlex _ R n).
Proof. intros; apply conv_refl, lex_refl; easy. Qed.

Lemma revlex_refl :
  ∀ {R : T → T → Prop},
    reflexive R → ∀ {n}, reflexive (@revlex _ R n).
Proof. intros; apply conv_refl, colex_refl; easy. Qed.

Lemma rev_irrefl :
  ∀ {n} {Rn : 'T^n → 'T^n → Prop},
    irreflexive Rn → irreflexive (reverse Rn).
Proof. move=>> HRn x; apply HRn. Qed.

Lemma lex_irrefl :
  ∀ {R : T → T → Prop},
    irreflexive R → ∀ {n}, irreflexive (@lex _ R n).
Proof.
intros R H n; induction n; intros y.
rewrite lex_nil; apply irrefl_not_refl; easy.
rewrite lex_S not_or_equiv !not_and_equiv NNPP_equiv; split;
    [left; easy | right; apply IHn].
Qed.

Lemma colex_irrefl :
  ∀ {R : T → T → Prop},
    irreflexive R → ∀ {n}, irreflexive (@colex _ R n).
Proof. intros; apply rev_irrefl, lex_irrefl; easy. Qed.

Lemma symlex_irrefl :
  ∀ {R : T → T → Prop},
    irreflexive R → ∀ {n}, irreflexive (@symlex _ R n).
Proof. intros; apply conv_irrefl, lex_irrefl; easy. Qed.

Lemma revlex_irrefl :
  ∀ {R : T → T → Prop},
    irreflexive R → ∀ {n}, irreflexive (@revlex _ R n).
Proof. intros; apply conv_irrefl, colex_irrefl; easy. Qed.

Lemma lex_insertF :
  ∀ {R : T → T → Prop} {n} i a b (x y : 'T^n),
    irreflexive R → br_or_eq R a b →
    lex R x y → lex R (insertF i a x) (insertF i b y).
Proof.
clear HT0.
intros R n i a b x y HR Hab H; induction n as [| n IHn];
    [contradict H; rewrite lex_nil; apply irrefl_not_refl; easy |].
destruct (ord_eq_dec i ord0) as [-> | Hi].
case Hab;intros Hab';[subst|idtac].
rewrite lex_irrefl_S// !insertF_correct_l//;
    right; split; [| rewrite !skipF_insertF]; easy.
rewrite lex_irrefl_S// !insertF_correct_l//; now left.
apply not_eq_sym in Hi; rewrite lex_irrefl_S// !insertF_correct_r insert_ord_0.
rewrite lex_irrefl_S// in H; destruct H as [H | [H1 H2]]; [left; easy |].
right; split; [easy |].
rewrite !skipF_insertF_comm; apply IHn; rewrite insert_ord_0; easy.
Qed.

Lemma colex_insertF :
  ∀ {R : T → T → Prop} {n} i a b (x y : 'T^n),
    irreflexive R → br_or_eq R a b →
    colex R x y → colex R (insertF i a x) (insertF i b y).
Proof.
move=>>; unfold colex, reverse; rewrite !revF_insertF; apply lex_insertF.
Qed.

Lemma symlex_insertF :
  ∀ {R : T → T → Prop} {n} i a b (x y : 'T^n),
    irreflexive R → br_or_eq R b a →
    symlex R x y → symlex R (insertF i a x) (insertF i b y).
Proof. move=>>; apply lex_insertF. Qed.

Lemma revlex_insertF :
  ∀ {R : T → T → Prop} {n} i a b (x y : 'T^n),
    irreflexive R → br_or_eq R b a →
    revlex R x y → revlex R (insertF i a x) (insertF i b y).
Proof. move=>>; apply colex_insertF. Qed.

With the symmetric / antisymmetric / asymmetric elementary properties.

Lemma rev_sym :
  ∀ {n} {Rn : 'T^n → 'T^n → Prop},
    symmetric Rn → symmetric (reverse Rn).
Proof. move=>> HRn x y; apply HRn. Qed.

Lemma lex_sym :
  ∀ {R : T → T → Prop},
    symmetric R → ∀ {n}, symmetric (@lex _ R n).
Proof.
intros R H1 n x y; induction n; [rewrite (hat0F_unit x _ y); easy |].
rewrite !lex_S; intros [H2 | H2].
left; split; [apply not_eq_sym | apply H1]; easy.
right; split; [| apply IHn]; easy.
Qed.

Lemma colex_sym :
  ∀ {R : T → T → Prop},
    symmetric R → ∀ {n}, symmetric (@colex _ R n).
Proof. intros; apply rev_sym, lex_sym; easy. Qed.

Lemma symlex_sym :
  ∀ {R : T → T → Prop},
    symmetric R → ∀ {n}, symmetric (@symlex _ R n).
Proof. intros; apply conv_sym, lex_sym; easy. Qed.

Lemma revlex_sym :
  ∀ {R : T → T → Prop},
    symmetric R → ∀ {n}, symmetric (@revlex _ R n).
Proof. intros; apply conv_sym, colex_sym; easy. Qed.

Lemma rev_antisym :
  ∀ {n} {Rn : 'T^n → 'T^n → Prop},
    antisymmetric Rn → antisymmetric (reverse Rn).
Proof. move=>> HRn x y H1 H2; apply revF_ext, HRn; easy. Qed.

Lemma lex_antisym :
  ∀ {R : T → T → Prop},
    antisymmetric R → ∀ {n}, antisymmetric (@lex _ R n).
Proof.
intros R H1 n x y; induction n; [rewrite (hat0F_unit x _ y); easy |].
rewrite !lex_S; intros [[H2a H2b] | [H2 H3]] [[H4a H4b] | [H4 H5]].
contradict H2a; apply H1; easy.
contradict H2a; easy.
contradict H4a; easy.
apply extF_skipF with ord0; [| apply IHn]; easy.
Qed.

Lemma colex_antisym :
  ∀ {R : T → T → Prop},
    antisymmetric R → ∀ {n}, antisymmetric (@colex _ R n).
Proof. intros; apply rev_antisym, lex_antisym; easy. Qed.

Lemma symlex_antisym :
  ∀ {R : T → T → Prop},
    antisymmetric R → ∀ {n}, antisymmetric (@symlex _ R n).
Proof. intros; apply conv_antisym, lex_antisym; easy. Qed.

Lemma revlex_antisym :
  ∀ {R : T → T → Prop},
    antisymmetric R → ∀ {n}, antisymmetric (@revlex _ R n).
Proof. intros; apply conv_antisym, colex_antisym; easy. Qed.

Lemma rev_asym :
  ∀ {n} {Rn : 'T^n → 'T^n → Prop},
    asymmetric Rn → asymmetric (reverse Rn).
Proof. move=>> HRn x y; apply HRn. Qed.

Lemma lex_asym :
  ∀ {R : T → T → Prop},
    asymmetric R → ∀ {n}, asymmetric (@lex _ R n).
Proof.
intros R H n x y; induction n.
rewrite (hat0F_unit x _ y)//; intros _; apply lex_irrefl, asym_irrefl; easy.
rewrite !lex_S; intros [[H3a H3b] | [H3a H3b]];
    rewrite not_or_equiv !not_and_equiv NNPP_equiv; split.
right; apply H; easy.
left; apply neq_sym_equiv; easy.
left; easy.
right; apply IHn; easy.
Qed.

Lemma colex_asym :
  ∀ {R : T → T → Prop},
    asymmetric R → ∀ {n}, asymmetric (@colex _ R n).
Proof. intros; apply rev_asym, lex_asym; easy. Qed.

Lemma symlex_asym :
  ∀ {R : T → T → Prop},
    asymmetric R → ∀ {n}, asymmetric (@symlex _ R n).
Proof. intros; apply conv_asym, lex_asym; easy. Qed.

Lemma revlex_asym :
  ∀ {R : T → T → Prop},
    asymmetric R → ∀ {n}, asymmetric (@revlex _ R n).
Proof. intros; apply conv_asym, colex_asym; easy. Qed.

With the connected / strongly_connected / trichotomous elementary properties.

Lemma rev_conn :
  ∀ {n} {Rn : 'T^n → 'T^n → Prop},
    connected Rn → connected (reverse Rn).
Proof. move=>> HRn x y H; apply HRn, revF_next; easy. Qed.

Lemma lex_conn :
  ∀ {R : T → T → Prop},
    connected R → ∀ {n}, connected (@lex _ R n).
Proof.
intros R H n x y H1; induction n.
rewrite (hat0F_unit x _ y) in H1; easy.
rewrite !lex_S; destruct (eq_dec (x ord0) (y ord0)) as [H2 | H2].
assert (H3 : skipF0 x ≠ skipF0 y).
  contradict H1; apply extF_skipF with ord0; easy.
destruct (IHn _ _ H3) as [H4 | H4]; [left | right]; right; easy.
destruct (H _ _ H2) as [H3 | H3];
    [left | right; rewrite neq_sym_equiv]; left; easy.
Qed.

Lemma colex_conn :
  ∀ {R : T → T → Prop},
    connected R → ∀ {n}, connected (@colex _ R n).
Proof. intros; apply rev_conn, lex_conn; easy. Qed.

Lemma symlex_conn :
  ∀ {R : T → T → Prop},
    connected R → ∀ {n}, connected (@symlex _ R n).
Proof. intros; apply conv_conn, lex_conn; easy. Qed.

Lemma revlex_conn :
  ∀ {R : T → T → Prop},
    connected R → ∀ {n}, connected (@revlex _ R n).
Proof. intros; apply conv_conn, colex_conn; easy. Qed.

Lemma rev_str_conn :
  ∀ {n} {Rn : 'T^n → 'T^n → Prop},
    strongly_connected Rn → strongly_connected (reverse Rn).
Proof. move=>> HRn x y; apply HRn. Qed.

Lemma lex_str_conn :
  ∀ {R : T → T → Prop},
    strongly_connected R → ∀ {n}, strongly_connected (@lex _ R n).
Proof.
intros R H1 n x y; induction n.
rewrite lex_nil; left; apply str_conn_refl; easy.
rewrite !lex_S; destruct (eq_dec (x ord0) (y ord0)) as [H2 | H2].
destruct (IHn (skipF0 x) (skipF0 y)) as [H3 | H3];
    [left | right]; right; split; easy.
destruct (H1 (x ord0) (y ord0)) as [H3 | H3];
    [left | right; rewrite neq_sym_equiv]; left; split; try easy.
Qed.

Lemma colex_str_conn :
  ∀ {R : T → T → Prop},
    strongly_connected R → ∀ {n}, strongly_connected (@colex _ R n).
Proof. intros; apply rev_str_conn, lex_str_conn; easy. Qed.

Lemma symlex_str_conn :
  ∀ {R : T → T → Prop},
    strongly_connected R → ∀ {n}, strongly_connected (@symlex _ R n).
Proof. intros; apply conv_str_conn, lex_str_conn; easy. Qed.

Lemma revlex_str_conn :
  ∀ {R : T → T → Prop},
    strongly_connected R → ∀ {n}, strongly_connected (@revlex _ R n).
Proof. intros; apply conv_str_conn, colex_str_conn; easy. Qed.

Lemma rev_tricho :
  ∀ {n} {Rn : 'T^n → 'T^n → Prop},
    trichotomous Rn → trichotomous (reverse Rn).
Proof.
intros n Rn; rewrite !tricho_equiv_asym_conn; apply modus_ponens_and;
    [apply rev_asym | apply rev_conn].
Qed.

Lemma lex_tricho :
  ∀ {R : T → T → Prop},
    trichotomous R → ∀ {n}, trichotomous (@lex _ R n).
Proof.
intros R H n; move: H.
rewrite !tricho_equiv_asym_conn; apply modus_ponens_and; intros H;
    [apply lex_asym | apply lex_conn]; easy.
Qed.

Lemma colex_tricho :
  ∀ {R : T → T → Prop},
    trichotomous R → ∀ {n}, trichotomous (@colex _ R n).
Proof. intros; apply rev_tricho, lex_tricho; easy. Qed.

Lemma symlex_tricho :
  ∀ {R : T → T → Prop},
    trichotomous R → ∀ {n}, trichotomous (@symlex _ R n).
Proof. intros; apply conv_tricho, lex_tricho; easy. Qed.

Lemma revlex_tricho :
  ∀ {R : T → T → Prop},
    trichotomous R → ∀ {n}, trichotomous (@revlex _ R n).
Proof. intros; apply conv_tricho, colex_tricho; easy. Qed.

Lemma lex_br_and_neq_correct :
  ∀ {R : T → T → Prop} {n},
    @lex _ (br_and_neq R) n = br_and_neq (lex (br_or_eq R)).
Proof.
intros R n; fun_ext2 x y; apply prop_ext; induction n as [| n IHn].
rewrite lex_nil (hat0F_unit x _ y)//; split; intros H; contradict H.
apply irrefl_not_refl; [easy | apply br_and_neq_irrefl].
apply br_and_neq_irrefl.
destruct (eq_dec x y) as [-> | H].
split; intros H1; contradict H1; [apply lex_irrefl |]; apply br_and_neq_irrefl.
rewrite lex_br_and_neq// !br_and_neq_w_neq// lex_br_or_eq_w_neq//.
unfold br_or_eq; tauto.
Qed.

"w" stands for "when".
Lemma lex_br_and_neq_correct_w_refl :
  ∀ {R : T → T → Prop} {n},
    reflexive R → @lex _ (br_and_neq R) n = br_and_neq (lex R).
Proof. intros; rewrite lex_br_and_neq_correct (proj1 br_or_eq_id_alt)//. Qed.

"w" stands for "when".
Lemma lex_br_and_neq_correct_w_irrefl :
  ∀ {R : T → T → Prop} {n},
    irreflexive R → @lex _ R n = br_and_neq (lex (br_or_eq R)).
Proof.
intros; rewrite -lex_br_and_neq_correct (proj1 br_and_neq_id_alt); easy.
Qed.

Lemma lex_br_or_eq_correct :
  ∀ {R : T → T → Prop} {n},
    @lex _ (br_or_eq R) n = br_or_eq (lex (br_and_neq R)).
Proof.
intros R n; fun_ext2 x y; apply prop_ext; induction n as [| n IHn].
rewrite lex_nil (hat0F_unit x _ y)//; split; intros _; apply br_or_eq_refl.
destruct (eq_dec x y) as [-> | H].
split; intros _; [| apply lex_refl]; apply br_or_eq_refl.
rewrite lex_br_or_eq_w_neq// !br_or_eq_w_neq// lex_br_and_neq//.
unfold br_and_neq; tauto.
Qed.

"w" stands for "when".
Lemma lex_br_or_eq_correct_w_refl :
  ∀ {R : T → T → Prop} {n},
    reflexive R → @lex _ R n = br_or_eq (lex (br_and_neq R)).
Proof. intros; rewrite -lex_br_or_eq_correct (proj1 br_or_eq_id_alt)//. Qed.

"w" stands for "when".
Lemma lex_br_or_eq_correct_w_irrefl :
  ∀ {R : T → T → Prop} {n},
    irreflexive R → @lex _ (br_or_eq R) n = br_or_eq (lex R).
Proof. intros; rewrite lex_br_or_eq_correct (proj1 br_and_neq_id_alt)//. Qed.

Lemma colex_br_and_neq_correct :
  ∀ {R : T → T → Prop} {n},
    @colex _ (br_and_neq R) n = br_and_neq (colex (br_or_eq R)).
Proof. intros; rewrite -rev_br_and_neq -lex_br_and_neq_correct; easy. Qed.

"w" stands for "when".
Lemma colex_br_and_neq_correct_w_refl :
  ∀ {R : T → T → Prop} {n},
    reflexive R → @colex _ (br_and_neq R) n = br_and_neq (colex R).
Proof. intros; rewrite colex_br_and_neq_correct (proj1 br_or_eq_id_alt)//. Qed.

"w" stands for "when".
Lemma colex_br_and_neq_correct_w_irrefl :
  ∀ {R : T → T → Prop} {n},
    irreflexive R → @colex _ R n = br_and_neq (colex (br_or_eq R)).
Proof.
intros; rewrite -colex_br_and_neq_correct (proj1 br_and_neq_id_alt); easy.
Qed.

Lemma colex_br_or_eq_correct :
  ∀ {R : T → T → Prop} {n},
    @colex _ (br_or_eq R) n = br_or_eq (colex (br_and_neq R)).
Proof. intros; rewrite -rev_br_or_eq -lex_br_or_eq_correct; easy. Qed.

"w" stands for "when".
Lemma colex_br_or_eq_correct_w_refl :
  ∀ {R : T → T → Prop} {n},
    reflexive R → @colex _ R n = br_or_eq (colex (br_and_neq R)).
Proof. intros; rewrite -colex_br_or_eq_correct (proj1 br_or_eq_id_alt)//. Qed.

"w" stands for "when".
Lemma colex_br_or_eq_correct_w_irrefl :
  ∀ {R : T → T → Prop} {n},
    irreflexive R → @colex _ (br_or_eq R) n = br_or_eq (colex R).
Proof. intros; rewrite colex_br_or_eq_correct (proj1 br_and_neq_id_alt)//. Qed.

Lemma symlex_br_and_neq_correct :
  ∀ {R : T → T → Prop} {n},
    @symlex _ (br_and_neq R) n = br_and_neq (symlex (br_or_eq R)).
Proof.
intros; fun_ext2; unfold symlex;
    rewrite lex_br_and_neq_correct br_and_neq_conv; easy.
Qed.

"w" stands for "when".
Lemma symlex_br_and_neq_correct_w_refl :
  ∀ {R : T → T → Prop} {n},
    reflexive R → @symlex _ (br_and_neq R) n = br_and_neq (symlex R).
Proof.
intros; rewrite symlex_br_and_neq_correct (proj1 br_or_eq_id_alt); easy.
Qed.

"w" stands for "when".
Lemma symlex_br_and_neq_correct_w_irrefl :
  ∀ {R : T → T → Prop} {n},
    irreflexive R → @symlex _ R n = br_and_neq (symlex (br_or_eq R)).
Proof.
intros; rewrite -symlex_br_and_neq_correct (proj1 br_and_neq_id_alt); easy.
Qed.

Lemma symlex_br_or_eq_correct :
  ∀ {R : T → T → Prop} {n},
    @symlex _ (br_or_eq R) n = br_or_eq (symlex (br_and_neq R)).
Proof.
intros; fun_ext2; unfold symlex;
    rewrite lex_br_or_eq_correct br_or_eq_conv; easy.
Qed.

"w" stands for "when".
Lemma symlex_br_or_eq_correct_w_refl :
  ∀ {R : T → T → Prop} {n},
    reflexive R → @symlex _ R n = br_or_eq (symlex (br_and_neq R)).
Proof. intros; rewrite -symlex_br_or_eq_correct (proj1 br_or_eq_id_alt)//. Qed.

"w" stands for "when".
Lemma symlex_br_or_eq_correct_w_irrefl :
  ∀ {R : T → T → Prop} {n},
    irreflexive R → @symlex _ (br_or_eq R) n = br_or_eq (symlex R).
Proof.
intros; rewrite symlex_br_or_eq_correct (proj1 br_and_neq_id_alt); easy.
Qed.

Lemma revlex_br_and_neq_correct :
  ∀ {R : T → T → Prop} {n},
    @revlex _ (br_and_neq R) n = br_and_neq (revlex (br_or_eq R)).
Proof.
intros; fun_ext2; unfold revlex;
    rewrite colex_br_and_neq_correct br_and_neq_conv; easy.
Qed.

"w" stands for "when".
Lemma revlex_br_and_neq_correct_w_refl :
  ∀ {R : T → T → Prop} {n},
    reflexive R → @revlex _ (br_and_neq R) n = br_and_neq (revlex R).
Proof. intros; rewrite revlex_br_and_neq_correct (proj1 br_or_eq_id_alt)//. Qed.

"w" stands for "when".
Lemma revlex_br_and_neq_correct_w_irrefl :
  ∀ {R : T → T → Prop} {n},
    irreflexive R → @revlex _ R n = br_and_neq (revlex (br_or_eq R)).
Proof.
intros; rewrite -revlex_br_and_neq_correct (proj1 br_and_neq_id_alt); easy.
Qed.

Lemma revlex_br_or_eq_correct :
  ∀ {R : T → T → Prop} {n},
    @revlex _ (br_or_eq R) n = br_or_eq (revlex (br_and_neq R)).
Proof.
intros; fun_ext2; unfold revlex;
    rewrite colex_br_or_eq_correct br_or_eq_conv; easy.
Qed.

"w" stands for "when".
Lemma revlex_br_or_eq_correct_w_refl :
  ∀ {R : T → T → Prop} {n},
    reflexive R → @revlex _ R n = br_or_eq (revlex (br_and_neq R)).
Proof. intros; rewrite -revlex_br_or_eq_correct (proj1 br_or_eq_id_alt)//. Qed.

"w" stands for "when".
Lemma revlex_br_or_eq_correct_w_irrefl :
  ∀ {R : T → T → Prop} {n},
    irreflexive R → @revlex _ (br_or_eq R) n = br_or_eq (revlex R).
Proof.
intros; rewrite revlex_br_or_eq_correct (proj1 br_and_neq_id_alt); easy.
Qed.

End Lex_orders_Facts2.

Section Lex_orders_Facts3.

Context {T : Type}.
Hypothesis HT0 : inhabited T.

Compatibility results with compound properties.
Compatibility of *lex operators with transitive needs antisymmetric for the argument binary relation. Thus, there is no compatibility result with partial_equivalence_relation, equivalence_relation, preorder, and total_preorder since they do not involve antisymmetry.
With the preorder / total_preorder compound properties.

Lemma rev_pro :
  ∀ {n} {Rn : 'T^n → 'T^n → Prop}, preorder Rn → preorder (reverse Rn).
Proof. intros n Rn H; split; [apply rev_trans | apply rev_refl]; apply H. Qed.

Lemma rev_tpro :
  ∀ {n} {Rn : 'T^n → 'T^n → Prop},
    total_preorder Rn → total_preorder (reverse Rn).
Proof.
move=>>; rewrite !tpro_equiv_pro; apply modus_ponens_and;
    [apply rev_pro | apply rev_conn].
Qed.

Compatibility of *lex operators with transitive needs antisymmetric for the argument binary relation. Thus, a compatibility result with preorder or total_preorder, which involves transitive, would also need antisymmetric, and preorder and total_preorder would become partial_order and total_order.
With the partial_order / total_order compound properties.

Lemma rev_po :
  ∀ {n} {Rn : 'T^n → 'T^n → Prop},
    partial_order Rn → partial_order (reverse Rn).
Proof.
intros n Rn H; split; [| split];
    [apply rev_trans | apply rev_refl | apply rev_antisym]; apply H.
Qed.

Lemma lex_po :
  ∀ {R : T → T → Prop},
    partial_order R → ∀ {n}, partial_order (@lex _ R n).
Proof.
intros R H n; split; [| split];
    [apply lex_trans | apply lex_refl | apply lex_antisym]; apply H.
Qed.

Lemma colex_po :
  ∀ {R : T → T → Prop},
    partial_order R → ∀ {n}, partial_order (@colex _ R n).
Proof. intros; apply rev_po, lex_po; easy. Qed.

Lemma symlex_po :
  ∀ {R : T → T → Prop},
    partial_order R → ∀ {n}, partial_order (@symlex _ R n).
Proof. intros; apply conv_po, lex_po; easy. Qed.

Lemma revlex_po :
  ∀ {R : T → T → Prop},
    partial_order R → ∀ {n}, partial_order (@revlex _ R n).
Proof. intros; apply conv_po, colex_po; easy. Qed.

Lemma rev_to :
  ∀ {n} {Rn : 'T^n → 'T^n → Prop},
    total_order Rn → total_order (reverse Rn).
Proof.
move=>>; rewrite to_equiv_po to_equiv_po; intro; split;
    [apply rev_po | apply rev_conn]; easy.
Qed.

Lemma lex_to :
  ∀ {R : T → T → Prop},
    total_order R → ∀ {n}, total_order (@lex _ R n).
Proof.
intros R H n; move: H.
rewrite to_equiv_po to_equiv_po; intros H; split;
    [apply lex_po | apply lex_conn]; easy.
Qed.

Lemma colex_to :
  ∀ {R : T → T → Prop},
    total_order R → ∀ {n}, total_order (@colex _ R n).
Proof. intros; apply rev_to, lex_to; easy. Qed.

Lemma symlex_to :
  ∀ {R : T → T → Prop},
    total_order R → ∀ {n}, total_order (@symlex _ R n).
Proof. intros; apply conv_to, lex_to; easy. Qed.

Lemma revlex_to :
  ∀ {R : T → T → Prop},
    total_order R → ∀ {n}, total_order (@revlex _ R n).
Proof. intros; apply conv_to, colex_to; easy. Qed.

With the strict_partial_order / strict_total_order compound properties.

Lemma rev_spo :
  ∀ {n} {Rn : 'T^n → 'T^n → Prop},
    strict_partial_order Rn → strict_partial_order (reverse Rn).
Proof.
intros n Rn H; apply spo_equiv_no_asym; split;
    [apply rev_trans | apply rev_irrefl]; apply H.
Qed.

Lemma lex_spo :
  ∀ {R : T → T → Prop},
    strict_partial_order R → ∀ {n}, strict_partial_order (@lex _ R n).
Proof.
intros R H n; apply spo_equiv_no_asym; split.
apply lex_trans; [apply asym_antisym |]; apply H.
apply lex_irrefl; [easy | apply H].
Qed.

Lemma colex_spo :
  ∀ {R : T → T → Prop},
    strict_partial_order R → ∀ {n}, strict_partial_order (@colex _ R n).
Proof. intros; apply rev_spo, lex_spo; easy. Qed.

Lemma symlex_spo :
  ∀ {R : T → T → Prop},
    strict_partial_order R → ∀ n, strict_partial_order (@symlex _ R n).
Proof. intros; apply conv_spo, lex_spo; easy. Qed.

Lemma revlex_spo :
  ∀ {R : T → T → Prop},
    strict_partial_order R → ∀ n, strict_partial_order (@revlex _ R n).
Proof. intros; apply conv_spo, colex_spo; easy. Qed.

Lemma rev_sto :
  ∀ {n} {Rn : 'T^n → 'T^n → Prop},
    strict_total_order Rn → strict_total_order (reverse Rn).
Proof.
move=>>; rewrite !sto_equiv_spo; apply modus_ponens_and;
    [apply rev_spo | apply rev_conn].
Qed.

Lemma lex_sto :
  ∀ {R : T → T → Prop},
    strict_total_order R → ∀ {n}, strict_total_order (@lex _ R n).
Proof.
intros R H n; move: H; rewrite !sto_equiv_spo; intros H; split;
    [apply lex_spo; [easy.. | apply H] | apply lex_conn; easy].
Qed.

Lemma colex_sto :
  ∀ {R : T → T → Prop},
    strict_total_order R → ∀ {n}, strict_total_order (@colex _ R n).
Proof. intros; apply rev_sto, lex_sto; easy. Qed.

Lemma symlex_sto :
  ∀ {R : T → T → Prop},
    strict_total_order R → ∀ {n}, strict_total_order (@symlex _ R n).
Proof. intros; apply conv_sto, lex_sto; easy. Qed.

Lemma revlex_sto :
  ∀ {R : T → T → Prop},
    strict_total_order R → ∀ {n}, strict_total_order (@revlex _ R n).
Proof. intros; apply conv_sto, colex_sto; easy. Qed.

With the strict_partial_order / strict_total_order compound properties.

Lemma rev_swo :
  ∀ {n} {Rn : 'T^n → 'T^n → Prop},
    strict_weak_order Rn → strict_weak_order (reverse Rn).
Proof.
move=>>; rewrite !swo_equiv; apply modus_ponens_and;
    [apply rev_neg_trans | apply rev_asym].
Qed.

Compatibility of *lex operators with negatively_transitive needs connected for the argument binary relation. Thus, a compatibility result with strict_weak_order, which involves negatively_transitive, would also need connected, and strict_weak_order would become strict_total_order.

End Lex_orders_Facts3.

Notation lex_lt := (lex lt).
Notation colex_lt := (colex lt).
Notation symlex_lt := (symlex lt).
Notation revlex_lt := (revlex lt).

Section Lex_orders_nat_Facts.

Let HN := inhabits 0.
Let Hlt := lt_irrefl.

Lemma lex_lt_nil : ∀ (x y : 'nat^0), ¬ lex_lt x y.
Proof. intros; rewrite lex_nil; apply irrefl_not_refl; easy. Qed.

Lemma colex_lt_nil : ∀ (x y : 'nat^0), ¬ colex_lt x y.
Proof. intros; rewrite colex_nil; apply irrefl_not_refl; easy. Qed.

Lemma symlex_lt_nil : ∀ (x y : 'nat^0), ¬ symlex_lt x y.
Proof. intros; rewrite symlex_nil; apply irrefl_not_refl; easy. Qed.

Lemma revlex_lt_nil : ∀ (x y : 'nat^0), ¬ revlex_lt x y.
Proof. intros; rewrite revlex_nil; apply irrefl_not_refl; easy. Qed.

Lemma lex_lt_sto :
  ∀ {n}, strict_total_order (lex_lt : 'nat^n → 'nat^n → Prop).
Proof. intro; apply (lex_sto HN), lt_sto. Qed.

Lemma colex_lt_sto :
  ∀ {n}, strict_total_order (colex_lt : 'nat^n → 'nat^n → Prop).
Proof. intro; apply (colex_sto HN), lt_sto. Qed.

Lemma symlex_lt_sto :
  ∀ {n}, strict_total_order (symlex_lt : 'nat^n → 'nat^n → Prop).
Proof. intro; apply (symlex_sto HN), lt_sto. Qed.

Lemma revlex_lt_sto :
  ∀ {n}, strict_total_order (revlex_lt : 'nat^n → 'nat^n → Prop).
Proof. intro; apply (revlex_sto HN), lt_sto. Qed.

Lemma lex_le_lt : ∀ {n}, @lex _ le n = br_or_eq (lex lt).
Proof. intros; rewrite -lex_br_or_eq_correct_w_irrefl// br_or_eq_lt//. Qed.

Lemma lex_lt_le : ∀ {n}, @lex _ lt n = br_and_neq (lex le).
Proof. intros; rewrite -lex_br_and_neq// br_and_neq_le//. Qed.

End Lex_orders_nat_Facts.