@@ -6,7 +6,7 @@
6-- This module is intended for internal use only, and may change without warning
6-- This module is intended for internal use only, and may change without warning
7-- in subsequent releases.
7-- in subsequent releases.
8module Effectful.Internal.Monad
8module Effectful.Internal.Monad
9− ( -- * Monad
9+ ( -- * The 'Eff' monad
11 , runPureEff
11 , runPureEff
@@ -22,11 +22,11 @@ module Effectful.Internal.Monad
25− -- * Primitive
25+ -- * Prim
29− -- ** Unlift strategies
29+ -- * Unlift strategies
30 , UnliftStrategy(..)
30 , UnliftStrategy(..)
31 , Persistence(..)
31 , Persistence(..)
32 , Limit(..)
32 , Limit(..)
@@ -34,34 +34,29 @@ module Effectful.Internal.Monad
34 , withUnliftStrategy
34 , withUnliftStrategy
35 , withEffToIO
35 , withEffToIO
37− --- *** Low-level helpers
37+ -- ** Low-level unlifts
38 , seqUnliftIO
38 , seqUnliftIO
39 , concUnliftIO
39 , concUnliftIO
41 -- * Effects
41 -- * Effects
43− -- ** Handler
43+ -- ** Dynamic dispatch
44 , EffectHandler
44 , EffectHandler
45 , LocalEnv(..)
45 , LocalEnv(..)
46− , Handler(..)
46+ , HandlerA(..)
47 , runHandler
47 , runHandler
49− -- *** Sending operations
52− -- ** Primitive
50+ -- ** Static dispatch
54− -- *** Running
52+ , runData
55− , runEffect
53+ , evalData
56− , evalEffect
54+ , execData
57− , execEffect
55+ , getData
59− -- *** Modification
57+ , stateData
60− , getEffect
58+ , stateDataM
61− , putEffect
59+ , localData
67import Control.Applicative (liftA2)
62import Control.Applicative (liftA2)
@@ -84,44 +79,43 @@ import Effectful.Internal.Unlift
85type role Eff nominal representational
80type role Eff nominal representational
87−-- | The 'Eff' monad provides the implementation of an operation that performs
82+-- | The 'Eff' monad provides the implementation of a computation that performs
88-- an arbitrary set of effects. In @'Eff' es a@, @es@ is a type-level list that
83-- an arbitrary set of effects. In @'Eff' es a@, @es@ is a type-level list that
89−-- contains all the effects that the operation may perform. For example, an
84+-- contains all the effects that the computation may perform. For example, a
90−-- operation that produces an 'Integer' by consuming a 'String' from the global
85+-- computation that produces an 'Integer' by consuming a 'String' from the
91−-- environment and acting upon a single mutable value of type 'Bool' would have
86+-- global environment and acting upon a single mutable value of type 'Bool'
92−-- the following type:
87+-- would have the following type:
95−-- 'Eff' '['Effectful.Reader.Reader' 'String', 'Effectful.State.State' 'Bool'] 'Integer'
90+-- 'Eff' '['Effectful.Reader.Reader' 'String', 'Effectful.State.Local.State' 'Bool'] 'Integer'
98-- Normally, a concrete list of effects is not used to parameterize 'Eff'.
93-- Normally, a concrete list of effects is not used to parameterize 'Eff'.
99-- Instead, the '(:>)' type class is used to express constraints on the list of
94-- Instead, the '(:>)' type class is used to express constraints on the list of
100−-- effects without coupling an operation to a concrete list of effects. For
95+-- effects without coupling a computation to a concrete list of effects. For
101−-- example, the above example would more commonly be expressed with the
96+-- example, the above example would be expressed with the following type:
105−-- ('Effectful.Reader.Reader' 'String' ':>' es, 'Effectful.State.State' 'Bool' ':>' es) => 'Eff' es 'Integer'
99+-- ('Effectful.Reader.Reader' 'String' ':>' es, 'Effectful.State.Local.State' 'Bool' ':>' es) => 'Eff' es 'Integer'
108−-- This abstraction allows the operation to be used in functions that may
102+-- This abstraction allows the computation to be used in functions that may
109-- perform other effects, and it also allows the effects to be handled in any
103-- perform other effects, and it also allows the effects to be handled in any
111newtype Eff (es :: [Effect]) a = Eff (Env es -> IO a)
105newtype Eff (es :: [Effect]) a = Eff (Env es -> IO a)
112 deriving (Monoid, Semigroup)
106 deriving (Monoid, Semigroup)
114−-- | Run a pure 'Eff' operation.
108+-- | Run a pure 'Eff' computation.
116−-- For running operations with side effects see 'runEff'.
110+-- For running computations with side effects see 'runEff'.
117runPureEff :: Eff '[] a -> a
111runPureEff :: Eff '[] a -> a
118runPureEff (Eff m) =
112runPureEff (Eff m) =
119− -- unsafePerformIO is safe here since IOE was not on the stack, so no IO with
113+ -- unsafeDupablePerformIO is safe here since IOE was not on the stack, so no
120− -- side effects was performed (unless someone sneakily introduced side effects
114+ -- IO with side effects was performed (unless someone sneakily introduced side
121− -- with unsafeEff, but then all bets are off).
115+ -- effects with unsafeEff, but then all bets are off).
123 -- Moreover, internals don't allocate any resources that require explicit
117 -- Moreover, internals don't allocate any resources that require explicit
124− -- cleanup actions to run, so the "Dupable" part should also be just fine.
118+ -- cleanup actions to run.
125 unsafeDupablePerformIO $ m =<< emptyEnv
119 unsafeDupablePerformIO $ m =<< emptyEnv
127----------------------------------------
121----------------------------------------
@@ -134,14 +128,14 @@ unEff (Eff m) = m
134-- | Access the underlying 'IO' monad along with the environment.
128-- | Access the underlying 'IO' monad along with the environment.
136-- This function is __unsafe__ because it can be used to introduce arbitrary
130-- This function is __unsafe__ because it can be used to introduce arbitrary
137−-- 'IO' actions into pure 'Eff' operations.
131+-- 'IO' actions into pure 'Eff' computations.
138unsafeEff :: (Env es -> IO a) -> Eff es a
132unsafeEff :: (Env es -> IO a) -> Eff es a
139unsafeEff m = Eff (oneShot m)
133unsafeEff m = Eff (oneShot m)
141-- | Access the underlying 'IO' monad.
135-- | Access the underlying 'IO' monad.
143-- This function is __unsafe__ because it can be used to introduce arbitrary
137-- This function is __unsafe__ because it can be used to introduce arbitrary
144−-- 'IO' actions into pure 'Eff' operations.
138+-- 'IO' actions into pure 'Eff' computations.
145unsafeEff_ :: IO a -> Eff es a
139unsafeEff_ :: IO a -> Eff es a
146unsafeEff_ m = unsafeEff $ \_ -> m
140unsafeEff_ m = unsafeEff $ \_ -> m
@@ -151,12 +145,12 @@ unsafeEff_ m = unsafeEff $ \_ -> m
151-- | Get the current 'UnliftStrategy'.
145-- | Get the current 'UnliftStrategy'.
152unliftStrategy :: IOE :> es => Eff es UnliftStrategy
146unliftStrategy :: IOE :> es => Eff es UnliftStrategy
153unliftStrategy = do
147unliftStrategy = do
154− IdE (IOE unlift) <- getEffect
148+ DataA (IOE unlift) <- getData
155 pure unlift
149 pure unlift
157-- | Locally override the 'UnliftStrategy' with the given value.
151-- | Locally override the 'UnliftStrategy' with the given value.
158withUnliftStrategy :: IOE :> es => UnliftStrategy -> Eff es a -> Eff es a
152withUnliftStrategy :: IOE :> es => UnliftStrategy -> Eff es a -> Eff es a
159−withUnliftStrategy unlift = localEffect $ \_ -> IdE (IOE unlift)
153+withUnliftStrategy unlift = localData $ \_ -> DataA (IOE unlift)
161-- | Create an unlifting function with the current 'UnliftStrategy'.
155-- | Create an unlifting function with the current 'UnliftStrategy'.
@@ -164,7 +158,7 @@ withUnliftStrategy unlift = localEffect $ \_ -> IdE (IOE unlift)
164-- constraint for accurate stack traces in case an insufficiently powerful
158-- constraint for accurate stack traces in case an insufficiently powerful
165-- 'UnliftStrategy' is used and the unlifting function fails.
159-- 'UnliftStrategy' is used and the unlifting function fails.
167−-- /Note:/ the strategy is reset to 'SeqUnlift' for new threads.
161+-- /Note:/ the strategy is reset to 'SeqUnlift' inside the continuation.
168withEffToIO
162withEffToIO
169 :: (HasCallStack, IOE :> es)
163 :: (HasCallStack, IOE :> es)
170 => ((forall r. Eff es r -> IO r) -> IO a)
164 => ((forall r. Eff es r -> IO r) -> IO a)
@@ -264,15 +258,15 @@ instance Fail :> es => MonadFail (Eff es) where
264----------------------------------------
258----------------------------------------
267−-- | Run arbitrary 'IO' operations via 'MonadIO' or 'MonadUnliftIO'.
261+-- | Run arbitrary 'IO' computations via 'MonadIO' or 'MonadUnliftIO'.
268newtype IOE :: Effect where
262newtype IOE :: Effect where
269 IOE :: UnliftStrategy -> IOE m r
263 IOE :: UnliftStrategy -> IOE m r
271−-- | Run an 'Eff' operation with side effects.
265+-- | Run an 'Eff' computation with side effects.
273−-- For running pure operations see 'runPureEff'.
267+-- For running pure computations see 'runPureEff'.
274runEff :: Eff '[IOE] a -> IO a
268runEff :: Eff '[IOE] a -> IO a
275−runEff m = unEff (evalEffect (IdE (IOE SeqUnlift)) m) =<< emptyEnv
269+runEff m = unEff (evalData (DataA (IOE SeqUnlift)) m) =<< emptyEnv
277instance IOE :> es => MonadIO (Eff es) where
271instance IOE :> es => MonadIO (Eff es) where
278 liftIO = unsafeEff_
272 liftIO = unsafeEff_
@@ -300,16 +294,16 @@ instance IOE :> es => MonadBaseControl IO (Eff es) where
300data Prim :: Effect where
294data Prim :: Effect where
301 Prim :: Prim m r
295 Prim :: Prim m r
303−-- | Run an 'Eff' operation with primitive state-transformer actions.
297+-- | Run an 'Eff' computation with primitive state-transformer actions.
304runPrim :: IOE :> es => Eff (Prim : es) a -> Eff es a
298runPrim :: IOE :> es => Eff (Prim : es) a -> Eff es a
305−runPrim = evalEffect (IdE Prim)
299+runPrim = evalData (DataA Prim)
307instance Prim :> es => PrimMonad (Eff es) where
301instance Prim :> es => PrimMonad (Eff es) where
308 type PrimState (Eff es) = RealWorld
302 type PrimState (Eff es) = RealWorld
309 primitive = unsafeEff_ . IO
303 primitive = unsafeEff_ . IO
311----------------------------------------
305----------------------------------------
312−-- Handler
306+-- Dynamic dispatch
314type role LocalEnv nominal nominal
308type role LocalEnv nominal nominal
@@ -325,78 +319,97 @@ newtype LocalEnv (localEs :: [Effect]) (handlerEs :: [Effect]) = LocalEnv (Env l
325type EffectHandler e es
319type EffectHandler e es
326 = forall a localEs. HasCallStack
320 = forall a localEs. HasCallStack
327 => LocalEnv localEs es
321 => LocalEnv localEs es
328− -- ^ Capture of the local environment for handling local 'Eff' operations when
322+ -- ^ Capture of the local environment for handling local 'Eff' computations
329− -- @e@ is a higher order effect.
323+ -- when @e@ is a higher order effect.
330 -> e (Eff localEs) a
324 -> e (Eff localEs) a
331 -- ^ The effect performed in the local environment.
325 -- ^ The effect performed in the local environment.
332 -> Eff es a
326 -> Eff es a
334-- | An adapter for dynamically dispatched effects.
328-- | An adapter for dynamically dispatched effects.
336−-- Represents the effect handler bundled along with its environment.
330+-- Represents the effect handler bundled with its environment.
337−data Handler :: Effect -> Type where
331+data HandlerA :: Effect -> Type where
338− Handler :: !(Env es) -> !(EffectHandler e es) -> Handler e
332+ HandlerA :: !(Env es) -> !(EffectHandler e es) -> HandlerA e
340−-- | Run the effect with the given handler.
334+-- | Run a dynamically dispatched effect with the given handler.
341−runHandler :: Handler e -> Eff (e : es) a -> Eff es a
335+runHandler :: HandlerA e -> Eff (e : es) a -> Eff es a
342runHandler e m = unsafeEff $ \es0 -> do
336runHandler e m = unsafeEff $ \es0 -> do
343 size0 <- sizeEnv es0
337 size0 <- sizeEnv es0
344 E.bracket (unsafeConsEnv e relinker es0)
338 E.bracket (unsafeConsEnv e relinker es0)
345 (unsafeTailEnv size0)
339 (unsafeTailEnv size0)
346 (\es -> unEff m es)
340 (\es -> unEff m es)
348− relinker :: Relinker Handler e
342+ relinker :: Relinker HandlerA e
349− relinker = Relinker $ \relink (Handler handlerEs handle) -> do
343+ relinker = Relinker $ \relink (HandlerA handlerEs handle) -> do
350 newHandlerEs <- relink handlerEs
344 newHandlerEs <- relink handlerEs
351− pure $ Handler newHandlerEs handle
345+ pure $ HandlerA newHandlerEs handle
353-- | Send an operation of the given effect to its handler for execution.
347-- | Send an operation of the given effect to its handler for execution.
354send :: (HasCallStack, e :> es) => e (Eff es) a -> Eff es a
348send :: (HasCallStack, e :> es) => e (Eff es) a -> Eff es a
355send op = unsafeEff $ \es -> do
349send op = unsafeEff $ \es -> do
356− Handler handlerEs handle <- getEnv es
350+ HandlerA handlerEs handle <- getEnv es
357 unEff (handle (LocalEnv es) op) handlerEs
351 unEff (handle (LocalEnv es) op) handlerEs
359----------------------------------------
353----------------------------------------
360−-- Helpers
354+-- Static dispatch
356+-- | An adapter for statically dispatched effects.
358+-- Represents an arbitrary data type with the appropriate number of phantom type
360+newtype DataA :: Effect -> Type where
361+ DataA :: (forall m r. e m r) -> DataA e
363+-- | Run a statically dispatched effect with the given initial state and return
364+-- the final value along with the final state.
362−runEffect :: adapter e -> Eff (e : es) a -> Eff es (a, adapter e)
365+runData :: DataA e -> Eff (e : es) a -> Eff es (a, DataA e)
363−runEffect e0 m = unsafeEff $ \es0 -> do
366+runData e0 m = unsafeEff $ \es0 -> do
364 size0 <- sizeEnv es0
367 size0 <- sizeEnv es0
365 E.bracket (unsafeConsEnv e0 noRelinker es0)
368 E.bracket (unsafeConsEnv e0 noRelinker es0)
366 (unsafeTailEnv size0)
369 (unsafeTailEnv size0)
367 (\es -> (,) <$> unEff m es <*> getEnv es)
370 (\es -> (,) <$> unEff m es <*> getEnv es)
372+-- | Run a statically dispatched effect with the given initial state and return
373+-- the final value, discarding the final state.
369−evalEffect :: adapter e -> Eff (e : es) a -> Eff es a
374+evalData :: DataA e -> Eff (e : es) a -> Eff es a
370−evalEffect e m = unsafeEff $ \es0 -> do
375+evalData e m = unsafeEff $ \es0 -> do
371 size0 <- sizeEnv es0
376 size0 <- sizeEnv es0
372 E.bracket (unsafeConsEnv e noRelinker es0)
377 E.bracket (unsafeConsEnv e noRelinker es0)
373 (unsafeTailEnv size0)
378 (unsafeTailEnv size0)
374 (\es -> unEff m es)
379 (\es -> unEff m es)
381+-- | Run a statically dispatched effect with the given initial state and return
382+-- the final state, discarding the final value.
376−execEffect :: adapter e -> Eff (e : es) a -> Eff es (adapter e)
383+execData :: DataA e -> Eff (e : es) a -> Eff es (DataA e)
377−execEffect e0 m = unsafeEff $ \es0 -> do
384+execData e0 m = unsafeEff $ \es0 -> do
378 size0 <- sizeEnv es0
385 size0 <- sizeEnv es0
379 E.bracket (unsafeConsEnv e0 noRelinker es0)
386 E.bracket (unsafeConsEnv e0 noRelinker es0)
380 (unsafeTailEnv size0)
387 (unsafeTailEnv size0)
381 (\es -> unEff m es *> getEnv es)
388 (\es -> unEff m es *> getEnv es)
383−getEffect :: e :> es => Eff es (adapter e)
390+-- | Fetch the current state of the effect.
391+getData :: e :> es => Eff es (DataA e)
384−getEffect = unsafeEff $ \es -> getEnv es
392+getData = unsafeEff $ \es -> getEnv es
386−putEffect :: e :> es => adapter e -> Eff es ()
394+-- | Set the current state of the effect to the given value.
395+putData :: e :> es => DataA e -> Eff es ()
387−putEffect e = unsafeEff $ \es -> putEnv es e
396+putData e = unsafeEff $ \es -> putEnv es e
389−stateEffect :: e :> es => (adapter e -> (a, adapter e)) -> Eff es a
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
390−stateEffect f = unsafeEff $ \es -> stateEnv es f
400+stateData f = unsafeEff $ \es -> stateEnv es f
402+-- | Apply the monadic function to the current state of the effect and return a
392−stateEffectM :: e :> es => (adapter e -> Eff es (a, adapter e)) -> Eff es a
404+stateDataM :: e :> es => (DataA e -> Eff es (a, DataA e)) -> Eff es a
393−stateEffectM f = unsafeEff $ \es -> E.mask $ \release -> do
405+stateDataM f = unsafeEff $ \es -> E.mask $ \release -> do
394 (a, e) <- (\e -> release $ unEff (f e) es) =<< getEnv es
406 (a, e) <- (\e -> release $ unEff (f e) es) =<< getEnv es
395 putEnv es e
407 putEnv es e
410+-- | Execute a computation with a temporarily modified state of the effect.
398−localEffect :: e :> es => (adapter e -> adapter e) -> Eff es a -> Eff es a
411+localData :: e :> es => (DataA e -> DataA e) -> Eff es a -> Eff es a
399−localEffect f m = unsafeEff $ \es -> do
412+localData f m = unsafeEff $ \es -> do
400 E.bracket (stateEnv es $ \e -> (e, f e))
413 E.bracket (stateEnv es $ \e -> (e, f e))
401 (\e -> putEnv es e)
414 (\e -> putEnv es e)
402 (\_ -> unEff m es)
415 (\_ -> unEff m es)