| @@ -0,0 +1,70 @@ |
| 1 | +# ADR 0018: Multi-parameter traits and higher-kinded types |
| 2 | + |
| 3 | +**Status:** Proposed — 2026-10-09 (issue #49, v1 milestone M5); needs engineering review (#44). It extends [ADR 0007](0007-type-inference-and-traits.md), which deferred both features. |
| 4 | + |
| 5 | +## Context |
| 6 | +[ADR 0007](0007-type-inference-and-traits.md) made traits single-parameter, with Haskell 98-shaped impls, and noted that more could be added later without breaking code. Two common needs fall outside it: |
| 7 | +- **Relations between types:** `Convert<a, b>` (conversions), `Store<c, k>` (a collection and its keys), heterogeneous operators. |
| 8 | +- **Abstraction over type constructors:** `Functor<f>` for `List`, `Option` and `Result<e>`. |
| 9 | + |
| 10 | +The checker must stay predictable, terminate, and keep coherence (one impl per instance, no orphans). |
| 11 | + |
| 12 | +## Decision |
| 13 | + |
| 14 | +### Multi-parameter traits |
| 15 | +- **A trait has one or more distinct type parameters:** `trait Convert<a, b> { fn convert(x: a) -> b }`. A constraint names one type per parameter: `where Convert<a, String>`. |
| 16 | +- **Impls keep the Haskell 98 shape, per argument:** `impl Convert<List<a>, List<b>> where Convert<a, b>`. |
| 17 | + - Every trait argument is a named type applied to type variables (E0409). |
| 18 | + - No variable appears twice in the whole head, so `impl Convert<Option<a>, List<a>>` and `impl Convert<a, Int>` are rejected. |
| 19 | + - The named type at the head of each argument is the impl's **head**: `[List, List]` above. |
| 20 | +- **Overlap:** two impls of a trait overlap exactly when their heads are equal, because their argument patterns then always unify. The duplicate check (E0404) is therefore the overlap check. |
| 21 | +- **Solving `Trait<T1, ..., Tn>`** extends ADR 0007's steps: |
| 22 | + 1. A given that matches exactly. |
| 23 | + 2. If every `Ti` has a named type at its head, the impl with those heads, whose own constraints are solved recursively. Matching is one-way: impl variables are distinct, so no unification is needed, and chains still shrink the types and terminate. |
| 24 | + 3. If some `Ti` is still an inference variable, the constraint is **stuck**. If all its variables are generalized, it becomes a constraint of the inferred scheme (`fn convert_each(xs) { list.map(xs, convert) }` has type `fn(List<a>) -> List<b> where Convert<a, b>`). Otherwise it is ambiguous (E0403). |
| 25 | + 4. If some `Ti` is a signature's type variable and no given matches, the signature lacks a `where` (E0402). |
| 26 | + |
| 27 | +### Ambiguity: every parameter is determined at use sites |
| 28 | +There are **no functional dependencies** and no associated types. A call chooses an impl only from types already known from its arguments, its expected result, or annotations. |
| 29 | + |
| 30 | +```tilcayo |
| 31 | +fn known() -> Int { convert(True) } // Convert<Bool, Int> |
| 32 | +fn unknown() -> String { show(convert(True)) } // E0403: `Convert<Bool, _>` has unknown types |
| 33 | +``` |
| 34 | + |
| 35 | +- **Why:** this is the simplest sound rule. It needs no improvement step in the solver: a constraint never changes a type, so inference stays HM plus a separate solving pass, and error messages stay local. |
| 36 | +- **The cost:** some annotations that functional dependencies would infer. Given the evidence from use, they can be added later without breaking code: a dependency only makes more programs check. |
| 37 | + |
| 38 | +**Declarations are checked too**, so that the error appears where the problem is, not at every call: |
| 39 | +- every type variable of a signature's `where` clause must appear in its type. In `fn through(x: a) -> c where Convert<a, b>, Convert<b, c>`, nothing can determine `b` (E0403); |
| 40 | +- every trait method must mention every parameter of its trait. `trait Collection<c, e> { fn empty() -> c }` is rejected (E0403), since a call to `empty` could never determine `e`. |
| 41 | + |
| 42 | +### Coherence: the orphan rule, generalized |
| 43 | +**An impl must live in the module of its trait or in the module of one of its head types** (E0405). For one parameter, this is ADR 0007's rule. |
| 44 | + |
| 45 | +**Why this is enough for global uniqueness:** |
| 46 | +- An impl's module names its trait and all its head types. |
| 47 | +- Imports are acyclic (E0105). If two different modules each defined the same impl, each would have to be the home of one of the impl's items and name the other's. So each would import the other, which is a cycle. |
| 48 | +- Only one module can therefore hold a given impl, and the whole-program duplicate check (E0404) catches two in the same module. |
| 49 | + |
| 50 | +Unlike Rust's rule (RFC 2451), no ordering condition on the arguments is needed, because impl arguments are never bare type variables. |
| 51 | + |
| 52 | +### Dictionaries |
| 53 | +Nothing changes after type checking: a constraint still has one piece of evidence, and an impl is still one dictionary. Multi-parameter dictionaries work in both backends unchanged: |
| 54 | +- the trait's own constraint comes first in each method's scheme; |
| 55 | +- Core lowering, the interpreter, Perceus and the C backend are untouched, except for the impl names shown in Core (`dict Convert<List, List>`). |
| 56 | + |
| 57 | +## Consequences |
| 58 | +- `Constraint` holds a list of types and `ImplInfo` a list of heads. The language server prints constraints and impls with all their arguments. |
| 59 | +- Programs that checked before still check, with one exception: a signature whose `where` clause has a variable that appears nowhere else is now rejected at the declaration (E0403). Such a function could never be called without an ambiguity error. |
| 60 | +- **Tests:** |
| 61 | + - `tests/run/multitraits.til` runs on both backends; |
| 62 | + - `tests/ui/multitraits.til` covers ambiguity, overlap, shape, arity, missing givens and orphans; |
| 63 | + - `crates/tilc_types/tests/coherence.rs` checks the orphan rule across modules; |
| 64 | + - `evidence.rs` checks the impl tree chosen by both arguments. |
| 65 | + |
| 66 | +## Alternatives considered |
| 67 | +- **Functional dependencies** (`trait Convert<a, b> | a -> b`). They improve inference, but need an improvement step in the solver and a consistency check between impls. The engineering team should decide whether they are worth it after use. |
| 68 | +- **Associated types.** They are more expressive and are what Rust chose, but they add type-level functions, and therefore normalization, to unification. |
| 69 | +- **Flexible impl heads** (repeated variables, bare-variable arguments such as `impl Into<a, Option<a>>`). Overlap would need unification of impl heads, and matching would interact with unknown types. They can be added later if needed. |
| 70 | +- **Rust's orphan rule with uncovered type parameters.** It is unnecessary here, since arguments are never bare variables. |