Skip to content

Commit

Permalink
feat: check invariant for parents
Browse files Browse the repository at this point in the history
  • Loading branch information
leodemoura committed Dec 23, 2024
1 parent 169d7a7 commit a43888f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Lean/Meta/Tactic/Grind/Core.lean
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ Remove `root` parents from the congruence table.
This is an auxiliary function performed while merging equivalence classes.
-/
private def removeParents (root : Expr) : GoalM ParentSet := do
let occsgetParents root
for parent in occs do
let parentsgetParentsAndReset root
for parent in parents do
modify fun s => { s with congrTable := s.congrTable.erase { e := parent } }
return occs
return parents

/--
Reinsert parents into the congruence table and detect new equalities.
Expand Down
15 changes: 15 additions & 0 deletions src/Lean/Meta/Tactic/Grind/Inv.lean
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,27 @@ private def checkEqc (root : ENode) : GoalM Unit := do
-- 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

Expand Down
14 changes: 10 additions & 4 deletions src/Lean/Meta/Tactic/Grind/Types.lean
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,16 @@ Returns the set of expressions `e` is a child of, or an expression in
The information is only up to date if `e` is the root (aka canonical representative) of the equivalence class.
-/
def getParents (e : Expr) : GoalM ParentSet := do
if let some occs := (← get).parents.find? (toENodeKey e) then
return occs
else
return {}
let some parents := (← get).parents.find? (toENodeKey e) | return {}
return parents

/--
Similar to `getParents`, but also removes the entry `e ↦ parents` from the parent map.
-/
def getParentsAndReset (e : Expr) : GoalM ParentSet := do
let parents ← getParents e
modify fun s => { s with parents := s.parents.erase (toENodeKey e) }
return parents

/--
Copy `parents` to the parents of `root`.
Expand Down

0 comments on commit a43888f

Please sign in to comment.