mirror of
https://github.com/github/semantic.git
synced 2024-12-22 22:31:36 +03:00
Move envLookupOrAlloc to Abstract.Store.
This commit is contained in:
parent
77e2576d38
commit
57f531edf2
@ -1,7 +1,7 @@
|
||||
{-# LANGUAGE TypeOperators, MultiParamTypeClasses, FlexibleInstances, FlexibleContexts, UndecidableInstances, DeriveFoldable, DeriveFunctor, DeriveTraversable, DeriveGeneric, GeneralizedNewtypeDeriving #-}
|
||||
module Abstract.Environment where
|
||||
|
||||
import Abstract.Store
|
||||
import Abstract.Address
|
||||
import Abstract.FreeVariables
|
||||
import Control.Monad.Effect
|
||||
import Control.Monad.Effect.Reader
|
||||
@ -9,7 +9,6 @@ import Data.Functor.Classes
|
||||
import Data.Functor.Classes.Show.Generic
|
||||
import qualified Data.Map as Map
|
||||
import Data.Pointed
|
||||
import Data.Foldable (toList)
|
||||
import Data.Semigroup
|
||||
import qualified Data.Set as Set
|
||||
import GHC.Generics
|
||||
@ -21,27 +20,6 @@ newtype Environment l a = Environment { unEnvironment :: Map.Map Name (Address l
|
||||
envLookup :: Name -> Environment l a -> Maybe (Address l a)
|
||||
envLookup = (. unEnvironment) . Map.lookup
|
||||
|
||||
envLookupOrAlloc' ::
|
||||
( FreeVariables t
|
||||
, Semigroup (Cell l a)
|
||||
, MonadStore l a m
|
||||
, MonadAddress l m
|
||||
)
|
||||
=> t -> Environment l a -> a -> m (Name, Address l a)
|
||||
envLookupOrAlloc' term = let [name] = toList (freeVariables term) in
|
||||
envLookupOrAlloc name
|
||||
|
||||
envLookupOrAlloc ::
|
||||
( Semigroup (Cell l a)
|
||||
, MonadStore l a m
|
||||
, MonadAddress l m
|
||||
)
|
||||
=> Name -> Environment l a -> a -> m (Name, Address l a)
|
||||
envLookupOrAlloc name env v = do
|
||||
a <- maybe (alloc name) pure (envLookup name env)
|
||||
assign a v
|
||||
pure (name, a)
|
||||
|
||||
envInsert :: Name -> Address l a -> Environment l a -> Environment l a
|
||||
envInsert name value (Environment m) = Environment (Map.insert name value m)
|
||||
|
||||
|
@ -8,6 +8,8 @@ module Abstract.Store
|
||||
, storeLookup
|
||||
, storeLookupAll
|
||||
, storeRestrict
|
||||
, envLookupOrAlloc'
|
||||
, envLookupOrAlloc
|
||||
, Address(..)
|
||||
, deref
|
||||
, assign
|
||||
@ -16,6 +18,7 @@ module Abstract.Store
|
||||
) where
|
||||
|
||||
import Abstract.Address
|
||||
import Abstract.Environment
|
||||
import Abstract.FreeVariables
|
||||
import Control.Applicative
|
||||
import Control.Monad ((<=<))
|
||||
@ -62,6 +65,27 @@ storeSize = Map.size . unStore
|
||||
storeRestrict :: Ord l => Store l a -> Set.Set (Address l a) -> Store l a
|
||||
storeRestrict (Store m) roots = Store (Map.filterWithKey (\ address _ -> Address address `Set.member` roots) m)
|
||||
|
||||
envLookupOrAlloc' ::
|
||||
( FreeVariables t
|
||||
, Semigroup (Cell l a)
|
||||
, MonadStore l a m
|
||||
, MonadAddress l m
|
||||
)
|
||||
=> t -> Environment l a -> a -> m (Name, Address l a)
|
||||
envLookupOrAlloc' term = let [name] = toList (freeVariables term) in
|
||||
envLookupOrAlloc name
|
||||
|
||||
envLookupOrAlloc ::
|
||||
( Semigroup (Cell l a)
|
||||
, MonadStore l a m
|
||||
, MonadAddress l m
|
||||
)
|
||||
=> Name -> Environment l a -> a -> m (Name, Address l a)
|
||||
envLookupOrAlloc name env v = do
|
||||
a <- maybe (alloc name) pure (envLookup name env)
|
||||
assign a v
|
||||
pure (name, a)
|
||||
|
||||
|
||||
assign :: (Ord l, Semigroup (Cell l a), Pointed (Cell l), MonadStore l a m) => Address l a -> a -> m ()
|
||||
assign = (modifyStore .) . storeInsert
|
||||
|
Loading…
Reference in New Issue
Block a user