DS conversions — the :: operator#
Prefix LCST. The postfix conversion operator ::: its surface and precedence,
how totality and fallibility are encoded in the result type, the ?-presence rule,
and the closed table of defined (source, target) conversions.
Paired with: identifiers over Unicode — [LNAM]; positional type
classification and the type namespace a cast target resolves in — [LGRA];
the lexer realization of the NAME / TYPE_SYM terminals — [LLEX]. This doc
owns LCST.4.
Depends on: [LNUL] (a fallible ::T? is a T? producer feeding the
null stack), [LDET.3.2.7] (the canonical Float-to-text form),
[LUNI.8.5] (the unit-assertion / dimension-strip casts),
[LOPQ.4.3] (the opaque seal a cast must honour), [LEXP] (where :: sits among the operators).
4. The :: conversion operator [LCST.4]#
4.1 Surface, terminal, and precedence [LCST.4.1]#
[LCST.4.1.1] Form. A conversion is a postfix operator, '::' TYPE_SYM NULLABLE?, in the same left-to-right postfix_op* chain that carries member
access, method calls, and indexing ([LGRA.9.6.1]). So :: composes with ., a
method call, and […] in one chain evaluated left to right; it is not a
separate precedence level above member access. R.v::Int.bit_count is
therefore (R.v::Int).bit_count ([LCST.4.1.4]) — ::Int and .bit_count
are adjacent postfix operators on the same primary.
see also: LCST.4.1.4, LGRA.9.6.1
[LCST.4.1.2] One target terminal — no type/unit split. TYPE_SYM is a single
terminal over the type-symbol character class — XID plus the small non-XID unit-glyph
set (e.g. °, %) — valid only in type positions ([LGRA]) so non-XID glyphs
never enter identifiers. The compiler must resolve TYPE_SYM in the type namespace
([LGRA]). There is no separate UNIT_SYM alternative and no overlapping-terminal
choice: units resolve as types.
[LCST.4.1.3] NULLABLE binds to the target. The optional ? in cast_suffix must
bind to the immediately preceding TYPE_SYM and must be consumed as part of the cast
before any following operator is considered — x::Int? is a fallible cast whose ?
cannot be stolen by a following operator. Because ?? (COALESCE) is a single token under
maximal munch ([LLEX.10.8.5]), x::Int??d lexes as x :: Int ?? d and parses as
(x::Int) ?? d — a total cast then coalesce, with no NULLABLE on Int. The
fallible-then-coalesce idiom is therefore written with the target ? separated from the
??: x::Int? ?? d → (x::Int?) ?? d. (No whitespace-free spelling of the fallible
idiom exists — x::Int???d mis-munches — so a fallible cast that then coalesces must
separate the target ? from the ??; a total cast on a nullable source is instead the
[LCST.4.3] missing-? error, which guides the author to the spaced form.)
see also: LCST.4.3, LLEX.10.8.5
[LCST.4.1.4] Precedence. Because :: is a postfix operator, it interleaves with
member/method access left to right and binds tighter than every unary and binary operator.
Thus R.v::Int.bit_count is (R.v::Int).bit_count, R.temp::Float > x is
((R.temp)::Float) > x, and x::Int? ?? 0 is (x::Int?) ?? 0.
[LCST.4.1.5] Chaining. A chain x::A::B applies each conversion left to right,
threading nullability per [LCST.4.3.1]: the result of x::A is the source of ::B, so if
x::A is nullable then ::B must carry ?. x::Int?::Float is therefore a compile error
(nullable source, bare target; DS_VALIDATE_CAST_NULLABILITY, [LCST.4.3.2]); the author
writes x::Int?::Float?.
see also: LCST.4.3.1, LCST.4.3.2
[LCST.4.1.6] Target must be convertible-to. TYPE_SYM must resolve to a scalar
builtin (Time, Date and Duration among them), Json, or a unit. The compiler must reject a schema as a cast target
(<expr>::Schema), even though a schema shares the type namespace
(DS_VALIDATE_CAST_UNDEFINED).
ratified — checks: idris
4.2 Totality encoded in the result type [LCST.4.2]#
checks: alloy idris
[LCST.4.2.1] ::T is total. <expr>::T denotes a conversion that must succeed; its
result type is T. Total means cannot-fail, not lossless: Float → Int truncates and
is total. The compiler should warn on a lossy total conversion but must not require ? for
it.
checks: idris×3
[LCST.4.2.2] ::T? is fallible. <expr>::T? denotes a conversion whose failure yields
null; its result type is T? (the nullable T of [LNUL]). The ? is the
NULLABLE marker.
[LCST.4.2.3] Null propagates — stated on the legal form. When the source is nullable
and evaluates to null, the conversion yields null. Because a nullable source requires a
? target ([LCST.4.3.1]), this is written null::T? → null. null::T — a bare
target on the null literal or any nullable source — is a compile error, not a runtime
propagation. The runtime propagation of null through a fallible cast is specified in
[LNUL].
ratified — checks: idris×2 — see also: LCST.4.3.1
4.3 The ?-presence rule [LCST.4.3]#
checks: alloy×2
[LCST.4.3.1] Let the source static type be S (non-null) or S? (nullable) and the
conversion have fallibility f. The result is nullable if and only if S is nullable
or f is fallible. The compiler must reject a missing ? when the result is
nullable.
source |
|
|
|
|---|---|---|---|
|
total |
conforms → |
compile error — redundant |
|
fallible |
compile error — must be |
conforms → |
|
total |
compile error — source may be null |
conforms → |
|
fallible |
compile error |
conforms → |
ratified — checks: alloy×2 idris×3
[LCST.4.3.2] Both directions reject — the ?-bijection is strict. The ? is
present if and only if the result can be null. A missing ? on a nullable/fallible
result is a must reject (the load-bearing safety); a redundant ? on a non-null source
with a total conversion (a T? that can never be null) is equally a compile error. The
compiler must reject both, with DS_VALIDATE_CAST_NULLABILITY; generated code and casts carry the exact fallibility — no
widening, no noise.
ratified — checks: alloy×4
[LCST.4.3.3] A failed conversion flows through ?? ([LNUL.5.3]), Kleene logic
([LNUL.4.2]), and narrowing ([LNUL.5.4]) with no new machinery; ::T? is a T? producer
feeding [LNUL.5].
see also: LNUL.4.2, LNUL.5, LNUL.5.3, LNUL.5.4
[LCST.4.3.4] A null source converts to null, and the conversion is
not attempted. Where the source value of a ::T? is null, the result is
null: the runtime must not hand the absent value to the conversion, and must
not raise. This holds for every defined conversion ([LCST.4.5.2]), whatever its
fallibility, and it is what makes [LCST.4.3.1]'s nullable rows mean anything —
the ? on S? … ::T? exists to carry exactly this case, and a runtime that
refused it would make a legal program unrunnable the first time the slot was
absent.
The result feeds ??, Kleene logic and narrowing as any other T? does
([LCST.4.3.3]), so a cast propagates absence the way an operator does
([LNUL.4.1]) rather than ending the activation.
This is not [LCST.4.5.4]'s trap. That carve-out is for a source value
the target cannot represent — a Float with no Int, to Int — which is a value
the conversion was handed and could not convert. null is the absence of a
value, so there is nothing to represent and nothing to trap.
ratified — checks: idris×2 — see also: LCST.4.3.1, LCST.4.3.3, LCST.4.5.2, LCST.4.5.4, LNUL.4.1
4.4 Worked [LCST.4.4]#
[LCST.4.4.1] A fallible cast's result narrows like any nullable value: after a
guard that x::Int? is not null, the parsed value narrows from Int? to Int
for the rest of that scope ([LNUL.5.4]).
see also: LNUL.5.4
4.5 Conversion rules [LCST.4.5]#
checks: k
[LCST.4.5.1] Identity. x::T where x already has type T (including the same unit)
is a legal no-op; the compiler should warn (redundant cast), and the warning is
DS_VALIDATE_CAST_IDENTITY.
checks: idris×2
[LCST.4.5.2] Defined conversions. The compiler must classify each defined
(source, target) pair total or fallible per this table:
source → target |
class |
note |
|---|---|---|
|
total |
widening |
|
total |
a value whose truncation toward zero is an |
numeric |
rejected |
no implicit truthiness; use an explicit comparison |
unit |
total |
|
storage kind |
total |
|
unit |
rejected |
convert the number first ( |
|
total |
|
|
rejected |
no temporal text is read ([LTIM.14]); falls to [LCST.4.5.3] |
|
total |
the canonical text form; see [LCST.4.5.9] |
|
total |
its nanoseconds. |
|
total |
one type ([LTIM.1.3.5]); runtime identity |
|
fallible |
parse — requires |
|
rejected |
no conversion in either direction; |
any fact-typed |
rejected |
deferred ([LCST.4.5.10]); the compiler must refuse it with |
scalar |
rejected |
|
|
rejected |
|
opaque |
rejected |
sealed ([LOPQ.4.3]) — the compiler must reject |
ratified — checks: idris×7 — see also: LCST.4.5.10, LCST.4.5.11, LCST.4.5.3, LCST.4.5.4, LCST.4.5.5, LCST.4.5.6, LCST.4.5.7, LCST.4.5.8, LCST.4.5.9, LDET.3.2.7, LERR.1.4.10, LEXP.4.2, LEXP.7.13.1, LOPQ.4.3, LSTR.4.6, LTIM.1.3.1, LTIM.1.3.5, LTIM.14, LUNI.8.5, LUNI.8.7.1, LUNI.8.7.2, LUNI.8.7.3, RLOW.1.6
[LCST.4.5.11] A Json leaf converts as the value it holds. A leaf a path
reads from a Json value, and a value computed from one, has its kind only at
runtime ([LEXP.7.12.1]). x ?? d with x such a value is one too, whatever
type [LNUL.5.3] gives it, because ?? yields a present x as it is
([IRET.9.6]); so is a let name bound to one ([LLET.3.5]). A conversion of such
a value takes [LCST.4.5.2]'s row for the kind the value holds when it is
converted, and that row's class.
[LCST.4.3.1] decides the ?. Such a value is a nullable source: a path leaf may
be null, from an absent path or a JSON null ([LNUL.8.4]). A guard
is not null does not change that, because [LNUL.5.4] narrows a T? to a T
and such a value has no T until it is converted. The one exception is
x ?? d with a d that is not nullable, and a let name bound to one: it is
not null ([LNUL.5.3]), so it is not a nullable source. The
conversion is fallible when T is Int, Float or Bool, which a String
reaches by a parse, and total otherwise. The compiler must refuse a missing
? and a redundant one with DS_VALIDATE_CAST_NULLABILITY ([LCST.4.3.2]), and
the result's type is T? exactly when the source is nullable or the conversion
fallible. When the conversion runs, the engine must give:
for
null,null([LCST.4.2.3]);for a value of a kind with a row to
T, that row's result: a total row's value, a fallible row's value ornull, a trap as the row states ([LCST.4.5.4]);for a value of a kind with no row to
T, an array or an object among them, aTypeError([IRET.9.6]).
So a JSON 31 and a JSON 31.5 both convert under ::Float?, a JSON "31.5"
parses as a String does, and a JSON true is a TypeError. With j.a a
JSON 31.5, (j.a ?? 0)::Float? is 31.5, and (j.a ?? 0)::String takes no
?. j.a::String? takes its ? after j.a is not null and as well. A whole Json value read with no path is [LCST.4.5.2]'s Json row, and is
refused.
ratified — see also: IRET.9.6, LCST.4.2.3, LCST.4.3.1, LCST.4.3.2, LCST.4.5.2, LCST.4.5.4, LEXP.7.12.1, LLET.3.5, LNUL.5.3, LNUL.5.4, LNUL.8.4
[LCST.4.5.3] Undefined pairs — closed rejection. A (source, target) pair that is
neither an identity ([LCST.4.5.1]) nor a defined conversion ([LCST.4.5.2]) is a compile
error carrying a "no conversion from S to T" diagnostic,
DS_VALIDATE_CAST_UNDEFINED. The identity rule plus the
table is the complete, closed set of legal conversions; there is no silent gap and no
accidentally-forbidden common case (identity is explicitly legal).
A conversion the table defines and a compiler does not implement is refused with
DS_VALIDATE_CAST_UNSUPPORTED, and never compiled as some other conversion. The two
codes are distinct because they blame different parties: an undefined pair is the
program's error, an unimplemented one the compiler's.
ratified — see also: LCST.4.5.1, LCST.4.5.2
[LCST.4.5.4] Runtime totality, and the trap carve-out. A total ::T must not produce
null at runtime; the runtime must guarantee a defined result for every source value (this
is what makes [LCST.4.3.1] sound). A conversion whose failure mode yields a recoverable
value must be classified fallible (::T?). A total cast may, however, define a hard
runtime trap for a genuinely unrepresentable input — a trap, not a null, exactly as
an Int ÷0 is (Float → Int on a value with no Int, [LCST.4.5.2]). The trap is not a fallible
result: it never feeds the T? / ?? / Kleene stack.
The trap is not fatal. Recoverability is a property of the occurrence, not of the
kind ([LERR.1.4.9]): the test is whether engine state is still trustworthy. A
Float → Int on a value with no Int leaves working memory consistent, so the engine
abandons the activation and continues — the same disposition an Int ÷0 gets, and the reason the
two are named together here. The tier halts only where state is no longer trustworthy
([LERR.1.4.7]).
A null source is not an unrepresentable input. It reaches no
conversion at all ([LCST.4.3.4]), so it neither traps nor feeds this
carve-out.
ratified — checks: idris×5 — see also: LCST.4.3.1, LCST.4.3.4, LCST.4.5.2, LERR.1.4.7, LERR.1.4.9
[LCST.4.5.5] String → Bool accepted inputs. x::Bool? parses exactly the NFC strings
"true" and "false" (case-sensitive) to the corresponding Bool; any other value yields
null.
ratified — checks: idris×4
[LCST.4.5.7] String → Int accepted inputs. x::Int? parses a string that
is, in its entirety, an optional sign (+ or -) followed by an INT_LIT
([LLEX.11.1]): decimal, 0x hexadecimal, 0b binary or 0o octal, with _
separators placed as a source literal may place them. The runtime must yield
the signed value when it lies in the 64-bit range, and null for every other
string: one outside that range, one with any surrounding whitespace, a decimal
point or exponent, a unit suffix, or any spelling a source INT_LIT would not
admit.
ratified — checks: idris×8 — see also: LLEX.11.1
[LCST.4.5.8] String → Float accepted inputs. x::Float? parses a string
that is, in its entirety, an optional sign (+ or -) followed by either an
INT_LIT ([LLEX.11.1]) or a FLOAT_LIT ([LLEX.11.2]), with _ separators
placed as a source literal may place them, or by inf. The runtime must yield
the value rounded to the nearest Float, ties to even ([LDET.3.2.7]); a zero
keeps the sign it is spelt with, as an INT_LIT (-0, -0x0) or a
FLOAT_LIT (-0.0) alike, and inf with its sign yields that infinity. It must yield
null for every other string: a finite spelling whose value rounds to an
infinity, one with any surrounding whitespace, a unit suffix, a NaN spelling,
or any spelling a source literal would not admit.
Every canonical rendering of a Float ([LCST.4.5.6]) is accepted here, so a
Float → String → Float round trip returns the value it started from.
ratified — checks: alloy idris×7 — see also: LCST.4.5.6, LDET.3.2.7, LLEX.11.1, LLEX.11.2
[LCST.4.5.9] Temporal text. Each temporal type has one canonical text form,
and the ::String cast writes it:
Type |
Canonical form |
|---|---|
|
|
|
|
|
the compact form ([LTIM.8.4.1]) |
t::String, d::String and dur::String must render the value in its type's
canonical form. No cast reads one back ([LCST.4.5.2]). Formats other than the
canonical one are the to_string member's ([LTIM.8.1.2]).
see also: LCST.4.5.2, LTIM.8.1.2, LTIM.8.2.1, LTIM.8.4.1
[LCST.4.5.6] → String rendering. Float → String renders the finite value in the
canonical shortest-round-trip form ([LDET.3.2.7]). It is total and lossless (round-trips through String::Float?),
so [LCST.4.2.1]'s lossy-warn does not fire. ±Inf renders "inf" / "-inf" —
round-trips (strtod("inf") = Inf), keeping Float::String total. NaN, which an
expression can produce ([LDET.3.6.2]), renders "nan" whatever its sign and payload
([LEXP.4.3]). Int → String is the exact decimal; Bool → String is "true" /
"false" (the inverse of [LCST.4.5.5]). (The finite form is fixed by [LDET.3.2.7], not by
a cast-specific choice; the human-facing display default ("{x}", log)
resolves to the same shortest-round-trip form, with Inf displaying "inf" likewise —
the text plane renders non-finite, the JSON data plane rejects it.)
ratified — checks: idris×5 — see also: LCST.4.2.1, LCST.4.5.5, LDET.3.2.7, LDET.3.6.2, LEXP.4.3
[LCST.4.5.10] Fact → Json. <binding>::Json is a total conversion from any
fact-typed source: a canonical serialization of the fact. A non-finite Float slot
is a NOT_REPRESENTABLE trap ([LERR.1.4.10]), which abandons the activation where
the conversion stands ([LNUL.11.1]).
Deferred: not binding in this edition.
see also: LERR.1.4.10, LNUL.11.1
4.6 The :: cast is the only conversion form [LCST.4.6]#
[LCST.4.6.1] The type-headed call forms Json(x), Float(x), Int(x), and the
unit-assertion call <unit>(x) are not admitted. Authors write x::Json,
x::Float, x::Int, x::<unit>. The compiler must reject the call forms with
DS_VALIDATE_COERCION_FORM_REMOVED, naming the :: form as the replacement.
ratified
[LCST.4.6.2] Ordinary function calls (abs, min, series aggregations) and method calls
(t.hour) are unaffected. No schema-construction call form exists, so no other NAME(...)
shape changes.
see also: LCST.4.1, LCST.4.2, LCST.4.3, LCST.4.5.2, LCST.4.5.3, LCST.4.5.4, LCST.4.5.6, LDET.3.2.7, LGRA.22.7