Introduction to Mathlib
Mathlib is the community mathematics library for Lean 4. It adds definitions and theorems across most of undergraduate mathematics and beyond, plus tactics such as ring and linarith.
What is Mathlib?
Mathlib is a community-driven mathematical library containing:
- Over a million lines of formalized mathematics
- Algebra, analysis, topology, number theory, and more
- Powerful automation tactics for common proof patterns
- Type class hierarchies for abstract mathematics
Setting Up Mathlib
The quickest start is a new project from Lake's math template, which already depends on Mathlib:
1lake new my_project math2cd my_project3lake exe cache get # download prebuilt Mathlib instead of compiling it4lake buildTo add Mathlib to an existing project, declare it in lakefile.toml:
1[[require]]2name = "mathlib"3scope = "leanprover-community"or, in a lakefile.lean:
1require mathlib from git2 "https://github.com/leanprover-community/mathlib4"Then run lake update and lake exe cache get. Without the cache, Lake compiles all of Mathlib from source, which takes hours. Run lake exe cache get again whenever you update Mathlib.
1-- In your Lean files:2import Mathlib.Tactic -- Import all tactics34-- Or import specific modules:5import Mathlib.Tactic.Ring6import Mathlib.Tactic.Linarith7import Mathlib.Tactic.FieldSimpMathlib.Tactic brings in all common tactics.Finding a Lemma in Mathlib
Mathlib has hundreds of thousands of lemmas, so the usual question is not whether a fact is proved but what it is called. Lean can search for you from inside a proof:
1example (a b : ℕ) : a + b = b + a := by2 exact? -- suggests: exact Nat.add_comm a b34example (a b c : ℝ) (h : a ≤ b) (hc : 0 ≤ c) : a * c ≤ b * c := by5 apply? -- lists lemmas whose conclusion fits, such as mul_le_mul_of_nonneg_rightexact? looks for a single lemma that closes the goal, apply? for lemmas whose conclusion matches it, and rw? for equations that rewrite it. Outside a proof, search by type shape with Loogle, or by name in the Mathlib documentation. Names follow the statement: mul_le_mul_of_nonneg_right says "multiply both sides of a ≤ on the right by a nonnegative number", so once you know the convention you can often guess a name and confirm it with #check.
Core Lean 4 vs Mathlib Tactics
1-- Core Lean 4 tactics (always available):2-- rfl, rw, simp, exact, apply, intro, cases, induction3-- constructor, have, let, omega, decide, native_decide45-- Mathlib tactics (require import):6-- ring, field_simp, linarith, nlinarith, polyrith7-- rcases, obtain, push_neg, contrapose, ext, congr8-- norm_num, positivity, gcongr, and many moreWhy Use Mathlib Tactics?
1. Domain-Specific Automation
1import Mathlib.Tactic.Ring23-- ring solves polynomial equations automatically4example (x y : Int) : (x + y)^2 = x^2 + 2*x*y + y^2 := by ring56-- Without ring, this requires many rewrites!2. Proof Search
1import Mathlib.Tactic.Linarith23-- linarith finds linear arithmetic contradictions4example (x y : Int) (h1 : x < y) (h2 : y < x) : False := by linarith56-- It searches through hypotheses automatically3. Structural Tactics
1import Mathlib.Tactic.Cases23-- rcases provides powerful pattern matching4example (h : ∃ n : Nat, n > 0 ∧ n < 10) : True := by5 rcases h with ⟨n, hn_pos, hn_lt⟩6 trivialDeep Dive: Mathlib's Philosophy
Mathlib follows several design principles:
- Generality: Define concepts at their most general level
- Bundling: Group related structures together
- Automation: Provide tactics that handle routine proofs
- Consistency: Follow naming conventions strictly
Mathlib Tactics in This Module
We'll cover the most commonly used Mathlib tactics:
Arithmetic Tactics
ring— Solve polynomial ring equationsfield_simp— Simplify field expressions with divisionlinarith— Linear arithmetic reasoningnlinarith— Nonlinear arithmeticpolyrith— Polynomial arithmetic search
Structural Tactics
rcases/obtain— Pattern matchingpush_neg— Push negations inwardcontrapose— Contrapositive reasoningext— Extensionalitycongr— Congruence
Getting Started
1-- Quick setup with all Mathlib tactics:2import Mathlib.Tactic34-- Now you can use ring, linarith, and more!5example (a b : ℤ) : (a - b) * (a + b) = a^2 - b^2 := by ringimport Mathlib.Tactic to get all tactics. Later, you can import only what you need for faster compilation.Import Mathlib tactics and use ring on a simple identity.
1import Mathlib.Tactic23example (x : Int) : (x + 1)^2 = x^2 + 2*x + 1 := by4 ring