Introduction
Lean 4 is a functional programming language and interactive theorem prover developed at Microsoft Research and Carnegie Mellon University. It lets developers write ordinary programs and formal mathematical proofs in the same language, making it a tool for both verified software and research-level mathematics.
What Lean 4 Does
- Provides a dependent type system for expressing and verifying complex properties
- Supports writing executable programs alongside machine-checked proofs
- Includes a tactic framework for interactive proof construction
- Compiles to efficient native code via C intermediate representation
- Powers the Mathlib library with hundreds of thousands of formalized theorems
Architecture Overview
Lean 4 is implemented in Lean itself (self-hosted). The compiler type-checks source code against a dependent type theory kernel, ensuring logical consistency. Code that passes the type checker is compiled to C for native execution. The tactic framework allows users to build proofs step-by-step, with the kernel verifying each step.
Self-Hosting & Configuration
- Install via
elan, the Lean version manager - Manage projects and dependencies with
lake, the built-in build system - Configure dependencies in
lakefile.leanorlakefile.toml - Use VS Code with the Lean 4 extension for interactive development
- Pin toolchain versions in
lean-toolchainfile per project
Key Features
- Dependent types enabling proofs as programs and programs as proofs
- Powerful metaprogramming and custom tactic authoring
- Mathlib: one of the largest libraries of formalized mathematics
- Efficient compiled execution for non-proof code
- Interactive feedback in editors with goal state display
Comparison with Similar Tools
- Coq (Rocq) — Coq has a longer history and different tactic language; Lean 4 offers a more modern syntax and self-hosted compiler
- Agda — Agda focuses on dependently typed programming; Lean 4 adds a richer tactic framework for proof automation
- Haskell — Haskell has a powerful type system but cannot verify arbitrary properties; Lean 4 can
- Idris 2 — Idris targets practical dependently typed programming; Lean 4 covers both proofs and practical code
- F* — F* targets verified security code; Lean 4 is more general-purpose for both math and programs
FAQ
Q: Is Lean 4 only for mathematicians? A: No. Lean 4 is a general-purpose functional language. You can write regular programs without proofs, or combine both.
Q: What is Mathlib? A: Mathlib is a community-maintained library of formalized mathematics in Lean 4, covering algebra, analysis, topology, and more.
Q: Can Lean 4 produce fast executables? A: Yes. Lean 4 compiles to C and produces native binaries with performance suitable for practical applications.
Q: How does Lean 4 relate to Lean 3? A: Lean 4 is a complete rewrite with a new compiler, build system, and syntax. It is not backward-compatible with Lean 3.