Stage 06 · Step 26 of 50
A library of mathematics.
01The idea
A workshop is more useful with a well-stocked toolbox. Mathlib supplies a large collection of mathematical definitions, theorems, and tactics.
The ring tactic handles polynomial identities.
02In maths
For real a and b, (a + b)² = a² + 2ab + b².
Picture it
Swipe sideways to see the whole picture.
ring works like careful algebra homework: multiply out both sides, collect like terms, and compare. On the left, the table gives a·a + a·b + b·a + b·b, and since a·b and b·a are equal, that is a² + 2ab + b². The right side is already in that form, so the two match. This works for every real a and b, positive or negative.03In Lean
This code comes directly from the file checked by Lean.
module
import Mathlib.Tactic.Ring
import Mathlib.Basic.Real.Basic
example (a b : ℝ) : (a + b)^2 = a^2 + 2*a*b + b^2 := by
ring
Mathlib is a community mathematical library for Lean. An import loads a library module.
A module is a source-file unit. In this pinned Lean release, the module header enables the module system used by Mathlib.
The symbol ℝ denotes the real numbers; type it with \R. A polynomial combines variables using addition, multiplication, and natural-number powers.
04Step by step
Choose a line to see Lean’s recorded goal state after that line.
a b : ℝ ⊢ (a + b) ^ 2 = a ^ 2 + 2 * a * b + b ^ 2
After line 6: example (a b : ℝ) : (a + b)^2 = a^2 + 2*a*b + b^2 := by
a b : ℝ ⊢ (a + b) ^ 2 = a ^ 2 + 2 * a * b + b ^ 2
After line 7: ring
no goals
05Lean says
no goals
A goal is what remains to prove. The symbol ⊢ points to it. Meet the turnstile.
06A common slip
Lean is telling us something useful here.
module
import Mathlib.Tactic.Ring
import Mathlib.Basic.Real.Basic
example (a b : ℝ) : (a + b)^2 = a^2 + b^2 := by
ring
unsolved goals a b : ℝ ⊢ a * b * 2 + a ^ 2 + b ^ 2 = a ^ 2 + b ^ 2
The proposed identity omits the cross term.
The fix: Include twice a times b in the expansion.
07Your turn
sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.
- Warm-up
Expand the square of x plus one.
Show a hint
This is a polynomial identity.
One more hint
The ring tactic can normalize both sides.
- A little stretch
Prove the difference-of-squares identity.
Show a hint
Multiplication distributes over addition and subtraction.
One more hint
Use ring to check the polynomial equality.
module
import Mathlib.Tactic.Ring
import Mathlib.Basic.Real.Basic
example (x : ℝ) : (x + 1)^2 = x^2 + 2*x + 1 := by
sorry
example (x y : ℝ) : (x + y) * (x - y) = x^2 - y^2 := by
sorry
Compare with the checked solutions
There are often several valid proofs. These solutions were compiled with the same pinned Lean version.
module
import Mathlib.Tactic.Ring
import Mathlib.Basic.Real.Basic
example (x : ℝ) : (x + 1)^2 = x^2 + 2*x + 1 := by
ring
example (x y : ℝ) : (x + y) * (x - y) = x^2 - y^2 := by
ring
no goals
08A quick check
09New words & tools
Take your time. Progress is yours to mark.