lean4-htt/src/Lean/Util/HasConstCache.lean
2022-06-14 17:02:59 -07:00

37 lines
1.5 KiB
Text

/-
Copyright (c) 2022 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Expr
namespace Lean
structure HasConstCache (declName : Name) where
cache : Std.HashMapImp Expr Bool := Std.mkHashMapImp
unsafe def HasConstCache.containsUnsafe (e : Expr) : StateM (HasConstCache declName) Bool := do
if let some r := (← get).cache.find? (beq := ⟨Expr.ptrEq⟩) e then
return r
else
match e with
| .const n .. => return n == declName
| .app f a _ => cache e (← containsUnsafe f <||> containsUnsafe a)
| .lam _ d b _ => cache e (← containsUnsafe d <||> containsUnsafe b)
| .forallE _ d b _ => cache e (← containsUnsafe d <||> containsUnsafe b)
| .letE _ t v b _ => cache e (← containsUnsafe t <||> containsUnsafe v <||> containsUnsafe b)
| .mdata _ b _ => cache e (← containsUnsafe b)
| .proj _ _ b _ => cache e (← containsUnsafe b)
| _ => return false
where
cache (e : Expr) (r : Bool) : StateM (HasConstCache declName) Bool := do
modify fun ⟨cache⟩ => ⟨cache.insert (beq := ⟨Expr.ptrEq⟩) e r |>.1⟩
return r
/--
Return true iff `e` contains the constant `declName`.
Remark: the results for visited expressions are stored in the state cache. -/
@[implementedBy HasConstCache.containsUnsafe]
opaque HasConstCache.contains (e : Expr) : StateM (HasConstCache declName) Bool
end Lean