Literal spelling establishes a numeric origin. Moth keeps that spelling until the permitted receiving and peer constraints have resolved its exact type.
Spelling and defaults
whole = 42
decimal = 0.5
large = 1e6
small = 1e-6
precise = 1.0e+21
- Whole-number spelling creates an integer origin. An otherwise unconstrained origin defaults to
I32. - Decimal-point or exponent spelling creates a binary-float origin. An otherwise unconstrained origin defaults to
F64. - The grammar has no unsigned literal category, numeric suffixes or radix forms.
- Value size never chooses a width. An unconstrained whole literal outside
I32 diagnoses instead of retrying as I64, U64 or Dec. - A concrete receiver can materialise a retained whole spelling directly as
U64, including values above the I32 range. - Decimal or exponent spelling cannot initialise an integer merely because its mathematical value is integral.
- Exponents use lowercase
e. Uppercase E, unary + and a spaced prefix minus such as - count are invalid. - A leading
- stays attached to its literal or expression. Signed minimum values materialise directly from their negative spelling.
Body-local constraints
Each runtime function body forms an inference boundary, including nested blocks, branches, loops and handlers. The implicit root runtime body follows the same rule. Each binding has one static type across all writes and control-flow paths. Compile-time declarations resolve at their own declaration boundary, so later runtime uses cannot retype a named constant. Each concrete generic instance gets its own body-local inference state.
Value-producing blocks keep their existing placement and explicit-receiver requirements. Inference operates inside legal constructs and does not invent a missing receiver to legalise a different block form.
Numeric constraints can come from exact annotations, assignments, concrete parameters, return slots, fields, typed container entries and eligible value-producing receivers. Copies connect the same numeric unknown. Mutation constrains one binding type rather than creating a new type for each write. A known generic signature can connect an existing numeric unknown across its parameters and result. The compiler uses that signature alone: it does not inspect a callee body or search traits to choose a type.
Every eligible use contributes before defaulting. Source order cannot pick the winning receiver. Conflicting exact receivers produce MOTH-TYPE-0001 with both receiving witnesses in authored order, independent of traversal order. A trait requirement, overload, failure effect, proven value range, backend capability or optimisation never chooses a numeric type.
This bounded process does not add general type inference. Function signatures, fields, public interfaces and named constants remain concrete at their existing declaration boundaries. No pending numeric unknown crosses a module boundary.
Receiving, peer and default priority
Inference applies this priority before materialisation:
eligible receiving context
-> eligible local numeric peer context
-> language default
An exact receiver reaches unresolved literal arithmetic and grouping, including raw literal leaves beside named operands. It never retypes a named binding, constant, field, call result or completed operator result.
base I32 = 10
total I64 = base + 1
The receiver types raw 1 as I64, so the mixed fixed-integer operation produces I64. base stays I32. If both operands name I32 values, the operation stays I32 and an I64 receiver rejects it.
Without an explicit receiver, an eligible typed numeric peer can supply its exact type to the opposite unresolved literal-only arithmetic subtree. The peer never changes its own type. A subtree containing a binding, field, call result or cast result is not literal-only. Call arguments use their own parameter receivers.
small U8 = 10
same_width = small + 1
The local U8 peer types the raw 1 as U8, so same_width has type U8. An explicit wider receiver can type the raw leaf wider without changing small:
wider U16 = small + 1
Here 1 receives U16 and ordinary mixed-integer arithmetic produces U16. small remains U8.
Forward arithmetic only across named values
A later consumer can resolve an initializer that still consists only of unresolved literal arithmetic:
value = 1 + 2
take_u64(value)
The initializer becomes U64 before operator selection. A completed operation over named values cannot search backwards for a wider operand type:
base = 1
twice = base + base
take_u64(twice)
base defaults to I32, so twice has type I32. The receiver cannot make either named operand U64. An independent take_u64(base) can resolve base before forward arithmetic. Its position relative to other eligible uses does not change that result.
Copies preserve the same rule. A copied numeric unknown shares its type constraints with the source, while a copy of a completed value keeps that value's concrete type. Mutation also keeps one type across its initializer and all later writes. A write cannot create a new type version or push a constraint back through a completed named arithmetic expression.
Assume take_u64 accepts a U64 parameter:
source = 1
duplicate = copy source
take_u64(duplicate)
count ~= 1
~count += 1
take_u64(count)
The copy connects source and duplicate to one numeric type. Mutation keeps one type across count's initializer, update and later use. Both examples resolve to U64 from the direct parameter receiver; source order does not choose a different type.
Literal families in collections
A non-empty numeric collection can retain one unresolved numeric element type until an exact collection receiver or another eligible use resolves it. Raw whole and decimal/exponent literals select the binary-float family regardless of order:
values = {1, 2.5}
samples = {2.5, 1}
Both default to F64 unless a receiver selects another binary-float precision. A bound integer never changes family to fit:
count = 1
mixed = {count, 2.5}
An empty collection creates no numeric unknown and keeps its existing explicit typing requirements. Arithmetic promotion does not search for a common collection storage type.
Destination-aware materialisation
A literal materialises directly at an immediate concrete numeric receiver. The compiler does not first construct a default value and convert it:
small U8 = 200
large U64 = 18_000_000_000
half F16 = 0.5
money Dec2 = 12.34
raw Byte = 255
- A whole literal can initialise a fixed integer, binary float,
Dec or Byte when it fits. - A negative value cannot initialise an unsigned type or
Byte. Negative unsigned spellings, including -0, are rejected. - Integer literals must fit the receiving range.
4_294_967_296 fits U64 but not U32. - Decimal or exponent literals must fit a receiving
Dec scale exactly. money Dec2 = 1.239 fails rather than rounding. - A binary-float literal rounds directly to its destination using nearest, ties-to-even. Inexact values such as
0.1, subnormals and signed zero remain valid, while a non-finite rounded result fails.
Dec literal context
An explicit Dec receiver reaches every unresolved literal leaf in its arithmetic and grouping. Each leaf materialises directly at the selected scale, without passing through a binary float or a formatted intermediate:
whole Dec4 = 1 + 1
fraction Dec4 = 0.1 + 0.2
grouped Dec4 = (1 + 2) * 3
fraction holds exactly 0.3. Parentheses preserve the expression's grouping but do not add a receiving boundary. A Dec peer can supply its scale to an opposite literal-only arithmetic subtree from either side. A concrete Dec value, call result or cast result does not become a raw literal.
Every decimal leaf must fit the selected scale. small Dec2 = 0.005 + 0.005 fails even though the mathematical sum fits because neither leaf can materialise at scale two. Different Dec scales never rescale silently. A completed operation over named operands also keeps its own type rather than changing to satisfy a wider decimal receiver.
Dec context reaches a power base but not its exponent. The exponent uses the separate I32 rule below.
Cast and operator barriers
A cast destination never chooses the source type. Resolve the source from its own eligible constraints or its language default, then check the complete source-target pair:
value = 1
take_u32(cast value)
take_i64(value)
The later I64 parameter resolves value as I64; the U32 cast target does not. The cast then uses the I64-to-U32 policy. A cast also forms a barrier to incoming arithmetic receiver context. An untyped declaration such as converted = cast source remains invalid because no immediate target exists.
Already concrete operator results never change type to satisfy a wider receiver. Comparison and logical results stop numeric context and remain Bool. Member access, propagation and completed catch results also stop later peer context. Explicit cast operands resolve independently.
Dec exponentiation has one separate boundary:
Base | Exponent type | Result |
DecN | I32 | DecN |
An explicit Dec receiver reaches the power base but not the exponent. A raw exponent receives the default I32; an already typed other-width integer needs an explicit cast at a legal receiving site. Negative exponents keep their existing failure behaviour.
MON data boundary
MON literal data shares numeric spelling and materialisation facts but keeps its independent schema-directed parser. Its integer and decimal/exponent categories remain lossless until the schema selects a destination. See MON format.