---
title: "Basic Polymorphic Typechecking"
authors:
  - "Luca Cardelli"
year: 1987
venue: "Science of Computer Programming 8(2), 147–172, April 1987"
arxiv: null
doi: "10.1016/0167-6423(87)90019-0"
source: "https://lucacardelli.name/Papers/BasicTypechecking.pdf"
version_reviewed: "Author PDF from Cardelli's papers page, marked 'Revised 6/21/88', read in full including the Modula-2 appendix; the Elsevier version was not opened"
topics:
  - "type-inference"
  - "hindley-milner"
  - "parametric-polymorphism"
  - "unification"
  - "let-polymorphism"
  - "typechecker-implementation"
seed_rank: 2776
seed_batch: "archive-2026-12-02"
reviewed: "2026-12-02"
pool: "languages"
relevance_score: 8
lineage: programming-language-foundations
cites:
  - title: "The Principal Type-Scheme of an Object in Combinatory Logic"
    url: "https://doi.org/10.1090/S0002-9947-1969-0253905-6"
    year: 1969
    arxiv: null
    doi: "10.1090/S0002-9947-1969-0253905-6"
  - title: "A Theory of Type Polymorphism in Programming"
    url: "https://doi.org/10.1016/0022-0000(78)90014-4"
    year: 1978
    arxiv: null
    doi: "10.1016/0022-0000(78)90014-4"
  - title: "Principal Type-Schemes for Functional Programs"
    url: "https://doi.org/10.1145/582153.582176"
    year: 1982
    arxiv: null
    doi: "10.1145/582153.582176"
  - title: "A Machine-Oriented Logic Based on the Resolution Principle"
    url: "https://doi.org/10.1145/321250.321253"
    year: 1965
    arxiv: null
    doi: "10.1145/321250.321253"
  - title: "On Understanding Types, Data Abstraction, and Polymorphism"
    url: "https://doi.org/10.1145/6041.6042"
    year: 1985
    arxiv: null
    doi: "10.1145/6041.6042"
  - title: "Fundamental Concepts in Programming Languages"
    url: "https://doi.org/10.1023/A:1010000313106"
    year: 1967
    arxiv: null
    doi: "10.1023/A:1010000313106"
  - title: "Une extension de l'interprétation de Gödel à l'analyse, et son application à l'élimination des coupures dans l'analyse et la théorie des types"
    url: "https://doi.org/10.1016/S0049-237X(08)70843-7"
    year: 1971
    arxiv: null
    doi: "10.1016/S0049-237X(08)70843-7"
  - title: "Data Types as Lattices"
    url: "https://doi.org/10.1137/0205037"
    year: 1976
    arxiv: null
    doi: "10.1137/0205037"
see:
  - "628-the-principal-type-scheme-of-an-object-in-combinatory-logic"
  - "629-a-theory-of-type-polymorphism-in-programming"
  - "640-principal-type-schemes-for-functional-programs"
  - "668-on-understanding-types-data-abstraction-and-polymorphism"
  - "615-fundamental-concepts-in-programming-languages"
  - "638-girard-system-f-une-extension-de-linterpretation-de-godel"
  - "614-data-types-as-lattices"
---

# Basic Polymorphic Typechecking

## One-sentence takeaway

This is the practitioner's Hindley–Milner. Cardelli takes a six-form kernel of ML (identifiers, if, fun, application, let, and rec/sequential declarations) and gives the complete typechecker as a short Modula-2 program. Types are mutable type-variable cells with an `instance` field, `Prune` collapses chains of instantiated variables, and unification includes the occurs check. Let-polymorphism is reduced to one bookkeeping rule: a list of *non-generic* variables, which `FreshType` shares while copying every generic one.

## Why it matters here

ano's standing rules and BQN-style twins will eventually want inferred types rather than annotations. This is the smallest correct, implementation-first account of how to do that with destructive unification instead of substitution maps: a shape (in-place variable cells plus path pruning, close to union-find) that many HM implementations still follow. It is also a clear explanation of exactly where let-generalization stops (fun-bound identifiers, and rec-bound ones inside their own definition, stay monomorphic). That matters for whether an ano rule body can reuse a helper at two column types. Reading it next to Milner 1978 (629) and Damas–Milner (640) gives theory and code side by side. The ano bridge is this card's reading.

## Key ideas

- **Implicit vs explicit polymorphism.** Implicit polymorphism is explicit polymorphism with the type abstractions and applications erased. Inference rediscovers them, which is why the same implicit program can map to different explicit ones, and why side effects make it delicate.
- **Typing as constraint solving.** A bottom-up walk assigns fresh variables to fun parameters and unifies the function type against A → β at each application. Robinson's most-general unifiers guarantee a unique best (principal) type, and the order of inspection does not change the result (worked example: `length : α list → int`).
- **Generic vs non-generic.** A variable is generic in e iff it does not occur in the type of any enclosing fun binder. `fun(f) pair(f(3))(f(true))` is rejected, while the let-bound version is accepted. Recursive definitions are typed with fresh *non-generic* variables for all their identifiers, which become generic only after the `rec`.
- **Implementation tricks.** Instantiation happens in place on the variable cell. `Prune` returns either an uninstantiated variable or an operator, shortening chains as it goes. When a non-generic variable is unified with a term, every variable in that term becomes non-generic automatically through the shared list, so `Unify` needs no special case. New data types are just identifiers in the initial environment, so the checker never changes.
- **Formal system first.** A deduction system (IDE, COND, ABS, COMB, LET, GEN, SPEC, …) is presented as the specification, with the heuristic of applying GEN and SPEC only around LET as the bridge to the algorithm.

## Caveats

This is an expository tutorial, not new theory. Soundness and principality are cited (Milner 1978, Damas & Milner 1982), not proved here. The language has no references or side effects, no records or overloading, and no type declarations. The paper flags the side-effect interaction with polymorphism as unresolved at the time. The code favours clarity: `RetrieveTypeEnv` copies more than necessary, and parsers and printers are omitted. The reviewed PDF is the 1988 revision, which may differ slightly from the 1987 journal pages. The ano bridge is this card's reading.

## Links

- DOI: https://doi.org/10.1016/0167-6423(87)90019-0
- PDF (author copy, revised 1988, as read): https://lucacardelli.name/Papers/BasicTypechecking.pdf
