Equations
- instInhabitedOrdering = { default := Ordering.lt }
If o₁
and o₂
are Ordering
, then o₁.then o₂
returns o₁
unless it is .eq
,
in which case it returns o₂
. Additionally, it has "short-circuiting" semantics similar to
boolean x && y
: if o₁
is not .eq
then the expression for o₂
is not evaluated.
This is a useful primitive for constructing lexicographic comparator functions:
structure Person where
name : String
age : Nat
instance : Ord Person where
compare a b := (compare a.name b.name).then (compare b.age a.age)
This example will sort people first by name (in ascending order) and will sort people with
the same name by age (in descending order). (If all fields are sorted ascending and in the same
order as they are listed in the structure, you can also use deriving Ord
on the structure
definition for the same effect.)
Equations
- Ordering.eq.then x = x
- x✝.then x = x✝
Instances For
Check whether the ordering is 'less than or equal to'.
Equations
- Ordering.gt.isLE = false
- x.isLE = true
Instances For
Check whether the ordering is 'greater than or equal'.
Instances For
Yields an Ordering
s.t. x < y
corresponds to Ordering.lt
/ Ordering.gt
and
x = y
corresponds to Ordering.eq
.
Equations
- compareOfLessAndEq x y = if x < y then Ordering.lt else if x = y then Ordering.eq else Ordering.gt
Instances For
Yields an Ordering
s.t. x < y
corresponds to Ordering.lt
/ Ordering.gt
and
x == y
corresponds to Ordering.eq
.
Equations
- compareOfLessAndBEq x y = if x < y then Ordering.lt else if (x == y) = true then Ordering.eq else Ordering.gt
Instances For
Compare a
and b
lexicographically by cmp₁
and cmp₂
. a
and b
are
first compared by cmp₁
. If this returns 'equal', a
and b
are compared
by cmp₂
to break the tie.
Equations
- compareLex cmp₁ cmp₂ a b = (cmp₁ a b).then (cmp₂ a b)
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
Equations
- instOrdNat = { compare := fun (x y : Nat) => compareOfLessAndEq x y }
Equations
- instOrdInt = { compare := fun (x y : Int) => compareOfLessAndEq x y }
Equations
- instOrdBool = { compare := fun (x x_1 : Bool) => match x, x_1 with | false, true => Ordering.lt | true, false => Ordering.gt | x, x_2 => Ordering.eq }
Equations
- instOrdString = { compare := fun (x y : String) => compareOfLessAndEq x y }
Equations
- instOrdUInt64 = { compare := fun (x y : UInt64) => compareOfLessAndEq x y }
Equations
- instOrdChar = { compare := fun (x y : Char) => compareOfLessAndEq x y }
Equations
- instDecidableRelLe = inferInstanceAs (DecidableRel fun (a b : α) => (compare a b).isLE = true)