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 0019: User-defined derives
Status: Accepted by the project owner on 2026-10-12; proposed 2026-10-09 (issue #49, v1 milestone M5); needs engineering review (#44). It extends the @derive part of ADR 0007.
Context
@derive covered six prelude traits (Eq, Ord, Show, Debug, Hash, Default), each generated by Rust code in Core lowering. A library could not make its own traits derivable: a ToJson, a Pretty, a size or a checksum had to be implemented by hand for every type.
What the mechanism should be:
- written in Louise, not as compiler plugins: there are no Rust proc macros for users;
- type-checked once, where it is written, not after expansion at each use;
- the same in both backends, and simple enough to review.
Decision
A derivable trait names a derive function
@derivable json_of_constructor
trait ToJson<a> {
fn to_json :: x: a -> Json
}
fn json_of_constructor :: c: Constructor<Json> -> Json {
match c.fields {
[] -> JStr(c.name)
, fields -> JArr([JStr(c.name), ..list.map fields fn f { f.value }])
}
}
@derive ToJson
pub type Shape
{ Dot
, Circle(Int)
, Rect(Int, Int)
}
The structural view (prelude):
pub type Constructor<r>
{ Constructor(name: String, index: Int, fields: List<Field<r>>) }
pub type Field<r> { Field(label: String, value: r) }
A derived method sees a value as its constructor (a sum of products, Generic-style, but at the value level): the constructor's name, its index in the type's declaration, and its fields in order, each with its label ("" when positional).
What @derive(T) on a type D<p1..pn> generates:
- the impl
T<D<p1..pn>> where T<pi>, for each parameterpiof kindType(as before); - the method
m(x) = f(Constructor(name, index, [Field(label, m(field)), ...])). Each field's value is the trait's own method applied to that field, through the field type's impl. A recursive type therefore recurses through its own derived impl. - Each field type must implement the trait (E0410, as before).
A derivable trait must have this shape (E0412 otherwise):
- one type parameter, of kind
Type; - exactly one method,
fn m(x: a) -> R. It takes one argument of the trait's type and has no effects and nowhereclause. Its resultRmentions no type variable; - the derive function
fis in scope in the trait's module, with typefn(Constructor<R>) -> R, total and without constraints. It may be polymorphic (fn(Constructor<r>) -> Int).
@derive(module.Trait) names a trait of an imported module.
Why this design:
- Simple: the derive function is ordinary code, checked once like any other function. The compiler adds one small generator (about 70 lines of Core lowering) and one validation function. There are no templates, no new syntax, and no expansion before type checking.
- Useful: it covers the common derives: serialization (
ToJson), printing (Show,Debug,Pretty), and folds over the structure (size, checksums). - Sound by construction: derived code type-checks if each field implements the trait and
fhas the right type. Both are checked at the@deriveand at the trait, not in generated code. - Both backends: derived methods are Core, so the interpreter and the C backend need no change.
The built-in derives
ShowandDebugare derived through this mechanism:@derivable(show_constructor)and@derivable(debug_constructor), with derive functions written in the prelude in Louise. Their output is unchanged, and the Rust code that generated them is gone.Eq,Ord,HashandDefaultstay derived by the compiler. They are marked@derivablewithout a function; only these four may be.EqandOrdtake two values, and stop at the first differing field. As a consumer of one value, the list of fields would compare every field, eagerly.Defaultproduces a value rather than consuming one.Hashis a consumer, but it is on lookup paths. Through the derive function, a derived hash allocates the constructor and its field list. On a three-field record (5M hashes,--release), that took 0.69 s instead of 0.26 s: 2.6 times slower.Showwas 1.65 times slower (2M shows: 0.88 s instead of 0.53 s), where building strings already dominates.
Consequences
- The prelude exports two more names,
ConstructorandField. A module's own definitions shadow prelude names, so existing code is unaffected. - Derived
ShowandDebugallocate the field list per call (see above). Specializing known evidence (M2) and inlining the derive function could remove the allocation later. - New diagnostic: E0412 for an invalid
@derivable. E0408 now says the trait is not marked@derivable. - Tests:
tests/run/user_derive.lseruns on both backends. It coversToJsonon records, enums and a recursive generic tree, aSizetrait with anIntresult, andShowandDebugnext to them;tests/run/derive.lsechecks that the built-in derives print what they printed before;tests/ui/derives.lsecovers every shape error, missing field impls, and a polymorphic derive function.
Alternatives considered
- Type-level
Generic(Haskell'sReptype family, with a generic impl over it). Exact and fast after optimization, but it needs type families or associated types, which ADR 0018 rejected for now. - Templates expanded per constructor (a
deriveblock withfor field in fields). No allocation, but new syntax in the parser, which another stream is changing, and generated code type-checked at every use: errors would point into expansions. - Two more shapes: binary consumers and producers, to also express
Eq,OrdandDefault. A binary shape would compare all fields eagerly unless field values became closures, which allocate more. The four built-in derives are not worth that complexity; a binary shape can be added if users need one. - Several derivable methods per trait (
@derivable(m1: f1, m2: f2)). Nothing needs it yet. Single-method traits keep the attribute and the generated code simple. - Compiler plugins in Rust. Rejected: derives must be written in Louise.
Note — 2026-10-18: phantom parameters are not constrained (issue #236)
Status: Proposed; needs engineering review (#44).
A derived impl's constraints no longer include a type parameter no
constructor stores. @derive(Eq) type Id<tag> { Id(Int) } gives
Eq<Id<tag>> needing Eq<Int> only — tag is phantom: it exists for
the compiler and disappears at runtime, so requiring Eq<tag> was wrong
(Eq<Id<User>> with User not Eq failed for no reason). The rule, in
collect_impls (louise_types/src/env/impls.rs): a parameter of kind
Type is constrained only if some constructor's field mentions it.
Constructors that refine or hide types (ADR 0041) are skipped (the type
isn't derivable then, per check_derived). A used parameter still
constrains (tests/ui/derives.lse has both).