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
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
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
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
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)
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
- Array.instOrd
- IO.FS.instOrdSystemTime
- IO.instOrdTaskState
- Lake.MTime.instOrd
- Lake.instOrdBuildType
- Lake.instOrdDate
- Lake.instOrdJobAction
- Lake.instOrdLogLevel
- Lake.instOrdPos
- Lake.instOrdSemVerCore
- Lake.instOrdStdVer
- Lake.instOrdVerbosity
- Lean.JsonNumber.instOrd
- Lean.JsonRpc.instOrdRequestID
- Lean.Lsp.DiagnosticWith.instOrdUserVisible
- Lean.Lsp.instOrdDiagnosticCode
- Lean.Lsp.instOrdDiagnosticRelatedInformation
- Lean.Lsp.instOrdDiagnosticSeverity
- Lean.Lsp.instOrdDiagnosticTag
- Lean.Lsp.instOrdLeanDiagnosticTag
- Lean.Lsp.instOrdLocation
- Lean.Lsp.instOrdPosition
- Lean.Lsp.instOrdRange
- Lean.Lsp.instOrdRefIdent
- Lean.Meta.LibrarySearch.instOrdDeclMod
- Lean.ReducibilityHints.instOrd
- Lean.SubExpr.Pos.instOrd
- List.instOrd
- Std.Time.Day.Ordinal.instOrdOfYear
- Std.Time.Day.instOrdOffset
- Std.Time.Day.instOrdOrdinal
- Std.Time.Hour.instOrdOffset
- Std.Time.Hour.instOrdOrdinal
- Std.Time.Internal.Bounded.instOrd
- Std.Time.Internal.instOrdUnitVal
- Std.Time.Millisecond.instOrdOffset
- Std.Time.Millisecond.instOrdOrdinal
- Std.Time.Minute.instOrdOffset
- Std.Time.Minute.instOrdOrdinal
- Std.Time.Month.instOrdOffset
- Std.Time.Month.instOrdOrdinal
- Std.Time.Month.instOrdQuarter
- Std.Time.Nanosecond.Ordinal.instOrdOfDay
- Std.Time.Nanosecond.instOrdOffset
- Std.Time.Nanosecond.instOrdOrdinal
- Std.Time.Nanosecond.instOrdSpan
- Std.Time.Second.instOrdOffset
- Std.Time.Second.instOrdOrdinal
- Std.Time.TimeZone.instOrdOffset
- Std.Time.Week.Ordinal.instOrdOfMonth
- Std.Time.Week.instOrdOffset
- Std.Time.Week.instOrdOrdinal
- Std.Time.Weekday.instOrd
- Std.Time.Weekday.instOrdOrdinal
- Std.Time.Year.instOrdOffset
- Std.Time.instOrdDateTime
- Std.Time.instOrdDuration
- Std.Time.instOrdHourMarker
- Std.Time.instOrdPlainDate
- Std.Time.instOrdPlainDateTime
- Std.Time.instOrdPlainTime
- Std.Time.instOrdTimestamp
- Std.Time.instOrdValidDate
- Vector.instOrd
- instOrdBitVec
- instOrdBool
- instOrdChar
- instOrdFin
- instOrdISize
- instOrdInt
- instOrdInt16
- instOrdInt32
- instOrdInt64
- instOrdInt8
- instOrdNat
- instOrdOption
- instOrdOrdering
- instOrdString
- instOrdUInt16
- instOrdUInt32
- instOrdUInt64
- instOrdUInt8
- instOrdUSize
Compares two values by comparing the results of applying a function.
In particular, x
is compared to y
by comparing f x
and f y
.
Examples:
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
Equations
- List.instOrd = { compare := List.compareLex compare }
Equations
- Array.compareLex cmp a₁ a₂ = Array.compareLex.go cmp a₁ a₂ 0
Equations
- Array.instOrd = { compare := Array.compareLex compare }
Equations
- Vector.compareLex cmp a b = Array.compareLex cmp a.toArray b.toArray
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
.
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 }