lean4-htt/src/Lean/Compiler/LCNF/MonadScope.lean
2025-07-25 12:02:51 +00:00

45 lines
1.3 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/-
Copyright (c) 2022 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
module
prelude
public import Lean.Compiler.LCNF.Basic
public section
namespace Lean.Compiler.LCNF
abbrev Scope := FVarIdSet
class MonadScope (m : Type → Type) where
getScope : m Scope
withScope : (Scope → Scope) → m α → m α
export MonadScope (getScope withScope)
abbrev ScopeT (m : Type → Type) := ReaderT Scope m
instance [Monad m] : MonadScope (ScopeT m) where
getScope := read
withScope := withReader
instance (m n) [MonadLift m n] [MonadFunctor m n] [MonadScope m] : MonadScope n where
getScope := liftM (getScope : m _)
withScope f := monadMap (m := m) (withScope f)
def inScope [MonadScope m] [Monad m] (fvarId : FVarId) : m Bool :=
return (← getScope).contains fvarId
@[inline] def withParams [MonadScope m] [Monad m] (ps : Array Param) (x : m α) : m α :=
withScope (fun s => ps.foldl (init := s) fun s p => s.insert p.fvarId) x
@[inline] def withFVar [MonadScope m] [Monad m] (fvarId : FVarId) (x : m α) : m α :=
withScope (fun s => s.insert fvarId) x
@[inline] def withNewScope [MonadScope m] [Monad m] (x : m α) : m α := do
withScope (fun _ => {}) x
end Lean.Compiler.LCNF