init::number
init/src/number.mo
Number types and operations
pub def I8.sub (a b : I8) : I8pub def I8.beq (a b : I8) : Boolpub def I8.gt (a b : I8) : Boolpub def I8.to_string (a : I8) : Stringpub instance Add I8 {
def add (a b : I8) : I8 := I8.add a b
}pub instance Sub I8 {
def sub (a b : I8) : I8 := I8.sub a b
}pub instance Mul I8 {
def mul (a b : I8) : I8 := I8.mul a b
}pub instance Div I8 {
def div (a b : I8) : I8 := I8.div a b
}pub instance BEq I8 {
def beq (a b : I8) : Bool := I8.beq a b
}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)
}pub instance ToString I8 {
def to_string (a : I8) : String := I8.to_string a
}pub def I16.sub (a b : I16) : I16pub def I16.beq (a b : I16) : Boolpub def I16.gt (a b : I16) : Boolpub def I16.to_string (a : I16) : Stringpub instance Add I16 {
def add (a b : I16) : I16 := I16.add a b
}pub instance Sub I16 {
def sub (a b : I16) : I16 := I16.sub a b
}pub instance Mul I16 {
def mul (a b : I16) : I16 := I16.mul a b
}pub instance Div I16 {
def div (a b : I16) : I16 := I16.div a b
}pub instance BEq I16 {
def beq (a b : I16) : Bool := I16.beq a b
}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)
}pub instance ToString I16 {
def to_string (a : I16) : String := I16.to_string a
}pub def I32.sub (a b : I32) : I32pub def I32.beq (a b : I32) : Boolpub def I32.gt (a b : I32) : Boolpub def I32.to_string (a : I32) : Stringpub def I32.to_i64 (a : I32) : I64pub instance Add I32 {
def add (a b : I32) : I32 := I32.add a b
}pub instance Sub I32 {
def sub (a b : I32) : I32 := I32.sub a b
}pub instance Mul I32 {
def mul (a b : I32) : I32 := I32.mul a b
}pub instance Div I32 {
def div (a b : I32) : I32 := I32.div a b
}pub instance BEq I32 {
def beq (a b : I32) : Bool := I32.beq a b
}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)
}pub instance ToString I32 {
def to_string (a : I32) : String := I32.to_string a
}pub def I64.add (a b : I64) : I64pub def I64.sub (a b : I64) : I64pub def I64.mul (a b : I64) : I64pub def I64.div (a b : I64) : I64pub def I64.rem (a b : I64) : I64Signed remainder. A zero divisor yields 0, matching I64.div.
pub def I64.mod (a b : I64) : I64The mod spelling of I64.rem (truncated division, like Rust's %).
pub def I64.and (a b : I64) : I64pub def I64.or (a b : I64) : I64pub def I64.xor (a b : I64) : I64pub def I64.shl (a b : I64) : I64pub def I64.shr (a b : I64) : I64Arithmetic (sign-propagating) shift, like Rust's >> on i64.
pub def I64.not (a : I64) : I64pub def I64.lt (a b : I64) : Boolpub def I64.gt (a b : I64) : Boolpub def I64.to_u64 (a : I64) : U64pub def I64.to_i32 (a : I64) : I32For 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.
pub def I64.to_u32 (a : I64) : U32pub def I64.beq (a b : I64) : Boolpub def I64.to_string (a : I64) : Stringpub def I64.neg (n : I64) : I64Arithmetic 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).
pub instance Add I64 {
def add (a b : I64) : I64 := I64.add a b
}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
}pub instance Mul I64 {
def mul (a b : I64) : I64 := I64.mul a b
}pub instance Div I64 {
def div (a b : I64) : I64 := I64.div a b
}pub instance BEq I64 {
def beq (a b : I64) : Bool := I64.beq a b
}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)
}pub instance ToString I64 {
def to_string (a : I64) : String := I64.to_string a
}pub instance Hashable I64 {
def hash (a : I64) : U64 := I64.to_u64 a
}pub def U8.add (a b : U8) : U8pub def U8.sub (a b : U8) : U8pub def U8.mul (a b : U8) : U8pub def U8.div (a b : U8) : U8pub def U8.beq (a b : U8) : Boolpub def U8.to_i64 (a : U8) : I64Payload 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.
pub def U8.to_u32 (a : U8) : U32pub def U8.lt (a b : U8) : Boolpub def U8.gt (a b : U8) : Boolpub def U8.to_string (a : U8) : Stringpub instance Add U8 {
def add (a b : U8) : U8 := U8.add a b
}pub instance Sub U8 {
def sub (a b : U8) : U8 := U8.sub a b
}pub instance Mul U8 {
def mul (a b : U8) : U8 := U8.mul a b
}pub instance Div U8 {
def div (a b : U8) : U8 := U8.div a b
}pub instance BEq U8 {
def beq (a b : U8) : Bool := U8.beq a b
}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)
}pub instance ToString U8 {
def to_string (a : U8) : String := U8.to_string a
}pub def U16.add (a b : U16) : U16pub def U16.sub (a b : U16) : U16pub def U16.mul (a b : U16) : U16pub def U16.beq (a b : U16) : Boolpub def U16.gt (a b : U16) : Boolpub def U16.to_string (a : U16) : Stringpub def U16.to_i64 (a : U16) : I64pub instance Add U16 {
def add (a b : U16) : U16 := U16.add a b
}pub instance Sub U16 {
def sub (a b : U16) : U16 := U16.sub a b
}pub instance Mul U16 {
def mul (a b : U16) : U16 := U16.mul a b
}pub instance Div U16 {
def div (a b : U16) : U16 := U16.div a b
}pub instance BEq U16 {
def beq (a b : U16) : Bool := U16.beq a b
}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)
}pub instance ToString U16 {
def to_string (a : U16) : String := U16.to_string a
}pub def U32.add (a b : U32) : U32pub def U32.sub (a b : U32) : U32pub def U32.mul (a b : U32) : U32pub def U32.div (a b : U32) : U32pub def U32.rem (a b : U32) : U32Remainder in [0, b); a zero divisor yields 0, matching U32.div.
pub def U32.mod (a b : U32) : U32pub def U32.beq (a b : U32) : Boolpub def U32.lt (a b : U32) : Boolpub def U32.gt (a b : U32) : Boolpub def U32.to_string (a : U32) : Stringpub def U32.to_i64 (a : U32) : I64pub instance Add U32 {
def add (a b : U32) : U32 := U32.add a b
}pub instance Sub U32 {
def sub (a b : U32) : U32 := U32.sub a b
}pub instance Mul U32 {
def mul (a b : U32) : U32 := U32.mul a b
}pub instance Div U32 {
def div (a b : U32) : U32 := U32.div a b
}pub instance BEq U32 {
def beq (a b : U32) : Bool := U32.beq a b
}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)
}pub instance ToString U32 {
def to_string (a : U32) : String := U32.to_string a
}pub def U32.and (a b : U32) : U32pub def U32.or (a b : U32) : U32pub def U32.xor (a b : U32) : U32pub def U32.shl (a b : U32) : U32pub def U32.shr (a b : U32) : U32pub def U32.to_u8 (a : U32) : U8pub def U32.not (a : U32) : U32pub def U32.rotr (a n : U32) : U32pub def U64.add (a b : U64) : U64pub def U64.sub (a b : U64) : U64pub def U64.mul (a b : U64) : U64pub def U64.div (a b : U64) : U64pub def U64.mod (a b : U64) : U64pub def U64.xor (a b : U64) : U64pub def U64.and (a b : U64) : U64pub def U64.or (a b : U64) : U64pub def U64.shl (a b : U64) : U64pub def U64.shr (a b : U64) : U64Logical (zero-filling) shift, the unsigned counterpart of I64.shr.
pub def U64.not (a : U64) : U64pub def U64.beq (a b : U64) : Boolpub def U64.lt (a b : U64) : Boolpub def U64.gt (a b : U64) : Boolpub def U64.to_string (a : U64) : Stringpub def U64.to_i64 (a : U64) : I64pub def U64.bits_of_string (text : String) : I64The 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.
pub instance Add U64 {
def add (a b : U64) : U64 := U64.add a b
}pub instance Sub U64 {
def sub (a b : U64) : U64 := U64.sub a b
}pub instance Mul U64 {
def mul (a b : U64) : U64 := U64.mul a b
}pub instance Div U64 {
def div (a b : U64) : U64 := U64.div a b
}pub instance Hashable U64 {
def hash (a : U64) : U64 := a
}pub instance BEq U64 {
def beq (a b : U64) : Bool := U64.beq a b
}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)
}pub instance ToString U64 {
def to_string (a : U64) : String := U64.to_string a
}pub def F32.sub (a b : F32) : F32pub def F32.beq (a b : F32) : Boolpub def F32.gt (a b : F32) : Boolpub def F32.to_string (a : F32) : Stringpub def F64.to_f32 (a : F64) : F32Round to the nearest single-precision value (IEEE round-to-nearest; out-of-range magnitudes become infinities).
pub def F32.to_f64 (a : F32) : F64Widen exactly: every F32 is representable as an F64.
pub def I64.to_f64 (a : I64) : F64The nearest F64 (exact for magnitudes up to 2^53).
pub def F64.to_i64 (a : F64) : I64Truncate toward zero. NaN is 0; values beyond I64's range saturate to its bounds.
pub instance Add F32 {
def add (a b : F32) : F32 := F32.add a b
}pub instance Sub F32 {
def sub (a b : F32) : F32 := F32.sub a b
}pub instance Mul F32 {
def mul (a b : F32) : F32 := F32.mul a b
}pub instance Div F32 {
def div (a b : F32) : F32 := F32.div a b
}pub instance BEq F32 {
def beq (a b : F32) : Bool := F32.beq a b
}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)
}pub instance ToString F32 {
def to_string (a : F32) : String := F32.to_string a
}pub def F64.sub (a b : F64) : F64pub def F64.beq (a b : F64) : Boolpub def F64.gt (a b : F64) : Boolpub def F64.to_string (a : F64) : Stringpub def F64.bits_of_string (text : String) : I64Text -> 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.
pub instance Add F64 {
def add (a b : F64) : F64 := F64.add a b
}pub instance Sub F64 {
def sub (a b : F64) : F64 := F64.sub a b
}pub instance Mul F64 {
def mul (a b : F64) : F64 := F64.mul a b
}pub instance Div F64 {
def div (a b : F64) : F64 := F64.div a b
}pub instance BEq F64 {
def beq (a b : F64) : Bool := F64.beq a b
}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)
}pub instance ToString F64 {
def to_string (a : F64) : String := F64.to_string a
}pub def Nat.of_i64 (n : I64) : NatBuild 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.
pub def Nat.to_string (n : Nat) : Stringpub instance ToString Nat {
def to_string (n : Nat) : String := Nat.to_string n
}