@@ -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− ( -- * Monad
9+ ( -- * The 'Eff' monad
1010 Eff
1111 , runPureEff
1212
@@ -22,11 +22,11 @@ module Effectful.Internal.Monad
2222 , IOE
2323 , runEff
2424
25− -- * Primitive
25+ -- * Prim
2626 , Prim
2727 , runPrim
2828
29− -- ** Unlift strategies
29+ -- * Unlift strategies
3030 , UnliftStrategy(..)
3131 , Persistence(..)
3232 , Limit(..)
@@ -34,34 +34,29 @@ module Effectful.Internal.Monad
3434 , withUnliftStrategy
3535 , withEffToIO
3636
37− --- *** Low-level helpers
37+ -- ** Low-level unlifts
3838 , seqUnliftIO
3939 , concUnliftIO
4040
4141 -- * Effects
4242
43− -- ** Handler
43+ -- ** Dynamic dispatch
4444 , EffectHandler
4545 , LocalEnv(..)
4646 , HandlerA(..)
47− , runHandlerA
48−
49− -- *** Sending operations
47+ , runHandler
5048 , send
5149
52− -- ** Primitive
53−
54− -- *** Running
55− , runEffect
56− , evalEffect
57− , execEffect
58−
59− -- *** Modification
60− , getEffect
61− , putEffect
62− , stateEffect
63− , stateEffectM
64− , localEffect
50+ -- ** Static dispatch
51+ , DataA(..)
52+ , runData
53+ , evalData
54+ , execData
55+ , getData
56+ , putData
57+ , stateData
58+ , stateDataM
59+ , localData
6560 ) where
6661
6762import Control.Applicative (liftA2)
@@ -92,17 +87,16 @@ type role Eff nominal representational
9287-- would have the following type:
9388--
9489-- @
95−-- 'Eff' '['Effectful.Reader.Reader' 'String', 'Effectful.State.State' 'Bool'] 'Integer'
90+-- 'Eff' '['Effectful.Reader.Reader' 'String', 'Effectful.State.Local.State' 'Bool'] 'Integer'
9691-- @
9792--
9893-- Normally, a concrete list of effects is not used to parameterize 'Eff'.
9994-- Instead, the '(:>)' type class is used to express constraints on the list of
10095-- effects without coupling a computation to a concrete list of effects. For
101−-- example, the above example would more commonly be expressed with the
102−-- following type:
96+-- example, the above example would be expressed with the following type:
10397--
10498-- @
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'
106100-- @
107101--
108102-- This abstraction allows the computation to be used in functions that may
@@ -116,12 +110,12 @@ newtype Eff (es :: [Effect]) a = Eff (Env es -> IO a)
116110-- For running computations with side effects see 'runEff'.
117111runPureEff :: Eff '[] a -> a
118112runPureEff (Eff m) =
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).
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).
122116 --
123117 -- 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.
125119 unsafeDupablePerformIO $ m =<< emptyEnv
126120
127121----------------------------------------
@@ -151,12 +145,12 @@ unsafeEff_ m = unsafeEff $ \_ -> m
151145-- | Get the current 'UnliftStrategy'.
152146unliftStrategy :: IOE :> es => Eff es UnliftStrategy
153147unliftStrategy = do
154− IdA (IOE unlift) <- getEffect
148+ DataA (IOE unlift) <- getData
155149 pure unlift
156150
157151-- | Locally override the 'UnliftStrategy' with the given value.
158152withUnliftStrategy :: IOE :> es => UnliftStrategy -> Eff es a -> Eff es a
159−withUnliftStrategy unlift = localEffect $ \_ -> IdA (IOE unlift)
153+withUnliftStrategy unlift = localData $ \_ -> DataA (IOE unlift)
160154
161155-- | Create an unlifting function with the current 'UnliftStrategy'.
162156--
@@ -164,7 +158,7 @@ withUnliftStrategy unlift = localEffect $ \_ -> IdA (IOE unlift)
164158-- constraint for accurate stack traces in case an insufficiently powerful
165159-- 'UnliftStrategy' is used and the unlifting function fails.
166160--
167−-- /Note:/ the strategy is reset to 'SeqUnlift' for new threads.
161+-- /Note:/ the strategy is reset to 'SeqUnlift' inside the continuation.
168162withEffToIO
169163 :: (HasCallStack, IOE :> es)
170164 => ((forall r. Eff es r -> IO r) -> IO a)
@@ -272,7 +266,7 @@ newtype IOE :: Effect where
272266--
273267-- For running pure computations see 'runPureEff'.
274268runEff :: Eff '[IOE] a -> IO a
275−runEff m = unEff (evalEffect (IdA (IOE SeqUnlift)) m) =<< emptyEnv
269+runEff m = unEff (evalData (DataA (IOE SeqUnlift)) m) =<< emptyEnv
276270
277271instance IOE :> es => MonadIO (Eff es) where
278272 liftIO = unsafeEff_
@@ -302,14 +296,14 @@ data Prim :: Effect where
302296
303297-- | Run an 'Eff' computation with primitive state-transformer actions.
304298runPrim :: IOE :> es => Eff (Prim : es) a -> Eff es a
305−runPrim = evalEffect (IdA Prim)
299+runPrim = evalData (DataA Prim)
306300
307301instance Prim :> es => PrimMonad (Eff es) where
308302 type PrimState (Eff es) = RealWorld
309303 primitive = unsafeEff_ . IO
310304
311305----------------------------------------
312−-- Handler
306+-- Dynamic dispatch
313307
314308type role LocalEnv nominal nominal
315309
@@ -333,13 +327,13 @@ type EffectHandler e es
333327
334328-- | An adapter for dynamically dispatched effects.
335329--
336−-- Represents the effect handler bundled along with its environment.
330+-- Represents the effect handler bundled with its environment.
337331data HandlerA :: Effect -> Type where
338332 HandlerA :: !(Env es) -> !(EffectHandler e es) -> HandlerA e
339333
340−-- | Run the effect with the given handler.
341−runHandlerA :: HandlerA e -> Eff (e : es) a -> Eff es a
342−runHandlerA e m = unsafeEff $ \es0 -> do
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
343337 size0 <- sizeEnv es0
344338 E.bracket (unsafeConsEnv e relinker es0)
345339 (unsafeTailEnv size0)
@@ -357,46 +351,65 @@ send op = unsafeEff $ \es -> do
357351 unEff (handle (LocalEnv es) op) handlerEs
358352
359353----------------------------------------
360−-- Helpers
354+-- Static dispatch
361355
362−runEffect :: adapter e -> Eff (e : es) a -> Eff es (a, adapter e)
363−runEffect e0 m = unsafeEff $ \es0 -> do
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
364367 size0 <- sizeEnv es0
365368 E.bracket (unsafeConsEnv e0 noRelinker es0)
366369 (unsafeTailEnv size0)
367370 (\es -> (,) <$> unEff m es <*> getEnv es)
368371
369−evalEffect :: adapter e -> Eff (e : es) a -> Eff es a
370−evalEffect e m = unsafeEff $ \es0 -> do
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
371376 size0 <- sizeEnv es0
372377 E.bracket (unsafeConsEnv e noRelinker es0)
373378 (unsafeTailEnv size0)
374379 (\es -> unEff m es)
375380
376−execEffect :: adapter e -> Eff (e : es) a -> Eff es (adapter e)
377−execEffect e0 m = unsafeEff $ \es0 -> do
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
378385 size0 <- sizeEnv es0
379386 E.bracket (unsafeConsEnv e0 noRelinker es0)
380387 (unsafeTailEnv size0)
381388 (\es -> unEff m es *> getEnv es)
382389
383−getEffect :: e :> es => Eff es (adapter e)
384−getEffect = unsafeEff $ \es -> getEnv es
390+-- | Fetch the current state of the effect.
391+getData :: e :> es => Eff es (DataA e)
392+getData = unsafeEff $ \es -> getEnv es
385393
386−putEffect :: e :> es => adapter e -> Eff es ()
387−putEffect e = unsafeEff $ \es -> putEnv es e
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
388397
389−stateEffect :: e :> es => (adapter e -> (a, adapter e)) -> Eff es a
390−stateEffect f = unsafeEff $ \es -> stateEnv es f
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
391401
392−stateEffectM :: e :> es => (adapter e -> Eff es (a, adapter e)) -> Eff es a
393−stateEffectM f = unsafeEff $ \es -> E.mask $ \release -> do
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
394406 (a, e) <- (\e -> release $ unEff (f e) es) =<< getEnv es
395407 putEnv es e
396408 pure a
397409
398−localEffect :: e :> es => (adapter e -> adapter e) -> Eff es a -> Eff es a
399−localEffect f m = unsafeEff $ \es -> do
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
400413 E.bracket (stateEnv es $ \e -> (e, f e))
401414 (\e -> putEnv es e)
402415 (\_ -> unEff m es)