|
| 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.Combinators |
| 8 | +import Lean.Meta.Tactic.Grind.Split |
| 9 | +import Lean.Meta.Tactic.Grind.EMatch |
| 10 | + |
| 11 | +namespace Lean.Meta.Grind |
| 12 | + |
| 13 | +namespace Solve |
| 14 | + |
| 15 | +structure State where |
| 16 | + todo : List Goal |
| 17 | + failures : List Goal := [] |
| 18 | + stop : Bool := false |
| 19 | + |
| 20 | +private abbrev M := StateRefT State GrindM |
| 21 | + |
| 22 | +def getNext? : M (Option Goal) := do |
| 23 | + let goal::todo := (← get).todo | return none |
| 24 | + modify fun s => { s with todo } |
| 25 | + return some goal |
| 26 | + |
| 27 | +def pushGoal (goal : Goal) : M Unit := |
| 28 | + modify fun s => { s with todo := goal :: s.todo } |
| 29 | + |
| 30 | +def pushGoals (goals : List Goal) : M Unit := |
| 31 | + modify fun s => { s with todo := goals ++ s.todo } |
| 32 | + |
| 33 | +def pushFailure (goal : Goal) : M Unit := do |
| 34 | + modify fun s => { s with failures := goal :: s.failures } |
| 35 | + if (← get).failures.length ≥ (← getConfig).failures then |
| 36 | + modify fun s => { s with stop := true } |
| 37 | + |
| 38 | +@[inline] def stepGuard (x : Goal → M Bool) (goal : Goal) : M Bool := do |
| 39 | + try |
| 40 | + x goal |
| 41 | + catch ex => |
| 42 | + if ex.isMaxHeartbeat || ex.isMaxRecDepth then |
| 43 | + let goal ← goal.reportIssue ex.toMessageData |
| 44 | + pushFailure goal |
| 45 | + return true |
| 46 | + else |
| 47 | + throw ex |
| 48 | + |
| 49 | +def applyTac (x : GrindTactic) (goal : Goal) : M Bool := do |
| 50 | + let go (goal : Goal) : M Bool := do |
| 51 | + let some goals ← x goal | return false |
| 52 | + pushGoals goals |
| 53 | + return true |
| 54 | + stepGuard go goal |
| 55 | + |
| 56 | +def tryAssertNext : Goal → M Bool := applyTac assertNext |
| 57 | + |
| 58 | +def tryEmatch : Goal → M Bool := applyTac ematchAndAssert |
| 59 | + |
| 60 | +def trySplit : Goal → M Bool := applyTac splitNext |
| 61 | + |
| 62 | +def maxNumFailuresReached : M Bool := do |
| 63 | + return (← get).failures.length ≥ (← getConfig).failures |
| 64 | + |
| 65 | +partial def main : M Unit := do |
| 66 | + repeat do |
| 67 | + if (← get).stop then |
| 68 | + return () |
| 69 | + let some goal ← getNext? | |
| 70 | + return () |
| 71 | + if goal.inconsistent then |
| 72 | + continue |
| 73 | + if (← tryAssertNext goal) then |
| 74 | + continue |
| 75 | + if (← tryEmatch goal) then |
| 76 | + continue |
| 77 | + if (← trySplit goal) then |
| 78 | + continue |
| 79 | + pushFailure goal |
| 80 | + |
| 81 | +end Solve |
| 82 | + |
| 83 | +/-- |
| 84 | +Try to solve/close the given goals, and returns the ones that could not be solved. |
| 85 | +-/ |
| 86 | +def solve (goals : List Goal) : GrindM (List Goal) := do |
| 87 | + let (_, s) ← Solve.main.run { todo := goals } |
| 88 | + let todo ← s.todo.mapM fun goal => do |
| 89 | + goal.reportIssue m!"this goal was not fully processed due to previous failures, threshold: `(failures := {(← getConfig).failures})`" |
| 90 | + return s.failures.reverse ++ todo |
| 91 | + |
| 92 | +end Lean.Meta.Grind |
0 commit comments