Std.Comparable

View source →

use Std.Bool supplies `not` for the derived `<=`/`>=`; use Std.Equatable provides the Equatable(t) superinterface required by Comparable(t).

Types

  • type Ordering = LessThan | EqualTo | GreaterThan

    Total-ordering interface on a minimal basis.

    The sole primitive obligation is `<`; `<=`, `>`, `>=` and the three-way compare (returning an Ordering) are derived requires Comparable(t) top-level functions, each expressed with `<` alone (a total order is trichotomous in `<`). Comparable(t) requires Equatable(t), so every comparable type is also equatable.

    The derived operations are standalone requires-constrained functions rather than interface defaults: an instance-specialised default re-dispatching a sibling method at a concrete operand cannot be elaborated for every leaf today, whereas a requires-constrained function stays polymorphic and is resolved per call site (the same idiom Std.Equatable. `!=` uses).

    Examples

    cure
    use Std.Comparable
    
    compare(1, 2)                           # => LessThan
    `<`('a', 'b')                           # => true
    `<`("ada", "grace")                     # => true
    

    Group tag consumed by Cure.Stdlib.Preload. Three-way ordering result.

Functions

  • # fn <=(a: t, b: t) -> Bool

    `<=` derived: a <= b iff not (b < a).

  • # fn >(a: t, b: t) -> Bool

    `>` derived by flipping the operands: a > b iff b < a.

  • # fn >=(a: t, b: t) -> Bool

    `>=` derived: a >= b iff not (a < b).

  • # fn clamp(value: t, lo: t, hi: t) -> t

    Clamp value into the inclusive range [lo, hi].

  • # fn compare(a: t, b: t) -> Ordering

    Derived tripartite compare, generated from `<` alone (trichotomy of a total order). Kept present and named compare so compare_op_call and min/max/clamp (which dispatch to compare) keep working.

  • # fn max(x: t, y: t) -> t

    Pointwise maximum under the Comparable ordering.

  • # fn min(x: t, y: t) -> t

    Pointwise minimum under the Comparable ordering.