Skip to content

Commit 00f6b1e

Browse files
authored
fix: denotation functions for interfacing CommRing and linarith (#8693)
This PR fixes the denotation functions used to interface the ring and linarith modules in grind.
1 parent 8422d93 commit 00f6b1e

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

src/Init/Grind/CommRing/Poly.lean

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,13 +1142,31 @@ end Stepwise
11421142

11431143
/-! IntModule interface -/
11441144

1145+
def Mon.denoteAsIntModule [CommRing α] (ctx : Context α) (m : Mon) : α :=
1146+
match m with
1147+
| .unit => One.one
1148+
| .mult pw m => go m (pw.denote ctx)
1149+
where
1150+
go (m : Mon) (acc : α) : α :=
1151+
match m with
1152+
| .unit => acc
1153+
| .mult pw m => go m (acc * pw.denote ctx)
1154+
11451155
def Poly.denoteAsIntModule [CommRing α] (ctx : Context α) (p : Poly) : α :=
11461156
match p with
11471157
| .num k => Int.cast k * One.one
1148-
| .add k m p => Int.cast k * m.denote ctx + denote ctx p
1158+
| .add k m p => Int.cast k * m.denoteAsIntModule ctx + denoteAsIntModule ctx p
1159+
1160+
theorem Mon.denoteAsIntModule_go_eq_denote {α} [CommRing α] (ctx : Context α) (m : Mon) (acc : α)
1161+
: denoteAsIntModule.go ctx m acc = acc * m.denote ctx := by
1162+
induction m generalizing acc <;> simp [*, denoteAsIntModule.go, denote, mul_one, One.one, *, mul_assoc]
1163+
1164+
theorem Mon.denoteAsIntModule_eq_denote {α} [CommRing α] (ctx : Context α) (m : Mon)
1165+
: m.denoteAsIntModule ctx = m.denote ctx := by
1166+
cases m <;> simp [denoteAsIntModule, denote, denoteAsIntModule_go_eq_denote]; rfl
11491167

11501168
theorem Poly.denoteAsIntModule_eq_denote {α} [CommRing α] (ctx : Context α) (p : Poly) : p.denoteAsIntModule ctx = p.denote ctx := by
1151-
induction p <;> simp [*, denoteAsIntModule, denote, mul_one, One.one]
1169+
induction p <;> simp [*, denoteAsIntModule, denote, mul_one, One.one, Mon.denoteAsIntModule_eq_denote]
11521170

11531171
open Stepwise
11541172

src/Lean/Meta/Tactic/Grind/Arith/Linear/DenoteExpr.lean

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,9 @@ def NotIneqCnstr.denoteExpr (c : NotIneqCnstr) : M Expr := do
6767
private def denoteNum (k : Int) : LinearM Expr := do
6868
return mkApp2 (← getStruct).hmulFn (mkIntLit k) (← getOne)
6969

70-
def _root_.Lean.Grind.CommRing.Power.denoteAsIntModuleExpr (pw : Grind.CommRing.Power) : LinearM Expr := do
71-
let x := (← getRing).vars[pw.x]!
72-
if pw.k == 1 then
73-
return x
74-
else
75-
return mkApp2 (← getRing).powFn x (toExpr pw.k)
76-
77-
def _root_.Lean.Grind.CommRing.Mon.denoteAsIntModuleExpr (m : Grind.CommRing.Mon) : LinearM Expr := do
78-
match m with
79-
| .unit => getOne
80-
| .mult pw m => go m (← pw.denoteAsIntModuleExpr)
81-
where
82-
go (m : Grind.CommRing.Mon) (acc : Expr) : LinearM Expr := do
83-
match m with
84-
| .unit => return acc
85-
| .mult pw m => go m (mkApp2 (← getRing).mulFn acc (← pw.denoteAsIntModuleExpr))
86-
8770
def _root_.Lean.Grind.CommRing.Poly.denoteAsIntModuleExpr (p : Grind.CommRing.Poly) : LinearM Expr := do
8871
match p with
8972
| .num k => denoteNum k
90-
| .add k m p => return mkApp2 (← getStruct).addFn (mkApp2 (← getRing).mulFn (mkIntLit k) (← m.denoteExpr)) (← denoteAsIntModuleExpr p)
73+
| .add k m p => return mkApp2 (← getStruct).addFn (mkApp2 (← getStruct).hmulFn (mkIntLit k) (← m.denoteExpr)) (← denoteAsIntModuleExpr p)
9174

9275
end Lean.Meta.Grind.Arith.Linear

tests/lean/run/grind_linarith_1.lean

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ example [IntModule α] [LinearOrder α] [IntModule.IsOrdered α] (a b c : α)
5252
: a ≤ b → b ≤ c → a ≤ c := by
5353
grind
5454

55-
example [IntModule α] [LinearOrder α] [IntModule.IsOrdered α] (a b c : α)
55+
example [IntModule α] [LinearOrder α] [IntModule.IsOrdered α] (a b c d : α)
56+
: a < b → b < c + d → a - d < c := by
57+
grind
58+
59+
example [CommRing α] [LinearOrder α] [Ring.IsOrdered α] (a b c d : α)
5660
: a < b → b < c + d → a - d < c := by
5761
grind
62+
63+
example [CommRing α] [Preorder α] [Ring.IsOrdered α] (a b c : α)
64+
: a < b → b < c → c < a → False := by
65+
grind

0 commit comments

Comments
 (0)