This guide takes you from a blank file to a working collection of Lean 4 proofs using Mathlib. It assumes you have Lean 4 installed and a project with Mathlib as a dependency. If you have not done that yet, follow the Installation and Using Mathlib as a Dependency guides first. Once your project is set up andDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/leanprover-community/mathlib4/llms.txt
Use this file to discover all available pages before exploring further.
lake exe cache get has finished, you are ready to go — no further compilation is needed to run the examples below.
Your First Proof File
Inside your Lake project, create a new file calledProofs.lean at the root of the project (next to lakefile.lean). Every Lean file that uses Mathlib starts with an import:
Proofs.lean
Exploring the Library with #check
Before writing proofs, it helps to know how to look things up. The #check command prints the type of any expression. Open Proofs.lean in VS Code, add the lines below, and hover over each #check to see the result in the Lean Infoview panel on the right:
Proofs.lean
Example 1: Algebraic Identities with ring
The ring tactic proves equalities that hold in any commutative (semi)ring by normalising both sides. It handles polynomial identities over any ring type automatically — you never need to cite individual lemmas.
Proofs.lean
ring works over any type that carries a CommSemiring, CommRing, or compatible algebraic structure. It does not work for goals involving division or general field operations — use field_simp followed by ring in those cases.Example 2: Linear Arithmetic with linarith
The linarith tactic discharges linear arithmetic goals over ordered fields and rings. It can combine hypotheses using linear combinations to close inequalities and contradictions.
Proofs.lean
Example 3: Numeric Calculations with norm_num
The norm_num tactic evaluates numeric expressions and decides numeric propositions. It can verify primality, divisibility, inequalities between numeric literals, and much more — all by computation.
Proofs.lean
Example 4: Using an Existing Mathlib Theorem
Mathlib contains hundreds of thousands of named theorems. Once you find one with#check, you can apply it directly in a proof using exact or apply.
Proofs.lean
Example 5: Combining Tactics
Real proofs often chain several tactics together. Theby block is a sequenced tactic proof; each tactic transforms the current proof goal until it is closed.
Proofs.lean
Running Your File
Check in VS Code (recommended)
With the Lean 4 VS Code extension installed, simply open
Proofs.lean. Lean checks the file incrementally as you type. The Lean Infoview panel (open with Ctrl+Shift+P → Lean 4: Show Infoview) displays the proof state at your cursor position, showing remaining goals and any error messages.Check from the command line
To check a single file without the editor:Lean prints any errors or warnings and exits. No output means all proofs are accepted.
Putting It All Together
Here is a complete, self-contained file you can drop into any Mathlib project and run immediately:Proofs.lean
The
/-! ... -/ block at the top is a module docstring — Mathlib style encourages documenting every file this way. The text inside is rendered in the generated API docs.Key Tactics Reference
| Tactic | What it closes |
|---|---|
ring | Polynomial identities in commutative (semi)rings |
linarith | Linear arithmetic inequalities over ordered types |
nlinarith | Nonlinear arithmetic inequalities |
norm_num | Numeric computations, primality, divisibility |
decide | Decidable propositions on finite/computable types |
simp | Simplification using a set of rewrite rules |
aesop | Automated proof search for structured goals |
omega | Linear arithmetic over ℕ and ℤ specifically |
positivity | Proves expressions are non-negative or positive |
exact | Closes the goal with a provided term |
apply | Applies a theorem, generating sub-goals for its hypotheses |
Next Steps
Mathlib Tactics Overview
Full reference for all tactics available in Mathlib.
Mathematics in Lean
The community’s interactive textbook — the best structured introduction to proving mathematics in Lean.
Mathlib4 API Docs
Search for any theorem, definition, or tactic in Mathlib by name or type signature.
Lean Zulip Chat
Ask questions, share proofs, and discuss Mathlib with the community.