blob: 4f1f995528b04ae6b19f2b48e36176824b5709f2 [file]
/-
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
-/
module
prelude
public import Lean.Meta.Tactic.Grind.Types
import Init.Grind.Util
import Lean.Meta.Tactic.Grind.Util
import Lean.Meta.Sym.Canon
namespace Lean.Meta.Grind
/-!
Debugging support code for checking basic invariants.
-/
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
-- Check congruence root. Literals are not in the congruence table: given `x = 1`, the term `-1`
-- is congruent to `-x`, but the satellite solvers are responsible for `-x = -1`.
if curr.isApp && (← getLitKind curr) matches .notLit then
if let some { e } := (← get).congrTable.find? { e := curr } then
if (← hasSameType e.getAppFn curr.getAppFn) then
assert! isSameExpr e (← getCongrRoot curr)
else
assert! (← isCongrRoot curr)
-- If the equivalence class does not have HEq proofs, then the types must be definitionally equal.
unless root.heqProofs do
assert! (← hasSameType 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
def checkChild (e : Expr) (child : Expr) : GoalM Bool := do
let some childRoot ← getRoot? child | return false
return isSameExpr childRoot e
/--
Returns `true` if `e` is the root of a field of a constructor application in the equivalence class
of `lhs` that the pattern `pat` constrains, or recursively of such a field.
These are the parents registered by `isMatchCondFalseHyp` (see `MatchCond.lean`). Since equivalence classes only grow,
we consider all constructor applications in the class, not only the root.
-/
partial def checkMatchCondNestedChild (e : Expr) (lhs pat : Expr) : GoalM Bool := do
let some patInfo ← isConstructorApp? pat | return false
let patArgs := pat.getAppArgs
for c in (← getEqc lhs) do
let some ctorInfo ← isConstructorApp? c | continue
let numArgs := ctorInfo.numParams + ctorInfo.numFields
unless ctorInfo.name == patInfo.name && pat.getAppNumArgs == numArgs do continue
let args := c.getAppArgs
for i in ctorInfo.numParams...numArgs do
if patArgs[i]!.getAppFn.isBVar then continue
if (← checkChild e args[i]! <||> checkMatchCondNestedChild e args[i]! patArgs[i]!) then
return true
return false
def checkMatchCondParent (e : Expr) (parent : Expr) : GoalM Bool := do
let_expr Grind.MatchCond parent ← parent | return false
let mut curr := parent
repeat
let .forallE _ d b _ := curr
| return false
match_expr d with
| Eq _ lhs rhs =>
if (← checkChild e lhs) then return true -- found it
if !lhs.hasLooseBVars && (← checkMatchCondNestedChild e lhs rhs) then return true
| HEq α lhs _ rhs =>
if (← checkChild e α <||> checkChild e lhs) then return true -- found it
if !lhs.hasLooseBVars && (← checkMatchCondNestedChild e lhs rhs) then return true
| _ => pure ()
curr := b
return false
def checkParents (e : Expr) : GoalM Unit := do
-- **Note**: We skip `funCC` parents because the parent-child relationship uses
-- one-level decomposition (`appFn!`/`appArg!`) rather than full `getAppArgs`/`getAppFn`.
if (← isRoot e) then
for parent in (← getParents e).elems do
if parent.isApp && (← useFunCC parent) then
pure ()
else if isMatchCond parent then
unless (← checkMatchCondParent e parent) do
throwError "e: {e}, parent: {parent}"
assert! (← checkMatchCondParent e parent)
else
let mut found := false
-- There is an argument `arg` s.t. root of `arg` is `e`.
for arg in parent.getAppArgs do
if (← checkChild e arg) then
found := true
break
-- Recall that we have support for `Expr.forallE` propagation. See `ForallProp.lean`.
if let .forallE _ d b _ := parent then
if (← checkChild e d) then
found := true
unless b.hasLooseBVars do
if (← checkChild e b) then
found := true
unless found do
unless (← checkChild e parent.getAppFn) do
throwError "e: {e}, parent: {parent}"
assert! (← checkChild e parent.getAppFn)
else
-- All the parents are stored in the root of the equivalence class.
assert! (← getParents e).isEmpty
def checkPtrEqImpliesStructEq : GoalM Unit := do
let exprs ← getExprs
for h₁ : i in *...exprs.size do
let e₁ := exprs[i]
for h₂ : j in (i+1)...exprs.size do
let e₂ := exprs[j]
-- We don't have multiple nodes for the same expression
assert! !isSameExpr e₁ e₂
-- and the two expressions must not be structurally equal
assert! !Expr.equal e₁ e₂
def checkProofs : GoalM Unit := do
let eqcs ← getEqcs
for eqc in eqcs do
for a in eqc do
for b in eqc do
unless isSameExpr a b do
let p ← mkEqHEqProof a b
trace_goal[grind.debug.proofs] "{a} = {b}"
check p
trace_goal[grind.debug.proofs] "checked: {← inferType p}"
/--
Every entry of the congruence table must be a congruence root, and must be found by a lookup
with its own key. The hash of a key depends on the roots of the children, so an entry that was
not reinserted after a merge of a child's class sits in a stale bucket: a lookup for a congruent
term finds it only when the old and new hashes collide. The per-class check in `checkEqc` is
therefore nondeterministic for such entries; this one is not.
-/
def checkCongrTable : GoalM Unit := do
let table := (← get).congrTable
for { e } in table.toList do
assert! (← isCongrRoot e)
let some { e := e' } := table.find? { e }
| throwError "`grind` internal error, stale congruence table entry{indentExpr e}"
assert! isSameExpr e e'
/-- Checks invariants if `grind.debug` is enabled. -/
public def checkInvariants (expensive := false) : GoalM Unit := do
if (← isDebugEnabled) then
checkCongrTable
for e in (← getExprs) do
let node ← getENode e
checkParents node.self
-- Interpreted nodes must be literals in `grind` normal form: a non-canonical spelling
-- breaks the assumption that distinct interpreted nodes denote distinct values.
if node.interpreted then
assert! (← Sym.Canon.normNumLit? node.self).isNone
if node.isRoot then
checkEqc node
if expensive then
checkPtrEqImpliesStructEq
Solvers.checkInvariants
if expensive && grind.debug.proofs.get (← getOptions) then
checkProofs
@[inherit_doc Grind.checkInvariants]
public def Goal.checkInvariants (goal : Goal) (expensive := false) : GrindM Unit :=
discard <| GoalM.run' goal <| Grind.checkInvariants expensive
end Lean.Meta.Grind