mirror of
https://github.com/github/semantic.git
synced 2025-01-05 14:11:33 +03:00
Parameterize MonadHeap by the location type.
This commit is contained in:
parent
a60682ee69
commit
c6d06c632b
@ -13,7 +13,7 @@ newtype BadVariables m (effects :: [* -> *]) a = BadVariables (m effects a)
|
||||
|
||||
deriving instance MonadControl term (m effects) => MonadControl term (BadVariables m effects)
|
||||
deriving instance MonadEnvironment value (m effects) => MonadEnvironment value (BadVariables m effects)
|
||||
deriving instance MonadHeap value (m effects) => MonadHeap value (BadVariables m effects)
|
||||
deriving instance MonadHeap location value (m effects) => MonadHeap location value (BadVariables m effects)
|
||||
deriving instance MonadModuleTable term value (m effects) => MonadModuleTable term value (BadVariables m effects)
|
||||
deriving instance MonadEvaluator term value (m effects) => MonadEvaluator term value (BadVariables m effects)
|
||||
|
||||
|
@ -27,7 +27,7 @@ newtype Caching m (effects :: [* -> *]) a = Caching (m effects a)
|
||||
|
||||
deriving instance MonadControl term (m effects) => MonadControl term (Caching m effects)
|
||||
deriving instance MonadEnvironment value (m effects) => MonadEnvironment value (Caching m effects)
|
||||
deriving instance MonadHeap value (m effects) => MonadHeap value (Caching m effects)
|
||||
deriving instance MonadHeap location value (m effects) => MonadHeap location value (Caching m effects)
|
||||
deriving instance MonadModuleTable term value (m effects) => MonadModuleTable term value (Caching m effects)
|
||||
deriving instance MonadEvaluator term value (m effects) => MonadEvaluator term value (Caching m effects)
|
||||
|
||||
|
@ -15,7 +15,7 @@ newtype Collecting m (effects :: [* -> *]) a = Collecting (m effects a)
|
||||
|
||||
deriving instance MonadControl term (m effects) => MonadControl term (Collecting m effects)
|
||||
deriving instance MonadEnvironment value (m effects) => MonadEnvironment value (Collecting m effects)
|
||||
deriving instance MonadHeap value (m effects) => MonadHeap value (Collecting m effects)
|
||||
deriving instance MonadHeap location value (m effects) => MonadHeap location value (Collecting m effects)
|
||||
deriving instance MonadModuleTable term value (m effects) => MonadModuleTable term value (Collecting m effects)
|
||||
|
||||
instance ( Effectful m
|
||||
|
@ -15,7 +15,7 @@ newtype DeadCode m (effects :: [* -> *]) a = DeadCode (m effects a)
|
||||
|
||||
deriving instance MonadControl term (m effects) => MonadControl term (DeadCode m effects)
|
||||
deriving instance MonadEnvironment value (m effects) => MonadEnvironment value (DeadCode m effects)
|
||||
deriving instance MonadHeap value (m effects) => MonadHeap value (DeadCode m effects)
|
||||
deriving instance MonadHeap location value (m effects) => MonadHeap location value (DeadCode m effects)
|
||||
deriving instance MonadModuleTable term value (m effects) => MonadModuleTable term value (DeadCode m effects)
|
||||
deriving instance MonadEvaluator term value (m effects) => MonadEvaluator term value (DeadCode m effects)
|
||||
|
||||
|
@ -116,7 +116,7 @@ instance Members '[ State (EvaluatingState term value)
|
||||
result <- a
|
||||
result <$ modifyEnv Env.pop
|
||||
|
||||
instance Member (State (EvaluatingState term value)) effects => MonadHeap value (Evaluating term value effects) where
|
||||
instance (Member (State (EvaluatingState term value)) effects, location ~ LocationFor value) => MonadHeap location value (Evaluating term value effects) where
|
||||
getHeap = view _heap
|
||||
putHeap = (_heap .=)
|
||||
|
||||
|
@ -28,7 +28,7 @@ newtype ImportGraphing m (effects :: [* -> *]) a = ImportGraphing (m effects a)
|
||||
|
||||
deriving instance MonadControl term (m effects) => MonadControl term (ImportGraphing m effects)
|
||||
deriving instance MonadEnvironment value (m effects) => MonadEnvironment value (ImportGraphing m effects)
|
||||
deriving instance MonadHeap value (m effects) => MonadHeap value (ImportGraphing m effects)
|
||||
deriving instance MonadHeap location value (m effects) => MonadHeap location value (ImportGraphing m effects)
|
||||
deriving instance MonadModuleTable term value (m effects) => MonadModuleTable term value (ImportGraphing m effects)
|
||||
deriving instance MonadEvaluator term value (m effects) => MonadEvaluator term value (ImportGraphing m effects)
|
||||
|
||||
|
@ -19,7 +19,7 @@ newtype Quietly m (effects :: [* -> *]) a = Quietly (m effects a)
|
||||
|
||||
deriving instance MonadControl term (m effects) => MonadControl term (Quietly m effects)
|
||||
deriving instance MonadEnvironment value (m effects) => MonadEnvironment value (Quietly m effects)
|
||||
deriving instance MonadHeap value (m effects) => MonadHeap value (Quietly m effects)
|
||||
deriving instance MonadHeap location value (m effects) => MonadHeap location value (Quietly m effects)
|
||||
deriving instance MonadModuleTable term value (m effects) => MonadModuleTable term value (Quietly m effects)
|
||||
deriving instance MonadEvaluator term value (m effects) => MonadEvaluator term value (Quietly m effects)
|
||||
|
||||
|
@ -17,7 +17,7 @@ newtype Tracing (trace :: * -> *) m (effects :: [* -> *]) a = Tracing (m effects
|
||||
|
||||
deriving instance MonadControl term (m effects) => MonadControl term (Tracing trace m effects)
|
||||
deriving instance MonadEnvironment value (m effects) => MonadEnvironment value (Tracing trace m effects)
|
||||
deriving instance MonadHeap value (m effects) => MonadHeap value (Tracing trace m effects)
|
||||
deriving instance MonadHeap location value (m effects) => MonadHeap location value (Tracing trace m effects)
|
||||
deriving instance MonadModuleTable term value (m effects) => MonadModuleTable term value (Tracing trace m effects)
|
||||
deriving instance MonadEvaluator term value (m effects) => MonadEvaluator term value (Tracing trace m effects)
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
{-# LANGUAGE MultiParamTypeClasses, TypeFamilies, UndecidableInstances #-}
|
||||
{-# LANGUAGE TypeFamilies, UndecidableInstances #-}
|
||||
module Control.Abstract.Addressable where
|
||||
|
||||
import Control.Abstract.Evaluator
|
||||
@ -29,7 +29,7 @@ lookupOrAlloc name = lookupEnv name >>= maybe (alloc name) pure
|
||||
|
||||
letrec :: ( MonadAddressable (LocationFor value) value m
|
||||
, MonadEnvironment value m
|
||||
, MonadHeap value m
|
||||
, MonadHeap (LocationFor value) value m
|
||||
)
|
||||
=> Name
|
||||
-> m value
|
||||
@ -55,7 +55,7 @@ letrec' name body = do
|
||||
-- Instances
|
||||
|
||||
-- | 'Precise' locations are always 'alloc'ated a fresh 'Address', and 'deref'erence to the 'Latest' value written.
|
||||
instance (MonadFail m, LocationFor value ~ Precise, MonadHeap value m) => MonadAddressable Precise value m where
|
||||
instance (MonadFail m, MonadHeap Precise value m) => MonadAddressable Precise value m where
|
||||
deref = derefWith (maybeM uninitializedAddress . unLatest)
|
||||
alloc _ = do
|
||||
-- Compute the next available address in the heap, then write an empty value into it.
|
||||
@ -63,12 +63,12 @@ instance (MonadFail m, LocationFor value ~ Precise, MonadHeap value m) => MonadA
|
||||
addr <$ modifyHeap (heapInit addr mempty)
|
||||
|
||||
-- | 'Monovariant' locations 'alloc'ate one 'Address' per unique variable name, and 'deref'erence once per stored value, nondeterministically.
|
||||
instance (Alternative m, LocationFor value ~ Monovariant, MonadFail m, MonadHeap value m, Ord value) => MonadAddressable Monovariant value m where
|
||||
instance (Alternative m, LocationFor value ~ Monovariant, MonadFail m, MonadHeap Monovariant value m, Ord value) => MonadAddressable Monovariant value m where
|
||||
deref = derefWith (foldMapA pure)
|
||||
alloc = pure . Address . Monovariant
|
||||
|
||||
-- | Dereference the given 'Address' in the heap, using the supplied function to act on the cell, or failing if the address is uninitialized.
|
||||
derefWith :: (MonadFail m, MonadHeap value m, Ord (LocationFor value)) => (CellFor value -> m a) -> Address (LocationFor value) value -> m a
|
||||
derefWith :: (MonadFail m, MonadHeap location value m, Ord location) => (Cell location value -> m a) -> Address location value -> m a
|
||||
derefWith with = maybe uninitializedAddress with <=< lookupHeap
|
||||
|
||||
-- | Fail with a message denoting an uninitialized address (i.e. one which was 'alloc'ated, but never 'assign'ed a value before being 'deref'erenced).
|
||||
|
@ -49,7 +49,7 @@ class ( MonadControl term m
|
||||
, MonadEnvironment value m
|
||||
, MonadFail m
|
||||
, MonadModuleTable term value m
|
||||
, MonadHeap value m
|
||||
, MonadHeap (LocationFor value) value m
|
||||
)
|
||||
=> MonadEvaluator term value m | m -> term, m -> value where
|
||||
-- | Get the current 'Configuration' with a passed-in term.
|
||||
@ -123,28 +123,28 @@ fullEnvironment :: MonadEnvironment value m => m (EnvironmentFor value)
|
||||
fullEnvironment = mappend <$> getEnv <*> defaultEnvironment
|
||||
|
||||
-- | A 'Monad' abstracting a heap of values.
|
||||
class Monad m => MonadHeap value m | m -> value where
|
||||
class Monad m => MonadHeap location value m | m -> value, m -> location where
|
||||
-- | Retrieve the heap.
|
||||
getHeap :: m (HeapFor value)
|
||||
getHeap :: m (Heap location value)
|
||||
-- | Set the heap.
|
||||
putHeap :: HeapFor value -> m ()
|
||||
putHeap :: Heap location value -> m ()
|
||||
|
||||
-- | Update the heap.
|
||||
modifyHeap :: MonadHeap value m => (HeapFor value -> HeapFor value) -> m ()
|
||||
modifyHeap :: MonadHeap location value m => (Heap location value -> Heap location value) -> m ()
|
||||
modifyHeap f = do
|
||||
s <- getHeap
|
||||
putHeap $! f s
|
||||
|
||||
-- | Look up the cell for the given 'Address' in the 'Heap'.
|
||||
lookupHeap :: (MonadHeap value m, Ord (LocationFor value)) => Address (LocationFor value) value -> m (Maybe (CellFor value))
|
||||
lookupHeap :: (MonadHeap location value m, Ord location) => Address location value -> m (Maybe (Cell location value))
|
||||
lookupHeap = flip fmap getHeap . heapLookup
|
||||
|
||||
-- | Write a value to the given 'Address' in the 'Store'.
|
||||
assign :: ( Ord (LocationFor value)
|
||||
, MonadHeap value m
|
||||
, Reducer value (CellFor value)
|
||||
assign :: ( Ord location
|
||||
, MonadHeap location value m
|
||||
, Reducer value (Cell location value)
|
||||
)
|
||||
=> Address (LocationFor value) value
|
||||
=> Address location value
|
||||
-> value
|
||||
-> m ()
|
||||
assign address = modifyHeap . heapInsert address
|
||||
|
@ -21,7 +21,7 @@ module Control.Abstract.Value
|
||||
import Control.Abstract.Evaluator
|
||||
import Data.Abstract.FreeVariables
|
||||
import Data.Abstract.Environment as Env
|
||||
import Data.Abstract.Address (Address)
|
||||
import Data.Abstract.Address (Address, Cell)
|
||||
import Data.Abstract.Number as Number
|
||||
import Data.Scientific (Scientific)
|
||||
import Data.Semigroup.Reducer hiding (unit)
|
||||
@ -173,12 +173,12 @@ doWhile body cond = loop $ \ continue -> body *> do
|
||||
|
||||
makeNamespace :: ( MonadValue value m
|
||||
, MonadEnvironment value m
|
||||
, MonadHeap value m
|
||||
, Reducer value (CellFor value)
|
||||
, Ord (LocationFor value)
|
||||
, MonadHeap location value m
|
||||
, Ord location
|
||||
, Reducer value (Cell location value)
|
||||
)
|
||||
=> Name
|
||||
-> Address (LocationFor value) value
|
||||
-> Address location value
|
||||
-> [value]
|
||||
-> m value
|
||||
makeNamespace name addr supers = do
|
||||
|
@ -52,7 +52,7 @@ instance ValueRoots Type where
|
||||
|
||||
|
||||
-- | Discard the value arguments (if any), constructing a 'Type' instead.
|
||||
instance (Alternative m, MonadEnvironment Type m, MonadFail m, MonadFresh m, MonadHeap Type m) => MonadValue Type m where
|
||||
instance (Alternative m, MonadEnvironment Type m, MonadFail m, MonadFresh m, MonadHeap Monovariant Type m) => MonadValue Type m where
|
||||
abstract names (Subterm _ body) = do
|
||||
(env, tvars) <- foldr (\ name rest -> do
|
||||
a <- alloc name
|
||||
|
Loading…
Reference in New Issue
Block a user