diff options
Diffstat (limited to 'std/prelude/compare.pk')
-rw-r--r-- | std/prelude/compare.pk | 36 |
1 files changed, 21 insertions, 15 deletions
diff --git a/std/prelude/compare.pk b/std/prelude/compare.pk index b506761..5f3ae74 100644 --- a/std/prelude/compare.pk +++ b/std/prelude/compare.pk @@ -1,30 +1,36 @@ -## std.compare: Interfaces for comparable types. +## std.compare: Classes for comparable types. ## reference: https://doc.rust-lang.org/std/cmp -## The Eq interface -pub type Eq = interface +## The Eq class. For types with some notion of equivalence. +pub type Eq = class ==(Self, Self): bool -## A possible Equivalence interface. -# equality, equivalence, isomorphism, homomorphism, congruence -# pub type Equiv = interface -# ... +## A blanket implementation of a corresponding not-equal function. +pub !=[T: Eq](a: T, b: T): bool = + not(a == b) -pub type PartialOrd = interface +## The Compare class. For a type comparable with itself. +pub type Compare = class <(a: Self, b: Self): bool -pub type Ord = interface +## A blanket implementation of a corresponding greater-than function. +## Note to self: do NOT inline! +pub func >[T: Compare](a: T, b: T): bool = + b < a + +## The Ord class. For types with some notion of equivalence and comparision. +## +## Note: This is *not* a mathematical notion of an order! +## No invariants on `<` nor `==` are guaranteed to hold, as classes +## are implicitly implementable. +pub type Ord = class <(a: Self, b: Self): bool ==(a: Self, b: Self): bool -pub !=[T: Eq](a: T, b: T): bool = - not a == b - -pub func >[T: PartialOrd](a: T, b: T): bool = - b < a - +## A blanket implementation of a corresponding less-than-or-equal function. pub func <=[T: Ord](a: T, b: T): bool = a < b or a == b +## A blanket implementation of a corresponding greater-than-or-equal function. pub func >=[T: Ord](a: T, b: T): bool = a > b or a == b |