Algorithm Search⌘K

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 Vec A n\mathsf{Vec}\ A\ n of lists of length exactly nn depends on the number nn, and the function type Π(x:A). B\Pi(x{:}A).\ B has a result type BB that may mention the argument xx. 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 U\mathcal U (the type of small types), dependent functions, and the natural numbers:

t,A,B::=x∣U∣Π(x:A). B∣λx:A. t∣t u∣Nat∣zero∣succ t∣natrec P z s tt, A, B ::= x \mid \mathcal U \mid \Pi(x{:}A).\ B \mid \lambda x{:}A.\ t \mid t\ u \mid \mathsf{Nat} \mid \mathsf{zero} \mid \mathsf{succ}\ t \mid \mathsf{natrec}\ P\ z\ s\ t

When xx does not occur in BB, Π(x:A). B\Pi(x{:}A).\ B is the ordinary function type A→BA \to B. The dependent pair Σ(x:A). B\Sigma(x{:}A).\ B, whose second component has a type that depends on the first, is added in the same way.

Judgments and rules

There are two judgments: Γ⊢t:A\Gamma \vdash t : A (typing) and Γ⊢t≡u:A\Gamma \vdash t \equiv u : A (definitional equality). In the text blocks, (x : A) -> B is Π(x:A). B\Pi(x{:}A).\ B 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 Nat\mathsf{Nat} give Nat:U\mathsf{Nat} : \mathcal U, zero:Nat\mathsf{zero} : \mathsf{Nat}, succ:Nat→Nat\mathsf{succ} : \mathsf{Nat} \to \mathsf{Nat}, 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

(λx:A. t) u≡t[u/x](β)natrec P z s zero≡znatrec P z s (succ m)≡s m (natrec P z s m)(\lambda x{:}A.\ t)\ u \equiv t[u/x] \quad (\beta) \qquad \mathsf{natrec}\ P\ z\ s\ \mathsf{zero} \equiv z \qquad \mathsf{natrec}\ P\ z\ s\ (\mathsf{succ}\ m) \equiv s\ m\ (\mathsf{natrec}\ P\ z\ s\ m)

and often the η\eta rule for functions. T-App is where dependency enters: the type of the result is BB with the actual argument put in. T-Conv is where computation enters: a term of type AA also has every type that is definitionally equal to AA.

The calculus of constructions (Coquand and Huet 1988) uses the same rules with a second sort, so that Π\Pi 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 natrec\mathsf{natrec} rules, read from left to right as steps t⟶t′t \longrightarrow t'. Soundness is progress and preservation. Preservation (often called subject reduction here): if Γ⊢t:A\Gamma \vdash t : A and t⟶t′t \longrightarrow t', then Γ⊢t′:A\Gamma \vdash t' : A. Its proof needs the substitution lemma and injectivity of Π\Pi: if Π(x:A). B≡Π(x:A′). B′\Pi(x{:}A).\ B \equiv \Pi(x{:}A').\ B', then A≡A′A \equiv A' and B≡B′B \equiv B'. Injectivity follows from confluence of reduction (Church-Rosser), because two terms are definitionally equal exactly when they reduce to a common term, and a Π\Pi only reduces to a Π\Pi. Progress: a closed well-typed term is a value (a λ\lambda, zero\mathsf{zero}, succ\mathsf{succ} 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 Π\Pi 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 A≡BA \equiv B?

Theorem. If reduction is confluent and every well-typed term has a normal form that can be computed, then A≡BA \equiv B is decidable for well-typed AA and BB, and so type checking of annotated terms is decidable.

Proof sketch. By confluence, A≡BA \equiv B holds if and only if AA and BB 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 β\beta, 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 Π\Pi. □\square

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 MM 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 MM halts, so a type checker would solve the halting problem. The same happens with the rule U:U\mathcal U : \mathcal U: Girard's paradox gives a well-typed term with no normal form, and Meyer and Reinhold (1986) showed that type checking with U:U\mathcal U : \mathcal U 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 Vec:U→Nat→U\mathsf{Vec} : \mathcal U \to \mathsf{Nat} \to \mathcal U with

nil:Π(A:U). Vec A zerocons:Π(A:U). Π(n:Nat). A→Vec A n→Vec A (succ n)\mathsf{nil} : \Pi(A{:}\mathcal U).\ \mathsf{Vec}\ A\ \mathsf{zero} \qquad \mathsf{cons} : \Pi(A{:}\mathcal U).\ \Pi(n{:}\mathsf{Nat}).\ A \to \mathsf{Vec}\ A\ n \to \mathsf{Vec}\ A\ (\mathsf{succ}\ n)

Then head:Π(A:U). Π(n:Nat). Vec A (succ n)→A\mathsf{head} : \Pi(A{:}\mathcal U).\ \Pi(n{:}\mathsf{Nat}).\ \mathsf{Vec}\ A\ (\mathsf{succ}\ n) \to A needs no case for the empty vector: nil\mathsf{nil} has type Vec A zero\mathsf{Vec}\ A\ \mathsf{zero}, and zero≢succ n\mathsf{zero} \not\equiv \mathsf{succ}\ n. The run-time error of head [] becomes a type error.

Conversion at work. Define ++ by recursion on its first argument, so zero+n≡n\mathsf{zero} + n \equiv n and succ m+n≡succ (m+n)\mathsf{succ}\ m + n \equiv \mathsf{succ}\ (m + n). A term v:Vec Nat 3v : \mathsf{Vec}\ \mathsf{Nat}\ 3 can be passed to a function that expects Vec Nat (2+1)\mathsf{Vec}\ \mathsf{Nat}\ (2 + 1): the checker reduces 2+12 + 1 to 33 and T-Conv applies. For append : Vec A m -> Vec A n -> Vec A (m + n), the clause append nil ys = ys checks because zero+n≡n\mathsf{zero} + n \equiv n.

Definitional against propositional equality. The type Vec A (m+zero)\mathsf{Vec}\ A\ (m + \mathsf{zero}) is not definitionally equal to Vec A m\mathsf{Vec}\ A\ m when mm is a variable: ++ recurses on its first argument, which is stuck. The equation m+zero=mm + \mathsf{zero} = m is true for every mm, but only propositionally: it has a proof by induction, a term p:Π(m:Nat). Id Nat (m+zero) mp : \Pi(m{:}\mathsf{Nat}).\ \mathsf{Id}\ \mathsf{Nat}\ (m + \mathsf{zero})\ m built with natrec\mathsf{natrec}. To use a vector of length m+zerom + \mathsf{zero} as one of length mm, the program must transport it along p mp\ m. Definitional equality is what the checker decides by itself. Propositional equality is what the programmer proves.

Specifications as types. The type Π(ℓ:List Nat). Σ(ℓ′:List Nat). Sorted ℓ′×Perm ℓ ℓ′\Pi(\ell{:}\mathsf{List}\ \mathsf{Nat}).\ \Sigma(\ell'{:}\mathsf{List}\ \mathsf{Nat}).\ \mathsf{Sorted}\ \ell' \times \mathsf{Perm}\ \ell\ \ell' 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/04XM6K/text.yaml