Lean’s Dependent Type Theory


Lean’s Dependent Type Theory

Anna
Welcome back. Today we're exploring the type theory behind Lean: the axiomatic system it depends on, how it developed from the lambda calculus, and some important properties and limitations.

Michael
Right. Lean is an open-source interactive theorem prover, originally developed by Leonardo de Moura at Microsoft Research, with further work at places including Carnegie Mellon and Pitt. It combines formalized mathematics with functional programming and metaprogramming capabilities, and its mathematical ecosystem includes the actively developed mathlib library.

Anna
The central point is that Lean is based on dependent type theory. In the classification used here, it is classical rather than intuitionistic, it is not based on homotopy type theory, and it is closely related to the Calculus of Inductive Constructions, or CIC, associated with Coq.

Michael
To understand the construction, we start with the untyped lambda calculus. Its expressions are variables, applications of one expression to another, or lambda abstractions. A lambda binds a variable inside its body, so an expression can represent a function that takes an input and uses it in another expression.

Anna
The main computation rule is beta reduction: apply a lambda to an argument, and substitute that argument for the bound variable in the body. In the presentation we're discussing, reduction is nondeterministic, meaning that you can choose a reducible part inside an application and continue reducing it. Different choices may produce different intermediate terms, and a computation could even continue forever.

Michael
But the lambda calculus has the Church-Rosser, or confluence, property. If one expression reduces in zero or more steps to both e1 and e2, then there is some later expression that both e1 and e2 can reduce to. So different reduction paths can diverge temporarily, but they can be brought back together.

Anna
The untyped lambda calculus was developed by Alonzo Church as a simple model of computation, and it has the same computational power as Turing machines. Evaluation strategies such as call-by-value and call-by-name restrict which reductions are performed, but the basic discussion here considers all possible reductions.

Michael
Next we add simple types. Lambdas now annotate their input variables with a type. In the basic system, we have function types and a base type of individuals. The base type is not the interesting part; it simply ensures that the theory contains some types at all.

Anna
Typing is expressed with judgments of the form ‘in context Gamma, expression e has type tau.’ The variable rule says that if the context contains x with type tau, then x has type tau. For an application, if e1 is a function from alpha to beta and e2 has type alpha, then applying e1 to e2 produces something of type beta.

Michael
And the lambda rule says that if, after extending the context with x of type alpha, the body e has type beta, then the lambda abstraction has function type alpha to beta. These rules are enough to typecheck simple functional terms.

Anna
A major consequence is strong normalization: every well-typed term eventually normalizes. Terms such as the untyped Y combinator, which can unfold forever in the untyped lambda calculus, do not have valid types in simple type theory. The type system excludes that kind of unrestricted nontermination.

Michael
Dependent type theory makes the function result type depend on the input. Instead of writing alpha arrow beta, we write a Pi type, or dependent forall, such as ‘for every x of type alpha, beta.’ The important difference is that beta is allowed to refer to x.

Anna
That changes application slightly. If a dependent function is applied to an argument, the result type is obtained by substituting that argument for x inside beta. This lets the type of the result vary according to the value supplied.

Michael
To make genuinely parametric functions, such as the identity function that works for every type, we need to quantify over types themselves. So the system introduces a universe of types, written here as U. A type such as iota is itself assigned a type, and a Pi expression is a type when its components are appropriately typed.

Anna
At this stage, terms and types are combined into one syntactic class of expressions. That means a lambda can return a type, not just an ordinary value. In other words, Lean supports computation at the type level, which is one source of the expressive power of dependent type theory.

Michael
However, the naive rule that says ‘type has type type’ is inconsistent. It allows a version of Russell's paradox, leading to the result that every proposition could be inhabited—in effect, that everything would be provable.

Anna
The solution is a hierarchy of universes. Instead of one universe that contains itself, we have U at level n as a type in the next universe, U at level n plus 1. When forming a Pi type from two universes, the resulting universe is selected using the larger level, which prevents the problematic circularity.

Michael
Now we bring in the Curry-Howard correspondence. Propositions behave like types, and proofs of a proposition are terms inhabiting that type. A false proposition corresponds to an empty type, while a provable proposition has one or more inhabitants.

Anna
That explains why function types and Pi types resemble implication and universal quantification. A term of type P implies Q is a proof that P implies Q, and a dependent function that produces a proof for every x corresponds to a universally quantified proposition.

Michael
Lean has a universe of propositions, called Prop and identified here with U0. Pi types whose result is a proposition remain propositions. But a straightforward universe rule would sometimes place a proposition involving a type from a higher universe into that higher universe, which is not what we want.

Anna
So Lean uses an imax operation rather than ordinary maximum when calculating universe levels. Imax behaves like max except that when one relevant level is zero, the result stays at zero. This is the impredicative treatment of Prop: propositions can quantify over objects from higher universes while remaining in the lowest universe.

Michael
The next issue is convertibility. A type such as the identity function applied to tau may reduce to tau, but syntactically it is not initially the same expression. Without a convertibility judgment, Lean could reject an application whose argument has type tau even though the function domain is definitionally equal to tau.

Anna
Convertibility allows expressions that are definitionally equal to be used interchangeably as types. It is reflexive, symmetric, and transitive, and it has compatibility rules saying that equal components give equal larger expressions.

Michael
The content-bearing rules include beta equality, which identifies a function application with the result of substitution, and eta equality. The eta rule says that when e does not depend on x, a function that takes x and immediately applies e to x is definitionally equal to e itself.

Anna
Another major feature is inductive types. An inductive specification lists constructors, and the resulting type is the smallest type freely generated by those constructors. Natural numbers are the standard example: zero is a natural number, successor maps a natural number to another natural number, and all natural numbers are generated from those constructors.

Michael
Inductive types also provide recursors. For natural numbers, a dependent recursor says that if we can produce a result at zero and can turn a result at n into a result at successor n, then we can produce a result for every natural number. Its computation rules reduce the recursor at zero and at a successor, just like primitive recursion.

Anna
When the result lies in Prop, this recursor gives induction on the natural numbers, corresponding to the Peano induction principle. So inductive definitions support both computation and proof by structural or mathematical induction.

Michael
The same mechanism defines logical connectives. Existential quantification has a constructor containing a witness x together with a proof that the predicate holds at x. Conjunction has a constructor requiring proofs of both propositions, while disjunction has separate constructors for the left and right alternatives.

Anna
False is an inductive proposition with no constructors, and true has exactly one named constructor. Every inductive declaration supplies the type, its constructors, a recursor, and computation rules associated with that recursor.

Michael
There is an important restriction: inductive definitions must be strictly positive. Roughly, the type being defined cannot appear on the left side of the left side of an arrow. Without this restriction, a bad inductive definition could construct an injection from the powerset of the type into the type itself, contradicting Cantor's theorem and producing inconsistency.

Anna
Lean also supports inductive families, which are indexed by values. Equality is defined this way: for a given x, equality with x is generated by a reflexivity constructor showing that x equals x. Its recursor expresses substitution: if x equals y and a property holds of x, then it holds of y.

Michael
Proof irrelevance says that two proofs, meaning two elements of Prop, are definitionally equal. This is incompatible with homotopy type theory's univalence principle, because univalence can produce types with multiple unequal proofs of equality. In Lean's setting, proof irrelevance also imposes restrictions on how propositions can be eliminated.

Anna
In particular, if an inductive type in Prop has multiple possible ways to be inhabited, its recursor is generally restricted to eliminating only into Prop. Such a type is called small eliminating. The idea is that proofs are being treated as irrelevant, so they cannot generally be used to compute data in higher universes.

Michael
There is an exception for certain propositions called subsingleton-eliminating types. A subsingleton is a type with at most one element. If the structure of an inductive proposition guarantees that its inhabitants are effectively unique, Lean may allow elimination into higher universes.

Anna
Equality is an example of a proposition that can eliminate into any universe. The conditions are technical, but in broad terms, nonrecursive arguments must be propositions or must appear appropriately in the final target. This distinction between small elimination and subsingleton elimination becomes important later.

Michael
Lean also includes additional axioms. Propositional extensionality says that if two propositions imply each other, then they are equal, allowing substitution between them. Quotient types identify elements according to a relation rather than being freely generated by constructors, using operations such as a constructor, a lift, and a rule requiring functions to preserve the relation.

Anna
The axiom of choice is expressed using a Prop called nonempty. It has a constructor saying that an element of alpha gives an element of nonempty alpha, and choice provides a way back from nonempty alpha to an element of alpha. This axiom has no computational interpretation; instead, it supports classical mathematics and yields the law of excluded middle.

Michael
The combination of these features leads to a surprising issue: full definitional equality is undecidable. The source of the problem is the interaction between recursive subsingleton elimination and definitional reduction.

Anna
The example uses an accessibility predicate, written acc. Informally, x is accessible when every element smaller than x is accessible. This inductive proposition has one constructor, and because it is recursive yet subsingleton-eliminating, its recursor can eliminate into arbitrary types.

Michael
From a hypothetical proof that zero is accessible under a greater-than relation, one can define a function that examines a decidable proposition P at successive natural numbers. If P holds at the current number, the function proceeds to the next number; if P fails, it reduces to zero.

Anna
By repeatedly unfolding this function, computation effectively searches through all natural numbers. That means whether the result reduces to zero can encode a statement such as whether there exists a number where P fails—or equivalently, whether it is not the case that P holds for every number. Choosing P from a problem such as whether a machine halts after a given number of steps connects definitional equality to an undecidable property.

Michael
The hypothetical accessibility proof creates an inconsistent context, but the issue is not confined to an unusable context. By lambda-abstracting over that proof, we obtain two functions that are definitionally equal exactly when the encoded undecidable condition holds. So full definitional equality itself cannot be decidable.

Anna
Lean therefore uses an algorithmic typing and conversion procedure that is an underapproximation of the ideal rules. It may reject a term as not definitionally equal even when the complete theory would eventually establish that equality, because Lean avoids evaluating indefinitely complicated computations.

Michael
This practical algorithmic relation can fail to be transitive. If it were both transitive and sufficiently complete, it would recover the undecidable full relation. A practical workaround is to insert identity functions at carefully chosen types, forcing Lean to recognize smaller conversion steps instead of trying and failing to see one large transitive step.

Anna
Those identity functions can act as blockers if unfolded carelessly, so they have to be controlled. In practice, when definitional equality becomes fragile, users may rely on ordinary propositional equality instead, although dependent types can make that less convenient because computation no longer follows automatically.

Michael
Another possible approach would be a richer reduction procedure that is only a semidecision procedure. It could keep reducing and eventually succeed whenever two terms really are definitionally equal, but it might not halt when they are not. If decidability is considered essential, one proposed solution is to restrict or remove subsingleton elimination for recursive propositions such as accessibility.

Anna
That would have consequences: some well-foundedness arguments would become ordinary types rather than propositions. Equality might still retain a more limited form of elimination because it is not recursive in the same problematic way.

Michael
Lean also has a set-theoretic model. Types are interpreted as sets, and terms as elements of those sets. Dependent function types are interpreted as dependent functions, while universes are interpreted using very large collections closed under the required type-forming operations.

Anna
Because Lean includes classical logic and proof irrelevance, Prop is modeled with just two possibilities: the empty set for false and a singleton set for true. The higher universes can be modeled using Grothendieck universes, which correspond to inaccessible cardinals; assuming enough inaccessible cardinals supplies enough semantic universes.

Michael
The soundness theorem says, in essence, that if e has type alpha, then the interpretation of e is an element of the interpretation of alpha. It also says that definitional equality is interpreted as ordinary equality. This establishes consistency for the model.

Anna
To make the model well-defined, we need unique typing. Lean has the theorem that if an expression has two types, those types are definitionally equal. Combining that with universe rules shows that an expression cannot inhabit two genuinely different universe levels.

Michael
This differs from Coq, where universe cumulativity allows types to move between universes through a subtyping-like relation. Lean does not have cumulativity, so the unique-typing statement is cleaner there.

Anna
There are also limits on what Lean can prove about equality. Heterogeneous equality does not generally support the theorem that application preserves it. And Lean usually cannot prove that two differently presented types are unequal—for example, int and nat, or two separately defined but structurally identical copies of nat.

Michael
Two such types may be shown to be isomorphic, but proving that they are not equal is generally difficult. Cardinality is the main reliable way to distinguish types: if two types are equal, they must have the same cardinality.

Anna
The Lean virtual machine introduces another subtle distinction. During evaluation, it can ignore casts between types instead of reducing a recursor all the way to a reflexivity proof. As a result, the virtual machine may temporarily evaluate terms that are not well typed according to the ordinary reduction story, yet still produce the correct final result.

Michael
This motivates a type-erasure perspective: remove the types from terms and study whether computation preserves the result. Such a theory could help justify VM overrides, where one implementation is replaced by another in the virtual machine without changing computational behavior.

Anna
Several theoretical questions remain important. One is a complete strong-normalization theorem for the full system. Some subsystems of Coq have such proofs, and the algorithmic reduction used in Lean is expected to be strongly normalizing because it stops before reaching the problematic behavior.

Michael
Another open direction is formalizing the whole theory itself. Since Lean is a formal theorem prover, its type theory, metatheory, models, and computational behavior should ideally be represented and verified formally.

Anna
Finally, there is a design question about identifying Prop with U0. Separating propositions from the ordinary universe hierarchy could eliminate some of the special imax rules and simplify parts of the presentation.

Michael
But that separation would interfere with universe parametricity. Lean's universe levels are expressions involving variables and imax, so definitions can be polymorphic over universe levels and can sometimes behave as propositions at one level and ordinary types at another. Keeping everything integrated avoids duplicating definitions for Prop and for higher universes, although it makes the theory more complicated.

Anna
So the overall picture is a carefully balanced system. Lean gains dependent types, universes, impredicative propositions, inductive families, proof irrelevance, classical axioms, and powerful computation, but those features interact in subtle ways.

Michael
And the central lesson is that the convenient notion of definitional equality is not merely a simple evaluator. In the full theory it can be undecidable, while Lean's practical algorithm approximates it. Understanding that tension helps explain both Lean's expressive power and some of its most surprising behavior.

Ask anything
We've read this whole study set, so questions about it get the best answers.