init::number

init/src/number.mo

Number types and operations

defI8.sub#
pub def I8.sub (a b : I8) : I8
defI8.beq#
pub def I8.beq (a b : I8) : Bool
defI8.gt#
pub def I8.gt (a b : I8) : Bool
defI8.to_string#
pub def I8.to_string (a : I8) : String
instance_#
pub instance Add I8 {
    def add (a b : I8) : I8 := I8.add a b
}
instance_#
pub instance Sub I8 {
    def sub (a b : I8) : I8 := I8.sub a b
}
instance_#
pub instance Mul I8 {
    def mul (a b : I8) : I8 := I8.mul a b
}
instance_#
pub instance Div I8 {
    def div (a b : I8) : I8 := I8.div a b
}
instance_#
pub instance BEq I8 {
    def beq (a b : I8) : Bool := I8.beq a b
}
instance_#
pub instance BOrd I8 {
    def lt (a b : I8) : Bool := I8.lt a b
    def gt (a b : I8) : Bool := I8.gt a b
    def lte (a b : I8) : Bool := Bool.not (I8.gt a b)
    def gte (a b : I8) : Bool := Bool.not (I8.lt a b)
}
instance_#
pub instance ToString I8 {
    def to_string (a : I8) : String := I8.to_string a
}
defI16.sub#
pub def I16.sub (a b : I16) : I16
defI16.beq#
pub def I16.beq (a b : I16) : Bool
defI16.gt#
pub def I16.gt (a b : I16) : Bool
defI16.to_string#
pub def I16.to_string (a : I16) : String
instance_#
pub instance Add I16 {
    def add (a b : I16) : I16 := I16.add a b
}
instance_#
pub instance Sub I16 {
    def sub (a b : I16) : I16 := I16.sub a b
}
instance_#
pub instance Mul I16 {
    def mul (a b : I16) : I16 := I16.mul a b
}
instance_#
pub instance Div I16 {
    def div (a b : I16) : I16 := I16.div a b
}
instance_#
pub instance BEq I16 {
    def beq (a b : I16) : Bool := I16.beq a b
}
instance_#
pub instance BOrd I16 {
    def lt (a b : I16) : Bool := I16.lt a b
    def gt (a b : I16) : Bool := I16.gt a b
    def lte (a b : I16) : Bool := Bool.not (I16.gt a b)
    def gte (a b : I16) : Bool := Bool.not (I16.lt a b)
}
instance_#
pub instance ToString I16 {
    def to_string (a : I16) : String := I16.to_string a
}
defI32.sub#
pub def I32.sub (a b : I32) : I32
defI32.beq#
pub def I32.beq (a b : I32) : Bool
defI32.gt#
pub def I32.gt (a b : I32) : Bool
defI32.to_string#
pub def I32.to_string (a : I32) : String
defI32.to_i64#
pub def I32.to_i64 (a : I32) : I64
instance_#
pub instance Add I32 {
    def add (a b : I32) : I32 := I32.add a b
}
instance_#
pub instance Sub I32 {
    def sub (a b : I32) : I32 := I32.sub a b
}
instance_#
pub instance Mul I32 {
    def mul (a b : I32) : I32 := I32.mul a b
}
instance_#
pub instance Div I32 {
    def div (a b : I32) : I32 := I32.div a b
}
instance_#
pub instance BEq I32 {
    def beq (a b : I32) : Bool := I32.beq a b
}
instance_#
pub instance BOrd I32 {
    def lt (a b : I32) : Bool := I32.lt a b
    def gt (a b : I32) : Bool := I32.gt a b
    def lte (a b : I32) : Bool := Bool.not (I32.gt a b)
    def gte (a b : I32) : Bool := Bool.not (I32.lt a b)
}
instance_#
pub instance ToString I32 {
    def to_string (a : I32) : String := I32.to_string a
}
defI64.add#
pub def I64.add (a b : I64) : I64
defI64.sub#
pub def I64.sub (a b : I64) : I64
defI64.mul#
pub def I64.mul (a b : I64) : I64
defI64.div#
pub def I64.div (a b : I64) : I64
defI64.rem#
pub def I64.rem (a b : I64) : I64

Signed remainder. A zero divisor yields 0, matching I64.div.

defI64.mod#
pub def I64.mod (a b : I64) : I64

The mod spelling of I64.rem (truncated division, like Rust's %).

defI64.and#
pub def I64.and (a b : I64) : I64
defI64.or#
pub def I64.or (a b : I64) : I64
defI64.xor#
pub def I64.xor (a b : I64) : I64
defI64.shl#
pub def I64.shl (a b : I64) : I64
defI64.shr#
pub def I64.shr (a b : I64) : I64

Arithmetic (sign-propagating) shift, like Rust's >> on i64.

defI64.not#
pub def I64.not (a : I64) : I64
defI64.lt#
pub def I64.lt (a b : I64) : Bool
defI64.gt#
pub def I64.gt (a b : I64) : Bool
defI64.to_u64#
pub def I64.to_u64 (a : I64) : U64
defI64.to_i32#
pub def I64.to_i32 (a : I64) : I32

For FFI calls that take a C int (SSL_set_fd in motes/tls takes the socket's descriptor). Identity at the payload level; no truncation is applied, matching the reference's int_to_int.

defI64.to_u32#
pub def I64.to_u32 (a : I64) : U32
defI64.beq#
pub def I64.beq (a b : I64) : Bool
defI64.to_string#
pub def I64.to_string (a : I64) : String
defI64.neg#
pub def I64.neg (n : I64) : I64

Arithmetic negation. Canonical home for what lang/json.mo and lang/toml.mo each had as their own neg_i64/Toml.neg_i64 (duplicated only because top-level names are not file-scoped).

instance_#
pub instance Add I64 {
    def add (a b : I64) : I64 := I64.add a b
}
instance_#
pub instance Sub I64 {
    // `I64.sub`, NOT `a - b`: `infix (-) := HSub.sub`
    // (init/prelude.mo) dispatches the operator through the
    // `[Sub A] HSub A A A` bridge back to THIS instance, so the
    // operator spelling would make it call itself.
    def sub (a b : I64) : I64 := I64.sub a b
}
instance_#
pub instance Mul I64 {
    def mul (a b : I64) : I64 := I64.mul a b
}
instance_#
pub instance Div I64 {
    def div (a b : I64) : I64 := I64.div a b
}
instance_#
pub instance BEq I64 {
    def beq (a b : I64) : Bool := I64.beq a b
}
instance_#
pub instance BOrd I64 {
    def lt (a b : I64) : Bool := I64.lt a b
    def gt (a b : I64) : Bool := I64.gt a b
    def lte (a b : I64) : Bool := Bool.not (I64.gt a b)
    def gte (a b : I64) : Bool := Bool.not (I64.lt a b)
}
instance_#
pub instance ToString I64 {
    def to_string (a : I64) : String := I64.to_string a
}
instance_#
pub instance Hashable I64 {
    def hash (a : I64) : U64 := I64.to_u64 a
}
defU8.add#
pub def U8.add (a b : U8) : U8
defU8.sub#
pub def U8.sub (a b : U8) : U8
defU8.mul#
pub def U8.mul (a b : U8) : U8
defU8.div#
pub def U8.div (a b : U8) : U8
defU8.beq#
pub def U8.beq (a b : U8) : Bool
defU8.to_i64#
pub def U8.to_i64 (a : U8) : I64

Payload identity, like every uN_to_* conversion in this backend (a U8 is an i64 whose high bits are zero). Exists so Monad-level code can take a byte back to I64 without a second native family; the evaluator's String.to_list/String.get bridge is the first caller.

defU8.to_u32#
pub def U8.to_u32 (a : U8) : U32
defU8.lt#
pub def U8.lt (a b : U8) : Bool
defU8.gt#
pub def U8.gt (a b : U8) : Bool
defU8.to_string#
pub def U8.to_string (a : U8) : String
instance_#
pub instance Add U8 {
    def add (a b : U8) : U8 := U8.add a b
}
instance_#
pub instance Sub U8 {
    def sub (a b : U8) : U8 := U8.sub a b
}
instance_#
pub instance Mul U8 {
    def mul (a b : U8) : U8 := U8.mul a b
}
instance_#
pub instance Div U8 {
    def div (a b : U8) : U8 := U8.div a b
}
instance_#
pub instance BEq U8 {
    def beq (a b : U8) : Bool := U8.beq a b
}
instance_#
pub instance BOrd U8 {
    def lt (a b : U8) : Bool := U8.lt a b
    def gt (a b : U8) : Bool := U8.gt a b
    def lte (a b : U8) : Bool := Bool.not (U8.gt a b)
    def gte (a b : U8) : Bool := Bool.not (U8.lt a b)
}
instance_#
pub instance ToString U8 {
    def to_string (a : U8) : String := U8.to_string a
}
defU16.add#
pub def U16.add (a b : U16) : U16
defU16.sub#
pub def U16.sub (a b : U16) : U16
defU16.mul#
pub def U16.mul (a b : U16) : U16
defU16.beq#
pub def U16.beq (a b : U16) : Bool
defU16.gt#
pub def U16.gt (a b : U16) : Bool
defU16.to_string#
pub def U16.to_string (a : U16) : String
defU16.to_i64#
pub def U16.to_i64 (a : U16) : I64
instance_#
pub instance Add U16 {
    def add (a b : U16) : U16 := U16.add a b
}
instance_#
pub instance Sub U16 {
    def sub (a b : U16) : U16 := U16.sub a b
}
instance_#
pub instance Mul U16 {
    def mul (a b : U16) : U16 := U16.mul a b
}
instance_#
pub instance Div U16 {
    def div (a b : U16) : U16 := U16.div a b
}
instance_#
pub instance BEq U16 {
    def beq (a b : U16) : Bool := U16.beq a b
}
instance_#
pub instance BOrd U16 {
    def lt (a b : U16) : Bool := U16.lt a b
    def gt (a b : U16) : Bool := U16.gt a b
    def lte (a b : U16) : Bool := Bool.not (U16.gt a b)
    def gte (a b : U16) : Bool := Bool.not (U16.lt a b)
}
instance_#
pub instance ToString U16 {
    def to_string (a : U16) : String := U16.to_string a
}
defU32.add#
pub def U32.add (a b : U32) : U32
defU32.sub#
pub def U32.sub (a b : U32) : U32
defU32.mul#
pub def U32.mul (a b : U32) : U32
defU32.div#
pub def U32.div (a b : U32) : U32
defU32.rem#
pub def U32.rem (a b : U32) : U32

Remainder in [0, b); a zero divisor yields 0, matching U32.div.

defU32.mod#
pub def U32.mod (a b : U32) : U32
defU32.beq#
pub def U32.beq (a b : U32) : Bool
defU32.lt#
pub def U32.lt (a b : U32) : Bool
defU32.gt#
pub def U32.gt (a b : U32) : Bool
defU32.to_string#
pub def U32.to_string (a : U32) : String
defU32.to_i64#
pub def U32.to_i64 (a : U32) : I64
instance_#
pub instance Add U32 {
    def add (a b : U32) : U32 := U32.add a b
}
instance_#
pub instance Sub U32 {
    def sub (a b : U32) : U32 := U32.sub a b
}
instance_#
pub instance Mul U32 {
    def mul (a b : U32) : U32 := U32.mul a b
}
instance_#
pub instance Div U32 {
    def div (a b : U32) : U32 := U32.div a b
}
instance_#
pub instance BEq U32 {
    def beq (a b : U32) : Bool := U32.beq a b
}
instance_#
pub instance BOrd U32 {
    def lt (a b : U32) : Bool := U32.lt a b
    def gt (a b : U32) : Bool := U32.gt a b
    def lte (a b : U32) : Bool := Bool.not (U32.gt a b)
    def gte (a b : U32) : Bool := Bool.not (U32.lt a b)
}
instance_#
pub instance ToString U32 {
    def to_string (a : U32) : String := U32.to_string a
}
defU32.and#
pub def U32.and (a b : U32) : U32
defU32.or#
pub def U32.or (a b : U32) : U32
defU32.xor#
pub def U32.xor (a b : U32) : U32
defU32.shl#
pub def U32.shl (a b : U32) : U32
defU32.shr#
pub def U32.shr (a b : U32) : U32
defU32.to_u8#
pub def U32.to_u8 (a : U32) : U8
defU32.not#
pub def U32.not (a : U32) : U32
defU32.rotr#
pub def U32.rotr (a n : U32) : U32
defU64.add#
pub def U64.add (a b : U64) : U64
defU64.sub#
pub def U64.sub (a b : U64) : U64
defU64.mul#
pub def U64.mul (a b : U64) : U64
defU64.div#
pub def U64.div (a b : U64) : U64
defU64.mod#
pub def U64.mod (a b : U64) : U64
defU64.xor#
pub def U64.xor (a b : U64) : U64
defU64.and#
pub def U64.and (a b : U64) : U64
defU64.or#
pub def U64.or (a b : U64) : U64
defU64.shl#
pub def U64.shl (a b : U64) : U64
defU64.shr#
pub def U64.shr (a b : U64) : U64

Logical (zero-filling) shift, the unsigned counterpart of I64.shr.

defU64.not#
pub def U64.not (a : U64) : U64
defU64.beq#
pub def U64.beq (a b : U64) : Bool
defU64.lt#
pub def U64.lt (a b : U64) : Bool
defU64.gt#
pub def U64.gt (a b : U64) : Bool
defU64.to_string#
pub def U64.to_string (a : U64) : String
defU64.to_i64#
pub def U64.to_i64 (a : U64) : I64
defU64.bits_of_string#
pub def U64.bits_of_string (text : String) : I64

The exact decimal parse for a u64: digit text in, its u64 bit pattern as an I64 out. U64.to_string is the inverse — the same text/bits pair F64.bits_of_string/F64.to_string already form for floats. The digits parse into exactly the bits a u64 literal of the same text has, on every backend (u64::from_str on the Rust host, strtoull in the C runtime, the evaluator's digit fold); the compiler itself never calls it (the self-hosted parser's u64 gate reuses its own digit fold, which keeps the native off the bootstrap surface). Leading zeros are accepted. Strict decimal digits only, 0 on anything else: a prefix parse of "12a4" would leave the three backends disagreeing on the same text, and 0 is already the out-of-range answer.

instance_#
pub instance Add U64 {
    def add (a b : U64) : U64 := U64.add a b
}
instance_#
pub instance Sub U64 {
    def sub (a b : U64) : U64 := U64.sub a b
}
instance_#
pub instance Mul U64 {
    def mul (a b : U64) : U64 := U64.mul a b
}
instance_#
pub instance Div U64 {
    def div (a b : U64) : U64 := U64.div a b
}
instance_#
pub instance Hashable U64 {
    def hash (a : U64) : U64 := a
}
instance_#
pub instance BEq U64 {
    def beq (a b : U64) : Bool := U64.beq a b
}
instance_#
pub instance BOrd U64 {
    def lt (a b : U64) : Bool := U64.lt a b
    def gt (a b : U64) : Bool := U64.gt a b
    def lte (a b : U64) : Bool := Bool.not (U64.gt a b)
    def gte (a b : U64) : Bool := Bool.not (U64.lt a b)
}
instance_#
pub instance ToString U64 {
    def to_string (a : U64) : String := U64.to_string a
}
defF32.sub#
pub def F32.sub (a b : F32) : F32
defF32.beq#
pub def F32.beq (a b : F32) : Bool
defF32.gt#
pub def F32.gt (a b : F32) : Bool
defF32.to_string#
pub def F32.to_string (a : F32) : String
defF64.to_f32#
pub def F64.to_f32 (a : F64) : F32

Round to the nearest single-precision value (IEEE round-to-nearest; out-of-range magnitudes become infinities).

defF32.to_f64#
pub def F32.to_f64 (a : F32) : F64

Widen exactly: every F32 is representable as an F64.

defI64.to_f64#
pub def I64.to_f64 (a : I64) : F64

The nearest F64 (exact for magnitudes up to 2^53).

defF64.to_i64#
pub def F64.to_i64 (a : F64) : I64

Truncate toward zero. NaN is 0; values beyond I64's range saturate to its bounds.

instance_#
pub instance Add F32 {
    def add (a b : F32) : F32 := F32.add a b
}
instance_#
pub instance Sub F32 {
    def sub (a b : F32) : F32 := F32.sub a b
}
instance_#
pub instance Mul F32 {
    def mul (a b : F32) : F32 := F32.mul a b
}
instance_#
pub instance Div F32 {
    def div (a b : F32) : F32 := F32.div a b
}
instance_#
pub instance BEq F32 {
    def beq (a b : F32) : Bool := F32.beq a b
}
instance_#
pub instance BOrd F32 {
    def lt (a b : F32) : Bool := F32.lt a b
    def gt (a b : F32) : Bool := F32.gt a b
    def lte (a b : F32) : Bool := Bool.not (F32.gt a b)
    def gte (a b : F32) : Bool := Bool.not (F32.lt a b)
}
instance_#
pub instance ToString F32 {
    def to_string (a : F32) : String := F32.to_string a
}
defF64.sub#
pub def F64.sub (a b : F64) : F64
defF64.beq#
pub def F64.beq (a b : F64) : Bool
defF64.gt#
pub def F64.gt (a b : F64) : Bool
defF64.to_string#
pub def F64.to_string (a : F64) : String
defF64.bits_of_string#
pub def F64.bits_of_string (text : String) : I64

Text -> the double's own bit pattern: the decimal parser this backend needs to lower a FLOAT LITERAL, since Literal.flt carries a literal's source text rather than a value (lang/types.mo: there is no native bridge from a decimal string to a float bit pattern on the self-hosted side, which is why the literal keeps its text at all). lang/codegen/emit.mo's compile_lit_ir calls this to turn 5.0 into the constant it emits, so the route is the ordinary one: a #[native] def, wired like the rest of this family.

I64, not F64, because the result IS the bit pattern and an F64 is nothing else here -- a codegen caller wants it as the integer it has to emit, with no further conversion in between. The C side is strtod (runtime/src/runtime.c) and the Rust host's equivalent is f64::from_str (rust/core/src/core_native.rs), which agree on every spelling the number parser can produce; text that parses to nothing gives 0.0 rather than failing, so a malformed literal is a wrong constant instead of a compiler crash.

instance_#
pub instance Add F64 {
    def add (a b : F64) : F64 := F64.add a b
}
instance_#
pub instance Sub F64 {
    def sub (a b : F64) : F64 := F64.sub a b
}
instance_#
pub instance Mul F64 {
    def mul (a b : F64) : F64 := F64.mul a b
}
instance_#
pub instance Div F64 {
    def div (a b : F64) : F64 := F64.div a b
}
instance_#
pub instance BEq F64 {
    def beq (a b : F64) : Bool := F64.beq a b
}
instance_#
pub instance BOrd F64 {
    def lt (a b : F64) : Bool := F64.lt a b
    def gt (a b : F64) : Bool := F64.gt a b
    def lte (a b : F64) : Bool := Bool.not (F64.gt a b)
    def gte (a b : F64) : Bool := Bool.not (F64.lt a b)
}
instance_#
pub instance ToString F64 {
    def to_string (a : F64) : String := F64.to_string a
}
defNat.of_i64#
pub def Nat.of_i64 (n : I64) : Nat

Build a Nat from an I64 payload, the elaboration rewrite target for context-only Nat literals ((3 : Nat) becomes Nat.of_i64 3; plans/implementations/true-numeric-types.md §5, Phase 3.1). The walk is O(n) over the unary representation -- the #[repr(BigInt)] compact repr (Phase 5) makes this O(1) over the compact word and this def stays the rewrite target unchanged. A negative payload clamps to Nat.zero: a Nat cannot hold it, and the literal elaboration rejects a negative literal at a Nat-expected position anyway, so the clamp is unreachable from literals and only guards direct calls.

defNat.to_string#
pub def Nat.to_string (n : Nat) : String
instance_#
pub instance ToString Nat {
    def to_string (n : Nat) : String := Nat.to_string n
}