Introduction to Mathlib
Mathlib is the mathematical library for Lean 4. It provides a vast collection of definitions, theorems, and powerful tactics that extend Lean's capabilities.
❗
The tactics in this module require Mathlib4. They are not part of core Lean 4 and must be imported from the Mathlib library.
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
lean
1-- In your lakefile.lean, add Mathlib as a dependency:2require mathlib from git3 "https://github.com/leanprover-community/mathlib4"45-- Then in your Lean files:6import Mathlib.Tactic -- Import all tactics78-- Or import specific modules:9import Mathlib.Tactic.Ring10import Mathlib.Tactic.Linarith11import Mathlib.Tactic.FieldSimpKey Takeaway
Mathlib tactics must be imported before use. The import
Mathlib.Tactic brings in all common tactics.Core Lean 4 vs Mathlib Tactics
lean
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
lean
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
lean
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
lean
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
lean
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 ring💡
Start with
import Mathlib.Tactic to get all tactics. Later, you can import only what you need for faster compilation.Exercise: Import and Use ring
Import Mathlib tactics and use ring on a simple identity.
lean
1import Mathlib.Tactic23example (x : Int) : (x + 1)^2 = x^2 + 2*x + 1 := by4 ring