Equations
- instInhabitedOrdering = { default := Ordering.lt }
Swaps less and greater ordering results
Equations
- Ordering.lt.swap = Ordering.gt
- Ordering.eq.swap = Ordering.eq
- Ordering.gt.swap = 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
- Ordering.eq.then x = x
- x✝.then x = x✝
Instances For
Check whether the ordering is 'not equal'.
Equations
- Ordering.eq.isNe = false
- x.isNe = true
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 'less than'.
Equations
- Ordering.lt.isLT = true
- x.isLT = false
Instances For
Check whether the ordering is 'greater than'.
Equations
- Ordering.gt.isGT = true
- x.isGT = false
Instances For
Check whether the ordering is 'greater than or equal'.
Equations
- Ordering.lt.isGE = false
- x.isGE = 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 }