waxwing

Rank-1 polymorphism and type parameters: design (P001)

Status: direction approved by the user in conversation 2026-10-08: parameterized types are in scope; type variables are opaque (no built-in comparison constraint); specialization is a separate pure phase (approach A). The user’s first written review (2026-10-08) gave conditional approval and required revisions to the termination rule, the handling of unsolved variables, and several precision points; section 12 maps each finding to its resolution. The revised spec was approved 2026-10-08. Nothing is implemented; the plan is docs/plans/2026-10-08-polymorphism-plan.md.

Goal and non-goals

Goal: generic data types and generic functions that are useful without type classes: type List(a) = Nil | Cons(a, List(a)); with length, append, reverse, zip, trees, Maybe(a), Pair(a, b), checked with rank-1 schemes from mandatory signatures and occurs-checked unification, and lowered by whole-program specialization (ADR 002) to ordinary monomorphic Go.

Non-goals: classes, constraints, derive, a Prelude (C001; section 11); comparing a value whose type contains a type variable; kinds, unapplied constructors and HKTs (K001); polymorphic recursion and nested data types (section 4); first-class functions or lambdas; type annotations inside expressions; explicit forall; modules and separate compilation (M001).

Assumption: whole-program compilation. Every program is one file and Go is generated only for what main and the monomorphic functions reach. When separate compilation, exported functions or library artifacts arrive (M001), reachability from main no longer determines what must be generated; that is M001’s design problem, not P001’s.

Preserved: phase boundaries and the internal-IR allowlist discipline (now two internal IRs, section 7); strict left-to-right evaluation; the structural order and print format of ADR 005; every existing diagnostic’s code, span and text. A program that declares no type parameters and mentions no type variable emits byte-identical Go: bootstrap/answer.go, shapes.go and tree.go are unchanged.

1. Syntax

typedecl  = "type", upper, [ "(", lower, { ",", lower }, ")" ], "=",
            ctor, { "|", ctor }, ";" ;
type      = "Int" | "Bool" | lower
          | upper, [ "(", type, { ",", type }, ")" ] ;
type List(a) = Nil | Cons(a, List(a));
type Pair(a, b) = Pair(a, b);
fn length(xs: List(a)): Int = match xs { Nil => 0, Cons(_,
  t) => 1 + length(t) };
fn zip(xs: List(a), ys: List(b)): List(Pair(a, b)) = …;

2. Names and declarations

3. Types, rigid and flexible variables

Four representations, each with only the variables its phase may hold:

Representation Module Variables it can hold
Source type Domain.Resolved Ty TRigid VarId (signature or declaration variable)
Checker type Features.Check.Unify (private) TRigid VarId and TMeta MetaId
Checked IR type Domain.Checked.Internal Ty TRigid VarId and THole HoleId
Monomorphic IR type Domain.IR.Internal Ty none: TInt \| TBool \| TData TypeId

All four share TInt, TBool and TData TypeId (Array Ty) (the last without arguments in the monomorphic IR, where each ground application has its own TypeId).

Unsolved metas. After a function’s body is checked, each meta still unbound becomes a hole in the checked IR (THole, numbered per function). Checking does not choose a type for it; there is no language-level default. Specialization later substitutes a representative (section 6).

Comparison. Both operands unify as today. After the function is checked, each comparison’s operand type, fully substituted, must be ground: a rigid variable in it is E_TYPE with new Problem NotComparable TypeName (fn same(x: a, y: a): Bool = x == y;, and List(a) == List(a)); a hole in it is E_TYPE with new Problem AmbiguousType TypeName (Nil == Nil). Both at the left operand. The second rejection is deliberate: under C001, comparison becomes an Ord use, and an Ord use at an undetermined type is an ambiguity error, so rejecting it now keeps C001 additive.

Patterns. On a scrutinee whose type is a rigid variable or a hole, only _ and binders fit; a constructor or literal pattern is E_TYPE as today.

4. Finite specialization

Specialization creates one copy per key (declaration, ground type arguments). This section gives the rule that makes the set of keys finite and its proof; the 10,000 limit (section 6) is a resource guard, never the termination argument.

4.1 The rule (checked in Features.Check, after typing)

Build two graphs: the call graph over functions (an edge for each call) and the reference graph over type declarations (an edge for each occurrence of a declared type anywhere inside a constructor field type, including inside another type’s arguments). Take strongly connected components of each separately.

Instantiation rule. The rule is applied after the referring function is checked, when each of its metas is either solved or a hole. At every reference from a member of a component to a member of the same component, each type argument, fully substituted, must be one of:

  1. a bare rigid variable of the referring declaration, or
  2. a ground type: no rigid variable; a hole counts as ground, because specialization replaces it with a fixed ground type.

A type that applies a constructor to an argument containing a variable, such as List(a) or Pair(Int, a), is neither, and is rejected.

Diagnostics, both new ErrorCode E_SPECIALIZATION, at the reference’s span, naming the referenced declaration:

4.2 Proof of finiteness

Functions. Fix a component C and an entering key (d0, τ̄0) with τ̄0 ground. Let G_C be the finite set of ground types written as type arguments at intra-component references in C (holes included, as their representative), and T = components(τ̄0) ∪ G_C. Claim: every key reached from (d0, τ̄0) through intra-component references has all its arguments in T. Induction on the path length: the entry satisfies it; at a key (d, σ̄) with σ̄ in T, a reference from d to d’ in C has each argument either a rigid variable v_i of d, instantiated to σ_i ∈ T, or a ground type in G_C ⊆ T. So C contributes at most Σ_{d ∈ C} |T|^{arity(d)} keys per entering key. Components form a DAG; each key’s body has finitely many references, so each component receives finitely many entering keys from the components above it (induction in topological order, starting from the finite seeds: the monomorphic functions). Hence finitely many keys.

Types. The same argument, over the reference graph, bounds the ground type applications reachable by unfolding constructor fields from any ground application; specialization of types and inhabitedness (section 5) both rely on it. Each key’s function body mentions finitely many types, so the type applications needed by finitely many function keys are finite too.

Swapping (f(a, b) calls g(b, a)), dropping (f(a, b) calls g(a)), concrete substitution (f(a, b) calls g(a, Int) and g(a, b) calls f(b, a)) and ordinary List(a) recursion (length(t); the field List(a)) are all accepted. f(a) calls g(List(a)) with g(a) calling f(a) is rejected at the first call.

4.3 Conservatism

The rule rejects some finite programs: f(a) calls g(List(a)) and g(b) calls f(Int) is finite but rejected. The exact criterion is the absence of an expanding cycle in the component’s instantiation graph (the check C# and the CLI apply to generic type definitions). P001 takes the simpler rule; relaxing it later rejects nothing that is accepted today.

5. Coverage and inhabitedness

Coverage runs on the checked polymorphic IR, once per source match, so each E_NON_EXHAUSTIVE or E_REDUNDANT is reported once, at its source span, and source correctness does not depend on which specializations are reachable.

6. Specialization

Features.Specialize (new, pure) consumes the checked IR and produces the monomorphic IR.

7. Phases, IR and module boundaries

Pipeline: Parse → Resolve → Check (types, section 4 rule, comparison groundness, coverage) → Specialize → Go.

8. Testing and proofs

Assurance follow-up (2026-10-21): the register keeps the existing §4.2 termination argument and the checks below. Adding kinds, dictionaries or new recursion rules must revisit their assumptions. This is no new proof requirement for the completed P001 milestone and no claim that its argument proves whole-compiler soundness. Local let-generalization with effects requires a separate reviewed design and soundness argument.

Every rejection row asserts exact code, span and text.

9. Usefulness without classes

examples/lists.bumpus doubles as documentation and as the seed of a future Prelude: length, append, reverse, zip, headOr(xs, d), last as Maybe(a), take/drop by Int, fromMaybe, fst/snd, swap, tree size, depth and flatten, Either(a, b) selection by match. A total head(xs: List(a)): a cannot be written (no value of a exists for Nil); headOr and Maybe(a) are the total forms. With no first-class functions, map and folds are written per use; that limit belongs to a later functions milestone, not P001.

10. Decisions

  1. Parameterized types are part of P001.
  2. Type variables are opaque; no built-in Ord; comparison requires a ground operand type (no rigid variable, no hole).
  3. Approach A: a separate pure Specialize phase; Format.Go stays monomorphic. Rejected: specializing inside Format.Go (breaks phase separation) and Go generics (ADR 002; cannot express future constructor variables).
  4. Lowercase implicit variables and parenthesized application.
  5. The instantiation rule of section 4, separately for functions and types, with its proof; the limit is a resource guard only.
  6. Unsolved metas become holes; Specialize picks a representative under the conditions of section 6. No typing-level default.
  7. Coverage on the polymorphic IR; rigid variables and holes abstract and inhabited.
  8. Byte-identical output for monomorphic programs; whole-program compilation assumed.

11. Relation to C001 (direction, not a settled design)

The user’s direction 2026-10-08: C001 brings a Bumpus Prelude in which Eq and Ord are ordinary classes (no compiler special case); derive exists; default Ord instances come from the Prelude and can be opted out of by hiding imports or similar; a type may have several instances, in the fp-ts style, without Haskell newtypes. ADR 002’s one-instance-per-type clause is dropped. This makes instance identity, not only type identity, part of the semantics: two Ord instances for one type can order it differently, and a collection built under one must not be used as if built under the other. Dictionary selection and instance identity are open questions for C001’s own design; nothing here settles them. P001 prepares only by keeping variables opaque, rejecting comparison at undetermined types, recording instantiations in the checked IR (where evidence will attach), and keeping holes out of any constrained position.

12. Review resolutions (2026-10-08)

Finding Resolution
1. Termination rule needs a proof; separate functions from types Section 4: rule stated over substituted arguments; ground means no variable at all; separate graphs, components and diagnostics for functions and types; proof in 4.2; conservatism and the exact criterion in 4.3; generated termination tests with the computed bound (section 8).
2. Int default not generally semantics-preserving No typing default. Unsolved metas become holes in the checked IR; Specialize picks a representative under four stated conditions (section 6); comparison at a hole type is rejected now so C001 stays additive; a representative-independence property test.
3. Distinguish rigid and flexible variables explicitly Section 3 table: each phase’s type admits only its variables; unifier tests for every rigid/flexible pair.
4. Variable-free monomorphic IR; structural keys Monomorphic Ty has no variable constructor; keys are structural over the full argument vector (section 6).
5. Rigid variables abstract in coverage Section 5, holes included.
6. Whole-program assumption Stated under Goal and non-goals.
7. Determinism, uniqueness, termination tests; limit scope; spec-key probe Section 8; limit global with code and span (section 6); spec-key probe on nested arguments.
8. C001 not settled Section 11 retitled and reworded; instance identity named as C001’s open question.