Elaboration in Dependent Type Theory

Leonardo Mendonça de Moura, Jeremy Avigad, Soonho Kong, Cody RouxarXiv 2015

Abstract

Interactive theorem provers require a robust "elaborator" to translate human-written, partially specified mathematical expressions into fully precise, type-checked code. This paper details the elaboration algorithm powering the Lean theorem prover, which seamlessly integrates higher-order unification, type class inference, and computational reduction. By employing smart heuristics and optimizations, the algorithm balances the immense computational complexity of dependent type theory with the practical performance needed by users.

Introduction

In strongly-typed foundational systems for mathematics—specifically dependent type theory—types can depend on values. This allows for incredibly expressive proofs and programs, but writing fully explicit code in these languages is tedious and unreadable. In practice, users leave out implicit arguments, use overloaded notation, and rely on automatic coercions (e.g., treating a natural number as an integer).

The process of passing from a user's quasi-formal, partially-specified input to a completely precise, machine-verifiable formal expression is called elaboration. Elaboration is notoriously difficult. It requires solving higher-order unification problems (which are generally undecidable), executing computational reductions (knowing that 2 + 2 is definitionally equal to 4), and resolving type classes—all at the same time. If an elaborator is too naive, it will freeze due to combinatorial explosion; if it is too restrictive, the user is forced to write overly verbose proofs. This paper details the practical, highly optimized algorithm implemented in the Lean theorem prover.

The Elaboration Algorithm

The authors frame elaboration as a massive constraint satisfaction problem, broken into two main phases: preprocessing and constraint resolution.

Preprocessing

The parser first converts user input into a preterm. The preprocessor traverses this preterm, replacing missing information with "holes" (metavariables). It simultaneously generates a list of constraints. Unification constraints enforce type matching, while choice constraints handle overloading, coercions, and type class inference.

Constraint Resolution

This is the heart of the elaborator. It uses a priority queue to solve "straightforward" constraints first (like simple pattern matching), which provides information to help solve harder constraints later.

  • Non-chronological backtracking: When faced with a choice constraint (e.g., an overloaded operator), the solver creates a case split. It uses pure data structures (red-black trees) to save the state, allowing it to easily backtrack if a branch fails.
  • Unfolding Heuristics: To prevent infinite loops during computational reduction, users can tag definitions as irreducible, semireducible, or reducible. The elaborator uses these hints to decide when to expand a definition during the search.
  • Locally Nameless Representation: Variables are managed using a "locally nameless" approach where free variables have unique IDs and bound variables use de Bruijn indices. To fix the historical performance bottlenecks of this approach, the authors introduced a caching optimization that tracks the maximum de Bruijn index bound for each term.

Key Architectural Insights

Several design choices distinguish Lean's elaborator from its predecessors:

  1. Uniformity via Choice Constraints: By treating type class resolution, ad hoc overloading, and coercions all as "choice constraints," the elaborator can manage them under a single, unified backtracking search framework rather than relying on disjoint, ad-hoc systems.
  2. State Copying over Trail Stacks: Implementing backtracking in complex solvers often involves complex "undo" trails. The authors found that simply copying the solver's state (Queue, Substitution, and Metavariable mapping) using pure, persistent data structures is highly efficient and not a bottleneck.
  3. The Power of Simple Caching: The locally nameless variable approach is elegant but requires traversing entire syntax trees to check for dangling bound variables. By simply storing an integer bound of de Bruijn indices on each term, the system can instantly skip subterms that don't need variable instantiation. This tiny architectural tweak yielded massive speedups.

Evaluation and Results

There are no traditional machine learning datasets used in this paper. Instead, the algorithm is evaluated on its ability to compile Lean's formal mathematical libraries, including the Lean Standard Library (~42,000 lines) and the Homotopy Type Theory (HoTT) Library (>25,000 lines).

The elaborator successfully and efficiently handles the complex interactions of Lean's features. Most notably, the de Bruijn index bound optimization completely eliminated the performance penalty of the locally nameless approach. Compiling the Lean standard library dropped from 2,189.97 seconds down to just 71.06 seconds, representing a ~30x speedup.

Limitations and Future Work

Because higher-order unification is undecidable, the solver uses an incomplete search. For instance, during the "imitation" step of Huet's algorithm, it intentionally ignores recursors to prevent the search space from exploding. Additionally, the preprocessor currently does not try to inject coercions if both the expected type and the actual type are "stuck" (unresolved) at preprocessing time. Future work involves comparing these specific design trade-offs—such as constraint postponement and unfolding heuristics—against alternative approaches emerging in other provers like Coq.

Summary updated

BibTeX
@misc{moura2015elaboration,
  author = {Leonardo Mendonça de Moura and Jeremy Avigad and Soonho Kong and Cody Roux},
  title = {{Elaboration in Dependent Type Theory}},
  year = {2015},
  eprint = {1505.04324},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1505.04324},
}
Figure