@@ -6,7 +6,7 @@
66-- This module is intended for internal use only, and may change without warning
77-- in subsequent releases.
88module Effectful.Internal.Monad
9− ( -- * The 'Eff' monad
9+ ( -- * Monad
1010 Eff
1111 , runPureEff
1212
@@ -16,17 +16,18 @@ module Effectful.Internal.Monad
1616 , unsafeEff_
1717
1818 -- * Fail
19− , Fail(..)
19+ , Fail
20+ , runFail
2021
2122 -- * IO
2223 , IOE
2324 , runEff
2425
25− -- * Prim
26+ -- * Primitive
2627 , Prim
2728 , runPrim
2829
29− -- * Unlift strategies
30+ -- ** Unlift strategies
3031 , UnliftStrategy(..)
3132 , Persistence(..)
3233 , Limit(..)
@@ -34,88 +35,87 @@ module Effectful.Internal.Monad
3435 , withUnliftStrategy
3536 , withEffToIO
3637
37− -- ** Low-level unlifts
38− , seqUnliftIO
39− , concUnliftIO
40−
41− -- * Effects
42−
43− -- ** Dynamic dispatch
44− , EffectHandler
45− , LocalEnv(..)
46− , HandlerA(..)
47− , runHandler
48− , send
49−
50− -- ** Static dispatch
51− , DataA(..)
52− , runData
53− , evalData
54− , execData
55− , getData
56− , putData
57− , stateData
58− , stateDataM
59− , localData
38+ --- *** Low-level helpers
39+ , unsafeWithLiftMapIO
40+ , seqUnliftEff
41+ , concUnliftEff
42+
43+ -- * Primitive effects
44+
45+ -- ** Running
46+ , runEffect
47+ , evalEffect
48+ , execEffect
49+
50+ -- ** Modification
51+ , getEffect
52+ , putEffect
53+ , stateEffect
54+ , stateEffectM
55+ , localEffect
6056 ) where
6157
6258import Control.Applicative (liftA2)
59+import Control.Concurrent (myThreadId)
60+import Control.Exception
6361import Control.Monad.Base
6462import Control.Monad.Fix
6563import Control.Monad.IO.Class
6664import Control.Monad.IO.Unlift
6765import Control.Monad.Primitive
6866import Control.Monad.Trans.Control
67+import Data.Unique
6968import GHC.Exts (oneShot)
7069import GHC.IO (IO(..))
7170import GHC.Stack (HasCallStack)
7271import System.IO.Unsafe (unsafeDupablePerformIO)
73−import qualified Control.Exception as E
74−import qualified Control.Monad.Catch as C
72+import qualified Control.Monad.Catch as E
7573
7674import Effectful.Internal.Effect
7775import Effectful.Internal.Env
7876import Effectful.Internal.Unlift
77+import Effectful.Internal.Utils
7978
8079type role Eff nominal representational
8180
82−-- | The 'Eff' monad provides the implementation of a computation that performs
81+-- | The 'Eff' monad provides the implementation of an operation that performs
8382-- an arbitrary set of effects. In @'Eff' es a@, @es@ is a type-level list that
84−-- contains all the effects that the computation may perform. For example, a
85−-- computation that produces an 'Integer' by consuming a 'String' from the
86−-- global environment and acting upon a single mutable value of type 'Bool'
87−-- would have the following type:
83+-- contains all the effects that the operation may perform. For example, an
84+-- operation that produces an 'Integer' by consuming a 'String' from the global
85+-- environment and acting upon a single mutable cell containing a 'Bool' would
86+-- have the following type:
8887--
8988-- @
90−-- 'Eff' '['Effectful.Reader.Reader' 'String', 'Effectful.State.Local.State' 'Bool'] 'Integer'
89+-- 'Eff' '['Effectful.Reader.Reader' 'String', 'Effectful.State.State' 'Bool'] 'Integer'
9190-- @
9291--
9392-- Normally, a concrete list of effects is not used to parameterize 'Eff'.
9493-- Instead, the '(:>)' type class is used to express constraints on the list of
95−-- effects without coupling a computation to a concrete list of effects. For
96−-- example, the above example would be expressed with the following type:
94+-- effects without coupling an operation to a concrete list of effects. For
95+-- example, the above example would more commonly be expressed with the
96+-- following type:
9797--
9898-- @
99−-- ('Effectful.Reader.Reader' 'String' ':>' es, 'Effectful.State.Local.State' 'Bool' ':>' es) => 'Eff' es 'Integer'
99+-- ('Effectful.Reader.Reader' 'String' ':>' es, 'Effectful.State.State' 'Bool' ':>' es) => 'Eff' es 'Integer'
100100-- @
101101--
102−-- This abstraction allows the computation to be used in functions that may
102+-- This abstraction allows the operation to be used in functions that may
103103-- perform other effects, and it also allows the effects to be handled in any
104104-- order.
105105newtype Eff (es :: [Effect]) a = Eff (Env es -> IO a)
106106 deriving (Monoid, Semigroup)
107107
108−-- | Run a pure 'Eff' computation.
108+-- | Run a pure 'Eff' operation.
109109--
110−-- For running computations with side effects see 'runEff'.
110+-- For running operations with side effects see 'runEff'.
111111runPureEff :: Eff '[] a -> a
112112runPureEff (Eff m) =
113− -- unsafeDupablePerformIO is safe here since IOE was not on the stack, so no
114− -- IO with side effects was performed (unless someone sneakily introduced side
115− -- effects with unsafeEff, but then all bets are off).
113+ -- unsafePerformIO is safe here since IOE was not on the stack, so no IO with
114+ -- side effects was performed (unless someone sneakily introduced side effects
115+ -- with unsafeEff, but then all bets are off).
116116 --
117117 -- Moreover, internals don't allocate any resources that require explicit
118− -- cleanup actions to run.
118+ -- cleanup actions to run, so the "Dupable" part should also be just fine.
119119 unsafeDupablePerformIO $ m =<< emptyEnv
120120
121121----------------------------------------
@@ -128,68 +128,69 @@ unEff (Eff m) = m
128128-- | Access the underlying 'IO' monad along with the environment.
129129--
130130-- This function is __unsafe__ because it can be used to introduce arbitrary
131−-- 'IO' actions into pure 'Eff' computations.
131+-- 'IO' actions into pure 'Eff' operations.
132132unsafeEff :: (Env es -> IO a) -> Eff es a
133133unsafeEff m = Eff (oneShot m)
134134
135135-- | Access the underlying 'IO' monad.
136136--
137137-- This function is __unsafe__ because it can be used to introduce arbitrary
138−-- 'IO' actions into pure 'Eff' computations.
138+-- 'IO' actions into pure 'Eff' operations.
139139unsafeEff_ :: IO a -> Eff es a
140140unsafeEff_ m = unsafeEff $ \_ -> m
141141
142−----------------------------------------
143−-- Unlifting IO
144−
145−-- | Get the current 'UnliftStrategy'.
146−unliftStrategy :: IOE :> es => Eff es UnliftStrategy
147−unliftStrategy = do
148− DataA (IOE unlift) <- getData
149− pure unlift
150−
151−-- | Locally override the 'UnliftStrategy' with the given value.
152−withUnliftStrategy :: IOE :> es => UnliftStrategy -> Eff es a -> Eff es a
153−withUnliftStrategy unlift = localData $ \_ -> DataA (IOE unlift)
154−
155−-- | Create an unlifting function with the current 'UnliftStrategy'.
142+-- | Utility for lifting 'IO' operations of type
156143--
157−-- This function is equivalent to 'withRunInIO', but has a 'HasCallStack'
158−-- constraint for accurate stack traces in case an insufficiently powerful
159−-- 'UnliftStrategy' is used and the unlifting function fails.
144+-- @'IO' a -> 'IO' b@
160145--
161−-- /Note:/ the strategy is reset to 'SeqUnlift' inside the continuation.
162−withEffToIO
163− :: (HasCallStack, IOE :> es)
164− => ((forall r. Eff es r -> IO r) -> IO a)
165− -- ^ Continuation with the unlifting function in scope.
166− -> Eff es a
167−withEffToIO f = unliftStrategy >>= \case
168− SeqUnlift -> unsafeEff $ \es -> seqUnliftIO es f
169− ConcUnlift p b -> withUnliftStrategy SeqUnlift $ do
170− unsafeEff $ \es -> concUnliftIO es p b f
146+-- to
147+--
148+-- @forall localEs. 'Eff' localEs a -> 'Eff' localEs b@
149+--
150+-- This function is __unsafe__ because it can be used to introduce arbitrary
151+-- 'IO' actions into pure 'Eff' operations.
152+--
153+-- /Note:/ the 'IO' operation must not run its argument in a separate thread,
154+-- attempting to do so will result in a runtime error.
155+unsafeWithLiftMapIO
156+ :: HasCallStack
157+ => ((forall a b localEs. (IO a -> IO b) -> Eff localEs a -> Eff localEs b) -> Eff es r)
158+ -> Eff es r
159+unsafeWithLiftMapIO k = k $ \mapIO m -> unsafeEff $ \es -> do
160+ seqUnliftEff es $ \unlift -> mapIO $ unlift m
171161
172−-- | Create an unlifting function with the 'SeqUnlift' strategy.
173−seqUnliftIO
162+-- | Lower 'Eff' operations into 'IO' ('SeqUnlift').
163+seqUnliftEff
174164 :: HasCallStack
175165 => Env es
176− -- ^ The environment.
177166 -> ((forall r. Eff es r -> IO r) -> IO a)
178− -- ^ Continuation with the unlifting function in scope.
179167 -> IO a
180−seqUnliftIO es k = seqUnlift k es unEff
181−
182−-- | Create an unlifting function with the 'ConcUnlift' strategy.
183−concUnliftIO
168+seqUnliftEff es k = do
169+ tid0 <- myThreadId
170+ k $ \m -> do
171+ tid <- myThreadId
172+ if tid `eqThreadId` tid0
173+ then unEff m es
174+ else error
175+ $ "If you want to use the unlifting function to run Eff operations "
176+ ++ "in multiple threads, have a look at UnliftStrategy (ConcUnlift)."
177+
178+-- | Lower 'Eff' operations into 'IO' ('ConcUnlift').
179+concUnliftEff
184180 :: HasCallStack
185181 => Env es
186− -- ^ The environment.
187182 -> Persistence
188183 -> Limit
189184 -> ((forall r. Eff es r -> IO r) -> IO a)
190− -- ^ Continuation with the unlifting function in scope.
191185 -> IO a
192−concUnliftIO es persistence limit k = concUnlift persistence limit k es unEff
186+concUnliftEff es Ephemeral (Limited uses) k =
187+ ephemeralConcUnliftIO uses k es unEff
188+concUnliftEff es Ephemeral Unlimited k =
189+ ephemeralConcUnliftIO maxBound k es unEff
190+concUnliftEff es Persistent (Limited threads) k =
191+ persistentConcUnliftIO False threads k es unEff
192+concUnliftEff es Persistent Unlimited k =
193+ persistentConcUnliftIO True maxBound k es unEff
193194
194195----------------------------------------
195196-- Base
@@ -217,56 +218,84 @@ instance MonadFix (Eff es) where
217218----------------------------------------
218219-- Exception
219220
220−instance C.MonadThrow (Eff es) where
221− throwM = unsafeEff_ . E.throwIO
221+instance E.MonadThrow (Eff es) where
222+ throwM = unsafeEff_ . throwIO
222223
223−instance C.MonadCatch (Eff es) where
224+instance E.MonadCatch (Eff es) where
224225 catch m handler = unsafeEff $ \es -> do
225226 size <- sizeEnv es
226− unEff m es `E.catch` \e -> do
227+ unEff m es `catch` \e -> do
227228 checkSizeEnv size es
228229 unEff (handler e) es
229230
230−instance C.MonadMask (Eff es) where
231− mask k = unsafeEff $ \es -> E.mask $ \restore ->
231+instance E.MonadMask (Eff es) where
232+ mask k = unsafeEff $ \es -> mask $ \restore ->
232233 unEff (k $ \m -> unsafeEff $ restore . unEff m) es
233234
234− uninterruptibleMask k = unsafeEff $ \es -> E.uninterruptibleMask $ \restore ->
235+ uninterruptibleMask k = unsafeEff $ \es -> uninterruptibleMask $ \restore ->
235236 unEff (k $ \m -> unsafeEff $ restore . unEff m) es
236237
237− generalBracket acquire release use = unsafeEff $ \es -> E.mask $ \restore -> do
238+ generalBracket acquire release use = unsafeEff $ \es -> mask $ \restore -> do
238239 size <- sizeEnv es
239240 resource <- unEff acquire es
240− b <- restore (unEff (use resource) es) `E.catch` \e -> do
241+ b <- restore (unEff (use resource) es) `catch` \e -> do
241242 checkSizeEnv size es
242− _ <- unEff (release resource $ C.ExitCaseException e) es
243− E.throwIO e
243+ _ <- unEff (release resource $ E.ExitCaseException e) es
244+ throwIO e
244245 checkSizeEnv size es
245− c <- unEff (release resource $ C.ExitCaseSuccess b) es
246+ c <- unEff (release resource $ E.ExitCaseSuccess b) es
246247 pure (b, c)
247248
248249----------------------------------------
249250-- Fail
250251
251−-- | Provide the ability to use the 'MonadFail' instance of 'Eff'.
252−data Fail :: Effect where
253− Fail :: String -> Fail m a
252+newtype Fail :: Effect where
253+ Fail :: Unique -> Fail m r
254+
255+runFail :: Eff (Fail : es) a -> Eff es (Either String a)
256+runFail m = unsafeEff $ \es0 -> mask $ \release -> do
257+ -- A unique tag is picked so that different runFail handlers don't catch each
258+ -- other's exceptions.
259+ tag <- newUnique
260+ size0 <- sizeEnv es0
261+ es <- unsafeConsEnv (IdE (Fail tag)) noRelinker es0
262+ r <- tryFailIO tag (release $ unEff m es) `onException` unsafeTailEnv size0 es
263+ unsafeTailEnv size0 es
264+ pure r
254265
255266instance Fail :> es => MonadFail (Eff es) where
256− fail = send . Fail
267+ fail msg = do
268+ IdE (Fail tag) <- getEffect
269+ unsafeEff_ . throwIO $ FailEx tag msg
270+
271+--------------------
272+
273+data FailEx = FailEx !Unique String
274+instance Show FailEx where
275+ showsPrec p (FailEx _ msg)
276+ = ("Effectful.Internal.Monad.FailEx " ++)
277+ . showsPrec p msg
278+instance Exception FailEx
279+
280+tryFailIO :: Unique -> IO a -> IO (Either String a)
281+tryFailIO tag m =
282+ (Right <$> m) `catch` \err@(FailEx etag msg) -> do
283+ if tag == etag
284+ then pure $ Left msg
285+ else throwIO err
257286
258287----------------------------------------
259288-- IO
260289
261−-- | Run arbitrary 'IO' computations via 'MonadIO' or 'MonadUnliftIO'.
290+-- | Run arbitrary 'IO' operations via 'MonadIO' or 'MonadUnliftIO'.
262291newtype IOE :: Effect where
263292 IOE :: UnliftStrategy -> IOE m r
264293
265−-- | Run an 'Eff' computation with side effects.
294+-- | Run an 'Eff' operation with side effects.
266295--
267−-- For running pure computations see 'runPureEff'.
296+-- For running pure operations see 'runPureEff'.
268297runEff :: Eff '[IOE] a -> IO a
269−runEff m = unEff (evalData (DataA (IOE SeqUnlift)) m) =<< emptyEnv
298+runEff m = unEff (evalEffect (IdE (IOE SeqUnlift)) m) =<< emptyEnv
270299
271300instance IOE :> es => MonadIO (Eff es) where
272301 liftIO = unsafeEff_
@@ -290,126 +319,141 @@ instance IOE :> es => MonadBaseControl IO (Eff es) where
290319----------------------------------------
291320-- Primitive
292321
293−-- | Provide the ability to perform primitive state-transformer actions.
322+-- | An effect to perform primitive state-transformer actions.
294323data Prim :: Effect where
295324 Prim :: Prim m r
296325
297−-- | Run an 'Eff' computation with primitive state-transformer actions.
326+-- | Run an 'Eff' operation with primitive state-transformer actions.
298327runPrim :: IOE :> es => Eff (Prim : es) a -> Eff es a
299−runPrim = evalData (DataA Prim)
328+runPrim = evalEffect (IdE Prim)
300329
301330instance Prim :> es => PrimMonad (Eff es) where
302331 type PrimState (Eff es) = RealWorld
303332 primitive = unsafeEff_ . IO
304333
305334----------------------------------------
306−-- Dynamic dispatch
335+-- Unlift strategies
336+
337+-- | The strategy to use when unlifting 'Eff' operations via 'withRunInIO' or
338+-- the 'Effectful.Interpreter.localUnlift' family.
339+data UnliftStrategy
340+ = SeqUnlift
341+ -- ^ The fastest strategy and a default setting for 'IOE'. An attempt to call
342+ -- the unlifting function in threads distinct from its creator will result in
343+ -- a runtime error.
344+ | ConcUnlift !Persistence !Limit
345+ -- ^ A strategy that makes it possible for the unlifting function to be called
346+ -- in threads distinct from its creator. See 'Persistence' and 'Limit'
347+ -- settings for more information.
348+ deriving (Eq, Ord, Show)
349+
350+-- | Persistence setting for the 'ConcUnlift' strategy.
351+--
352+-- Different functions require different persistence strategies. Examples:
353+--
354+-- - Lifting 'pooledMapConcurrentlyN' from the @unliftio@ library requires the
355+-- 'Ephemeral' strategy as we don't want jobs to share environment changes
356+-- made by previous jobs run in the same worker thread.
357+--
358+-- - Lifting 'Control.Concurrent.forkIOWithUnmask' requires the 'Persistent'
359+-- strategy, otherwise the unmasking function would start with a fresh
360+-- environment each time it's called.
361+data Persistence
362+ = Ephemeral
363+ -- ^ Don't persist the environment between calls to the unlifting function in
364+ -- threads distinct from its creator.
365+ | Persistent
366+ -- ^ Persist the environment between calls to the unlifting function within a
367+ -- particular thread.
368+ deriving (Eq, Ord, Show)
369+
370+-- | Limit setting for the 'ConcUnlift' strategy.
371+data Limit
372+ = Limited !Int
373+ -- ^ Behavior dependent on the 'Persistence' setting.
374+ --
375+ -- For 'Ephemeral', it limits the amount of uses of the unlifting function in
376+ -- threads distinct from its creator to @N@. The unlifting function will
377+ -- create @N@ copies of the environment when called @N@ times and @K+1@ copies
378+ -- when called @K < N@ times.
379+ --
380+ -- For 'Persistent', it limits the amount of threads, distinct from the
381+ -- creator of the unlifting function, it can be called in to @N@. The amount
382+ -- of calls to the unlifting function within a particular threads is
383+ -- unlimited. The unlifting function will create @N@ copies of the environment
384+ -- when called in @N@ threads and @K+1@ copies when called in @K < N@ threads.
385+ | Unlimited
386+ -- ^ Unlimited use of the unlifting function.
387+ deriving (Eq, Ord, Show)
307388
308−type role LocalEnv nominal nominal
389+-- | Get the current 'UnliftStrategy'.
390+unliftStrategy :: IOE :> es => Eff es UnliftStrategy
391+unliftStrategy = do
392+ IdE (IOE unlift) <- getEffect
393+ pure unlift
309394
310−-- | Opaque representation of the 'Eff' environment at the point of calling the
311−-- 'send' function, i.e. right before the control is passed to the effect
312−-- handler.
395+-- | Locally override the 'UnliftStrategy' with the given value.
313396--
314−-- The second type variable represents effects of a handler and is needed for
315−-- technical reasons to guarantee soundness.
316−newtype LocalEnv (localEs :: [Effect]) (handlerEs :: [Effect]) = LocalEnv (Env localEs)
317−
318−-- | Type signature of the effect handler.
319−type EffectHandler e es
320− = forall a localEs. HasCallStack
321− => LocalEnv localEs es
322− -- ^ Capture of the local environment for handling local 'Eff' computations
323− -- when @e@ is a higher order effect.
324− -> e (Eff localEs) a
325− -- ^ The effect performed in the local environment.
326− -> Eff es a
397+-- /Note:/ the strategy is reset to 'SeqUnlift' for new threads.
398+withUnliftStrategy :: IOE :> es => UnliftStrategy -> Eff es a -> Eff es a
399+withUnliftStrategy unlift = localEffect $ \_ -> IdE (IOE unlift)
327400
328−-- | An adapter for dynamically dispatched effects.
401+-- | Create an unlifting function with the current 'UnliftStrategy'.
329402--
330−-- Represents the effect handler bundled with its environment.
331−data HandlerA :: Effect -> Type where
332− HandlerA :: !(Env es) -> !(EffectHandler e es) -> HandlerA e
333−
334−-- | Run a dynamically dispatched effect with the given handler.
335−runHandler :: HandlerA e -> Eff (e : es) a -> Eff es a
336−runHandler e m = unsafeEff $ \es0 -> do
337− size0 <- sizeEnv es0
338− E.bracket (unsafeConsEnv e relinker es0)
339− (unsafeTailEnv size0)
340− (\es -> unEff m es)
341− where
342− relinker :: Relinker HandlerA e
343− relinker = Relinker $ \relink (HandlerA handlerEs handle) -> do
344− newHandlerEs <- relink handlerEs
345− pure $ HandlerA newHandlerEs handle
346−
347−-- | Send an operation of the given effect to its handler for execution.
348−send :: (HasCallStack, e :> es) => e (Eff es) a -> Eff es a
349−send op = unsafeEff $ \es -> do
350− HandlerA handlerEs handle <- getEnv es
351− unEff (handle (LocalEnv es) op) handlerEs
403+-- This function is equivalent to 'withRunInIO', but has a 'HasCallStack'
404+-- constraint for accurate stack traces in case an insufficiently powerful
405+-- 'UnliftStrategy' is used and the unlifting function fails.
406+withEffToIO
407+ :: (HasCallStack, IOE :> es)
408+ => ((forall r. Eff es r -> IO r) -> IO a)
409+ -- ^ Continuation with the unlifting function in scope.
410+ -> Eff es a
411+withEffToIO f = unliftStrategy >>= \case
412+ SeqUnlift -> unsafeEff $ \es -> seqUnliftEff es f
413+ ConcUnlift p b -> withUnliftStrategy SeqUnlift $ do
414+ unsafeEff $ \es -> concUnliftEff es p b f
352415
353416----------------------------------------
354−-- Static dispatch
417+-- Helpers
355418
356−-- | An adapter for statically dispatched effects.
357−--
358−-- Represents an arbitrary data type with the appropriate number of phantom type
359−-- parameters.
360−newtype DataA :: Effect -> Type where
361− DataA :: (forall m r. e m r) -> DataA e
362−
363−-- | Run a statically dispatched effect with the given initial state and return
364−-- the final value along with the final state.
365−runData :: DataA e -> Eff (e : es) a -> Eff es (a, DataA e)
366−runData e0 m = unsafeEff $ \es0 -> do
419+runEffect :: handler e -> Eff (e : es) a -> Eff es (a, handler e)
420+runEffect e0 m = unsafeEff $ \es0 -> do
367421 size0 <- sizeEnv es0
368− E.bracket (unsafeConsEnv e0 noRelinker es0)
369− (unsafeTailEnv size0)
370− (\es -> (,) <$> unEff m es <*> getEnv es)
371−
372−-- | Run a statically dispatched effect with the given initial state and return
373−-- the final value, discarding the final state.
374−evalData :: DataA e -> Eff (e : es) a -> Eff es a
375−evalData e m = unsafeEff $ \es0 -> do
422+ bracket (unsafeConsEnv e0 noRelinker es0)
423+ (unsafeTailEnv size0)
424+ (\es -> (,) <$> unEff m es <*> getEnv es)
425+
426+evalEffect :: handler e -> Eff (e : es) a -> Eff es a
427+evalEffect e m = unsafeEff $ \es0 -> do
376428 size0 <- sizeEnv es0
377− E.bracket (unsafeConsEnv e noRelinker es0)
378− (unsafeTailEnv size0)
379− (\es -> unEff m es)
380−
381−-- | Run a statically dispatched effect with the given initial state and return
382−-- the final state, discarding the final value.
383−execData :: DataA e -> Eff (e : es) a -> Eff es (DataA e)
384−execData e0 m = unsafeEff $ \es0 -> do
429+ bracket (unsafeConsEnv e noRelinker es0)
430+ (unsafeTailEnv size0)
431+ (\es -> unEff m es)
432+
433+execEffect :: handler e -> Eff (e : es) a -> Eff es (handler e)
434+execEffect e0 m = unsafeEff $ \es0 -> do
385435 size0 <- sizeEnv es0
386− E.bracket (unsafeConsEnv e0 noRelinker es0)
387− (unsafeTailEnv size0)
388− (\es -> unEff m es *> getEnv es)
389−
390−-- | Fetch the current state of the effect.
391−getData :: e :> es => Eff es (DataA e)
392−getData = unsafeEff $ \es -> getEnv es
393−
394−-- | Set the current state of the effect to the given value.
395−putData :: e :> es => DataA e -> Eff es ()
396−putData e = unsafeEff $ \es -> putEnv es e
397−
398−-- | Apply the function to the current state of the effect and return a value.
399−stateData :: e :> es => (DataA e -> (a, DataA e)) -> Eff es a
400−stateData f = unsafeEff $ \es -> stateEnv es f
401−
402−-- | Apply the monadic function to the current state of the effect and return a
403−-- value.
404−stateDataM :: e :> es => (DataA e -> Eff es (a, DataA e)) -> Eff es a
405−stateDataM f = unsafeEff $ \es -> E.mask $ \release -> do
436+ bracket (unsafeConsEnv e0 noRelinker es0)
437+ (unsafeTailEnv size0)
438+ (\es -> unEff m es *> getEnv es)
439+
440+getEffect :: e :> es => Eff es (handler e)
441+getEffect = unsafeEff $ \es -> getEnv es
442+
443+putEffect :: e :> es => handler e -> Eff es ()
444+putEffect e = unsafeEff $ \es -> putEnv es e
445+
446+stateEffect :: e :> es => (handler e -> (a, handler e)) -> Eff es a
447+stateEffect f = unsafeEff $ \es -> stateEnv es f
448+
449+stateEffectM :: e :> es => (handler e -> Eff es (a, handler e)) -> Eff es a
450+stateEffectM f = unsafeEff $ \es -> mask $ \release -> do
406451 (a, e) <- (\e -> release $ unEff (f e) es) =<< getEnv es
407452 putEnv es e
408453 pure a
409454
410−-- | Execute a computation with a temporarily modified state of the effect.
411−localData :: e :> es => (DataA e -> DataA e) -> Eff es a -> Eff es a
412−localData f m = unsafeEff $ \es -> do
413− E.bracket (stateEnv es $ \e -> (e, f e))
414− (\e -> putEnv es e)
415− (\_ -> unEff m es)
455+localEffect :: e :> es => (handler e -> handler e) -> Eff es a -> Eff es a
456+localEffect f m = unsafeEff $ \es -> do
457+ bracket (stateEnv es $ \e -> (e, f e))
458+ (\e -> putEnv es e)
459+ (\_ -> unEff m es)