|
| 1 | +/- |
| 2 | +Copyright (c) 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Leonardo de Moura |
| 5 | +-/ |
| 6 | +prelude |
| 7 | +import Init.Grind.CommRing.Poly |
| 8 | +import Lean.Meta.Tactic.Grind.Arith.CommRing.Reify |
| 9 | +import Lean.Meta.Tactic.Grind.Arith.CommRing.DenoteExpr |
| 10 | +import Lean.Meta.Tactic.Grind.Arith.Linear.Var |
| 11 | +import Lean.Meta.Tactic.Grind.Arith.Linear.StructId |
| 12 | +import Lean.Meta.Tactic.Grind.Arith.Linear.Reify |
| 13 | +import Lean.Meta.Tactic.Grind.Arith.Linear.IneqCnstr |
| 14 | +import Lean.Meta.Tactic.Grind.Arith.Linear.DenoteExpr |
| 15 | +import Lean.Meta.Tactic.Grind.Arith.Linear.Proof |
| 16 | + |
| 17 | +namespace Lean.Meta.Grind.Arith.Linear |
| 18 | +/-- Returns `some structId` if `a` and `b` are elements of the same structure. -/ |
| 19 | +private def inSameStruct? (a b : Expr) : GoalM (Option Nat) := do |
| 20 | + let some structId ← getTermStructId? a | return none |
| 21 | + let some structId' ← getTermStructId? b | return none |
| 22 | + unless structId == structId' do return none -- This can happen when we have heterogeneous equalities |
| 23 | + return structId |
| 24 | + |
| 25 | +private def processNewCommRingEq (a b : Expr) : LinearM Unit := do |
| 26 | + let some lhs ← withRingM <| CommRing.reify? a (skipVar := false) | return () |
| 27 | + let some rhs ← withRingM <| CommRing.reify? b (skipVar := false) | return () |
| 28 | + let p' := (lhs.sub rhs).toPoly |
| 29 | + let lhs' ← p'.denoteAsIntModuleExpr |
| 30 | + let some lhs' ← reify? lhs' (skipVar := false) | return () |
| 31 | + let p := lhs'.norm |
| 32 | + if p == .nil then return () |
| 33 | + let c₁ : IneqCnstr := { p, strict := false, h := .ofCommRingEq a b lhs rhs p' lhs' } |
| 34 | + c₁.assert |
| 35 | + let p := p.mul (-1) |
| 36 | + let p' := p'.mulConst (-1) |
| 37 | + let lhs' ← p'.denoteAsIntModuleExpr |
| 38 | + let some lhs' ← reify? lhs' (skipVar := false) | return () |
| 39 | + let c₂ : IneqCnstr := { p, strict := false, h := .ofCommRingEq b a rhs lhs p' lhs' } |
| 40 | + c₂.assert |
| 41 | + |
| 42 | +private def processNewIntModuleEq (a b : Expr) : LinearM Unit := do |
| 43 | + let some lhs ← reify? a (skipVar := false) | return () |
| 44 | + let some rhs ← reify? b (skipVar := false) | return () |
| 45 | + let p := (lhs.sub rhs).norm |
| 46 | + if p == .nil then return () |
| 47 | + let c₁ : IneqCnstr := { p, strict := false, h := .ofEq a b lhs rhs } |
| 48 | + c₁.assert |
| 49 | + let p := p.mul (-1) |
| 50 | + let c₂ : IneqCnstr := { p, strict := false, h := .ofEq b a rhs lhs } |
| 51 | + c₂.assert |
| 52 | + |
| 53 | +@[export lean_process_linarith_eq] |
| 54 | +def processNewEqImpl (a b : Expr) : GoalM Unit := do |
| 55 | + if isSameExpr a b then return () -- TODO: check why this is needed |
| 56 | + let some structId ← inSameStruct? a b | return () |
| 57 | + LinearM.run structId do |
| 58 | + trace_goal[grind.linarith.assert] "{← mkEq a b}" |
| 59 | + if (← isCommRing) then |
| 60 | + processNewCommRingEq a b |
| 61 | + else |
| 62 | + processNewIntModuleEq a b |
| 63 | + |
| 64 | +@[export lean_process_linarith_diseq] |
| 65 | +def processNewDiseqImpl (a b : Expr) : GoalM Unit := do |
| 66 | + trace[grind.linarith.assert] "{a} ≠ {b}" |
| 67 | + -- TODO |
| 68 | + |
| 69 | +end Lean.Meta.Grind.Arith.Linear |
0 commit comments