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
Equations
- instInhabitedOrdering = { default := Ordering.lt }
Swaps less-than and greater-than ordering results.
Examples:
Ordering.lt.swap = Ordering.gt
Ordering.eq.swap = Ordering.eq
Ordering.gt.swap = Ordering.lt
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
- Ordering.eq.then b = b
- a.then b = a
Instances For
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
- compareOfLessAndEq x y = if x < y then Ordering.lt else if x = y then Ordering.eq else Ordering.gt
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
- compareOfLessAndBEq x y = if x < y then Ordering.lt else if (x == y) = true then Ordering.eq else Ordering.gt
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
- 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
- instOrdFin n = { compare := fun (x y : Fin n) => compare ↑x ↑y }
Equations
- instOrdUInt8 = { compare := fun (x y : UInt8) => compareOfLessAndEq x y }
Equations
- instOrdUInt16 = { compare := fun (x y : UInt16) => compareOfLessAndEq x y }
Equations
- instOrdUInt32 = { compare := fun (x y : UInt32) => compareOfLessAndEq x y }
Equations
- instOrdUInt64 = { compare := fun (x y : UInt64) => compareOfLessAndEq x y }
Equations
- instOrdUSize = { compare := fun (x y : USize) => compareOfLessAndEq x y }
Equations
- instOrdChar = { compare := fun (x y : Char) => compareOfLessAndEq x y }
Equations
- instOrdInt8 = { compare := fun (x y : Int8) => compareOfLessAndEq x y }
Equations
- instOrdInt16 = { compare := fun (x y : Int16) => compareOfLessAndEq x y }
Equations
- instOrdInt32 = { compare := fun (x y : Int32) => compareOfLessAndEq x y }
Equations
- instOrdInt64 = { compare := fun (x y : Int64) => compareOfLessAndEq x y }
Equations
- instOrdISize = { compare := fun (x y : ISize) => compareOfLessAndEq x y }
Equations
- instOrdBitVec = { compare := fun (x y : BitVec n) => compareOfLessAndEq x y }
Equations
- instOrdOrdering = { compare := compareOn fun (x : Ordering) => x.toCtorIdx }
Equations
- List.compareLex cmp [] [] = Ordering.eq
- List.compareLex cmp [] x✝ = Ordering.lt
- List.compareLex cmp x✝ [] = Ordering.gt
- List.compareLex cmp (x_2 :: xs) (y :: ys) = match cmp x_2 y with | Ordering.lt => Ordering.lt | Ordering.eq => List.compareLex cmp xs ys | Ordering.gt => Ordering.gt
Instances For
Equations
- List.instOrd = { compare := List.compareLex compare }
Equations
- Array.compareLex cmp a₁ a₂ = Array.compareLex.go cmp a₁ a₂ 0
Instances For
Equations
- Array.instOrd = { compare := Array.compareLex compare }
Equations
- Vector.compareLex cmp a b = Array.compareLex cmp a.toArray b.toArray
Instances For
Equations
- Vector.instOrd = { compare := Vector.compareLex compare }
Equations
- instDecidableRelLt = inferInstanceAs (DecidableRel fun (a b : α) => compare a b = Ordering.lt)
Equations
- instDecidableRelLe = inferInstanceAs (DecidableRel fun (a b : α) => (compare a b).isLE = true)
Equations
- Ord.instDecidableRelLt = inferInstanceAs (DecidableRel fun (a b : α) => compare a b = Ordering.lt)
Equations
- Ord.instDecidableRelLe = inferInstanceAs (DecidableRel fun (a b : α) => (compare a b).isLE = true)
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
.
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.
Equations
- ord₁.lex' ord₂ = { compare := compareLex compare compare }