polyrith
Automatically find polynomial certificates for algebraic goals.polyrith uses external solvers to discover proofs that would be tedious to write by hand.
❗
polyrith requires Mathlib and an internet connection to query external polynomial solvers. Add import Mathlib.Tactic.Polyrith.Basic Usage
lean
1import Mathlib.Tactic.Polyrith23-- polyrith solves polynomial equations4example (x y : ℚ) (h1 : x + y = 3) (h2 : x - y = 1) : x = 2 := by5 polyrith67example (a b c : ℚ) (h1 : a + b + c = 0) (h2 : a*b + b*c + c*a = 0) : 8 a^2 + b^2 + c^2 = 0 := by9 polyrithKey Takeaway
polyrith finds polynomial combinations of hypotheses that prove the goal. It's like a super-poweredring that also uses your hypotheses.How polyrith Works
lean
1import Mathlib.Tactic.Polyrith23-- polyrith finds coefficients c₁, c₂, ... such that:4-- c₁ * h₁ + c₂ * h₂ + ... = goal56-- Example: Prove x = 2 from x + y = 3 and x - y = 17-- Solution: (1/2)(x + y - 3) + (1/2)(x - y - 1) = x - 28example (x y : ℚ) (h1 : x + y = 3) (h2 : x - y = 1) : x = 2 := by9 polyrith10 -- polyrith discovers: linear_combination (1/2) * h1 + (1/2) * h2polyrith Output
lean
1import Mathlib.Tactic.Polyrith23-- polyrith suggests a linear_combination proof4example (a b : ℚ) (h1 : a + b = 5) (h2 : a - b = 1) : a = 3 := by5 polyrith6 -- Try this: linear_combination (1/2) * h1 + (1/2) * h278-- You can use the suggestion directly:9example (a b : ℚ) (h1 : a + b = 5) (h2 : a - b = 1) : a = 3 := by10 linear_combination (1/2) * h1 + (1/2) * h2💡
Use
polyrith to discover proofs, then replace it with linear_combination for faster compilation (no network call).Polynomial Goals
lean
1import Mathlib.Tactic.Polyrith23-- polyrith handles genuinely polynomial (nonlinear) problems4example (x y : ℚ) (h1 : x^2 + y^2 = 1) (h2 : x + y = 1) : 5 x * y = 0 := by6 polyrith78-- It can find complex polynomial combinations9example (a b : ℚ) (h : a^2 = b^2) : (a - b) * (a + b) = 0 := by10 polyrithDeep Dive: The Polynomial Method
polyrith implements the polynomial method:
- Converts goal and hypotheses to polynomial equations
- Queries a Groebner basis solver or SOS solver
- The solver finds polynomial coefficients (if they exist)
- Constructs a Lean proof using these coefficients
This is complete for polynomial algebra: if a goal follows from hypotheses by polynomial reasoning, polyrith will find it.
linear_combination
lean
1import Mathlib.Tactic.LinearCombination23-- linear_combination applies a known combination4example (x y : ℚ) (h1 : 2*x + y = 7) (h2 : x + 3*y = 11) : 5 x = 2 := by6 linear_combination (3 * h1 - h2) / 578-- You specify the coefficients; it verifies and applies them9example (a b c : ℚ) (h1 : a = b + 1) (h2 : b = c + 1) : 10 a = c + 2 := by11 linear_combination h1 + h2When polyrith Helps
lean
1import Mathlib.Tactic.Polyrith23-- 1. Systems of equations4example (x y z : ℚ) 5 (h1 : x + y + z = 6) 6 (h2 : x + 2*y + 3*z = 14) 7 (h3 : x + 4*y + 9*z = 36) : 8 x = 1 := by9 polyrith1011-- 2. Derived algebraic facts12example (a b : ℚ) (h : a + b = 0) : a^3 + b^3 = -3*a*b*(a + b) := by13 polyrith -- Uses h to show a^3 + b^3 = 0 = -3ab*01415-- 3. Verification of formulas16example (x : ℚ) : (x + 1)^3 - (x - 1)^3 = 6*x^2 + 2 := by17 ring -- ring can do this too!Limitations
lean
1import Mathlib.Tactic.Polyrith23-- polyrith doesn't handle:45-- 1. Inequalities (use linarith, nlinarith)6-- example (x : ℚ) (h : x > 0) : x^2 > 0 := by polyrith -- Fails78-- 2. Non-polynomial functions9-- example (x : ℝ) : sin(x)^2 + cos(x)^2 = 1 := by polyrith -- Fails1011-- 3. May timeout on complex goals12-- 4. Requires network connectionReal-World Examples
Vieta's Formulas
lean
1import Mathlib.Tactic.Polyrith23-- If r, s are roots of x^2 - px + q = 04-- Then r + s = p and rs = q5theorem vieta (r s p q : ℚ) 6 (h1 : r^2 - p*r + q = 0) 7 (h2 : s^2 - p*s + q = 0)8 (h3 : r + s = p) : 9 r * s = q := by10 polyrithIdentity Verification
lean
1import Mathlib.Tactic.Polyrith23-- Sophie Germain identity with constraint4example (a b : ℚ) (h : a^4 + 4*b^4 = 0) (hb : b = 0) : a = 0 := by5 polyrith