tobogganSe connecter

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.

70 lignes · 4529 octetsBrut · Historique

ADR 0007: Type inference and traits

Status: Accepted — 2026-10-08, reviewed and accepted by the project owner (issue #2).

Context

Louise promises Hindley-Milner inference with Haskell-like traits (ADR 0001). Effect rows arrive in M3 and must fit into the same framework.

The checker has to be:

  • predictable for users,
  • error-tolerant for the LSP,
  • simple enough to verify.

Decision

Inference

  • Algorithm: HM with union-find unification. Expectations are pushed into lambdas, block tails and branches, so callback parameters are known and errors point at the offending expression.
  • Generalization happens only at top-level functions. Local lets are monomorphic, as with GHC's MonoLocalBinds.
    • Top-level functions are syntactic values, so generalizing them is always sound.
    • This avoids the value restriction for effects (M3).
  • Unannotated functions are inferred one strongly connected component of the call graph at a time, dependencies first. Within a component, functions are monomorphic in each other.
  • Fully annotated functions (every parameter and the result) are checked against their signature. The signature's type variables are rigid, so a body cannot be less general than its signature. Polymorphic recursion is allowed.
  • pub signatures are required (ADR 0001), so module interfaces never depend on inference.
  • Never coerces to any type at checking sites only: arguments, let annotations, branch results and returns. It is not part of unification: List<Never> is not List<Int>, which keeps contravariant positions sound.
  • An inferred function that never returns gets a polymorphic result: fn boom() { fail("x") } has type fn() -> a.

Records

  • A constructor with labelled fields is a record.
  • Field access r.f requires r's type to be known at that point, and the type to have exactly one constructor.
  • Ctor(..base, f: v) updates a record.

Traits

  • Single-parameter traits.
  • Impls are Haskell98-shaped: impl Trait<T<a, b>>, where T is a named type and a, b are distinct variables. Consequences:
    • impls can't overlap,
    • solving is directed by the head type and terminates.
  • Coherence:
    • at most one impl per (trait, type),
    • the impl must live in the trait's module or the type's module (no orphans).
  • Constraint sources:
    • instantiating a constrained scheme, such as a trait method or a function with where,
    • operators: ==/!= use Eq, <… use Ord, arithmetic uses Num.
  • Solving a constraint Trait<T>:
    1. Use a matching given: the function's where constraints, or constraints inferred for its generalized variables.
    2. Otherwise use the impl for T's head type, solving that impl's own constraints recursively.
    3. A remaining constraint on a non-generalizable variable is ambiguous (E0403).
  • Impl methods are checked against the trait method's signature, with the trait parameter replaced by the implementing type.
  • @derive(Eq, Ord, Show) synthesizes an impl that requires the trait for every type parameter. Every field must satisfy the trait under that assumption.

Dictionary elaboration plan (consumed in M4)

Solving produces evidence for each constraint, recorded at the expression that introduced it, in the callee's constraint order:

EvidenceMeaning
Impl { impl, args }Pass the dictionary built from this impl, applied to the evidence args for its own constraints.
Given(i)Pass the enclosing function's i-th dictionary parameter.

Lowering to Core will make:

  • each constrained function take one dictionary parameter per scheme constraint, in order;
  • each impl a dictionary constructor, a function from its constraints' dictionaries to a record of methods;
  • each trait method a projection from its dictionary.

Known Impl evidence at monomorphic call sites can be specialized later as an optimization. (Done for dictionaries without constraints in issue #61: see ADR 0013.)

Effects in M2

Function types already carry an effect slot, but it is unchecked until M3.

Consequences

  • Users annotate local polymorphic helpers, or hoist them to the top level.
  • Error messages stay local, because interfaces are explicit.
  • The trait system is deliberately small: no multi-parameter traits, no higher-kinded types, no overlapping impls. Any of these can be added later without breaking code.