Skip to content

Commit 6469890

Browse files
authored
fix: apply ring normalizer to equalities coming from grind to core to lia (#11613)
This PR ensures we apply the ring normalizer to equalities being propagated from the `grind` core module to `grind lia`. It also ensures we use the safe/managed polynomial functions when normalizing. Closes #11539
1 parent 37f9984 commit 6469890

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

src/Lean/Meta/Tactic/Grind/Arith/Cutsat/CommRing.lean

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Lean.Meta.Tactic.Grind.Arith.Cutsat.Util
1313
import Lean.Meta.Tactic.Grind.Arith.Cutsat.Var
1414
import Lean.Meta.Tactic.Grind.Arith.CommRing.Reify
1515
import Lean.Meta.Tactic.Grind.Arith.CommRing.DenoteExpr
16+
import Lean.Meta.Tactic.Grind.Arith.CommRing.SafePoly
1617
public section
1718
namespace Lean.Meta.Grind.Arith.Cutsat
1819
/-!
@@ -22,7 +23,7 @@ CommRing interface for cutsat. We use it to normalize nonlinear polynomials.
2223
/-- Returns `true` if `p` contains a nonlinear monomial. -/
2324
def _root_.Int.Linear.Poly.isNonlinear (p : Poly) : GoalM Bool := do
2425
let .add _ x p := p | return false
25-
if (← getVar x).isAppOf ``HMul.hMul then return true
26+
if (← getVar x).isAppOf ``HMul.hMul || (← getVar x).isAppOf ``HPow.hPow then return true
2627
p.isNonlinear
2728

2829
def _root_.Int.Linear.Poly.getGeneration (p : Poly) : GoalM Nat := do
@@ -46,7 +47,7 @@ def _root_.Int.Linear.Poly.normCommRing? (p : Poly) : GoalM (Option (CommRing.Ri
4647
let e ← shareCommon (← canon e)
4748
let gen ← p.getGeneration
4849
let some re ← CommRing.reify? e (gen := gen) | return none
49-
let p' := re.toPoly
50+
let some p' re.toPolyM? | return none
5051
let e' ← p'.denoteExpr
5152
let e' ← preprocessLight e'
5253
-- Remark: we are reusing the `IntModule` virtual parent.

src/Lean/Meta/Tactic/Grind/Arith/Cutsat/EqCnstr.lean

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,6 @@ private def exprAsPoly (a : Expr) : GoalM Poly := do
386386
else
387387
throwError "internal `grind` error, expression is not relevant to cutsat{indentExpr a}"
388388

389-
private def processNewIntEq (a b : Expr) : GoalM Unit := do
390-
let p₁ ← exprAsPoly a
391-
let p₂ ← exprAsPoly b
392-
-- Remark: we don't need to use the comm ring normalizer here because `p` is always linear.
393-
let p := p₁.combine (p₂.mul (-1))
394-
{ p, h := .core a b p₁ p₂ : EqCnstr }.assert
395-
396389
/-- Asserts a constraint coming from the core. -/
397390
private def EqCnstr.assertCore (c : EqCnstr) : GoalM Unit := do
398391
if let some (re, rp, p) ← c.p.normCommRing? then
@@ -401,6 +394,14 @@ private def EqCnstr.assertCore (c : EqCnstr) : GoalM Unit := do
401394
else
402395
c.assert
403396

397+
private def processNewIntEq (a b : Expr) : GoalM Unit := do
398+
let p₁ ← exprAsPoly a
399+
let p₂ ← exprAsPoly b
400+
-- Remark: we don't need to use the comm ring normalizer here because `p` is always linear.
401+
let p := p₁.combine (p₂.mul (-1))
402+
let c := { p, h := .core a b p₁ p₂ : EqCnstr }
403+
c.assertCore
404+
404405
/--
405406
Similar to `natToInt`, but checks first whether the term has already been internalized.
406407
-/

tests/lean/run/grind_11539.lean

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
example {n a b : Nat}
2+
(this : (b : Int) ^ (n + 1) + ↑(n + 1) * ↑b ^ (n + 1 - 1) * (↑a - ↑b) ≤
3+
(↑b + (↑a - ↑b)) ^ (n + 1)) :
4+
(n + 1) * a * b ^ n ≤ a ^ (n + 1) + n * b * b ^ n := by
5+
grind (splits := 0)
6+
7+
example {n a b : Nat}
8+
(this : (b : Int) ^ (n + 1) + ↑(n + 1) * ↑b ^ (n + 1 - 1) * (↑a - ↑b) ≤
9+
(↑b + (↑a - ↑b)) ^ (n + 1)) :
10+
(n + 1) * a * b ^ n ≤ a ^ (n + 1) + n * b * b ^ n := by
11+
grind

0 commit comments

Comments
 (0)