std::base

std/src/base.mo

typeOrdering#
pub type Ordering {
    lt,
    eq,
    gt
}

Three-way comparison result.

pub (with Ord below) so a mote outside std can declare Ord instances for its own types — motes/time's Instant/DateTime are the first such consumers.

classOrd#
pub class [BEq A] Ord A {
    def compare : A -> A -> Ordering
}

Total ordering with three-way comparison.

instance_#
pub instance Copy Ordering {
    def copy (o : Ordering) : Pair Ordering Ordering := Pair.pair o o
}

Ordering is a three-way tag: sharing one is a register move.

instance_#
pub instance BEq Ordering {
    def beq (a b : Ordering) : Bool :=
        match a {
            lt => match b {
                lt => true,
                eq => false,
                gt => false
            },
            eq => match b {
                lt => false,
                eq => true,
                gt => false
            },
            gt => match b {
                lt => false,
                eq => false,
                gt => true
            }
        }
}
instance_#
pub instance Ord Ordering {
    def compare (a b : Ordering) : Ordering :=
        match a {
            lt => match b {
                lt => eq,
                eq => lt,
                gt => lt
            },
            eq => match b {
                lt => gt,
                eq => eq,
                gt => lt
            },
            gt => match b {
                lt => gt,
                eq => gt,
                gt => eq
            }
        }
}
instance_#
pub instance Semigroup String {
    def combine (a b : String) : String := a ++ b
}
instance_#
pub instance Monoid String {
    def empty : String := ""
}
instance_#
pub instance Default Bool {
    def default : Bool := false
}
instance_#
pub instance Default I64 {
    def default : I64 := 0
}
instance_#
pub instance Enum Ordering {
    def succ (o : Ordering) : Ordering :=
        if BEq.beq o lt then eq
        else if BEq.beq o eq then gt
        else lt

    def pred (o : Ordering) : Ordering :=
        if BEq.beq o lt then gt
        else if BEq.beq o eq then lt
        else eq

    def to_nat (o : Ordering) : Nat :=
        if BEq.beq o lt then Nat.zero
        else if BEq.beq o eq then Nat.succ Nat.zero
        else Nat.succ (Nat.succ Nat.zero)

    def from_nat (n : Nat) : Ordering :=
        if Nat.eq n Nat.zero then lt
        else if Nat.eq n (Nat.succ Nat.zero) then eq
        else gt
}
instance_#
pub instance Bounded Ordering {
    def min_bound : Ordering := lt
    def max_bound : Ordering := gt
}
instance_#
pub instance Ord I64 {
    def compare (a b : I64) : Ordering :=
        if a == b then eq
        else if BOrd.lt a b then lt
        else gt
}
instance_#
pub instance Ord Bool {
    def compare (a b : Bool) : Ordering :=
        if a == b then eq
        else if a then gt
        else lt
}
instance_#
pub instance Ord U8 {
    def compare (a b : U8) : Ordering :=
        if a == b then eq
        else if BOrd.lt a b then lt
        else gt
}
instance_#
pub instance Ord I8 {
    def compare (a b : I8) : Ordering :=
        if a == b then eq
        else if BOrd.lt a b then lt
        else gt
}
instance_#
pub instance Ord I16 {
    def compare (a b : I16) : Ordering :=
        if a == b then eq
        else if BOrd.lt a b then lt
        else gt
}
instance_#
pub instance Ord I32 {
    def compare (a b : I32) : Ordering :=
        if a == b then eq
        else if BOrd.lt a b then lt
        else gt
}
instance_#
pub instance Ord U16 {
    def compare (a b : U16) : Ordering :=
        if a == b then eq
        else if BOrd.lt a b then lt
        else gt
}
instance_#
pub instance Ord U32 {
    def compare (a b : U32) : Ordering :=
        if a == b then eq
        else if BOrd.lt a b then lt
        else gt
}
instance_#
pub instance Ord U64 {
    def compare (a b : U64) : Ordering :=
        if a == b then eq
        else if BOrd.lt a b then lt
        else gt
}
instance_#
pub instance Ord F32 {
    def compare (a b : F32) : Ordering :=
        if a == b then eq
        else if BOrd.lt a b then lt
        else gt
}
instance_#
pub instance Ord F64 {
    def compare (a b : F64) : Ordering :=
        if a == b then eq
        else if BOrd.lt a b then lt
        else gt
}