Equations
- instInhabitedOrdering = { default := Ordering.lt }
Swaps less and greater ordering results
Equations
- x.swap = match x with | Ordering.lt => Ordering.gt | Ordering.eq => Ordering.eq | Ordering.gt => Ordering.lt
Instances For
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
- x✝.then x = match x✝, x with | Ordering.eq, f => f | o, x => o
Instances For
Check whether the ordering is 'equal'.
Equations
- x.isEq = match x with | Ordering.eq => true | x => false
Instances For
Check whether the ordering is 'not equal'.
Equations
- x.isNe = match x with | Ordering.eq => false | x => true
Instances For
Check whether the ordering is 'less than or equal to'.
Equations
- x.isLE = match x with | Ordering.gt => false | x => true
Instances For
Check whether the ordering is 'less than'.
Equations
- x.isLT = match x with | Ordering.lt => true | x => false
Instances For
Check whether the ordering is 'greater than'.
Equations
- x.isGT = match x with | Ordering.gt => true | x => false
Instances For
Check whether the ordering is 'greater than or equal'.
Equations
- x.isGE = match x with | Ordering.lt => false | x => true
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
- 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
- instDecidableRelLt = inferInstanceAs (DecidableRel fun (a b : α) => compare a b = Ordering.lt)
Equations
- instDecidableRelLe = inferInstanceAs (DecidableRel fun (a b : α) => (compare a b).isLE = true)
Creates an order which compares elements of an Array
in lexicographic order.
Equations
- Ord.arrayOrd = { compare := fun (x y : Array α) => let x_1 := a.toLT; let x_2 := a.toBEq; compareOfLessAndBEq x.toList y.toList }