Skip to main content
QUICK REVIEW

[Paper Review] Lean4Lean: Verifying a Typechecker for Lean, in Lean

Mario Carneiro|arXiv (Cornell University)|Mar 21, 2024
Manufacturing Process and OptimizationEngineering6 citations
TL;DR

Lean4Lean is an external Lean-written verifier for Lean 4, capable of checking Lean’s kernel, elaborator, compiler, Std, and Mathlib, with formal metatheory groundwork and partial proofs toward kernel correctness.

ABSTRACT

In this paper we present a new "external checker" for the Lean theorem prover, written in Lean itself. This is the first complete typechecker for Lean 4 other than the reference implementation in C++ used by Lean itself, and our new checker is competitive with the original, running between 20% and 50% slower and usable to verify all of Lean's mathlib library, forming an additional step in Lean's aim to self-host the full elaborator and compiler. Moreover, because the checker is written in a language which admits formal verification, it is possible to state and prove properties about the kernel itself, and we report on progress to formalize the Lean type theory abstractly and prove some theorems about it. Finally, we combine these to get a proof of correctness of parts of the kernel. We plan to use this project to help justify any future changes to the kernel and type theory and ensure unsoundness does not sneak in through either the abstract theory or implementation bugs. The verification is already paying off, as one soundness bug has been spotted and fixed as a result of this work.

Motivation & Objective

  • Motivate and implement an external verifier for Lean 4 written in Lean itself.
  • Formally specify a metatheory for Lean's type theory and establish core typing and definitional-equality properties.
  • Translate and relate Lean's internal Expr representation to a verified abstract syntax (VExpr) for verification.
  • Demonstrate verification feasibility by checking Lean’s kernel against a formalized theory and report partial metatheory results.

Proposed method

  • Define VExpr as a de Bruijn-based abstract syntax for Lean's type theory and formalize typing and definitional equality judgments (IsDefEq).
  • Relate VExpr to Lean's Expr via a translation mechanism driven by a TrExpr judgment parameterized by environment and universe information.
  • Implement Lean4Lean in Lean as a kernel mirroring the C++ Lean kernel to verify Lean’s compiler package, Std, and Mathlib.
  • Use a fuel-based RecM monad to manage termination and partiality in the verification routine, with a fixed deep-recursion limit.
  • Describe the global environment (Environment) and core operations (add, mkEmptyEnvironment) to tie together declarations for verification.

Experimental results

Research questions

  • RQ1Can Lean's kernel be formally verified against a Lean-defined metatheory using an external verifier implemented in Lean?
  • RQ2What core typing and definitional-equality properties can be established about Lean's type theory within the external verifier?
  • RQ3How can the internal Expr representation be faithfully modeled and related to a verified abstract syntax (VExpr) for verification purposes?
  • RQ4What is the performance trade-off of a Lean-implemented external verifier compared to the original C++ kernel, and is full mathlib verification feasible?
  • RQ5What partial metatheoretic results (e.g., unique typing, inversion properties) can be formalized and what remains conjectural?

Key findings

  • The Lean4Lean external verifier, implemented in Lean, can verify Lean 4’s kernel, compiler package, Std, and Mathlib, with 20%–50% slower performance than the original kernel.
  • The project provides a formalization effort around a base typing judgment and properties like regularity and invariants for the verified kernel.
  • A translation framework (TrExpr) and an abstract syntax (VExpr) connect Lean's internal Expr to the verifier, enabling formal reasoning about terms under a local context.
  • The verification uses a fuel-based RecM monad to ensure termination in the Lean environment, acknowledging nontermination in the underlying theory and practical constraints.
  • Some conjectured properties (e.g., UniqueTyping, Definitional Inversion) are identified as needing further proof due to a subtle proof error, but are believed true and essential for proving kernel correctness.
  • The work lays groundwork for future kernel changes to be justified by formal metatheory and verification results.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.