Conceptadvanced
Dependent types
Also called dependent type theory, pi types.
Types that depend on values, such as vectors of length n, so that types can state and check full program specifications.
A dependent type is a type that contains a term. The type of lists of length exactly depends on the number , and the function type has a result type that may mention the argument . With dependent types, a type can state a full specification, and by the Curry-Howard correspondence a term of that type is a proof of it. This page is about full dependent types as in Martin-Löf type theory and the calculus of constructions. It differs from System F, where types depend on types and not on terms, and from refinement types, which attach predicates from a fixed logic to ordinary types and check them with a decision procedure. Indexed types such as those of Dependent ML lie between the two: the indices come from a separate decidable domain.
In plain words
In most languages, a type is a fixed label such as "list of integers". A dependent type can mention a value. "A list of exactly 3 integers" is a type, and so is "a list whose length is n", where n is a variable of the program. A function can then promise, in its type, that it takes a list of length n and returns a list of the same length, or that it returns a sorted list. To check such types, the checker must sometimes run part of the program at compile time, for example to see that "2 + 1" and "3" are the same length. That is why the language must make sure that this computation always ends: if it could loop, the checker could loop too.
The idea
Syntax
In a dependent type theory, types and terms share one syntax. A minimal system has a universe (the type of small types), dependent functions, and the natural numbers:
When does not occur in , is the ordinary function type . The dependent pair , whose second component has a type that depends on the first, is added in the same way.
Judgments and rules
There are two judgments: (typing) and (definitional equality). In the text blocks, (x : A) -> B is and B[u/x] is substitution.
(x : A) in Gamma Gamma |- A : U Gamma, x : A |- B : U
----------------- T-Var ---------------------------------------- T-Pi
Gamma |- x : A Gamma |- (x : A) -> B : U
Gamma, x : A |- t : B
------------------------------------------ T-Abs
Gamma |- fun (x : A) -> t : (x : A) -> B
Gamma |- t : (x : A) -> B Gamma |- u : A
-------------------------------------------- T-App
Gamma |- t u : B[u/x]
Gamma |- t : A Gamma |- A == B : U
-------------------------------------- T-Conv
Gamma |- t : B
The rules for give , , , and the eliminator, which is both recursion and induction:
Gamma, n : Nat |- P : U Gamma |- z : P[zero/n]
Gamma |- s : (k : Nat) -> P[k/n] -> P[succ k/n] Gamma |- m : Nat
-------------------------------------------------------------------- T-NatRec
Gamma |- natrec P z s m : P[m/n]
Definitional equality is the least congruence (closed under all term formers, and an equivalence) that contains the computation rules
and often the rule for functions. T-App is where dependency enters: the type of the result is with the actual argument put in. T-Conv is where computation enters: a term of type also has every type that is definitionally equal to .
The calculus of constructions (Coquand and Huet 1988) uses the same rules with a second sort, so that can range over types as well as terms. It contains System F and simple dependent types as fragments.
Semantics and soundness
The operational semantics is beta reduction and the rules, read from left to right as steps . Soundness is progress and preservation. Preservation (often called subject reduction here): if and , then . Its proof needs the substitution lemma and injectivity of : if , then and . Injectivity follows from confluence of reduction (Church-Rosser), because two terms are definitionally equal exactly when they reduce to a common term, and a only reduces to a . Progress: a closed well-typed term is a value (a , , of a term, or a type former) or takes a step, by canonical forms.
For a type theory used as a logic, a stronger property matters: normalization. Every well-typed term reduces to a normal form. Coquand and Huet proved strong normalization for the calculus of constructions with the reducibility method. Normalization gives consistency: the empty type has no closed normal term, so no closed term at all.
Type checking needs definitional equality
The rules above are not syntax-directed, because T-Conv applies to any term. The usual algorithm is bidirectional: it infers a type for variables and applications, checks abstractions against a given type, and uses T-Conv only where an inferred type meets an expected one. So type checking reduces to one question, asked many times: is ?
Theorem. If reduction is confluent and every well-typed term has a normal form that can be computed, then is decidable for well-typed and , and so type checking of annotated terms is decidable.
Proof sketch. By confluence, holds if and only if and have a common reduct, and so if and only if their normal forms are the same up to renaming of bound variables. Normal forms can be computed: with strong normalization, any reduction order reaches the normal form; with weak normalization of , leftmost-outermost order does. So the checker normalizes both sides and compares them. The rest of the algorithm recurses on the structure of the term, and each recursive call is on a smaller term. Its completeness for the declarative rules uses subject reduction and injectivity of .
Normalization is not a detail. If definitional equality may unfold unrestricted recursion, type checking is undecidable. Take a function f : Unit -> Nat with general recursion that searches for a halting step of a Turing machine and returns 0 when it finds one. The term refl : Id Nat (f ()) 0 is well typed if and only if f () reduces to 0, that is, if and only if halts, so a type checker would solve the halting problem. The same happens with the rule : Girard's paradox gives a well-typed term with no normal form, and Meyer and Reinhold (1986) showed that type checking with is undecidable. Practical languages that allow general recursion keep checking decidable by not unfolding such definitions during conversion: Lean treats a partial def as opaque, and F* does not reduce code in its divergence effect. The part of the language that the checker computes with must be normalizing.
Inference is harder than checking. Filling in a missing argument or annotation means solving equations between terms that contain unknown functions, which is higher-order unification and is undecidable in general. Proof assistants solve the decidable cases, such as Miller's pattern fragment, and ask the user for the rest (see the unification problem for the first-order case).
Examples
Vectors. Let with
Then needs no case for the empty vector: has type , and . The run-time error of head [] becomes a type error.
Conversion at work. Define by recursion on its first argument, so and . A term can be passed to a function that expects : the checker reduces to and T-Conv applies. For append : Vec A m -> Vec A n -> Vec A (m + n), the clause append nil ys = ys checks because .
Definitional against propositional equality. The type is not definitionally equal to when is a variable: recurses on its first argument, which is stuck. The equation is true for every , but only propositionally: it has a proof by induction, a term built with . To use a vector of length as one of length , the program must transport it along . Definitional equality is what the checker decides by itself. Propositional equality is what the programmer proves.
Specifications as types. The type is the type of correct sorting functions. A term of this type is a sorting function together with a proof that its output is a sorted permutation of its input.
Where it is used
- Proof assistants. Coq (now Rocq) is based on the calculus of inductive constructions, Agda and Idris on Martin-Löf type theory, and Lean on a variant of the calculus of constructions. They are used for mathematics and for verified compilers, such as CompCert, written and proved in Coq.
- Programming. Idris and Agda are used as programming languages with dependent types. F* combines dependent types with an SMT solver that discharges many proof obligations, close to refinement types.
- Restricted forms. Dependent ML (Xi and Pfenning 1999) and ATS index types by integer expressions and check the constraints with a decision procedure, so type checking never runs program code. Haskell's GADTs and type families give a similar effect with type-level data.
- Quantities. Idris 2 adds use counts to dependent types, so that a variable used only in types is erased at run time, and others can be linear.
References
- Per Martin-Löf, Intuitionistic Type Theory (notes by Giovanni Sambin of a series of lectures given in Padua, June 1980), Bibliopolis, Naples, 1984, Studies in Proof Theory 1, the forms of judgment, including definitional equality, and the rules for the cartesian product of a family of sets and the disjoint union of a family of sets
- Thierry Coquand and Gérard Huet, The calculus of constructions, Information and Computation 76(2-3), 1988, pages 95 to 120, the calculus with dependent products over terms and types, and its strong normalization proof
- Benjamin C. Pierce (ed.), Advanced Topics in Types and Programming Languages, MIT Press, 2005, Chapter 2 (David Aspinall and Martin Hofmann, Dependent types), the conversion rule, algorithmic typing and the role of normalization in deciding it
- Albert R. Meyer and Mark B. Reinhold, "Type" is not a type, Thirteenth ACM Symposium on Principles of Programming Languages (POPL 1986), pages 287 to 295, the results on a typed lambda calculus with a type of all types
- Hongwei Xi and Frank Pfenning, Dependent types in practical programming, Twenty-Sixth ACM Symposium on Principles of Programming Languages (POPL 1999), pages 214 to 227, indexed types over a separate, decidable constraint domain, as opposed to full dependent types
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag 04XM6K Edit this page History Report an error Markdown