-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: detect congruent terms in
grind
(#6437)
This PR adds support for detecting congruent terms in the (WIP) `grind` tactic. It also introduces the `grind.debug` option, which, when set to `true`, checks many invariants after each equivalence class is merged. This option is intended solely for debugging purposes.
- Loading branch information
1 parent
5240405
commit b18f3a3
Showing
8 changed files
with
206 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/- | ||
Copyright (c) 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
Released under Apache 2.0 license as described in the file LICENSE. | ||
Authors: Leonardo de Moura | ||
-/ | ||
prelude | ||
import Lean.Meta.Tactic.Grind.Types | ||
|
||
namespace Lean.Meta.Grind | ||
|
||
/-! | ||
Debugging support code for checking basic invariants. | ||
-/ | ||
|
||
register_builtin_option grind.debug : Bool := { | ||
defValue := false | ||
group := "debug" | ||
descr := "check invariants after updates" | ||
} | ||
|
||
private def checkEqc (root : ENode) : GoalM Unit := do | ||
let mut size := 0 | ||
let mut curr := root.self | ||
repeat | ||
size := size + 1 | ||
-- The root of `curr` must be `root` | ||
assert! isSameExpr (← getRoot curr) root.self | ||
-- Starting at `curr`, following the `target?` field leads to `root`. | ||
let mut n := curr | ||
repeat | ||
if let some target ← getTarget? n then | ||
n := target | ||
else | ||
break | ||
assert! isSameExpr n root.self | ||
-- Go to next element | ||
curr ← getNext curr | ||
if isSameExpr root.self curr then | ||
break | ||
-- The size of the equivalence class is correct. | ||
assert! root.size == size | ||
|
||
private def checkParents (e : Expr) : GoalM Unit := do | ||
if (← isRoot e) then | ||
for parent in (← getParents e) do | ||
let mut found := false | ||
-- There is an argument `arg` s.t. root of `arg` is `e`. | ||
for arg in parent.getAppArgs do | ||
if isSameExpr (← getRoot arg) e then | ||
found := true | ||
break | ||
assert! found | ||
else | ||
-- All the parents are stored in the root of the equivalence class. | ||
assert! (← getParents e).isEmpty | ||
|
||
/-- | ||
Check basic invariants if `grind.debug` is enabled. | ||
-/ | ||
def checkInvariants : GoalM Unit := do | ||
if grind.debug.get (← getOptions) then | ||
for (_, node) in (← get).enodes do | ||
checkParents node.self | ||
if isSameExpr node.self node.root then | ||
checkEqc node | ||
|
||
end Lean.Meta.Grind |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters