ring
Automatically solve polynomial ring equations. The ring tactic handles algebraic manipulations that would otherwise require many manual steps.
❗
ring requires Mathlib. Add import Mathlib.Tactic.Ring to use this tactic.Basic Usage
lean
1import Mathlib.Tactic.Ring23-- ring solves polynomial equalities4example (x y : Int) : (x + y) * (x - y) = x^2 - y^2 := by ring56example (a b c : Int) : (a + b + c)^2 = a^2 + b^2 + c^2 + 2*a*b + 2*b*c + 2*a*c := by ring78example (x : Int) : (x + 1)^3 = x^3 + 3*x^2 + 3*x + 1 := by ringKey Takeaway
ring proves equalities that follow from ring axioms: associativity, commutativity, distributivity, and the properties of 0 and 1.What ring Handles
lean
1import Mathlib.Tactic.Ring23-- Expansion4example (x : Int) : (x + 1) * (x + 2) = x^2 + 3*x + 2 := by ring56-- Factoring (via equality)7example (x : Int) : x^2 - 1 = (x + 1) * (x - 1) := by ring89-- Combining like terms10example (a b : Int) : 3*a + 2*b + a - b = 4*a + b := by ring1112-- Nested expressions13example (x y z : Int) : (x + y) * z + x * (y + z) = x*y + 2*x*z + y*z := by ringWorks on Different Types
lean
1import Mathlib.Tactic.Ring23-- Natural numbers4example (n m : Nat) : (n + m) * (n + m) = n^2 + 2*n*m + m^2 := by ring56-- Integers7example (x : Int) : x * (-1) = -x := by ring89-- Rationals10example (p q : Rat) : (p + q) / 2 + (p - q) / 2 = p := by ring1112-- Reals (with Mathlib)13-- example (r s : ℝ) : (r + s)^2 - (r - s)^2 = 4*r*s := by ringring_nf for Normalization
lean
1import Mathlib.Tactic.Ring23-- ring_nf normalizes expressions without closing the goal4example (x y : Int) (h : x^2 + 2*x*y + y^2 = 0) : (x + y)^2 = 0 := by5 ring_nf -- Transforms goal to: x^2 + 2*x*y + y^2 = 06 exact h78-- Useful for rewriting to a canonical form9example (a b : Int) : (a - b) * (a - b) = (b - a) * (b - a) := by ringDeep Dive: How ring Works
The ring tactic works by:
- Converting both sides to a canonical polynomial form
- Expanding all multiplications and powers
- Collecting like terms
- Comparing the resulting normal forms
If the normal forms are identical, the equality holds by ring axioms. This makes it a decision procedure for polynomial ring equations.
Limitations
lean
1import Mathlib.Tactic.Ring23-- ring doesn't handle:45-- Division (use field_simp first)6-- example (x : Rat) (h : x ≠ 0) : x / x = 1 := by ring -- Fails!78-- Inequalities (use linarith or nlinarith)9-- example (x : Int) : x^2 ≥ 0 := by ring -- Fails!1011-- Functions or predicates12-- example (f : Int → Int) : f (x + y) = f (x + y) := by ring -- Fails!1314-- ring only handles polynomial expressions💡
For expressions with division, use
field_simpfirst to clear denominators, then ring.Real-World Examples
Proving Algebraic Identities
lean
1import Mathlib.Tactic.Ring23-- Sophie Germain identity4theorem sophie_germain (a b : Int) : 5 a^4 + 4*b^4 = (a^2 + 2*b^2 + 2*a*b) * (a^2 + 2*b^2 - 2*a*b) := by6 ring78-- Difference of cubes9theorem diff_cubes (x y : Int) :10 x^3 - y^3 = (x - y) * (x^2 + x*y + y^2) := by11 ringSimplifying in Proofs
lean
1import Mathlib.Tactic.Ring23-- Using ring as part of a larger proof4theorem sum_of_squares_formula (n : Nat) (h : 6 * s = n * (n + 1) * (2*n + 1)) :5 6 * s = 2*n^3 + 3*n^2 + n := by6 rw [h]7 ringCommon Patterns
lean
1import Mathlib.Tactic.Ring23-- Completing the square4example (x : Int) : x^2 + 6*x + 9 = (x + 3)^2 := by ring56-- Difference of squares7example (a b : Int) : a^2 - b^2 = (a + b) * (a - b) := by ring89-- Binomial theorem cases10example (x : Int) : (1 + x)^4 = 1 + 4*x + 6*x^2 + 4*x^3 + x^4 := by ring