@@ -9,22 +9,22 @@ module Effectful.Internal.Env
9 , Relinker(..)
9 , Relinker(..)
10 , noRelinker
10 , noRelinker
12− -- * Safe operations
12+ -- * Operations
13 , emptyEnv
13 , emptyEnv
14 , cloneEnv
14 , cloneEnv
18 , checkSizeEnv
17 , checkSizeEnv
20− -- * Extending and shrinking
19+ -- ** Extending and shrinking
21 , unsafeConsEnv
20 , unsafeConsEnv
22 , unsafeTailEnv
21 , unsafeTailEnv
24− -- * Data retrieval and update
23+ -- ** Data retrieval and update
25− , unsafePutEnv
24+ , getEnv
26− , unsafeModifyEnv
25+ , putEnv
27− , unsafeStateEnv
26+ , stateEnv
30import Control.Monad
30import Control.Monad
@@ -322,12 +322,6 @@ sizeEnv (Env (Forks _ baseIx lref _) _ _) = do
322 pure $ baseIx + n
322 pure $ baseIx + n
323{-# NOINLINE sizeEnv #-}
323{-# NOINLINE sizeEnv #-}
325−-- | Extract a specific data type from the environment.
326−getEnv :: forall e es handler. e :> es => Env es -> IO (handler e)
328− (i, es) <- getLocation @e env
329− fromAny <$> readSmallArray es i
331-- | Check that the size of the environment is the same as the expected value.
325-- | Check that the size of the environment is the same as the expected value.
332checkSizeEnv :: Int -> Env es -> IO ()
326checkSizeEnv :: Int -> Env es -> IO ()
333checkSizeEnv k (Env NoFork ref _) = do
327checkSizeEnv k (Env NoFork ref _) = do
@@ -345,6 +339,9 @@ checkSizeEnv k (Env (Forks _ baseIx lref _) _ _) = do
345-- Extending and shrinking
339-- Extending and shrinking
347-- | Extend the environment with a new data type (in place).
341-- | Extend the environment with a new data type (in place).
343+-- /Note:/ this function is __unsafe__ because it renders the input 'Env'
344+-- unusable, but it's not checked anywhere.
348unsafeConsEnv :: handler e -> Relinker handler e -> Env es -> IO (Env (e : es))
345unsafeConsEnv :: handler e -> Relinker handler e -> Env es -> IO (Env (e : es))
349unsafeConsEnv e f (Env fork gref gen) = case fork of
346unsafeConsEnv e f (Env fork gref gen) = case fork of
350 NoFork -> do
347 NoFork -> do
@@ -380,6 +377,9 @@ unsafeConsEnv e f (Env fork gref gen) = case fork of
381-- | Shrink the environment by one data type (in place). Makes sure the size of
378-- | Shrink the environment by one data type (in place). Makes sure the size of
382-- the environment is as expected.
379-- the environment is as expected.
381+-- /Note:/ this function is __unsafe__ because it renders the input 'Env'
382+-- unusable, but it's not checked anywhere.
383unsafeTailEnv :: Int -> Env (e : es) -> IO (Env es)
383unsafeTailEnv :: Int -> Env (e : es) -> IO (Env es)
384unsafeTailEnv len (Env fork gref gen) = case fork of
384unsafeTailEnv len (Env fork gref gen) = case fork of
385 NoFork -> do
385 NoFork -> do
@@ -403,39 +403,45 @@ unsafeTailEnv len (Env fork gref gen) = case fork of
403----------------------------------------
403----------------------------------------
404-- Data retrieval and update
404-- Data retrieval and update
406−-- | Replace the data type in the environment with a new value (in place).
406+-- | Extract a specific data type from the environment.
408− :: forall e es handler. e :> es
407+getEnv :: forall e es handler. e :> es => Env es -> IO (handler e)
409− => Env es
408+getEnv env = do
412−unsafePutEnv env e = do
413 (i, es) <- getLocation @e env
409 (i, es) <- getLocation @e env
414− e `seq` writeSmallArray es i (toAny e)
410+ fromAny <$> readSmallArray es i
416−-- | Modify the data type in the environment (in place).
412+-- | Replace the data type in the environment with a new value (in place).
417−unsafeModifyEnv
413+putEnv
418 :: forall e es handler. e :> es
414 :: forall e es handler. e :> es
419 => Env es
415 => Env es
420− -> (handler e -> handler e)
416+ -> handler e
422−unsafeModifyEnv env f = do
418+putEnv env e = do
423 (i, es) <- getLocation @e env
419 (i, es) <- getLocation @e env
424− e <- f . fromAny <$> readSmallArray es i
425 e `seq` writeSmallArray es i (toAny e)
420 e `seq` writeSmallArray es i (toAny e)
427-- | Modify the data type in the environment (in place) and return a value.
422-- | Modify the data type in the environment (in place) and return a value.
428−unsafeStateEnv
423+stateEnv
429 :: forall e es handler a. e :> es
424 :: forall e es handler a. e :> es
430 => Env es
425 => Env es
431 -> (handler e -> (a, handler e))
426 -> (handler e -> (a, handler e))
433−unsafeStateEnv env f = do
428+stateEnv env f = do
434 (i, es) <- getLocation @e env
429 (i, es) <- getLocation @e env
435 (a, e) <- f . fromAny <$> readSmallArray es i
430 (a, e) <- f . fromAny <$> readSmallArray es i
436 e `seq` writeSmallArray es i (toAny e)
431 e `seq` writeSmallArray es i (toAny e)
434+-- | Modify the data type in the environment (in place).
436+ :: forall e es handler. e :> es
438+ -> (handler e -> handler e)
441+ (i, es) <- getLocation @e env
442+ e <- f . fromAny <$> readSmallArray es i
443+ e `seq` writeSmallArray es i (toAny e)
439----------------------------------------
445----------------------------------------
440-- Internal helpers
446-- Internal helpers