Stage 06 · Step 26 of 50

A library of mathematics.

Needs Mathlib12 minutesLean 4.36.0

01The idea

A workshop is more useful with a well-stocked toolbox. Mathlib supplies a large collection of mathematical definitions, theorems, and tactics.

Tacto · Your guide for this idea
Tacto pointing

The ring tactic handles polynomial identities.

02In maths

For real a and b, (a + b)² = a² + 2ab + b².

Picture it

ring: multiply out both sides, then compareTwo cards show the two sides of the goal: (a + b) ^ 2 on the left and a ^ 2 + 2 * a * b + b ^ 2 on the right. Under the left side, (a + b)(a + b) is multiplied out in a two by two table with entries a·a, a·b, b·a and b·b. Under the right side, the terms are already multiplied out: a², 2ab and b², and 2 * a * b counts a·b twice. Arrows from both sides meet in a green box: both sides become the same terms, a² + 2ab + b². The ring tactic sees they match and closes the goal.LEFT SIDE(a + b) ^ 2=RIGHT SIDEa ^ 2 + 2 * a * b + b ^ 2multiply out (a + b)(a + b)ababa·aa·bb·ab·balready multiplied outa²2abb²++2 * a * b counts a·b twiceboth sides: a² + 2ab + b²ring

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.

E26.leanTry it live (opens official Lean editor)
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.

After line 6
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

Captured from Lean 4.36.0
no goals

A goal is what remains to prove. The symbol ⊢ points to it. Meet the turnstile.

07Your turn

sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.

  1. Warm-up

    Expand the square of x plus one.

  2. A little stretch

    Prove the difference-of-squares identity.

Exercises/E26.leanTry it live (opens official Lean editor)
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

08A quick check

1. What is Mathlib?

Read the answer and explanation

A mathematical library for Lean. It adds reusable definitions, theorems, and tactics.

2. What kind of goal is ring designed for?

Read the answer and explanation

Polynomial identities. It normalizes polynomial expressions in supported algebraic structures.

One more idea in your toolkit.

Take your time. Progress is yours to mark.