
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
ADR 0041: GADTs and existential types
Status: Proposed (2026-10-13, issue #148); needs engineering review (#44), in particular of the soundness argument below: it changes the type checker's unification and adds local assumptions.
Context
Louise has Hindley–Milner inference with levels, effect rows and traits (ADRs 0007, 0008, 0013, 0018, 0019). It had neither generalized algebraic data types (GADTs) nor existential types. Issue #148 lists the uses:
- typed syntax trees, so an
evalneeds no run-time tags; - actor protocols whose requests say the type of their reply;
- heterogeneous lists of values that carry their traits.
The owner chose to have all of it: GADTs and existentials, with constraints.
Decision
Syntax
A constructor may end with its result type and constraints:
type Expr<a>
{ IntLit(Int) -> Expr<Int>
, If(Expr<Bool>, Expr<a>, Expr<a>) -> Expr<a>
, Pair(Expr<b>, Expr<c>) -> Expr<(b, c)>
, Lit(a) -> Expr<a> where Show<a>
}
type Showable { Showable(b) -> Showable where Show<b> }
- The constructor's type variables are its own; the header's parameters only give the type's arity.
- Those in the result are refined. The others are existential (hidden).
whereneeds the->. A constructor without->is an ordinary one, and an unknown variable there is still an error (E0211), so a typo can't make a variable existential.- A constructor's
whereends at the end of its line: a comma there separates constructors (the parser and tree-sitter grammar agree).
Typing
Schemes: a refined constructor's scheme is forall vars. constraints => fn(fields) -> T<indices>, the result's variables first.
- The result must be the type itself with all its arguments (E0214).
- A constraint variable must appear in a field or the result (E0211).
- The type's parameters must all be of kind
Type: equalities are between types, not effect rows or type constructors (E0214).
Building a value is an ordinary call. The scheme is instantiated, and its constraints are solved at the call like any function's. Their evidence is stored in the value.
Matching happens in a match arm. An arm whose pattern contains a refined constructor gets a scope (ArmScope): a level (enter/leave), equalities and givens. The constructor's result indices are matched against the scrutinee's type arguments (refined_ctor_pat):
- A constructor variable takes the type it meets (substitution, not an assumption).
If(..) -> Expr<a>on anExpr<x>makes the constructor'sabex. - A rigid variable of the scrutinee (a signature's, or one hidden by an enclosing pattern) that meets an index is assumed equal to it in the arm:
a := IntunderIntLit. Variables the index still has become hidden types (a := (b, c)underPair). - A unification variable meets an index by unification, with fresh variables for the index's unknowns. The scrutinee's type is being inferred, which is sound but order-dependent: annotate.
- Different named types make the constructor impossible here: E0202, plus W0301 since the arm can't match.
- Variables left are hidden: rigid variables at the arm's level (
fresh_scoped).
The constructor's constraints, instantiated, become the arm's givens. Their evidence is Evidence::Stored { pat, index }: the index-th dictionary of the value that pattern pat matched.
Assumptions in unification (InferCtx::rigid_eqs):
shallowfollows an assumed rigid to its type.assumeandretractscope the assumptions to the arm.unifybinds a variable to the other side before following assumptions. So a variable bound inside the arm names the rigid (?r := a, not?r := Int), which stays true outside the arm.zonkdoes not follow assumptions: recorded types and escape checks see the variables as written.
Constraints are solved after the body, each in its arm (Wanted::arm). The solver substitutes the equalities of the arm and its enclosing arms, and looks for a stored dictionary before the function's givens and the impls.
Escape: a hidden type is a scoped rigid. A variable outside the arm can't be bound to a type mentioning it (the level check of ADR 0010), so it can't leave through the arm's result or an outer variable (E0705).
Only in a match arm of its own (E0215). Not in a let or a parameter: the scope would be the rest of the block. Not in an alternative of |: alternatives would assume conflicting equalities. .field, ..base and @derive are refused for such constructors: they read fields by the type's parameters, which a refined constructor's fields don't follow.
Soundness
The aim is that every type the checker accepts is true at run time:
- An assumption
a := Tholds only in an arm whose pattern matched a constructor proving it. Inside the arm,aandTare the same type for the rest of that call of the function:ais fixed per call (no rank-n types). - Bindings made inside the arm are of variables to types as written. A binding to
ameansaoutside; one toT(from code that usesT) is checked against the variable's other uses outside the arm, where no assumption holds. A wrong guess is an error, never an unsound acceptance. This is why there are no "untouchable" variables (OutsideIn(X)): we give up principal types, not soundness. Inference may depend on the order of arms, which annotations settle. - A hidden type is rigid and scoped to the arm. Nothing outside can name it, and inside only its constraints' dictionaries are known about it.
- Exhaustiveness leaves out a constructor only if its result can't match the scrutinee's type. Equalities learned on rigid variables follow the search into the other columns (
(IntLit(n), e2)makese2anExpr<Int>). Where unsure (a variable, an unknown), it keeps the constructor: more arms are asked for, never fewer.
Run time
A value of a constructor with constraints stores their dictionaries after its fields. Field indices, labels, Perceus' arities (FnDecl::shapes, ADR 0040) and TRMC's holes count the dictionaries, and code that reads fields by index is unchanged. Dictionaries are ordinary values to Perceus, the interpreter, send (they are tuples of closures) and the runtime.
A refined constructor used as a function value captures its dictionaries in its closure, as a constrained function does. Matching binds the stored dictionaries like pattern variables (Key::Stored).
Consequences
- Typed syntax trees, values carrying their traits and typed actor protocols can be written.
tests/run/gadts.lsehas all three, with identical output on both backends and no leaked block. - Errors:
tests/ui/gadts.lsecovers each refused use. - Existing code is unchanged. Arm scopes exist only for patterns with refined constructors, and assumptions only inside them. The reordered
unifymatters only where an assumption exists. All UI, run and native tests pass as before. - Limits:
- no deriving for these types;
- no refined constructors in
let; - no indices of kind row;
- exhaustiveness is conservative with non-linear indices such as
T<x, x>; - no type-level computation;
- a refined match on a value whose type is still being inferred decides it from the first arm.
- Editors: the tree-sitter grammar parses the new syntax; hover shows a refined constructor's scheme with its
where.
Alternatives considered
- No: typed trees would need a run-time tag and a cast, or one type per result.
- Existentials only, as values with their dictionaries: covers mixed lists but not typed trees, and existentials alone don't give the actor-protocol case without a cast.
- Phantom types with
pub opaque type(#139): an opaqueRequest<r>with smart constructors gives a typedask, but the server'smatchcan't learnr, so it needs a cast inside. - GHC's OutsideIn(X), with untouchable variables: principal types at the cost of more annotations and a more complex solver. We keep simple unification and accept order-dependent inference, documented.
- An explicit
exists b.binder, orC(..): T<..>: the owner chose->with implicit existentials.
Revision 1 — 2026-10-14: pass-through effect-row parameters
Status: Proposed; needs engineering review (#44). Issue #199's lazy iterators.
The restriction "a refined constructor's parameters must all be of kind Type" (E0214) is relaxed: a parameter of kind row may appear in the result, as itself, unchanged — Iter(s, step: fn(s) -> Step<s, a> / <e>) -> Iter<a, e> carries the step's effects in the type while s stays existential. A row index that is anything but the parameter itself is still refused: matching would need an equality between rows, and the arm machinery (rigid assumptions, mentions_rigid) is type-only. Pass-through rows need no equality — they are substitution, which rows already support — and they cannot be hidden, so nothing new can escape an arm: a hidden type keeps guarding the value, and the row is a parameter of the result type, visible to callers. A fully hidden existential row was considered and rejected: effects are capabilities, and a hidden row that unified with the unpacker's row would run an arbitrary packed effect in a pure context — unsound, unlike hiding a type.
Note — 2026-10-15: one representation of what an arm refines
Status: Proposed; needs engineering review (#44). Issue #224.
Three places read the equalities a refined constructor gives: inference (InferCtx::rigid_eqs, undone arm by arm), trait resolution (a wanted constraint solved under its arm's equalities) and the exhaustiveness checker (equalities that follow its search). The last two kept separate maps with separate substitutions; they now share one, refine::Refinements, which keeps them acyclic: it refuses an equality whose type mentions its variable, through the known ones too.
That refusal fixed a crash. The checker learned equalities without an occurs check: a constructor Cycle(Int) -> Cyclic<a, List<a>, a> checked against a Cyclic<x, x, x> made x be List<x>, and comparing two types then recursed until the compiler's stack overflowed — even for a function that only takes such a value, through the check that its parameter's pattern is irrefutable. A refused equality now means the constructor is impossible there, as inference already reported it (E0202).
The two index walks — inference's match_index and the checker's index_match — stay separate, each documented with why: inference decides what an arm's code may assume (it unifies, instantiates what the scrutinee doesn't fix as hidden types, and undoes it after the arm), while the checker only asks whether a constructor is possible, after inference, where whatever it cannot tell counts as possible. Whether one walk parameterised by those choices would be clearer is left to the review.