15. Becoming productive with Mathlib
Mathlib is Lean’s community library of mathematical structures, theorems, notation, and proof automation. Lean’s core and Std cover the account application. Use Mathlib when you need existing mathematical results or specialised proof tools.
Use the library’s toolchain
Create a directory called math-workbook. Add lean-toolchain containing leanprover/lean4:v4.35.0 and a lakefile.toml with this pinned configuration:
name = "math-workbook"
version = "0.1.0"
[[require]]
name = "mathlib"
scope = "leanprover-community"
git = "https://github.com/leanprover-community/mathlib4.git"
rev = "56a005855ea17e0b0958f73ec801ae140b0a1679"
Run these commands inside that directory:
MATHLIB_NO_CACHE_ON_UPDATE=1 lake update
lake exe cache get Mathlib.Tactic.Ring Mathlib.Tactic.Linarith Mathlib.Data.Rat.Defs Mathlib.Data.Set.Basic
This revision is Mathlib’s Lean 4.35.0 release. lake update resolves dependencies and creates a manifest. Commit the manifest with the configuration.
The update command above disables the automatic full-cache fetch so that the cache command can fetch prebuilt artefacts for this chapter’s imports. This avoids building all of Mathlib from source. For a different Mathlib revision, use its supported toolchain and matching cache.
If a Nix-provided toolchain reports bad .ltar file while extracting the cache, check the bundled leantar version. The book’s environment supplies 0.1.19, while the cache requires the newer format supported by 0.1.21.
Use a distribution with a compatible extractor or update the extractor in your Nix toolchain. The standard-library workbook can be checked without the Mathlib cache.
The main book’s automated check uses only Std. Save the following example as MathPractice.lean in the separate Mathlib project.
import Mathlib.Tactic.Ring
import Mathlib.Tactic.Linarith
import Mathlib.Data.Rat.Defs
import Mathlib.Data.Set.Basic
example (x y : ℤ) : (x + y) ^ 2 = x ^ 2 + 2 * x * y + y ^ 2 := by
ring
example (x y : ℚ) (h : x ≤ y) : 3 * x + 1 ≤ 3 * y + 1 := by
linarith
example (s t : Set ℕ) : s ∩ t ⊆ s := by
intro x hx
exact hx.1
Check it using lake env lean MathPractice.lean. ring normalises polynomial expressions using the relevant algebraic laws. linarith combines linear hypotheses over suitable ordered number systems.
In the subset proof, intro introduces an arbitrary element and its membership hypothesis. Membership in the intersection gives a pair of proofs, one for each set.
The proof techniques are the same as in earlier chapters. The library supplies the definitions and lemmas for common mathematical arguments.
Read mathematical notation through membership
A set s : Set α describes a predicate on values of α. To prove s ⊆ t, introduce an arbitrary x, assume x ∈ s, and prove x ∈ t. To prove set equality, use extensionality to compare membership for an arbitrary element.
Use Finset α for finite sets with computational operations. Lists preserve order and multiplicity. Choose the representation that matches the claim.
When algebraic notation is overloaded, inspect the ambient type and its class assumptions. A theorem about an arbitrary group has different available laws from one about a commutative group. A proof about natural subtraction cannot be transferred to integer subtraction merely by changing a type annotation.
Search and automation
exact? and apply? can suggest existing results. simp? can suggest an explicit list of simplification lemmas.
aesop performs structured proof search over suitable rules. norm_num proves numerical facts, ring handles polynomial identities, and linarith handles suitable linear consequences.
Use suggestions to discover an argument, then keep a readable proof with explicit dependencies where useful. Begin with one question: which existing theorem states this fact, or a stronger fact from which it follows? A theorem application is often clearer than a fresh automated derivation.
A practical search loop
- State the goal using the library’s existing types and operations.
- Inspect namespaces, documentation, and search results for the central operation.
- Check the candidate’s exact type in your project.
- Match its assumptions with your local context.
- Use
exact,apply,rw, orsimpa usingto connect it to the goal.
Check the resulting proof locally.
Exercises
- In a Mathlib project, prove the square identity above with the summands reversed.
- Prove
s ∩ t ⊆ tusing the second membership component. - Find an existing theorem for commutativity of set intersection and inspect its type before using it.
These exercises are an optional library track. Their import and version requirements are separate from the standard-library workbook.