nlinarith
Extend linarith to handle nonlinear arithmetic.nlinarith can prove goals involving products and powers of variables.
❗
nlinarith requires Mathlib. Add import Mathlib.Tactic.Linarith to use this tactic.Basic Usage
lean
1import Mathlib.Tactic.Linarith23-- nlinarith handles products of variables4example (x y : Int) (hx : x ≥ 0) (hy : y ≥ 0) : x * y ≥ 0 := by5 nlinarith [sq_nonneg x, sq_nonneg y, sq_nonneg (x + y), sq_nonneg (x - y)]67-- Squares are non-negative8example (x : Int) : x * x ≥ 0 := by9 nlinarith [sq_nonneg x]Key Takeaway
nlinarith works by adding products of hypotheses to create new linear constraints that linarith can use.When to Use nlinarith
lean
1import Mathlib.Tactic.Linarith23-- Use nlinarith when goals involve:4-- 1. Products of variables5example (a b : Int) (ha : a ≥ 1) (hb : b ≥ 1) : a * b ≥ 1 := by6 nlinarith [sq_nonneg a, sq_nonneg b, sq_nonneg (a-1), sq_nonneg (b-1)]78-- 2. Squares or powers9example (x : Int) (h : x ≥ 2) : x * x ≥ 4 := by10 nlinarith [sq_nonneg x, sq_nonneg (x - 2)]1112-- 3. Bounds that multiply13example (x y : Int) (hx : 0 ≤ x) (hx' : x ≤ 3) (hy : 0 ≤ y) (hy' : y ≤ 4) : 14 x * y ≤ 12 := by15 nlinarith [sq_nonneg x, sq_nonneg y, sq_nonneg (3-x), sq_nonneg (4-y)]Providing Hints
lean
1import Mathlib.Tactic.Linarith23-- nlinarith often needs hints about non-negativity4example (a b : Int) (ha : a ≥ 0) (hb : b ≥ 0) (h : a + b = 0) : a = 0 := by5 nlinarith [sq_nonneg a, sq_nonneg b]67-- Common hint: sq_nonneg for any expression8-- sq_nonneg x proves x^2 ≥ 0910example (x y : Int) : (x - y)^2 ≥ 0 := sq_nonneg (x - y)11-- This is: x^2 - 2*x*y + y^2 ≥ 0💡
When
nlinarith fails, try addingsq_nonneg hints for relevant expressions like sq_nonneg x or sq_nonneg (x - y).AM-GM Style Proofs
lean
1import Mathlib.Tactic.Linarith23-- Prove (a + b)^2 ≥ 4ab from (a - b)^2 ≥ 04example (a b : Int) : (a + b)^2 ≥ 4 * a * b := by5 have h : (a - b)^2 ≥ 0 := sq_nonneg (a - b)6 -- (a-b)^2 = a^2 - 2ab + b^2 ≥ 07 -- So a^2 + b^2 ≥ 2ab8 -- Thus (a+b)^2 = a^2 + 2ab + b^2 = (a^2+b^2) + 2ab ≥ 2ab + 2ab = 4ab9 nlinarith [sq_nonneg a, sq_nonneg b]Deep Dive: How nlinarith Works
nlinarith extends linarith by:
- Taking products of pairs of hypotheses
- Adding these products as new linear constraints
- Running
linarithwith the expanded set
For example, if you have h1 : a ≥ 0 andh2 : b ≥ 0, nlinarith might generatea * b ≥ 0 from their product.
Comparison: linarith vs nlinarith
lean
1import Mathlib.Tactic.Linarith23-- linarith: linear only4example (x y : Int) (h1 : x + y ≤ 10) (h2 : x ≥ 3) : y ≤ 7 := by5 linarith -- Works!67-- nlinarith: handles nonlinear8example (x : Int) (h : x ≥ 3) : x * x ≥ 9 := by9 -- linarith -- Fails10 nlinarith [sq_nonneg x, sq_nonneg (x - 3)] -- Works!1112-- nlinarith is slower, use linarith when possibleReal-World Examples
Quadratic Bounds
lean
1import Mathlib.Tactic.Linarith23-- Prove bounds on quadratic expressions4theorem quadratic_lower_bound (x : Int) (h : x ≥ 1) : 5 x^2 + x ≥ 2 := by6 nlinarith [sq_nonneg x, sq_nonneg (x - 1)]Product Bounds
lean
1import Mathlib.Tactic.Linarith23-- Bounded inputs give bounded outputs4theorem product_bound (x y : Int) 5 (hx1 : 1 ≤ x) (hx2 : x ≤ 10)6 (hy1 : 1 ≤ y) (hy2 : y ≤ 10) :7 1 ≤ x * y ∧ x * y ≤ 100 := by8 constructor9 · nlinarith [sq_nonneg x, sq_nonneg y, sq_nonneg (x-1), sq_nonneg (y-1)]10 · nlinarith [sq_nonneg (10-x), sq_nonneg (10-y)]Cauchy-Schwarz Style
lean
1import Mathlib.Tactic.Linarith23-- (a^2 + b^2)(c^2 + d^2) ≥ (ac + bd)^24-- This is Cauchy-Schwarz for 2D vectors5theorem cauchy_schwarz_2d (a b c d : Int) : 6 (a^2 + b^2) * (c^2 + d^2) ≥ (a*c + b*d)^2 := by7 have h : (a*d - b*c)^2 ≥ 0 := sq_nonneg (a*d - b*c)8 nlinarith [sq_nonneg a, sq_nonneg b, sq_nonneg c, sq_nonneg d]Troubleshooting
lean
1import Mathlib.Tactic.Linarith23-- When nlinarith fails:45-- 1. Add more sq_nonneg hints6example (x : Int) (h : x ≥ 5) : x^2 ≥ 25 := by7 nlinarith [sq_nonneg x, sq_nonneg (x - 5)]89-- 2. Break into cases10-- 3. Use other tactics (polyrith, positivity)11-- 4. Prove intermediate lemmasℹ
nlinarith is incomplete—it can't prove all true nonlinear statements. For harder goals, try polyrithor manual proofs.