ring — Polynomial Equations
Automatically prove polynomial ring equations. The ring tactic proves identities in commutative rings, such as expanding a product, which would otherwise take many rewriting steps.
ring requires Mathlib. Add import Mathlib.Tactic.Ring to use this tactic.The Problem ring Solves
Imagine proving (x + y)² = x² + 2xy + y² by hand. You'd need to apply distributivity, commutativity, and associativity multiple times. Thering tactic does all of this automatically:
1import Mathlib.Tactic.Ring23-- Without ring, you'd need many steps:4-- (x + y) * (x + y)5-- = x * (x + y) + y * (x + y) -- distributivity6-- = x*x + x*y + y*x + y*y -- distributivity again7-- = x² + x*y + x*y + y² -- commutativity8-- = x² + 2*x*y + y² -- combining like terms910-- With ring: one tactic, done11example (x y : Int) : (x + y)^2 = x^2 + 2*x*y + y^2 := by ringBasic Usage
The ring tactic proves equalities between polynomial expressions by expanding both sides to a canonical form and comparing. It handles the tedious algebraic manipulation automatically.
1import Mathlib.Tactic.Ring23-- Expansion and simplification4example (x y : Int) : (x + y) * (x - y) = x^2 - y^2 := by ring56-- Multinomial expansion7example (a b c : Int) : (a + b + c)^2 = a^2 + b^2 + c^2 + 2*a*b + 2*b*c + 2*a*c := by ring89-- Cubes10example (x : Int) : (x + 1)^3 = x^3 + 3*x^2 + 3*x + 1 := by ring1112-- Combining like terms13example (a b : Int) : 3*a + 2*b + a - b + 5*a = 9*a + b := by ringring proves equalities that follow from ring axioms: associativity, commutativity, distributivity, and the properties of 0 and 1. It works by reducing both sides to a canonical form.Understanding Ring Axioms
A ring is an algebraic structure with two operations (+ and ×) satisfying these properties:
- Addition is commutative: a + b = b + a
- Addition is associative: (a + b) + c = a + (b + c)
- Additive identity: a + 0 = a
- Additive inverse: a + (-a) = 0
- Multiplication is associative: (a × b) × c = a × (b × c)
- Multiplicative identity: a × 1 = a
- Distributivity: a × (b + c) = a × b + a × c
The ring tactic proves any equality that follows from these axioms. It doesn't need to know specific values—it works purely symbolically.
What ring Handles
ring works with expressions built from:
- Addition (+) and subtraction (-)
- Multiplication (*)
- Exponentiation by natural numbers (^)
- Negation (-x)
- Numeric constants
1import Mathlib.Tactic.Ring23-- Product expansion4example (x : Int) : (x + 1) * (x + 2) = x^2 + 3*x + 2 := by ring56-- Factoring (proving the expansion equals the factored form)7example (x : Int) : x^2 - 1 = (x + 1) * (x - 1) := by ring89-- Collecting 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 ring1415-- Powers16example (x : Int) : x^4 - 1 = (x - 1) * (x + 1) * (x^2 + 1) := by ringWorks on Different Types
ring works on any type that forms a ring (or commutative ring). The standard library provides instances for common numeric types:
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 ring8example (a b : Int) : (a - b) * (a + b) = a^2 - b^2 := by ring910-- Rationals11example (p q : Rat) : (p + q) / 2 + (p - q) / 2 = p := by ring1213-- With Mathlib, also works on:14-- ℝ (reals), ℂ (complex), ℚ (rationals), polynomial rings, etc.ring_nf: Normalization Without Closing
Sometimes you want to simplify an expression to canonical form without trying to close the goal. Use ring_nf for this:
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-- Normalize at a hypothesis9example (x y : Int) (h : (x + y)^2 = 9) : x^2 + 2*x*y + y^2 = 9 := by10 ring_nf at h -- h becomes: x^2 + 2*x*y + y^2 = 911 exact h1213-- Useful when ring doesn't quite close the goal14example (x : Int) (h : x^2 = 4) : (x + 1)^2 - 2*x - 1 = 4 := by15 ring_nf -- Simplifies to: x^2 = 416 exact hFamous Algebraic Identities
ring can prove many famous identities from algebra:
Difference of Powers
1import Mathlib.Tactic.Ring23-- Difference of squares4theorem diff_squares (a b : Int) : a^2 - b^2 = (a + b) * (a - b) := by ring56-- Difference of cubes7theorem diff_cubes (x y : Int) : 8 x^3 - y^3 = (x - y) * (x^2 + x*y + y^2) := by ring910-- Sum of cubes11theorem sum_cubes (x y : Int) : 12 x^3 + y^3 = (x + y) * (x^2 - x*y + y^2) := by ringSophie Germain Identity
1import Mathlib.Tactic.Ring23-- Sophie Germain's identity (discovered in early 1800s)4theorem 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 ringBinomial Expansions
1import Mathlib.Tactic.Ring23-- Binomial theorem cases4example (x : Int) : (1 + x)^2 = 1 + 2*x + x^2 := by ring5example (x : Int) : (1 + x)^3 = 1 + 3*x + 3*x^2 + x^3 := by ring6example (x : Int) : (1 + x)^4 = 1 + 4*x + 6*x^2 + 4*x^3 + x^4 := by ring78-- General pattern9example (a b : Int) : (a + b)^3 = a^3 + 3*a^2*b + 3*a*b^2 + b^3 := by ringLimitations
ring only handles polynomial equalities. It doesn't work with:
1import Mathlib.Tactic.Ring23-- ❌ Division (use field_simp first to clear denominators)4-- example (x : Rat) (h : x ≠ 0) : x / x = 1 := by ring -- Fails!56-- ✅ With field_simp:7example (x : Rat) (h : x ≠ 0) : x / x = 1 := by field_simp89-- ❌ Inequalities (use linarith or nlinarith)10-- example (x : Int) : x^2 ≥ 0 := by ring -- Fails!1112-- ✅ With nlinarith:13example (x : Int) : x^2 ≥ 0 := by nlinarith [sq_nonneg x]1415-- ❌ Functions or predicates16-- example (f : Int → Int) : f (x + y) = f (x + y) := by ring -- Fails!1718-- ✅ Use rfl instead:19example (f : Int → Int) (x y : Int) : f (x + y) = f (x + y) := rflfield_simpfirst to clear denominators, then ring.Deep Dive: How ring Works
The ring tactic works by:
- Parsing: Convert both sides of the equality to an internal polynomial representation
- Normalization: Expand all products, collect like terms, and sort monomials lexicographically
- Comparison: Check if the normalized forms are identical
- Certificate: If equal, construct a proof term witnessing the equality
This normalization-based approach is why ring is adecision procedure—it always terminates and can definitively say whether a polynomial equality holds.
ring vs Other Tactics
| Goal Type | Use |
|---|---|
| Polynomial equality | ring |
| Equality with division | field_simp then ring |
| Linear arithmetic (Nat/Int) | omega |
| Linear inequalities | linarith |
| Non-linear inequalities | nlinarith |
| Simple definitions | simp |
Common Patterns
Completing the Square
1import Mathlib.Tactic.Ring23-- Standard completing the square4example (x : Int) : x^2 + 6*x + 9 = (x + 3)^2 := by ring56-- General form7example (x a : Int) : x^2 + 2*a*x + a^2 = (x + a)^2 := by ring89-- More complex10example (x : Int) : x^2 + 4*x + 4 = (x + 2)^2 := by ringSimplifying in Larger Proofs
1import Mathlib.Tactic.Ring23-- Using ring as part of a larger proof4theorem sum_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 ring89-- Combining with other tactics10example (x y : Int) (h : x = y + 1) : x^2 = y^2 + 2*y + 1 := by11 rw [h]12 ringUse ring to prove this binomial expansion.
1import Mathlib.Tactic.Ring23example (x y : Int) : (x + y)^2 = x^2 + 2*x*y + y^2 := by4 ringProve the difference of squares formula.
1import Mathlib.Tactic.Ring23example (a b : Int) : (a + b) * (a - b) = a^2 - b^2 := by4 ringSimplify this expression to its simplest form.
1import Mathlib.Tactic.Ring23example (x : Int) : (x + 1)^2 - (x - 1)^2 = 4*x := by4 ring