louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
ADR 0051: @derive(MsgPack) — deriving a MessagePack decoder
Status: Proposed (2026-10-18); needs engineering review (#44). It extends ADR 0019 (issue #140, part 3).
Context
- The codec landed.
std/msgpackis the MessagePack format in pure Louise overBytes:Value,write/read(untrusted-input safe: length prefixes checked before reads, nesting capped at 512,strUTF-8-checked,float 32widened), and the traitsToMsgPackandFromMsgPackwith impls forInt,Float,String,Bytes,Bool,List,Array,Option,ResultandDict(tuples and()wait for #106). No new externs: ADR 0035's bit operations and float bits (#143) were what the codec needed. - Encoding is derivable today.
ToMsgPackis@derivable(to_msgpack_of_constructor): its one methodfn to_msgpack(x: a) -> Valuefits ADR 0019's consumer shape, and the derive function writes the wire format below.@derive(ToMsgPack)works now; this ADR ratifies its format. - Decoding is not.
fn from_msgpack(Value) -> Result<a, DecodeError>is a producer: nox: ato fold over. ADR 0019 allows only single-method consumer derives and names "producers" and "several derivable methods per trait" as alternatives not taken. - Opaque types (ADR 0033, #139): a derived
decoder builds values through constructors, so on a
pub opaque typeit would forge values the module's smart constructor exists to refuse. - Decoding errors should say where decoding failed (a path such as
lines[2].qty), not only what was expected.
Decision
The mechanism: a producer shape for @derivable
ADR 0019 gains a second derivable shape. Next to the consumer
fn m(x: a) -> R, a derivable trait may have one method
fn m :: x: R -> F<a> // the result is one type constructor applied to `a`
(Result<a, E>, Option<a>, List<a>; R mentions no type variable).
The prelude gains the producer's structural view, dual to Constructor:
pub type Builder<a, r>
{ Builder
( name: String
, index: Int
, labels: List<String>
, make: fn(List<r>) -> F<a>
)
}
@derive(T) on a type generates m(x) = f(builders, x), where each
constructor's make is compiler-generated: it takes the fields raw
(as r; for MessagePack, as Values), runs the trait's own method of
each field's type on them (E0410 when a field has no impl, as today), and
builds the value. The derive function f is ordinary polymorphic Louise,
fn(List<Builder<a, R>>, R) -> F<a>, checked once where it is written —
every property ADR 0019 bought (no templates, no expansion, both
backends) is kept. For FromMsgPack, std/msgpack defines:
@derivable from_msgpack_of_builders
pub trait FromMsgPack<a> {
fn from_msgpack :: v: Value -> Result<a, DecodeError>
}
and from_msgpack_of_builders implements the reading rules below in
Louise, not in the compiler.
One trait or two: two
@derive(ToMsgPack, FromMsgPack) derives both directions; the two
traits already exist. "Several derivable methods per trait" (one
@derive(MsgPack) on a two-method trait) is rejected: two derives
compose today, and a multi-method @derivable(to: f, from: g) would
tangle two shapes into one validation for no new power. If a single name
is wanted later, that is sugar (@derive(MsgPack) expanding to both),
not a new mechanism.
A compiler derive: rejected
Deriving MsgPack in Rust, as Eq, Ord, Hash and Default are, is
simpler to specify but is what ADR 0019 moved away from: derives are
written in Louise, type-checked once, and reviewed as Louise. Those four
stay the only compiler derives, for the reasons ADR 0019 gave (a binary
consumer's eagerness; hashing on lookup paths). Nothing about MessagePack
is special enough to reopen that.
The wire format of user types (ratifies @derive(ToMsgPack))
Chosen for schema evolution:
- A record (labelled fields) is a map keyed by field label:
{"id": 7, "lines": [...]}. Fields can be added and reordered. - A positional constructor is a one-entry map from its name to its
fields' array:
{"Circle": [3]}. Tagged by name, not index, so constructors can be reordered. - A constructor without fields is its name:
"Dot". - Reading: unknown keys are ignored; a missing field of type
OptionisNone; any other missing field is an error. A field's error carries its path (lines[2].qty):DecodeErrorgains the path onMismatch(the codec's v0 errors carry the empty path; std is pre-1.0, the change is an edit, not a new type).
Int outside 64 bits
MessagePack integers are uint64/int64; Louise's Int promotes to a big
integer (ADR 0014). v0: write panics
with a clear message on an Int above 2^64 − 1 or below −2^63 (writing
is of local, trusted data; a Result there would tax every call site
for a case that is a bug). Reading a uint64 above 2^62 yields a big
integer, as it should. An Ext-encoded bignum (a Louise-reserved tag,
two's-complement big-endian payload) is the alternative, deferred until
a use case asks — it wants a small registry of Louise extension tags,
which the timestamp type (−1) will also want once std has a time type.
Opaque types
@derive(FromMsgPack) is refused on pub opaque types (an error
naming the rule): the derived decoder would forge values past the smart
constructor. The impl is written by hand, through the validating
function. A via: form (@derive(FromMsgPack(via: parse)), decoding
through the named function) is a possible later addition; @derive
takes only trait names today.
Where the codec lives
Louise over Bytes (both backends for free). If a benchmark shows the
codec hot, a pure kernel in runtime/louise_rt_prims — shared by the
runtime and the interpreter, like the hashes — can replace the insides
with no API change; the byte-exact vectors in tests/run/msgpack.lse
pin the format either way. Measured when it matters, not before.
HTTP (part 4 of the issue, decided here, built there)
http.msgpack(value) -> Response and a request body decoder, next to
http.html and http.form_body; the content type is
application/msgpack (the registered MIME type). The body size
limit (http.Settings) applies before decoding, as for any body.
Consequences
- E0412's validation grows the producer shape; the prelude gains
Builder; the derive generator grows the per-constructormakefunctions (the same order of size as ADR 0019's consumer generator). DecodeErrorgains a path;std/msgpack's hand-written impls fill it in as the derive does.- Tests (issue #140, part 5, with the derive): derived records, sum,
recursive and generic types in
tests/runon both backends; byte-exact vectors against another implementation (thermpcrate) in a Rust test;tests/uifor a derive on a fieldless-impl type (E0410) and on an opaque type. @derive(ToMsgPack)is already accepted and writing this format; rejecting this ADR's format means changingto_msgpack_of_constructorand its tests, not any user code (none can exist yet).
Alternatives considered
- Several derivable methods per trait — rejected above (two derives compose; the validation stays one shape per trait).
- A compiler derive — rejected above (ADR 0019's direction).
- Decoding through the consumer view (folding a
Constructorback into a value): impossible — the view cannot build ana; a producer needs construction capability, which is exactlyBuilder.make. - An
Extbignum for big integers — deferred above (a tag registry first; no use case yet). - Deriving through opaque types' validators (
via:) — later; refusal plus a hand-written impl covers v0.