Equations
- instInhabitedOrdering = { default := Ordering.lt }
Equations
- Ordering.instDecidableEqOrdering x y = if h : Ordering.toCtorIdx x = Ordering.toCtorIdx y then isTrue ⋯ else isFalse ⋯
Swaps less and greater ordering results
Equations
- Ordering.swap x = 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
- Ordering.then x✝ x = match x✝, x with | Ordering.eq, f => f | o, x => o
Instances For
Check whether the ordering is 'equal'.
Equations
- Ordering.isEq x = match x with | Ordering.eq => true | x => false
Instances For
Check whether the ordering is 'not equal'.
Equations
- Ordering.isNe x = match x with | Ordering.eq => false | x => true
Instances For
Check whether the ordering is 'less than or equal to'.
Equations
- Ordering.isLE x = match x with | Ordering.gt => false | x => true
Instances For
Check whether the ordering is 'less than'.
Equations
- Ordering.isLT x = match x with | Ordering.lt => true | x => false
Instances For
Check whether the ordering is 'greater than'.
Equations
- Ordering.isGT x = match x with | Ordering.gt => true | x => false
Instances For
Check whether the ordering is 'greater than or equal'.
Equations
- Ordering.isGE x = match x with | Ordering.lt => false | x => true
Instances For
Equations
- compareOfLessAndEq x y = if x < y then Ordering.lt else if x = y 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 = Ordering.then (cmp₁ a b) (cmp₂ a b)
Instances For
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
- instDecidableRelLtLtOfOrd = inferInstanceAs (DecidableRel fun (a b : α) => (compare a b == Ordering.lt) = true)
Equations
- instDecidableRelLeLeOfOrd = inferInstanceAs (DecidableRel fun (a b : α) => Ordering.isLE (compare a b) = true)
Invert the order of an Ord
instance.
Equations
- Ord.opposite ord = { compare := fun (x y : α) => compare y x }