@@ -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
@@ -22,11 +22,11 @@ module Effectful.Internal.Monad
2222 , IOE
2323 , runEff
2424
25− -- * Prim
25+ -- * Primitive
2626 , Prim
2727 , runPrim
2828
29− -- * Unlift strategies
29+ -- ** Unlift strategies
3030 , UnliftStrategy(..)
3131 , Persistence(..)
3232 , Limit(..)
@@ -34,29 +34,34 @@ module Effectful.Internal.Monad
3434 , withUnliftStrategy
3535 , withEffToIO
3636
37− -- ** Low-level unlifts
37+ --- *** Low-level helpers
3838 , seqUnliftIO
3939 , concUnliftIO
4040
4141 -- * Effects
4242
43− -- ** Dynamic dispatch
43+ -- ** Handler
4444 , EffectHandler
4545 , LocalEnv(..)
46− , HandlerA(..)
46+ , Handler(..)
4747 , runHandler
48+
49+ -- *** Sending operations
4850 , send
4951
50− -- ** Static dispatch
51− , DataA(..)
52− , runData
53− , evalData
54− , execData
55− , getData
56− , putData
57− , stateData
58− , stateDataM
59− , localData
52+ -- ** Primitive
53+
54+ -- *** Running
55+ , runEffect
56+ , evalEffect
57+ , execEffect
58+
59+ -- *** Modification
60+ , getEffect
61+ , putEffect
62+ , stateEffect
63+ , stateEffectM
64+ , localEffect
6065 ) where
6166
6267import Control.Applicative (liftA2)
@@ -79,43 +84,44 @@ import Effectful.Internal.Unlift
7984
8085type role Eff nominal representational
8186
82−-- | The 'Eff' monad provides the implementation of a computation that performs
87+-- | The 'Eff' monad provides the implementation of an operation that performs
8388-- 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:
89+-- contains all the effects that the operation may perform. For example, an
90+-- operation that produces an 'Integer' by consuming a 'String' from the global
91+-- environment and acting upon a single mutable cell containing a 'Bool' would
92+-- have the following type:
8893--
8994-- @
90−-- 'Eff' '['Effectful.Reader.Reader' 'String', 'Effectful.State.Local.State' 'Bool'] 'Integer'
95+-- 'Eff' '['Effectful.Reader.Reader' 'String', 'Effectful.State.State' 'Bool'] 'Integer'
9196-- @
9297--
9398-- Normally, a concrete list of effects is not used to parameterize 'Eff'.
9499-- 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:
100+-- effects without coupling an operation to a concrete list of effects. For
101+-- example, the above example would more commonly be expressed with the
102+-- following type:
97103--
98104-- @
99−-- ('Effectful.Reader.Reader' 'String' ':>' es, 'Effectful.State.Local.State' 'Bool' ':>' es) => 'Eff' es 'Integer'
105+-- ('Effectful.Reader.Reader' 'String' ':>' es, 'Effectful.State.State' 'Bool' ':>' es) => 'Eff' es 'Integer'
100106-- @
101107--
102−-- This abstraction allows the computation to be used in functions that may
108+-- This abstraction allows the operation to be used in functions that may
103109-- perform other effects, and it also allows the effects to be handled in any
104110-- order.
105111newtype Eff (es :: [Effect]) a = Eff (Env es -> IO a)
106112 deriving (Monoid, Semigroup)
107113
108−-- | Run a pure 'Eff' computation.
114+-- | Run a pure 'Eff' operation.
109115--
110−-- For running computations with side effects see 'runEff'.
116+-- For running operations with side effects see 'runEff'.
111117runPureEff :: Eff '[] a -> a
112118runPureEff (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).
119+ -- unsafePerformIO is safe here since IOE was not on the stack, so no IO with
120+ -- side effects was performed (unless someone sneakily introduced side effects
121+ -- with unsafeEff, but then all bets are off).
116122 --
117123 -- Moreover, internals don't allocate any resources that require explicit
118− -- cleanup actions to run.
124+ -- cleanup actions to run, so the "Dupable" part should also be just fine.
119125 unsafeDupablePerformIO $ m =<< emptyEnv
120126
121127----------------------------------------
@@ -128,14 +134,14 @@ unEff (Eff m) = m
128134-- | Access the underlying 'IO' monad along with the environment.
129135--
130136-- This function is __unsafe__ because it can be used to introduce arbitrary
131−-- 'IO' actions into pure 'Eff' computations.
137+-- 'IO' actions into pure 'Eff' operations.
132138unsafeEff :: (Env es -> IO a) -> Eff es a
133139unsafeEff m = Eff (oneShot m)
134140
135141-- | Access the underlying 'IO' monad.
136142--
137143-- This function is __unsafe__ because it can be used to introduce arbitrary
138−-- 'IO' actions into pure 'Eff' computations.
144+-- 'IO' actions into pure 'Eff' operations.
139145unsafeEff_ :: IO a -> Eff es a
140146unsafeEff_ m = unsafeEff $ \_ -> m
141147
@@ -145,12 +151,12 @@ unsafeEff_ m = unsafeEff $ \_ -> m
145151-- | Get the current 'UnliftStrategy'.
146152unliftStrategy :: IOE :> es => Eff es UnliftStrategy
147153unliftStrategy = do
148− DataA (IOE unlift) <- getData
154+ IdE (IOE unlift) <- getEffect
149155 pure unlift
150156
151157-- | Locally override the 'UnliftStrategy' with the given value.
152158withUnliftStrategy :: IOE :> es => UnliftStrategy -> Eff es a -> Eff es a
153−withUnliftStrategy unlift = localData $ \_ -> DataA (IOE unlift)
159+withUnliftStrategy unlift = localEffect $ \_ -> IdE (IOE unlift)
154160
155161-- | Create an unlifting function with the current 'UnliftStrategy'.
156162--
@@ -158,7 +164,7 @@ withUnliftStrategy unlift = localData $ \_ -> DataA (IOE unlift)
158164-- constraint for accurate stack traces in case an insufficiently powerful
159165-- 'UnliftStrategy' is used and the unlifting function fails.
160166--
161−-- /Note:/ the strategy is reset to 'SeqUnlift' inside the continuation.
167+-- /Note:/ the strategy is reset to 'SeqUnlift' for new threads.
162168withEffToIO
163169 :: (HasCallStack, IOE :> es)
164170 => ((forall r. Eff es r -> IO r) -> IO a)
@@ -169,25 +175,21 @@ withEffToIO f = unliftStrategy >>= \case
169175 ConcUnlift p b -> withUnliftStrategy SeqUnlift $ do
170176 unsafeEff $ \es -> concUnliftIO es p b f
171177
172−-- | Create an unlifting function with the 'SeqUnlift' strategy.
178+-- | Lower 'Eff' operations into 'IO' ('SeqUnlift').
173179seqUnliftIO
174180 :: HasCallStack
175181 => Env es
176− -- ^ The environment.
177182 -> ((forall r. Eff es r -> IO r) -> IO a)
178− -- ^ Continuation with the unlifting function in scope.
179183 -> IO a
180184seqUnliftIO es k = seqUnlift k es unEff
181185
182−-- | Create an unlifting function with the 'ConcUnlift' strategy.
186+-- | Lower 'Eff' operations into 'IO' ('ConcUnlift').
183187concUnliftIO
184188 :: HasCallStack
185189 => Env es
186− -- ^ The environment.
187190 -> Persistence
188191 -> Limit
189192 -> ((forall r. Eff es r -> IO r) -> IO a)
190− -- ^ Continuation with the unlifting function in scope.
191193 -> IO a
192194concUnliftIO es persistence limit k = concUnlift persistence limit k es unEff
193195
@@ -258,15 +260,15 @@ instance Fail :> es => MonadFail (Eff es) where
258260----------------------------------------
259261-- IO
260262
261−-- | Run arbitrary 'IO' computations via 'MonadIO' or 'MonadUnliftIO'.
263+-- | Run arbitrary 'IO' operations via 'MonadIO' or 'MonadUnliftIO'.
262264newtype IOE :: Effect where
263265 IOE :: UnliftStrategy -> IOE m r
264266
265−-- | Run an 'Eff' computation with side effects.
267+-- | Run an 'Eff' operation with side effects.
266268--
267−-- For running pure computations see 'runPureEff'.
269+-- For running pure operations see 'runPureEff'.
268270runEff :: Eff '[IOE] a -> IO a
269−runEff m = unEff (evalData (DataA (IOE SeqUnlift)) m) =<< emptyEnv
271+runEff m = unEff (evalEffect (IdE (IOE SeqUnlift)) m) =<< emptyEnv
270272
271273instance IOE :> es => MonadIO (Eff es) where
272274 liftIO = unsafeEff_
@@ -290,20 +292,20 @@ instance IOE :> es => MonadBaseControl IO (Eff es) where
290292----------------------------------------
291293-- Primitive
292294
293−-- | Provide the ability to perform primitive state-transformer actions.
295+-- | An effect to perform primitive state-transformer actions.
294296data Prim :: Effect where
295297 Prim :: Prim m r
296298
297−-- | Run an 'Eff' computation with primitive state-transformer actions.
299+-- | Run an 'Eff' operation with primitive state-transformer actions.
298300runPrim :: IOE :> es => Eff (Prim : es) a -> Eff es a
299−runPrim = evalData (DataA Prim)
301+runPrim = evalEffect (IdE Prim)
300302
301303instance Prim :> es => PrimMonad (Eff es) where
302304 type PrimState (Eff es) = RealWorld
303305 primitive = unsafeEff_ . IO
304306
305307----------------------------------------
306−-- Dynamic dispatch
308+-- Handler
307309
308310type role LocalEnv nominal nominal
309311
@@ -319,97 +321,78 @@ newtype LocalEnv (localEs :: [Effect]) (handlerEs :: [Effect]) = LocalEnv (Env l
319321type EffectHandler e es
320322 = forall a localEs. HasCallStack
321323 => LocalEnv localEs es
322− -- ^ Capture of the local environment for handling local 'Eff' computations
323− -- when @e@ is a higher order effect.
324+ -- ^ Capture of the local environment for handling local 'Eff' operations when
325+ -- @e@ is a higher order effect.
324326 -> e (Eff localEs) a
325327 -- ^ The effect performed in the local environment.
326328 -> Eff es a
327329
328330-- | An adapter for dynamically dispatched effects.
329331--
330−-- Represents the effect handler bundled with its environment.
331−data HandlerA :: Effect -> Type where
332− HandlerA :: !(Env es) -> !(EffectHandler e es) -> HandlerA e
332+-- Represents the effect handler bundled along with its environment.
333+data Handler :: Effect -> Type where
334+ Handler :: !(Env es) -> !(EffectHandler e es) -> Handler e
333335
334−-- | Run a dynamically dispatched effect with the given handler.
335−runHandler :: HandlerA e -> Eff (e : es) a -> Eff es a
336+-- | Run the effect with the given handler.
337+runHandler :: Handler e -> Eff (e : es) a -> Eff es a
336338runHandler e m = unsafeEff $ \es0 -> do
337339 size0 <- sizeEnv es0
338340 E.bracket (unsafeConsEnv e relinker es0)
339341 (unsafeTailEnv size0)
340342 (\es -> unEff m es)
341343 where
342− relinker :: Relinker HandlerA e
343− relinker = Relinker $ \relink (HandlerA handlerEs handle) -> do
344− newHandlerEs <- relink handlerEs
345− pure $ HandlerA newHandlerEs handle
344+ relinker :: Relinker Handler e
345+ relinker = Relinker $ \relink (Handler env handle) -> do
346+ newEnv <- relink env
347+ pure $ Handler newEnv handle
346348
347349-- | Send an operation of the given effect to its handler for execution.
348350send :: (HasCallStack, e :> es) => e (Eff es) a -> Eff es a
349351send op = unsafeEff $ \es -> do
350− HandlerA handlerEs handle <- getEnv es
352+ Handler handlerEs handle <- getEnv es
351353 unEff (handle (LocalEnv es) op) handlerEs
352354
353355----------------------------------------
354−-- Static dispatch
356+-- Helpers
355357
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
358+runEffect :: adapter e -> Eff (e : es) a -> Eff es (a, adapter e)
359+runEffect e0 m = unsafeEff $ \es0 -> do
367360 size0 <- sizeEnv es0
368361 E.bracket (unsafeConsEnv e0 noRelinker es0)
369362 (unsafeTailEnv size0)
370363 (\es -> (,) <$> unEff m es <*> getEnv es)
371364
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
365+evalEffect :: adapter e -> Eff (e : es) a -> Eff es a
366+evalEffect e m = unsafeEff $ \es0 -> do
376367 size0 <- sizeEnv es0
377368 E.bracket (unsafeConsEnv e noRelinker es0)
378369 (unsafeTailEnv size0)
379370 (\es -> unEff m es)
380371
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
372+execEffect :: adapter e -> Eff (e : es) a -> Eff es (adapter e)
373+execEffect e0 m = unsafeEff $ \es0 -> do
385374 size0 <- sizeEnv es0
386375 E.bracket (unsafeConsEnv e0 noRelinker es0)
387376 (unsafeTailEnv size0)
388377 (\es -> unEff m es *> getEnv es)
389378
390−-- | Fetch the current state of the effect.
391−getData :: e :> es => Eff es (DataA e)
392−getData = unsafeEff $ \es -> getEnv es
379+getEffect :: e :> es => Eff es (adapter e)
380+getEffect = unsafeEff $ \es -> getEnv es
393381
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
382+putEffect :: e :> es => adapter e -> Eff es ()
383+putEffect e = unsafeEff $ \es -> putEnv es e
397384
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
385+stateEffect :: e :> es => (adapter e -> (a, adapter e)) -> Eff es a
386+stateEffect f = unsafeEff $ \es -> stateEnv es f
401387
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
388+stateEffectM :: e :> es => (adapter e -> Eff es (a, adapter e)) -> Eff es a
389+stateEffectM f = unsafeEff $ \es -> E.mask $ \release -> do
406390 (a, e) <- (\e -> release $ unEff (f e) es) =<< getEnv es
407391 putEnv es e
408392 pure a
409393
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
394+localEffect :: e :> es => (adapter e -> adapter e) -> Eff es a -> Eff es a
395+localEffect f m = unsafeEff $ \es -> do
413396 E.bracket (stateEnv es $ \e -> (e, f e))
414397 (\e -> putEnv es e)
415398 (\_ -> unEff m es)