Library mathcomp.ssreflect.order

(* (c) Copyright 2006-2019 Microsoft Corporation and Inria.                  
 Distributed under the terms of CeCILL-B.                                  *)

From mathcomp Require Import ssreflect ssrfun ssrbool eqtype ssrnat choice seq.
From mathcomp Require Import path fintype tuple bigop finset div prime finfun.
From mathcomp Require Import finset.

This files defines types equipped with order relations.
Use one of the following modules implementing different theories: Order.LTheory: partially ordered types and lattices excluding complement and totality related theorems. Order.CTheory: complemented lattices including Order.LTheory. Order.TTheory: totally ordered types including Order.LTheory. Order.Theory: ordered types including all of the above theory modules
To access the definitions, notations, and the theory from, say, "Order.Xyz", insert "Import Order.Xyz." at the top of your scripts. Notations are accessible by opening the scope "order_scope" bound to the delimiting key "O".
We provide the following structures of ordered types porderType d == the type of partially ordered types latticeType d == the type of non-distributive lattices bLatticeType d == latticeType with a bottom element tbLatticeType d == latticeType with both a top and a bottom distrLatticeType d == the type of distributive lattices bDistrLatticeType d == distrLatticeType with a bottom element tbDistrLatticeType d == distrLatticeType with both a top and a bottom cbDistrLatticeType d == the type of sectionally complemented distributive lattices (lattices with bottom and a difference operation) ctbDistrLatticeType d == the type of complemented distributive lattices (lattices with top, bottom, difference, and complement) orderType d == the type of totally ordered types finPOrderType d == the type of partially ordered finite types finLatticeType d == the type of nonempty finite non-distributive lattices finDistrLatticeType d == the type of nonempty finite distributive lattices finCDistrLatticeType d == the type of nonempty finite complemented distributive lattices finOrderType d == the type of nonempty totally ordered finite types
Each generic partial order and lattice operations symbols also has a first argument which is the display, the second which is the minimal structure they operate on and then the operands. Here is the exhaustive list of all such symbols for partial orders and lattices together with their default display (as displayed by Check). We document their meaning in the paragraph after the next.
For porderType T @Order.le disp T == <=%O (in fun_scope) @Order.lt disp T == <%O (in fun_scope) @Order.comparable disp T == >=<%O (in fun_scope) @Order.ge disp T == >=%O (in fun_scope) @Order.gt disp T == >%O (in fun_scope) @Order.leif disp T == <?=%O (in fun_scope) @Order.lteif disp T == <?<=%O (in fun_scope) For latticeType T @Order.meet disp T x y == x `&` y (in order_scope) @Order.join disp T x y == x `|` y (in order_scope) For bLatticeType T @Order.bottom disp T == 0 (in order_scope) For tbLatticeType T @Order.top disp T == 1 (in order_scope) For cbDistrLatticeType T @Order.sub disp T x y == x `|` y (in order_scope) For ctbDistrLatticeType T @Order.compl disp T x == ~` x (in order_scope)
This first argument named either d, disp or display, of type unit, configures the printing of notations. Instantiating d with tt or an unknown key will lead to a default display for notations, i.e. we have: For x, y of type T, where T is canonically a porderType d: x <= y <-> x is less than or equal to y. x < y <-> x is less than y (:= (y != x) && (x <= y)). min x y <-> if x < y then x else y max x y <-> if x < y then y else x x >= y <-> x is greater than or equal to y (:= y <= x). x > y <-> x is greater than y (:= y < x). x <= y ?= iff C <-> x is less than y, or equal iff C is true. x < y ?<= if C <-> x is smaller than y, and strictly if C is false. x >=< y <-> x and y are comparable (:= (x <= y) || (y <= x)). x >< y <-> x and y are incomparable (:= ~~ x >=< y). f \min g <-> the function x |-> Order.min (f x) (g x); f \min g simplifies on application. f \max g <-> the function x |-> Order.max (f x) (g x); f \max g simplifies on application. For x, y of type T, where T is canonically a latticeType d: x `&` y == the meet of x and y. x `|` y == the join of x and y. In a type T, where T is canonically a bLatticeType d: 0 == the bottom element. \join<range> e == iterated join of a lattice with a bottom. In a type T, where T is canonically a tbLatticeType d: 1 == the top element. \meet<range> e == iterated meet of a lattice with a top. For x, y of type T, where T is canonically a cbDistrLatticeType d: x `\` y == the (sectional) complement of y in [0, x]. For x of type T, where T is canonically a ctbDistrLatticeType d: ~` x == the complement of x in [0, 1].
There are three distinct uses of the symbols <, <=, >, >=, _ <= _ ?= iff _, >=<, and >< in the default display: they can be 0-ary, unary (prefix), and binary (infix). 0. <%O, <=%O, >%O, >=%O, <?=%O, >=<%O, and ><%O stand respectively for lt, le, gt, ge, leif (_ <= _ ?= iff _), comparable, and incomparable. 1. (< x), (<= x), (> x), (>= x), (>=< x), and (>< x) stand respectively for (>%O x), (>=%O x), (<%O x), (<=%O x), (>=<%O x), and (><%O x). So (< x) is a predicate characterizing elements smaller than x. 2. (x < y), (x <= y), ... mean what they are expected to. These conventions are compatible with Haskell's, where ((< y) x) = (x < y) = ((<) x y), except that we write <%O instead of (<).
Alternative notation displays can be defined by : 1. declaring a new opaque definition of type unit. Using the idiom `Lemma my_display : unit. Proof. exact: tt. Qed.` 2. using this symbol to tag canonical porderType structures using `Canonical my_porderType := POrderType my_display my_type my_mixin`, 3. declaring notations for the main operations of this library, by setting the first argument of the definition to the display, e.g. `Notation my_syndef_le x y := @Order.le my_display _ x y.` or `Notation "x <=< y" := @Order.lt my_display _ x y (at level ...).` Non overloaded notations will default to the default display.
One may use displays either for convenience or to disambiguate between different structures defined on "copies" of a type (as explained below.) We provide the following "copies" of types, the first one is a *documented example* natdvd := nat == a "copy" of nat which is canonically ordered using divisibility predicate dvdn. Notation %|, %<|, gcd, lcm are used instead of <=, <, meet and join. T^d := dual T, where dual is a new definition for (fun T => T) == a "copy" of T, such that if T is canonically ordered, then T^d is canonically ordered with the dual order, and displayed with an extra ^d in the notation i.e. <=^d, <^d, >=<^d, ><^d, `&`^d, `|`^d are used and displayed instead of <=, <, >=<, ><, `&`, `|` T *prod[d] T' := T * T' == a "copy" of the cartesian product such that, if T and T' are canonically ordered, then T *prod[d] T' is canonically ordered in product order. i.e. (x1, x2) <= (y1, y2) = (x1 <= y1) && (x2 <= y2), and displayed in display d T *p T' := T *prod[prod_display] T' where prod_display adds an extra ^p to all notations T *lexi[d] T' := T * T' == a "copy" of the cartesian product such that, if T and T' are canonically ordered, then T *lexi[d] T' is canonically ordered in lexicographic order i.e. (x1, x2) <= (y1, y2) = (x1 <= y1) && ((x1 >= y1) ==> (x2 <= y2)) and (x1, x2) < (y1, y2) = (x1 <= y1) && ((x1 >= y1) ==> (x2 < y2)) and displayed in display d T *l T' := T *lexi[lexi_display] T' where lexi_display adds an extra ^l to all notations seqprod_with d T := seq T == a "copy" of seq, such that if T is canonically ordered, then seqprod_with d T is canonically ordered in product order i.e. [:: x1, .., xn] <= [y1, .., yn] = (x1 <= y1) && ... && (xn <= yn) and displayed in display d n.-tupleprod[d] T == same with n.tuple T seqprod T := seqprod_with prod_display T n.-tupleprod T := n.-tuple[prod_display] T seqlexi_with d T := seq T == a "copy" of seq, such that if T is canonically ordered, then seqprod_with d T is canonically ordered in lexicographic order i.e. [:: x1, .., xn] <= [y1, .., yn] = (x1 <= x2) && ((x1 >= y1) ==> ((x2 <= y2) && ...)) and displayed in display d n.-tuplelexi[d] T == same with n.tuple T seqlexi T := lexiprod_with lexi_display T n.-tuplelexi T := n.-tuple[lexi_display] T {subset[d] T} := {set T} == a "copy" of set which is canonically ordered by the subset order and displayed in display d {subset T} := {subset[subset_display] T}
Beware that canonical structure inference will not try to find the copy of the structures that fits the display one mentioned, but will rather determine which canonical structure and display to use depending on the copy of the type one provided. In this sense they are merely displays to inform the user of what the inference did, rather than additional input for the inference.
Existing displays are either dual_display d (where d is a display), dvd_display (both explained above), ring_display (from algebra/ssrnum to change the scope of the usual notations to ring_scope). We also provide lexi_display and prod_display for lexicographic and product order respectively. The default display is tt and users can define their own as explained above.
For porderType we provide the following operations [arg min(i < i0 | P) M] == a value i : T minimizing M : R, subject to the condition P (i may appear in P and M), and provided P holds for i0. [arg max(i > i0 | P) M] == a value i maximizing M subject to P and provided P holds for i0. [arg min(i < i0 in A) M] == an i \in A minimizing M if i0 \in A. [arg max(i > i0 in A) M] == an i \in A maximizing M if i0 \in A. [arg min(i < i0) M] == an i : T minimizing M, given i0 : T. [arg max(i > i0) M] == an i : T maximizing M, given i0 : T. with head symbols Order.arg_min and Order.arg_max The user may use extremumP or extremum_inP to eliminate them.
  • > patterns for contextual rewriting: leLHS := (X in (X <= _)%O)%pattern leRHS := (X in (_ <= X)%O)%pattern ltLHS := (X in (X < _)%O)%pattern ltRHS := (X in (_ < X)%O)%pattern
In order to build the above structures, one must provide the appropriate factory instance to the following structure constructors. The list of possible factories is indicated after each constructor. Each factory is documented in the next paragraph. NB: Since each mixim_of record of structure in this library is an internal interface that is not designed to be used by users directly, one should not build structure instances from their Mixin constructors.
POrderType disp T pord_mixin == builds a porderType from a canonical choiceType instance of T where pord_mixin can be of types lePOrderMixin, ltPOrderMixin, meetJoinMixin, leOrderMixin, or ltOrderMixin or computed using PcanPOrderMixin or CanPOrderMixin. disp is a display as explained above
LatticeType T lat_mixin == builds a latticeType from a porderType where lat_mixin can be of types latticeMixin, distrLatticePOrderMixin, totalPOrderMixin, meetJoinMixin, leOrderMixin, or ltOrderMixin or computed using IsoLatticeMixin.
BLatticeType T bot_mixin == builds a bLatticeType from a latticeType and bottom where bot_mixin is of type bottomMixin.
TBLatticeType T top_mixin == builds a tbLatticeType from a bLatticeType and top where top_mixin is of type topMixin.
DistrLatticeType T lat_mixin == builds a distrLatticeType from a porderType where lat_mixin can be of types distrLatticeMixin, distrLatticePOrderMixin, totalLatticeMixin, totalPOrderMixin, meetJoinMixin, leOrderMixin, or ltOrderMixin or computed using IsoLatticeMixin.
CBDistrLatticeType T sub_mixin == builds a cbDistrLatticeType from a bDistrLatticeType and a difference operation where sub_mixin is of type cbDistrLatticeMixin.
CTBDistrLatticeType T compl_mixin == builds a ctbDistrLatticeType from a tbDistrLatticeType and a complement operation where compl_mixin is of type ctbDistrLatticeMixin.
OrderType T ord_mixin == builds an orderType from a distrLatticeType where ord_mixin can be of types totalOrderMixin, totalPOrderMixin, totalLatticeMixin, leOrderMixin, or ltOrderMixin or computed using MonoTotalMixin.
Additionally:
  • [porderType of _ ] ... notations are available to recover structures on "copies" of the types, as in eqType, choiceType, ssralg...
  • [finPOrderType of _ ] ... notations to compute joins between finite types and ordered types
List of possible factories:
  • lePOrderMixin == on a choiceType, takes le, lt, reflexivity, antisymmetry and transitivity of le. (can build: porderType)
  • ltPOrderMixin == on a choiceType, takes le, lt, irreflexivity and transitivity of lt. (can build: porderType)
  • latticeMixin == on a porderType, takes meet, join, commutativity and associativity of meet and join, and some absorption laws. (can build: latticeType)
  • distrLatticeMixin == on a latticeType, takes distributivity of meet over join. (can build: distrLatticeType)
  • distrLatticePOrderMixin == on a porderType, takes meet, join, commutativity and associativity of meet and join, and the absorption and distributive laws. (can build: latticeType, distrLatticeType)
  • meetJoinMixin == on a choiceType, takes le, lt, meet, join, commutativity and associativity of meet and join, the absorption and distributive laws, and idempotence of meet. (can build: porderType, latticeType, distrLatticeType)
  • meetJoinLeMixin == on a porderType, takes meet, join, and a proof that those are respectvely the greatest lower bound and the least upper bound. (can build: latticeType)
  • leOrderMixin == on a choiceType, takes le, lt, meet, join, antisymmetry, transitivity and totality of le. (can build: porderType, latticeType, distrLatticeType, orderType)
  • ltOrderMixin == on a choiceType, takes le, lt, meet, join, irreflexivity, transitivity and totality of lt. (can build: porderType, latticeType, distrLatticeType, orderType)
  • totalPOrderMixin == on a porderType T, totality of the order of T := total (<=%O : rel T) (can build: latticeType, distrLatticeType, orderType)
  • totalLatticeMixin == on a latticeType T, totality of the order of T := total (<=%O : rel T) (can build distrLatticeType, orderType)
  • totalOrderMixin == on a distrLatticeType T, totality of the order of T := total (<=%O : rel T) (can build: orderType) NB: the above three mixins are kept separate from each other (even though they are convertible), in order to avoid ambiguous coercion paths.
  • bottomMixin, topMixin, cbDistrLatticeMixin, ctbDistrLatticeMixin == mixins with one extra operation (respectively bottom, top, difference, and complement)
Additionally:
  • [porderMixin of T by <: ] creates a porderMixin by subtyping.
  • [totalOrderMixin of T by <: ] creates the associated totalOrderMixin.
  • PCanPOrderMixin, CanPOrderMixin create porderMixin from cancellations
  • MonoTotalMixin creates a totalPOrderMixin from monotonicity
  • IsoLatticeMixin creates a distrLatticeMixin from an ordered structure isomorphism (i.e., cancel f f', cancel f' f, {mono f : x y / x <= y})
List of "big pack" notations:
  • DistrLatticeOfChoiceType builds a distrLatticeType from a choiceType and a meetJoinMixin.
  • DistrLatticeOfPOrderType builds a distrLatticeType from a porderType and a distrLatticePOrderMixin.
  • OrderOfChoiceType builds an orderType from a choiceType, and a leOrderMixin or a ltOrderMixin.
  • OrderOfPOrder builds an orderType from a porderType and a totalPOrderMixin.
  • OrderOfLattice builds an orderType from a latticeType and a totalLatticeMixin.
NB: These big pack notations should be used only to construct instances on the fly, e.g., in the middle of a proof, and should not be used to declare canonical instances. See field/algebraics_fundamentals.v for an example usage.
We provide the following canonical instances of ordered types
  • all possible structures on bool
  • porderType, latticeType, distrLatticeType, orderType and bLatticeType on nat for the leq order
  • porderType, latticeType, distrLatticeType, orderType and finPOrderType on 'I_n and bLatticeType, tbLatticeType, bDistrLatticeType, tbDistrLatticeType, finLatticeType, finDistrLatticeType and finOrderType on 'I_n.+1 (to guarantee it is nonempty).
  • porderType, latticeType, distrLatticeType, bLatticeType, tbLatticeType, on nat for the dvdn order, where meet and join are respectively gcdn and lcmn
  • porderType, latticeType, distrLatticeType, orderType, bLatticeType, tbLatticeType, cbDistrLatticeType, ctbDistrLatticeType on T *prod[disp] T' a "copy" of T * T' using product order (and T *p T' its specialization to prod_display)
  • porderType, latticeType, distrLatticeType, and orderType, on T *lexi[disp] T' another "copy" of T * T', with lexicographic ordering (and T *l T' its specialization to lexi_display)
  • porderType, latticeType, distrLatticeType, and orderType, on {t : T & T' x} with lexicographic ordering
  • porderType, latticeType, distrLatticeType, orderType, bLatticeType, tbLatticeType, cbDistrLatticeType, ctbDistrLatticeType on seqprod_with disp T a "copy" of seq T using product order (and seqprod T' its specialization to prod_display)
  • porderType, latticeType, distrLatticeType, and orderType, on seqlexi_with disp T another "copy" of seq T, with lexicographic ordering (and seqlexi T its specialization to lexi_display)
  • porderType, latticeType, distrLatticeType, orderType, bLatticeType, tbLatticeType, cbDistrLatticeType, ctbDistrLatticeType on n.-tupleprod[disp] a "copy" of n.-tuple T using product order (and n.-tupleprod T its specialization to prod_display)
  • porderType, latticeType, distrLatticeType, and orderType, on n.-tuplelexi[d] T another "copy" of n.-tuple T, with lexicographic ordering (and n.-tuplelexi T its specialization to lexi_display)
  • porderType, latticeType, distrLatticeType, orderType, bLatticeType, tbLatticeType, cbDistrLatticeType, ctbDistrLatticeType on {subset[disp] T} a "copy" of {set T} using subset order (and {subset T} its specialization to subset_display)
and all possible finite type instances
In order to get a canonical order on prod, seq, tuple or set, one may import modules DefaultProdOrder or DefaultProdLexiOrder, DefaultSeqProdOrder or DefaultSeqLexiOrder, DefaultTupleProdOrder or DefaultTupleLexiOrder and DefaultSetSubsetOrder.
On orderType, leP ltP ltgtP are the three main lemmas for case analysis. On porderType, one may use comparableP, comparable_leP, comparable_ltP, and comparable_ltgtP, which are the four main lemmas for case analysis.
We also provide specialized versions of some theorems from path.v.
We provide Order.enum_val, Order.enum_rank, and Order.enum_rank_in, which are monotonous variations of enum_val, enum_rank, and enum_rank_in whenever the type is porderType, and their monotonicity is provided if this order is total. The theory is in the module Order (Order.enum_valK, Order.enum_rank_inK, etc) but Order.Enum can be imported to shorten these.
We provide an opaque monotonous bijection tagnat.sig / tagnat.rank between the finite types {i : 'I_n & 'I(p i)} and 'I(\sum_i p i): tagnat.sig : 'I(\sum_i p i) -> {i : 'I_n & 'I(p i)} tagnat.rank : {i : 'I_n & 'I(p i)} -> 'I(\sum_i p i) tagnat.sig1 : 'I(\sum_i p i) -> 'I_n tagnat.sig2 : forall p : 'I(\sum_i p i), 'I(p (tagnat.sig1 p)) tagnat.Rank : forall i, 'I(p i) -> 'I(\sum_i p i)
This file is based on prior work by D. Dreyer, G. Gonthier, A. Nanevski, P-Y Strub, B. Ziliani

Set Implicit Arguments.

Declare Scope order_scope.

Delimit Scope order_scope with O.
Local Open Scope order_scope.

Reserved Notation "<= y" (at level 35).
Reserved Notation ">= y" (at level 35).
Reserved Notation "< y" (at level 35).
Reserved Notation "> y" (at level 35).
Reserved Notation "<= y :> T" (at level 35, y at next level).
Reserved Notation ">= y :> T" (at level 35, y at next level).
Reserved Notation "< y :> T" (at level 35, y at next level).
Reserved Notation "> y :> T" (at level 35, y at next level).
Reserved Notation "x >=< y" (at level 70, no associativity).
Reserved Notation ">=< y" (at level 35).
Reserved Notation ">=< y :> T" (at level 35, y at next level).
Reserved Notation "x >< y" (at level 70, no associativity).
Reserved Notation ">< x" (at level 35).
Reserved Notation ">< y :> T" (at level 35, y at next level).
Reserved Notation "f \min g" (at level 50, left associativity).
Reserved Notation "f \max g" (at level 50, left associativity).

Reserved Notation "x < y ?<= 'if' c" (at level 70, y, c at next level,
  format "x '[hv' < y '/' ?<= 'if' c ']'").
Reserved Notation "x < y ?<= 'if' c :> T" (at level 70, y, c at next level,
  format "x '[hv' < y '/' ?<= 'if' c :> T ']'").

Reserved notation for lattice operations.
Reserved Notation "A `&` B" (at level 48, left associativity).
Reserved Notation "A `|` B" (at level 52, left associativity).
Reserved Notation "A `\` B" (at level 50, left associativity).
Reserved Notation "~` A" (at level 35, right associativity).

Notations for dual partial and total order
Reserved Notation "x <=^d y" (at level 70, y at next level).
Reserved Notation "x >=^d y" (at level 70, y at next level).
Reserved Notation "x <^d y" (at level 70, y at next level).
Reserved Notation "x >^d y" (at level 70, y at next level).
Reserved Notation "x <=^d y :> T" (at level 70, y at next level).
Reserved Notation "x >=^d y :> T" (at level 70, y at next level).
Reserved Notation "x <^d y :> T" (at level 70, y at next level).
Reserved Notation "x >^d y :> T" (at level 70, y at next level).
Reserved Notation "<=^d y" (at level 35).
Reserved Notation ">=^d y" (at level 35).
Reserved Notation "<^d y" (at level 35).
Reserved Notation ">^d y" (at level 35).
Reserved Notation "<=^d y :> T" (at level 35, y at next level).
Reserved Notation ">=^d y :> T" (at level 35, y at next level).
Reserved Notation "<^d y :> T" (at level 35, y at next level).
Reserved Notation ">^d y :> T" (at level 35, y at next level).
Reserved Notation "x >=<^d y" (at level 70, no associativity).
Reserved Notation ">=<^d y" (at level 35).
Reserved Notation ">=<^d y :> T" (at level 35, y at next level).
Reserved Notation "x ><^d y" (at level 70, no associativity).
Reserved Notation "><^d x" (at level 35).
Reserved Notation "><^d y :> T" (at level 35, y at next level).

Reserved Notation "x <=^d y <=^d z" (at level 70, y, z at next level).
Reserved Notation "x <^d y <=^d z" (at level 70, y, z at next level).
Reserved Notation "x <=^d y <^d z" (at level 70, y, z at next level).
Reserved Notation "x <^d y <^d z" (at level 70, y, z at next level).
Reserved Notation "x <=^d y ?= 'iff' c" (at level 70, y, c at next level,
  format "x '[hv' <=^d y '/' ?= 'iff' c ']'").
Reserved Notation "x <=^d y ?= 'iff' c :> T" (at level 70, y, c at next level,
  format "x '[hv' <=^d y '/' ?= 'iff' c :> T ']'").
Reserved Notation "x <^d y ?<= 'if' c" (at level 70, y, c at next level,
  format "x '[hv' <^d y '/' ?<= 'if' c ']'").
Reserved Notation "x <^d y ?<= 'if' c :> T" (at level 70, y, c at next level,
  format "x '[hv' <^d y '/' ?<= 'if' c :> T ']'").

Reserved notation for dual lattice operations.
Reserved Notation "A `&^d` B" (at level 48, left associativity).
Reserved Notation "A `|^d` B" (at level 52, left associativity).
Reserved Notation "A `\^d` B" (at level 50, left associativity).
Reserved Notation "~^d` A" (at level 35, right associativity).

Reserved Notation "0^d" (at level 0).
Reserved Notation "1^d" (at level 0).

Reserved notations for product ordering of prod or seq
Reserved Notation "x <=^p y" (at level 70, y at next level).
Reserved Notation "x >=^p y" (at level 70, y at next level).
Reserved Notation "x <^p y" (at level 70, y at next level).
Reserved Notation "x >^p y" (at level 70, y at next level).
Reserved Notation "x <=^p y :> T" (at level 70, y at next level).
Reserved Notation "x >=^p y :> T" (at level 70, y at next level).
Reserved Notation "x <^p y :> T" (at level 70, y at next level).
Reserved Notation "x >^p y :> T" (at level 70, y at next level).
Reserved Notation "<=^p y" (at level 35).
Reserved Notation ">=^p y" (at level 35).
Reserved Notation "<^p y" (at level 35).
Reserved Notation ">^p y" (at level 35).
Reserved Notation "<=^p y :> T" (at level 35, y at next level).
Reserved Notation ">=^p y :> T" (at level 35, y at next level).
Reserved Notation "<^p y :> T" (at level 35, y at next level).
Reserved Notation ">^p y :> T" (at level 35, y at next level).
Reserved Notation "x >=<^p y" (at level 70, no associativity).
Reserved Notation ">=<^p x" (at level 35).
Reserved Notation ">=<^p y :> T" (at level 35, y at next level).
Reserved Notation "x ><^p y" (at level 70, no associativity).
Reserved Notation "><^p x" (at level 35).
Reserved Notation "><^p y :> T" (at level 35, y at next level).

Reserved Notation "x <=^p y <=^p z" (at level 70, y, z at next level).
Reserved Notation "x <^p y <=^p z" (at level 70, y, z at next level).
Reserved Notation "x <=^p y <^p z" (at level 70, y, z at next level).
Reserved Notation "x <^p y <^p z" (at level 70, y, z at next level).
Reserved Notation "x <=^p y ?= 'iff' c" (at level 70, y, c at next level,
  format "x '[hv' <=^p y '/' ?= 'iff' c ']'").
Reserved Notation "x <=^p y ?= 'iff' c :> T" (at level 70, y, c at next level,
  format "x '[hv' <=^p y '/' ?= 'iff' c :> T ']'").

Reserved notation for dual lattice operations.
Reserved Notation "A `&^p` B" (at level 48, left associativity).
Reserved Notation "A `|^p` B" (at level 52, left associativity).
Reserved Notation "A `\^p` B" (at level 50, left associativity).
Reserved Notation "~^p` A" (at level 35, right associativity).

Reserved notations for lexicographic ordering of prod or seq
Reserved Notation "x <=^l y" (at level 70, y at next level).
Reserved Notation "x >=^l y" (at level 70, y at next level).
Reserved Notation "x <^l y" (at level 70, y at next level).
Reserved Notation "x >^l y" (at level 70, y at next level).
Reserved Notation "x <=^l y :> T" (at level 70, y at next level).
Reserved Notation "x >=^l y :> T" (at level 70, y at next level).
Reserved Notation "x <^l y :> T" (at level 70, y at next level).
Reserved Notation "x >^l y :> T" (at level 70, y at next level).
Reserved Notation "<=^l y" (at level 35).
Reserved Notation ">=^l y" (at level 35).
Reserved Notation "<^l y" (at level 35).
Reserved Notation ">^l y" (at level 35).
Reserved Notation "<=^l y :> T" (at level 35, y at next level).
Reserved Notation ">=^l y :> T" (at level 35, y at next level).
Reserved Notation "<^l y :> T" (at level 35, y at next level).
Reserved Notation ">^l y :> T" (at level 35, y at next level).
Reserved Notation "x >=<^l y" (at level 70, no associativity).
Reserved Notation ">=<^l x" (at level 35).
Reserved Notation ">=<^l y :> T" (at level 35, y at next level).
Reserved Notation "x ><^l y" (at level 70, no associativity).
Reserved Notation "><^l x" (at level 35).
Reserved Notation "><^l y :> T" (at level 35, y at next level).

Reserved Notation "x <=^l y <=^l z" (at level 70, y, z at next level).
Reserved Notation "x <^l y <=^l z" (at level 70, y, z at next level).
Reserved Notation "x <=^l y <^l z" (at level 70, y, z at next level).
Reserved Notation "x <^l y <^l z" (at level 70, y, z at next level).
Reserved Notation "x <=^l y ?= 'iff' c" (at level 70, y, c at next level,
  format "x '[hv' <=^l y '/' ?= 'iff' c ']'").
Reserved Notation "x <=^l y ?= 'iff' c :> T" (at level 70, y, c at next level,
  format "x '[hv' <=^l y '/' ?= 'iff' c :> T ']'").

Reserved notations for divisibility
Reserved Notation "x %<| y" (at level 70, no associativity).

Reserved Notation "\gcd_ i F"
  (at level 41, F at level 41, i at level 0,
           format "'[' \gcd_ i '/ ' F ']'").
Reserved Notation "\gcd_ ( i <- r | P ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \gcd_ ( i <- r | P ) '/ ' F ']'").
Reserved Notation "\gcd_ ( i <- r ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \gcd_ ( i <- r ) '/ ' F ']'").
Reserved Notation "\gcd_ ( m <= i < n | P ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \gcd_ ( m <= i < n | P ) '/ ' F ']'").
Reserved Notation "\gcd_ ( m <= i < n ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \gcd_ ( m <= i < n ) '/ ' F ']'").
Reserved Notation "\gcd_ ( i | P ) F"
  (at level 41, F at level 41, i at level 50,
           format "'[' \gcd_ ( i | P ) '/ ' F ']'").
Reserved Notation "\gcd_ ( i : t | P ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\gcd_ ( i : t ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\gcd_ ( i < n | P ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \gcd_ ( i < n | P ) '/ ' F ']'").
Reserved Notation "\gcd_ ( i < n ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \gcd_ ( i < n ) F ']'").
Reserved Notation "\gcd_ ( i 'in' A | P ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \gcd_ ( i 'in' A | P ) '/ ' F ']'").
Reserved Notation "\gcd_ ( i 'in' A ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \gcd_ ( i 'in' A ) '/ ' F ']'").

Reserved Notation "\lcm_ i F"
  (at level 41, F at level 41, i at level 0,
           format "'[' \lcm_ i '/ ' F ']'").
Reserved Notation "\lcm_ ( i <- r | P ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \lcm_ ( i <- r | P ) '/ ' F ']'").
Reserved Notation "\lcm_ ( i <- r ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \lcm_ ( i <- r ) '/ ' F ']'").
Reserved Notation "\lcm_ ( m <= i < n | P ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \lcm_ ( m <= i < n | P ) '/ ' F ']'").
Reserved Notation "\lcm_ ( m <= i < n ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \lcm_ ( m <= i < n ) '/ ' F ']'").
Reserved Notation "\lcm_ ( i | P ) F"
  (at level 41, F at level 41, i at level 50,
           format "'[' \lcm_ ( i | P ) '/ ' F ']'").
Reserved Notation "\lcm_ ( i : t | P ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\lcm_ ( i : t ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\lcm_ ( i < n | P ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \lcm_ ( i < n | P ) '/ ' F ']'").
Reserved Notation "\lcm_ ( i < n ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \lcm_ ( i < n ) F ']'").
Reserved Notation "\lcm_ ( i 'in' A | P ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \lcm_ ( i 'in' A | P ) '/ ' F ']'").
Reserved Notation "\lcm_ ( i 'in' A ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \lcm_ ( i 'in' A ) '/ ' F ']'").

Reserved notation for dual lattice operations.
Reserved Notation "A `&^l` B" (at level 48, left associativity).
Reserved Notation "A `|^l` B" (at level 52, left associativity).
Reserved Notation "A `\^l` B" (at level 50, left associativity).
Reserved Notation "~^l` A" (at level 35, right associativity).

Reserved Notation "\meet_ i F"
  (at level 41, F at level 41, i at level 0,
           format "'[' \meet_ i '/ ' F ']'").
Reserved Notation "\meet_ ( i <- r | P ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \meet_ ( i <- r | P ) '/ ' F ']'").
Reserved Notation "\meet_ ( i <- r ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \meet_ ( i <- r ) '/ ' F ']'").
Reserved Notation "\meet_ ( m <= i < n | P ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \meet_ ( m <= i < n | P ) '/ ' F ']'").
Reserved Notation "\meet_ ( m <= i < n ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \meet_ ( m <= i < n ) '/ ' F ']'").
Reserved Notation "\meet_ ( i | P ) F"
  (at level 41, F at level 41, i at level 50,
           format "'[' \meet_ ( i | P ) '/ ' F ']'").
Reserved Notation "\meet_ ( i : t | P ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\meet_ ( i : t ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\meet_ ( i < n | P ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \meet_ ( i < n | P ) '/ ' F ']'").
Reserved Notation "\meet_ ( i < n ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \meet_ ( i < n ) F ']'").
Reserved Notation "\meet_ ( i 'in' A | P ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \meet_ ( i 'in' A | P ) '/ ' F ']'").
Reserved Notation "\meet_ ( i 'in' A ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \meet_ ( i 'in' A ) '/ ' F ']'").

Reserved Notation "\join_ i F"
  (at level 41, F at level 41, i at level 0,
           format "'[' \join_ i '/ ' F ']'").
Reserved Notation "\join_ ( i <- r | P ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \join_ ( i <- r | P ) '/ ' F ']'").
Reserved Notation "\join_ ( i <- r ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \join_ ( i <- r ) '/ ' F ']'").
Reserved Notation "\join_ ( m <= i < n | P ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \join_ ( m <= i < n | P ) '/ ' F ']'").
Reserved Notation "\join_ ( m <= i < n ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \join_ ( m <= i < n ) '/ ' F ']'").
Reserved Notation "\join_ ( i | P ) F"
  (at level 41, F at level 41, i at level 50,
           format "'[' \join_ ( i | P ) '/ ' F ']'").
Reserved Notation "\join_ ( i : t | P ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\join_ ( i : t ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\join_ ( i < n | P ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \join_ ( i < n | P ) '/ ' F ']'").
Reserved Notation "\join_ ( i < n ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \join_ ( i < n ) F ']'").
Reserved Notation "\join_ ( i 'in' A | P ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \join_ ( i 'in' A | P ) '/ ' F ']'").
Reserved Notation "\join_ ( i 'in' A ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \join_ ( i 'in' A ) '/ ' F ']'").

Reserved Notation "\min_ i F"
  (at level 41, F at level 41, i at level 0,
           format "'[' \min_ i '/ ' F ']'").
Reserved Notation "\min_ ( i <- r | P ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \min_ ( i <- r | P ) '/ ' F ']'").
Reserved Notation "\min_ ( i <- r ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \min_ ( i <- r ) '/ ' F ']'").
Reserved Notation "\min_ ( m <= i < n | P ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \min_ ( m <= i < n | P ) '/ ' F ']'").
Reserved Notation "\min_ ( m <= i < n ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \min_ ( m <= i < n ) '/ ' F ']'").
Reserved Notation "\min_ ( i | P ) F"
  (at level 41, F at level 41, i at level 50,
           format "'[' \min_ ( i | P ) '/ ' F ']'").
Reserved Notation "\min_ ( i : t | P ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\min_ ( i : t ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\min_ ( i < n | P ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \min_ ( i < n | P ) '/ ' F ']'").
Reserved Notation "\min_ ( i < n ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \min_ ( i < n ) F ']'").
Reserved Notation "\min_ ( i 'in' A | P ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \min_ ( i 'in' A | P ) '/ ' F ']'").
Reserved Notation "\min_ ( i 'in' A ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \min_ ( i 'in' A ) '/ ' F ']'").

Reserved Notation "\max_ i F"
  (at level 41, F at level 41, i at level 0,
           format "'[' \max_ i '/ ' F ']'").
Reserved Notation "\max_ ( i <- r | P ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \max_ ( i <- r | P ) '/ ' F ']'").
Reserved Notation "\max_ ( i <- r ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \max_ ( i <- r ) '/ ' F ']'").
Reserved Notation "\max_ ( m <= i < n | P ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \max_ ( m <= i < n | P ) '/ ' F ']'").
Reserved Notation "\max_ ( m <= i < n ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \max_ ( m <= i < n ) '/ ' F ']'").
Reserved Notation "\max_ ( i | P ) F"
  (at level 41, F at level 41, i at level 50,
           format "'[' \max_ ( i | P ) '/ ' F ']'").
Reserved Notation "\max_ ( i : t | P ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\max_ ( i : t ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\max_ ( i < n | P ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \max_ ( i < n | P ) '/ ' F ']'").
Reserved Notation "\max_ ( i < n ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \max_ ( i < n ) F ']'").
Reserved Notation "\max_ ( i 'in' A | P ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \max_ ( i 'in' A | P ) '/ ' F ']'").
Reserved Notation "\max_ ( i 'in' A ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \max_ ( i 'in' A ) '/ ' F ']'").

Reserved Notation "\meet^d_ i F"
  (at level 41, F at level 41, i at level 0,
           format "'[' \meet^d_ i '/ ' F ']'").
Reserved Notation "\meet^d_ ( i <- r | P ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \meet^d_ ( i <- r | P ) '/ ' F ']'").
Reserved Notation "\meet^d_ ( i <- r ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \meet^d_ ( i <- r ) '/ ' F ']'").
Reserved Notation "\meet^d_ ( m <= i < n | P ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \meet^d_ ( m <= i < n | P ) '/ ' F ']'").
Reserved Notation "\meet^d_ ( m <= i < n ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \meet^d_ ( m <= i < n ) '/ ' F ']'").
Reserved Notation "\meet^d_ ( i | P ) F"
  (at level 41, F at level 41, i at level 50,
           format "'[' \meet^d_ ( i | P ) '/ ' F ']'").
Reserved Notation "\meet^d_ ( i : t | P ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\meet^d_ ( i : t ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\meet^d_ ( i < n | P ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \meet^d_ ( i < n | P ) '/ ' F ']'").
Reserved Notation "\meet^d_ ( i < n ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \meet^d_ ( i < n ) F ']'").
Reserved Notation "\meet^d_ ( i 'in' A | P ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \meet^d_ ( i 'in' A | P ) '/ ' F ']'").
Reserved Notation "\meet^d_ ( i 'in' A ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \meet^d_ ( i 'in' A ) '/ ' F ']'").

Reserved Notation "\join^d_ i F"
  (at level 41, F at level 41, i at level 0,
           format "'[' \join^d_ i '/ ' F ']'").
Reserved Notation "\join^d_ ( i <- r | P ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \join^d_ ( i <- r | P ) '/ ' F ']'").
Reserved Notation "\join^d_ ( i <- r ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \join^d_ ( i <- r ) '/ ' F ']'").
Reserved Notation "\join^d_ ( m <= i < n | P ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \join^d_ ( m <= i < n | P ) '/ ' F ']'").
Reserved Notation "\join^d_ ( m <= i < n ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \join^d_ ( m <= i < n ) '/ ' F ']'").
Reserved Notation "\join^d_ ( i | P ) F"
  (at level 41, F at level 41, i at level 50,
           format "'[' \join^d_ ( i | P ) '/ ' F ']'").
Reserved Notation "\join^d_ ( i : t | P ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\join^d_ ( i : t ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\join^d_ ( i < n | P ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \join^d_ ( i < n | P ) '/ ' F ']'").
Reserved Notation "\join^d_ ( i < n ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \join^d_ ( i < n ) F ']'").
Reserved Notation "\join^d_ ( i 'in' A | P ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \join^d_ ( i 'in' A | P ) '/ ' F ']'").
Reserved Notation "\join^d_ ( i 'in' A ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \join^d_ ( i 'in' A ) '/ ' F ']'").

Reserved Notation "\min^d_ i F"
  (at level 41, F at level 41, i at level 0,
           format "'[' \min^d_ i '/ ' F ']'").
Reserved Notation "\min^d_ ( i <- r | P ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \min^d_ ( i <- r | P ) '/ ' F ']'").
Reserved Notation "\min^d_ ( i <- r ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \min^d_ ( i <- r ) '/ ' F ']'").
Reserved Notation "\min^d_ ( m <= i < n | P ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \min^d_ ( m <= i < n | P ) '/ ' F ']'").
Reserved Notation "\min^d_ ( m <= i < n ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \min^d_ ( m <= i < n ) '/ ' F ']'").
Reserved Notation "\min^d_ ( i | P ) F"
  (at level 41, F at level 41, i at level 50,
           format "'[' \min^d_ ( i | P ) '/ ' F ']'").
Reserved Notation "\min^d_ ( i : t | P ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\min^d_ ( i : t ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\min^d_ ( i < n | P ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \min^d_ ( i < n | P ) '/ ' F ']'").
Reserved Notation "\min^d_ ( i < n ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \min^d_ ( i < n ) F ']'").
Reserved Notation "\min^d_ ( i 'in' A | P ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \min^d_ ( i 'in' A | P ) '/ ' F ']'").
Reserved Notation "\min^d_ ( i 'in' A ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \min^d_ ( i 'in' A ) '/ ' F ']'").

Reserved Notation "\max^d_ i F"
  (at level 41, F at level 41, i at level 0,
           format "'[' \max^d_ i '/ ' F ']'").
Reserved Notation "\max^d_ ( i <- r | P ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \max^d_ ( i <- r | P ) '/ ' F ']'").
Reserved Notation "\max^d_ ( i <- r ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \max^d_ ( i <- r ) '/ ' F ']'").
Reserved Notation "\max^d_ ( m <= i < n | P ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \max^d_ ( m <= i < n | P ) '/ ' F ']'").
Reserved Notation "\max^d_ ( m <= i < n ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \max^d_ ( m <= i < n ) '/ ' F ']'").
Reserved Notation "\max^d_ ( i | P ) F"
  (at level 41, F at level 41, i at level 50,
           format "'[' \max^d_ ( i | P ) '/ ' F ']'").
Reserved Notation "\max^d_ ( i : t | P ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\max^d_ ( i : t ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\max^d_ ( i < n | P ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \max^d_ ( i < n | P ) '/ ' F ']'").
Reserved Notation "\max^d_ ( i < n ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \max^d_ ( i < n ) F ']'").
Reserved Notation "\max^d_ ( i 'in' A | P ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \max^d_ ( i 'in' A | P ) '/ ' F ']'").
Reserved Notation "\max^d_ ( i 'in' A ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \max^d_ ( i 'in' A ) '/ ' F ']'").

Reserved Notation "\meet^p_ i F"
  (at level 41, F at level 41, i at level 0,
           format "'[' \meet^p_ i '/ ' F ']'").
Reserved Notation "\meet^p_ ( i <- r | P ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \meet^p_ ( i <- r | P ) '/ ' F ']'").
Reserved Notation "\meet^p_ ( i <- r ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \meet^p_ ( i <- r ) '/ ' F ']'").
Reserved Notation "\meet^p_ ( m <= i < n | P ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \meet^p_ ( m <= i < n | P ) '/ ' F ']'").
Reserved Notation "\meet^p_ ( m <= i < n ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \meet^p_ ( m <= i < n ) '/ ' F ']'").
Reserved Notation "\meet^p_ ( i | P ) F"
  (at level 41, F at level 41, i at level 50,
           format "'[' \meet^p_ ( i | P ) '/ ' F ']'").
Reserved Notation "\meet^p_ ( i : t | P ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\meet^p_ ( i : t ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\meet^p_ ( i < n | P ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \meet^p_ ( i < n | P ) '/ ' F ']'").
Reserved Notation "\meet^p_ ( i < n ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \meet^p_ ( i < n ) F ']'").
Reserved Notation "\meet^p_ ( i 'in' A | P ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \meet^p_ ( i 'in' A | P ) '/ ' F ']'").
Reserved Notation "\meet^p_ ( i 'in' A ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \meet^p_ ( i 'in' A ) '/ ' F ']'").

Reserved Notation "\join^p_ i F"
  (at level 41, F at level 41, i at level 0,
           format "'[' \join^p_ i '/ ' F ']'").
Reserved Notation "\join^p_ ( i <- r | P ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \join^p_ ( i <- r | P ) '/ ' F ']'").
Reserved Notation "\join^p_ ( i <- r ) F"
  (at level 41, F at level 41, i, r at level 50,
           format "'[' \join^p_ ( i <- r ) '/ ' F ']'").
Reserved Notation "\join^p_ ( m <= i < n | P ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \join^p_ ( m <= i < n | P ) '/ ' F ']'").
Reserved Notation "\join^p_ ( m <= i < n ) F"
  (at level 41, F at level 41, i, m, n at level 50,
           format "'[' \join^p_ ( m <= i < n ) '/ ' F ']'").
Reserved Notation "\join^p_ ( i | P ) F"
  (at level 41, F at level 41, i at level 50,
           format "'[' \join^p_ ( i | P ) '/ ' F ']'").
Reserved Notation "\join^p_ ( i : t | P ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\join^p_ ( i : t ) F"
  (at level 41, F at level 41, i at level 50).
Reserved Notation "\join^p_ ( i < n | P ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \join^p_ ( i < n | P ) '/ ' F ']'").
Reserved Notation "\join^p_ ( i < n ) F"
  (at level 41, F at level 41, i, n at level 50,
           format "'[' \join^p_ ( i < n ) F ']'").
Reserved Notation "\join^p_ ( i 'in' A | P ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \join^p_ ( i 'in' A | P ) '/ ' F ']'").
Reserved Notation "\join^p_ ( i 'in' A ) F"
  (at level 41, F at level 41, i, A at level 50,
           format "'[' \join^p_ ( i 'in' A ) '/ ' F ']'").

Module Order.

STRUCTURES

Module POrder.
Section ClassDef.

Record mixin_of (T0 : Type) (b : Equality.class_of T0)
                (T := Equality.Pack b) := Mixin {
  le : rel T;
  lt : rel T;
  _ : x y, lt x y = (y != x) && (le x y);
  _ : reflexive le;
  _ : antisymmetric le;
  _ : transitive le;
}.

Record class_of (T : Type) := Class {
  base : Choice.class_of T;
  mixin : mixin_of base;
}.


Structure type (disp : unit) := Pack { sort; _ : class_of sort }.


Variables (T : Type) (disp : unit) (cT : type disp).

Definition class := let: Pack _ c as cT' := cT return class_of cT' in c.
Definition clone c of phant_id class c := @Pack disp T c.
Definition clone_with disp' c of phant_id class c := @Pack disp' T c.

Definition pack :=
  fun bT b & phant_id (Choice.class bT) b
  fun mPack disp (@Class T b m).

Definition eqType := @Equality.Pack cT class.
Definition choiceType := @Choice.Pack cT class.
End ClassDef.

Module Exports.
Coercion base : class_of >-> Choice.class_of.
Coercion mixin : class_of >-> mixin_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Coercion choiceType : type >-> Choice.type.
Canonical eqType.
Canonical choiceType.
Notation porderType := type.
Notation POrderType disp T m := (@pack T disp _ _ id m).
Notation "[ 'porderType' 'of' T 'for' cT ]" := (@clone T _ cT _ id)
  (at level 0, format "[ 'porderType' 'of' T 'for' cT ]") : form_scope.
Notation "[ 'porderType' 'of' T 'for' cT 'with' disp ]" :=
  (@clone_with T _ cT disp _ id)
  (at level 0, format "[ 'porderType' 'of' T 'for' cT 'with' disp ]") :
  form_scope.
Notation "[ 'porderType' 'of' T ]" := [porderType of T for _]
  (at level 0, format "[ 'porderType' 'of' T ]") : form_scope.
Notation "[ 'porderType' 'of' T 'with' disp ]" :=
  [porderType of T for _ with disp]
  (at level 0, format "[ 'porderType' 'of' T 'with' disp ]") : form_scope.
End Exports.

End POrder.
Import POrder.Exports.

Section POrderDef.

Variable (disp : unit) (T : porderType disp).

Definition le : rel T := POrder.le (POrder.class T).

Definition lt : rel T := POrder.lt (POrder.class T).

Definition comparable : rel T := fun (x y : T) ⇒ (x y) || (y x).

Definition ge : simpl_rel T := [rel x y | y x].
Definition gt : simpl_rel T := [rel x y | y < x].
Definition leif (x y : T) C : Prop := ((x y) × ((x == y) = C))%type.

Definition le_of_leif x y C (le_xy : @leif x y C) := le_xy.1 : le x y.

Definition lteif x y C := if C then x y else x < y.

Variant le_xor_gt (x y : T) :
  T T T T bool bool Set :=
  | LeNotGt of x y : le_xor_gt x y x x y y true false
  | GtNotLe of y < x : le_xor_gt x y y y x x false true.

Variant lt_xor_ge (x y : T) :
  T T T T bool bool Set :=
  | LtNotGe of x < y : lt_xor_ge x y x x y y false true
  | GeNotLt of y x : lt_xor_ge x y y y x x true false.

Definition min x y := if x < y then x else y.
Definition max x y := if x < y then y else x.

Variant compare (x y : T) :
   T T T T
   bool bool bool bool bool bool Set :=
  | CompareLt of x < y : compare x y
    x x y y false false false true false true
  | CompareGt of y < x : compare x y
    y y x x false false true false true false
  | CompareEq of x = y : compare x y
    x x x x true true true true false false.

Variant incompare (x y : T) :
   T T T T
  bool bool bool bool bool bool bool bool Set :=
  | InCompareLt of x < y : incompare x y
    x x y y false false false true false true true true
  | InCompareGt of y < x : incompare x y
    y y x x false false true false true false true true
  | InCompare of x >< y : incompare x y
    x y y x false false false false false false false false
  | InCompareEq of x = y : incompare x y
    x x x x true true true true false false true true.

Definition arg_min {I : finType} := @extremum T I le.
Definition arg_max {I : finType} := @extremum T I ge.

Lifted min/max operations.
Section LiftedPOrder.
Variable T' : Type.
Implicit Type f : T' T.
Definition min_fun f g x := min (f x) (g x).
Definition max_fun f g x := min (f x) (g x).
End LiftedPOrder.

End POrderDef.

Arguments ge {_ _}.
Arguments gt {_ _}.
Arguments min {_ _}.
Arguments max {_ _}.
Arguments comparable {_ _}.
Arguments min_fun {_ _ _} f g _ /.
Arguments max_fun {_ _ _} f g _ /.

Module Import POSyntax.

Notation "<=%O" := le : fun_scope.
Notation ">=%O" := ge : fun_scope.
Notation "<%O" := lt : fun_scope.
Notation ">%O" := gt : fun_scope.
Notation "<?=%O" := leif : fun_scope.
Notation "<?<=%O" := lteif : fun_scope.
Notation ">=<%O" := comparable : fun_scope.
Notation "><%O" := (fun x y~~ (comparable x y)) : fun_scope.

Notation "<= y" := (ge y) : order_scope.
Notation "<= y :> T" := ( (y : T)) (only parsing) : order_scope.
Notation ">= y" := (le y) : order_scope.
Notation ">= y :> T" := ( (y : T)) (only parsing) : order_scope.

Notation "< y" := (gt y) : order_scope.
Notation "< y :> T" := (< (y : T)) (only parsing) : order_scope.
Notation "> y" := (lt y) : order_scope.
Notation "> y :> T" := (> (y : T)) (only parsing) : order_scope.

Notation "x <= y" := (le x y) : order_scope.
Notation "x <= y :> T" := ((x : T) (y : T)) (only parsing) : order_scope.
Notation "x >= y" := (y x) (only parsing) : order_scope.
Notation "x >= y :> T" := ((x : T) (y : T)) (only parsing) : order_scope.

Notation "x < y" := (lt x y) : order_scope.
Notation "x < y :> T" := ((x : T) < (y : T)) (only parsing) : order_scope.
Notation "x > y" := (y < x) (only parsing) : order_scope.
Notation "x > y :> T" := ((x : T) > (y : T)) (only parsing) : order_scope.

Notation "x <= y <= z" := ((x y) && (y z)) : order_scope.
Notation "x < y <= z" := ((x < y) && (y z)) : order_scope.
Notation "x <= y < z" := ((x y) && (y < z)) : order_scope.
Notation "x < y < z" := ((x < y) && (y < z)) : order_scope.

Notation "x <= y ?= 'iff' C" := (leif x y C) : order_scope.
Notation "x <= y ?= 'iff' C :> T" := ((x : T) (y : T) ?= iff C)
  (only parsing) : order_scope.

Notation "x < y ?<= 'if' C" := (lteif x y C) : order_scope.
Notation "x < y ?<= 'if' C :> T" := ((x : T) < (y : T) ?<= if C)
  (only parsing) : order_scope.

Notation ">=< y" := [pred x | comparable x y] : order_scope.
Notation ">=< y :> T" := (>=< (y : T)) (only parsing) : order_scope.
Notation "x >=< y" := (comparable x y) : order_scope.

Notation ">< y" := [pred x | ~~ comparable x y] : order_scope.
Notation ">< y :> T" := (>< (y : T)) (only parsing) : order_scope.
Notation "x >< y" := (~~ (comparable x y)) : order_scope.

Notation "[ 'arg' 'min_' ( i < i0 | P ) F ]" :=
    (arg_min i0 (fun iP%B) (fun iF))
  (at level 0, i, i0 at level 10,
   format "[ 'arg' 'min_' ( i < i0 | P ) F ]") : order_scope.

Notation "[ 'arg' 'min_' ( i < i0 'in' A ) F ]" :=
    [arg min_(i < i0 | i \in A) F]
  (at level 0, i, i0 at level 10,
   format "[ 'arg' 'min_' ( i < i0 'in' A ) F ]") : order_scope.

Notation "[ 'arg' 'min_' ( i < i0 ) F ]" := [arg min_(i < i0 | true) F]
  (at level 0, i, i0 at level 10,
   format "[ 'arg' 'min_' ( i < i0 ) F ]") : order_scope.

Notation "[ 'arg' 'max_' ( i > i0 | P ) F ]" :=
     (arg_max i0 (fun iP%B) (fun iF))
  (at level 0, i, i0 at level 10,
   format "[ 'arg' 'max_' ( i > i0 | P ) F ]") : order_scope.

Notation "[ 'arg' 'max_' ( i > i0 'in' A ) F ]" :=
    [arg max_(i > i0 | i \in A) F]
  (at level 0, i, i0 at level 10,
   format "[ 'arg' 'max_' ( i > i0 'in' A ) F ]") : order_scope.

Notation "[ 'arg' 'max_' ( i > i0 ) F ]" := [arg max_(i > i0 | true) F]
  (at level 0, i, i0 at level 10,
   format "[ 'arg' 'max_' ( i > i0 ) F ]") : order_scope.

Notation "f \min g" := (min_fun f g) : order_scope.
Notation "f \max g" := (max_fun f g) : order_scope.

Notation leLHS := (X in (X _)%O)%pattern.
Notation leRHS := (X in (_ X)%O)%pattern.
Notation ltLHS := (X in (X < _)%O)%pattern.
Notation ltRHS := (X in (_ < X)%O)%pattern.

End POSyntax.

Module POCoercions.
Coercion le_of_leif : leif >-> is_true.
End POCoercions.

Module Lattice.
Section ClassDef.

Record mixin_of (T0 : Type) (b : POrder.class_of T0)
                (T := POrder.Pack tt b) := Mixin {
  meet : T T T;
  join : T T T;
  _ : commutative meet;
  _ : commutative join;
  _ : associative meet;
  _ : associative join;
  _ : y x, meet x (join x y) = x;
  _ : y x, join x (meet x y) = x;
  _ : x y, (x y) = (meet x y == x);
}.

Record class_of (T : Type) := Class {
  base : POrder.class_of T;
  mixin : mixin_of base;
}.


Structure type (disp : unit) := Pack { sort; _ : class_of sort }.


Variables (T : Type) (disp : unit) (cT : type disp).

Definition class := let: Pack _ c as cT' := cT return class_of cT' in c.
Definition clone c of phant_id class c := @Pack disp T c.
Definition clone_with disp' c of phant_id class c := @Pack disp' T c.

Definition pack :=
  fun bT b & phant_id (@POrder.class disp bT) b
  fun mPack disp (@Class T b m).

Definition eqType := @Equality.Pack cT class.
Definition choiceType := @Choice.Pack cT class.
Definition porderType := @POrder.Pack disp cT class.
End ClassDef.

Module Exports.
Coercion base : class_of >-> POrder.class_of.
Coercion mixin : class_of >-> mixin_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Coercion choiceType : type >-> Choice.type.
Coercion porderType : type >-> POrder.type.
Canonical eqType.
Canonical choiceType.
Canonical porderType.
Notation latticeType := type.
Notation LatticeType T m := (@pack T _ _ _ id m).
Notation "[ 'latticeType' 'of' T 'for' cT ]" := (@clone T _ cT _ id)
  (at level 0, format "[ 'latticeType' 'of' T 'for' cT ]") : form_scope.
Notation "[ 'latticeType' 'of' T 'for' cT 'with' disp ]" :=
  (@clone_with T _ cT disp _ id)
  (at level 0, format "[ 'latticeType' 'of' T 'for' cT 'with' disp ]") :
  form_scope.
Notation "[ 'latticeType' 'of' T ]" := [latticeType of T for _]
  (at level 0, format "[ 'latticeType' 'of' T ]") : form_scope.
Notation "[ 'latticeType' 'of' T 'with' disp ]" :=
  [latticeType of T for _ with disp]
  (at level 0, format "[ 'latticeType' 'of' T 'with' disp ]") : form_scope.
End Exports.
End Lattice.
Export Lattice.Exports.

Section LatticeDef.
Context {disp : unit} {T : latticeType disp}.
Definition meet : T T T := Lattice.meet (Lattice.class T).
Definition join : T T T := Lattice.join (Lattice.class T).

Variant lel_xor_gt (x y : T) :
  T T T T T T T T bool bool Set :=
  | LelNotGt of x y : lel_xor_gt x y x x y y x x y y true false
  | GtlNotLe of y < x : lel_xor_gt x y y y x x y y x x false true.

Variant ltl_xor_ge (x y : T) :
  T T T T T T T T bool bool Set :=
  | LtlNotGe of x < y : ltl_xor_ge x y x x y y x x y y false true
  | GelNotLt of y x : ltl_xor_ge x y y y x x y y x x true false.

Variant comparel (x y : T) :
   T T T T T T T T
   bool bool bool bool bool bool Set :=
  | ComparelLt of x < y : comparel x y
    x x y y x x y y false false false true false true
  | ComparelGt of y < x : comparel x y
    y y x x y y x x false false true false true false
  | ComparelEq of x = y : comparel x y
    x x x x x x x x true true true true false false.

Variant incomparel (x y : T) :
  T T T T T T T T
  bool bool bool bool bool bool bool bool Set :=
  | InComparelLt of x < y : incomparel x y
    x x y y x x y y false false false true false true true true
  | InComparelGt of y < x : incomparel x y
    y y x x y y x x false false true false true false true true
  | InComparel of x >< y : incomparel x y
    x y y x (meet y x) (meet x y) (join y x) (join x y)
    false false false false false false false false
  | InComparelEq of x = y : incomparel x y
    x x x x x x x x true true true true false false true true.

End LatticeDef.

Module Import LatticeSyntax.

Notation "x `&` y" := (meet x y) : order_scope.
Notation "x `|` y" := (join x y) : order_scope.

End LatticeSyntax.

Module BLattice.
Section ClassDef.

Record mixin_of (T : Type) (b : POrder.class_of T)
                (T := POrder.Pack tt b) := Mixin {
  bottom : T;
  _ : x, bottom x;
}.

Record class_of (T : Type) := Class {
  base : Lattice.class_of T;
  mixin : mixin_of base;
}.


Structure type (disp : unit) := Pack { sort; _ : class_of sort }.


Variables (T : Type) (disp : unit) (cT : type disp).

Definition class := let: Pack _ c as cT' := cT return class_of cT' in c.
Definition clone c of phant_id class c := @Pack disp T c.
Definition clone_with disp' c of phant_id class c := @Pack disp' T c.

Definition pack :=
  fun bT b & phant_id (@Lattice.class disp bT) b
  fun mPack disp (@Class T b m).

Definition eqType := @Equality.Pack cT class.
Definition choiceType := @Choice.Pack cT class.
Definition porderType := @POrder.Pack disp cT class.
Definition latticeType := @Lattice.Pack disp cT class.
End ClassDef.

Module Exports.
Coercion base : class_of >-> Lattice.class_of.
Coercion mixin : class_of >-> mixin_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Coercion choiceType : type >-> Choice.type.
Coercion porderType : type >-> POrder.type.
Coercion latticeType : type >-> Lattice.type.
Canonical eqType.
Canonical choiceType.
Canonical porderType.
Canonical latticeType.
Notation bLatticeType := type.
Notation BLatticeType T m := (@pack T _ _ _ id m).
Notation "[ 'bLatticeType' 'of' T 'for' cT ]" := (@clone T _ cT _ id)
  (at level 0, format "[ 'bLatticeType' 'of' T 'for' cT ]") : form_scope.
Notation "[ 'bLatticeType' 'of' T 'for' cT 'with' disp ]" :=
  (@clone_with T _ cT disp _ id)
  (at level 0,
   format "[ 'bLatticeType' 'of' T 'for' cT 'with' disp ]") :
  form_scope.
Notation "[ 'bLatticeType' 'of' T ]" := [bLatticeType of T for _]
  (at level 0, format "[ 'bLatticeType' 'of' T ]") : form_scope.
Notation "[ 'bLatticeType' 'of' T 'with' disp ]" :=
  [bLatticeType of T for _ with disp]
  (at level 0, format "[ 'bLatticeType' 'of' T 'with' disp ]") :
  form_scope.
End Exports.

End BLattice.
Export BLattice.Exports.

Definition bottom {disp : unit} {T : bLatticeType disp} : T :=
  BLattice.bottom (BLattice.class T).

Module Import BLatticeSyntax.
Notation "0" := bottom : order_scope.

Notation "\join_ ( i <- r | P ) F" :=
  (\big[@join _ _/0%O]_(i <- r | P%B) F%O) : order_scope.
Notation "\join_ ( i <- r ) F" :=
  (\big[@join _ _/0%O]_(i <- r) F%O) : order_scope.
Notation "\join_ ( i | P ) F" :=
  (\big[@join _ _/0%O]_(i | P%B) F%O) : order_scope.
Notation "\join_ i F" :=
  (\big[@join _ _/0%O]_i F%O) : order_scope.
Notation "\join_ ( i : I | P ) F" :=
  (\big[@join _ _/0%O]_(i : I | P%B) F%O) (only parsing) : order_scope.
Notation "\join_ ( i : I ) F" :=
  (\big[@join _ _/0%O]_(i : I) F%O) (only parsing) : order_scope.
Notation "\join_ ( m <= i < n | P ) F" :=
  (\big[@join _ _/0%O]_(m i < n | P%B) F%O) : order_scope.
Notation "\join_ ( m <= i < n ) F" :=
  (\big[@join _ _/0%O]_(m i < n) F%O) : order_scope.
Notation "\join_ ( i < n | P ) F" :=
  (\big[@join _ _/0%O]_(i < n | P%B) F%O) : order_scope.
Notation "\join_ ( i < n ) F" :=
  (\big[@join _ _/0%O]_(i < n) F%O) : order_scope.
Notation "\join_ ( i 'in' A | P ) F" :=
  (\big[@join _ _/0%O]_(i in A | P%B) F%O) : order_scope.
Notation "\join_ ( i 'in' A ) F" :=
  (\big[@join _ _/0%O]_(i in A) F%O) : order_scope.

End BLatticeSyntax.

Module TBLattice.
Section ClassDef.

Record mixin_of (T0 : Type) (b : POrder.class_of T0)
                (T := POrder.Pack tt b) := Mixin {
  top : T;
  _ : x, x top;
}.

Record class_of (T : Type) := Class {
  base : BLattice.class_of T;
  mixin : mixin_of base;
}.


Structure type (disp : unit) := Pack { sort; _ : class_of sort }.


Variables (T : Type) (disp : unit) (cT : type disp).

Definition class := let: Pack _ c as cT' := cT return class_of cT' in c.
Definition clone c of phant_id class c := @Pack disp T c.
Definition clone_with disp' c of phant_id class c := @Pack disp' T c.

Definition pack :=
  fun bT b & phant_id (@BLattice.class disp bT) b
  fun mPack disp (@Class T b m).

Definition eqType := @Equality.Pack cT class.
Definition choiceType := @Choice.Pack cT class.
Definition porderType := @POrder.Pack disp cT class.
Definition latticeType := @Lattice.Pack disp cT class.
Definition bLatticeType := @BLattice.Pack disp cT class.
End ClassDef.

Module Exports.
Coercion base : class_of >-> BLattice.class_of.
Coercion mixin : class_of >-> mixin_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Coercion choiceType : type >-> Choice.type.
Coercion porderType : type >-> POrder.type.
Coercion latticeType : type >-> Lattice.type.
Coercion bLatticeType : type >-> BLattice.type.
Canonical eqType.
Canonical choiceType.
Canonical porderType.
Canonical latticeType.
Canonical bLatticeType.
Notation tbLatticeType := type.
Notation TBLatticeType T m := (@pack T _ _ _ id m).
Notation "[ 'tbLatticeType' 'of' T 'for' cT ]" := (@clone T _ cT _ id)
  (at level 0, format "[ 'tbLatticeType' 'of' T 'for' cT ]") : form_scope.
Notation "[ 'tbLatticeType' 'of' T 'for' cT 'with' disp ]" :=
  (@clone_with T _ cT disp _ id)
  (at level 0,
   format "[ 'tbLatticeType' 'of' T 'for' cT 'with' disp ]") : form_scope.
Notation "[ 'tbLatticeType' 'of' T ]" := [tbLatticeType of T for _]
  (at level 0, format "[ 'tbLatticeType' 'of' T ]") : form_scope.
Notation "[ 'tbLatticeType' 'of' T 'with' disp ]" :=
  [tbLatticeType of T for _ with disp]
  (at level 0, format "[ 'tbLatticeType' 'of' T 'with' disp ]") : form_scope.
End Exports.

End TBLattice.
Export TBLattice.Exports.

Definition top disp {T : tbLatticeType disp} : T :=
  TBLattice.top (TBLattice.class T).

Module Import TBLatticeSyntax.

Notation "1" := top : order_scope.

Notation "\meet_ ( i <- r | P ) F" :=
  (\big[meet/1]_(i <- r | P%B) F%O) : order_scope.
Notation "\meet_ ( i <- r ) F" :=
  (\big[meet/1]_(i <- r) F%O) : order_scope.
Notation "\meet_ ( i | P ) F" :=
  (\big[meet/1]_(i | P%B) F%O) : order_scope.
Notation "\meet_ i F" :=
  (\big[meet/1]_i F%O) : order_scope.
Notation "\meet_ ( i : I | P ) F" :=
  (\big[meet/1]_(i : I | P%B) F%O) (only parsing) : order_scope.
Notation "\meet_ ( i : I ) F" :=
  (\big[meet/1]_(i : I) F%O) (only parsing) : order_scope.
Notation "\meet_ ( m <= i < n | P ) F" :=
 (\big[meet/1]_(m i < n | P%B) F%O) : order_scope.
Notation "\meet_ ( m <= i < n ) F" :=
 (\big[meet/1]_(m i < n) F%O) : order_scope.
Notation "\meet_ ( i < n | P ) F" :=
 (\big[meet/1]_(i < n | P%B) F%O) : order_scope.
Notation "\meet_ ( i < n ) F" :=
 (\big[meet/1]_(i < n) F%O) : order_scope.
Notation "\meet_ ( i 'in' A | P ) F" :=
 (\big[meet/1]_(i in A | P%B) F%O) : order_scope.
Notation "\meet_ ( i 'in' A ) F" :=
 (\big[meet/1]_(i in A) F%O) : order_scope.

End TBLatticeSyntax.

Module DistrLattice.
Section ClassDef.

Record mixin_of (T0 : Type) (b : Lattice.class_of T0)
                (T := Lattice.Pack tt b) := Mixin {
  _ : @left_distributive T T meet join;
}.

Record class_of (T : Type) := Class {
  base : Lattice.class_of T;
  mixin : mixin_of base;
}.


Structure type (disp : unit) := Pack { sort; _ : class_of sort }.


Variables (T : Type) (disp : unit) (cT : type disp).

Definition class := let: Pack _ c as cT' := cT return class_of cT' in c.
Definition clone c of phant_id class c := @Pack disp T c.
Definition clone_with disp' c of phant_id class c := @Pack disp' T c.

Definition pack :=
  fun bT b & phant_id (@Lattice.class disp bT) b
  fun mPack disp (@Class T b m).

Definition eqType := @Equality.Pack cT class.
Definition choiceType := @Choice.Pack cT class.
Definition porderType := @POrder.Pack disp cT class.
Definition latticeType := @Lattice.Pack disp cT class.
End ClassDef.

Module Exports.
Coercion base : class_of >-> Lattice.class_of.
Coercion mixin : class_of >-> mixin_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Coercion choiceType : type >-> Choice.type.
Coercion porderType : type >-> POrder.type.
Coercion latticeType : type >-> Lattice.type.
Canonical eqType.
Canonical choiceType.
Canonical porderType.
Canonical latticeType.
Notation distrLatticeType := type.
Notation DistrLatticeType T m := (@pack T _ _ _ id m).
Notation "[ 'distrLatticeType' 'of' T 'for' cT ]" := (@clone T _ cT _ id)
  (at level 0, format "[ 'distrLatticeType' 'of' T 'for' cT ]") :
  form_scope.
Notation "[ 'distrLatticeType' 'of' T 'for' cT 'with' disp ]" :=
  (@clone_with T _ cT disp _ id)
  (at level 0,
   format "[ 'distrLatticeType' 'of' T 'for' cT 'with' disp ]") :
  form_scope.
Notation "[ 'distrLatticeType' 'of' T ]" := [distrLatticeType of T for _]
  (at level 0, format "[ 'distrLatticeType' 'of' T ]") : form_scope.
Notation "[ 'distrLatticeType' 'of' T 'with' disp ]" :=
  [distrLatticeType of T for _ with disp]
  (at level 0, format "[ 'distrLatticeType' 'of' T 'with' disp ]") :
  form_scope.
End Exports.

End DistrLattice.
Export DistrLattice.Exports.

Module BDistrLattice.
Section ClassDef.

Record class_of (T : Type) := Class {
  base : DistrLattice.class_of T;
  mixin : BLattice.mixin_of base;
}.


Structure type (disp : unit) := Pack { sort; _ : class_of sort }.


Variables (T : Type) (disp : unit) (cT : type disp).

Definition class := let: Pack _ c as cT' := cT return class_of cT' in c.

Definition pack :=
  fun bT b & phant_id (@DistrLattice.class disp bT) b
  fun mT m & phant_id (@BLattice.class disp mT) (BLattice.Class m) ⇒
  Pack disp (@Class T b m).

Definition eqType := @Equality.Pack cT class.
Definition choiceType := @Choice.Pack cT class.
Definition porderType := @POrder.Pack disp cT class.
Definition latticeType := @Lattice.Pack disp cT class.
Definition bLatticeType := @BLattice.Pack disp cT class.
Definition distrLatticeType := @DistrLattice.Pack disp cT class.
Definition nb_distrLatticeType := @DistrLattice.Pack disp bLatticeType class.
End ClassDef.

Module Exports.
Coercion base : class_of >-> DistrLattice.class_of.
Coercion base2 : class_of >-> BLattice.class_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Coercion choiceType : type >-> Choice.type.
Coercion porderType : type >-> POrder.type.
Coercion latticeType : type >-> Lattice.type.
Coercion bLatticeType : type >-> BLattice.type.
Coercion distrLatticeType : type >-> DistrLattice.type.
Canonical eqType.
Canonical choiceType.
Canonical porderType.
Canonical latticeType.
Canonical bLatticeType.
Canonical distrLatticeType.
Canonical nb_distrLatticeType.
Notation bDistrLatticeType := type.
Notation "[ 'bDistrLatticeType' 'of' T ]" := (@pack T _ _ _ id _ _ id)
  (at level 0, format "[ 'bDistrLatticeType' 'of' T ]") : form_scope.
End Exports.

End BDistrLattice.
Export BDistrLattice.Exports.

Module TBDistrLattice.
Section ClassDef.

Record class_of (T : Type) := Class {
  base : BDistrLattice.class_of T;
  mixin : TBLattice.mixin_of base;
}.


Structure type (disp : unit) := Pack { sort; _ : class_of sort }.


Variables (T : Type) (disp : unit) (cT : type disp).

Definition class := let: Pack _ c as cT' := cT return class_of cT' in c.

Definition pack :=
  fun bT (b : BDistrLattice.class_of T)
      & phant_id (@BDistrLattice.class disp bT) b
  fun mT m & phant_id (@TBLattice.class disp mT) (@TBLattice.Class _ b m) ⇒
  Pack disp (@Class T b m).

Definition eqType := @Equality.Pack cT class.
Definition choiceType := @Choice.Pack cT class.
Definition porderType := @POrder.Pack disp cT class.
Definition latticeType := @Lattice.Pack disp cT class.
Definition bLatticeType := @BLattice.Pack disp cT class.
Definition tbLatticeType := @TBLattice.Pack disp cT class.
Definition distrLatticeType := @DistrLattice.Pack disp cT class.
Definition bDistrLatticeType := @BDistrLattice.Pack disp cT class.
Definition ntb_distrLatticeType := @DistrLattice.Pack disp tbLatticeType class.
Definition ntb_bDistrLatticeType :=
  @BDistrLattice.Pack disp tbLatticeType class.
End ClassDef.

Module Exports.
Coercion base : class_of >-> BDistrLattice.class_of.
Coercion base2 : class_of >-> TBLattice.class_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Coercion choiceType : type >-> Choice.type.
Coercion porderType : type >-> POrder.type.
Coercion latticeType : type >-> Lattice.type.
Coercion bLatticeType : type >-> BLattice.type.
Coercion tbLatticeType : type >-> TBLattice.type.
Coercion distrLatticeType : type >-> DistrLattice.type.
Coercion bDistrLatticeType : type >-> BDistrLattice.type.
Canonical eqType.
Canonical choiceType.
Canonical porderType.
Canonical latticeType.
Canonical bLatticeType.
Canonical tbLatticeType.
Canonical distrLatticeType.
Canonical bDistrLatticeType.
Canonical ntb_distrLatticeType.
Canonical ntb_bDistrLatticeType.
Notation tbDistrLatticeType := type.
Notation "[ 'tbDistrLatticeType' 'of' T ]" := (@pack T _ _ _ id _ _ id)
  (at level 0, format "[ 'tbDistrLatticeType' 'of' T ]") : form_scope.
End Exports.

End TBDistrLattice.
Export TBDistrLattice.Exports.

Module CBDistrLattice.
Section ClassDef.

Record mixin_of (T0 : Type) (b : BDistrLattice.class_of T0)
                (T := BDistrLattice.Pack tt b) := Mixin {
  sub : T T T;
  _ : x y, y `&` sub x y = bottom;
  _ : x y, (x `&` y) `|` sub x y = x
}.

Record class_of (T : Type) := Class {
  base : BDistrLattice.class_of T;
  mixin : mixin_of base;
}.


Structure type (disp : unit) := Pack { sort; _ : class_of sort }.


Variables (T : Type) (disp : unit) (cT : type disp).

Definition class := let: Pack _ c as cT' := cT return class_of cT' in c.
Definition clone c of phant_id class c := @Pack disp T c.
Definition clone_with disp' c of phant_id class c := @Pack disp' T c.

Definition pack :=
  fun bT b & phant_id (@BDistrLattice.class disp bT) b
  fun mPack disp (@Class T b m).

Definition eqType := @Equality.Pack cT class.
Definition choiceType := @Choice.Pack cT class.
Definition porderType := @POrder.Pack disp cT class.
Definition latticeType := @Lattice.Pack disp cT class.
Definition bLatticeType := @BLattice.Pack disp cT class.
Definition distrLatticeType := @DistrLattice.Pack disp cT class.
Definition bDistrLatticeType := @BDistrLattice.Pack disp cT class.
End ClassDef.

Module Exports.
Coercion base : class_of >-> BDistrLattice.class_of.
Coercion mixin : class_of >-> mixin_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Coercion choiceType : type >-> Choice.type.
Coercion porderType : type >-> POrder.type.
Coercion latticeType : type >-> Lattice.type.
Coercion bLatticeType : type >-> BLattice.type.
Coercion distrLatticeType : type >-> DistrLattice.type.
Coercion bDistrLatticeType : type >-> BDistrLattice.type.
Canonical eqType.
Canonical choiceType.
Canonical porderType.
Canonical latticeType.
Canonical bLatticeType.
Canonical distrLatticeType.
Canonical bDistrLatticeType.
Notation cbDistrLatticeType := type.
Notation CBDistrLatticeType T m := (@pack T _ _ _ id m).
Notation "[ 'cbDistrLatticeType' 'of' T 'for' cT ]" := (@clone T _ cT _ id)
  (at level 0, format "[ 'cbDistrLatticeType' 'of' T 'for' cT ]") :
  form_scope.
Notation "[ 'cbDistrLatticeType' 'of' T 'for' cT 'with' disp ]" :=
  (@clone_with T _ cT disp _ id)
  (at level 0,
   format "[ 'cbDistrLatticeType' 'of' T 'for' cT 'with' disp ]") :
  form_scope.
Notation "[ 'cbDistrLatticeType' 'of' T ]" := [cbDistrLatticeType of T for _]
  (at level 0, format "[ 'cbDistrLatticeType' 'of' T ]") : form_scope.
Notation "[ 'cbDistrLatticeType' 'of' T 'with' disp ]" :=
  [cbDistrLatticeType of T for _ with disp]
  (at level 0, format "[ 'cbDistrLatticeType' 'of' T 'with' disp ]") :
  form_scope.
End Exports.

End CBDistrLattice.
Export CBDistrLattice.Exports.

Definition sub {disp : unit} {T : cbDistrLatticeType disp} : T T T :=
  CBDistrLattice.sub (CBDistrLattice.class T).

Module Import CBDistrLatticeSyntax.
Notation "x `\` y" := (sub x y) : order_scope.
End CBDistrLatticeSyntax.

Module CTBDistrLattice.
Section ClassDef.

Record mixin_of (T0 : Type) (b : TBDistrLattice.class_of T0)
                (T := TBDistrLattice.Pack tt b) (sub : T T T) := Mixin {
  compl : T T;
  _ : x, compl x = sub top x
}.

Record class_of (T : Type) := Class {
  base : TBDistrLattice.class_of T;
  mixin1 : CBDistrLattice.mixin_of base;
  mixin2 : @mixin_of _ base (CBDistrLattice.sub mixin1);
}.


Structure type (disp : unit) := Pack { sort; _ : class_of sort }.


Variables (T : Type) (disp : unit) (cT : type disp).

Definition class := let: Pack _ c as cT' := cT return class_of cT' in c.
Definition clone c of phant_id class c := @Pack disp T c.
Definition clone_with disp' c of phant_id class c := @Pack disp' T c.

Definition pack :=
  fun bT b & phant_id (@TBDistrLattice.class disp bT) b
  fun mT m0 & phant_id (@CBDistrLattice.class disp mT) (CBDistrLattice.Class m0) ⇒
  fun m1Pack disp (@Class T b m0 m1).

Definition eqType := @Equality.Pack cT class.
Definition choiceType := @Choice.Pack cT class.
Definition porderType := @POrder.Pack disp cT class.
Definition latticeType := @Lattice.Pack disp cT class.
Definition bLatticeType := @BLattice.Pack disp cT class.
Definition tbLatticeType := @TBLattice.Pack disp cT class.
Definition distrLatticeType := @DistrLattice.Pack disp cT class.
Definition bDistrLatticeType := @BDistrLattice.Pack disp cT class.
Definition tbDistrLatticeType := @TBDistrLattice.Pack disp cT class.
Definition cbDistrLatticeType := @CBDistrLattice.Pack disp cT class.
Definition cb_tbLatticeType := @TBLattice.Pack disp cbDistrLatticeType class.
Definition cb_tbDistrLatticeType :=
  @TBDistrLattice.Pack disp cbDistrLatticeType class.
End ClassDef.

Module Exports.
Coercion base : class_of >-> TBDistrLattice.class_of.
Coercion base2 : class_of >-> CBDistrLattice.class_of.
Coercion mixin1 : class_of >-> CBDistrLattice.mixin_of.
Coercion mixin2 : class_of >-> mixin_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Coercion choiceType : type >-> Choice.type.
Coercion porderType : type >-> POrder.type.
Coercion latticeType : type >-> Lattice.type.
Coercion bLatticeType : type >-> BLattice.type.
Coercion tbLatticeType : type >-> TBLattice.type.
Coercion distrLatticeType : type >-> DistrLattice.type.
Coercion bDistrLatticeType : type >-> BDistrLattice.type.
Coercion tbDistrLatticeType : type >-> TBDistrLattice.type.
Coercion cbDistrLatticeType : type >-> CBDistrLattice.type.
Canonical eqType.
Canonical choiceType.
Canonical porderType.
Canonical latticeType.
Canonical bLatticeType.
Canonical tbLatticeType.
Canonical distrLatticeType.
Canonical bDistrLatticeType.
Canonical tbDistrLatticeType.
Canonical cbDistrLatticeType.
Canonical cb_tbLatticeType.
Canonical cb_tbDistrLatticeType.
Notation ctbDistrLatticeType := type.
Notation CTBDistrLatticeType T m := (@pack T _ _ _ id _ _ id m).
Notation "[ 'ctbDistrLatticeType' 'of' T 'for' cT ]" := (@clone T _ cT _ id)
  (at level 0, format "[ 'ctbDistrLatticeType' 'of' T 'for' cT ]") :
  form_scope.
Notation "[ 'ctbDistrLatticeType' 'of' T 'for' cT 'with' disp ]" :=
  (@clone_with T _ cT disp _ id)
  (at level 0,
   format "[ 'ctbDistrLatticeType' 'of' T 'for' cT 'with' disp ]")
  : form_scope.
Notation "[ 'ctbDistrLatticeType' 'of' T ]" := [ctbDistrLatticeType of T for _]
  (at level 0, format "[ 'ctbDistrLatticeType' 'of' T ]") : form_scope.
Notation "[ 'ctbDistrLatticeType' 'of' T 'with' disp ]" :=
  [ctbDistrLatticeType of T for _ with disp]
  (at level 0, format "[ 'ctbDistrLatticeType' 'of' T 'with' disp ]") :
  form_scope.
Notation "[ 'default_ctbDistrLatticeType' 'of' T ]" :=
  (@pack T _ _ _ id _ _ id (Mixin (fun erefl)))
  (at level 0, format "[ 'default_ctbDistrLatticeType' 'of' T ]") :
  form_scope.
End Exports.

End CTBDistrLattice.
Export CTBDistrLattice.Exports.

Definition compl {disp : unit} {T : ctbDistrLatticeType disp} : T T :=
  CTBDistrLattice.compl (CTBDistrLattice.class T).

Module Import CTBDistrLatticeSyntax.
Notation "~` A" := (compl A) : order_scope.
End CTBDistrLatticeSyntax.

Module Total.
Section ClassDef.

Definition mixin_of T0 (b : POrder.class_of T0) (T := POrder.Pack tt b) :=
  total (<=%O : rel T).

Record class_of (T : Type) := Class {
  base : DistrLattice.class_of T;
  mixin : mixin_of base;
}.


Structure type (disp : unit) := Pack { sort; _ : class_of sort }.


Variables (T : Type) (disp : unit) (cT : type disp).

Definition class := let: Pack _ c as cT' := cT return class_of cT' in c.
Definition clone c & phant_id class c := @Pack disp T c.
Definition clone_with disp' c & phant_id class c := @Pack disp' T c.

Definition pack :=
  fun bT b & phant_id (@DistrLattice.class disp bT) b
  fun mPack disp (@Class T b m).

Definition eqType := @Equality.Pack cT class.
Definition choiceType := @Choice.Pack cT class.
Definition porderType := @POrder.Pack disp cT class.
Definition latticeType := @Lattice.Pack disp cT class.
Definition distrLatticeType := @DistrLattice.Pack disp cT class.

End ClassDef.

Module Exports.
Coercion base : class_of >-> DistrLattice.class_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Coercion choiceType : type >-> Choice.type.
Coercion porderType : type >-> POrder.type.
Coercion latticeType : type >-> Lattice.type.
Coercion distrLatticeType : type >-> DistrLattice.type.
Canonical eqType.
Canonical choiceType.
Canonical porderType.
Canonical latticeType.
Canonical distrLatticeType.
Notation orderType := type.
Notation OrderType T m := (@pack T _ _ _ id m).
Notation "[ 'orderType' 'of' T 'for' cT ]" := (@clone T _ cT _ id)
  (at level 0, format "[ 'orderType' 'of' T 'for' cT ]") : form_scope.
Notation "[ 'orderType' 'of' T 'for' cT 'with' disp ]" :=
  (@clone_with T _ cT disp _ id)
  (at level 0, format "[ 'orderType' 'of' T 'for' cT 'with' disp ]") :
  form_scope.
Notation "[ 'orderType' 'of' T ]" := [orderType of T for _]
  (at level 0, format "[ 'orderType' 'of' T ]") : form_scope.
Notation "[ 'orderType' 'of' T 'with' disp ]" :=
  [orderType of T for _ with disp]
  (at level 0, format "[ 'orderType' 'of' T 'with' disp ]") : form_scope.
End Exports.

End Total.
Import Total.Exports.

FINITE

Module FinPOrder.
Section ClassDef.

Record class_of (T : Type) := Class {
  base : POrder.class_of T;
  mixin : Finite.mixin_of (Equality.Pack base)
}.


Structure type (disp : unit) := Pack { sort; _ : class_of sort }.


Variables (T : Type) (disp : unit) (cT : type disp).

Definition class := let: Pack _ c as cT' := cT return class_of cT' in c.

Definition pack :=
  fun bT b & phant_id (@POrder.class disp bT) b
  fun mT m & phant_id (@Finite.class mT) (@Finite.Class _ _ m) ⇒
  Pack disp (@Class T b m).

Definition eqType := @Equality.Pack cT class.
Definition choiceType := @Choice.Pack cT class.
Definition countType := @Countable.Pack cT class.
Definition finType := @Finite.Pack cT class.
Definition porderType := @POrder.Pack disp cT class.
Definition count_porderType := @POrder.Pack disp countType class.
Definition fin_porderType := @POrder.Pack disp finType class.
End ClassDef.

Module Exports.
Coercion base : class_of >-> POrder.class_of.
Coercion base2 : class_of >-> Finite.class_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Coercion choiceType : type >-> Choice.type.
Coercion countType : type >-> Countable.type.
Coercion finType : type >-> Finite.type.
Coercion porderType : type >-> POrder.type.
Canonical eqType.
Canonical choiceType.
Canonical countType.
Canonical finType.
Canonical porderType.
Canonical count_porderType.
Canonical fin_porderType.
Notation finPOrderType := type.
Notation "[ 'finPOrderType' 'of' T ]" := (@pack T _ _ _ id _ _ id)
  (at level 0, format "[ 'finPOrderType' 'of' T ]") : form_scope.
End Exports.

End FinPOrder.
Import FinPOrder.Exports.

Module FinLattice.
Section ClassDef.

Record class_of (T : Type) := Class {
  base : TBLattice.class_of T;
  mixin : Finite.mixin_of (Equality.Pack base);
}.


Structure type (disp : unit) := Pack { sort; _ : class_of sort }.


Variables (T : Type) (disp : unit) (cT : type disp).

Definition class := let: Pack _ c as cT' := cT return class_of cT' in c.

Definition pack :=
  fun bT b & phant_id (@TBLattice.class disp bT) b
  fun mT m & phant_id (@Finite.class mT) (@Finite.Class _ _ m) ⇒
  Pack disp (@Class T b m).

Definition eqType := @Equality.Pack cT class.
Definition choiceType := @Choice.Pack cT class.
Definition countType := @Countable.Pack cT class.
Definition finType := @Finite.Pack cT class.
Definition porderType := @POrder.Pack disp cT class.
Definition finPOrderType := @FinPOrder.Pack disp cT class.
Definition latticeType := @Lattice.Pack disp cT class.
Definition bLatticeType := @BLattice.Pack disp cT class.
Definition tbLatticeType := @TBLattice.Pack disp cT class.
Definition count_latticeType := @Lattice.Pack disp countType class.
Definition count_bLatticeType := @BLattice.Pack disp countType class.
Definition count_tbLatticeType := @TBLattice.Pack disp countType class.
Definition fin_latticeType := @Lattice.Pack disp finType class.
Definition fin_bLatticeType := @BLattice.Pack disp finType class.
Definition fin_tbLatticeType := @TBLattice.Pack disp finType class.
Definition finPOrder_latticeType := @Lattice.Pack disp finPOrderType class.
Definition finPOrder_bLatticeType := @BLattice.Pack disp finPOrderType class.
Definition finPOrder_tbLatticeType := @TBLattice.Pack disp finPOrderType class.

End ClassDef.

Module Exports.
Coercion base : class_of >-> TBLattice.class_of.
Coercion base2 : class_of >-> FinPOrder.class_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Coercion choiceType : type >-> Choice.type.
Coercion countType : type >-> Countable.type.
Coercion finType : type >-> Finite.type.
Coercion porderType : type >-> POrder.type.
Coercion finPOrderType : type >-> FinPOrder.type.
Coercion latticeType : type >-> Lattice.type.
Coercion bLatticeType : type >-> BLattice.type.
Coercion tbLatticeType : type >-> TBLattice.type.
Canonical eqType.
Canonical choiceType.
Canonical countType.
Canonical finType.
Canonical porderType.
Canonical finPOrderType.
Canonical latticeType.
Canonical bLatticeType.
Canonical tbLatticeType.
Canonical count_latticeType.
Canonical count_bLatticeType.
Canonical count_tbLatticeType.
Canonical fin_latticeType.
Canonical fin_bLatticeType.
Canonical fin_tbLatticeType.
Canonical finPOrder_latticeType.
Canonical finPOrder_bLatticeType.
Canonical finPOrder_tbLatticeType.
Notation finLatticeType := type.
Notation "[ 'finLatticeType' 'of' T ]" := (@pack T _ _ _ id _ _ id)
  (at level 0, format "[ 'finLatticeType' 'of' T ]") : form_scope.
End Exports.

End FinLattice.
Export FinLattice.Exports.

Module FinDistrLattice.
Section ClassDef.

Record class_of (T : Type) := Class {
  base : TBDistrLattice.class_of T;
  mixin : Finite.mixin_of (Equality.Pack base);
}.


Structure type (disp : unit) := Pack { sort; _ : class_of sort }.


Variables (T : Type) (disp : unit) (cT : type disp).

Definition class := let: Pack _ c as cT' := cT return class_of cT' in c.

Definition pack :=
  fun bT b & phant_id (@TBDistrLattice.class disp bT) b
  fun mT m & phant_id (@Finite.class mT) (@Finite.Class _ _ m) ⇒
  Pack disp (@Class T b m).

Definition eqType := @Equality.Pack cT class.
Definition choiceType := @Choice.Pack cT class.
Definition countType := @Countable.Pack cT class.
Definition finType := @Finite.Pack cT class.
Definition porderType := @POrder.Pack disp cT class.
Definition finPOrderType := @FinPOrder.Pack disp cT class.
Definition latticeType := @Lattice.Pack disp cT class.
Definition bLatticeType := @BLattice.Pack disp cT class.
Definition tbLatticeType := @TBLattice.Pack disp cT class.
Definition finLatticeType := @FinLattice.Pack disp cT class.
Definition distrLatticeType := @DistrLattice.Pack disp cT class.
Definition bDistrLatticeType := @BDistrLattice.Pack disp cT class.
Definition tbDistrLatticeType := @TBDistrLattice.Pack disp cT class.
Definition count_distrLatticeType := @DistrLattice.Pack disp countType class.
Definition count_bDistrLatticeType := @BDistrLattice.Pack disp countType class.
Definition count_tbDistrLatticeType :=
  @TBDistrLattice.Pack disp countType class.
Definition fin_distrLatticeType := @DistrLattice.Pack disp finType class.
Definition fin_bDistrLatticeType := @BDistrLattice.Pack disp finType class.
Definition fin_tbDistrLatticeType := @TBDistrLattice.Pack disp finType class.
Definition finPOrder_distrLatticeType :=
  @DistrLattice.Pack disp finPOrderType class.
Definition finPOrder_bDistrLatticeType :=
  @BDistrLattice.Pack disp finPOrderType class.
Definition finPOrder_tbDistrLatticeType :=
  @TBDistrLattice.Pack disp finPOrderType class.
Definition finLattice_distrLatticeType :=
  @DistrLattice.Pack disp finLatticeType class.
Definition finLattice_bDistrLatticeType :=
  @BDistrLattice.Pack disp finLatticeType class.
Definition finLattice_tbDistrLatticeType :=
  @TBDistrLattice.Pack disp finLatticeType class.

End ClassDef.

Module Exports.
Coercion base : class_of >-> TBDistrLattice.class_of.
Coercion base2 : class_of >-> FinLattice.class_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Coercion choiceType : type >-> Choice.type.
Coercion countType : type >-> Countable.type.
Coercion finType : type >-> Finite.type.
Coercion porderType : type >-> POrder.type.
Coercion finPOrderType : type >-> FinPOrder.type.
Coercion latticeType : type >-> Lattice.type.
Coercion bLatticeType : type >-> BLattice.type.
Coercion tbLatticeType : type >-> TBLattice.type.
Coercion finLatticeType : type >-> FinLattice.type.
Coercion distrLatticeType : type >-> DistrLattice.type.
Coercion bDistrLatticeType : type >-> BDistrLattice.type.
Coercion tbDistrLatticeType : type >-> TBDistrLattice.type.
Canonical eqType.
Canonical choiceType.
Canonical countType.
Canonical finType.
Canonical porderType.
Canonical finPOrderType.
Canonical latticeType.
Canonical bLatticeType.
Canonical tbLatticeType.
Canonical finLatticeType.
Canonical distrLatticeType.
Canonical bDistrLatticeType.
Canonical tbDistrLatticeType.
Canonical count_distrLatticeType.
Canonical count_bDistrLatticeType.
Canonical count_tbDistrLatticeType.
Canonical fin_distrLatticeType.
Canonical fin_bDistrLatticeType.
Canonical fin_tbDistrLatticeType.
Canonical finPOrder_distrLatticeType.
Canonical finPOrder_bDistrLatticeType.
Canonical finPOrder_tbDistrLatticeType.
Canonical finLattice_distrLatticeType.
Canonical finLattice_bDistrLatticeType.
Canonical finLattice_tbDistrLatticeType.
Notation finDistrLatticeType := type.
Notation "[ 'finDistrLatticeType' 'of' T ]" := (@pack T _ _ _ id _ _ id)
  (at level 0, format "[ 'finDistrLatticeType' 'of' T ]") : form_scope.
End Exports.

End FinDistrLattice.
Export FinDistrLattice.Exports.

Module FinCDistrLattice.
Section ClassDef.

Record class_of (T : Type) := Class {
  base : CTBDistrLattice.class_of T;
  mixin : Finite.mixin_of (Equality.Pack base);
}.


Structure type (disp : unit) := Pack { sort; _ : class_of sort }.


Variables (T : Type) (disp : unit) (cT : type disp).

Definition class := let: Pack _ c as cT' := cT return class_of cT' in c.

Definition pack :=
  fun bT b & phant_id (@CTBDistrLattice.class disp bT) b
  fun mT m & phant_id (@Finite.class mT) (@Finite.Class _ _ m) ⇒
  Pack disp (@Class T b m).

Definition eqType := @Equality.Pack cT class.
Definition choiceType := @Choice.Pack cT class.
Definition countType := @Countable.Pack cT class.
Definition finType := @Finite.Pack cT class.
Definition porderType := @POrder.Pack disp cT class.
Definition finPOrderType := @FinPOrder.Pack disp cT class.
Definition latticeType := @Lattice.Pack disp cT class.
Definition bLatticeType := @BLattice.Pack disp cT class.
Definition tbLatticeType := @TBLattice.Pack disp cT class.
Definition finLatticeType := @FinLattice.Pack disp cT class.
Definition distrLatticeType := @DistrLattice.Pack disp cT class.
Definition bDistrLatticeType := @BDistrLattice.Pack disp cT class.
Definition tbDistrLatticeType := @TBDistrLattice.Pack disp cT class.
Definition finDistrLatticeType := @FinDistrLattice.Pack disp cT class.
Definition cbDistrLatticeType := @CBDistrLattice.Pack disp cT class.
Definition ctbDistrLatticeType := @CTBDistrLattice.Pack disp cT class.
Definition count_cbDistrLatticeType :=
  @CBDistrLattice.Pack disp countType class.
Definition count_ctbDistrLatticeType :=
  @CTBDistrLattice.Pack disp countType class.
Definition fin_cbDistrLatticeType := @CBDistrLattice.Pack disp finType class.
Definition fin_ctbDistrLatticeType := @CTBDistrLattice.Pack disp finType class.
Definition finPOrder_cbDistrLatticeType :=
  @CBDistrLattice.Pack disp finPOrderType class.
Definition finPOrder_ctbDistrLatticeType :=
  @CTBDistrLattice.Pack disp finPOrderType class.
Definition finLattice_cbDistrLatticeType :=
  @CBDistrLattice.Pack disp finLatticeType class.
Definition finLattice_ctbDistrLatticeType :=
  @CTBDistrLattice.Pack disp finLatticeType class.
Definition finDistrLattice_cbDistrLatticeType :=
  @CBDistrLattice.Pack disp finDistrLatticeType class.
Definition finDistrLattice_ctbDistrLatticeType :=
  @CTBDistrLattice.Pack disp finDistrLatticeType class.

End ClassDef.

Module Exports.
Coercion base : class_of >-> CTBDistrLattice.class_of.
Coercion base2 : class_of >-> FinDistrLattice.class_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Coercion choiceType : type >-> Choice.type.
Coercion countType : type >-> Countable.type.
Coercion finType : type >-> Finite.type.
Coercion porderType : type >-> POrder.type.
Coercion finPOrderType : type >-> FinPOrder.type.
Coercion latticeType : type >-> Lattice.type.
Coercion bLatticeType : type >-> BLattice.type.
Coercion tbLatticeType : type >-> TBLattice.type.
Coercion finLatticeType : type >-> FinLattice.type.
Coercion distrLatticeType : type >-> DistrLattice.type.
Coercion bDistrLatticeType : type >-> BDistrLattice.type.
Coercion tbDistrLatticeType : type >-> TBDistrLattice.type.
Coercion finDistrLatticeType : type >-> FinDistrLattice.type.
Coercion cbDistrLatticeType : type >-> CBDistrLattice.type.
Coercion ctbDistrLatticeType : type >-> CTBDistrLattice.type.
Canonical eqType.
Canonical choiceType.
Canonical countType.
Canonical finType.
Canonical porderType.
Canonical finPOrderType.
Canonical latticeType.
Canonical bLatticeType.
Canonical tbLatticeType.
Canonical finLatticeType.
Canonical distrLatticeType.
Canonical bDistrLatticeType.
Canonical tbDistrLatticeType.
Canonical finDistrLatticeType.
Canonical cbDistrLatticeType.
Canonical ctbDistrLatticeType.
Canonical count_cbDistrLatticeType.
Canonical count_ctbDistrLatticeType.
Canonical fin_cbDistrLatticeType.
Canonical fin_ctbDistrLatticeType.
Canonical finPOrder_cbDistrLatticeType.
Canonical finPOrder_ctbDistrLatticeType.
Canonical finLattice_cbDistrLatticeType.
Canonical finLattice_ctbDistrLatticeType.
Canonical finDistrLattice_cbDistrLatticeType.
Canonical finDistrLattice_ctbDistrLatticeType.
Notation finCDistrLatticeType := type.
Notation "[ 'finCDistrLatticeType' 'of' T ]" := (@pack T _ _ _ id _ _ id)
  (at level 0, format "[ 'finCDistrLatticeType' 'of' T ]") : form_scope.
End Exports.

End FinCDistrLattice.
Export FinCDistrLattice.Exports.

Module FinTotal.
Section ClassDef.

Record class_of (T : Type) := Class {
  base : FinDistrLattice.class_of T;
  mixin : Total.mixin_of base;
}.


Structure type (disp : unit) := Pack { sort; _ : class_of sort }.


Variables (T : Type) (disp : unit) (cT : type disp).

Definition class := let: Pack _ c as cT' := cT return class_of cT' in c.

Definition pack :=
  fun bT b & phant_id (@FinDistrLattice.class disp bT) b
  fun mT m & phant_id (@Total.class disp mT) (Total.Class m) ⇒
  Pack disp (@Class T b m).

Definition eqType := @Equality.Pack cT class.
Definition choiceType := @Choice.Pack cT class.
Definition countType := @Countable.Pack cT class.
Definition finType := @Finite.Pack cT class.
Definition porderType := @POrder.Pack disp cT class.
Definition finPOrderType := @FinPOrder.Pack disp cT class.
Definition latticeType := @Lattice.Pack disp cT class.
Definition bLatticeType := @BLattice.Pack disp cT class.
Definition tbLatticeType := @TBLattice.Pack disp cT class.
Definition finLatticeType := @FinLattice.Pack disp cT class.
Definition distrLatticeType := @DistrLattice.Pack disp cT class.
Definition bDistrLatticeType := @BDistrLattice.Pack disp cT class.
Definition tbDistrLatticeType := @TBDistrLattice.Pack disp cT class.
Definition finDistrLatticeType := @FinDistrLattice.Pack disp cT class.
Definition orderType := @Total.Pack disp cT class.
Definition order_countType := @Countable.Pack orderType class.
Definition order_finType := @Finite.Pack orderType class.
Definition order_finPOrderType := @FinPOrder.Pack disp orderType class.
Definition order_bLatticeType := @BLattice.Pack disp orderType class.
Definition order_tbLatticeType := @TBLattice.Pack disp orderType class.
Definition order_finLatticeType := @FinLattice.Pack disp orderType class.
Definition order_bDistrLatticeType := @BDistrLattice.Pack disp orderType class.
Definition order_tbDistrLatticeType :=
  @TBDistrLattice.Pack disp orderType class.
Definition order_finDistrLatticeType :=
  @FinDistrLattice.Pack disp orderType class.

End ClassDef.

Module Exports.
Coercion base : class_of >-> FinDistrLattice.class_of.
Coercion base2 : class_of >-> Total.class_of.
Coercion sort : type >-> Sortclass.
Coercion eqType : type >-> Equality.type.
Coercion choiceType : type >-> Choice.type.
Coercion countType : type >-> Countable.type.
Coercion finType : type >-> Finite.type.
Coercion porderType : type >-> POrder.type.
Coercion finPOrderType : type >-> FinPOrder.type.
Coercion latticeType : type >-> Lattice.type.
Coercion bLatticeType : type >-> BLattice.type.
Coercion tbLatticeType : type >-> TBLattice.type.
Coercion finLatticeType : type >-> FinLattice.type.
Coercion distrLatticeType : type >-> DistrLattice.type.
Coercion bDistrLatticeType : type >-> BDistrLattice.type.
Coercion tbDistrLatticeType : type >-> TBDistrLattice.type.
Coercion finDistrLatticeType : type >-> FinDistrLattice.type.
Coercion orderType : type >-> Total.type.
Canonical eqType.
Canonical choiceType.
Canonical countType.
Canonical finType.
Canonical porderType.
Canonical finPOrderType.
Canonical latticeType.
Canonical bLatticeType.
Canonical tbLatticeType.
Canonical finLatticeType.
Canonical distrLatticeType.
Canonical bDistrLatticeType.
Canonical tbDistrLatticeType.
Canonical finDistrLatticeType.
Canonical orderType.
Canonical order_countType.
Canonical order_finType.
Canonical order_finPOrderType.
Canonical order_bLatticeType.
Canonical order_tbLatticeType.
Canonical order_finLatticeType.
Canonical order_bDistrLatticeType.
Canonical order_tbDistrLatticeType.
Canonical order_finDistrLatticeType.
Notation finOrderType := type.
Notation "[ 'finOrderType' 'of' T ]" := (@pack T _ _ _ id _ _ id)
  (at level 0, format "[ 'finOrderType' 'of' T ]") : form_scope.
End Exports.

End FinTotal.
Export FinTotal.Exports.

DUAL

Definition dual T : Type := T.
Definition dual_display : unit unit.

Notation dual_le := (@le (dual_display _) _).
Notation dual_lt := (@lt (dual_display _) _).
Notation dual_comparable := (@comparable (dual_display _) _).
Notation dual_ge := (@ge (dual_display _) _).
Notation dual_gt := (@gt (dual_display _) _).
Notation dual_leif := (@leif (dual_display _) _).
Notation dual_lteif := (@lteif (dual_display _) _).
Notation dual_max := (@max (dual_display _) _).
Notation dual_min := (@min (dual_display _) _).
Notation dual_meet := (@meet (dual_display _) _).
Notation dual_join := (@join (dual_display _) _).
Notation dual_bottom := (@bottom (dual_display _) _).
Notation dual_top := (@top (dual_display _) _).

Module Import DualSyntax.

Notation "T ^d" := (dual T) (at level 2, format "T ^d") : type_scope.
Notation "<=^d%O" := dual_le : fun_scope.
Notation ">=^d%O" := dual_ge : fun_scope.
Notation "<^d%O" := dual_lt : fun_scope.
Notation ">^d%O" := dual_gt : fun_scope.
Notation "<?=^d%O" := dual_leif : fun_scope.
Notation "<?<=^d%O" := dual_lteif : fun_scope.
Notation ">=<^d%O" := dual_comparable : fun_scope.
Notation "><^d%O" := (fun x y~~ dual_comparable x y) : fun_scope.

Notation "<=^d y" := (>=^d%O y) : order_scope.
Notation "<=^d y :> T" := (<=^d (y : T)) (only parsing) : order_scope.
Notation ">=^d y" := (<=^d%O y) : order_scope.
Notation ">=^d y :> T" := (>=^d (y : T)) (only parsing) : order_scope.

Notation "<^d y" := (>^d%O y) : order_scope.
Notation "<^d y :> T" := (<^d (y : T)) (only parsing) : order_scope.
Notation ">^d y" := (<^d%O y) : order_scope.
Notation ">^d y :> T" := (>^d (y : T)) (only parsing) : order_scope.

Notation "x <=^d y" := (<=^d%O x y) : order_scope.
Notation "x <=^d y :> T" := ((x : T) <=^d (y : T)) (only parsing) : order_scope.
Notation "x >=^d y" := (y <=^d x) (only parsing) : order_scope.
Notation "x >=^d y :> T" := ((x : T) >=^d (y : T)) (only parsing) : order_scope.

Notation "x <^d y" := (<^d%O x y) : order_scope.
Notation "x <^d y :> T" := ((x : T) <^d (y : T)) (only parsing) : order_scope.
Notation "x >^d y" := (y <^d x) (only parsing) : order_scope.
Notation "x >^d y :> T" := ((x : T) >^d (y : T)) (only parsing) : order_scope.

Notation "x <=^d y <=^d z" := ((x <=^d y) && (y <=^d z)) : order_scope.
Notation "x <^d y <=^d z" := ((x <^d y) && (y <=^d z)) : order_scope.
Notation "x <=^d y <^d z" := ((x <=^d y) && (y <^d z)) : order_scope.
Notation "x <^d y <^d z" := ((x <^d y) && (y <^d z)) : order_scope.

Notation "x <=^d y ?= 'iff' C" := (<?=^d%O x y C) : order_scope.
Notation "x <=^d y ?= 'iff' C :> T" := ((x : T) <=^d (y : T) ?= iff C)
  (only parsing) : order_scope.

Notation "x <^d y ?<= 'if' C" := (<?<=^d%O x y C) : order_scope.
Notation "x <^d y ?<= 'if' C :> T" := ((x : T) <^d (y : T) ?<= if C)
  (only parsing) : order_scope.

Notation ">=<^d x" := (>=<^d%O x) : order_scope.
Notation ">=<^d y :> T" := (>=<^d (y : T)) (only parsing) : order_scope.
Notation "x >=<^d y" := (>=<^d%O x y) : order_scope.

Notation "><^d y" := [pred x | ~~ dual_comparable x y] : order_scope.
Notation "><^d y :> T" := (><^d (y : T)) (only parsing) : order_scope.
Notation "x ><^d y" := (~~ (><^d%O x y)) : order_scope.

Notation "x `&^d` y" := (dual_meet x y) : order_scope.
Notation "x `|^d` y" := (dual_join x y) : order_scope.

Notation "0^d" := dual_bottom : order_scope.
Notation "1^d" := dual_top : order_scope.

The following Local Notations are here to define the \join^d and \meet^d notations later. Do not remove them.

Notation "\join^d_ ( i <- r | P ) F" :=
  (\big[join/0]_(i <- r | P%B) F%O) : order_scope.
Notation "\join^d_ ( i <- r ) F" :=
  (\big[join/0]_(i <- r) F%O) : order_scope.
Notation "\join^d_ ( i | P ) F" :=
  (\big[join/0]_(i | P%B) F%O) : order_scope.
Notation "\join^d_ i F" :=
  (\big[join/0]_i F%O) : order_scope.
Notation "\join^d_ ( i : I | P ) F" :=
  (\big[join/0]_(i : I | P%B) F%O) (only parsing) : order_scope.
Notation "\join^d_ ( i : I ) F" :=
  (\big[join/0]_(i : I) F%O) (only parsing) : order_scope.
Notation "\join^d_ ( m <= i < n | P ) F" :=
 (\big[join/0]_(m i < n | P%B) F%O) : order_scope.
Notation "\join^d_ ( m <= i < n ) F" :=
 (\big[join/0]_(m i < n) F%O) : order_scope.
Notation "\join^d_ ( i < n | P ) F" :=
 (\big[join/0]_(i < n | P%B) F%O) : order_scope.
Notation "\join^d_ ( i < n ) F" :=
 (\big[join/0]_(i < n) F%O) : order_scope.
Notation "\join^d_ ( i 'in' A | P ) F" :=
 (\big[join/0]_(i in A | P%B) F%O) : order_scope.
Notation "\join^d_ ( i 'in' A ) F" :=
 (\big[join/0]_(i in A) F%O) : order_scope.

Notation "\meet^d_ ( i <- r | P ) F" :=
  (\big[meet/1]_(i <- r | P%B) F%O) : order_scope.
Notation "\meet^d_ ( i <- r ) F" :=
  (\big[meet/1]_(i <- r) F%O) : order_scope.
Notation "\meet^d_ ( i | P ) F" :=
  (\big[meet/1]_(i | P%B) F%O) : order_scope.
Notation "\meet^d_ i F" :=
  (\big[meet/1]_i F%O) : order_scope.
Notation "\meet^d_ ( i : I | P ) F" :=
  (\big[meet/1]_(i : I | P%B) F%O) (only parsing) : order_scope.
Notation "\meet^d_ ( i : I ) F" :=
  (\big[meet/1]_(i : I) F%O) (only parsing) : order_scope.
Notation "\meet^d_ ( m <= i < n | P ) F" :=
 (\big[meet/1]_(m i < n | P%B) F%O) : order_scope.
Notation "\meet^d_ ( m <= i < n ) F" :=
 (\big[meet/1]_(m i < n) F%O) : order_scope.
Notation "\meet^d_ ( i < n | P ) F" :=
 (\big[meet/1]_(i < n | P%B) F%O) : order_scope.
Notation "\meet^d_ ( i < n ) F" :=
 (\big[meet/1]_(i < n) F%O) : order_scope.
Notation "\meet^d_ ( i 'in' A | P ) F" :=
 (\big[meet/1]_(i in A | P%B) F%O) : order_scope.
Notation "\meet^d_ ( i 'in' A ) F" :=
 (\big[meet/1]_(i in A) F%O) : order_scope.

End DualSyntax.

THEORY

Module Import POrderTheory.
Section POrderTheory.

Context {disp : unit} {T : porderType disp}.

Implicit Types (x y : T) (s : seq T).

Lemma geE x y : ge x y = (y x).
Lemma gtE x y : gt x y = (y < x).

Lemma lexx (x : T) : x x.
Hint Resolve lexx : core.

Definition le_refl : reflexive le := lexx.
Definition ge_refl : reflexive ge := lexx.
Hint Resolve le_refl : core.

Lemma le_anti: antisymmetric (<=%O : rel T).

Lemma ge_anti: antisymmetric (>=%O : rel T).

Lemma le_trans: transitive (<=%O : rel T).

Lemma ge_trans: transitive (>=%O : rel T).

Lemma le_le_trans x y z t : z x y t x y z t.

Lemma lt_def x y: (x < y) = (y != x) && (x y).

Lemma lt_neqAle x y: (x < y) = (x != y) && (x y).

Lemma ltxx x: x < x = false.

Definition lt_irreflexive : irreflexive lt := ltxx.
Hint Resolve lt_irreflexive : core.

Definition ltexx := (lexx, ltxx).

Lemma le_eqVlt x y: (x y) = (x == y) || (x < y).

Lemma lt_eqF x y: x < y x == y = false.

Lemma gt_eqF x y : y < x x == y = false.

Lemma eq_le x y: (x == y) = (x y x).

Lemma ltW x y: x < y x y.

Lemma lt_le_trans y x z: x < y y z x < z.

Lemma lt_trans: transitive (<%O : rel T).

Lemma le_lt_trans y x z: x y y < z x < z.

Lemma lt_nsym x y : x < y y < x False.

Lemma lt_asym x y : x < y < x = false.

Lemma le_gtF x y: x y y < x = false.

Lemma lt_geF x y : (x < y) y x = false.

Definition lt_gtF x y hxy := le_gtF (@ltW x y hxy).

Lemma lt_leAnge x y : (x < y) = (x y) && ~~ (y x).

Lemma lt_le_asym x y : x < y x = false.

Lemma le_lt_asym x y : x y < x = false.

Definition lte_anti := (=^~ eq_le, lt_asym, lt_le_asym, le_lt_asym).

Lemma le_path_min x s : path <=%O x s all ( x) s.

Lemma lt_path_min x s : path <%O x s all (> x) s.

Lemma le_path_sortedE x s : path <=%O x s = all ( x) s && sorted <=%O s.

Lemma lt_path_sortedE x s : path <%O x s = all (> x) s && sorted <%O s.

Lemma le_sorted_pairwise s : sorted <=%O s = pairwise <=%O s.

Lemma lt_sorted_pairwise s : sorted <%O s = pairwise <%O s.

Lemma le_path_pairwise x s : path <=%O x s = pairwise <=%O (x :: s).

Lemma lt_path_pairwise x s : path <%O x s = pairwise <%O (x :: s).

Lemma lt_sorted_uniq_le s : sorted <%O s = uniq s && sorted <=%O s.

Lemma le_sorted_mask m s : sorted <=%O s sorted <=%O (mask m s).

Lemma lt_sorted_mask m s : sorted <%O s sorted <%O (mask m s).

Lemma le_sorted_filter a s : sorted <=%O s sorted <=%O (filter a s).

Lemma lt_sorted_filter a s : sorted <%O s sorted <%O (filter a s).

Lemma le_path_mask x m s : path <=%O x s path <=%O x (mask m s).

Lemma lt_path_mask x m s : path <%O x s path <%O x (mask m s).

Lemma le_path_filter x a s : path <=%O x s path <=%O x (filter a s).

Lemma lt_path_filter x a s : path <%O x s path <%O x (filter a s).

Lemma le_sorted_ltn_nth (x0 : T) (s : seq T) : sorted <=%O s
 {in [pred n | (n < size s)%N] &,
    {homo nth x0 s : i j / (i < j)%N >-> i j}}.

Lemma le_sorted_leq_nth (x0 : T) (s : seq T) : sorted <=%O s
  {in [pred n | (n < size s)%N] &,
    {homo nth x0 s : i j / (i j)%N >-> i j}}.

Lemma lt_sorted_leq_nth (x0 : T) (s : seq T) : sorted <%O s
  {in [pred n | (n < size s)%N] &,
    {mono nth x0 s : i j / (i j)%N >-> i j}}.

Lemma lt_sorted_ltn_nth (x0 : T) (s : seq T) : sorted <%O s
  {in [pred n | (n < size s)%N] &,
    {mono nth x0 s : i j / (i < j)%N >-> i < j}}.

Lemma subseq_le_path x s1 s2 : subseq s1 s2 path <=%O x s2 path <=%O x s1.

Lemma subseq_lt_path x s1 s2 : subseq s1 s2 path <%O x s2 path <%O x s1.

Lemma subseq_le_sorted s1 s2 : subseq s1 s2 sorted <=%O s2 sorted <=%O s1.

Lemma subseq_lt_sorted s1 s2 : subseq s1 s2 sorted <%O s2 sorted <%O s1.

Lemma lt_sorted_uniq s : sorted <%O s uniq s.

Lemma lt_sorted_eq s1 s2 :
  sorted <%O s1 sorted <%O s2 s1 =i s2 s1 = s2.

Lemma le_sorted_eq s1 s2 :
  sorted <=%O s1 sorted <=%O s2 perm_eq s1 s2 s1 = s2.

Lemma filter_lt_nth x0 s i : sorted <%O s (i < size s)%N
  [seq x <- s | x < nth x0 s i] = take i s.

Lemma count_lt_nth x0 s i : sorted <%O s (i < size s)%N
  count (< nth x0 s i) s = i.

Lemma filter_le_nth x0 s i : sorted <%O s (i < size s)%N
  [seq x <- s | x nth x0 s i] = take i.+1 s.

Lemma count_le_nth x0 s i : sorted <%O s (i < size s)%N
  count ( nth x0 s i) s = i.+1.

Lemma count_lt_le_mem x s : (count (< x) s < count ( x) s)%N = (x \in s).

Lemma sorted_filter_lt x s :
  sorted <=%O s [seq y <- s | y < x] = take (count (< x) s) s.

Lemma sorted_filter_le x s :
  sorted <=%O s [seq y <- s | y x] = take (count ( x) s) s.

Lemma nth_count_le x x0 s i : sorted <=%O s
  (i < count ( x) s)%N nth x0 s i x.

Lemma nth_count_lt x x0 s i : sorted <=%O s
  (i < count (< x) s)%N nth x0 s i < x.

Lemma sort_le_id s : sorted <=%O s sort <=%O s = s.

Lemma sort_lt_id s : sorted <%O s sort <%O s = s.

Lemma comparable_leNgt x y : x >=< y (x y) = ~~ (y < x).

Lemma comparable_ltNge x y : x >=< y (x < y) = ~~ (y x).

Lemma comparable_ltgtP x y : x >=< y
  compare x y (min y x) (min x y) (max y x) (max x y)
  (y == x) (x == y) (x y) (x y) (x > y) (x < y).

Lemma comparable_leP x y : x >=< y
  le_xor_gt x y (min y x) (min x y) (max y x) (max x y) (x y) (y < x).

Lemma comparable_ltP x y : x >=< y
  lt_xor_ge x y (min y x) (min x y) (max y x) (max x y) (y x) (x < y).

Lemma comparable_sym x y : (y >=< x) = (x >=< y).

Lemma comparablexx x : x >=< x.

Lemma incomparable_eqF x y : (x >< y) (x == y) = false.

Lemma incomparable_leF x y : (x >< y) (x y) = false.

Lemma incomparable_ltF x y : (x >< y) (x < y) = false.

Lemma comparableP x y : incompare x y
  (min y x) (min x y) (max y x) (max x y)
  (y == x) (x == y) (x y) (x y) (x > y) (x < y)
  (y >=< x) (x >=< y).

Lemma le_comparable (x y : T) : x y x >=< y.

Lemma lt_comparable (x y : T) : x < y x >=< y.

Lemma ge_comparable (x y : T) : y x x >=< y.

Lemma gt_comparable (x y : T) : y < x x >=< y.

leif

Lemma leifP x y C : reflect (x y ?= iff C) (if C then x == y else x < y).

Lemma leif_refl x C : reflect (x x ?= iff C) C.

Lemma leif_trans x1 x2 x3 C12 C23 :
  x1 x2 ?= iff C12 x2 x3 ?= iff C23 x1 x3 ?= iff C12 && C23.

Lemma leif_le x y : x y x y ?= iff (x y).

Lemma leif_eq x y : x y x y ?= iff (x == y).

Lemma ge_leif x y C : x y ?= iff C (y x) = C.

Lemma lt_leif x y C : x y ?= iff C (x < y) = ~~ C.

Lemma ltNleif x y C : x y ?= iff ~~ C (x < y) = C.

Lemma eq_leif x y C : x y ?= iff C (x == y) = C.

Lemma eqTleif x y C : x y ?= iff C C x = y.

lteif

Lemma lteif_trans x y z C1 C2 :
  x < y ?<= if C1 y < z ?<= if C2 x < z ?<= if C1 && C2.

Lemma lteif_anti C1 C2 x y :
  (x < y ?<= if C1) && (y < x ?<= if C2) = C1 && C2 && (x == y).

Lemma lteifxx x C : (x < x ?<= if C) = C.

Lemma lteifNF x y C : y < x ?<= if ~~ C x < y ?<= if C = false.

Lemma lteifS x y C : x < y x < y ?<= if C.

Lemma lteifT x y : x < y ?<= if true = (x y).

Lemma lteifF x y : x < y ?<= if false = (x < y).

Lemma lteif_orb x y : {morph lteif x y : p q / p || q}.

Lemma lteif_andb x y : {morph lteif x y : p q / p && q}.

Lemma lteif_imply C1 C2 x y : C1 ==> C2 x < y ?<= if C1 x < y ?<= if C2.

Lemma lteifW C x y : x < y ?<= if C x y.

Lemma ltrW_lteif C x y : x < y x < y ?<= if C.

Lemma lteifN C x y : x < y ?<= if ~~ C ~~ (y < x ?<= if C).

min and max

Lemma minElt x y : min x y = if x < y then x else y.
Lemma maxElt x y : max x y = if x < y then y else x.

Lemma minEle x y : min x y = if x y then x else y.

Lemma maxEle x y : max x y = if x y then y else x.

Lemma comparable_minEgt x y : x >=< y min x y = if x > y then y else x.
Lemma comparable_maxEgt x y : x >=< y max x y = if x > y then x else y.
Lemma comparable_minEge x y : x >=< y min x y = if x y then y else x.
Lemma comparable_maxEge x y : x >=< y max x y = if x y then x else y.

Lemma min_l x y : x y min x y = x.
Lemma min_r x y : y x min x y = y.
Lemma max_l x y : y x max x y = x.
Lemma max_r x y : x y max x y = y.

Lemma minxx : idempotent (min : T T T).

Lemma maxxx : idempotent (max : T T T).

Lemma eq_minl x y : (min x y == x) = (x y).

Lemma eq_maxr x y : (max x y == y) = (x y).

Lemma min_idPl x y : reflect (min x y = x) (x y).

Lemma max_idPr x y : reflect (max x y = y) (x y).

Lemma min_minKx x y : min (min x y) y = min x y.

Lemma min_minxK x y : min x (min x y) = min x y.

Lemma max_maxKx x y : max (max x y) y = max x y.

Lemma max_maxxK x y : max x (max x y) = max x y.

Lemma comparable_minl z : {in >=< z &, x y, min x y >=< z}.

Lemma comparable_minr z : {in >=<%O z &, x y, z >=< min x y}.

Lemma comparable_maxl z : {in >=< z &, x y, max x y >=< z}.

Lemma comparable_maxr z : {in >=<%O z &, x y, z >=< max x y}.

Section Comparable2.
Variables (z x y : T) (cmp_xy : x >=< y).

Lemma comparable_minC : min x y = min y x.

Lemma comparable_maxC : max x y = max y x.

Lemma comparable_eq_minr : (min x y == y) = (y x).

Lemma comparable_eq_maxl : (max x y == x) = (y x).

Lemma comparable_min_idPr : reflect (min x y = y) (y x).

Lemma comparable_max_idPl : reflect (max x y = x) (y x).

Lemma comparable_le_minr : (z min x y) = (z x) && (z y).

Lemma comparable_le_minl : (min x y z) = (x z) || (y z).

Lemma comparable_lt_minr : (z < min x y) = (z < x) && (z < y).

Lemma comparable_lt_minl : (min x y < z) = (x < z) || (y < z).

Lemma comparable_le_maxr : (z max x y) = (z x) || (z y).

Lemma comparable_le_maxl : (max x y z) = (x z) && (y z).

Lemma comparable_lt_maxr : (z < max x y) = (z < x) || (z < y).

Lemma comparable_lt_maxl : (max x y < z) = (x < z) && (y < z).

Lemma comparable_minxK : max (min x y) y = y.

Lemma comparable_minKx : max x (min x y) = x.

Lemma comparable_maxxK : min (max x y) y = y.

Lemma comparable_maxKx : min x (max x y) = x.

Lemma comparable_lteifNE C : x >=< y x < y ?<= if ~~ C = ~~ (y < x ?<= if C).

Lemma comparable_lteif_minr C :
  (z < Order.min x y ?<= if C) = (z < x ?<= if C) && (z < y ?<= if C).

Lemma comparable_lteif_minl C :
  (Order.min x y < z ?<= if C) = (x < z ?<= if C) || (y < z ?<= if C).

Lemma comparable_lteif_maxr C :
  (z < Order.max x y ?<= if C) = (z < x ?<= if C) || (z < y ?<= if C).

Lemma comparable_lteif_maxl C :
  (Order.max x y < z ?<= if C) = (x < z ?<= if C) && (y < z ?<= if C).

End Comparable2.

Section Comparable3.
Variables (x y z : T) (cmp_xy : x >=< y) (cmp_xz : x >=< z) (cmp_yz : y >=< z).
Let P := comparableP.

Lemma comparable_minA : min x (min y z) = min (min x y) z.

Lemma comparable_maxA : max x (max y z) = max (max x y) z.

Lemma comparable_max_minl : max (min x y) z = min (max x z) (max y z).

Lemma comparable_min_maxl : min (max x y) z = max (min x z) (min y z).

End Comparable3.

Lemma comparable_minAC x y z : x >=< y x >=< z y >=< z
  min (min x y) z = min (min x z) y.

Lemma comparable_maxAC x y z : x >=< y x >=< z y >=< z
  max (max x y) z = max (max x z) y.

Lemma comparable_minCA x y z : x >=< y x >=< z y >=< z
  min x (min y z) = min y (min x z).

Lemma comparable_maxCA x y z : x >=< y x >=< z y >=< z
  max x (max y z) = max y (max x z).

Lemma comparable_minACA x y z t :
    x >=< y x >=< z x >=< t y >=< z y >=< t z >=< t
  min (min x y) (min z t) = min (min x z) (min y t).

Lemma comparable_maxACA x y z t :
    x >=< y x >=< z x >=< t y >=< z y >=< t z >=< t
  max (max x y) (max z t) = max (max x z) (max y t).

Lemma comparable_max_minr x y z : x >=< y x >=< z y >=< z
  max x (min y z) = min (max x y) (max x z).

Lemma comparable_min_maxr x y z : x >=< y x >=< z y >=< z
  min x (max y z) = max (min x y) (min x z).

Section ArgExtremum.

Context (I : finType) (i0 : I) (P : {pred I}) (F : I T) (Pi0 : P i0).
Hypothesis F_comparable : {in P &, i j, F i >=< F j}.

Lemma comparable_arg_minP: extremum_spec <=%O P F (arg_min i0 P F).

Lemma comparable_arg_maxP: extremum_spec >=%O P F (arg_max i0 P F).

End ArgExtremum.

monotonicity

Lemma mono_in_leif (A : {pred T}) (f : T T) C :
   {in A &, {mono f : x y / x y}}
  {in A &, x y, (f x f y ?= iff C) = (x y ?= iff C)}.

Lemma mono_leif (f : T T) C :
    {mono f : x y / x y}
   x y, (f x f y ?= iff C) = (x y ?= iff C).

Lemma nmono_in_leif (A : {pred T}) (f : T T) C :
    {in A &, {mono f : x y /~ x y}}
  {in A &, x y, (f x f y ?= iff C) = (y x ?= iff C)}.

Lemma nmono_leif (f : T T) C : {mono f : x y /~ x y}
   x y, (f x f y ?= iff C) = (y x ?= iff C).

Lemma comparable_bigl x x0 op I (P : pred I) F (s : seq I) :
  {in >=< x &, y z, op y z >=< x} x0 >=< x
  {in P, i, F i >=< x} \big[op/x0]_(i <- s | P i) F i >=< x.

Lemma comparable_bigr x x0 op I (P : pred I) F (s : seq I) :
  {in >=<%O x &, y z, x >=< op y z} x >=< x0
  {in P, i, x >=< F i} x >=< \big[op/x0]_(i <- s | P i) F i.

Section bigminmax.

Variables (I : Type) (r : seq I) (f : I T) (x0 x : T) (P : pred I).

Lemma bigmax_le : x0 x ( i, P i f i x)
  \big[max/x0]_(i <- r | P i) f i x.

Lemma bigmax_lt : x0 < x ( i, P i f i < x)
  \big[max/x0]_(i <- r | P i) f i < x.

Lemma lt_bigmin : x < x0 ( i, P i x < f i)
  x < \big[min/x0]_(i <- r | P i) f i.

Lemma le_bigmin : x x0 ( i, P i x f i)
  x \big[min/x0]_(i <- r | P i) f i.

End bigminmax.

End POrderTheory.
#[global] Hint Resolve comparable_minr comparable_minl : core.
#[global] Hint Resolve comparable_maxr comparable_maxl : core.

Section ContraTheory.
Context {disp1 disp2 : unit} {T1 : porderType disp1} {T2 : porderType disp2}.
Implicit Types (x y : T1) (z t : T2) (b : bool) (m n : nat) (P : Prop).

Lemma comparable_contraTle b x y : x >=< y (y < x ~~ b) (b x y).

Lemma comparable_contraTlt b x y : x >=< y (y x ~~ b) (b x < y).

Lemma comparable_contraPle P x y : x >=< y (y < x ¬ P) (P x y).

Lemma comparable_contraPlt P x y : x >=< y (y x ¬ P) (P x < y).

Lemma comparable_contraNle b x y : x >=< y (y < x b) (~~ b x y).

Lemma comparable_contraNlt b x y : x >=< y (y x b) (~~ b x < y).

Lemma comparable_contra_not_le P x y : x >=< y (y < x P) (¬ P x y).

Lemma comparable_contra_not_lt P x y : x >=< y (y x P) (¬ P x < y).

Lemma comparable_contraFle b x y : x >=< y (y < x b) (b = false x y).

Lemma comparable_contraFlt b x y : x >=< y (y x b) (b = false x < y).

Lemma contra_leT b x y : (~~ b x < y) (y x b).

Lemma contra_ltT b x y : (~~ b x y) (y < x b).

Lemma contra_leN b x y : (b x < y) (y x ~~ b).

Lemma contra_ltN b x y : (b x y) (y < x ~~ b).

Lemma contra_le_not P x y : (P x < y) (y x ¬ P).

Lemma contra_lt_not P x y : (P x y) (y < x ¬ P).

Lemma contra_leF b x y : (b x < y) (y x b = false).

Lemma contra_ltF b x y : (b x y) (y < x b = false).

Lemma comparable_contra_leq_le m n x y : x >=< y
  (y < x (n < m)%N) ((m n)%N x y).

Lemma comparable_contra_leq_lt m n x y : x >=< y
  (y x (n < m)%N) ((m n)%N x < y).

Lemma comparable_contra_ltn_le m n x y : x >=< y
  (y < x (n m)%N) ((m < n)%N x y).

Lemma comparable_contra_ltn_lt m n x y : x >=< y
  (y x (n m)%N) ((m < n)%N x < y).

Lemma contra_le_leq x y m n : ((n < m)%N y < x) (x y (m n)%N).

Lemma contra_le_ltn x y m n : ((n m)%N y < x) (x y (m < n)%N).

Lemma contra_lt_leq x y m n : ((n < m)%N y x) (x < y (m n)%N).

Lemma contra_lt_ltn x y m n : ((n m)%N y x) (x < y (m < n)%N).

Lemma comparable_contra_le x y z t : z >=< t
  (t < z y < x) (x y z t).

Lemma comparable_contra_le_lt x y z t : z >=< t
  (t z y < x) (x y z < t).

Lemma comparable_contra_lt_le x y z t : z >=< t
  (t < z y x) (x < y z t).

Lemma comparable_contra_lt x y z t : z >=< t
 (t z y x) (x < y z < t).

End ContraTheory.

Section POrderMonotonyTheory.

Context {disp disp' : unit}.
Context {T : porderType disp} {T' : porderType disp'}.
Implicit Types (m n p : nat) (x y z : T) (u v w : T').
Variables (D D' : {pred T}) (f : T T').

Let leT_anti := @le_anti _ T.
Let leT'_anti := @le_anti _ T'.
Hint Resolve lexx lt_neqAle lt_def : core.

Let ge_antiT : antisymmetric (>=%O : rel T).

Lemma ltW_homo : {homo f : x y / x < y} {homo f : x y / x y}.

Lemma ltW_nhomo : {homo f : x y /~ x < y} {homo f : x y /~ x y}.

Lemma inj_homo_lt :
  injective f {homo f : x y / x y} {homo f : x y / x < y}.

Lemma inj_nhomo_lt :
  injective f {homo f : x y /~ x y} {homo f : x y /~ x < y}.

Lemma inc_inj : {mono f : x y / x y} injective f.

Lemma dec_inj : {mono f : x y /~ x y} injective f.

Lemma leW_mono : {mono f : x y / x y} {mono f : x y / x < y}.

Lemma leW_nmono : {mono f : x y /~ x y} {mono f : x y /~ x < y}.

Monotony in D D'
Lemma ltW_homo_in :
  {in D & D', {homo f : x y / x < y}} {in D & D', {homo f : x y / x y}}.

Lemma ltW_nhomo_in :
  {in D & D', {homo f : x y /~ x < y}} {in D & D', {homo f : x y /~ x y}}.

Lemma inj_homo_lt_in :
    {in D & D', injective f} {in D & D', {homo f : x y / x y}}
  {in D & D', {homo f : x y / x < y}}.

Lemma inj_nhomo_lt_in :
    {in D & D', injective f} {in D & D', {homo f : x y /~ x y}}
  {in D & D', {homo f : x y /~ x < y}}.

Lemma inc_inj_in : {in D &, {mono f : x y / x y}}
   {in D &, injective f}.

Lemma dec_inj_in :
  {in D &, {mono f : x y /~ x y}} {in D &, injective f}.

Lemma leW_mono_in :
  {in D &, {mono f : x y / x y}} {in D &, {mono f : x y / x < y}}.

Lemma leW_nmono_in :
  {in D &, {mono f : x y /~ x y}} {in D &, {mono f : x y /~ x < y}}.

End POrderMonotonyTheory.

End POrderTheory.

#[global] Hint Resolve lexx le_refl ltxx lt_irreflexive ltW lt_eqF : core.

Arguments leifP {disp T x y C}.
Arguments leif_refl {disp T x C}.
Arguments mono_in_leif [disp T A f C].
Arguments nmono_in_leif [disp T A f C].
Arguments mono_leif [disp T f C].
Arguments nmono_leif [disp T f C].
Arguments min_idPl {disp T x y}.
Arguments max_idPr {disp T x y}.
Arguments comparable_min_idPr {disp T x y _}.
Arguments comparable_max_idPl {disp T x y _}.

Module Import DualPOrder.
Section DualPOrder.
Canonical dual_eqType (T : eqType) := EqType T [eqMixin of T^d].
Canonical dual_choiceType (T : choiceType) := [choiceType of T^d].
Canonical dual_countType (T : countType) := [countType of T^d].
Canonical dual_finType (T : finType) := [finType of T^d].

Context {disp : unit}.
Variable T : porderType disp.

Lemma dual_lt_def (x y : T) : gt x y = (y != x) && ge x y.

Fact dual_le_anti : antisymmetric (@ge _ T).

Definition dual_porderMixin :=
  @POrder.Mixin _ _ ge gt dual_lt_def (lexx : reflexive ge) dual_le_anti
                (fun y z x zy yx ⇒ @le_trans _ _ y x z yx zy).
Canonical dual_porderType :=
  POrderType (dual_display disp) T^d dual_porderMixin.

Lemma leEdual (x y : T) : (x <=^d y :> T^d) = (y x).
Lemma ltEdual (x y : T) : (x <^d y :> T^d) = (y < x).

End DualPOrder.

Canonical dual_finPOrderType d (T : finPOrderType d) :=
  [finPOrderType of T^d].

End DualPOrder.

Module Import DualLattice.
Section DualLattice.
Context {disp : unit}.
Variable L : latticeType disp.
Implicit Types (x y : L).

Lemma meetC : commutative (@meet _ L).
Lemma joinC : commutative (@join _ L).

Lemma meetA : associative (@meet _ L).
Lemma joinA : associative (@join _ L).

Lemma joinKI y x : x `&` (x `|` y) = x.
Lemma meetKU y x : x `|` (x `&` y) = x.

Lemma joinKIC y x : x `&` (y `|` x) = x.
Lemma meetKUC y x : x `|` (y `&` x) = x.

Lemma meetUK x y : (x `&` y) `|` y = y.
Lemma joinIK x y : (x `|` y) `&` y = y.

Lemma meetUKC x y : (y `&` x) `|` y = y.
Lemma joinIKC x y : (y `|` x) `&` y = y.

Lemma leEmeet x y : (x y) = (x `&` y == x).

Lemma leEjoin x y : (x y) = (x `|` y == y).

Fact dual_leEmeet (x y : L^d) : (x y) = (x `|` y == x).

Definition dual_latticeMixin :=
  @Lattice.Mixin _ (POrder.class [porderType of L^d]) _ _ joinC meetC
                 joinA meetA meetKU joinKI dual_leEmeet.

Canonical dual_latticeType := LatticeType L^d dual_latticeMixin.

Lemma meetEdual x y : ((x : L^d) `&^d` y) = (x `|` y).
Lemma joinEdual x y : ((x : L^d) `|^d` y) = (x `&` y).

End DualLattice.
End DualLattice.

Module Import LatticeTheoryMeet.
Section LatticeTheoryMeet.
Context {disp : unit} {L : latticeType disp}.
Implicit Types (x y : L).

lattice theory
Lemma meetAC : right_commutative (@meet _ L).
Lemma meetCA : left_commutative (@meet _ L).
Lemma meetACA : interchange (@meet _ L) (@meet _ L).

Lemma meetxx x : x `&` x = x.

Lemma meetKI y x : x `&` (x `&` y) = x `&` y.
Lemma meetIK y x : (x `&` y) `&` y = x `&` y.
Lemma meetKIC y x : x `&` (y `&` x) = x `&` y.
Lemma meetIKC y x : y `&` x `&` y = x `&` y.

interaction with order

Lemma lexI x y z : (x y `&` z) = (x y) && (x z).

Lemma leIxl x y z : y x y `&` z x.

Lemma leIxr x y z : z x y `&` z x.

Lemma leIx2 x y z : (y x) || (z x) y `&` z x.

Lemma leIr x y : y `&` x x.

Lemma leIl x y : x `&` y x.

Lemma meet_idPl {x y} : reflect (x `&` y = x) (x y).
Lemma meet_idPr {x y} : reflect (y `&` x = x) (x y).

Lemma meet_l x y : x y x `&` y = x.
Lemma meet_r x y : y x x `&` y = y.

Lemma leIidl x y : (x x `&` y) = (x y).
Lemma leIidr x y : (x y `&` x) = (x y).

Lemma eq_meetl x y : (x `&` y == x) = (x y).

Lemma eq_meetr x y : (x `&` y == y) = (y x).

Lemma leI2 x y z t : x z y t x `&` y z `&` t.

End LatticeTheoryMeet.
End LatticeTheoryMeet.

Module Import LatticeTheoryJoin.
Section LatticeTheoryJoin.
Context {disp : unit} {L : latticeType disp}.
Implicit Types (x y : L).

lattice theory
Lemma joinAC : right_commutative (@join _ L).
Lemma joinCA : left_commutative (@join _ L).
Lemma joinACA : interchange (@join _ L) (@join _ L).

Lemma joinxx x : x `|` x = x.

Lemma joinKU y x : x `|` (x `|` y) = x `|` y.
Lemma joinUK y x : (x `|` y) `|` y = x `|` y.
Lemma joinKUC y x : x `|` (y `|` x) = x `|` y.
Lemma joinUKC y x : y `|` x `|` y = x `|` y.

interaction with order
Lemma leUx x y z : (x `|` y z) = (x z) && (y z).
Lemma lexUl x y z : x y x y `|` z.
Lemma lexUr x y z : x z x y `|` z.
Lemma lexU2 x y z : (x y) || (x z) x y `|` z.

Lemma leUr x y : x y `|` x.
Lemma leUl x y : x x `|` y.

Lemma join_idPr {x y} : reflect (x `|` y = y) (x y).
Lemma join_idPl {x y} : reflect (y `|` x = y) (x y).

Lemma join_l x y : y x x `|` y = x.
Lemma join_r x y : x y x `|` y = y.

Lemma leUidl x y : (x `|` y y) = (x y).
Lemma leUidr x y : (y `|` x y) = (x y).

Lemma eq_joinl x y : (x `|` y == x) = (y x).
Lemma eq_joinr x y : (x `|` y == y) = (x y).

Lemma leU2 x y z t : x z y t x `|` y z `|` t.

Lemma lcomparableP x y : incomparel x y
  (min y x) (min x y) (max y x) (max x y)
  (y `&` x) (x `&` y) (y `|` x) (x `|` y)
  (y == x) (x == y) (x y) (x y) (x > y) (x < y) (y >=< x) (x >=< y).

Lemma lcomparable_ltgtP x y : x >=< y
  comparel x y (min y x) (min x y) (max y x) (max x y)
               (y `&` x) (x `&` y) (y `|` x) (x `|` y)
               (y == x) (x == y) (x y) (x y) (x > y) (x < y).

Lemma lcomparable_leP x y : x >=< y
  lel_xor_gt x y (min y x) (min x y) (max y x) (max x y)
                 (y `&` x) (x `&` y) (y `|` x) (x `|` y) (x y) (y < x).

Lemma lcomparable_ltP x y : x >=< y
  ltl_xor_ge x y (min y x) (min x y) (max y x) (max x y)
                 (y `&` x) (x `&` y) (y `|` x) (x `|` y) (y x) (x < y).

End LatticeTheoryJoin.
End LatticeTheoryJoin.

Arguments meet_idPl {disp L x y}.
Arguments join_idPl {disp L x y}.

Module Import DistrLatticeTheory.
Section DistrLatticeTheory.
Context {disp : unit}.
Variable L : distrLatticeType disp.
Implicit Types (x y : L).

Lemma meetUl : left_distributive (@meet _ L) (@join _ L).

Lemma meetUr : right_distributive (@meet _ L) (@join _ L).

Lemma joinIl : left_distributive (@join _ L) (@meet _ L).

Lemma joinIr : right_distributive (@join _ L) (@meet _ L).

Definition dual_distrLatticeMixin :=
  @DistrLattice.Mixin _ (Lattice.class [latticeType of L^d]) joinIl.

Canonical dual_distrLatticeType := DistrLatticeType L^d dual_distrLatticeMixin.

End DistrLatticeTheory.
End DistrLatticeTheory.

Module Import TotalTheory.
Section TotalTheory.
Context {disp : unit} {T : orderType disp}.
Implicit Types (x y z t : T) (s : seq T).

Lemma le_total : total (<=%O : rel T).
Hint Resolve le_total : core.

Lemma ge_total : total (>=%O : rel T).
Hint Resolve ge_total : core.

Lemma comparableT x y : x >=< y.
Hint Resolve comparableT : core.

Lemma sort_le_sorted s : sorted <=%O (sort <=%O s).
Hint Resolve sort_le_sorted : core.

Lemma sort_lt_sorted s : sorted <%O (sort <=%O s) = uniq s.

Lemma perm_sort_leP s1 s2 : reflect (sort <=%O s1 = sort <=%O s2) (perm_eq s1 s2).

Lemma filter_sort_le p s : filter p (sort <=%O s) = sort <=%O (filter p s).

Lemma mask_sort_le s (m : bitseq) :
  {m_s : bitseq | mask m_s (sort <=%O s) = sort <=%O (mask m s)}.

Lemma sorted_mask_sort_le s (m : bitseq) :
  sorted <=%O (mask m s) {m_s : bitseq | mask m_s (sort <=%O s) = mask m s}.

Lemma subseq_sort_le : {homo sort <=%O : s1 s2 / @subseq T s1 s2}.

Lemma sorted_subseq_sort_le s1 s2 :
  subseq s1 s2 sorted <=%O s1 subseq s1 (sort <=%O s2).

Lemma mem2_sort_le s x y : x y mem2 s x y mem2 (sort <=%O s) x y.

Lemma leNgt x y : (x y) = ~~ (y < x).

Lemma ltNge x y : (x < y) = ~~ (y x).

Definition ltgtP x y := LatticeTheoryJoin.lcomparable_ltgtP (comparableT x y).
Definition leP x y := LatticeTheoryJoin.lcomparable_leP (comparableT x y).
Definition ltP x y := LatticeTheoryJoin.lcomparable_ltP (comparableT x y).

Lemma wlog_le P :
     ( x y, P y x P x y) ( x y, x y P x y)
    x y, P x y.

Lemma wlog_lt P :
    ( x, P x x)
    ( x y, (P y x P x y)) ( x y, x < y P x y)
   x y, P x y.

Lemma neq_lt x y : (x != y) = (x < y) || (y < x).

Lemma lt_total x y : x != y (x < y) || (y < x).

Lemma eq_leLR x y z t :
  (x y z t) (y < x t < z) (x y) = (z t).

Lemma eq_leRL x y z t :
  (x y z t) (y < x t < z) (z t) = (x y).

Lemma eq_ltLR x y z t :
  (x < y z < t) (y x t z) (x < y) = (z < t).

Lemma eq_ltRL x y z t :
  (x < y z < t) (y x t z) (z < t) = (x < y).

max and min is join and meet

Lemma meetEtotal x y : x `&` y = min x y.
Lemma joinEtotal x y : x `|` y = max x y.

max and min theory

Lemma minEgt x y : min x y = if x > y then y else x.
Lemma maxEgt x y : max x y = if x > y then x else y.
Lemma minEge x y : min x y = if x y then y else x.
Lemma maxEge x y : max x y = if x y then x else y.

Lemma minC : commutative (min : T T T).

Lemma maxC : commutative (max : T T T).

Lemma minA : associative (min : T T T).

Lemma maxA : associative (max : T T T).

Lemma minAC : right_commutative (min : T T T).

Lemma maxAC : right_commutative (max : T T T).

Lemma minCA : left_commutative (min : T T T).

Lemma maxCA : left_commutative (max : T T T).

Lemma minACA : interchange (min : T T T) min.

Lemma maxACA : interchange (max : T T T) max.

Lemma eq_minr x y : (min x y == y) = (y x).

Lemma eq_maxl x y : (max x y == x) = (y x).

Lemma min_idPr x y : reflect (min x y = y) (y x).

Lemma max_idPl x y : reflect (max x y = x) (y x).

Lemma le_minr z x y : (z min x y) = (z x) && (z y).

Lemma le_minl z x y : (min x y z) = (x z) || (y z).

Lemma lt_minr z x y : (z < min x y) = (z < x) && (z < y).

Lemma lt_minl z x y : (min x y < z) = (x < z) || (y < z).

Lemma le_maxr z x y : (z max x y) = (z x) || (z y).

Lemma le_maxl z x y : (max x y z) = (x z) && (y z).

Lemma lt_maxr z x y : (z < max x y) = (z < x) || (z < y).

Lemma lt_maxl z x y : (max x y < z) = (x < z) && (y < z).

Lemma minxK x y : max (min x y) y = y.
Lemma minKx x y : max x (min x y) = x.
Lemma maxxK x y : min (max x y) y = y.
Lemma maxKx x y : min x (max x y) = x.

Lemma max_minl : left_distributive (max : T T T) min.

Lemma min_maxl : left_distributive (min : T T T) max.

Lemma max_minr : right_distributive (max : T T T) min.

Lemma min_maxr : right_distributive (min : T T T) max.

Lemma leIx x y z : (meet y z x) = (y x) || (z x).

Lemma lexU x y z : (x join y z) = (x y) || (x z).

Lemma ltxI x y z : (x < meet y z) = (x < y) && (x < z).

Lemma ltIx x y z : (meet y z < x) = (y < x) || (z < x).

Lemma ltxU x y z : (x < join y z) = (x < y) || (x < z).

Lemma ltUx x y z : (join y z < x) = (y < x) && (z < x).

Definition ltexI := (@lexI _ T, ltxI).
Definition lteIx := (leIx, ltIx).
Definition ltexU := (lexU, ltxU).
Definition lteUx := (@leUx _ T, ltUx).

lteif

Lemma lteifNE x y C : x < y ?<= if ~~ C = ~~ (y < x ?<= if C).

Lemma lteif_minr z x y C :
  (z < Order.min x y ?<= if C) = (z < x ?<= if C) && (z < y ?<= if C).

Lemma lteif_minl z x y C :
  (Order.min x y < z ?<= if C) = (x < z ?<= if C) || (y < z ?<= if C).

Lemma lteif_maxr z x y C :
  (z < Order.max x y ?<= if C) = (z < x ?<= if C) || (z < y ?<= if C).

Lemma lteif_maxl z x y C :
  (Order.max x y < z ?<= if C) = (x < z ?<= if C) && (y < z ?<= if C).

Section ArgExtremum.

Context (I : finType) (i0 : I) (P : {pred I}) (F : I T) (Pi0 : P i0).

Lemma arg_minP: extremum_spec <=%O P F (arg_min i0 P F).

Lemma arg_maxP: extremum_spec >=%O P F (arg_max i0 P F).

End ArgExtremum.

Lemma count_le_gt x s : count ( x) s = size s - count (> x) s.

Lemma count_lt_ge x s : count (< x) s = size s - count ( x) s.

Section bigminmax_Type.
Variables (I : Type) (r : seq I) (x : T).
Implicit Types (P : pred I) (F : I T).

Lemma bigmin_mkcond P F : \big[min/x]_(i <- r | P i) F i =
  \big[min/x]_(i <- r) (if P i then F i else x).

Lemma bigmax_mkcond P F :
  \big[max/x]_(i <- r | P i) F i = \big[max/x]_(i <- r) if P i then F i else x.

Lemma bigmin_split P F1 F2 :
  \big[min/x]_(i <- r | P i) (min (F1 i) (F2 i)) =
    min (\big[min/x]_(i <- r | P i) F1 i) (\big[min/x]_(i <- r | P i) F2 i).

Lemma bigmax_split P F1 F2 :
  \big[max/x]_(i <- r | P i) (max (F1 i) (F2 i)) =
    max (\big[max/x]_(i <- r | P i) F1 i) (\big[max/x]_(i <- r | P i) F2 i).

Lemma bigmin_idl P F :
  \big[min/x]_(i <- r | P i) F i = min x (\big[min/x]_(i <- r | P i) F i).

Lemma bigmax_idl P F :
  \big[max/x]_(i <- r | P i) F i = max x (\big[max/x]_(i <- r | P i) F i).

Lemma bigmin_idr P F :
  \big[min/x]_(i <- r | P i) F i = min (\big[min/x]_(i <- r | P i) F i) x.

Lemma bigmax_idr P F :
  \big[max/x]_(i <- r | P i) F i = max (\big[max/x]_(i <- r | P i) F i) x.

Lemma bigminID a P F : \big[min/x]_(i <- r | P i) F i =
  min (\big[min/x]_(i <- r | P i && a i) F i)
      (\big[min/x]_(i <- r | P i && ~~ a i) F i).

Lemma bigmaxID a P F : \big[max/x]_(i <- r | P i) F i =
  max (\big[max/x]_(i <- r | P i && a i) F i)
      (\big[max/x]_(i <- r | P i && ~~ a i) F i).

End bigminmax_Type.

Let le_minr_id (x y : T) : x min x y.
Let le_maxr_id (x y : T) : x max x y.

Lemma sub_bigmin [x0] I r (P P' : {pred I}) (F : I T) :
    ( i, P' i P i)
  \big[min/x0]_(i <- r | P i) F i \big[min/x0]_(i <- r | P' i) F i.

Lemma sub_bigmax [x0] I r (P P' : {pred I}) (F : I T) :
    ( i, P i P' i)
  \big[max/x0]_(i <- r | P i) F i \big[max/x0]_(i <- r | P' i) F i.

Lemma sub_bigmin_seq [x0] (I : eqType) r r' P (F : I T) : {subset r' r}
  \big[min/x0]_(i <- r | P i) F i \big[min/x0]_(i <- r' | P i) F i.

Lemma sub_bigmax_seq [x0] (I : eqType) r r' P (F : I T) : {subset r r'}
  \big[max/x0]_(i <- r | P i) F i \big[max/x0]_(i <- r' | P i) F i.

Lemma sub_bigmin_cond [x0] (I : eqType) r r' P P' (F : I T) :
    {subset [seq i <- r | P i] [seq i <- r' | P' i]}
  \big[min/x0]_(i <- r' | P' i) F i \big[min/x0]_(i <- r | P i) F i.

Lemma sub_bigmax_cond [x0] (I : eqType) r r' P P' (F : I T) :
    {subset [seq i <- r | P i] [seq i <- r' | P' i]}
  \big[max/x0]_(i <- r | P i) F i \big[max/x0]_(i <- r' | P' i) F i.

Lemma sub_in_bigmin [x0] [I : eqType] (r : seq I) (P P' : {pred I}) F :
    {in r, i, P' i P i}
  \big[min/x0]_(i <- r | P i) F i \big[min/x0]_(i <- r | P' i) F i.

Lemma sub_in_bigmax [x0] [I : eqType] (r : seq I) (P P' : {pred I}) F :
    {in r, i, P i P' i}
  \big[max/x0]_(i <- r | P i) F i \big[max/x0]_(i <- r | P' i) F i.

Lemma le_bigmin_nat [x0] n m n' m' P (F : nat T) :
    (n n')%N (m' m)%N
  \big[min/x0]_(n i < m | P i) F i \big[min/x0]_(n' i < m' | P i) F i.

Lemma le_bigmax_nat [x0] n m n' m' P (F : nat T) :
    (n' n)%N (m m')%N
  \big[max/x0]_(n i < m | P i) F i \big[max/x0]_(n' i < m' | P i) F i.

Lemma le_bigmin_nat_cond [x0] n m n' m' (P P' : pred nat) (F : nat T) :
    (n n')%N (m' m)%N ( i, (n' i < m')%N P' i P i)
  \big[min/x0]_(n i < m | P i) F i \big[min/x0]_(n' i < m' | P' i) F i.

Lemma le_bigmax_nat_cond [x0] n m n' m' (P P' : {pred nat}) (F : nat T) :
    (n' n)%N (m m')%N ( i, (n i < m)%N P i P' i)
  \big[max/x0]_(n i < m | P i) F i \big[max/x0]_(n' i < m' | P' i) F i.

Lemma le_bigmin_ord [x0] n m (P : pred nat) (F : nat T) : (m n)%N
  \big[min/x0]_(i < n | P i) F i \big[min/x0]_(i < m | P i) F i.

Lemma le_bigmax_ord [x0] n m (P : {pred nat}) (F : nat T) : (n m)%N
  \big[max/x0]_(i < n | P i) F i \big[max/x0]_(i < m | P i) F i.

Lemma le_bigmin_ord_cond [x0] n m (P P' : pred nat) (F : nat T) :
    (m n)%N ( i : 'I_m, P' i P i)
  \big[min/x0]_(i < n | P i) F i \big[min/x0]_(i < m | P' i) F i.

Lemma le_bigmax_ord_cond [x0] n m (P P' : {pred nat}) (F : nat T) :
    (n m)%N ( i : 'I_n, P i P' i)
  \big[max/x0]_(i < n | P i) F i \big[max/x0]_(i < m | P' i) F i.

Lemma subset_bigmin [x0] [I : finType] [A A' P : {pred I}] (F : I T) :
    A' \subset A
  \big[min/x0]_(i in A | P i) F i \big[min/x0]_(i in A' | P i) F i.

Lemma subset_bigmax [x0] [I : finType] (A A' P : {pred I}) (F : I T) :
    A \subset A'
  \big[max/x0]_(i in A | P i) F i \big[max/x0]_(i in A' | P i) F i.

Lemma subset_bigmin_cond [x0] (I : finType) (A A' P P' : {pred I}) (F : I T) :
    [set i in A' | P' i] \subset [set i in A | P i]
  \big[min/x0]_(i in A | P i) F i \big[min/x0]_(i in A' | P' i) F i.

Lemma subset_bigmax_cond [x0] (I : finType) (A A' P P' : {pred I}) (F : I T) :
    [set i in A | P i] \subset [set i in A' | P' i]
  \big[max/x0]_(i in A | P i) F i \big[max/x0]_(i in A' | P' i) F i.

Section bigminmax_finType.
Variable (I : finType) (x : T).
Implicit Types (P : pred I) (F : I T).

Lemma bigminD1 j P F : P j
  \big[min/x]_(i | P i) F i = min (F j) (\big[min/x]_(i | P i && (i != j)) F i).

Lemma bigmaxD1 j P F : P j
  \big[max/x]_(i | P i) F i = max (F j) (\big[max/x]_(i | P i && (i != j)) F i).

Lemma bigmin_le_cond j P F : P j \big[min/x]_(i | P i) F i F j.

Lemma le_bigmax_cond j P F : P j F j \big[max/x]_(i | P i) F i.

Lemma bigmin_le j F : \big[min/x]_i F i F j.

Lemma le_bigmax F j : F j \big[max/x]_i F i.

Lemma bigmin_inf j P m F : P j F j m \big[min/x]_(i | P i) F i m.

NB: as of [2022-08-02], bigop.bigmax_sup already exists for nat
Lemma bigmax_sup j P m F : P j m F j m \big[max/x]_(i | P i) F i.

Lemma bigmin_geP m P F :
  reflect (m x i, P i m F i)
          (m \big[min/x]_(i | P i) F i).

Lemma bigmax_leP m P F :
  reflect (x m i, P i F i m)
          (\big[max/x]_(i | P i) F i m).

Lemma bigmin_gtP m P F :
  reflect (m < x i, P i m < F i) (m < \big[min/x]_(i | P i) F i).

Lemma bigmax_ltP m P F :
  reflect (x < m i, P i F i < m) (\big[max/x]_(i | P i) F i < m).

Lemma bigmin_eq_arg j P F : P j ( i, P i F i x)
  \big[min/x]_(i | P i) F i = F [arg min_(i < j | P i) F i].

Lemma bigmax_eq_arg j P F : P j ( i, P i x F i)
  \big[max/x]_(i | P i) F i = F [arg max_(i > j | P i) F i].

Lemma eq_bigmin j P F : P j ( i, P i F i x)
  {i0 | i0 \in I & \big[min/x]_(i | P i) F i = F i0}.

Lemma eq_bigmax j P F : P j ( i, P i x F i)
  {i0 | i0 \in I & \big[max/x]_(i | P i) F i = F i0}.

Lemma le_bigmin2 P F1 F2 : ( i, P i F1 i F2 i)
  \big[min/x]_(i | P i) F1 i \big[min/x]_(i | P i) F2 i.

Lemma le_bigmax2 P F1 F2 : ( i, P i F1 i F2 i)
  \big[max/x]_(i | P i) F1 i \big[max/x]_(i | P i) F2 i.

End bigminmax_finType.

End TotalTheory.

#[global] Hint Resolve le_total : core.
#[global] Hint Resolve ge_total : core.
#[global] Hint Resolve comparableT : core.
#[global] Hint Resolve sort_le_sorted : core.

Arguments min_idPr {disp T x y}.
Arguments max_idPl {disp T x y}.
Arguments bigmin_mkcond {disp T I r}.
Arguments bigmax_mkcond {disp T I r}.
Arguments bigminID {disp T I r}.
Arguments bigmaxID {disp T I r}.
Arguments bigminD1 {disp T I x} j.
Arguments bigmaxD1 {disp T I x} j.
Arguments bigmin_inf {disp T I x} j.
Arguments bigmax_sup {disp T I x} j.
Arguments bigmin_eq_arg {disp T I} x j.
Arguments bigmax_eq_arg {disp T I} x j.
Arguments eq_bigmin {disp T I x} j.
Arguments eq_bigmax {disp T I x} j.

contra lemmas

Section ContraTheory.
Context {disp1 disp2 : unit} {T1 : porderType disp1} {T2 : orderType disp2}.
Implicit Types (x y : T1) (z t : T2) (b : bool) (m n : nat) (P : Prop).

Lemma contraTle b z t : (t < z ~~ b) (b z t).

Lemma contraTlt b z t : (t z ~~ b) (b z < t).

Lemma contraPle P z t : (t < z ¬ P) (P z t).

Lemma contraPlt P z t : (t z ¬ P) (P z < t).

Lemma contraNle b z t : (t < z b) (~~ b z t).

Lemma contraNlt b z t : (t z b) (~~ b z < t).

Lemma contra_not_le P z t : (t < z P) (¬ P z t).

Lemma contra_not_lt P z t : (t z P) (¬ P z < t).

Lemma contraFle b z t : (t < z b) (b = false z t).

Lemma contraFlt b z t : (t z b) (b = false z < t).

Lemma contra_leq_le m n z t : (t < z (n < m)%N) ((m n)%N z t).

Lemma contra_leq_lt m n z t : (t z (n < m)%N) ((m n)%N z < t).

Lemma contra_ltn_le m n z t : (t < z (n m)%N) ((m < n)%N z t).

Lemma contra_ltn_lt m n z t : (t z (n m)%N) ((m < n)%N z < t).

Lemma contra_le x y z t : (t < z y < x) (x y z t).

Lemma contra_le_lt x y z t : (t z y < x) (x y z < t).

Lemma contra_lt_le x y z t : (t < z y x) (x < y z t).

Lemma contra_lt x y z t : (t z y x) (x < y z < t).

End ContraTheory.

Section TotalMonotonyTheory.

Context {disp : unit} {disp' : unit}.
Context {T : orderType disp} {T' : porderType disp'}.
Variables (D : {pred T}) (f : T T').
Implicit Types (x y z : T) (u v w : T').

Let leT_anti := @le_anti _ T.
Let leT'_anti := @le_anti _ T'.
Let ltT_neqAle := @lt_neqAle _ T.
Let ltT'_neqAle := @lt_neqAle _ T'.
Let ltT_def := @lt_def _ T.
Let leT_total := @le_total _ T.

Lemma le_mono : {homo f : x y / x < y} {mono f : x y / x y}.

Lemma le_nmono : {homo f : x y /~ x < y} {mono f : x y /~ x y}.

Lemma le_mono_in :
  {in D &, {homo f : x y / x < y}} {in D &, {mono f : x y / x y}}.

Lemma le_nmono_in :
  {in D &, {homo f : x y /~ x < y}} {in D &, {mono f : x y /~ x y}}.

End TotalMonotonyTheory.
End TotalTheory.

Module Import BLatticeTheory.
Section BLatticeTheory.
Context {disp : unit} {L : bLatticeType disp}.
Implicit Types (I : finType) (T : eqType) (x y z : L).

Non-distributive lattice theory with 0 & 1
Lemma le0x x : 0 x.
Hint Resolve le0x : core.

Lemma lex0 x : (x 0) = (x == 0).

Lemma ltx0 x : (x < 0) = false.

Lemma lt0x x : (0 < x) = (x != 0).

Lemma meet0x : left_zero 0 (@meet _ L).

Lemma meetx0 : right_zero 0 (@meet _ L).

Lemma join0x : left_id 0 (@join _ L).

Lemma joinx0 : right_id 0 (@join _ L).

Lemma join_eq0 x y : (x `|` y == 0) = (x == 0) && (y == 0).

Variant eq0_xor_gt0 x : bool bool Set :=
    Eq0NotPOs : x = 0 eq0_xor_gt0 x true false
  | POsNotEq0 : 0 < x eq0_xor_gt0 x false true.

Lemma posxP x : eq0_xor_gt0 x (x == 0) (0 < x).

Canonical join_monoid := Monoid.Law (@joinA _ _) join0x joinx0.
Canonical join_comoid := Monoid.ComLaw (@joinC _ _).

Lemma joins_sup_seq T (r : seq T) (P : {pred T}) (F : T L) (x : T) :
  x \in r P x F x \join_(i <- r | P i) F i.

Lemma joins_min_seq T (r : seq T) (P : {pred T}) (F : T L) (x : T) (l : L) :
  x \in r P x l F x l \join_(x <- r | P x) F x.

Lemma joins_sup I (j : I) (P : {pred I}) (F : I L) :
  P j F j \join_(i | P i) F i.

Lemma joins_min I (j : I) (l : L) (P : {pred I}) (F : I L) :
  P j l F j l \join_(i | P i) F i.

Lemma joins_le J (r : seq J) (P : {pred J}) (F : J L) (u : L) :
  ( x : J, P x F x u) \join_(x <- r | P x) F x u.

Lemma joinsP_seq T (r : seq T) (P : {pred T}) (F : T L) (u : L) :
  reflect ( x : T, x \in r P x F x u)
          (\join_(x <- r | P x) F x u).

Lemma joinsP I (u : L) (P : {pred I}) (F : I L) :
  reflect ( i : I, P i F i u) (\join_(i | P i) F i u).

Lemma le_joins I (A B : {set I}) (F : I L) :
  A \subset B \join_(i in A) F i \join_(i in B) F i.

Lemma joins_setU I (A B : {set I}) (F : I L) :
  \join_(i in (A :|: B)) F i = \join_(i in A) F i `|` \join_(i in B) F i.

Lemma joins_seq I (r : seq I) (F : I L) :
  \join_(i <- r) F i = \join_(i in r) F i.

End BLatticeTheory.

End BLatticeTheory.

Module Import DualTBLattice.
Section DualTBLattice.
Context {disp : unit} {L : tbLatticeType disp}.

Lemma lex1 (x : L) : x top.

Definition dual_bLatticeMixin :=
  @BLattice.Mixin _ (Lattice.class [latticeType of L^d]) top lex1.
Canonical dual_bLatticeType := BLatticeType L^d dual_bLatticeMixin.

Definition dual_tbLatticeMixin :=
  @TBLattice.Mixin _ (BLattice.class [bLatticeType of L^d])
                   (bottom : L) (@le0x _ L).
Canonical dual_tbLatticeType := TBLatticeType L^d dual_tbLatticeMixin.

Lemma botEdual : (dual_bottom : L^d) = 1 :> L.
Lemma topEdual : (dual_top : L^d) = 0 :> L.

End DualTBLattice.

Canonical dual_finLatticeType d (T : finLatticeType d) :=
  [finLatticeType of T^d].

End DualTBLattice.

Module Import TBLatticeTheory.
Section TBLatticeTheory.
Context {disp : unit} {L : tbLatticeType disp}.
Implicit Types (I : finType) (T : eqType) (x y : L).


Hint Resolve le0x lex1 : core.

Lemma meetx1 : right_id 1 (@meet _ L).

Lemma meet1x : left_id 1 (@meet _ L).

Lemma joinx1 : right_zero 1 (@join _ L).

Lemma join1x : left_zero 1 (@join _ L).

Lemma le1x x : (1 x) = (x == 1).

Lemma meet_eq1 x y : (x `&` y == 1) = (x == 1) && (y == 1).

Canonical meet_monoid := Monoid.Law (@meetA _ _) meet1x meetx1.
Canonical meet_comoid := Monoid.ComLaw (@meetC _ _).

Canonical meet_muloid := Monoid.MulLaw (@meet0x _ L) (@meetx0 _ _).
Canonical join_muloid := Monoid.MulLaw join1x joinx1.

Lemma meets_inf_seq T (r : seq T) (P : {pred T}) (F : T L) (x : T) :
  x \in r P x \meet_(i <- r | P i) F i F x.

Lemma meets_max_seq T (r : seq T) (P : {pred T}) (F : T L) (x : T) (u : L) :
  x \in r P x F x u \meet_(x <- r | P x) F x u.

Lemma meets_inf I (j : I) (P : {pred I}) (F : I L) :
   P j \meet_(i | P i) F i F j.

Lemma meets_max I (j : I) (u : L) (P : {pred I}) (F : I L) :
   P j F j u \meet_(i | P i) F i u.

Lemma meets_ge J (r : seq J) (P : {pred J}) (F : J L) (u : L) :
  ( x : J, P x u F x) u \meet_(x <- r | P x) F x.

Lemma meetsP_seq T (r : seq T) (P : {pred T}) (F : T L) (l : L) :
  reflect ( x : T, x \in r P x l F x)
          (l \meet_(x <- r | P x) F x).

Lemma meetsP I (l : L) (P : {pred I}) (F : I L) :
   reflect ( i : I, P i l F i) (l \meet_(i | P i) F i).

Lemma le_meets I (A B : {set I}) (F : I L) :
   A \subset B \meet_(i in B) F i \meet_(i in A) F i.

Lemma meets_setU I (A B : {set I}) (F : I L) :
   \meet_(i in (A :|: B)) F i = \meet_(i in A) F i `&` \meet_(i in B) F i.

Lemma meets_seq I (r : seq I) (F : I L) :
   \meet_(i <- r) F i = \meet_(i in r) F i.

End TBLatticeTheory.

End TBLatticeTheory.

Module Import BDistrLatticeTheory.
Section BDistrLatticeTheory.
Context {disp : unit} {L : bDistrLatticeType disp}.
Implicit Types (I : finType) (T : eqType) (x y z : L).
Distributive lattice theory with 0 & 1

Lemma leU2l_le y t x z : x `&` t = 0 x `|` y z `|` t x z.

Lemma leU2r_le y t x z : x `&` t = 0 y `|` x t `|` z x z.

Lemma disjoint_lexUl z x y : x `&` z = 0 (x y `|` z) = (x y).

Lemma disjoint_lexUr z x y : x `&` z = 0 (x z `|` y) = (x y).

Lemma leU2E x y z t : x `&` t = 0 y `&` z = 0
  (x `|` y z `|` t) = (x z) && (y t).

Lemma joins_disjoint I (d : L) (P : {pred I}) (F : I L) :
   ( i : I, P i d `&` F i = 0) d `&` \join_(i | P i) F i = 0.

End BDistrLatticeTheory.
End BDistrLatticeTheory.

Module Import DualTBDistrLattice.
Section DualTBDistrLattice.
Context {disp : unit} {L : tbDistrLatticeType disp}.

Canonical dual_bDistrLatticeType := [bDistrLatticeType of L^d].
Canonical dual_tbDistrLatticeType := [tbDistrLatticeType of L^d].

End DualTBDistrLattice.

Canonical dual_finDistrLatticeType d (T : finDistrLatticeType d) :=
  [finDistrLatticeType of T^d].

End DualTBDistrLattice.

Module Import TBDistrLatticeTheory.
Section TBDistrLatticeTheory.
Context {disp : unit} {L : tbDistrLatticeType disp}.
Implicit Types (I : finType) (T : eqType) (x y : L).


Lemma leI2l_le y t x z : y `|` z = 1 x `&` y z `&` t x z.

Lemma leI2r_le y t x z : y `|` z = 1 y `&` x t `&` z x z.

Lemma cover_leIxl z x y : z `|` y = 1 (x `&` z y) = (x y).

Lemma cover_leIxr z x y : z `|` y = 1 (z `&` x y) = (x y).

Lemma leI2E x y z t : x `|` t = 1 y `|` z = 1
  (x `&` y z `&` t) = (x z) && (y t).

Canonical join_addoid := Monoid.AddLaw (@meetUl _ L) (@meetUr _ _).
Canonical meet_addoid := Monoid.AddLaw (@joinIl _ L) (@joinIr _ _).

Lemma meets_total I (d : L) (P : {pred I}) (F : I L) :
   ( i : I, P i d `|` F i = 1) d `|` \meet_(i | P i) F i = 1.

End TBDistrLatticeTheory.
End TBDistrLatticeTheory.

Module Import CBDistrLatticeTheory.
Section CBDistrLatticeTheory.
Context {disp : unit} {L : cbDistrLatticeType disp}.
Implicit Types (x y z : L).

Lemma subKI x y : y `&` (x `\` y) = 0.

Lemma subIK x y : (x `\` y) `&` y = 0.

Lemma meetIB z x y : (z `&` y) `&` (x `\` y) = 0.

Lemma meetBI z x y : (x `\` y) `&` (z `&` y) = 0.

Lemma joinIB y x : (x `&` y) `|` (x `\` y) = x.

Lemma joinBI y x : (x `\` y) `|` (x `&` y) = x.

Lemma joinIBC y x : (y `&` x) `|` (x `\` y) = x.

Lemma joinBIC y x : (x `\` y) `|` (y `&` x) = x.

Lemma leBx x y : x `\` y x.
Hint Resolve leBx : core.

Lemma subxx x : x `\` x = 0.

Lemma leBl z x y : x y x `\` z y `\` z.

Lemma subKU y x : y `|` (x `\` y) = y `|` x.

Lemma subUK y x : (x `\` y) `|` y = x `|` y.

Lemma leBKU y x : y x y `|` (x `\` y) = x.

Lemma leBUK y x : y x (x `\` y) `|` y = x.

Lemma leBLR x y z : (x `\` y z) = (x y `|` z).

Lemma subUx x y z : (x `|` y) `\` z = (x `\` z) `|` (y `\` z).

Lemma sub_eq0 x y : (x `\` y == 0) = (x y).

Lemma joinxB x y z : x `|` (y `\` z) = ((x `|` y) `\` z) `|` (x `&` z).

Lemma joinBx x y z : (y `\` z) `|` x = ((y `|` x) `\` z) `|` (z `&` x).

Lemma leBr z x y : x y z `\` y z `\` x.

Lemma leB2 x y z t : x z t y x `\` y z `\` t.

Lemma meet_eq0E_sub z x y : x z (x `&` y == 0) = (x z `\` y).

Lemma leBRL x y z : (x z `\` y) = (x z) && (x `&` y == 0).

Lemma eq_sub x y z : (x `\` y == z) = (z x y `|` z) && (z `&` y == 0).

Lemma subxU x y z : z `\` (x `|` y) = (z `\` x) `&` (z `\` y).

Lemma subx0 x : x `\` 0 = x.

Lemma sub0x x : 0 `\` x = 0.

Lemma subIx x y z : (x `&` y) `\` z = (x `\` z) `&` (y `\` z).

Lemma meetxB x y z : x `&` (y `\` z) = (x `&` y) `\` z.

Lemma meetBx x y z : (x `\` y) `&` z = (x `&` z) `\` y.

Lemma subxI x y z : x `\` (y `&` z) = (x `\` y) `|` (x `\` z).

Lemma subBx x y z : (x `\` y) `\` z = x `\` (y `|` z).

Lemma subxB x y z : x `\` (y `\` z) = (x `\` y) `|` (x `&` z).

Lemma joinBK x y : (y `|` x) `\` x = (y `\` x).

Lemma joinBKC x y : (x `|` y) `\` x = (y `\` x).

Lemma disj_le x y : x `&` y == 0 x y = (x == 0).

Lemma disj_leC x y : y `&` x == 0 x y = (x == 0).

Lemma disj_subl x y : x `&` y == 0 x `\` y = x.

Lemma disj_subr x y : x `&` y == 0 y `\` x = y.

Lemma lt0B x y : x < y 0 < y `\` x.

End CBDistrLatticeTheory.
End CBDistrLatticeTheory.

Module Import CTBDistrLatticeTheory.
Section CTBDistrLatticeTheory.
Context {disp : unit} {L : ctbDistrLatticeType disp}.
Implicit Types (x y z : L).

Lemma complE x : ~` x = 1 `\` x.

Lemma sub1x x : 1 `\` x = ~` x.

Lemma subE x y : x `\` y = x `&` ~` y.

Lemma complK : involutive (@compl _ L).

Lemma compl_inj : injective (@compl _ L).

Lemma disj_leC x y : (x `&` y == 0) = (x ~` y).

Lemma leC x y : (~` x ~` y) = (y x).

Lemma complU x y : ~` (x `|` y) = ~` x `&` ~` y.

Lemma complI x y : ~` (x `&` y) = ~` x `|` ~` y.

Lemma joinxC x : x `|` ~` x = 1.

Lemma joinCx x : ~` x `|` x = 1.

Lemma meetxC x : x `&` ~` x = 0.

Lemma meetCx x : ~` x `&` x = 0.

Lemma compl1 : ~` 1 = 0 :> L.

Lemma compl0 : ~` 0 = 1 :> L.

Lemma complB x y : ~` (x `\` y) = ~` x `|` y.

Lemma leBC x y : x `\` y ~` y.

Lemma leCx x y : (~` x y) = (~` y x).

Lemma lexC x y : (x ~` y) = (y ~` x).

Lemma compl_joins (J : Type) (r : seq J) (P : {pred J}) (F : J L) :
   ~` (\join_(j <- r | P j) F j) = \meet_(j <- r | P j) ~` F j.

Lemma compl_meets (J : Type) (r : seq J) (P : {pred J}) (F : J L) :
   ~` (\meet_(j <- r | P j) F j) = \join_(j <- r | P j) ~` F j.

End CTBDistrLatticeTheory.
End CTBDistrLatticeTheory.

FACTORIES

Module LePOrderMixin.
Section LePOrderMixin.
Variable (T : eqType).

Record of_ := Build {
  le : rel T;
  lt : rel T;
  lt_def : x y, lt x y = (y != x) && (le x y);
  lexx : reflexive le;
  le_anti : antisymmetric le;
  le_trans : transitive le;
}.

Definition porderMixin (m : of_) :=
  @POrder.Mixin _ _ (le m) (lt m)
                (lt_def m) (lexx m) (@le_anti m) (@le_trans m).

End LePOrderMixin.

Module Exports.
Notation lePOrderMixin := of_.
Notation LePOrderMixin := Build.
Coercion porderMixin : of_ >-> POrder.mixin_of.
End Exports.

End LePOrderMixin.
Import LePOrderMixin.Exports.

Module BottomMixin.
Section BottomMixin.
Variable (disp : unit) (T : porderType disp).

Record of_ := Build {
  bottom : T;
  le0x : x, bottom x;
}.

Definition bLatticeMixin (m : of_) := @BLattice.Mixin _ _ (bottom m) (le0x m).

End BottomMixin.

Module Exports.
Notation bottomMixin := of_.
Notation BottomMixin := Build.
Coercion bLatticeMixin : of_ >-> BLattice.mixin_of.
End Exports.

End BottomMixin.
Import BottomMixin.Exports.

Module TopMixin.
Section TopMixin.
Variable (disp : unit) (T : porderType disp).

Record of_ := Build {
  top : T;
  lex1 : x, x top;
}.

Definition tbLatticeMixin (m : of_) := @TBLattice.Mixin _ _ (top m) (lex1 m).

End TopMixin.

Module Exports.
Notation topMixin := of_.
Notation TopMixin := Build.
Coercion tbLatticeMixin : of_ >-> TBLattice.mixin_of.
End Exports.

End TopMixin.
Import TopMixin.Exports.

Module LatticeMixin.
Section LatticeMixin.
Variable (disp : unit) (T : porderType disp).

Record of_ := Build {
  meet : T T T;
  join : T T T;
  meetC : commutative meet;
  joinC : commutative join;
  meetA : associative meet;
  joinA : associative join;
  joinKI : y x, meet x (join x y) = x;
  meetKU : y x, join x (meet x y) = x;
  leEmeet : x y, (x y) = (meet x y == x);
}.

Definition latticeMixin (m : of_) :=
  @Lattice.Mixin
    T _ (meet m) (join m)
    (meetC m) (joinC m) (meetA m) (joinA m) (joinKI m) (meetKU m) (leEmeet m).

End LatticeMixin.

Module Exports.
Coercion latticeMixin : of_ >-> Lattice.mixin_of.
Notation latticeMixin := of_.
Notation LatticeMixin := Build.
End Exports.

End LatticeMixin.
Import LatticeMixin.Exports.

Module DistrLatticeMixin.
Section DistrLatticeMixin.
Variable (disp : unit) (T : latticeType disp).

Record of_ := Build {
  meetUl : @left_distributive T T meet join;
}.

Definition distrLatticeMixin (m : of_) := @DistrLattice.Mixin _ _ (meetUl m).

End DistrLatticeMixin.

Module Exports.
Coercion distrLatticeMixin : of_ >-> DistrLattice.mixin_of.
Notation distrLatticeMixin := of_.
Notation DistrLatticeMixin := Build.
End Exports.

End DistrLatticeMixin.
Import DistrLatticeMixin.Exports.

Module CBDistrLatticeMixin.
Section CBDistrLatticeMixin.
Variable (disp : unit) (T : bDistrLatticeType disp).

Record of_ := Build {
  sub : T T T;
  subKI : x y, y `&` sub x y = bottom;
  joinIB : x y, (x `&` y) `|` sub x y = x;
}.

Definition cbDistrLatticeMixin (m : of_) :=
  @CBDistrLattice.Mixin _ _ (sub m) (subKI m) (joinIB m).

End CBDistrLatticeMixin.

Module Exports.
Coercion cbDistrLatticeMixin : of_ >-> CBDistrLattice.mixin_of.
Notation cbDistrLatticeMixin := of_.
Notation CBDistrLatticeMixin := Build.
End Exports.

End CBDistrLatticeMixin.
Import CBDistrLatticeMixin.Exports.

Module CTBDistrLatticeMixin.
Section CTBDistrLatticeMixin.
Variable (disp : unit) (T : tbDistrLatticeType disp) (sub : T T T).

Record of_ := Build {
  compl : T T;
  complE : x, compl x = sub top x
}.

Definition ctbDistrLatticeMixin (m : of_) :=
  @CTBDistrLattice.Mixin _ _ sub (compl m) (complE m).

End CTBDistrLatticeMixin.

Module Exports.
Coercion ctbDistrLatticeMixin : of_ >-> CTBDistrLattice.mixin_of.
Notation ctbDistrLatticeMixin := of_.
Notation CTBDistrLatticeMixin := Build.
End Exports.

End CTBDistrLatticeMixin.
Import CTBDistrLatticeMixin.Exports.

Module TotalOrderMixin.
Section TotalOrderMixin.
Variable (disp : unit) (T : distrLatticeType disp).
Definition of_ := total (<=%O : rel T).

Definition totalOrderMixin (m : of_) : Total.mixin_of (DistrLattice.class T) :=
  m.

End TotalOrderMixin.

Module Exports.
Coercion totalOrderMixin : of_ >-> Total.mixin_of.
Notation totalOrderMixin := of_.
End Exports.

End TotalOrderMixin.
Import TotalOrderMixin.Exports.

Module DistrLatticePOrderMixin.
Section DistrLatticePOrderMixin.
Variable (disp : unit) (T : porderType disp).

Record of_ := Build {
  meet : T T T;
  join : T T T;
  meetC : commutative meet;
  joinC : commutative join;
  meetA : associative meet;
  joinA : associative join;
  joinKI : y x, meet x (join x y) = x;
  meetKU : y x, join x (meet x y) = x;
  leEmeet : x y, (x y) = (meet x y == x);
  meetUl : left_distributive meet join;
}.

Variable (m : of_).

Definition latticeMixin :=
  @LatticeMixin
    _ _ (meet m) (join m) (meetC m) (joinC m) (meetA m) (joinA m)
    (joinKI m) (meetKU m) (leEmeet m).

Definition distrLatticeMixin :=
  @DistrLatticeMixin _ (LatticeType T latticeMixin) (meetUl m).

End DistrLatticePOrderMixin.

Module Exports.
Notation distrLatticePOrderMixin := of_.
Notation DistrLatticePOrderMixin := Build.
Coercion latticeMixin : of_ >-> LatticeMixin.of_.
Coercion distrLatticeMixin : of_ >-> DistrLatticeMixin.of_.
Definition DistrLatticeOfPOrderType disp (T : porderType disp) (m : of_ T) :=
  DistrLatticeType (LatticeType T m) m.
End Exports.

End DistrLatticePOrderMixin.
Import DistrLatticePOrderMixin.Exports.

Module TotalLatticeMixin.
Section TotalLatticeMixin.
Variable (disp : unit) (T : latticeType disp).
Definition of_ := total (<=%O : rel T).

Variable (m : of_).
Implicit Types (x y z : T).

Let comparableT x y : x >=< y := m x y.

Fact meetUl : @left_distributive T T meet join.

Definition distrLatticeMixin := @DistrLatticeMixin _ T meetUl.

Definition totalMixin :
  totalOrderMixin (DistrLatticeType T distrLatticeMixin) := m.

End TotalLatticeMixin.

Module Exports.
Notation totalLatticeMixin := of_.
Coercion distrLatticeMixin : of_ >-> DistrLatticeMixin.of_.
Coercion totalMixin : of_ >-> totalOrderMixin.
Definition OrderOfLattice disp (T : latticeType disp) (m : of_ T) :=
  OrderType (DistrLatticeType T m) m.
End Exports.

End TotalLatticeMixin.
Import TotalLatticeMixin.Exports.

Module TotalPOrderMixin.
Section TotalPOrderMixin.
Variable (disp : unit) (T : porderType disp).
Definition of_ := total (<=%O : rel T).

Variable (m : of_).
Implicit Types (x y z : T).

Let comparableT x y : x >=< y := m x y.

Fact ltgtP x y :
  compare x y (min y x) (min x y) (max y x) (max x y)
              (y == x) (x == y) (x y) (x y) (x > y) (x < y).

Fact leP x y : le_xor_gt x y
  (min y x) (min x y) (max y x) (max x y) (x y) (y < x).

Definition meet := @min _ T.
Definition join := @max _ T.

Fact meetC : commutative meet.

Fact joinC : commutative join.

Fact meetA : associative meet.

Fact joinA : associative join.

Fact joinKI y x : meet x (join x y) = x.

Fact meetKU y x : join x (meet x y) = x.

Fact leEmeet x y : (x y) = (meet x y == x).

Definition latticeMixin :=
  @LatticeMixin _ T _ _ meetC joinC meetA joinA joinKI meetKU leEmeet.

Definition totalLatticeMixin :
  totalLatticeMixin (LatticeType T latticeMixin) := m.

End TotalPOrderMixin.

Module Exports.
Notation totalPOrderMixin := of_.
Coercion latticeMixin : of_ >-> LatticeMixin.of_.
Coercion totalLatticeMixin : of_ >-> TotalLatticeMixin.of_.
Definition OrderOfPOrder disp (T : porderType disp) (m : of_ T) :=
  OrderType (DistrLatticeType (LatticeType T m) m) m.
End Exports.

End TotalPOrderMixin.
Import TotalPOrderMixin.Exports.

Module LtPOrderMixin.
Section LtPOrderMixin.
Variable (T : eqType).

Record of_ := Build {
  le : rel T;
  lt : rel T;
  le_def : x y, le x y = (x == y) || lt x y;
  lt_irr : irreflexive lt;
  lt_trans : transitive lt;
}.

Variable (m : of_).

Fact lt_asym x y : (lt m x y && lt m y x) = false.

Fact lt_def x y : lt m x y = (y != x) && le m x y.

Fact le_refl : reflexive (le m).

Fact le_anti : antisymmetric (le m).

Fact le_trans : transitive (le m).

Definition lePOrderMixin : lePOrderMixin T :=
  @LePOrderMixin _ (le m) (lt m) lt_def le_refl le_anti le_trans.

End LtPOrderMixin.

Module Exports.
Notation ltPOrderMixin := of_.
Notation LtPOrderMixin := Build.
Coercion lePOrderMixin : of_ >-> LePOrderMixin.of_.
End Exports.

End LtPOrderMixin.
Import LtPOrderMixin.Exports.

Module MeetJoinMixin.
Section MeetJoinMixin.

Variable (T : choiceType).

Record of_ := Build {
  le : rel T;
  lt : rel T;
  meet : T T T;
  join : T T T;
  le_def : x y : T, le x y = (meet x y == x);
  lt_def : x y : T, lt x y = (y != x) && le x y;
  meetC : commutative meet;
  joinC : commutative join;
  meetA : associative meet;
  joinA : associative join;
  joinKI : y x : T, meet x (join x y) = x;
  meetKU : y x : T, join x (meet x y) = x;
  meetUl : left_distributive meet join;
  meetxx : idempotent meet;
}.

Variable (m : of_).

Fact le_refl : reflexive (le m).

Fact le_anti : antisymmetric (le m).

Fact le_trans : transitive (le m).

Definition porderMixin : lePOrderMixin T :=
  LePOrderMixin (lt_def m) le_refl le_anti le_trans.

Let T_porderType := POrderType tt T porderMixin.

Definition distrLatticeMixin : distrLatticePOrderMixin T_porderType :=
  @DistrLatticePOrderMixin _ T_porderType _ _
                           (meetC m) (joinC m) (meetA m) (joinA m)
                           (joinKI m) (meetKU m) (le_def m) (meetUl m).

End MeetJoinMixin.

Module Exports.
Notation meetJoinMixin := of_.
Notation MeetJoinMixin := Build.
Coercion porderMixin : of_ >-> lePOrderMixin.
Coercion distrLatticeMixin : of_ >-> DistrLatticePOrderMixin.of_.
Definition DistrLatticeOfChoiceType disp (T : choiceType) (m : of_ T) :=
  DistrLatticeType (LatticeType (POrderType disp T m) m) m.
End Exports.

End MeetJoinMixin.
Import MeetJoinMixin.Exports.

Module MeetJoinLeMixin.
Section MeetJoinLeMixin.

Variable (disp : unit) (T : porderType disp).

Record of_ := Build {
  meet : T T T;
  join : T T T;
  meetP : x y z, (x meet y z) = (x y) && (x z);
  joinP : x y z, (join x y z) = (x z) && (y z);
}.

Variable (m : of_).

Fact meet_lel x y : meet m x y meet m y x.
Fact meetC : commutative (meet m).
Fact meet_leL {x y} : (meet m x y) x.
Fact meet_leR {x y} : (meet m x y) y.

Fact join_lel x y : join m x y join m y x.
Fact joinC : commutative (join m).
Fact join_leL {x y} : x (join m x y).
Fact join_leR {x y} : y (join m x y).

Fact meetA : associative (meet m).
Fact joinA : associative (join m).
Fact joinKI y x : meet m x (join m x y) = x.
Fact meetKU y x : join m x (meet m x y) = x.
Fact leEmeet x y : (x y) = (meet m x y == x).

Definition latticeMixin :=
  LatticeMixin meetC joinC meetA joinA joinKI meetKU leEmeet.

End MeetJoinLeMixin.

Module Exports.
Coercion latticeMixin : of_ >-> Order.LatticeMixin.of_.
Notation meetJoinLeMixin := of_.
Notation MeetJoinLeMixin := Build.
End Exports.

End MeetJoinLeMixin.
Import MeetJoinLeMixin.Exports.

Module LeOrderMixin.
Section LeOrderMixin.

Variables (T : choiceType).

Record of_ := Build {
  le : rel T;
  lt : rel T;
  meet : T T T;
  join : T T T;
  lt_def : x y, lt x y = (y != x) && le x y;
  meet_def : x y, meet x y = if lt x y then x else y;
  join_def : x y, join x y = if lt x y then y else x;
  le_anti : antisymmetric le;
  le_trans : transitive le;
  le_total : total le;
}.

Variables (m : of_).

Fact le_refl : reflexive (le m).

Definition lePOrderMixin :=
  LePOrderMixin (lt_def m) le_refl (@le_anti m) (@le_trans m).

Let T_orderType :=
  OrderOfPOrder (le_total m : totalPOrderMixin (POrderType tt T lePOrderMixin)).

Implicit Types (x y z : T_orderType).

Fact meetE x y : meet m x y = x `&` y.
Fact joinE x y : join m x y = x `|` y.
Fact meetC : commutative (meet m).
Fact joinC : commutative (join m).
Fact meetA : associative (meet m).
Fact joinA : associative (join m).
Fact joinKI y x : meet m x (join m x y) = x.
Fact meetKU y x : join m x (meet m x y) = x.
Fact meetUl : left_distributive (meet m) (join m).
Fact meetxx : idempotent (meet m).
Fact le_def x y : x y = (meet m x y == x).

Definition distrLatticeMixin : meetJoinMixin T :=
  @MeetJoinMixin _ (le m) (lt m) (meet m) (join m) le_def (lt_def m)
    meetC joinC meetA joinA joinKI meetKU meetUl meetxx.

Let T_distrLatticeType := DistrLatticeOfChoiceType tt distrLatticeMixin.

Definition totalMixin : totalOrderMixin T_distrLatticeType := le_total m.

End LeOrderMixin.

Module Exports.
Notation leOrderMixin := of_.
Notation LeOrderMixin := Build.
Coercion distrLatticeMixin : of_ >-> meetJoinMixin.
Coercion totalMixin : of_ >-> totalOrderMixin.
Definition OrderOfChoiceType disp (T : choiceType) (m : of_ T) :=
  OrderType (DistrLatticeOfChoiceType disp m) m.
End Exports.

End LeOrderMixin.
Import LeOrderMixin.Exports.

Module LtOrderMixin.
Section LtOrderMixin.

Variable (T : choiceType).

Record of_ := Build {
  le : rel T;
  lt : rel T;
  meet : T T T;
  join : T T T;
  le_def : x y, le x y = (x == y) || lt x y;
  meet_def : x y, meet x y = if lt x y then x else y;
  join_def : x y, join x y = if lt x y then y else x;
  lt_irr : irreflexive lt;
  lt_trans : transitive lt;
  lt_total : x y, x != y lt x y || lt y x;
}.

Variables (m : of_).

Fact lt_def x y : lt m x y = (y != x) && le m x y.

Fact meet_def_le x y : meet m x y = if lt m x y then x else y.

Fact join_def_le x y : join m x y = if lt m x y then y else x.

Fact le_anti : antisymmetric (le m).

Fact le_trans : transitive (le m).

Fact le_total : total (le m).

Definition orderMixin : leOrderMixin T :=
  @LeOrderMixin _ (le m) (lt m) (meet m) (join m)
                lt_def meet_def_le join_def_le le_anti le_trans le_total.

End LtOrderMixin.

Module Exports.
Notation ltOrderMixin := of_.
Notation LtOrderMixin := Build.
Coercion orderMixin : of_ >-> leOrderMixin.
End Exports.

End LtOrderMixin.
Import LtOrderMixin.Exports.

Module CanMixin.
Section CanMixin.

Section Total.

Variables (disp : unit) (T : porderType disp).
Variables (disp' : unit) (T' : orderType disp') (f : T T').

Lemma MonoTotal : {mono f : x y / x y}
  totalPOrderMixin T' totalPOrderMixin T.

End Total.

Section Order.

Variables (T : choiceType) (disp : unit).

Section Partial.
Variables (T' : porderType disp) (f : T T').

Section PCan.
Variables (f' : T' option T) (f_can : pcancel f f').

Definition le (x y : T) := f x f y.
Definition lt (x y : T) := f x < f y.

Fact refl : reflexive le.
Fact anti : antisymmetric le.
Fact trans : transitive le.
Fact lt_def x y : lt x y = (y != x) && le x y.

Definition PcanPOrder := LePOrderMixin lt_def refl anti trans.

End PCan.

Definition CanPOrder f' (f_can : cancel f f') := PcanPOrder (can_pcan f_can).

End Partial.

Section Total.

Variables (T' : orderType disp) (f : T T').

Section PCan.

Variables (f' : T' option T) (f_can : pcancel f f').

Let T_porderType := POrderType disp T (PcanPOrder f_can).

Let total_le : total (le f).

Definition PcanOrder := LeOrderMixin
  (@lt_def _ _ _ f_can) (fun _ _erefl) (fun _ _erefl)
  (@anti _ _ _ f_can) (@trans _ _) total_le.

End PCan.

Definition CanOrder f' (f_can : cancel f f') := PcanOrder (can_pcan f_can).

End Total.
End Order.

Section Lattice.

Variables (disp : unit) (T : porderType disp).
Variables (disp' : unit) (T' : latticeType disp') (f : T T').

Variables (f' : T' T) (f_can : cancel f f') (f'_can : cancel f' f).
Variable (f_mono : {mono f : x y / x y}).

Definition meet (x y : T) := f' (meet (f x) (f y)).
Definition join (x y : T) := f' (join (f x) (f y)).

Lemma meetC : commutative meet.
Lemma joinC : commutative join.
Lemma meetA : associative meet.
Lemma joinA : associative join.
Lemma joinKI y x : meet x (join x y) = x.
Lemma meetKI y x : join x (meet x y) = x.
Lemma meet_eql x y : (x y) = (meet x y == x).

Definition IsoLattice :=
  @LatticeMixin _ T _ _ meetC joinC meetA joinA joinKI meetKI meet_eql.

End Lattice.

Section DistrLattice.

Variables (disp : unit) (T : porderType disp).
Variables (disp' : unit) (T' : distrLatticeType disp') (f : T T').

Variables (f' : T' T) (f_can : cancel f f') (f'_can : cancel f' f).
Variable (f_mono : {mono f : x y / x y}).

Lemma meetUl : left_distributive (meet f f') (join f f').

Definition IsoDistrLattice :=
  @DistrLatticeMixin _ (LatticeType T (IsoLattice f_can f'_can f_mono)) meetUl.

End DistrLattice.

End CanMixin.

Module Exports.
Notation MonoTotalMixin := MonoTotal.
Notation PcanPOrderMixin := PcanPOrder.
Notation CanPOrderMixin := CanPOrder.
Notation PcanOrderMixin := PcanOrder.
Notation CanOrderMixin := CanOrder.
Notation IsoLatticeMixin := IsoLattice.
Notation IsoDistrLatticeMixin := IsoDistrLattice.
End Exports.
End CanMixin.
Import CanMixin.Exports.

Module SubOrder.

Section Partial.
Context {disp : unit} {T : porderType disp} (P : {pred T}) (sT : subType P).

Definition sub_POrderMixin := PcanPOrderMixin (@valK _ _ sT).
Canonical sub_POrderType := Eval hnf in POrderType disp sT sub_POrderMixin.

Lemma leEsub (x y : sT) : (x y) = (val x val y).
Lemma ltEsub (x y : sT) : (x < y) = (val x < val y).

End Partial.

Section Total.
Context {disp : unit} {T : orderType disp} (P : {pred T}) (sT : subType P).

Definition sub_TotalOrderMixin : totalPOrderMixin (sub_POrderType sT) :=
  @MonoTotalMixin _ _ _ _ val (fun _ _erefl) (@le_total _ T).
Canonical sub_LatticeType :=
  Eval hnf in LatticeType sT sub_TotalOrderMixin.
Canonical sub_DistrLatticeType :=
  Eval hnf in DistrLatticeType sT sub_TotalOrderMixin.
Canonical sub_OrderType := Eval hnf in OrderType sT sub_TotalOrderMixin.

End Total.
Arguments sub_TotalOrderMixin {disp T} [P].

Module Exports.
Notation "[ 'porderMixin' 'of' T 'by' <: ]" :=
  (sub_POrderMixin _ : lePOrderMixin [eqType of T])
  (at level 0, format "[ 'porderMixin' 'of' T 'by' <: ]") : form_scope.

Notation "[ 'totalOrderMixin' 'of' T 'by' <: ]" :=
  (sub_TotalOrderMixin _ : totalPOrderMixin [porderType of T])
  (at level 0, only parsing) : form_scope.

Canonical sub_POrderType.
Canonical sub_LatticeType.
Canonical sub_DistrLatticeType.
Canonical sub_OrderType.

Definition leEsub := @leEsub.
Definition ltEsub := @ltEsub.
End Exports.
End SubOrder.
Import SubOrder.Exports.

INSTANCES
Canonical structures on nat
This is an example of creation of multiple canonical declarations on the same type, with distinct displays, on the example of natural numbers. We declare two distinct canonical orders:
  • leq which is total, and where meet and join are minn and maxn, on nat
  • dvdn which is partial, and where meet and join are gcdn and lcmn, on a "copy" of nat we name natdiv
The Module NatOrder defines leq as the canonical order on the type nat, i.e. without creating a "copy". We define and use nat_display and proceed like standard canonical structure declaration, except we use this display. We also use a single factory LeOrderMixin to instantiate three different canonical declarations porderType, distrLatticeType, orderType We finish by providing theorems to convert the operations of ordered and lattice types to their definition without structure abstraction.

Module NatOrder.
Section NatOrder.

Lemma nat_display : unit.

Lemma ltn_def x y : (x < y)%N = (y != x) && (x y)%N.

Definition orderMixin :=
  LeOrderMixin ltn_def (fun _ _erefl) (fun _ _erefl)
               anti_leq leq_trans leq_total.

Canonical porderType := POrderType nat_display nat orderMixin.
Canonical latticeType := LatticeType nat orderMixin.
Canonical bLatticeType := BLatticeType nat (BottomMixin leq0n).
Canonical distrLatticeType := DistrLatticeType nat orderMixin.
Canonical bDistrLatticeType := [bDistrLatticeType of nat].
Canonical orderType := OrderType nat orderMixin.

Lemma leEnat : le = leq.
Lemma ltEnat : lt = ltn.
Lemma minEnat : min = minn.
Lemma maxEnat : max = maxn.
Lemma botEnat : 0%O = 0%N :> nat.

End NatOrder.

Module Exports.
Canonical porderType.
Canonical latticeType.
Canonical bLatticeType.
Canonical distrLatticeType.
Canonical bDistrLatticeType.
Canonical orderType.
Definition leEnat := leEnat.
Definition ltEnat := ltEnat.
Definition minEnat := minEnat.
Definition maxEnat := maxEnat.
Definition botEnat := botEnat.
End Exports.
End NatOrder.

Module NatMonotonyTheory.
Section NatMonotonyTheory.
Import NatOrder.Exports.

Context {disp : unit} {T : porderType disp}.
Variables (D : {pred nat}) (f : nat T).
Hypothesis Dconvex : {in D &, i j k, i < k < j k \in D}.

Lemma homo_ltn_lt_in : {in D, i, i.+1 \in D f i < f i.+1}
  {in D &, {homo f : i j / i < j}}.

Lemma incn_inP : {in D, i, i.+1 \in D f i < f i.+1}
  {in D &, {mono f : i j / i j}}.

Lemma nondecn_inP : {in D, i, i.+1 \in D f i f i.+1}
  {in D &, {homo f : i j / i j}}.

Lemma nhomo_ltn_lt_in : {in D, i, i.+1 \in D f i > f i.+1}
  {in D &, {homo f : i j /~ i < j}}.

Lemma decn_inP : {in D, i, i.+1 \in D f i > f i.+1}
  {in D &, {mono f : i j /~ i j}}.

Lemma nonincn_inP : {in D, i, i.+1 \in D f i f i.+1}
  {in D &, {homo f : i j /~ i j}}.

Lemma homo_ltn_lt : ( i, f i < f i.+1) {homo f : i j / i < j}.

Lemma incnP : ( i, f i < f i.+1) {mono f : i j / i j}.

Lemma nondecnP : ( i, f i f i.+1) {homo f : i j / i j}.

Lemma nhomo_ltn_lt : ( i, f i > f i.+1) {homo f : i j /~ i < j}.

Lemma decnP : ( i, f i > f i.+1) {mono f : i j /~ i j}.

Lemma nonincnP : ( i, f i f i.+1) {homo f : i j /~ i j}.

End NatMonotonyTheory.
Arguments homo_ltn_lt_in {disp T} [D f].
Arguments incn_inP {disp T} [D f].
Arguments nondecn_inP {disp T} [D f].
Arguments nhomo_ltn_lt_in {disp T} [D f].
Arguments decn_inP {disp T} [D f].
Arguments nonincn_inP {disp T} [D f].
Arguments homo_ltn_lt {disp T} [f].
Arguments incnP {disp T} [f].
Arguments nondecnP {disp T} [f].
Arguments nhomo_ltn_lt {disp T} [f].
Arguments decnP {disp T} [f].
Arguments nonincnP {disp T} [f].

End NatMonotonyTheory.

The Module DvdSyntax introduces a new set of notations using the newly created display dvd_display. We first define the display as an opaque definition of type unit, and we use it as the first argument of the operator which display we want to change from the default one (here le, lt, dvd sdvd, meet, join, top and bottom, as well as big op notations on gcd and lcm). This notations will now be used for any ordered type which first parameter is set to dvd_display.

Lemma dvd_display : unit.

Module DvdSyntax.

Notation dvd := (@le dvd_display _).
Notation "@ 'dvd' T" :=
  (@le dvd_display T) (at level 10, T at level 8, only parsing) : fun_scope.
Notation sdvd := (@lt dvd_display _).
Notation "@ 'sdvd' T" :=
  (@lt dvd_display T) (at level 10, T at level 8, only parsing) : fun_scope.

Notation "x %| y" := (dvd x y) : order_scope.
Notation "x %<| y" := (sdvd x y) : order_scope.

Notation gcd := (@meet dvd_display _).
Notation "@ 'gcd' T" :=
  (@meet dvd_display T) (at level 10, T at level 8, only parsing) : fun_scope.
Notation lcm := (@join dvd_display _).
Notation "@ 'lcm' T" :=
  (@join dvd_display T) (at level 10, T at level 8, only parsing) : fun_scope.

Notation nat0 := (@top dvd_display _).
Notation nat1 := (@bottom dvd_display _).

Notation "\gcd_ ( i <- r | P ) F" :=
  (\big[gcd/nat0]_(i <- r | P%B) F%O) : order_scope.
Notation "\gcd_ ( i <- r ) F" :=
  (\big[gcd/nat0]_(i <- r) F%O) : order_scope.
Notation "\gcd_ ( i | P ) F" :=
  (\big[gcd/nat0]_(i | P%B) F%O) : order_scope.
Notation "\gcd_ i F" :=
  (\big[gcd/nat0]_i F%O) : order_scope.
Notation "\gcd_ ( i : I | P ) F" :=
  (\big[gcd/nat0]_(i : I | P%B) F%O) (only parsing) :
  order_scope.
Notation "\gcd_ ( i : I ) F" :=
  (\big[gcd/nat0]_(i : I) F%O) (only parsing) : order_scope.
Notation "\gcd_ ( m <= i < n | P ) F" :=
  (\big[gcd/nat0]_(m i < n | P%B) F%O) : order_scope.
Notation "\gcd_ ( m <= i < n ) F" :=
  (\big[gcd/nat0]_(m i < n) F%O) : order_scope.
Notation "\gcd_ ( i < n | P ) F" :=
  (\big[gcd/nat0]_(i < n | P%B) F%O) : order_scope.
Notation "\gcd_ ( i < n ) F" :=
  (\big[gcd/nat0]_(i < n) F%O) : order_scope.
Notation "\gcd_ ( i 'in' A | P ) F" :=
  (\big[gcd/nat0]_(i in A | P%B) F%O) : order_scope.
Notation "\gcd_ ( i 'in' A ) F" :=
  (\big[gcd/nat0]_(i in A) F%O) : order_scope.

Notation "\lcm_ ( i <- r | P ) F" :=
  (\big[lcm/nat1]_(i <- r | P%B) F%O) : order_scope.
Notation "\lcm_ ( i <- r ) F" :=
  (\big[lcm/nat1]_(i <- r) F%O) : order_scope.
Notation "\lcm_ ( i | P ) F" :=
  (\big[lcm/nat1]_(i | P%B) F%O) : order_scope.
Notation "\lcm_ i F" :=
  (\big[lcm/nat1]_i F%O) : order_scope.
Notation "\lcm_ ( i : I | P ) F" :=
  (\big[lcm/nat1]_(i : I | P%B) F%O) (only parsing) :
  order_scope.
Notation "\lcm_ ( i : I ) F" :=
  (\big[lcm/nat1]_(i : I) F%O) (only parsing) : order_scope.
Notation "\lcm_ ( m <= i < n | P ) F" :=
  (\big[lcm/nat1]_(m i < n | P%B) F%O) : order_scope.
Notation "\lcm_ ( m <= i < n ) F" :=
  (\big[lcm/nat1]_(m i < n) F%O) : order_scope.
Notation "\lcm_ ( i < n | P ) F" :=
  (\big[lcm/nat1]_(i < n | P%B) F%O) : order_scope.
Notation "\lcm_ ( i < n ) F" :=
  (\big[lcm/nat1]_(i < n) F%O) : order_scope.
Notation "\lcm_ ( i 'in' A | P ) F" :=
  (\big[lcm/nat1]_(i in A | P%B) F%O) : order_scope.
Notation "\lcm_ ( i 'in' A ) F" :=
  (\big[lcm/nat1]_(i in A) F%O) : order_scope.

End DvdSyntax.

The Module NatDvd defines dvdn as the canonical order on NatDvd.t, which is abbreviated using the notation natdvd at the end of the module. We use the newly defined dvd_display, described above. This looks like standard canonical structure declaration, except we use a display and we declare it on a "copy" of the type. We first recover structures that are common to both nat and natdiv (eqType, choiceType, countType) through the clone mechanisms, then we use a single factory MeetJoinMixin to instantiate both porderType and distrLatticeType canonical structures, and end with top and bottom. We finish by providing theorems to convert the operations of ordered and lattice types to their definition without structure abstraction.

Module NatDvd.
Section NatDvd.

Implicit Types m n p : nat.

Lemma lcmnn n : lcmn n n = n.

Lemma le_def m n : m %| n = (gcdn m n == m)%N.

Lemma joinKI n m : gcdn m (lcmn m n) = m.

Lemma meetKU n m : lcmn m (gcdn m n) = m.

Lemma meetUl : left_distributive gcdn lcmn.

Definition t_distrLatticeMixin := MeetJoinMixin le_def (fun _ _erefl _)
  gcdnC lcmnC gcdnA lcmnA joinKI meetKU meetUl gcdnn.

Definition t := nat.

Canonical eqType := [eqType of t].
Canonical choiceType := [choiceType of t].
Canonical countType := [countType of t].
Canonical porderType := POrderType dvd_display t t_distrLatticeMixin.
Canonical latticeType := LatticeType t t_distrLatticeMixin.
Canonical bLatticeType := BLatticeType t
  (BottomMixin (dvd1n : m : t, (1 %| m))).
Canonical tbLatticeType := TBLatticeType t
  (TopMixin (dvdn0 : m : t, (m %| 0))).
Canonical distrLatticeType := DistrLatticeType t t_distrLatticeMixin.
Canonical bDistrLatticeType := [bDistrLatticeType of t].
Canonical tbDistrLatticeType := [tbDistrLatticeType of t].

Import DvdSyntax.
Lemma dvdE : dvd = dvdn :> rel t.
Lemma sdvdE (m n : t) : m %<| n = (n != m) && (m %| n).
Lemma gcdE : gcd = gcdn :> (t t t).
Lemma lcmE : lcm = lcmn :> (t t t).
Lemma nat1E : nat1 = 1%N :> t.
Lemma nat0E : nat0 = 0%N :> t.

End NatDvd.
Module Exports.
Notation natdvd := t.
Canonical eqType.
Canonical choiceType.
Canonical countType.
Canonical porderType.
Canonical latticeType.
Canonical bLatticeType.
Canonical tbLatticeType.
Canonical distrLatticeType.
Canonical bDistrLatticeType.
Canonical tbDistrLatticeType.
Definition dvdEnat := dvdE.
Definition sdvdEnat := sdvdE.
Definition gcdEnat := gcdE.
Definition lcmEnat := lcmE.
Definition nat1E := nat1E.
Definition nat0E := nat0E.
End Exports.
End NatDvd.

Canonical structures on ordinal

Module OrdinalOrder.
Section OrdinalOrder.
Import NatOrder.

Lemma ord_display : unit.

Section PossiblyTrivial.
Variable (n : nat).

Definition porderMixin := [porderMixin of 'I_n by <:].
Canonical porderType := POrderType ord_display 'I_n (porderMixin).

Definition orderMixin := [totalOrderMixin of 'I_n by <:].
Canonical latticeType := LatticeType 'I_n orderMixin.
Canonical distrLatticeType := DistrLatticeType 'I_n orderMixin.
Canonical orderType := OrderType 'I_n orderMixin.
Canonical finPOrderType := [finPOrderType of 'I_n].

Lemma leEord : (le : rel 'I_n) = leq.
Lemma ltEord : (lt : rel 'I_n) = (fun m nm < n)%N.
End PossiblyTrivial.

Section NonTrivial.
Variable (n' : nat).
Let n := n'.+1.

Canonical bLatticeType :=
   BLatticeType 'I_n (BottomMixin (leq0n : x, ord0 x)).
Canonical bDistrLatticeType := [bDistrLatticeType of 'I_n].
Canonical tbLatticeType :=
   TBLatticeType 'I_n (TopMixin (@leq_ord _ : x, x ord_max)).
Canonical tbDistrLatticeType := [tbDistrLatticeType of 'I_n].
Canonical finLatticeType := [finLatticeType of 'I_n].
Canonical finDistrLatticeType := [finDistrLatticeType of 'I_n].
Canonical finOrderType := [finOrderType of 'I_n].

Lemma botEord : 0%O = ord0.
Lemma topEord : 1%O = ord_max.

End NonTrivial.

End OrdinalOrder.

Module Exports.
Canonical porderType.
Canonical latticeType.
Canonical bLatticeType.
Canonical tbLatticeType.
Canonical distrLatticeType.
Canonical bDistrLatticeType.
Canonical tbDistrLatticeType.
Canonical orderType.
Canonical finPOrderType.
Canonical finLatticeType.
Canonical finDistrLatticeType.
Canonical finOrderType.

Definition leEord := leEord.
Definition ltEord := ltEord.
Definition botEord := botEord.
Definition topEord := topEord.
End Exports.
End OrdinalOrder.

Canonical structure on bool

Module BoolOrder.
Section BoolOrder.
Implicit Types (x y : bool).

Fact bool_display : unit.

Fact andbE x y : x && y = if (x < y)%N then x else y.

Fact orbE x y : x || y = if (x < y)%N then y else x.

Fact ltn_def x y : (x < y)%N = (y != x) && (x y)%N.

Fact anti : antisymmetric (leq : rel bool).

Definition sub x y := x && ~~ y.

Lemma subKI x y : y && sub x y = false.
Lemma joinIB x y : (x && y) || sub x y = x.

Definition orderMixin :=
  LeOrderMixin ltn_def andbE orbE anti leq_trans leq_total.

Canonical porderType := POrderType bool_display bool orderMixin.
Canonical latticeType := LatticeType bool orderMixin.
Canonical bLatticeType := BLatticeType bool (@BottomMixin _ _ false leq0n).
Canonical tbLatticeType := TBLatticeType bool (@TopMixin _ _ true leq_b1).
Canonical distrLatticeType := DistrLatticeType bool orderMixin.
Canonical orderType := OrderType bool orderMixin.
Canonical bDistrLatticeType := [bDistrLatticeType of bool].
Canonical tbDistrLatticeType := [tbDistrLatticeType of bool].
Canonical cbDistrLatticeType := CBDistrLatticeType bool
  (@CBDistrLatticeMixin _ _ (fun x yx && ~~ y) subKI joinIB).
Canonical ctbDistrLatticeType := CTBDistrLatticeType bool
  (@CTBDistrLatticeMixin _ _ sub negb (fun xerefl : ~~ x = sub true x)).

Canonical finPOrderType := [finPOrderType of bool].
Canonical finLatticeType := [finLatticeType of bool].
Canonical finDistrLatticeType := [finDistrLatticeType of bool].
Canonical finCDistrLatticeType := [finCDistrLatticeType of bool].
Canonical finOrderType := [finOrderType of bool].

Lemma leEbool : le = (leq : rel bool).
Lemma ltEbool x y : (x < y) = (x < y)%N.
Lemma andEbool : meet = andb.
Lemma orEbool : meet = andb.
Lemma subEbool x y : x `\` y = x && ~~ y.
Lemma complEbool : compl = negb.

End BoolOrder.
Module Exports.
Canonical porderType.
Canonical latticeType.
Canonical bLatticeType.
Canonical tbLatticeType.
Canonical distrLatticeType.
Canonical bDistrLatticeType.
Canonical tbDistrLatticeType.
Canonical cbDistrLatticeType.
Canonical ctbDistrLatticeType.
Canonical orderType.
Canonical finPOrderType.
Canonical finLatticeType.
Canonical finDistrLatticeType.
Canonical finOrderType.
Canonical finCDistrLatticeType.
Definition leEbool := leEbool.
Definition ltEbool := ltEbool.
Definition andEbool := andEbool.
Definition orEbool := orEbool.
Definition subEbool := subEbool.
Definition complEbool := complEbool.
End Exports.
End BoolOrder.

Definition of prod_display.

Fact prod_display : unit.

Module Import ProdSyntax.

Notation "<=^p%O" := (@le prod_display _) : fun_scope.
Notation ">=^p%O" := (@ge prod_display _) : fun_scope.
Notation ">=^p%O" := (@ge prod_display _) : fun_scope.
Notation "<^p%O" := (@lt prod_display _) : fun_scope.
Notation ">^p%O" := (@gt prod_display _) : fun_scope.
Notation "<?=^p%O" := (@leif prod_display _) : fun_scope.
Notation ">=<^p%O" := (@comparable prod_display _) : fun_scope.
Notation "><^p%O" := (fun x y~~ (@comparable prod_display _ x y)) :
  fun_scope.

Notation "<=^p y" := (>=^p%O y) : order_scope.
Notation "<=^p y :> T" := (<=^p (y : T)) (only parsing) : order_scope.
Notation ">=^p y" := (<=^p%O y) : order_scope.
Notation ">=^p y :> T" := (>=^p (y : T)) (only parsing) : order_scope.

Notation "<^p y" := (>^p%O y) : order_scope.
Notation "<^p y :> T" := (<^p (y : T)) (only parsing) : order_scope.
Notation ">^p y" := (<^p%O y) : order_scope.
Notation ">^p y :> T" := (>^p (y : T)) (only parsing) : order_scope.

Notation "x <=^p y" := (<=^p%O x y) : order_scope.
Notation "x <=^p y :> T" := ((x : T) <=^p (y : T)) (only parsing) : order_scope.
Notation "x >=^p y" := (y <=^p x) (only parsing) : order_scope.
Notation "x >=^p y :> T" := ((x : T) >=^p (y : T)) (only parsing) : order_scope.

Notation "x <^p y" := (<^p%O x y) : order_scope.
Notation "x <^p y :> T" := ((x : T) <^p (y : T)) (only parsing) : order_scope.
Notation "x >^p y" := (y <^p x) (only parsing) : order_scope.
Notation "x >^p y :> T" := ((x : T) >^p (y : T)) (only parsing) : order_scope.

Notation "x <=^p y <=^p z" := ((x <=^p y) && (y <=^p z)) : order_scope.
Notation "x <^p y <=^p z" := ((x <^p y) && (y <=^p z)) : order_scope.
Notation "x <=^p y <^p z" := ((x <=^p y) && (y <^p z)) : order_scope.
Notation "x <^p y <^p z" := ((x <^p y) && (y <^p z)) : order_scope.

Notation "x <=^p y ?= 'iff' C" := (<?=^p%O x y C) : order_scope.
Notation "x <=^p y ?= 'iff' C :> T" := ((x : T) <=^p (y : T) ?= iff C)
  (only parsing) : order_scope.

Notation ">=<^p y" := [pred x | >=<^p%O x y] : order_scope.
Notation ">=<^p y :> T" := (>=<^p (y : T)) (only parsing) : order_scope.
Notation "x >=<^p y" := (>=<^p%O x y) : order_scope.

Notation "><^p y" := [pred x | ~~ (>=<^p%O x y)] : order_scope.
Notation "><^p y :> T" := (><^p (y : T)) (only parsing) : order_scope.
Notation "x ><^p y" := (~~ (><^p%O x y)) : order_scope.

Notation "x `&^p` y" := (@meet prod_display _ x y) : order_scope.
Notation "x `|^p` y" := (@join prod_display _ x y) : order_scope.

Notation "\join^p_ ( i <- r | P ) F" :=
  (\big[join/0]_(i <- r | P%B) F%O) : order_scope.
Notation "\join^p_ ( i <- r ) F" :=
  (\big[join/0]_(i <- r) F%O) : order_scope.
Notation "\join^p_ ( i | P ) F" :=
  (\big[join/0]_(i | P%B) F%O) : order_scope.
Notation "\join^p_ i F" :=
  (\big[join/0]_i F%O) : order_scope.
Notation "\join^p_ ( i : I | P ) F" :=
  (\big[join/0]_(i : I | P%B) F%O) (only parsing) : order_scope.
Notation "\join^p_ ( i : I ) F" :=
  (\big[join/0]_(i : I) F%O) (only parsing) : order_scope.
Notation "\join^p_ ( m <= i < n | P ) F" :=
 (\big[join/0]_(m i < n | P%B) F%O) : order_scope.
Notation "\join^p_ ( m <= i < n ) F" :=
 (\big[join/0]_(m i < n) F%O) : order_scope.
Notation "\join^p_ ( i < n | P ) F" :=
 (\big[join/0]_(i < n | P%B) F%O) : order_scope.
Notation "\join^p_ ( i < n ) F" :=
 (\big[join/0]_(i < n) F%O) : order_scope.
Notation "\join^p_ ( i 'in' A | P ) F" :=
 (\big[join/0]_(i in A | P%B) F%O) : order_scope.
Notation "\join^p_ ( i 'in' A ) F" :=
 (\big[join/0]_(i in A) F%O) : order_scope.

Notation "\meet^p_ ( i <- r | P ) F" :=
  (\big[meet/1]_(i <- r | P%B) F%O) : order_scope.
Notation "\meet^p_ ( i <- r ) F" :=
  (\big[meet/1]_(i <- r) F%O) : order_scope.
Notation "\meet^p_ ( i | P ) F" :=
  (\big[meet/1]_(i | P%B) F%O) : order_scope.
Notation "\meet^p_ i F" :=
  (\big[meet/1]_i F%O) : order_scope.
Notation "\meet^p_ ( i : I | P ) F" :=
  (\big[meet/1]_(i : I | P%B) F%O) (only parsing) : order_scope.
Notation "\meet^p_ ( i : I ) F" :=
  (\big[meet/1]_(i : I) F%O) (only parsing) : order_scope.
Notation "\meet^p_ ( m <= i < n | P ) F" :=
 (\big[meet/1]_(m i < n | P%B) F%O) : order_scope.
Notation "\meet^p_ ( m <= i < n ) F" :=
 (\big[meet/1]_(m i < n) F%O) : order_scope.
Notation "\meet^p_ ( i < n | P ) F" :=
 (\big[meet/1]_(i < n | P%B) F%O) : order_scope.
Notation "\meet^p_ ( i < n ) F" :=
 (\big[meet/1]_(i < n) F%O) : order_scope.
Notation "\meet^p_ ( i 'in' A | P ) F" :=
 (\big[meet/1]_(i in A | P%B) F%O) : order_scope.
Notation "\meet^p_ ( i 'in' A ) F" :=
 (\big[meet/1]_(i in A) F%O) : order_scope.

End ProdSyntax.

Definition of lexi_display.

Fact lexi_display : unit.

Module Import LexiSyntax.

Notation "<=^l%O" := (@le lexi_display _) : fun_scope.
Notation ">=^l%O" := (@ge lexi_display _) : fun_scope.
Notation ">=^l%O" := (@ge lexi_display _) : fun_scope.
Notation "<^l%O" := (@lt lexi_display _) : fun_scope.
Notation ">^l%O" := (@gt lexi_display _) : fun_scope.
Notation "<?=^l%O" := (@leif lexi_display _) : fun_scope.
Notation ">=<^l%O" := (@comparable lexi_display _) : fun_scope.
Notation "><^l%O" := (fun x y~~ (@comparable lexi_display _ x y)) :
  fun_scope.

Notation "<=^l y" := (>=^l%O y) : order_scope.
Notation "<=^l y :> T" := (<=^l (y : T)) (only parsing) : order_scope.
Notation ">=^l y" := (<=^l%O y) : order_scope.
Notation ">=^l y :> T" := (>=^l (y : T)) (only parsing) : order_scope.

Notation "<^l y" := (>^l%O y) : order_scope.
Notation "<^l y :> T" := (<^l (y : T)) (only parsing) : order_scope.
Notation ">^l y" := (<^l%O y) : order_scope.
Notation ">^l y :> T" := (>^l (y : T)) (only parsing) : order_scope.

Notation "x <=^l y" := (<=^l%O x y) : order_scope.
Notation "x <=^l y :> T" := ((x : T) <=^l (y : T)) (only parsing) : order_scope.
Notation "x >=^l y" := (y <=^l x) (only parsing) : order_scope.
Notation "x >=^l y :> T" := ((x : T) >=^l (y : T)) (only parsing) : order_scope.

Notation "x <^l y" := (<^l%O x y) : order_scope.
Notation "x <^l y :> T" := ((x : T) <^l (y : T)) (only parsing) : order_scope.
Notation "x >^l y" := (y <^l x) (only parsing) : order_scope.
Notation "x >^l y :> T" := ((x : T) >^l (y : T)) (only parsing) : order_scope.

Notation "x <=^l y <=^l z" := ((x <=^l y) && (y <=^l z)) : order_scope.
Notation "x <^l y <=^l z" := ((x <^l y) && (y <=^l z)) : order_scope.
Notation "x <=^l y <^l z" := ((x <=^l y) && (y <^l z)) : order_scope.
Notation "x <^l y <^l z" := ((x <^l y) && (y <^l z)) : order_scope.

Notation "x <=^l y ?= 'iff' C" := (<?=^l%O x y C) : order_scope.
Notation "x <=^l y ?= 'iff' C :> T" := ((x : T) <=^l (y : T) ?= iff C)
  (only parsing) : order_scope.

Notation ">=<^l y" := [pred x | >=<^l%O x y] : order_scope.
Notation ">=<^l y :> T" := (>=<^l (y : T)) (only parsing) : order_scope.
Notation "x >=<^l y" := (>=<^l%O x y) : order_scope.

Notation "><^l y" := [pred x | ~~ (>=<^l%O x y)] : order_scope.
Notation "><^l y :> T" := (><^l (y : T)) (only parsing) : order_scope.
Notation "x ><^l y" := (~~ (><^l%O x y)) : order_scope.

Notation meetlexi := (@meet lexi_display _).
Notation joinlexi := (@join lexi_display _).

Notation "x `&^l` y" := (meetlexi x y) : order_scope.
Notation "x `|^l` y" := (joinlexi x y) : order_scope.

End LexiSyntax.

We declare a "copy" of the cartesian product, which has canonical product order.

Module ProdOrder.
Section ProdOrder.

Definition type (disp : unit) (T T' : Type) := (T × T')%type.

Context {disp1 disp2 disp3 : unit}.


Canonical eqType (T T' : eqType):= Eval hnf in [eqType of T × T'].
Canonical choiceType (T T' : choiceType):= Eval hnf in [choiceType of T × T'].
Canonical countType (T T' : countType):= Eval hnf in [countType of T × T'].
Canonical finType (T T' : finType):= Eval hnf in [finType of T × T'].

Section POrder.
Variable (T : porderType disp1) (T' : porderType disp2).
Implicit Types (x y : T × T').

Definition le x y := (x.1 y.1) && (x.2 y.2).

Fact refl : reflexive le.

Fact anti : antisymmetric le.

Fact trans : transitive le.

Definition porderMixin := LePOrderMixin (rrefl _) refl anti trans.
Canonical porderType := POrderType disp3 (T × T') porderMixin.

Lemma leEprod x y : (x y) = (x.1 y.1) && (x.2 y.2).

Lemma ltEprod x y : (x < y) = [&& x != y, x.1 y.1 & x.2 y.2].

Lemma le_pair (x1 y1 : T) (x2 y2 : T') :
  (x1, x2) (y1, y2) :> T × T' = (x1 y1) && (x2 y2).

Lemma lt_pair (x1 y1 : T) (x2 y2 : T') : (x1, x2) < (y1, y2) :> T × T' =
  [&& (x1 != y1) || (x2 != y2), x1 y1 & x2 y2].

End POrder.

Section Lattice.
Variable (T : latticeType disp1) (T' : latticeType disp2).
Implicit Types (x y : T × T').

Definition meet x y := (x.1 `&` y.1, x.2 `&` y.2).
Definition join x y := (x.1 `|` y.1, x.2 `|` y.2).

Fact meetC : commutative meet.

Fact joinC : commutative join.

Fact meetA : associative meet.

Fact joinA : associative join.

Fact joinKI y x : meet x (join x y) = x.

Fact meetKU y x : join x (meet x y) = x.

Fact leEmeet x y : (x y) = (meet x y == x).

Definition latticeMixin :=
  Lattice.Mixin meetC joinC meetA joinA joinKI meetKU leEmeet.
Canonical latticeType := LatticeType (T × T') latticeMixin.

Lemma meetEprod x y : x `&` y = (x.1 `&` y.1, x.2 `&` y.2).

Lemma joinEprod x y : x `|` y = (x.1 `|` y.1, x.2 `|` y.2).

End Lattice.

Section BLattice.
Variable (T : bLatticeType disp1) (T' : bLatticeType disp2).

Fact le0x (x : T × T') : (0, 0) x :> T × T'.

Canonical bLatticeType := BLatticeType (T × T') (BLattice.Mixin le0x).

Lemma botEprod : 0 = (0, 0) :> T × T'.

End BLattice.

Section TBLattice.
Variable (T : tbLatticeType disp1) (T' : tbLatticeType disp2).

Fact lex1 (x : T × T') : x (top, top).

Canonical tbLatticeType := TBLatticeType (T × T') (TBLattice.Mixin lex1).

Lemma topEprod : 1 = (1, 1) :> T × T'.

End TBLattice.

Section DistrLattice.
Variable (T : distrLatticeType disp1) (T' : distrLatticeType disp2).

Fact meetUl : left_distributive (@meet T T') (@join T T').

Definition distrLatticeMixin := DistrLatticeMixin meetUl.
Canonical distrLatticeType := DistrLatticeType (T × T') distrLatticeMixin.

End DistrLattice.

Canonical bDistrLatticeType
          (T : bDistrLatticeType disp1) (T' : bDistrLatticeType disp2) :=
  [bDistrLatticeType of T × T'].

Canonical tbDistrLatticeType
          (T : tbDistrLatticeType disp1) (T' : tbDistrLatticeType disp2) :=
  [tbDistrLatticeType of T × T'].

Section CBDistrLattice.
Variable (T : cbDistrLatticeType disp1) (T' : cbDistrLatticeType disp2).
Implicit Types (x y : T × T').

Definition sub x y := (x.1 `\` y.1, x.2 `\` y.2).

Lemma subKI x y : y `&` sub x y = 0.

Lemma joinIB x y : x `&` y `|` sub x y = x.

Definition cbDistrLatticeMixin := CBDistrLatticeMixin subKI joinIB.
Canonical cbDistrLatticeType := CBDistrLatticeType (T × T') cbDistrLatticeMixin.

Lemma subEprod x y : x `\` y = (x.1 `\` y.1, x.2 `\` y.2).

End CBDistrLattice.

Section CTBDistrLattice.
Variable (T : ctbDistrLatticeType disp1) (T' : ctbDistrLatticeType disp2).
Implicit Types (x y : T × T').

Definition compl x : T × T' := (~` x.1, ~` x.2).

Lemma complE x : compl x = sub 1 x.

Definition ctbDistrLatticeMixin := CTBDistrLatticeMixin complE.
Canonical ctbDistrLatticeType :=
  CTBDistrLatticeType (T × T') ctbDistrLatticeMixin.

Lemma complEprod x : ~` x = (~` x.1, ~` x.2).

End CTBDistrLattice.

Canonical finPOrderType (T : finPOrderType disp1)
  (T' : finPOrderType disp2) := [finPOrderType of T × T'].

Canonical finLatticeType (T : finLatticeType disp1)
  (T' : finLatticeType disp2) := [finLatticeType of T × T'].

Canonical finDistrLatticeType (T : finDistrLatticeType disp1)
  (T' : finDistrLatticeType disp2) := [finDistrLatticeType of T × T'].

Canonical finCDistrLatticeType (T : finCDistrLatticeType disp1)
  (T' : finCDistrLatticeType disp2) := [finCDistrLatticeType of T × T'].

End ProdOrder.

Module Exports.

Notation "T *prod[ d ] T'" := (type d T T')
  (at level 70, d at next level, format "T *prod[ d ] T'") : type_scope.
Notation "T *p T'" := (type prod_display T T')
  (at level 70, format "T *p T'") : type_scope.

Canonical eqType.
Canonical choiceType.
Canonical countType.
Canonical finType.
Canonical porderType.
Canonical latticeType.
Canonical bLatticeType.
Canonical tbLatticeType.
Canonical distrLatticeType.
Canonical bDistrLatticeType.
Canonical tbDistrLatticeType.
Canonical cbDistrLatticeType.
Canonical ctbDistrLatticeType.
Canonical finPOrderType.
Canonical finLatticeType.
Canonical finDistrLatticeType.
Canonical finCDistrLatticeType.

Definition leEprod := @leEprod.
Definition ltEprod := @ltEprod.
Definition le_pair := @le_pair.
Definition lt_pair := @lt_pair.
Definition meetEprod := @meetEprod.
Definition joinEprod := @joinEprod.
Definition botEprod := @botEprod.
Definition topEprod := @topEprod.
Definition subEprod := @subEprod.
Definition complEprod := @complEprod.

End Exports.
End ProdOrder.
Import ProdOrder.Exports.

Module DefaultProdOrder.
Section DefaultProdOrder.
Context {disp1 disp2 : unit}.

Canonical prod_porderType (T : porderType disp1) (T' : porderType disp2) :=
  [porderType of T × T' for [porderType of T ×p T']].
Canonical prod_latticeType (T : latticeType disp1) (T' : latticeType disp2) :=
  [latticeType of T × T' for [latticeType of T ×p T']].
Canonical prod_bLatticeType
    (T : bLatticeType disp1) (T' : bLatticeType disp2) :=
  [bLatticeType of T × T' for [bLatticeType of T ×p T']].
Canonical prod_tbLatticeType
    (T : tbLatticeType disp1) (T' : tbLatticeType disp2) :=
  [tbLatticeType of T × T' for [tbLatticeType of T ×p T']].
Canonical prod_distrLatticeType
    (T : distrLatticeType disp1) (T' : distrLatticeType disp2) :=
  [distrLatticeType of T × T' for [distrLatticeType of T ×p T']].
Canonical prod_bDistrLatticeType
    (T : bDistrLatticeType disp1) (T' : bDistrLatticeType disp2) :=
  [bDistrLatticeType of T × T'].
Canonical prod_tbDistrLatticeType
    (T : tbDistrLatticeType disp1) (T' : tbDistrLatticeType disp2) :=
  [tbDistrLatticeType of T × T'].
Canonical prod_cbDistrLatticeType
    (T : cbDistrLatticeType disp1) (T' : cbDistrLatticeType disp2) :=
  [cbDistrLatticeType of T × T' for [cbDistrLatticeType of T ×p T']].
Canonical prod_ctbDistrLatticeType
    (T : ctbDistrLatticeType disp1) (T' : ctbDistrLatticeType disp2) :=
  [ctbDistrLatticeType of T × T' for [ctbDistrLatticeType of T ×p T']].
Canonical prod_finPOrderType (T : finPOrderType disp1)
  (T' : finPOrderType disp2) := [finPOrderType of T × T'].
Canonical prod_finLatticeType (T : finLatticeType disp1)
  (T' : finLatticeType disp2) := [finLatticeType of T × T'].
Canonical prod_finDistrLatticeType (T : finDistrLatticeType disp1)
  (T' : finDistrLatticeType disp2) := [finDistrLatticeType of T × T'].
Canonical prod_finCDistrLatticeType (T : finCDistrLatticeType disp1)
  (T' : finCDistrLatticeType disp2) := [finCDistrLatticeType of T × T'].

End DefaultProdOrder.
End DefaultProdOrder.

We declare lexicographic ordering on dependent pairs

Module SigmaOrder.
Section SigmaOrder.

Context {disp1 disp2 : unit}.

Section POrder.

Variable (T : porderType disp1) (T' : T porderType disp2).
Implicit Types (x y : {t : T & T' t}).

Definition le x y := (tag x tag y) &&
  ((tag x tag y) ==> (tagged x tagged_as x y)).
Definition lt x y := (tag x tag y) &&
  ((tag x tag y) ==> (tagged x < tagged_as x y)).

Fact refl : reflexive le.

Fact anti : antisymmetric le.

Fact trans : transitive le.

Fact lt_def x y : lt x y = (y != x) && le x y.

Definition porderMixin := LePOrderMixin lt_def refl anti trans.
Canonical porderType := POrderType disp2 {t : T & T' t} porderMixin.

Lemma leEsig x y : x y =
  (tag x tag y) && ((tag x tag y) ==> (tagged x tagged_as x y)).

Lemma ltEsig x y : x < y =
  (tag x tag y) && ((tag x tag y) ==> (tagged x < tagged_as x y)).

Lemma le_Taggedl x (u : T' (tag x)) : (Tagged T' u x) = (u tagged x).

Lemma le_Taggedr x (u : T' (tag x)) : (x Tagged T' u) = (tagged x u).

Lemma lt_Taggedl x (u : T' (tag x)) : (Tagged T' u < x) = (u < tagged x).

Lemma lt_Taggedr x (u : T' (tag x)) : (x < Tagged T' u) = (tagged x < u).

End POrder.

Section Total.
Variable (T : orderType disp1) (T' : T orderType disp2).
Implicit Types (x y : {t : T & T' t}).

Fact total : totalPOrderMixin [porderType of {t : T & T' t}].

Canonical latticeType := LatticeType {t : T & T' t} total.
Canonical distrLatticeType := DistrLatticeType {t : T & T' t} total.
Canonical orderType := OrderType {t : T & T' t} total.

End Total.

Section FinDistrLattice.
Variable (T : finOrderType disp1) (T' : T finOrderType disp2).

Fact le0x (x : {t : T & T' t}) : Tagged T' (0 : T' 0) x.
Canonical bLatticeType :=
  BLatticeType {t : T & T' t} (BLattice.Mixin le0x).
Canonical bDistrLatticeType := [bDistrLatticeType of {t : T & T' t}].

Lemma botEsig : 0 = Tagged T' (0 : T' 0).

Fact lex1 (x : {t : T & T' t}) : x Tagged T' (1 : T' 1).
Canonical tbLatticeType :=
  TBLatticeType {t : T & T' t} (TBLattice.Mixin lex1).
Canonical tbDistrLatticeType := [tbDistrLatticeType of {t : T & T' t}].

Lemma topEsig : 1 = Tagged T' (1 : T' 1).

End FinDistrLattice.

Canonical finPOrderType (T : finPOrderType disp1)
  (T' : T finPOrderType disp2) := [finPOrderType of {t : T & T' t}].
Canonical finLatticeType (T : finOrderType disp1)
  (T' : T finOrderType disp2) := [finLatticeType of {t : T & T' t}].
Canonical finDistrLatticeType (T : finOrderType disp1)
  (T' : T finOrderType disp2) := [finDistrLatticeType of {t : T & T' t}].
Canonical finOrderType (T : finOrderType disp1)
  (T' : T finOrderType disp2) := [finOrderType of {t : T & T' t}].

End SigmaOrder.

Module Exports.

Canonical porderType.
Canonical latticeType.
Canonical bLatticeType.
Canonical tbLatticeType.
Canonical distrLatticeType.
Canonical bDistrLatticeType.
Canonical tbDistrLatticeType.
Canonical orderType.
Canonical finPOrderType.
Canonical finLatticeType.
Canonical finDistrLatticeType.
Canonical finOrderType.

Definition leEsig := @leEsig.
Definition ltEsig := @ltEsig.
Definition le_Taggedl := @le_Taggedl.
Definition lt_Taggedl := @lt_Taggedl.
Definition le_Taggedr := @le_Taggedr.
Definition lt_Taggedr := @lt_Taggedr.
Definition topEsig := @topEsig.
Definition botEsig := @botEsig.

End Exports.
End SigmaOrder.
Import SigmaOrder.Exports.

We declare a "copy" of the cartesian product, which has canonical lexicographic order.

Module ProdLexiOrder.
Section ProdLexiOrder.

Definition type (disp : unit) (T T' : Type) := (T × T')%type.

Context {disp1 disp2 disp3 : unit}.


Canonical eqType (T T' : eqType):= Eval hnf in [eqType of T × T'].
Canonical choiceType (T T' : choiceType):= Eval hnf in [choiceType of T × T'].
Canonical countType (T T' : countType):= Eval hnf in [countType of T × T'].
Canonical finType (T T' : finType):= Eval hnf in [finType of T × T'].

Section POrder.
Variable (T : porderType disp1) (T' : porderType disp2).

Implicit Types (x y : T × T').

Definition le x y := (x.1 y.1) && ((x.1 y.1) ==> (x.2 y.2)).
Definition lt x y := (x.1 y.1) && ((x.1 y.1) ==> (x.2 < y.2)).

Fact refl : reflexive le.

Fact anti : antisymmetric le.

Fact trans : transitive le.

Fact lt_def x y : lt x y = (y != x) && le x y.

Definition porderMixin := LePOrderMixin lt_def refl anti trans.
Canonical porderType := POrderType disp3 (T × T') porderMixin.

Lemma leEprodlexi x y :
  (x y) = (x.1 y.1) && ((x.1 y.1) ==> (x.2 y.2)).

Lemma ltEprodlexi x y :
  (x < y) = (x.1 y.1) && ((x.1 y.1) ==> (x.2 < y.2)).

Lemma lexi_pair (x1 y1 : T) (x2 y2 : T') :
   (x1, x2) (y1, y2) :> T × T' = (x1 y1) && ((x1 y1) ==> (x2 y2)).

Lemma ltxi_pair (x1 y1 : T) (x2 y2 : T') :
   (x1, x2) < (y1, y2) :> T × T' = (x1 y1) && ((x1 y1) ==> (x2 < y2)).

End POrder.

Section Total.
Variable (T : orderType disp1) (T' : orderType disp2).
Implicit Types (x y : T × T').

Fact total : totalPOrderMixin [porderType of T × T'].

Canonical latticeType := LatticeType (T × T') total.
Canonical distrLatticeType := DistrLatticeType (T × T') total.
Canonical orderType := OrderType (T × T') total.

End Total.

Section FinDistrLattice.
Variable (T : finOrderType disp1) (T' : finOrderType disp2).

Fact le0x (x : T × T') : (0, 0) x :> T × T'.
Canonical bLatticeType := BLatticeType (T × T') (BLattice.Mixin le0x).
Canonical bDistrLatticeType := [bDistrLatticeType of T × T'].

Lemma botEprodlexi : 0 = (0, 0) :> T × T'.

Fact lex1 (x : T × T') : x (1, 1) :> T × T'.
Canonical tbLatticeType := TBLatticeType (T × T') (TBLattice.Mixin lex1).
Canonical tbDistrLatticeType := [tbDistrLatticeType of T × T'].

Lemma topEprodlexi : 1 = (1, 1) :> T × T'.

End FinDistrLattice.

Canonical finPOrderType (T : finPOrderType disp1)
    (T' : finPOrderType disp2) := [finPOrderType of T × T'].
Canonical finLatticeType (T : finOrderType disp1)
  (T' : finOrderType disp2) := [finLatticeType of T × T'].
Canonical finDistrLatticeType (T : finOrderType disp1)
  (T' : finOrderType disp2) := [finDistrLatticeType of T × T'].
Canonical finOrderType (T : finOrderType disp1)
  (T' : finOrderType disp2) := [finOrderType of T × T'].

Lemma sub_prod_lexi d (T : POrder.Exports.porderType disp1)
                      (T' : POrder.Exports.porderType disp2) :
   subrel (<=%O : rel (T ×prod[d] T')) (<=%O : rel (T × T')).

End ProdLexiOrder.

Module Exports.

Notation "T *lexi[ d ] T'" := (type d T T')
  (at level 70, d at next level, format "T *lexi[ d ] T'") : type_scope.
Notation "T *l T'" := (type lexi_display T T')
  (at level 70, format "T *l T'") : type_scope.

Canonical eqType.
Canonical choiceType.
Canonical countType.
Canonical finType.
Canonical porderType.
Canonical latticeType.
Canonical bLatticeType.
Canonical tbLatticeType.
Canonical distrLatticeType.
Canonical bDistrLatticeType.
Canonical tbDistrLatticeType.
Canonical orderType.
Canonical finPOrderType.
Canonical finLatticeType.
Canonical finDistrLatticeType.
Canonical finOrderType.

Definition leEprodlexi := @leEprodlexi.
Definition ltEprodlexi := @ltEprodlexi.
Definition lexi_pair := @lexi_pair.
Definition ltxi_pair := @ltxi_pair.
Definition topEprodlexi := @topEprodlexi.
Definition botEprodlexi := @botEprodlexi.
Definition sub_prod_lexi := @sub_prod_lexi.

End Exports.
End ProdLexiOrder.
Import ProdLexiOrder.Exports.

Module DefaultProdLexiOrder.
Section DefaultProdLexiOrder.
Context {disp1 disp2 : unit}.

Canonical prodlexi_porderType
    (T : porderType disp1) (T' : porderType disp2) :=
  [porderType of T × T' for [porderType of T ×l T']].
Canonical prodlexi_latticeType
    (T : orderType disp1) (T' : orderType disp2) :=
  [latticeType of T × T' for [latticeType of T ×l T']].
Canonical prodlexi_bLatticeType
    (T : finOrderType disp1) (T' : finOrderType disp2) :=
  [bLatticeType of T × T' for [bLatticeType of T ×l T']].
Canonical prodlexi_tbLatticeType
    (T : finOrderType disp1) (T' : finOrderType disp2) :=
  [tbLatticeType of T × T' for [tbLatticeType of T ×l T']].
Canonical prodlexi_distrLatticeType
    (T : orderType disp1) (T' : orderType disp2) :=
  [distrLatticeType of T × T' for [distrLatticeType of T ×l T']].
Canonical prodlexi_orderType
    (T : orderType disp1) (T' : orderType disp2) :=
  [orderType of T × T' for [orderType of T ×l T']].
Canonical prodlexi_bDistrLatticeType
    (T : finOrderType disp1) (T' : finOrderType disp2) :=
  [bDistrLatticeType of T × T'].
Canonical prodlexi_tbDistrLatticeType
    (T : finOrderType disp1) (T' : finOrderType disp2) :=
  [tbDistrLatticeType of T × T'].
Canonical prodlexi_finPOrderType (T : finPOrderType disp1)
  (T' : finPOrderType disp2) := [finPOrderType of T × T'].
Canonical prodlexi_finLatticeType (T : finOrderType disp1)
  (T' : finOrderType disp2) := [finLatticeType of T × T'].
Canonical prodlexi_finDistrLatticeType (T : finOrderType disp1)
  (T' : finOrderType disp2) := [finDistrLatticeType of T × T'].
Canonical prodlexi_finOrderType (T : finOrderType disp1)
  (T' : finOrderType disp2) := [finOrderType of T × T'].

End DefaultProdLexiOrder.
End DefaultProdLexiOrder.

We declare a "copy" of the sequences, which has canonical product order.

Module SeqProdOrder.
Section SeqProdOrder.

Definition type (disp : unit) T := seq T.

Context {disp disp' : unit}.


Canonical eqType (T : eqType):= Eval hnf in [eqType of seq T].
Canonical choiceType (T : choiceType):= Eval hnf in [choiceType of seq T].
Canonical countType (T : countType):= Eval hnf in [countType of seq T].

Section POrder.
Variable T : porderType disp.
Implicit Types s : seq T.

Fixpoint le s1 s2 := if s1 isn't x1 :: s1' then true else
                     if s2 isn't x2 :: s2' then false else
                     (x1 x2) && le s1' s2'.

Fact refl : reflexive le.

Fact anti : antisymmetric le.

Fact trans : transitive le.

Definition porderMixin := LePOrderMixin (rrefl _) refl anti trans.
Canonical porderType := POrderType disp' (seq T) porderMixin.

Lemma leEseq s1 s2 : s1 s2 = if s1 isn't x1 :: s1' then true else
                                if s2 isn't x2 :: s2' then false else
                                (x1 x2) && (s1' s2' :> seq _).

Lemma le0s s : [::] s :> seq _.

Lemma les0 s : s [::] = (s == [::]).

Lemma le_cons x1 s1 x2 s2 :
   x1 :: s1 x2 :: s2 :> seq _ = (x1 x2) && (s1 s2).

End POrder.

Section Lattice.
Variable T : latticeType disp.
Implicit Types s : seq T.

Fixpoint meet s1 s2 :=
  match s1, s2 with
    | x1 :: s1', x2 :: s2'(x1 `&` x2) :: meet s1' s2'
    | _, _[::]
  end.

Fixpoint join s1 s2 :=
  match s1, s2 with
    | [::], _s2 | _, [::]s1
    | x1 :: s1', x2 :: s2'(x1 `|` x2) :: join s1' s2'
  end.

Fact meetC : commutative meet.

Fact joinC : commutative join.

Fact meetA : associative meet.

Fact joinA : associative join.

Fact meetss s : meet s s = s.

Fact joinKI y x : meet x (join x y) = x.

Fact meetKU y x : join x (meet x y) = x.

Fact leEmeet x y : (x y) = (meet x y == x).

Definition latticeMixin :=
  Lattice.Mixin meetC joinC meetA joinA joinKI meetKU leEmeet.
Canonical latticeType := LatticeType (seq T) latticeMixin.

Lemma meetEseq s1 s2 : s1 `&` s2 = [seq x.1 `&` x.2 | x <- zip s1 s2].

Lemma meet_cons x1 s1 x2 s2 :
  (x1 :: s1 : seq T) `&` (x2 :: s2) = (x1 `&` x2) :: s1 `&` s2.

Lemma joinEseq s1 s2 : s1 `|` s2 =
  match s1, s2 with
    | [::], _s2 | _, [::]s1
    | x1 :: s1', x2 :: s2'(x1 `|` x2) :: ((s1' : seq _) `|` s2')
  end.

Lemma join_cons x1 s1 x2 s2 :
  (x1 :: s1 : seq T) `|` (x2 :: s2) = (x1 `|` x2) :: s1 `|` s2.

Canonical bLatticeType := BLatticeType (seq T) (BLattice.Mixin (@le0s _)).

Lemma botEseq : 0 = [::] :> seq T.

End Lattice.

Section DistrLattice.
Variable T : distrLatticeType disp.

Fact meetUl : left_distributive (@meet T) (@join T).

Definition distrLatticeMixin := DistrLatticeMixin meetUl.
Canonical distrLatticeType := DistrLatticeType (seq T) distrLatticeMixin.
Canonical bDistrLatticeType := [bDistrLatticeType of seq T].

End DistrLattice.

End SeqProdOrder.

Module Exports.

Notation seqprod_with := type.
Notation seqprod := (type prod_display).

Canonical porderType.
Canonical latticeType.
Canonical bLatticeType.
Canonical distrLatticeType.
Canonical bDistrLatticeType.

Definition leEseq := @leEseq.
Definition le0s := @le0s.
Definition les0 := @les0.
Definition le_cons := @le_cons.
Definition botEseq := @botEseq.
Definition meetEseq := @meetEseq.
Definition meet_cons := @meet_cons.
Definition joinEseq := @joinEseq.

End Exports.
End SeqProdOrder.
Import SeqProdOrder.Exports.

Module DefaultSeqProdOrder.
Section DefaultSeqProdOrder.
Context {disp : unit}.

Canonical seqprod_porderType (T : porderType disp) :=
  [porderType of seq T for [porderType of seqprod T]].
Canonical seqprod_latticeType (T : latticeType disp) :=
  [latticeType of seq T for [latticeType of seqprod T]].
Canonical seqprod_ndbLatticeType (T : latticeType disp) :=
  [bLatticeType of seq T for [bLatticeType of seqprod T]].
Canonical seqprod_distrLatticeType (T : distrLatticeType disp) :=
  [distrLatticeType of seq T for [distrLatticeType of seqprod T]].
Canonical seqprod_bDistrLatticeType (T : bDistrLatticeType disp) :=
  [bDistrLatticeType of seq T].

End DefaultSeqProdOrder.
End DefaultSeqProdOrder.

We declare a "copy" of the sequences, which has canonical lexicographic order.

Module SeqLexiOrder.
Section SeqLexiOrder.

Definition type (disp : unit) T := seq T.

Context {disp disp' : unit}.


Canonical eqType (T : eqType):= Eval hnf in [eqType of seq T].
Canonical choiceType (T : choiceType):= Eval hnf in [choiceType of seq T].
Canonical countType (T : countType):= Eval hnf in [countType of seq T].

Section POrder.
Variable T : porderType disp.
Implicit Types s : seq T.

Fixpoint le s1 s2 := if s1 isn't x1 :: s1' then true else
                     if s2 isn't x2 :: s2' then false else
                       (x1 x2) && ((x1 x2) ==> le s1' s2').
Fixpoint lt s1 s2 := if s2 isn't x2 :: s2' then false else
                     if s1 isn't x1 :: s1' then true else
                       (x1 x2) && ((x1 x2) ==> lt s1' s2').

Fact refl: reflexive le.

Fact anti: antisymmetric le.

Fact trans: transitive le.

Lemma lt_def s1 s2 : lt s1 s2 = (s2 != s1) && le s1 s2.

Definition porderMixin := LePOrderMixin lt_def refl anti trans.
Canonical porderType := POrderType disp' (seq T) porderMixin.

Lemma leEseqlexi s1 s2 :
   s1 s2 = if s1 isn't x1 :: s1' then true else
              if s2 isn't x2 :: s2' then false else
              (x1 x2) && ((x1 x2) ==> (s1' s2' :> seq T)).

Lemma ltEseqlexi s1 s2 :
   s1 < s2 = if s2 isn't x2 :: s2' then false else
              if s1 isn't x1 :: s1' then true else
              (x1 x2) && ((x1 x2) ==> (s1' < s2' :> seq T)).

Lemma lexi0s s : [::] s :> seq T.

Lemma lexis0 s : s [::] = (s == [::]).

Lemma ltxi0s s : ([::] < s :> seq T) = (s != [::]).

Lemma ltxis0 s : s < [::] = false.

Lemma lexi_cons x1 s1 x2 s2 :
  x1 :: s1 x2 :: s2 :> seq T = (x1 x2) && ((x1 x2) ==> (s1 s2)).

Lemma ltxi_cons x1 s1 x2 s2 :
  x1 :: s1 < x2 :: s2 :> seq T = (x1 x2) && ((x1 x2) ==> (s1 < s2)).

Lemma lexi_lehead x s1 y s2 : x :: s1 y :: s2 :> seq T x y.

Lemma ltxi_lehead x s1 y s2 : x :: s1 < y :: s2 :> seq T x y.

Lemma eqhead_lexiE (x : T) s1 s2 : (x :: s1 x :: s2 :> seq _) = (s1 s2).

Lemma eqhead_ltxiE (x : T) s1 s2 : (x :: s1 < x :: s2 :> seq _) = (s1 < s2).

Lemma neqhead_lexiE (x y : T) s1 s2 : x != y
  (x :: s1 y :: s2 :> seq _) = (x < y).

Lemma neqhead_ltxiE (x y : T) s1 s2 : x != y
  (x :: s1 < y :: s2 :> seq _) = (x < y).

End POrder.

Section Total.
Variable T : orderType disp.
Implicit Types s : seq T.

Fact total : totalPOrderMixin [porderType of seq T].

Canonical latticeType := LatticeType (seq T) total.
Canonical bLatticeType :=
  BLatticeType (seq T) (BLattice.Mixin (@lexi0s _)).
Canonical distrLatticeType := DistrLatticeType (seq T) total.
Canonical bDistrLatticeType := [bDistrLatticeType of seq T].
Canonical orderType := OrderType (seq T) total.

End Total.

Lemma sub_seqprod_lexi d (T : POrder.Exports.porderType disp) :
   subrel (<=%O : rel (seqprod_with d T)) (<=%O : rel (seq T)).

End SeqLexiOrder.

Module Exports.

Notation seqlexi_with := type.
Notation seqlexi := (type lexi_display).

Canonical porderType.
Canonical latticeType.
Canonical bLatticeType.
Canonical distrLatticeType.
Canonical bDistrLatticeType.
Canonical orderType.

Definition leEseqlexi := @leEseqlexi.
Definition lexi0s := @lexi0s.
Definition lexis0 := @lexis0.
Definition lexi_cons := @lexi_cons.
Definition lexi_lehead := @lexi_lehead.
Definition eqhead_lexiE := @eqhead_lexiE.
Definition neqhead_lexiE := @neqhead_lexiE.

Definition ltEseqltxi := @ltEseqlexi.
Definition ltxi0s := @ltxi0s.
Definition ltxis0 := @ltxis0.
Definition ltxi_cons := @ltxi_cons.
Definition ltxi_lehead := @ltxi_lehead.
Definition eqhead_ltxiE := @eqhead_ltxiE.
Definition neqhead_ltxiE := @neqhead_ltxiE.
Definition sub_seqprod_lexi := @sub_seqprod_lexi.

End Exports.
End SeqLexiOrder.
Import SeqLexiOrder.Exports.

Module DefaultSeqLexiOrder.
Section DefaultSeqLexiOrder.
Context {disp : unit}.

Canonical seqlexi_porderType (T : porderType disp) :=
  [porderType of seq T for [porderType of seqlexi T]].
Canonical seqlexi_latticeType (T : orderType disp) :=
  [latticeType of seq T for [latticeType of seqlexi T]].
Canonical seqlexi_bLatticeType (T : orderType disp) :=
  [bLatticeType of seq T for [bLatticeType of seqlexi T]].
Canonical seqlexi_distrLatticeType (T : orderType disp) :=
  [distrLatticeType of seq T for [distrLatticeType of seqlexi T]].
Canonical seqlexi_bDistrLatticeType (T : orderType disp) :=
  [bDistrLatticeType of seq T].
Canonical seqlexi_orderType (T : orderType disp) :=
  [orderType of seq T for [orderType of seqlexi T]].

End DefaultSeqLexiOrder.
End DefaultSeqLexiOrder.

We declare a "copy" of the tuples, which has canonical product order.

Module TupleProdOrder.
Import DefaultSeqProdOrder.

Section TupleProdOrder.

Definition type (disp : unit) n T := n.-tuple T.

Context {disp disp' : unit}.

Section Basics.
Variable (n : nat).

Canonical eqType (T : eqType):= Eval hnf in [eqType of n.-tuple T].
Canonical choiceType (T : choiceType):= Eval hnf in [choiceType of n.-tuple T].
Canonical countType (T : countType):= Eval hnf in [countType of n.-tuple T].
Canonical finType (T : finType):= Eval hnf in [finType of n.-tuple T].
End Basics.

Section POrder.
Implicit Types (T : porderType disp).

Definition porderMixin n T := [porderMixin of n.-tuple T by <:].
Canonical porderType n T := POrderType disp' (n.-tuple T) (porderMixin n T).

Lemma leEtprod n T (t1 t2 : n.-tuple T) :
   t1 t2 = [ i, tnth t1 i tnth t2 i].

Lemma ltEtprod n T (t1 t2 : n.-tuple T) :
  t1 < t2 = [ i, tnth t1 i != tnth t2 i] &&
            [ i, tnth t1 i tnth t2 i].

End POrder.

Section Lattice.
Variables (n : nat) (T : latticeType disp).
Implicit Types (t : n.-tuple T).

Definition meet t1 t2 : n.-tuple T :=
  [tuple of [seq x.1 `&` x.2 | x <- zip t1 t2]].
Definition join t1 t2 : n.-tuple T :=
  [tuple of [seq x.1 `|` x.2 | x <- zip t1 t2]].

Fact tnth_meet t1 t2 i : tnth (meet t1 t2) i = tnth t1 i `&` tnth t2 i.

Fact tnth_join t1 t2 i : tnth (join t1 t2) i = tnth t1 i `|` tnth t2 i.

Fact meetC : commutative meet.

Fact joinC : commutative join.

Fact meetA : associative meet.

Fact joinA : associative join.

Fact joinKI t2 t1 : meet t1 (join t1 t2) = t1.

Fact meetKU y x : join x (meet x y) = x.

Fact leEmeet t1 t2 : (t1 t2) = (meet t1 t2 == t1).

Definition latticeMixin :=
  Lattice.Mixin meetC joinC meetA joinA joinKI meetKU leEmeet.
Canonical latticeType := LatticeType (n.-tuple T) latticeMixin.

Lemma meetEtprod t1 t2 :
  t1 `&` t2 = [tuple of [seq x.1 `&` x.2 | x <- zip t1 t2]].

Lemma joinEtprod t1 t2 :
  t1 `|` t2 = [tuple of [seq x.1 `|` x.2 | x <- zip t1 t2]].

End Lattice.

Section BLattice.
Variables (n : nat) (T : bLatticeType disp).
Implicit Types (t : n.-tuple T).

Fact le0x t : [tuple of nseq n 0] t :> n.-tuple T.

Canonical bLatticeType := BLatticeType (n.-tuple T) (BLattice.Mixin le0x).

Lemma botEtprod : 0 = [tuple of nseq n 0] :> n.-tuple T.

End BLattice.

Section TBLattice.
Variables (n : nat) (T : tbLatticeType disp).
Implicit Types (t : n.-tuple T).

Fact lex1 t : t [tuple of nseq n 1] :> n.-tuple T.

Canonical tbLatticeType :=
  TBLatticeType (n.-tuple T) (TBLattice.Mixin lex1).

Lemma topEtprod : 1 = [tuple of nseq n 1] :> n.-tuple T.

End TBLattice.

Section DistrLattice.
Variables (n : nat) (T : distrLatticeType disp).
Implicit Types (t : n.-tuple T).

Fact meetUl : left_distributive (@meet n T) (@join n T).

Definition distrLatticeMixin := DistrLatticeMixin meetUl.
Canonical distrLatticeType := DistrLatticeType (n.-tuple T) distrLatticeMixin.

End DistrLattice.

Canonical bDistrLatticeType (n : nat) (T : bDistrLatticeType disp) :=
  [bDistrLatticeType of n.-tuple T].
Canonical tbDistrLatticeType (n : nat) (T : tbDistrLatticeType disp) :=
  [tbDistrLatticeType of n.-tuple T].

Section CBDistrLattice.
Variables (n : nat) (T : cbDistrLatticeType disp).
Implicit Types (t : n.-tuple T).

Definition sub t1 t2 : n.-tuple T :=
  [tuple of [seq x.1 `\` x.2 | x <- zip t1 t2]].

Fact tnth_sub t1 t2 i : tnth (sub t1 t2) i = tnth t1 i `\` tnth t2 i.

Lemma subKI t1 t2 : t2 `&` sub t1 t2 = 0.

Lemma joinIB t1 t2 : t1 `&` t2 `|` sub t1 t2 = t1.

Definition cbDistrLatticeMixin := CBDistrLatticeMixin subKI joinIB.
Canonical cbDistrLatticeType :=
  CBDistrLatticeType (n.-tuple T) cbDistrLatticeMixin.

Lemma subEtprod t1 t2 :
  t1 `\` t2 = [tuple of [seq x.1 `\` x.2 | x <- zip t1 t2]].

End CBDistrLattice.

Section CTBDistrLattice.
Variables (n : nat) (T : ctbDistrLatticeType disp).
Implicit Types (t : n.-tuple T).

Definition compl t : n.-tuple T := map_tuple compl t.

Fact tnth_compl t i : tnth (compl t) i = ~` tnth t i.

Lemma complE t : compl t = sub 1 t.

Definition ctbDistrLatticeMixin := CTBDistrLatticeMixin complE.
Canonical ctbDistrLatticeType :=
  CTBDistrLatticeType (n.-tuple T) ctbDistrLatticeMixin.

Lemma complEtprod t : ~` t = [tuple of [seq ~` x | x <- t]].

End CTBDistrLattice.

Canonical finPOrderType n (T : finPOrderType disp) :=
  [finPOrderType of n.-tuple T].

Canonical finLatticeType n (T : finLatticeType disp) :=
  [finLatticeType of n.-tuple T].

Canonical finDistrLatticeType n (T : finDistrLatticeType disp) :=
  [finDistrLatticeType of n.-tuple T].

Canonical finCDistrLatticeType n (T : finCDistrLatticeType disp) :=
  [finCDistrLatticeType of n.-tuple T].

End TupleProdOrder.

Module Exports.

Notation "n .-tupleprod[ disp ]" := (type disp n)
  (at level 2, disp at next level, format "n .-tupleprod[ disp ]") :
  type_scope.
Notation "n .-tupleprod" := (n.-tupleprod[prod_display])
  (at level 2, format "n .-tupleprod") : type_scope.

Canonical eqType.
Canonical choiceType.
Canonical countType.
Canonical finType.
Canonical porderType.
Canonical latticeType.
Canonical bLatticeType.
Canonical tbLatticeType.
Canonical distrLatticeType.
Canonical bDistrLatticeType.
Canonical tbDistrLatticeType.
Canonical cbDistrLatticeType.
Canonical ctbDistrLatticeType.
Canonical finPOrderType.
Canonical finLatticeType.
Canonical finDistrLatticeType.
Canonical finCDistrLatticeType.

Definition leEtprod := @leEtprod.
Definition ltEtprod := @ltEtprod.
Definition meetEtprod := @meetEtprod.
Definition joinEtprod := @joinEtprod.
Definition botEtprod := @botEtprod.
Definition topEtprod := @topEtprod.
Definition subEtprod := @subEtprod.
Definition complEtprod := @complEtprod.

Definition tnth_meet := @tnth_meet.
Definition tnth_join := @tnth_join.
Definition tnth_sub := @tnth_sub.
Definition tnth_compl := @tnth_compl.

End Exports.
End TupleProdOrder.
Import TupleProdOrder.Exports.

Module DefaultTupleProdOrder.
Section DefaultTupleProdOrder.
Context {disp : unit}.

Canonical tprod_porderType n (T : porderType disp) :=
  [porderType of n.-tuple T for [porderType of n.-tupleprod T]].
Canonical tprod_latticeType n (T : latticeType disp) :=
  [latticeType of n.-tuple T for [latticeType of n.-tupleprod T]].
Canonical tprod_bLatticeType n (T : bLatticeType disp) :=
  [bLatticeType of n.-tuple T for [bLatticeType of n.-tupleprod T]].
Canonical tprod_tbLatticeType n (T : tbLatticeType disp) :=
  [tbLatticeType of n.-tuple T for [tbLatticeType of n.-tupleprod T]].
Canonical tprod_distrLatticeType n (T : distrLatticeType disp) :=
  [distrLatticeType of n.-tuple T for [distrLatticeType of n.-tupleprod T]].
Canonical tprod_bDistrLatticeType n (T : bDistrLatticeType disp) :=
  [bDistrLatticeType of n.-tuple T].
Canonical tprod_tbDistrLatticeType n (T : tbDistrLatticeType disp) :=
  [tbDistrLatticeType of n.-tuple T].
Canonical tprod_cbDistrLatticeType n (T : cbDistrLatticeType disp) :=
  [cbDistrLatticeType of n.-tuple T for [cbDistrLatticeType of n.-tupleprod T]].
Canonical tprod_ctbDistrLatticeType n (T : ctbDistrLatticeType disp) :=
  [ctbDistrLatticeType of n.-tuple T for
                       [ctbDistrLatticeType of n.-tupleprod T]].
Canonical tprod_finPOrderType n (T : finPOrderType disp) :=
  [finPOrderType of n.-tuple T].
Canonical tprod_finLatticeType n (T : finLatticeType disp) :=
  [finLatticeType of n.-tuple T].
Canonical tprod_finDistrLatticeType n (T : finDistrLatticeType disp) :=
  [finDistrLatticeType of n.-tuple T].
Canonical tprod_finCDistrLatticeType n (T : finCDistrLatticeType disp) :=
  [finCDistrLatticeType of n.-tuple T].

End DefaultTupleProdOrder.
End DefaultTupleProdOrder.

We declare a "copy" of the tuples, which has canonical lexicographic order.

Module TupleLexiOrder.
Section TupleLexiOrder.
Import DefaultSeqLexiOrder.

Definition type (disp : unit) n T := n.-tuple T.

Context {disp disp' : unit}.

Section Basics.
Variable (n : nat).

Canonical eqType (T : eqType):= Eval hnf in [eqType of n.-tuple T].
Canonical choiceType (T : choiceType):= Eval hnf in [choiceType of n.-tuple T].
Canonical countType (T : countType):= Eval hnf in [countType of n.-tuple T].
Canonical finType (T : finType):= Eval hnf in [finType of n.-tuple T].
End Basics.

Section POrder.
Implicit Types (T : porderType disp).

Definition porderMixin n T := [porderMixin of n.-tuple T by <:].
Canonical porderType n T := POrderType disp' (n.-tuple T) (porderMixin n T).

Lemma lexi_tupleP n T (t1 t2 : n.-tuple T) :
   reflect ( k : 'I_n.+1, i : 'I_n, (i k)%N
               tnth t1 i tnth t2 i ?= iff (i != k :> nat)) (t1 t2).

Lemma ltxi_tupleP n T (t1 t2 : n.-tuple T) :
   reflect ( k : 'I_n, i : 'I_n, (i k)%N
               tnth t1 i tnth t2 i ?= iff (i != k :> nat)) (t1 < t2).

Lemma ltxi_tuplePlt n T (t1 t2 : n.-tuple T) : reflect
  (exists2 k : 'I_n, i : 'I_n, (i < k)%N tnth t1 i = tnth t2 i
                                                 & tnth t1 k < tnth t2 k)
  (t1 < t2).

End POrder.

Section Total.
Variables (n : nat) (T : orderType disp).
Implicit Types (t : n.-tuple T).

Definition total : totalPOrderMixin [porderType of n.-tuple T] :=
  [totalOrderMixin of n.-tuple T by <:].
Canonical latticeType := LatticeType (n.-tuple T) total.
Canonical distrLatticeType := DistrLatticeType (n.-tuple T) total.
Canonical orderType := OrderType (n.-tuple T) total.

End Total.

Section BDistrLattice.
Variables (n : nat) (T : finOrderType disp).
Implicit Types (t : n.-tuple T).

Fact le0x t : [tuple of nseq n 0] t :> n.-tuple T.

Canonical bLatticeType := BLatticeType (n.-tuple T) (BLattice.Mixin le0x).
Canonical bDistrLatticeType := [bDistrLatticeType of n.-tuple T].

Lemma botEtlexi : 0 = [tuple of nseq n 0] :> n.-tuple T.

End BDistrLattice.

Section TBDistrLattice.
Variables (n : nat) (T : finOrderType disp).
Implicit Types (t : n.-tuple T).

Fact lex1 t : t [tuple of nseq n 1].

Canonical tbLatticeType :=
  TBLatticeType (n.-tuple T) (TBLattice.Mixin lex1).
Canonical tbDistrLatticeType := [tbDistrLatticeType of n.-tuple T].

Lemma topEtlexi : 1 = [tuple of nseq n 1] :> n.-tuple T.

End TBDistrLattice.

Canonical finPOrderType n (T : finPOrderType disp) :=
  [finPOrderType of n.-tuple T].
Canonical finLatticeType n (T : finOrderType disp) :=
  [finLatticeType of n.-tuple T].
Canonical finDistrLatticeType n (T : finOrderType disp) :=
  [finDistrLatticeType of n.-tuple T].
Canonical finOrderType n (T : finOrderType disp) :=
  [finOrderType of n.-tuple T].

Lemma sub_tprod_lexi d n (T : POrder.Exports.porderType disp) :
   subrel (<=%O : rel (n.-tupleprod[d] T)) (<=%O : rel (n.-tuple T)).

End TupleLexiOrder.

Module Exports.

Notation "n .-tuplelexi[ disp ]" := (type disp n)
  (at level 2, disp at next level, format "n .-tuplelexi[ disp ]") :
  order_scope.
Notation "n .-tuplelexi" := (n.-tuplelexi[lexi_display])
  (at level 2, format "n .-tuplelexi") : order_scope.

Canonical eqType.
Canonical choiceType.
Canonical countType.
Canonical finType.
Canonical porderType.
Canonical latticeType.
Canonical bLatticeType.
Canonical tbLatticeType.
Canonical distrLatticeType.
Canonical orderType.
Canonical bDistrLatticeType.
Canonical tbDistrLatticeType.
Canonical finPOrderType.
Canonical finLatticeType.
Canonical finDistrLatticeType.
Canonical finOrderType.

Definition lexi_tupleP := @lexi_tupleP.
Arguments lexi_tupleP {disp disp' n T t1 t2}.
Definition ltxi_tupleP := @ltxi_tupleP.
Arguments ltxi_tupleP {disp disp' n T t1 t2}.
Definition ltxi_tuplePlt := @ltxi_tuplePlt.
Arguments ltxi_tuplePlt {disp disp' n T t1 t2}.
Definition topEtlexi := @topEtlexi.
Definition botEtlexi := @botEtlexi.
Definition sub_tprod_lexi := @sub_tprod_lexi.

End Exports.
End TupleLexiOrder.
Import TupleLexiOrder.Exports.

Module DefaultTupleLexiOrder.
Section DefaultTupleLexiOrder.
Context {disp : unit}.

Canonical tlexi_porderType n (T : porderType disp) :=
  [porderType of n.-tuple T for [porderType of n.-tuplelexi T]].
Canonical tlexi_latticeType n (T : orderType disp) :=
  [latticeType of n.-tuple T for [latticeType of n.-tuplelexi T]].
Canonical tlexi_bLatticeType n (T : finOrderType disp) :=
  [bLatticeType of n.-tuple T for [bLatticeType of n.-tuplelexi T]].
Canonical tlexi_tbLatticeType n (T : finOrderType disp) :=
  [tbLatticeType of n.-tuple T for [tbLatticeType of n.-tuplelexi T]].
Canonical tlexi_distrLatticeType n (T : orderType disp) :=
  [distrLatticeType of n.-tuple T for [distrLatticeType of n.-tuplelexi T]].
Canonical tlexi_bDistrLatticeType n (T : finOrderType disp) :=
  [bDistrLatticeType of n.-tuple T].
Canonical tlexi_tbDistrLatticeType n (T : finOrderType disp) :=
  [tbDistrLatticeType of n.-tuple T].
Canonical tlexi_orderType n (T : orderType disp) :=
  [orderType of n.-tuple T for [orderType of n.-tuplelexi T]].
Canonical tlexi_finPOrderType n (T : finPOrderType disp) :=
  [finPOrderType of n.-tuple T].
Canonical tlexi_finLatticeType n (T : finOrderType disp) :=
  [finLatticeType of n.-tuple T].
Canonical tlexi_finDistrLatticeType n (T : finOrderType disp) :=
  [finDistrLatticeType of n.-tuple T].
Canonical tlexi_finOrderType n (T : finOrderType disp) :=
  [finOrderType of n.-tuple T].

End DefaultTupleLexiOrder.
End DefaultTupleLexiOrder.

Module Import DualOrder.
Section DualOrder.
Context {disp : unit}.
Variable O : orderType disp.

Lemma dual_totalMixin : totalOrderMixin [distrLatticeType of O^d].
Canonical dual_orderType := OrderType O^d dual_totalMixin.

End DualOrder.

Canonical dual_finOrderType d (T : finOrderType d) :=
  [finOrderType of T^d].

Section DualOrderTheory.

Context {disp : unit} {T : orderType disp}.
Implicit Type s : seq T.

Lemma sorted_filter_gt x s :
  sorted <=%O s [seq y <- s | x < y] = drop (count ( x) s) s.

Lemma sorted_filter_ge x s :
  sorted <=%O s [seq y <- s | x y] = drop (count (< x) s) s.

Lemma nth_count_ge x x0 s i : sorted <=%O s
  (count (< x) s i < size s)%N x nth x0 s i.

Lemma nth_count_gt x x0 s i : sorted <=%O s
  (count ( x) s i < size s)%N x < nth x0 s i.

Lemma nth_count_eq x x0 s i : sorted <=%O s
  (count (< x) s i < count ( x) s)%N nth x0 s i = x.

End DualOrderTheory.

End DualOrder.

We declare a "copy" of the sets, which is canonically ordered by inclusion

Module SetSubsetOrder.
Section SetSubsetOrder.

Definition type (disp : unit) (T : finType) := {set T}.
Definition type_of (disp : unit) (T : finType) of phant T := type disp T.
Identity Coercion type_of_type_of : type_of >-> type.

Context {disp : unit} {T : finType}.
Implicit Type (A B C : {subset T}).

Lemma le_def A B : A \subset B = (A :&: B == A).

Lemma setKUC B A : A :&: (A :|: B) = A.

Lemma setKIC B A : A :|: (A :&: B) = A.

Definition t_distrLatticeMixin :=
  MeetJoinMixin le_def (fun _ _erefl _) (@setIC _) (@setUC _)
                (@setIA _) (@setUA _) setKUC setKIC (@setIUl _) (@setIid _).

Lemma subset_display : unit.

Canonical eqType := [eqType of {subset T}].
Canonical choiceType := [choiceType of {subset T}].
Canonical countType := [countType of {subset T}].
Canonical finType := [finType of {subset T}].
Canonical porderType := POrderType subset_display {subset T} t_distrLatticeMixin.
Canonical latticeType := LatticeType {subset T} t_distrLatticeMixin.
Canonical bLatticeType := BLatticeType {subset T}
  (BottomMixin (@sub0set _ : A, (set0 A :> {subset T})%O)).
Canonical tbLatticeType := TBLatticeType {subset T}
  (TopMixin (@subsetT _ : A, (A setT :> {subset T})%O)).
Canonical distrLatticeType := DistrLatticeType {subset T} t_distrLatticeMixin.
Canonical bDistrLatticeType := [bDistrLatticeType of {subset T}].
Canonical tbDistrLatticeType := [tbDistrLatticeType of {subset T}].

Lemma setIDv A B : B :&: (A :\: B) = set0.

Definition t_cbdistrLatticeMixin := CBDistrLatticeMixin setIDv (@setID _).
Canonical cbDistrLatticeType := CBDistrLatticeType {subset T} t_cbdistrLatticeMixin.

Lemma setTDsym A : ~: A = setT :\: A.

Definition t_ctbdistrLatticeMixin := CTBDistrLatticeMixin setTDsym.
Canonical ctbDistrLatticeType := CTBDistrLatticeType {subset T} t_ctbdistrLatticeMixin.

Canonical finPOrderType := [finPOrderType of {subset T}].
Canonical finLatticeType := [finLatticeType of {subset T}].
Canonical finDistrLatticeType := [finDistrLatticeType of {subset T}].
Canonical finCDistrLatticeType := [finCDistrLatticeType of {subset T}].

Lemma leEsubset A B : (A B) = (A \subset B).
Lemma meetEsubset A B : A `&` B = A :&: B.
Lemma joinEsubset A B : A `|` B = A :|: B.
Lemma botEsubset : 0 = set0 :> {subset T}.
Lemma topEsubset : 1 = setT :> {subset T}.
Lemma subEsubset A B : A `\` B = A :\: B.
Lemma complEsubset A : ~` A = ~: A.

End SetSubsetOrder.

Module Exports.
Notation "{ 'subset' [ d ] T }" := (type_of d (Phant T))
  (at level 2, d at next level, format "{ 'subset' [ d ] T }") : type_scope.
Notation "{ 'subset' T }" := {subset[subset_display] T}
  (at level 2, format "{ 'subset' T }") : type_scope.

Canonical eqType.
Canonical choiceType.
Canonical countType.
Canonical finType.
Canonical porderType.
Canonical latticeType.
Canonical bLatticeType.
Canonical tbLatticeType.
Canonical distrLatticeType.
Canonical bDistrLatticeType.
Canonical tbDistrLatticeType.
Canonical cbDistrLatticeType.
Canonical ctbDistrLatticeType.
Canonical finPOrderType.
Canonical finLatticeType.
Canonical finDistrLatticeType.
Canonical finCDistrLatticeType.

Definition leEsubset := @leEsubset.
Definition meetEsubset := @meetEsubset.
Definition joinEsubset := @joinEsubset.
Definition botEsubset := @botEsubset.
Definition topEsubset := @topEsubset.
Definition subEsubset := @subEsubset.
Definition complEsubset := @complEsubset.

End Exports.
End SetSubsetOrder.
Export SetSubsetOrder.Exports.

Module DefaultSetSubsetOrder.
Section DefaultSetSubsetOrder.
Context {disp : unit}.

Canonical subset_porderType (T : finType) :=
  [porderType of {set T} for [porderType of {subset T}]].
Canonical subset_latticeType (T : finType) :=
  [latticeType of {set T} for [latticeType of {subset T}]].
Canonical subset_bLatticeType (T : finType) :=
  [bLatticeType of {set T} for [bLatticeType of {subset T}]].
Canonical subset_tbLatticeType (T : finType) :=
  [tbLatticeType of {set T} for [tbLatticeType of {subset T}]].
Canonical subset_distrLatticeType (T : finType) :=
  [distrLatticeType of {set T} for [distrLatticeType of {subset T}]].
Canonical subset_bDistrLatticeType (T : finType) :=
  [bDistrLatticeType of {set T}].
Canonical subset_tbDistrLatticeType (T : finType) :=
  [tbDistrLatticeType of {set T}].
Canonical subset_cbDistrLatticeType (T : finType) :=
  [cbDistrLatticeType of {set T} for [cbDistrLatticeType of {subset T}]].
Canonical subset_ctbDistrLatticeType (T : finType) :=
  [ctbDistrLatticeType of {set T} for [ctbDistrLatticeType of {subset T}]].
Canonical subset_finPOrderType (T : finType) :=
  [finPOrderType of {set T}].
Canonical subset_finLatticeType (T : finType) :=
  [finLatticeType of {set T}].
Canonical subset_finDistrLatticeType (T : finType) :=
  [finDistrLatticeType of {set T}].
Canonical subset_finCDistrLatticeType (T : finType) :=
  [finCDistrLatticeType of {set T}].

End DefaultSetSubsetOrder.
End DefaultSetSubsetOrder.

Notation enum A := (sort <=%O (enum A)).

Section Enum.
Variables (d : unit) (T : finPOrderType d).

Lemma cardE (A : {pred T}) : #|A| = size (enum A).

Lemma mem_enum (A : {pred T}) : enum A =i A.

Lemma enum_uniq (A : {pred T}) : uniq (enum A).

Lemma cardT : #|T| = size (enum T).

Lemma enumT : enum T = sort <=%O (Finite.enum T).

Lemma enum0 : enum (pred0 : {pred T}) = [::].

Lemma enum1 (x : T) : enum (pred1 x) = [:: x].

Lemma eq_enum (A B : {pred T}) : A =i B enum A = enum B.

Lemma eq_cardT (A : {pred T}) : A =i predT #|A| = size (enum T).

Lemma set_enum (A : {set T}) : [set x in enum A] = A.

Lemma enum_set0 : enum (set0 : {set T}) = [::].

Lemma enum_setT : enum [set: T] = sort <=%O (Finite.enum T).

Lemma enum_set1 (a : T) : enum [set a] = [:: a].

End Enum.

Section Ordinal.
Import OrdinalOrder.Exports.

Lemma enum_ord n : enum 'I_n = fintype.enum 'I_n.

Lemma val_enum_ord n : [seq val i | i <- enum 'I_n] = iota 0 n.

Lemma size_enum_ord n : size (enum 'I_n) = n.

Lemma nth_enum_ord (n : nat) (i0 : 'I_n) (m : nat) :
  (m < n)%N nth i0 (enum 'I_n) m = m :> nat.

Lemma nth_ord_enum (n : nat) (i0 i : 'I_n) : nth i0 (enum 'I_n) i = i.

Lemma index_enum_ord (n : nat) (i : 'I_n) : index i (enum 'I_n) = i.

End Ordinal.

Lemma mono_sorted_enum d d' (T : finPOrderType d)
    (T' : porderType d') (f : T T') :
    total (<=%O : rel T) {mono f : x y / (x y)%O}
  sorted <=%O [seq f x | x <- Order.enum T].

Lemma mono_unique d (T T' : finPOrderType d) (f g : T T') :
    total (<=%O : rel T) (#|T'| #|T|)%N
    {mono f : x y / x y} {mono g : x y / x y}
  f =1 g.

This module should be exported on demand, as in module tagnat below
We circumvent a shortcomming of finOrderType which requires the type to be nonempty and we do not want to rule this out
Hypothesis (leT_total : total (<=%O : rel T)).

Lemma le_enum_val A : {mono @enum_val A : i j / i j}.

Lemma le_enum_rank_in x0 A (Ax0 : x0 \in A) :
  {in A &, {mono enum_rank_in Ax0 : x y / x y}}.

Lemma le_enum_rank : {mono enum_rank : i j / i j}.

End total.
End EnumVal.
Arguments enum_val {d T A}.
Arguments enum_rank {d T}.
End EnumVal.

Notation enum_val := enum_val.
Notation enum_rank_in := enum_rank_in.
Notation enum_rank := enum_rank.
Notation enum_valP := enum_valP.
Notation enum_val_nth := enum_val_nth.
Notation nth_enum_rank_in := nth_enum_rank_in.
Notation nth_enum_rank := nth_enum_rank.
Notation enum_rankK_in := enum_rankK_in.
Notation enum_rankK := enum_rankK.
Notation enum_valK_in := enum_valK_in.
Notation enum_valK := enum_valK.
Notation enum_rank_inj := enum_rank_inj.
Notation enum_val_inj := enum_val_inj.
Notation enum_val_bij_in := enum_val_bij_in.
Notation eq_enum_rank_in := eq_enum_rank_in.
Notation enum_rank_in_inj := enum_rank_in_inj.
Notation enum_rank_bij := enum_rank_bij.
Notation enum_val_bij := enum_val_bij.
Notation le_enum_val := le_enum_val.
Notation le_enum_rank_in := le_enum_rank_in.
Notation le_enum_rank := le_enum_rank.

Module Syntax.
Export POSyntax.
Export LatticeSyntax.
Export BLatticeSyntax.
Export TBLatticeSyntax.
Export CBDistrLatticeSyntax.
Export CTBDistrLatticeSyntax.
Export DualSyntax.
Export DvdSyntax.
End Syntax.

Module LTheory.
Export POCoercions.
Export POrderTheory.
Export DualPOrder.

Export DualLattice.
Export LatticeTheoryMeet.
Export LatticeTheoryJoin.
Export DistrLatticeTheory.
Export BLatticeTheory.
Export DualTBLattice.
Export TBLatticeTheory.
Export BDistrLatticeTheory.
Export DualTBDistrLattice.
Export TBDistrLatticeTheory.
Export DualOrder.
End LTheory.

Module CTheory.
Export LTheory CBDistrLatticeTheory CTBDistrLatticeTheory.
End CTheory.

Module TTheory.
Export LTheory TotalTheory.
End TTheory.

Module Theory.
Export CTheory TotalTheory.
End Theory.

End Order.

Export Order.Syntax.

Export Order.POrder.Exports.
Export Order.FinPOrder.Exports.
Export Order.Lattice.Exports.
Export Order.BLattice.Exports.
Export Order.TBLattice.Exports.
Export Order.FinLattice.Exports.
Export Order.DistrLattice.Exports.
Export Order.BDistrLattice.Exports.
Export Order.TBDistrLattice.Exports.
Export Order.FinDistrLattice.Exports.
Export Order.CBDistrLattice.Exports.
Export Order.CTBDistrLattice.Exports.
Export Order.FinCDistrLattice.Exports.
Export Order.Total.Exports.
Export Order.FinTotal.Exports.

Export Order.LePOrderMixin.Exports.
Export Order.BottomMixin.Exports.
Export Order.TopMixin.Exports.
Export Order.LatticeMixin.Exports.
Export Order.DistrLatticeMixin.Exports.
Export Order.CBDistrLatticeMixin.Exports.
Export Order.CTBDistrLatticeMixin.Exports.
Export Order.TotalOrderMixin.Exports.
Export Order.DistrLatticePOrderMixin.Exports.
Export Order.TotalLatticeMixin.Exports.
Export Order.TotalPOrderMixin.Exports.
Export Order.LtPOrderMixin.Exports.
Export Order.MeetJoinMixin.Exports.
Export Order.LeOrderMixin.Exports.
Export Order.LtOrderMixin.Exports.
Export Order.CanMixin.Exports.
Export Order.SubOrder.Exports.

Export Order.NatOrder.Exports.
Export Order.NatMonotonyTheory.
Export Order.NatDvd.Exports.
Export Order.OrdinalOrder.Exports.
Export Order.BoolOrder.Exports.
Export Order.ProdOrder.Exports.
Export Order.SigmaOrder.Exports.
Export Order.ProdLexiOrder.Exports.
Export Order.SeqProdOrder.Exports.
Export Order.SeqLexiOrder.Exports.
Export Order.TupleProdOrder.Exports.
Export Order.TupleLexiOrder.Exports.

Module DefaultProdOrder := Order.DefaultProdOrder.
Module DefaultSeqProdOrder := Order.DefaultSeqProdOrder.
Module DefaultTupleProdOrder := Order.DefaultTupleProdOrder.
Module DefaultProdLexiOrder := Order.DefaultProdLexiOrder.
Module DefaultSeqLexiOrder := Order.DefaultSeqLexiOrder.
Module DefaultTupleLexiOrder := Order.DefaultTupleLexiOrder.

Import Order.Theory.

Module tagnat.
Section tagnat.
Import Order.EnumVal.

Context {n : nat} {p_ : 'I_n nat}.


Implicit Types (i : 'I_n) (s : ordsum) (p : T).

Lemma card : #|{: T}| = \sum_i p_ i.

Definition sig : ordsum T := enum_val \o (cast_ord (esym card)).
Definition rank : T ordsum := (cast_ord card) \o enum_rank.

Lemma sigK : cancel sig rank.
Lemma sig_inj : injective sig.

Lemma rankK : cancel rank sig.
Lemma rank_inj : injective rank.

Definition sig1 s : 'I_n := tag (sig s).
Definition sig2 s : 'I_(p_ (sig1 s)) := tagged (sig s).
Definition Rank i (j : 'I_(p_ i)) := rank (Tagged _ j).

Lemma sigE12 s : sig s = @Tagged _ (sig1 s) _ (sig2 s).

Lemma rankE p : rank p = @Rank (tag p) (tagged p).

Lemma sig2K s : Rank (sig2 s) = s.

Lemma Rank1K i0 (k : 'I_(p_ i0)) : sig1 (Rank k) = i0.

Lemma Rank2K i0 (k : 'I_(p_ i0)) :
  sig2 (Rank k) = cast_ord (congr1 p_ (esym (Rank1K k))) k.
#[local] Hint Resolve sigK rankK : core.

Lemma rank_bij : bijective rank.
Lemma sig_bij : bijective sig.

Lemma rank_bij_on : {on [pred _ | true], bijective rank}.

Lemma sig_bij_on : {on [pred _ | true], bijective sig}.

Lemma le_sig : {mono sig : i j / i j}.

Lemma le_sig1 : {homo sig1 : i j / i j}.

Lemma le_rank : {mono rank : p q / p q}.

Lemma le_Rank i : {mono @Rank i : j k / j k}.

Lemma lt_sig : {mono sig : i j / i < j}.

Lemma lt_rank : {mono rank : p q / p < q}.

Lemma lt_Rank i : {mono @Rank i : j k / j < k}.

Lemma eq_Rank i i' (j : 'I_(p_ i)) (j': 'I_(p_ i')) :
  (Rank j == Rank j' :> nat) = (i == i') && (j == j' :> nat).

Lemma rankEsum p : rank p = \sum_(i < n | (i < tag p)%N) p_ i + tagged p :> nat.

Lemma RankEsum i j : @Rank i j = \sum_(k < n | (k < i)%N) p_ k + j :> nat.

Lemma rect s : s = \sum_(i < n | (i < sig1 s)%N) p_ i + sig2 s :> nat.

Lemma eqRank (i0 j : nat) (li0 : (i0 < n)%N) (lj : (j < p_ (Ordinal li0))%N) :
  (\sum_(i < n | (i < i0)%N) p_ i) + j = Rank (Ordinal lj) :> nat.

End tagnat.
End tagnat.
Arguments tagnat.Rank {n p_}.