Equations
- Ordering.instDecidableEqOrdering x y = if h : Ordering.toCtorIdx x = Ordering.toCtorIdx y then isTrue (_ : x = y) else isFalse (_ : x = y → False)
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'.
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
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
Invert the order of an Ord
instance.
Equations
- Ord.opposite ord = { compare := fun (x y : α) => compare y x }