The result of a comparison according to a total order.
The relationship between the compared items may be:
Ordering.lt
: less thanOrdering.eq
: equalOrdering.gt
: greater than
Instances For
Swaps less-than and greater-than ordering results.
Examples:
Ordering.lt.swap = Ordering.gt
Ordering.eq.swap = Ordering.eq
Ordering.gt.swap = Ordering.lt
Equations
Instances For
If a
and b
are Ordering
, then a.then b
returns a
unless it is .eq
, in which case it
returns b
. Additionally, it has “short-circuiting” behavior similar to boolean &&
: if a
is not
.eq
then the expression for b
is not evaluated.
This is a useful primitive for constructing lexicographic comparator functions. The deriving Ord
syntax on a structure uses the Ord
instance to compare each field in order, combining the results
equivalently to Ordering.then
.
Use compareLex
to lexicographically combine two comparison functions.
Examples:
structure Person where
name : String
age : Nat
-- Sort people first by name (in ascending order), and people with the same name by age (in
-- descending order)
instance : Ord Person where
compare a b := (compare a.name b.name).then (compare b.age a.age)
#eval Ord.compare (⟨"Gert", 33⟩ : Person) ⟨"Dana", 50⟩
Ordering.gt
#eval Ord.compare (⟨"Gert", 33⟩ : Person) ⟨"Gert", 50⟩
Ordering.gt
#eval Ord.compare (⟨"Gert", 33⟩ : Person) ⟨"Gert", 20⟩
Ordering.lt
Equations
Instances For
Equations
Uses decidable less-than and equality relations to find an Ordering
.
In particular, if x < y
then the result is Ordering.lt
. If x = y
then the result is
Ordering.eq
. Otherwise, it is Ordering.gt
.
compareOfLessAndBEq
uses BEq
instead of DecidableEq
.
Equations
Instances For
Uses a decidable less-than relation and Boolean equality to find an Ordering
.
In particular, if x < y
then the result is Ordering.lt
. If x == y
then the result is
Ordering.eq
. Otherwise, it is Ordering.gt
.
compareOfLessAndEq
uses DecidableEq
instead of BEq
.
Equations
Instances For
Compares a
and b
lexicographically by cmp₁
and cmp₂
.
a
and b
are first compared by cmp₁
. If this returns Ordering.eq
, a
and b
are compared
by cmp₂
to break the tie.
To lexicographically combine two Ordering
s, use Ordering.then
.
Equations
Instances For
Ord α
provides a computable total order on α
, in terms of the
compare : α → α → Ordering
function.
Typically instances will be transitive, reflexive, and antisymmetric, but this is not enforced by the typeclass.
There is a derive handler, so appending deriving Ord
to an inductive type or structure
will attempt to create an Ord
instance.
- compare : α → α → Ordering
Compare two elements in
α
using the comparator contained in an[Ord α]
instance.
Instances
Inverts the order of an Ord
instance.
The result is an Ord α
instance that returns Ordering.lt
when ord
would return Ordering.gt
and that returns Ordering.gt
when ord
would return Ordering.lt
.
Equations
Instances For
Constructs an Ord
instance from two existing instances by combining them lexicographically.
The resulting instance compares elements first by ord₁
and then, if this returns Ordering.eq
, by
ord₂
.
The function compareLex
can be used to perform this comparison without constructing an
intermediate Ord
instance. Ordering.then
can be used to lexicographically combine the results of
comparisons.