|
| 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 Lean.Meta.Tactic.Grind.Canon |
| 8 | +import Lean.Meta.Tactic.Grind.MBTC |
| 9 | +import Lean.Meta.Tactic.Grind.Arith.Linear.Model |
| 10 | +import Lean.Meta.Tactic.Grind.Arith.Linear.PropagateEq |
| 11 | + |
| 12 | +namespace Lean.Meta.Grind.Arith.Linear |
| 13 | + |
| 14 | +private partial def toRatValue? (a : Expr) : Option Rat := |
| 15 | + if a.isAppOfArity ``Zero.zero 2 then |
| 16 | + some 0 |
| 17 | + else if a.isAppOfArity ``One.one 2 then |
| 18 | + some 1 |
| 19 | + else if a.isAppOfArity ``OfNat.ofNat 3 then |
| 20 | + toRatValue? (a.getArg! 1) |
| 21 | + else if a.isAppOfArity ``Neg.neg 3 then |
| 22 | + (- ·) <$> toRatValue? a.appArg! |
| 23 | + else if a.isAppOfArity ``HDiv.hDiv 6 then |
| 24 | + (· / ·) <$> toRatValue? a.appFn!.appArg! <*> toRatValue? a.appArg! |
| 25 | + else if let .lit (.natVal n) := a then |
| 26 | + some (Std.Internal.mkRat n 1) |
| 27 | + else |
| 28 | + none |
| 29 | + |
| 30 | +private def getAssignmentExt? (s : Struct) (a : Expr) : Option Rat := do |
| 31 | + if let some v := getAssignment? s a then |
| 32 | + some v |
| 33 | + else |
| 34 | + toRatValue? a |
| 35 | + |
| 36 | +private def hasTheoryVar (e : Expr) : GoalM Bool := do |
| 37 | + return (← getRootENode e).linarith?.isSome || (toRatValue? e).isSome |
| 38 | + |
| 39 | +private def isInterpreted (e : Expr) : GoalM Bool := do |
| 40 | + if isInterpretedTerm e then return true |
| 41 | + let f := e.getAppFn |
| 42 | + return f.isConstOf ``LE.le || f.isConstOf ``LT.lt || f.isConstOf ``Dvd.dvd |
| 43 | + |
| 44 | +private def eqAssignment (a b : Expr) : GoalM Bool := do |
| 45 | + let structId₁? := (← get).arith.linear.exprToStructId.find? { expr := a } |
| 46 | + let structId₂? := (← get).arith.linear.exprToStructId.find? { expr := b } |
| 47 | + let some structId := structId₁? <|> structId₂? | return false |
| 48 | + let s := (← get).arith.linear.structs[structId]! |
| 49 | + -- It is pointless to generate case-splits unless we have support for disequality. |
| 50 | + unless s.linearInst?.isSome do return false |
| 51 | + let some v₁ := getAssignmentExt? s a | return false |
| 52 | + let some v₂ := getAssignmentExt? s b | return false |
| 53 | + return v₁ == v₂ |
| 54 | + |
| 55 | +def mbtc : GoalM Bool := do |
| 56 | + Grind.mbtc { |
| 57 | + hasTheoryVar := hasTheoryVar |
| 58 | + isInterpreted := isInterpreted |
| 59 | + eqAssignment := eqAssignment |
| 60 | + } |
| 61 | + |
| 62 | +end Lean.Meta.Grind.Arith.Linear |
0 commit comments