@@ -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,41 +22,49 @@ 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(..)
3333 , unliftStrategy
3434 , withUnliftStrategy
3535 , withEffToIO
36+ , UnliftError(..)
3637
37− -- ** Low-level unlifts
38− , seqUnliftIO
39− , concUnliftIO
38+ --- *** Low-level helpers
39+ , unsafeLiftMapIO
40+ , unsafeUnliftToEff
41+ , seqUnliftEff
42+ , concUnliftEff
4043
4144 -- * Effects
4245
43− -- ** Dynamic dispatch
46+ -- ** Handler
4447 , EffectHandler
4548 , LocalEnv(..)
46− , HandlerA(..)
49+ , Handler(..)
4750 , runHandler
51+
52+ -- *** Sending operations
4853 , send
4954
50− -- ** Static dispatch
51− , DataA(..)
52− , runData
53− , evalData
54− , execData
55− , getData
56− , putData
57− , stateData
58− , stateDataM
59− , localData
55+ -- ** Primitive
56+
57+ -- *** Running
58+ , runEffect
59+ , evalEffect
60+ , execEffect
61+
62+ -- *** Modification
63+ , getEffect
64+ , putEffect
65+ , stateEffect
66+ , stateEffectM
67+ , localEffect
6068 ) where
6169
6270import Control.Applicative (liftA2)
@@ -79,43 +87,44 @@ import Effectful.Internal.Unlift
7987
8088type role Eff nominal representational
8189
82−-- | The 'Eff' monad provides the implementation of a computation that performs
90+-- | The 'Eff' monad provides the implementation of an operation that performs
8391-- 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:
92+-- contains all the effects that the operation may perform. For example, an
93+-- operation that produces an 'Integer' by consuming a 'String' from the global
94+-- environment and acting upon a single mutable cell containing a 'Bool' would
95+-- have the following type:
8896--
8997-- @
90−-- 'Eff' '['Effectful.Reader.Reader' 'String', 'Effectful.State.Local.State' 'Bool'] 'Integer'
98+-- 'Eff' '['Effectful.Reader.Reader' 'String', 'Effectful.State.State' 'Bool'] 'Integer'
9199-- @
92100--
93101-- Normally, a concrete list of effects is not used to parameterize 'Eff'.
94102-- 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:
103+-- effects without coupling an operation to a concrete list of effects. For
104+-- example, the above example would more commonly be expressed with the
105+-- following type:
97106--
98107-- @
99−-- ('Effectful.Reader.Reader' 'String' ':>' es, 'Effectful.State.Local.State' 'Bool' ':>' es) => 'Eff' es 'Integer'
108+-- ('Effectful.Reader.Reader' 'String' ':>' es, 'Effectful.State.State' 'Bool' ':>' es) => 'Eff' es 'Integer'
100109-- @
101110--
102−-- This abstraction allows the computation to be used in functions that may
111+-- This abstraction allows the operation to be used in functions that may
103112-- perform other effects, and it also allows the effects to be handled in any
104113-- order.
105114newtype Eff (es :: [Effect]) a = Eff (Env es -> IO a)
106115 deriving (Monoid, Semigroup)
107116
108−-- | Run a pure 'Eff' computation.
117+-- | Run a pure 'Eff' operation.
109118--
110−-- For running computations with side effects see 'runEff'.
119+-- For running operations with side effects see 'runEff'.
111120runPureEff :: Eff '[] a -> a
112121runPureEff (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).
122+ -- unsafePerformIO is safe here since IOE was not on the stack, so no IO with
123+ -- side effects was performed (unless someone sneakily introduced side effects
124+ -- with unsafeEff, but then all bets are off).
116125 --
117126 -- Moreover, internals don't allocate any resources that require explicit
118− -- cleanup actions to run.
127+ -- cleanup actions to run, so the "Dupable" part should also be just fine.
119128 unsafeDupablePerformIO $ m =<< emptyEnv
120129
121130----------------------------------------
@@ -128,29 +137,69 @@ unEff (Eff m) = m
128137-- | Access the underlying 'IO' monad along with the environment.
129138--
130139-- This function is __unsafe__ because it can be used to introduce arbitrary
131−-- 'IO' actions into pure 'Eff' computations.
140+-- 'IO' actions into pure 'Eff' operations.
132141unsafeEff :: (Env es -> IO a) -> Eff es a
133142unsafeEff m = Eff (oneShot m)
134143
135144-- | Access the underlying 'IO' monad.
136145--
137146-- This function is __unsafe__ because it can be used to introduce arbitrary
138−-- 'IO' actions into pure 'Eff' computations.
147+-- 'IO' actions into pure 'Eff' operations.
139148unsafeEff_ :: IO a -> Eff es a
140149unsafeEff_ m = unsafeEff $ \_ -> m
141150
151+-- | Utility for lifting 'IO' operations of type
152+--
153+-- @'IO' a -> 'IO' b@
154+--
155+-- to
156+--
157+-- @'Eff' es a -> 'Eff' es b@
158+--
159+-- This function is __unsafe__ because:
160+--
161+-- - It can be used to introduce arbitrary 'IO' actions into pure 'Eff'
162+-- operations.
163+--
164+-- - The 'IO' operation must not run its argument in a separate thread, but it's
165+-- not checked anywhere.
166+unsafeLiftMapIO :: (IO a -> IO b) -> Eff es a -> Eff es b
167+unsafeLiftMapIO f m = unsafeEff $ \es -> f (unEff m es)
168+
142169----------------------------------------
143−-- Unlifting IO
170+-- Unlifting Eff
144171
145172-- | Get the current 'UnliftStrategy'.
146173unliftStrategy :: IOE :> es => Eff es UnliftStrategy
147174unliftStrategy = do
148− DataA (IOE unlift) <- getData
175+ IdE (IOE unlift) <- getEffect
149176 pure unlift
150177
151178-- | Locally override the 'UnliftStrategy' with the given value.
152179withUnliftStrategy :: IOE :> es => UnliftStrategy -> Eff es a -> Eff es a
153−withUnliftStrategy unlift = localData $ \_ -> DataA (IOE unlift)
180+withUnliftStrategy unlift = localEffect $ \_ -> IdE (IOE unlift)
181+
182+-- | Lower 'Eff' operations into 'IO' ('SeqUnlift').
183+--
184+-- Exceptions thrown by this function:
185+--
186+-- - 'InvalidUseOfSeqUnlift' if the unlift function is used in another thread.
187+seqUnliftEff
188+ :: HasCallStack
189+ => Env es
190+ -> ((forall r. Eff es r -> IO r) -> IO a)
191+ -> IO a
192+seqUnliftEff es k = seqUnliftIO k es unEff
193+
194+-- | Lower 'Eff' operations into 'IO' ('ConcUnlift').
195+concUnliftEff
196+ :: HasCallStack
197+ => Env es
198+ -> Persistence
199+ -> Limit
200+ -> ((forall r. Eff es r -> IO r) -> IO a)
201+ -> IO a
202+concUnliftEff es persistence limit k = concUnliftIO persistence limit k es unEff
154203
155204-- | Create an unlifting function with the current 'UnliftStrategy'.
156205--
@@ -158,38 +207,25 @@ withUnliftStrategy unlift = localData $ \_ -> DataA (IOE unlift)
158207-- constraint for accurate stack traces in case an insufficiently powerful
159208-- 'UnliftStrategy' is used and the unlifting function fails.
160209--
161−-- /Note:/ the strategy is reset to 'SeqUnlift' inside the continuation.
210+-- /Note:/ the strategy is reset to 'SeqUnlift' for new threads.
162211withEffToIO
163212 :: (HasCallStack, IOE :> es)
164213 => ((forall r. Eff es r -> IO r) -> IO a)
165214 -- ^ Continuation with the unlifting function in scope.
166215 -> Eff es a
167216withEffToIO f = unliftStrategy >>= \case
168− SeqUnlift -> unsafeEff $ \es -> seqUnliftIO es f
217+ SeqUnlift -> unsafeEff $ \es -> seqUnliftEff es f
169218 ConcUnlift p b -> withUnliftStrategy SeqUnlift $ do
170− unsafeEff $ \es -> concUnliftIO es p b f
171−
172−-- | Create an unlifting function with the 'SeqUnlift' strategy.
173−seqUnliftIO
174− :: HasCallStack
175− => Env es
176− -- ^ The environment.
177− -> ((forall r. Eff es r -> IO r) -> IO a)
178− -- ^ Continuation with the unlifting function in scope.
179− -> IO a
180−seqUnliftIO es k = seqUnlift k es unEff
219+ unsafeEff $ \es -> concUnliftEff es p b f
181220
182−-- | Create an unlifting function with the 'ConcUnlift' strategy.
183−concUnliftIO
184− :: HasCallStack
185− => Env es
186− -- ^ The environment.
187− -> Persistence
188− -> Limit
189− -> ((forall r. Eff es r -> IO r) -> IO a)
190− -- ^ Continuation with the unlifting function in scope.
191− -> IO a
192−concUnliftIO es persistence limit k = concUnlift persistence limit k es unEff
221+-- | An unlifing function that lifts a function and its callback from 'IO' to
222+-- 'Eff'.
223+--
224+-- This function is unsafe since there are no checks wrt. to concurrency at all.
225+-- Use with care.
226+unsafeUnliftToEff :: ((a -> IO b) -> IO c) -> (a -> Eff es b) -> Eff es c
227+unsafeUnliftToEff k f = unsafeEff $ \es -> do
228+ k ((`unEff` es) . f)
193229
194230----------------------------------------
195231-- Base
@@ -258,15 +294,15 @@ instance Fail :> es => MonadFail (Eff es) where
258294----------------------------------------
259295-- IO
260296
261−-- | Run arbitrary 'IO' computations via 'MonadIO' or 'MonadUnliftIO'.
297+-- | Run arbitrary 'IO' operations via 'MonadIO' or 'MonadUnliftIO'.
262298newtype IOE :: Effect where
263299 IOE :: UnliftStrategy -> IOE m r
264300
265−-- | Run an 'Eff' computation with side effects.
301+-- | Run an 'Eff' operation with side effects.
266302--
267−-- For running pure computations see 'runPureEff'.
303+-- For running pure operations see 'runPureEff'.
268304runEff :: Eff '[IOE] a -> IO a
269−runEff m = unEff (evalData (DataA (IOE SeqUnlift)) m) =<< emptyEnv
305+runEff m = unEff (evalEffect (IdE (IOE SeqUnlift)) m) =<< emptyEnv
270306
271307instance IOE :> es => MonadIO (Eff es) where
272308 liftIO = unsafeEff_
@@ -290,20 +326,20 @@ instance IOE :> es => MonadBaseControl IO (Eff es) where
290326----------------------------------------
291327-- Primitive
292328
293−-- | Provide the ability to perform primitive state-transformer actions.
329+-- | An effect to perform primitive state-transformer actions.
294330data Prim :: Effect where
295331 Prim :: Prim m r
296332
297−-- | Run an 'Eff' computation with primitive state-transformer actions.
333+-- | Run an 'Eff' operation with primitive state-transformer actions.
298334runPrim :: IOE :> es => Eff (Prim : es) a -> Eff es a
299−runPrim = evalData (DataA Prim)
335+runPrim = evalEffect (IdE Prim)
300336
301337instance Prim :> es => PrimMonad (Eff es) where
302338 type PrimState (Eff es) = RealWorld
303339 primitive = unsafeEff_ . IO
304340
305341----------------------------------------
306−-- Dynamic dispatch
342+-- Handler
307343
308344type role LocalEnv nominal nominal
309345
@@ -319,97 +355,76 @@ newtype LocalEnv (localEs :: [Effect]) (handlerEs :: [Effect]) = LocalEnv (Env l
319355type EffectHandler e es
320356 = forall a localEs. HasCallStack
321357 => LocalEnv localEs es
322− -- ^ Capture of the local environment for handling local 'Eff' computations
323− -- when @e@ is a higher order effect.
358+ -- ^ Capture of the local environment for handling local 'Eff' operations when
359+ -- @e@ is a higher order effect.
324360 -> e (Eff localEs) a
325361 -- ^ The effect performed in the local environment.
326362 -> Eff es a
327363
328−-- | An adapter for dynamically dispatched effects.
329−--
330−-- Represents the effect handler bundled with its environment.
331−data HandlerA :: Effect -> Type where
332− HandlerA :: !(Env es) -> !(EffectHandler e es) -> HandlerA e
364+-- | An effect handler bundled along with its environment.
365+data Handler :: Effect -> Type where
366+ Handler :: !(Env es) -> !(EffectHandler e es) -> Handler e
333367
334−-- | Run a dynamically dispatched effect with the given handler.
335−runHandler :: HandlerA e -> Eff (e : es) a -> Eff es a
368+-- | Run the effect with the given handler.
369+runHandler :: Handler e -> Eff (e : es) a -> Eff es a
336370runHandler e m = unsafeEff $ \es0 -> do
337371 size0 <- sizeEnv es0
338372 E.bracket (unsafeConsEnv e relinker es0)
339373 (unsafeTailEnv size0)
340374 (\es -> unEff m es)
341375 where
342− relinker :: Relinker HandlerA e
343− relinker = Relinker $ \relink (HandlerA handlerEs handle) -> do
344− newHandlerEs <- relink handlerEs
345− pure $ HandlerA newHandlerEs handle
376+ relinker :: Relinker Handler e
377+ relinker = Relinker $ \relink (Handler env handle) -> do
378+ newEnv <- relink env
379+ pure $ Handler newEnv handle
346380
347381-- | Send an operation of the given effect to its handler for execution.
348382send :: (HasCallStack, e :> es) => e (Eff es) a -> Eff es a
349383send op = unsafeEff $ \es -> do
350− HandlerA handlerEs handle <- getEnv es
351− unEff (handle (LocalEnv es) op) handlerEs
384+ Handler env handle <- getEnv es
385+ unEff (handle (LocalEnv es) op) env
352386
353387----------------------------------------
354−-- Static dispatch
388+-- Helpers
355389
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
390+runEffect :: handler e -> Eff (e : es) a -> Eff es (a, handler e)
391+runEffect e0 m = unsafeEff $ \es0 -> do
367392 size0 <- sizeEnv es0
368393 E.bracket (unsafeConsEnv e0 noRelinker es0)
369394 (unsafeTailEnv size0)
370395 (\es -> (,) <$> unEff m es <*> getEnv es)
371396
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
397+evalEffect :: handler e -> Eff (e : es) a -> Eff es a
398+evalEffect e m = unsafeEff $ \es0 -> do
376399 size0 <- sizeEnv es0
377400 E.bracket (unsafeConsEnv e noRelinker es0)
378401 (unsafeTailEnv size0)
379402 (\es -> unEff m es)
380403
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
404+execEffect :: handler e -> Eff (e : es) a -> Eff es (handler e)
405+execEffect e0 m = unsafeEff $ \es0 -> do
385406 size0 <- sizeEnv es0
386407 E.bracket (unsafeConsEnv e0 noRelinker es0)
387408 (unsafeTailEnv size0)
388409 (\es -> unEff m es *> getEnv es)
389410
390−-- | Fetch the current state of the effect.
391−getData :: e :> es => Eff es (DataA e)
392−getData = unsafeEff $ \es -> getEnv es
411+getEffect :: e :> es => Eff es (handler e)
412+getEffect = unsafeEff $ \es -> getEnv es
393413
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
414+putEffect :: e :> es => handler e -> Eff es ()
415+putEffect e = unsafeEff $ \es -> putEnv es e
397416
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
417+stateEffect :: e :> es => (handler e -> (a, handler e)) -> Eff es a
418+stateEffect f = unsafeEff $ \es -> stateEnv es f
401419
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
420+stateEffectM :: e :> es => (handler e -> Eff es (a, handler e)) -> Eff es a
421+stateEffectM f = unsafeEff $ \es -> E.mask $ \release -> do
406422 (a, e) <- (\e -> release $ unEff (f e) es) =<< getEnv es
407423 putEnv es e
408424 pure a
409425
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
426+localEffect :: e :> es => (handler e -> handler e) -> Eff es a -> Eff es a
427+localEffect f m = unsafeEff $ \es -> do
413428 E.bracket (stateEnv es $ \e -> (e, f e))
414429 (\e -> putEnv es e)
415430 (\_ -> unEff m es)