5. Dependent types
Draft
Types that mention values
The types of Section 2 are built from types alone: in \(A \to B\), the output type \(B\) cannot depend on which input value of \(A\) was supplied. Allow a type expression to mention a value, and three capabilities unlock at once. First, data structures can enforce their own invariants in their types: Vec α n is the type of lists of length \(n\), and Fin n is the type of natural numbers strictly below \(n\). Second, logical predicates become parameterized types: since Section 4 identifies each proposition with the type of its proofs, "\(p\) is prime" assigns a distinct proof type to each number \(p\). Third, the quantifiers become ordinary type constructors: "for all" is a function type whose return type varies with the input value, and "exists" is a pair type whose second component's type depends on the first component's value. That single extension turns a programming language into a language expressive enough to write mathematical statements; it is the second step of the series' argument, and Lean's logic (Section 6) is built on it.
Two structural costs come with that expressive power and organize this part. Because types can now embed expressions like 0 + 1 + 1, checking whether two types match forces the checker to evaluate terms inside types using a decidable computational equality (Section 5.3), which is strictly weaker than the mathematical equality proved in theorems (Section 5.4). And because a parameterized type maps values into a universe of types, that universe is itself a type; letting a universe contain itself makes every proposition provable (Section 5.5), so universes must be stratified while still accommodating inductive definitions and mathematical set constructions (Sections 5.6 and 5.7).
Here are six families of types, drawn the way a mathematician draws a family of sets.
family n ↦ Fin (n + 1): over each point n of the base Fin 4 stands its fiber, the numbers 0 to n
fiber 3 a section: one element chosen in every fiber (in brackets), here
(2) [2] 0 ↦ 0, 1 ↦ 1, 2 ↦ 0, 3 ↦ 2, number 15 of 24: a dependent function,
[1] 1 1 (n : Fin 4) → Fin (n + 1)
[0] 0 [0] 0 a pair: one element of one fiber (in parentheses), here ⟨2, 2⟩:
base n 0 1 2 3 a dependent pair, (n : Fin 4) × Fin (n + 1)
sizes 1 2 3 4 sections 1 × 2 × 3 × 4 = 24 (the Π type), points 1 + 2 + 3 + 4 = 10 (the Σ type)
all six family sizes of the fibers sections points
i ↦ Fin 2 2 2 2 8 6 Fin 3 → Fin 2, Fin 3 × Fin 2
n ↦ Fin (n + 1) 1 2 3 4 24 10
n ↦ Fin n 0 1 2 3 0 6 an empty fiber: no section
n ↦ Vector (Fin 2) n 1 2 4 8 64 15
d ↦ d ∣ 60, d ≤ 6 1 1 1 1 1 1 1 6 the section proves ∀ d ≤ 6, d ∣ 60
d ↦ d ∣ 12, d ≤ 12 1 1 1 1 0 1 0 0 0 0 0 1 0 6 ∀ fails; a point proves ∃ d ≤ 12, d ∣ 12
(the live figure needs JavaScript and loads about 0.05 MB: what Lean 4.35.0 said of the 12 types)
Picture the input type \(A\) laid out horizontally as a base line of points, and above each point \(x\) draw a vertical column, its fiber \(B(x)\), holding the inhabitants of that type. Unlike a non-dependent product \(A \times B\) whose columns are identical copies of \(B\), the fibers of a dependent family vary freely from point to point: Fin (n + 1) grows taller as \(n\) increases, while the divisibility family "\(d\) divides 12" has one proof element over divisors of 12 and an empty fiber over non-divisors, remaining a genuine type in both cases. Set theory writes the same picture as an indexed family of sets \((B_x)_{x \in A}\), and Per Martin-Löf, whose type theory of the 1970s gave dependent types their modern form, reads his own notation directly in those terms: "that B(x) is a set under the assumption x ∈ A, or, better, that B(x) is a family of sets over A".P. Martin-Löf, Intuitionistic Type Theory, notes by G. Sambin of lectures given in Padua in June 1980, Bibliopolis, Naples, 1984 (retypeset edition, whose page numbers are used throughout this part), "Hypothetical judgements and substitution rules", p. 9. A set, in these notes, is what this part calls a type. Quantifying over values (Section 5.2), rewriting along an equation (Section 5.4), and proving by induction (Section 5.6) all act on families of fibers.
Definition 5.1.1 (family of types). Let \(A\) be a type. A family of types over \(A\), or dependent type, is a type \(B(x)\) in which a variable \(x\) of type \(A\) may occur. For a term \(a\) of type \(A\), the type \(B(a)\) obtained by putting \(a\) for \(x\) is the fiber of the family over \(a\).
In Lean a family is declared like any other type: here the vectors, with the length in the type, and the primes of the running example of the series, a proof of Euclid's theorem written with Lean's core library alone.
universe u v
inductive Vec (α : Type u) : Nat → Type u where
| nil : Vec α 0
| cons : {n : Nat} → α → Vec α n → Vec α (n + 1)
/-- A prime is a number from 2 on whose only divisors are 1 and itself. -/
def Prime (p : Nat) : Prop :=
2 ≤ p ∧ ∀ d, d ∣ p → d = 1 ∨ d = p
With these at the head of a file that imports nothing, Lean answers five questions.
#check (Fin)
#check (Vec Nat)
#check (Prime)
#check Prime 8
#check Vec.cons 1 (Vec.cons 2 Vec.nil)
Fin : Nat → Type
Vec Nat : Nat → Type
Prime : Nat → Prop
Prime 8 : Prop
Vec.cons 1 (Vec.cons 2 Vec.nil) : Vec Nat (0 + 1 + 1)
Fin, Vec Nat and Prime are three families over Nat, each a function into a type of types (Type for data and Prop for propositions, Section 5.5). Prime 8 is as much a type as Prime 7; it has no element. The last answer is what changes for a type checker: the type that Lean computes for a list of two numbers is Vec Nat (0 + 1 + 1), with two additions in it that nobody has carried out, and whether this is the type Vec Nat 2 is a question about values. In the words of the Stanford Encyclopedia's entry on Martin-Löf's theory, "dependent types introduce computation in types".P. Dybjer and E. Palmgren, "Intuitionistic Type Theory", Stanford Encyclopedia of Philosophy (substantive revision of 23 September 2024), Section 2.4, on the type of \(n\)-tuples defined by recursion on \(n\). That is the first cost, taken up in Section 5.3. The gain comes first.
Π and Σ
A type family \(B(x)\) over \(A\) supplies a separate fiber above each individual \(x\), whereas mathematical statements speak across all fibers at once ("for every \(x\)" or "for some \(x\)"). Geometrically, there are two natural ways to assemble a whole family of fibers into a single type. First, a dependent function chooses one element in every fiber simultaneously, tracing a cross-section across the base: over the four points of Fin 4, where Fin (n + 1) has fibers of sizes 1, 2, 3, and 4, choosing one element per column makes four independent choices, giving 1 × 2 × 3 × 4 = 24 functions. Second, a dependent pair picks a single base point \(x\) together with one element from its fiber \(B(x)\), pooling all columns into one tagged collection: there are 1 + 2 + 3 + 4 = 10 such pairs. Because the first operation multiplies fiber sizes and the second adds them, type theory names them \(\Pi\) and \(\Sigma\).
Definition 5.2.1 (Π and Σ). Let \(B(x)\) be a family of types over \(A\). The elements of the dependent function type \(\Pi (x : A).\, B(x)\) are the functions \(\lambda x.\, b\) in which \(b\) has the type \(B(x)\) for a variable \(x\) of type \(A\). Applied to \(a : A\), such a function \(f\) gives \(f\,a : B(a)\), and \((\lambda x.\, b)\,a \equiv b[x := a]\). The elements of the dependent pair type \(\Sigma (x : A).\, B(x)\) are the pairs \((a, b)\) with \(a : A\) and \(b : B(a)\). A pair \(c\) has the projections \(\pi_1\,c : A\) and \(\pi_2\,c : B(\pi_1\,c)\), and \(\pi_1\,(a, b) \equiv a\), \(\pi_2\,(a, b) \equiv b\). When \(B\) does not mention \(x\), the two types are the function type \(A \to B\) and the product \(A \times B\).Martin-Löf's names are "cartesian product of a family of sets" and "disjoint union of a family of sets", with the signs ∏ and ∑ of ordinary mathematics (Intuitionistic Type Theory, pp. 13 and 20). He gives one rule of elimination for Σ and defines the two projections from it (pp. 20 to 24); the form with projections is the one that Lean has for its structures (Section 5.8).
Here \(\equiv\) denotes equality by computation (Section 5.4): notice how \(\pi_2\,c : B(\pi_1\,c)\) mentions the first projection inside its type, and when \(c\) is a concrete pair \((a, b)\), the rule \(\pi_1\,(a, b) \equiv a\) simplifies \(B(\pi_1\,(a, b))\) to \(B(a)\), matching the type of \(b\).
The arithmetic of fiber sizes holds for every finite family, and shows immediately what a single empty fiber does to each construction: that contrast is what separates "for all" from "exists".
Proposition 5.2.2 (the two counts). Let \(A\) be a finite set and \(B(x)\) a finite set for each \(x \in A\). The functions that send every \(x\) to an element of \(B(x)\) number \(\prod_{x \in A} \lvert B(x) \rvert\), and the pairs \((x, b)\) with \(b \in B(x)\) number \(\sum_{x \in A} \lvert B(x) \rvert\). One empty \(B(x)\) leaves no function and takes away no pair; over an empty \(A\) there is one function and no pair.
Now let each fiber \(P(x)\) be a proposition. A family of propositions over \(A\) is a predicate on \(A\), and \(\Pi\) and \(\Sigma\) become the universal and existential quantifiers. An inhabitant of \(\Pi (x : A).\, P(x)\) is a function producing a proof of \(P(x)\) for every \(x : A\), which is a proof of \(\forall x.\, P(x)\). An inhabitant of \(\Sigma (x : A).\, P(x)\) is a specific witness \(a : A\) paired with a proof of \(P(a)\), proving \(\exists x.\, P(x)\). An empty fiber over \(x\) is a counterexample where \(P(x)\) is false: one zero factor collapses the product \(\prod\) to zero (one counterexample refutes \(\forall\)), while contributing zero to the sum \(\sum\) without erasing valid witnesses elsewhere. Over \(d\) from 1 to 12, "every \(d\) divides 12" has zero proofs because non-divisors have empty fibers, whereas "some \(d\) divides 12" has six elements, one pair for each divisor. Euclid's theorem, that above every number there is a prime, becomes the nested type
\[\Pi (n : \mathbb{N}).\ \Sigma (p : \mathbb{N}).\ (n < p) \times \mathrm{Prime}(p),\]and an element of it is, in the encyclopedia's words, "a program which given any number produces a larger prime together with proofs that it indeed is larger and indeed is prime".Dybjer and Palmgren, Section 2.5. Section 4.5 runs one.
Both types have their place in the picture of fibers, by which the book of the Univalent Foundations Program, which reads type theory as a theory of spaces, thinks of a family.
Topologically, the transportation lemma can be viewed as a “path lifting” operation in a fibration. We think of a type family P : A → U as a fibration with base space A, with P(x) being the fiber over x, and with ∑(x:A) P(x) being the total space of the fibration, with first projection ∑(x:A)(P(x)) → A. The defining property of a fibration is that given a path p : x = y in the base space A and a point u : P(x) in the fiber over x, we may lift the path p to a path in the total space starting at u (and this lifting can be done continuously). The point p∗(u) can be thought of as the other endpoint of this lifted path. We can also define the path itself in type theory:
—The Univalent Foundations Program, Homotopy type theory (2013). Homotopy type theory: univalent foundations of mathematics, Institute for Advanced Study, 2013 (the book online), Section 2.3, p. 72. U is a universe (Section 5.5 of this part), ∑ is Σ, and the transportation lemma is part (i) of Proposition 5.4.2. The phrase on sections is on p. 73.
A dependent function is then, the book goes on, "a section of the fibration". Reading "exists" as a pair has a consequence that set theory does not have.
Theorem 5.2.3 (choice, for pairs). In a type theory with the two types of Definition 5.2.1, let \(B(x)\) be a family of types over \(A\) and \(C(x, y)\) a family over the pairs of an \(x : A\) and a \(y : B(x)\). The type
\[\Bigl(\Pi (x : A).\ \Sigma (y : B(x)).\ C(x, y)\Bigr) \;\to\; \Sigma \bigl(f : \Pi (x : A).\, B(x)\bigr).\ \Pi (x : A).\ C(x, f\,x)\]has the element \(\lambda g.\, \bigl(\lambda x.\, \pi_1\,(g\,x),\ \lambda x.\, \pi_2\,(g\,x)\bigr)\).
Proof. The pair \(g\,x\) has the projections \(\pi_1\,(g\,x) : B(x)\) and \(\pi_2\,(g\,x) : C(x, \pi_1\,(g\,x))\). With \(f = \lambda x.\, \pi_1\,(g\,x)\) we have \(f\,x \equiv \pi_1\,(g\,x)\), so the second projection has the type \(C(x, f\,x)\). ∎
No arbitrary selection takes place here: an inhabitant \(g\) of the premise is already an explicit function returning a pair \((y, \text{proof})\) for each \(x\), so splitting \(g\) into \(x \mapsto \pi_1\,(g\,x)\) and \(x \mapsto \pi_2\,(g\,x)\) simply unzips a function returning pairs into a pair of functions. Martin-Löf counted that transparency in favor of reading existence as \(\Sigma\), "the only known way of interpreting intuitionistic logic so that the axiom of choice becomes valid".Intuitionistic Type Theory, p. 4; the theorem is proved there on pp. 27 to 28. Dybjer and Palmgren, Section 3.8, "The Axiom of Choice is a Theorem", call it "an axiom of choice for types", types not being sets: the real numbers, their example, are Cauchy sequences up to an equivalence. That unzipping works only because \(\Sigma\) pairs allow projections. Stated instead with the weak ∃ of ordinary mathematics and Lean's Prop, whose proofs hide their witnesses behind proof irrelevance, extracting a choice function cannot be done by computation and requires Lean's Classical.choice axiom (Section 7.2).
Lean has both readings of "exists". The pair type, written (x : A) × B x, and the subtype { x // P x }, of the x that come with a proof of P x, are data and have projections; for them Lean accepts the term of Theorem 5.2.3 and reports no axiom under it. ∃ x, P x is a proposition, and a proof of it cannot be taken apart into data (Section 6.5 has the rule): the rule for \(\Sigma\), Martin-Löf had noted, is the stronger one, "since we take into consideration also proofs (constructions), which is not possible within the language of first order predicate logic".Intuitionistic Type Theory, pp. 22 to 23. So Euclid's theorem has two statements in Lean.
#check ∀ n : Nat, ∃ p, n < p ∧ Prime p
#check (n : Nat) → { p // n < p ∧ Prime p }
∀ (n : Nat), ∃ p, n < p ∧ Prime p : Prop
(n : Nat) → { p // n < p ∧ Prime p } : Type
Lean prints a function type with ∀ when it ends in a proposition; it is one type former.
What Lean decides when it accepts a proof of such a statement has not been said.
Judgments
What a checker does with a computation inside a type shows on two more lines in the same file.
example {α : Type u} {n : Nat} (v : Vec α (n + 0)) : Vec α n := v
example {α : Type u} {n : Nat} (v : Vec α (0 + n)) : Vec α n := v
DependentTypes.lean:21:64: error: Type mismatch
v
has type
Vec α (0 + n)
but is expected to have type
Vec α n
Lean accepts the first line silently and rejects the second, yet neither verdict proves or refutes a mathematical proposition. Lean was asked whether the term v already has type Vec α n, and it answered by running its computation rules: because natural-number addition in Lean recurses on its second argument, n + 0 reduces immediately to n, whereas 0 + n gets stuck waiting to see whether the variable n is zero or a successor.
To understand why n + 0 and 0 + n behave differently here, dependent type theory separates internal propositions from external judgments. Propositions such as Prime 7 or 0 + n = n are types inside the language: they can be negated, assumed as hypotheses, or quantified over, and proving one means exhibiting a term of that type. Judgments sit one level outside: they are the checker's own bookkeeping verdicts that an expression is a well-formed type, that a term a has type A, or that two expressions compute to the same form. Unlike a proposition, a judgment cannot be negated or assumed as a hypothesis; when Lean rejects the second line, it has not proved that 0 + n differs from n, only reported that mechanical reduction alone could not make Vec α (0 + n) and Vec α n coincide.
Definition 5.3.1 (judgments). A context is a list \(\Gamma = x_1 : A_1, \dots, x_n : A_n\) of variables with their types, in which each \(A_i\) may mention the variables before it. A dependent type theory derives judgments of four forms,
\[\Gamma \vdash A\ \ \mathsf{type}, \qquad \Gamma \vdash a : A, \qquad \Gamma \vdash A \equiv B, \qquad \Gamma \vdash a \equiv b : A,\]and the conversion rule ties the second form to the third:
\[\frac{\Gamma \vdash a : A \qquad \Gamma \vdash A \equiv B}{\Gamma \vdash a : B}.\]A family of types over \(A\) is a judgment of the first form in the context \(x : A\).The four forms are Martin-Löf's, who reads them, in the order of the display, "A is a set", "a is an element of the set A", "A and B are equal sets" and "a and b are equal elements of the set A" (Intuitionistic Type Theory, p. 3). The notation and the conversion rule are as in Dybjer and Palmgren, Sections 3.2 and 3.3, who call the rule "crucial for computation in types". Unlike the contexts of Section 2.2 these are ordered, because a type may mention an earlier variable, as the type of v mentions n. The first of the two lines is accepted by one use of the conversion rule.
The types themselves are given by rules, and the rules of every type come in one pattern of four.
Every type comes with four rulesa row to a rule, in the order in which a type is given; in ink, the rule for the function type, for the natural numbers and for equality, as Lean writes it
5.3formation when the type exists
Says from which types and families the new type is formed. For a family this is the place where a value enters a type.
(x : A) → B x from a type A and a family B over it
Nat from nothing
a = b from two terms a, b of one type
5.3introduction how its elements are made
Says what the canonical elements of the type are, and so what the type means. For an inductive type these are its constructors.
fun x => b
Nat.zero Nat.succ n
Eq.refl a : a = a
5.6elimination how they are used
Says how a function out of the type is defined: by its value on each canonical element. Application for a function type; for an inductive type the recursor, which the kernel computes from the constructors.
f a : B a
Nat.rec z s n : motive n
Eq.rec m h : motive b h written h ▸ m
5.4computation Martin-Löf: equality
Says what an elimination does on an element that an introduction has just made. These rules are what definitional equality is made of, and they are the detour removals of Section 3.
(fun x => b) a ≡ b with a put for x
Nat.rec z s (Nat.succ n) ≡ s n (Nat.rec z s n)
Eq.rec m (Eq.refl a) ≡ m
green: the rules by which the kernel computes
Martin-Löf's notes state the pattern before any particular type.
We now start to give the rules for the different symbols we use. We will follow a common pattern in giving them. For each operation we have four rules: set formation, introduction, elimination, equality. The formation rule says that we can form a certain set (proposition) from certain other sets (propositions) or families of sets (propositional functions). The introduction rules say what are the canonical elements (and equal canonical elements) of the set, thus giving its meaning. The elimination rule shows how we may define functions on the set defined by the introduction rules. The equality rules relate the introduction and elimination rules by showing how a function defined by means of the elimination rule operates on the canonical elements of the set which are generated by the introduction rules.
—Per Martin-Löf, Intuitionistic Type Theory (1984). "General remarks on the rules", p. 13 of the retypeset edition. The fourth rule, which the notes call equality, is now usually called the computation rule.
The notes go on to say where the pattern comes from: "introduction and elimination rules are like those of Gentzen, and the equality rules correspond to the reduction rules of Prawitz". These are the rules of proof of Section 3 and the removal of its detours, extended from the connectives to every type.
Three consequences depart sharply from set theory and first-order predicate logic. First, typing is not set membership: "in ordinary set theory, a∈b and a = b are propositions, while they are judgements here".Intuitionistic Type Theory, p. 3. One cannot meaningfully ask whether 2 belongs to 3, because a term comes into being already carrying its type rather than floating untyped across all sets. Second, grammar does not precede proof. In first-order logic, syntax checks whether a string is a well-formed formula before any proof begins; in dependent type theory, as the encyclopedia puts it, "the well-formed formulas (types) have to be generated simultaneously with the provably true formulas (inhabited types)", because whether an expression is even well-typed can depend on evaluating a computation or supplying a proof (such as a proof that an index lies in bounds).
Third, checking a given candidate term is fundamentally easier than finding one. Handed concrete expressions \(a\), \(b\), and \(A\), a machine can decide \(a : A\) and \(a \equiv b : A\) mechanically by reducing terms; deciding whether \(A\) has any inhabitant at all (whether the proposition \(A\) is provable) is undecidable. Martin-Löf drew that boundary in Kantian terms: "the logic of analytic judgments, that is, the logic for deriving judgments of the two analytic forms, is complete and decidable, whereas the logic of synthetic judgments is incomplete and undecidable, as was shown by Gödel".Dybjer and Palmgren, Section 3.2; the sentence is quoted in its Remark 3.1 from a paper of Martin-Löf's of 1994, p. 97, with the comment that the decidability "hinges on the metamathematical properties of type theory: strong normalization and decidable type checking". Interactive theorem proving rests on this exact split: finding a proof term requires open-ended search that need not be trusted, whereas checking that a candidate term has the claimed type is a finite computation verified by the kernel. For Martin-Löf's intensional theory (Remark 5.4.4), decidability of type-checking is a theorem; how far it holds for Lean's logic is the subject of Section 6.6.
What is checked has two layers. In \(t : A\) the term \(t\) is a proof object, and the derivation of the judgment \(t : A\) is a demonstration.The two words are Martin-Löf's, as reported by P. Schroeder-Heister, "Proof-Theoretic Semantics", Stanford Encyclopedia of Philosophy (substantive revision of 18 August 2023), Section 2.2.3: "Unlike proof objects, demonstrations have epistemic significance". A library stores proof objects. The demonstration is the run of a kernel, the small program that checks them (Section 6), and nobody stores it: whoever doubts it runs a kernel again (Section 13).
Meanwhile, the second vector example remains rejected even though 0 + n equals n for every natural number n: establishing 0 + n = n requires induction on n, and induction proves a proposition rather than a definitional judgment.
Equality as a type
An equation is a proposition, so it is a type: \(a = b\), whose elements are the proofs that \(a\) equals \(b\). Lean declares it as a family of types with one constructor, and everything else about equality is computed from that.
type inductive Eq.{u_1} : (α : Sort u_1) → α → α → Prop
Eq.refl : (α : Sort u_1) → (a : α) → a = a its one constructor
Eq.rec (α : Sort u_1) (a : α) (motive : (a_1 : α) → a = a_1 → Sort u)
(refl : motive a (Eq.refl a)) (a_1 : α) (t : a = a_1) : motive a_1 t its eliminator
uses four terms of Lean's core library, each one application of Eq.rec
family the element the proof carried to
transport x ↦ motive x m : motive a h : a = b h ▸ m : motive b
symmetry x ↦ x = a rfl : a = a h : a = b h ▸ rfl : b = a
transitivity x ↦ a = x h₁ : a = b h₂ : b = c h₂ ▸ h₁ : a = c
congruence x ↦ f a₁ = f x rfl : f a₁ = f a₁ h : a₁ = a₂ h ▸ rfl : f a₁ = f a₂
proofs one family, x ↦ Vec α x, a vector v, four proofs: what the kernel of this page makes of each
Eq.refl m : m = m the ends are one point v arrives as v
Nat.add_comm 2 3 : 2 + 3 = 3 + 2 one point: both are 5 v arrives as v
Nat.zero_add n : 0 + n = n two points ▨ v stays h ▸ v
a hypothesis h : m = n two points ▨ v stays h ▸ v
(the live figure needs JavaScript; it loads a kernel of about 0.2 MB and 0.3 MB of exported declarations, and checks
the 145 declarations first)
Every use of an equation reduces to a single geometric move: a proof of \(a = b\) transports any element in the fiber over \(a\) across to the fiber over \(b\). Whether that transported element simplifies cleanly to itself or keeps an explicit transport wrapper depends on how the two endpoints relate computationally, splitting equality into two distinct notions.
In Lean's prelude, propositional equality is declared in three lines as an inductive family with a single constructor.
inductive Eq : α → α → Prop where
/-- `Eq.refl a : a = a` is reflexivity, the unique constructor of the
equality type. See also `rfl`, which is usually used instead. -/
| refl (a : α) : Eq a a
Those three lines supply formation and introduction; all the power of equality sits in its elimination rule. Because Eq.refl a : a = a is the sole constructor building an equality proof, the only canonical way a = x can hold is when x is a itself and the proof is reflexivity. Consequently, to establish a target family \(C(x, p)\) for every endpoint \(x\) equal to \(a\) and every proof \(p : a = x\), it suffices to supply a value for the single base case where \(x\) is \(a\) and \(p\) is \(\mathsf{refl}_a\).
Definition 5.4.1 (the identity type). For terms \(a\) and \(b\) of a type \(A\) there is a type \(a = b\). Its constructor is \(\mathsf{refl}_a : a = a\). Its eliminator takes a family \(C(x, p)\) of types over the pairs of an \(x : A\) and a \(p : a = x\), called the motive, and an element \(d : C(a, \mathsf{refl}_a)\); for every \(b : A\) and \(h : a = b\) it returns an element \(\mathsf{J}(d, h) : C(b, h)\), and \(\mathsf{J}(d, \mathsf{refl}_a) \equiv d\).The eliminator, known as J, is Martin-Löf's, of 1975. The form given here, with \(a\) fixed, is C. Paulin-Mohring's of 1993, and the two are equivalent (Dybjer and Palmgren, Section 4.2; the notes to chapter 1 of Homotopy type theory, where the rule is called path induction). Lean's Eq.rec is this form: its motive is printed (a_1 : α) → a = a_1 → Sort u_1.
As the Univalent Foundations book summarizes this induction principle, the identity types \(a = x\) are "freely generated by the elements of the form refl".Homotopy type theory, Section 1.12.1, p. 49. No extra axioms are needed for equality: transport, symmetry, transitivity, and congruence all fall out of \(\mathsf{J}\) simply by choosing different motives.
Proposition 5.4.2 (transport and what follows from it). Let \(h : a = b\) in a type \(A\). (i) For a family \(P\) of types over \(A\) there is a function \(P(a) \to P(b)\), written \(u \mapsto h \triangleright u\), with \(\mathsf{refl}_a \triangleright u \equiv u\). (ii) \(b = a\). (iii) If \(k : b = c\), then \(a = c\). (iv) If \(f : A \to B\), then \(f\,a = f\,b\).
Proof. Each is one use of the eliminator. (i) Take the motive \(C(x, p) = P(x)\) and \(d = u\). (ii) In the family \(P(x) = (x = a)\), carry \(\mathsf{refl}_a\) along \(h\). (iii) In the family \(P(x) = (a = x)\), carry \(h\) along \(k\). (iv) In the family \(P(x) = (f\,a = f\,x)\), carry \(\mathsf{refl}_{f\,a}\) along \(h\). ∎
Lean writes \(h \triangleright u\) as h ▸ u, and its library proves (ii) to (iv) by these three terms, with no axiom under them.
Type theory now has two notions of equality where ordinary mathematics speaks of only one: a silent computational check run automatically by the type checker, and an explicit proposition proved as a theorem.
Definition 5.4.3 (the two equalities). Definitional equality is the judgment \(a \equiv b\) of Definition 5.3.1, generated by the computation rules of the type formers and the unfolding of definitions, applied anywhere inside a term (Section 6.3 lists Lean's rules). Propositional equality is the type \(a = b\).
By the conversion rule \(\mathsf{refl}_a\) has the type \(a = b\) whenever \(a \equiv b\). So rfl, which is Eq.refl with its argument left for Lean to find, proves the equations whose two sides are definitionally equal, and those only.
example (n : Nat) : n + 0 = n := rfl
example : 2 + 3 = 3 + 2 := rfl
example (n : Nat) : 0 + n = n := rfl
example (n : Nat) : 0 + n = n := Nat.zero_add n
DependentTypes.lean:25:33: error: Type mismatch
rfl
has type
?m.9 = ?m.9
but is expected to have type
0 + n = n
In the second line, 2 + 3 and 3 + 2 both reduce to 5, so Eq.refl 5 inhabits 2 + 3 = 3 + 2 automatically via the conversion rule. The third line states a true equation that the fourth line proves by induction on n, yet rfl fails on it: because 0 + n cannot reduce while n is a variable, 0 + n and n are not definitionally equal, and Lean cannot find a single expression ?m.9 definitionally equal to both sides.
The book of Section 5.2 marks the same line, with \(n + 1\) and \(1 + n\).
We emphasize that despite having some unfamiliar features, propositional equality is the equality of mathematics in homotopy type theory. This distinction does not belong to judgmental equality, which is rather a metatheoretic feature of the rules of type theory. For instance, the associativity of addition for natural numbers proven in §1.9 is a propositional equality, not a judgmental one. The same is true of the commutative law (Exercise 1.16). Even the very simple commutativity n + 1 = 1 + n is not a judgmental equality for a generic n (though it is judgmental for any specific n, e.g. 3 + 1 ≡ 1 + 3, since both are judgmentally equal to 4 by the computation rules defining +). We can only prove such facts by using the identity type, since we can only apply the induction principle for N with a type as output (not a judgment).
—The Univalent Foundations Program, Homotopy type theory (2013). Remark 1.12.2, p. 54. Judgmental equality is the book's name for definitional equality. The unfamiliar features are those of Remark 5.4.4 of this part: in the book's theory an equation may have several proofs.
Its first sentence is the one to keep: the equality of mathematics is the type. The judgment is there for the checker, which has to apply the conversion rule without being handed a proof and so needs an equality that it can decide. A decidable relation cannot be the equality that theorems are about.
Two equalitiesa row to a question; left, the equality that the kernel decides by computing; right, the equality that is proved
- what it is
a judgment, a ≡ b The two terms compute to one. A judgment can only be made: it cannot be negated, assumed or quantified over.
5.4a type, a = b A proposition like any other, whose elements are its proofs. It can be assumed, negated, and proved by induction.
- who settles it
the kernel, by computing Both sides are reduced until they agree or stop. No proof is written, and none is kept.
5.4whoever writes a proof A term of the type, built from Eq.refl, the recursor and earlier theorems. The kernel checks the term; it does not find it.
- an instance
n + 0 ≡ n Addition recurses on its second argument, and 0 is a constructor: the first computation rule applies. In Lean rfl proves n + 0 = n.
5.40 + n = n The recursion waits on the variable n, and nothing reduces. The equation is proved by induction on n: the theorem Nat.zero_add. With rfl Lean answers: Type mismatch.
- where it acts
the conversion rule A term of type A is a term of type B when A ≡ B. Nothing is written in the term: a vector of length n + 0 is a vector of length n.
5.4transport, h ▸ e A term of type P a becomes a term of type P b along a proof h : a = b, and the proof stays in the term: Nat.zero_add n ▸ v.
Martin-Löf's notes and the book just quoted differ from Lean over these two equalities, and Section 6.3 builds on Lean's choice.
Remark 5.4.4 (one theory or several, one proof or many). How definitional and propositional equality interact is a design choice, and "Martin-Löf type theory" covers both main branches. In Martin-Löf's 1984 notes, a reflection rule turns any proof \(h : a = b\) directly into the definitional judgment \(a \equiv b\); such a theory is called extensional, and a standard textbook warns that under reflection "judgemental equality will no longer be decidable", since checking types can now demand arbitrary mathematical proofs.The rule: Intuitionistic Type Theory, p. 32. The warning: B. Nordström, K. Petersson and J. M. Smith, Programming in Martin-Löf's type theory: an introduction, Oxford University Press, 1990, chapter 8, p. 57. The model: M. Hofmann and T. Streicher, "The groupoid interpretation of type theory", in G. Sambin and J. Smith (eds), Twenty-five years of constructive type theory, Oxford, 1998, pp. 83 to 111. The book of Section 5.2 puts the freedom in a sentence (Section 1.12, pp. 47 to 48): "Just as there can be more than one path between two points of a space, there can be more than one witness that two objects are equal". Lean, like Definition 5.4.1, is intensional: a proved equation changes a term's type only through an explicit transport h ▸ v, keeping type-checking separate from theorem-proving. Whether \(a = b\) can have multiple distinct proofs is an independent second choice left open by \(\mathsf{J}\) alone. In 1994 Hofmann and Streicher constructed a groupoid model in which types carry distinct equality paths, the foundation of homotopy type theory. Lean settles that question the opposite way: because Eq lands in Prop and Lean enforces proof irrelevance on Prop (Section 6.3), any two proofs p q : a = b satisfy p = q by rfl. Better still, whenever a and b reduce to the same expression (as in 2 + 3 = 3 + 2), any proof h : a = b is definitionally equal to Eq.refl, so the kernel reduces h ▸ v straight to v even if h was proved by induction rather than rfl; only when a and b fail to compute to a common form (as in 0 + n = n) must h ▸ v remain wrapped. A path, in Lean, is one arrow.
The second cost is the type of types that every family maps into, Type or Prop, used since Section 5.1 and never examined.
Universes
"A universe is a type whose elements are types",Homotopy type theory, Section 1.3, p. 24. and Fin : Nat → Type could not be written without one. With a universe a family over \(A\) is a function from \(A\) into it, and the judgment that \(A\) is a type becomes one of the second form, \(A : \mathcal{U}\). What the type of a universe is, and which function types may be formed across universes, are the rules in which type theories differ, and they can be set like switches.
switches a product Πx:A. B may be formed when the switch named by the sorts of A and of B is on
(*,*) terms on terms (□,*) terms on types (□,□) types on types (*,□) types on terms all on
□ : △ a universe above: on □ : □ Type in Type: off (λC with universes: Lean's corner)
table 32 terms, each checked under the switches in force (*,*) (□,*) (□,□) (*,□) □:△ □:□
A : * ⊢ λx:A. x ●
λα:*. λx:α. x ● ●
λα:*. α → α ● ●
Nat : *, Vec : Nat → * ⊢ λn:Nat. λv:(Vec n). v ● ●
λα:*. λx:α. λy:α. ΠP:(α → *). P x → P y ● ● ● ●
□ ●
(λA:□. A) □ refused ● ○
● a rule the term needs, on ○ a rule it needs, off 30 of the 32 have a type
cursor on the fourth of these rows, at (*,□): the rule forms Nat → *, found in the declaration of Vec
Γ ⊢ Nat : * Γ, x : Nat ⊢ * : □
───────────────────────────────────── (*, □)
Γ ⊢ Nat → * : □
systems how many of the 32 have a type: λ→ 4 λ2 10 λC 23 λC with universes 30 with Type in Type 32
Lean Lean 4.35.0 itself accepts 30 with * read as Prop and 29 with * read as Type (recorded)
(the live figure needs JavaScript and loads about 0.05 MB)
The calculus of constructions, the corner of the cube of Section 2.6 with every way for a term or a type to depend on a term or a type, gives a type to 23 of the 32 terms. Lean's rule adds a universe above every universe, and seven of the other nine were waiting for nothing else. The last two need the type of all types to be one of the types.
Lean's universe rule is compact, with a single exception that sets Prop apart from data: whereas a function returning data in Type must sit at least as high in the hierarchy as both its input and output types, a function returning a proposition in Prop (∀ x : A, P x) stays inside Prop no matter how high the domain A sits.
Definition 5.5.1 (Lean's universes). Lean has a universe Sort u for every level \(u = 0, 1, 2, \dots\); it writes Prop for Sort 0 and Type u for Sort (u + 1). Each is an element of the next, Sort u : Sort (u + 1), so that Prop : Type and Type : Type 1. If A : Sort u and B x : Sort v for x : A, then
Lean's logic has more in it (Sections 5.6 and 6): what the table calls Lean's corner is this rule and no more. Lean states the rule itself.
#check Prop
#check Type
#check fun (α : Sort u) (β : α → Sort v) => (a : α) → β a
#check Sort (imax u 0)
#check Sort (imax u (v + 1))
Prop : Type
Type : Type 1
fun α β => (a : α) → β a : (α : Sort u) → (α → Sort v) → Sort (imax u v)
Prop : Type
Sort (max u (v + 1)) : Type (max u (v + 1))
The third answer is the rule, and the last two are its two cases. The page's checker has it in one line, for sorts numbered from \(\ast\) at level 0.
axiom: function (s) { var l = levelOf(s); return l < 0 ? null : sortAt(l + 1); },
rule: function (s1, s2) {
var a = levelOf(s1), b = levelOf(s2);
// ...
return b === 0 ? '*' : a === 0 ? s2 : sortAt(Math.max(a, b));
}
On all 240,466 closed terms, those without free variables, of at most nine nodes that can be written with Prop and Type, this checker and Lean's kernel give the same verdict: 12,100 have a type, equal in both, and the others are refused by both.
A function type into a Type lies at the larger of the two levels, so nothing in Type u quantifies over Type u: the tower is stratified. A function type into Prop stays in Prop whatever it quantifies over (Section 5.7).
What the other switch costs is a proof of everything.
term eight abbreviations and a closed term of 2,039 nodes (Hurkens 1995), built in the page
⊥ ≡ Πp:*. p ¬ φ ≡ φ → ⊥ ℘ S ≡ S → *
U ≡ ΠX:□. (℘ (℘ X) → X) → ℘ (℘ X) U quantifies over every X in □
σ s ≡ [s U] (λt:(℘ (℘ U)). τ t) [s U]: s, of type U, applied to U itself
τ, Δ, Ω and the term are made of these; the term's type is ⊥, that is Πp:*. p: a proof of every proposition
systems ■ a type ▨ refused
■ λC with Type in Type ■ λ* ■ λU⁻ ■ λU the type Πp:*. p, in 6,769 judgments and 22 β-steps
▨ λHOL ▨ λC refused at U
▨ λC with universes, the corner Lean stands in refused at σ x: U has type △ where □ is expected
▨ Lean 4.35.0 itself (recorded) refused; of its own definition of σ it says
The argument U has type Type 1 of sort `Type 2` but is expected to have type Type of sort `Type 1`
reduced the paper proves that the reduction never ends; the page runs 10,000 β-steps, the leftmost outermost redex first
nodes after each step: 2,039, 2,173, 2,393, 2,771, 3,041, 2,573, ...
after 1,000 steps 14,985 (the largest so far 21,129); after 10,000 steps 46,017 (the largest 65,703)
in the paper's order of steps the term has its first form again at steps 84, 284, 574, 954, ...:
at the first of them 1,196 nodes larger than at the start, at each later one 1,040 larger than at the one before
(the live figure needs JavaScript and loads about 0.05 MB)
With the type of all types among the types, a term of 2,039 nodes is a proof of every proposition. The paper it is taken from proves that its reduction never ends; run in the page for 10,000 steps, it has 46,017 nodes at the last, and in the paper's own order of steps it comes back every three big steps to a term of the form it began in, larger each time. Lean has no switch for this: its kernel refuses the term at the application s U, where U has the type Type 1 and an element of Type is expected.
Reaching inconsistency does not even require a universe that is literally an element of itself: stacking two impredicative universes one inside the other (\(\ast : \square : \triangle\), where both \(\ast\) and \(\square\) remain at their own level when quantifying over higher levels) already suffices. Lean escapes because only Prop (Sort 0) has that impredicative exemption, while Type (Sort 1) and above are strictly stratified.
Theorem 5.5.2 (Girard's paradox, in the form of Hurkens). Let \(\lambda U^{-}\) be the pure type system (Definition 2.6.1) with the sorts \(\ast\), \(\square\) and \(\triangle\), the axioms \(\ast : \square\) and \(\square : \triangle\), and the rules \((\ast, \ast)\), \((\square, \square)\), \((\square, \ast)\) and \((\triangle, \square)\). It has a closed term of type \(\bot = \Pi (p : \ast).\, p\), and no closed term of that type is normal, that is, has nothing left to reduce. So every proposition of \(\lambda U^{-}\) has a proof, and no reduction of the term ends. The same term has the type \(\bot\) in the calculus of constructions with the axiom \(\square : \square\) added and, with \(\ast\) written for every \(\square\), in the system of Theorem 2.6.3 whose one axiom is \(\ast : \ast\).A. J. C. Hurkens, "A simplification of Girard's paradox", TLCA '95, Lecture Notes in Computer Science 902, Springer, 1995, pp. 266 to 278; the term is on p. 269. J.-Y. Girard's thesis (Interprétation fonctionnelle et élimination des coupures de l'arithmétique d'ordre supérieur, Université Paris VII, June 1972) proves a system U contradictory and remarks that Martin-Löf's theory of 1971 falls with it; the abstract of Hurkens' paper credits a paradox in \(\lambda U^{-}\), which has one rule fewer than U, to Coquand in 1991. Of the calculus of constructions with the rule that \(\ast\) has the type \(\ast\), its authors write that "the Curry-Howard paradigm seems to be lost forever then, since all propositions become provable" (T. Coquand and G. Huet, "The calculus of constructions", Information and Computation 76, 1988, pp. 95 to 120, at p. 117).
The idea of the proof. Because types forbid untyped self-application \(x\,x\), Hurkens adapts not Russell's paradox directly, but Burali-Forti's paradox of the ordinal of all ordinals stripped down to pure logic. Suppose we can construct a type \(U\) together with an unpacking map \(\sigma\) sending each element of \(U\) to a set of elements of \(U\) (its "predecessors") and a packing map \(\tau\) sending any set \(X\) back into \(U\) so that \(\sigma(\tau X) = \{\tau(\sigma x) : x \in X\}\). Call a set \(X\) inductive if it contains every \(x\) whose predecessors all lie in \(X\), and call \(x\) well-founded if \(x\) belongs to every inductive set. Define \(\Omega = \tau\{w : w \text{ is well-founded}\}\), whose predecessors are the elements \(\tau(\sigma w)\) for well-founded \(w\). On one hand, \(\Omega\) is well-founded: for any inductive set \(X\), the pullback \(\{y : \tau(\sigma y) \in X\}\) is also inductive, so it contains every well-founded \(w\), meaning all predecessors \(\tau(\sigma w)\) of \(\Omega\) lie in \(X\), hence \(\Omega \in X\). On the other hand, \(\Omega\) cannot be well-founded: the set \(Y = \{y : \tau(\sigma y) \notin \sigma y\}\) of elements not preceded by their own image is inductive, so a well-founded \(\Omega\) would have to belong to \(Y\), yet \(\Omega\) being well-founded simultaneously puts \(\tau(\sigma\Omega)\) among \(\Omega\)'s predecessors \(\sigma\Omega\). In Hurkens' words, "we prove and disprove that a certain object in U is well-founded". Hurkens' closed term builds \(U\), \(\sigma\), and \(\tau\) using double power sets \(\mathcal{P}(\mathcal{P}(X))\), which requires \(U : \square\) to quantify over all \(X : \square\); in a stratified tower, that quantification pushes \(U\) one universe higher, blocking the self-application s U.
Why there can be no type of all types was said most plainly by Martin-Löf, whose first theory, of 1971, had one and was refuted by Girard the next year: "because we are not able to exhibit once and for all all possible set forming operations".Intuitionistic Type Theory, pp. 47 to 48, of a set of all sets. The comparison with Grothendieck is from Dybjer and Palmgren, Section 3.6, who add that the existence of a Grothendieck universe cannot be proved from the usual axioms of set theory. A universe is closed under the operations given so far and is itself a new one. Set theory has the same idea: a universe of types is "analogous to a Grothendieck universe in set theory", and like one it is an assumption on top of the theory under it (Section 6.6).
And why had anyone wanted one? Thierry Coquand lists what Martin-Löf was after.
Martin-Löf’s introduction of a type of all types comes from the identification of the concept of propositions and types, suggested by the work of Curry and Howard (Howard 1980). It is worth recalling here his three motivating points:
Russell’s definition of types as ranges of significance of propositional functions
the fact that one needs to quantify over all propositions (impredicativity of simple type theory)
identification of proposition and types
Given (1) and (2) we should have a type of propositions (as in simple type theory), and given (3) this should also be the type of all types. Girard’s paradox shows that one cannot have (1), (2) and (3) simultaneously. Martin-Löf’s choice was to take away (2), restricting type theory to be predicative (and, indeed, the notion of universe appeared first in type theory as a predicative version of the type of all types). The alternative choice of taking away (3) is discussed in Coquand 1986.
—Thierry Coquand, "Type Theory", Stanford Encyclopedia of Philosophy. The entry, substantive revision of 20 November 2026, Section 6. Coquand 1986 is "An analysis of Girard's paradox", Proceedings of LICS 1986, pp. 227 to 236.
Martin-Löf gave up the second point, and the tower of Definition 5.5.1 is what remained of the type of all types. Coquand and Huet gave up the third (Section 5.7). Lean takes both exits at once: a tower of types in which nothing quantifies over its own level, and under it one universe of propositions closed under every quantification. Whether the second exit is safe waits for Section 5.7: part of its price is paid where types are made.
Inductive types
Each type so far came with its four rules, and a kernel cannot take four more on trust with every new type. Lean has one mechanism that makes them all: the author states a type and its constructors, which are the introduction rules, and the kernel computes the eliminator, called the recursor, and its computation rules.
table eight inductive types, a row to a type: what the kernel of this page is given, and what it computes
type parameters indices constructors recursive fields rules the motive ends in
Nat 0 0 2 1 2 Sort u
List 1 0 2 1 2 Sort u_1
Vec 1 1 2 1 2 Sort u_1 declared in the page
Eq 2 1 1 0 1 Sort u
Acc 2 1 1 1 1 Sort u_1
And 2 0 1 0 1 Sort u
Exists 2 0 1 0 1 Prop
Bad 0 0 1 ▨ refused: not positive ▨ declared in the page
in hand given inductive Nat : Type Nat.zero : Nat Nat.succ : (n : Nat) → Nat
computed Nat.rec (motive : Nat → Sort u) (zero : motive Nat.zero)
(succ : (n : Nat) → motive n → motive (Nat.succ n)) (t : Nat) : motive t
rules Nat.rec motive zero succ Nat.zero → zero
Nat.rec motive zero succ (Nat.succ n) → succ n (Nat.rec motive zero succ n)
run Nat.rec (fun _ => Nat) 2 (fun _ ih => Nat.succ ih) 2 3 uses of a rule, then = 4
Bad Bad.mk : (Bad → False) → Bad the kernel, in Lean's words:
arg #1 of 'Bad.mk' has a non positive occurrence of the datatypes being declared
with the check off: Bad.rec is generated, a proof of False checks with no axiom under it, and run it is
the term it began as after 2 uses of a rule
(the live figure needs JavaScript; it loads a kernel of about 0.2 MB and 0.3 MB of exported declarations, and checks
the 145 declarations first)
From the two constructors of Nat, the kernel derives a single eliminator (printed in Section 2.5) together with two computation rules: to compute or prove a target family \(C(t)\) (the motive) for any t : Nat, a caller supplies one handler (a minor premise) for zero and one for succ that receives the predecessor's result as its induction hypothesis. That uniform recipe works across every inductive declaration and generates the only form of recursion built into Lean's kernel (Section 8.2).
Definition 5.6.1 (inductive type and recursor). An inductive type \(I\) is given by its type and a list of constructors: functions into \(I\), some of whose arguments may be of type \(I\) (the recursive arguments). Its recursor takes a motive, a family \(C\) of types over \(I\); for each constructor a minor premise, a function that takes the constructor's arguments and, for each recursive argument \(y\), an element of \(C(y)\), and returns an element of \(C\) over the element constructed; and a major premise \(t : I\). It returns an element of \(C(t)\). For each constructor there is a computation rule: the recursor at a constructed element reduces to the minor premise of its constructor, applied to the arguments and to the recursor at each recursive argument. In an inductive family, whose constructors build elements of different fibers, the motive takes the index too.
For the natural numbers, with \(\mathcal{U}\) a universe,
\[\mathsf{rec} \;:\; \Pi (C : \mathbb{N} \to \mathcal{U}).\ \ C(0) \to \bigl(\Pi (n : \mathbb{N}).\ C(n) \to C(n + 1)\bigr) \to \Pi (n : \mathbb{N}).\ C(n),\] \[\mathsf{rec}\;C\;z\;s\;0 \equiv z, \qquad \mathsf{rec}\;C\;z\;s\;(n + 1) \equiv s\;n\;(\mathsf{rec}\;C\;z\;s\;n).\]With a constant motive this is the recursor of Gödel's T (Section 2.5): definition by recursion. With a family of propositions for motive the same term is the principle of induction. "So recursion and induction turn out to be the same concept when propositions are interpreted as sets."Intuitionistic Type Theory, p. 39. The phrase on equality that follows is from Dybjer and Palmgren, Section 4.5. The types of this part are instances. The equality of Section 5.4 is an inductive family with the one constructor refl, "it is the type of proofs generated by the reflexivity axiom", and its eliminator is the recursor that the kernel computes from the four lines quoted there. Pairs, the existential quantifier, conjunction and falsity are declared in Lean's library so too. Of the type formers of this part only the function type and the universes are built into the kernel.
In which universes a motive may lie depends on the type, by a rule that belongs to Section 6.5, and what is shown of it here is Lean 4.35.0.
Allowing arbitrary function types as constructor arguments would smuggle untyped self-application back in through data constructors, so the kernel restricts where the type being defined may appear.
inductive Bad where
| mk : (Bad → False) → Bad
DependentTypes.lean:34:0: error: (kernel) arg #1 of 'Bad.mk' has a non positive occurrence of the datatypes being declared
Example 5.6.2 (a type that proves everything). Suppose Bad were accepted, with the recursor of Definition 5.6.1. Let notBad : Bad → False be the recursor with the constant motive False and the minor premise fun a => a (Bad.mk a), and let boom be notBad (Bad.mk notBad), a closed term of type False. By the computation rule boom reduces to (fun a => a (Bad.mk a)) notBad, and that, by a beta step (Section 1.1), to boom again. It is the term \(\Omega\) of Section 1 with a type, and analysis by cases was enough to write it. Lean has no switch that lets this through. Marked unsafe, the type and boom are accepted, and a theorem that mentions them is refused: (kernel) invalid declaration, it uses unsafe declaration 'boom'. ◁
The loop needed a constructor whose argument takes Bad as an input, and the kernel's condition forbids that place.
Definition 5.6.3 (strict positivity). An argument type of a constructor of \(I\) is strictly positive if it does not mention \(I\); or if it is \(I\) itself, applied to its parameters and to indices that do not mention \(I\); or if it is a function type \(\Pi (y : D).\, E\) in which \(D\) does not mention \(I\) and \(E\) is strictly positive. Lean's kernel accepts a declaration, unless it is marked unsafe, only if every argument type of every constructor is strictly positive.
The page's kernel, a separate implementation written with Lean's at hand, has the check as Lean's own has it, the three clauses as three tests.
function checkPositivity(t, cname, argIdx) {
t = tc.whnf(t);
if (!hasIndOcc(t)) return;
if (t.k === PI) {
if (hasIndOcc(t.a)) fail('arg #' + (argIdx + 1) + " of '" + cname + "' has a non positive occurrence of the datatypes being declared", { name: cname, arg: argIdx }, 'inductive-positivity');
return checkPositivity(instantiate1(t.b, localFor(t)), cname, argIdx);
}
if (isValidIndApp(t) < 0) fail('arg #' + (argIdx + 1) + " of '" + cname + "' contains a non valid occurrence of the datatypes being declared", { name: cname, arg: argIdx }, 'inductive-positivity');
}
Notice why Definition 5.6.3 demands strict positivity (forbidding I to the left of any arrow in a constructor argument) rather than ordinary positivity (allowing I to the left of an even number of arrows). In (D → Prop) → Prop, D sits to the left of two arrows, a monotone covariant position, yet D → Prop represents the power set \(\mathcal{P}(D)\) and (D → Prop) → Prop the double power set \(\mathcal{P}(\mathcal{P}(D))\); giving D a constructor ((D → Prop) → Prop) → D injects \(\mathcal{P}(\mathcal{P}(D))\) into D, violating Cantor's diagonal theorem. "This enables us to reproduce Cantorian-style paradoxes", observes the book of Section 5.2, noting that the contradiction relies on an impredicative Prop that stays at level 0 when quantifying over larger types.Homotopy type theory, Section 5.6, pp. 166 to 168. The counterexample is that of T. Coquand and C. Paulin, "Inductively defined types" (1990), as the book and V. Sjöberg ("Why must inductive types be strictly positive?", 2015) present it. Lean's Prop has that exact property (Section 5.7), and Lean rejects ((D → Prop) → Prop) → D with the same error. Strict positivity therefore guards more than program termination: without it, False itself would be provable.
Twice now Prop has been the exception, here and in the rule of Section 5.5.
Impredicativity
Why keep a universe whose members quantify over anything and stay in it, after the tower of Section 5.5? Here is a definition that analysis uses every day.
def union (C : (Nat → Prop) → Prop) : Nat → Prop :=
fun q => ∃ P, C P ∧ P q
#check @union
union : ((Nat → Prop) → Prop) → Nat → Prop
A set of natural numbers is a predicate Nat → Prop, and union C is the union of a class C of such sets. The definition quantifies over all sets of numbers in order to define a set of numbers, one of the things quantified over, and Lean accepts it without raising a level, because ∃ P, … is a proposition whatever P ranges over. With the rationals in place of Nat, this is the least upper bound of a bounded class of real numbers, a real number being the set of the rationals below it.T. Coquand, "Type theory", Section 3, where the least upper bound is written as this formula and called impredicative because a predicate is defined "by an existential quantification over all predicates".
A definition is impredicative when it quantifies over a totality to which the thing defined belongs, and a universe is called impredicative when such a definition can stay inside it.
Definition 5.7.1 (impredicative universe). A universe \(\mathcal{U}\) is impredicative if, for every type \(A\) of any universe and every family \(B(x)\) of types in \(\mathcal{U}\) over \(A\), the type \(\Pi (x : A).\, B(x)\) is in \(\mathcal{U}\).
By Definition 5.5.1 Lean's Prop is impredicative and no Type u is.
The objection to such definitions is as old as the theory of types, and one of its targets was a definition of the natural numbers.
We will call recurrent class every class of numbers that includes zero, and also includes n + 1 if it includes n. We will call inductive number every number which forms a part of all recurrent classes. Upon what condition will this latter definition, which plays an essential part in Whitehead's demonstration, be "predicative" and consequently acceptable? Following upon what has been said above, we must understand by all recurrent classes all those whose definition does not contain the notion of inductive number; otherwise we shall be involved in the vicious circle which engendered the antinomies. Now, Whitehead has not taken this precaution. Whitehead's argument is therefore vicious; it is the same that led to the antinomies. It was illegitimate when it gave untrue results, and it remains illegitimate when it leads by chance to a true result.
—Henri Poincaré, Science and method, translated by Francis Maitland. Thomas Nelson and Sons, London (the title page carries no date), book II, chapter V, "The last efforts of the logisticians", §VIII, pp. 190 to 191: six short paragraphs, set here as one. Whitehead's demonstration is a proof of the principle of induction from this definition. Poincaré's diagnosis of the paradoxes by the vicious circle begins in an article of 1906 (S. Feferman, "Predicativity", the author's copy, pp. 1 to 2).
Section 2.1 traced the aftermath: Russell's ramified hierarchy enforced Poincaré's ban, only to add the axiom of reducibility to recover classical analysis, "a bold, an almost fantastic axiom" in Hermann Weyl's judgment. Weyl's own alternative in Das Kontinuum (1918) built real analysis while quantifying over natural numbers alone, paying a steep price: Cauchy sequences of real numbers still converge there, yet one can no longer prove that every bounded set of reals has a least upper bound. Zermelo had already answered Poincaré in 1908 that classical analysis forms impredicative definitions whenever it takes a maximum or minimum over a family of functions or sets, "and up to now it has not occurred to anyone to regard this as something illogical".Weyl's phrase, of 1946, is quoted from Coquand, "Type theory", Section 3. His analysis and Zermelo's reply are as Feferman reports them, "Predicativity", pp. 7 to 10 of the author's copy; that the principle of the least upper bound for bounded sets becomes unprovable is from J. L. Bell and H. Korté, "Hermann Weyl", Stanford Encyclopedia of Philosophy (substantive revision of 8 June 2024), Section 3.1. That debate is one chapter in the foundational quarrel told in The painted rock; in type theory, Coquand and Huet gave a mathematical answer.
Theorem 5.7.2 (Coquand and Huet, 1988). In the calculus of constructions, whose universe \(\ast\) is impredicative, every reduction of a typable term ends (strong normalization, Theorem 2.6.3), and the calculus is consistent: the proposition \(\Pi (A : \ast).\, A\) has no closed proof.Coquand and Huet (1988), p. 110. Strong normalization stands there as Corollary 1, for the pure lambda term that is left of a proof when its types are removed; for the typed terms as they stand it is the theorem cited at Theorem 2.6.3. Consistency is "COROLLARY 2. The calculus of constructions is consistent, in the sense that there exists a proposition which is not inhabited." Coquand's sentence is from "Type theory", Section 6.
The second part follows from the first: a closed proof would have a normal form \(\lambda A.\, t\), and no normal term \(t\) has the type \(A\) when the only variable in sight is \(A\) itself. The first part is the surprise. "It is quite remarkable that the circularity inherent in impredicativity does not result in non-normalisable terms", Coquand writes of Girard's proof for System F, the argument by reducibility candidates of Section 2.5.
Defining the logical connectives inside Prop shows both what impredicative quantification buys and why inductive types are still needed.
Example 5.7.3 (logic from one quantifier). In an impredicative universe the connectives need not be given by rules. Each can be defined by what follows from it.
def False' : Prop := ∀ p : Prop, p
def And' (a b : Prop) : Prop := ∀ c : Prop, (a → b → c) → c
def Eq' {α : Type u} (a b : α) : Prop := ∀ P : α → Prop, P a → P b
theorem And'.left {a b : Prop} (h : And' a b) : a := h a (fun ha _ => ha)
theorem Eq'.symm {α : Type u} {a b : α} (h : Eq' a b) : Eq' b a :=
h (fun x => Eq' x a) (fun _ ha => ha)
#print axioms And'.left
#print axioms Eq'.symm
'And'.left' does not depend on any axioms
'Eq'.symm' does not depend on any axioms
Eq' is Leibniz's definition of equality, by which Principia Mathematica defines identity,Intuitionistic Type Theory, p. 33. The two phrases that follow are from Dybjer and Palmgren, Sections 7.5 and 4.5; Section 7.4 gives the definitions of the numbers and of equality in the calculus of constructions. and the proof of its symmetry is that of Proposition 5.4.2 (ii), with the family fun x => Eq' x a. The numbers can be defined so too, as \(\Pi (X : \ast).\, X \to (X \to X) \to X\), the type of the Church numerals of Section 1.3. It is the counterpart among types of the definition that Poincaré attacked, a number being what lies in every class that contains zero and is closed under the successor, and for such definitions the calculus of constructions was designed. They did not last: "the full elimination rules could not be derived and instead had to be postulated", induction among them, and inductive types were added as primitives (Section 5.6), where, as in Martin-Löf's theory, they "are viewed as being built up from below". ◁
Its authors drew the line themselves.
Let us say that an object is predicative if it is defined by a quantification over a type which does not contain this object. In this sense, the calculus of construction allows the formation of nonpredicative notions. For instance, the polymorphic identity is not predicative since it can be instantiated over its own type. There is a tension between a purely logical language based on the Curry-Howard correspondence, and the power of expression of set theory. It is legitimate to use impredicative quantification inside the logical language, but if we want to complement it with a set-theoretic hierarchy, this latter part must be strictly stratified.
—Thierry Coquand and Gérard Huet, "The calculus of constructions" (1988). Information and Computation 76, pp. 95 to 120; two consecutive paragraphs of Section 7, p. 117, set here as one.
Its last sentence is Lean's design: impredicative quantification inside the logic, in Prop, and over it a hierarchy of types that is strictly stratified. It is also where Lean leaves Martin-Löf, whose theory is "an intuitionistic theory of types, which is also predicative (or ramified)" on principle.Intuitionistic Type Theory, p. 1. Two things follow for someone who relies on Lean. Prop is impredicative because the mathematics to be stated is: the union that opens this section is how analysis defines a supremum. And that this is safe cannot be read off from the way the types are built up, as a predicative theory means its own safety to be. It is a theorem about the calculus of constructions and, for Lean's whole logic, a model in set theory (Section 6.6). Part of the price was paid in Section 5.6: it is Prop that makes the positivity condition strict.
Every ingredient needed to work with dependent types and repair Section 5.3's rejected Vec α (0 + n) declaration is now in hand.
In Lean
Continuing the Lean 4.35.0 file begun in Section 5.1 puts each rule on display in concrete syntax, turning broken side-conditions into explicit kernel or elaborator errors. First come dependent functions, dependent pairs, and Fin, which Lean defines as a structure (a dependent pair type with named fields).
#check (n : Nat) → Fin (n + 1)
#check @Fin.last
#check (⟨3, 2, by decide⟩ : (n : Nat) × Fin n)
#print Fin
(n : Nat) → Fin (n + 1) : Type
Fin.last : (n : Nat) → Fin (n + 1)
⟨3, ⟨2, ⋯⟩⟩ : (n : Nat) × Fin n
structure Fin (n : Nat) : Type
number of parameters: 1
fields:
Fin.val : Nat
Fin.isLt : ↑self < n
constructor:
Fin.mk {n : Nat} (val : Nat) (isLt : val < n) : Fin n
The type of Fin.last, the function that returns the largest element of Fin (n + 1), is not an arrow: the type of its result mentions its argument. Fin n is itself a pair type, a number with a proof that it is below n; in the pair of 3 and 2 Lean found that proof by evaluating 2 < 3 (the by decide, Section 10.4) and prints it as ⋯.
Comparing the generated recursors of Sigma and Exists shows exactly how Lean enforces the boundary between strong and weak existence in their motives.
#check @Sigma.rec
#check @Exists.rec
def witness {p : Nat → Prop} (h : ∃ n, p n) : Nat :=
@Exists.rec Nat p (fun _ => Nat) (fun w _ => w) h
@Sigma.rec : {α : Type u_2} →
{β : α → Type u_3} →
{motive : Sigma β → Sort u_1} → ((fst : α) → (snd : β fst) → motive ⟨fst, snd⟩) → (t : Sigma β) → motive t
@Exists.rec : ∀ {α : Sort u_1} {p : α → Prop} {motive : Exists p → Prop},
(∀ (w : α) (h : p w), motive ⋯) → ∀ (t : Exists p), motive t
DependentTypes.lean:62:30: error: Type mismatch
Nat
has type
Type
of sort `Type 1` but is expected to have type
Prop
of sort `Type`
The motive of Sigma.rec is a family in any universe, Sort u_1, so a function out of a pair type may return data, the first component for one. The motive of Exists.rec is a family of propositions, and the attempt to return the witness is refused at the motive: fun _ => Nat is not one.
Explicit transport along an equality proof now repairs the rejected Vec α (0 + n) example of Section 5.3.
def castVec {α : Type u} {m n : Nat} (h : m = n) (v : Vec α m) : Vec α n := h ▸ v
set_option pp.explicit true in
#print castVec
example {α : Type u} {n : Nat} (v : Vec α (0 + n)) : Vec α n := castVec (Nat.zero_add n) v
def castVec.{u} : {α : Type u} → {m n : Nat} → @Eq Nat m n → Vec α m → Vec α n :=
fun {α} {m n} h v => @Eq.rec Nat m (fun x h => Vec α x) v n h
The triangle is notation. With every argument printed, h ▸ v is one application of Eq.rec, and the family that Lean found for it, fun x h => Vec α x, is the motive. The last line is the second line of Section 5.3 repaired: a vector of length 0 + n becomes a vector of length n by transport along Nat.zero_add n, and the proof stays in the term.
Finally, proving from scratch that 0 never equals n + 1 brings every concept of this part together on an elementary arithmetic fact that Peano had to postulate as an axiom.
Example 5.8.1 (zero is not a successor). Two declarations in the same Lean 4.35.0 file prove 0 = n + 1 → False with zero axioms.
def IsZero : Nat → Prop := @Nat.rec (fun _ => Prop) True (fun _ _ => False)
theorem zero_ne_succ' (n : Nat) (h : 0 = n + 1) : False :=
@Eq.rec Nat 0 (fun x _ => IsZero x) True.intro (n + 1) h
#print axioms zero_ne_succ'
'zero_ne_succ'' does not depend on any axioms
Here IsZero : Nat → Prop defines a family of propositions by recursion on Nat, instantiating Nat.rec at the constant motive fun _ => Prop, which is well-typed only because Prop is itself a term inhabiting the universe Type. By the two computation rules of Nat.rec, IsZero 0 reduces definitionally to True and IsZero (n + 1) reduces definitionally to False. Assuming h : 0 = n + 1, zero_ne_succ' transports True.intro : IsZero 0 along h into the fiber IsZero (n + 1), which converts directly to False. In two declarations and zero axioms, the proof combines a dependent family, a universe-valued recursor, equality transport, and two silent applications of the conversion rule. Without a universe, no function Nat → Prop could map 0 and n + 1 to different types, so transporting across 0 = n + 1 could never turn an equation between numbers into False: Martin-Löf noted that the proof "seems to involve treating sets as elements", and Jan Smith proved in 1988 that 0 ≠ n + 1 is unprovable in Martin-Löf type theory without universes.Intuitionistic Type Theory, p. 51. The theorem is J. M. Smith's (Journal of Symbolic Logic 53(3), 1988), as Nordström, Petersson and Smith report it, chapter 14, p. 86. ◁
A type may mention a value. Then "for all" is a function type, "exists" is a type of pairs and equality is a family of types with one constructor, so that programs, statements and proofs are written in one language. The price is that checking a type means computing, which gives two equalities, the one a machine decides and the one a person proves, and that the type of all types has to be kept out of itself.
These rules came from several type theories, and a checker needs one, in full. Section 6 states Lean's logic exactly: twelve kinds of term, the rules of this part among a few more, and the small program that applies them.