on master help sign in
34 files changed+537−357
since splitsnapshots
unifiedsplit
.github/workflows/haskell-ci.yml−5 README.md+2−2 effectful-core/effectful-core.cabal+1−1 effectful-core/src/Effectful.hs+3−3 effectful-core/src/Effectful/Dispatch/Dynamic.hs+27−15 effectful-core/src/Effectful/Dispatch/Static.hs+24−27 effectful-core/src/Effectful/Error.hs+40−10 effectful-core/src/Effectful/Internal/Effect.hs+4−20 effectful-core/src/Effectful/Internal/Env.hs+25−20 effectful-core/src/Effectful/Internal/Monad.hs+91−78 effectful-core/src/Effectful/Internal/Unlift.hs+2−2 effectful-core/src/Effectful/Reader.hs+3−3 effectful-core/src/Effectful/State/Local.hs+31−8 effectful-core/src/Effectful/State/Shared.hs+34−11 effectful-core/src/Effectful/Writer/Local.hs+24−9 effectful-core/src/Effectful/Writer/Shared.hs+33−13 effectful/bench/Main.hs+3−3 effectful/effectful.cabal+1−1 effectful/src/Effectful/Concurrent.hs+15−2 effectful/src/Effectful/Concurrent/Async.hs+2−2 effectful/src/Effectful/Concurrent/Chan.hs+1−2 effectful/src/Effectful/Concurrent/Effect.hs+64−5 effectful/src/Effectful/Concurrent/MVar.hs+1−2 effectful/src/Effectful/Concurrent/QSem.hs+2−3 effectful/src/Effectful/Concurrent/QSemN.hs+2−3 effectful/src/Effectful/Concurrent/STM.hs+1−2 effectful/src/Effectful/Environment.hs+2−3 effectful/src/Effectful/FileSystem.hs+89−90 effectful/src/Effectful/FileSystem/Effect.hs+1−1 effectful/src/Effectful/FileSystem/IO.hs+1−2 effectful/src/Effectful/Process.hs+2−3 effectful/src/Effectful/Resource.hs+4−4 effectful/src/Effectful/Temporary.hs+1−1 effectful/src/Effectful/Timeout.hs+1−1
.github/workflows/haskell-ci.yml−5
@@ -37,11 +37,6 @@ jobs:
3737 compilerVersion: 9.2.1
3838 setup-method: ghcup
3939 allow-failure: false
40− - compiler: ghc-9.0.1
41− compilerKind: ghc
42− compilerVersion: 9.0.1
43− setup-method: hvr-ppa
44− allow-failure: false
4540 - compiler: ghc-8.10.7
4641 compilerKind: ghc
4742 compilerVersion: 8.10.7
README.md+2−2
@@ -20,8 +20,8 @@ Main features:
2020
21212. Easy to use API (no boilerplate code and dealing with arcane types).
2222
23−3. Correct semantics in presence of runtime exceptions (no more lost or
24− discarded state).
23+3. Correct semantics in presence of runtime exceptions (no more lost state
24+ modifications).
2525
26264. Seamless integration with the existing ecosystem (`exceptions`,
2727 `monad-control`, `unliftio-core`, `resourcet` etc.).
effectful-core/effectful-core.cabal+1−1
@@ -18,7 +18,7 @@ description: An easy to use, performant extensible effects library with seamless
1818
1919extra-source-files: CHANGELOG.md
2020
21−tested-with: GHC ==8.8.4 || ==8.10.7 || ==9.0.1 || ==9.2.1
21+tested-with: GHC ==8.8.4 || ==8.10.7 || ==9.2.1
2222
2323bug-reports: https://github.com/arybczak/effectful/issues
2424source-repository head
effectful-core/src/Effectful.hs+3−3
@@ -8,10 +8,10 @@ module Effectful
88
99 -- * Running the 'Eff' monad
1010
11− -- ** Pure operations
11+ -- ** Pure computations
1212 , runPureEff
1313
14− -- ** Operations with side effects
14+ -- ** Computations with side effects
1515 , IOE
1616 , runEff
1717
@@ -32,7 +32,7 @@ module Effectful
3232 , interpret
3333 , reinterpret
3434
35− -- ** Handling local 'Eff' operations
35+ -- ** Handling local 'Eff' computations
3636 , LocalEnv
3737
3838 -- *** Unlifts
effectful-core/src/Effectful/Dispatch/Dynamic.hs+27−15
@@ -7,7 +7,7 @@ module Effectful.Dispatch.Dynamic
77 , interpret
88 , reinterpret
99
10− -- ** Handling local 'Eff' operations
10+ -- ** Handling local 'Eff' computations
1111 , LocalEnv
1212
1313 -- *** Unlifts
@@ -32,7 +32,8 @@ module Effectful.Dispatch.Dynamic
3232 ) where
3333
3434import Control.Monad.IO.Unlift
35−import GHC.Stack
35+import Data.Kind
36+import GHC.Stack (HasCallStack)
3637
3738import Effectful.Internal.Effect
3839import Effectful.Internal.Env
@@ -49,7 +50,7 @@ interpret
4950 -> Eff es a
5051interpret handler m = unsafeEff $ \es -> do
5152 les <- forkEnv es
52− (`unEff` es) $ runHandler (Handler les handler) m
53+ (`unEff` es) $ runHandler (HandlerA les handler) m
5354
5455-- | Interpret an effect using other effects.
5556reinterpret
@@ -62,7 +63,7 @@ reinterpret
6263reinterpret runHandlerEs handler m = unsafeEff $ \es -> do
6364 les0 <- forkEnv es
6465 (`unEff` les0) . runHandlerEs . unsafeEff $ \les -> do
65− (`unEff` es) $ runHandler (Handler les handler) m
66+ (`unEff` es) $ runHandler (HandlerA les handler) m
6667
6768----------------------------------------
6869-- Unlifts
@@ -121,7 +122,7 @@ localUnliftIO (LocalEnv les) strategy k = case strategy of
121122 SeqUnlift -> liftIO $ seqUnliftIO les k
122123 ConcUnlift p l -> liftIO $ concUnliftIO les p l k
123124
124−-- | Utility for lifting 'Eff' operations of type
125+-- | Utility for lifting 'Eff' computations of type
125126--
126127-- @'Eff' es a -> 'Eff' es b@
127128--
@@ -129,7 +130,7 @@ localUnliftIO (LocalEnv les) strategy k = case strategy of
129130--
130131-- @'Eff' localEs a -> 'Eff' localEs b@
131132--
132−-- /Note:/ the operation must not run its argument in a separate thread,
133+-- /Note:/ the computation must not run its argument in a separate thread,
133134-- attempting to do so will result in a runtime error.
134135withLiftMap
135136 :: (HasCallStack, SuffixOf es handlerEs)
@@ -145,7 +146,7 @@ withLiftMap !_ k = unsafeEff $ \es -> do
145146 seqUnliftIO localEs $ \unlift -> do
146147 (`unEff` es) . mapEff . unsafeEff_ $ unlift m
147148
148−-- | Utility for lifting 'IO' operations of type
149+-- | Utility for lifting 'IO' computations of type
149150--
150151-- @'IO' a -> 'IO' b@
151152--
@@ -153,11 +154,11 @@ withLiftMap !_ k = unsafeEff $ \es -> do
153154--
154155-- @'Eff' localEs a -> 'Eff' localEs b@
155156--
156−-- /Note:/ the operation must not run its argument in a separate thread,
157+-- /Note:/ the computation must not run its argument in a separate thread,
157158-- attempting to do so will result in a runtime error.
158159--
159160-- Useful e.g. for lifting the unmasking function in
160−-- 'Control.Exception.mask'-like operations:
161+-- 'Control.Exception.mask'-like computations:
161162--
162163-- >>> :{
163164-- data Fork :: Effect where
@@ -187,10 +188,10 @@ withLiftMapIO !_ k = k $ \mapIO m -> unsafeEff $ \es -> do
187188
188189-- | Create a local lifting and unlifting function with the given strategy.
189190--
190−-- Useful for lifting complicated 'Eff' operations where the monadic action
191+-- Useful for lifting complicated 'Eff' computations where the monadic action
191192-- shows in both positive (as a result) and negative (as an argument) position.
192193--
193−-- /Note:/ depending on the operation you're lifting 'localUnlift' along with
194+-- /Note:/ depending on the computation you're lifting 'localUnlift' along with
194195-- 'withLiftMap' might be enough and is more efficient.
195196localLiftUnlift
196197 :: (HasCallStack, SuffixOf es handlerEs)
@@ -213,11 +214,11 @@ localLiftUnlift (LocalEnv les) strategy k = case strategy of
213214-- | Create a local unlifting function with the given strategy along with an
214215-- unrestricted lifting function.
215216--
216−-- Useful for lifting complicated 'IO' operations where the monadic action shows
217−-- in both positive (as a result) and negative (as an argument) position.
217+-- Useful for lifting complicated 'IO' computations where the monadic action
218+-- shows in both positive (as a result) and negative (as an argument) position.
218219--
219−-- /Note:/ depending on the operation you're lifting 'localUnliftIO' along with
220−-- 'withLiftMapIO' might be enough and is more efficient.
220+-- /Note:/ depending on the computation you're lifting 'localUnliftIO' along
221+-- with 'withLiftMapIO' might be enough and is more efficient.
221222localLiftUnliftIO
222223 :: (HasCallStack, SuffixOf es handlerEs, IOE :> es)
223224 => LocalEnv localEs handlerEs
@@ -230,5 +231,16 @@ localLiftUnliftIO (LocalEnv les) strategy k = case strategy of
230231 SeqUnlift -> liftIO $ seqUnliftIO les $ k unsafeEff_
231232 ConcUnlift p l -> liftIO $ concUnliftIO les p l $ k unsafeEff_
232233
234+----------------------------------------
235+-- Utils
236+
237+-- | Require that the second list of effects is a suffix of the first one.
238+--
239+-- In other words, 'SuffixOf' @es@ @baseEs@ means "a suffix of @es@ is
240+-- @baseEs@".
241+type family SuffixOf (es :: [Effect]) (baseEs :: [Effect]) :: Constraint where
242+ SuffixOf baseEs baseEs = ()
243+ SuffixOf (e : es) baseEs = SuffixOf es baseEs
244+
233245-- $setup
234246-- >>> import Control.Concurrent
effectful-core/src/Effectful/Dispatch/Static.hs+24−27
@@ -1,23 +1,30 @@
11module Effectful.Dispatch.Static
22 ( -- * Low level API
3− IdE(..)
3+ DataA(..)
44
5− -- ** Running
6− , runEffect
7− , evalEffect
8− , execEffect
5+ -- ** Extending the environment
6+ , runData
7+ , evalData
8+ , execData
99
10− -- ** Modification
11− , getEffect
12− , putEffect
13− , stateEffect
14− , stateEffectM
15− , localEffect
10+ -- ** Data retrieval and update
11+ , getData
12+ , putData
13+ , stateData
14+ , stateDataM
15+ , localData
1616
1717 -- ** Unlifts
1818 , seqUnliftIO
1919 , concUnliftIO
2020
21+ -- ** Utils
22+ , unEff
23+ , unsafeEff
24+ , unsafeEff_
25+ , unsafeLiftMapIO
26+ , unsafeUnliftIO
27+
2128 -- * Primitive API
2229 , Env
2330 , Relinker(..)
@@ -39,22 +46,12 @@ module Effectful.Dispatch.Static
3946 , putEnv
4047 , stateEnv
4148 , modifyEnv
42−
43− -- * Unsafe 'Eff' operations
44− , unsafeEff
45− , unsafeEff_
46− , unsafeLiftMapIO
47− , unsafeUnliftIO
48−
49− -- * Utils
50− , unEff
5149 ) where
5250
53−import Effectful.Internal.Effect
5451import Effectful.Internal.Env
5552import Effectful.Internal.Monad
5653
57−-- | Utility for lifting 'IO' operations of type
54+-- | Utility for lifting 'IO' computations of type
5855--
5956-- @'IO' a -> 'IO' b@
6057--
@@ -65,10 +62,10 @@ import Effectful.Internal.Monad
6562-- This function is __unsafe__ because:
6663--
6764-- - It can be used to introduce arbitrary 'IO' actions into pure 'Eff'
68−-- operations.
65+-- computations.
6966--
70−-- - The 'IO' operation must not run its argument in a separate thread, but it's
71−-- not checked anywhere.
67+-- - The 'IO' computation must not run its argument in a separate thread, but
68+-- it's not checked anywhere.
7269unsafeLiftMapIO :: (IO a -> IO b) -> Eff es a -> Eff es b
7370unsafeLiftMapIO f m = unsafeEff $ \es -> f (unEff m es)
7471
@@ -77,9 +74,9 @@ unsafeLiftMapIO f m = unsafeEff $ \es -> f (unEff m es)
7774-- This function is __unsafe__ because:
7875--
7976-- - It can be used to introduce arbitrary 'IO' actions into pure 'Eff'
80−-- operations.
77+-- computations.
8178--
82−-- - Unlifted 'Eff' operations must not be run in a thread distinct from the
79+-- - Unlifted 'Eff' computations must not be run in a thread distinct from the
8380-- caller of 'unsafeUnliftIO', but it's not checked anywhere.
8481unsafeUnliftIO :: ((forall r. Eff es r -> IO r) -> IO a) -> Eff es a
8582unsafeUnliftIO k = unsafeEff $ \es -> k (`unEff` es)
effectful-core/src/Effectful/Error.hs+40−10
@@ -1,8 +1,6 @@
11-- | Support for handling errors of a particular type, i.e. checked exceptions.
22--
3−-- /Note:/ the 'Error' effect is a suitable replacement for the
4−-- 'Control.Monad.Trans.Except.ExceptT' monad transformer from the
5−-- @transformers@ library. It is __not__ a general mechanism for handling
3+-- The 'Error' effect is __not__ a general mechanism for handling regular
64-- exceptions, that's what functions from the @exceptions@ library are for (see
75-- "Control.Monad.Catch" for more information).
86--
@@ -25,18 +23,50 @@
2523-- resources such as 'Control.Monad.Catch.finally' and
2624-- 'Control.Monad.Catch.bracket' work as expected:
2725--
28−-- >>> let display = liftIO . putStrLn
26+-- >>> let msg = liftIO . putStrLn
2927-- >>> :{
3028-- runEff . fmap (either (Left . snd) Right) . runError @String $ do
31−-- E.bracket_ (display "Beginning.")
32−-- (display "Cleaning up.")
33−-- (display "Computing." >> throwError "oops" >> display "More.")
29+-- E.bracket_ (msg "Beginning.")
30+-- (msg "Cleaning up.")
31+-- (msg "Computing." >> throwError "oops" >> msg "More.")
3432-- :}
3533-- Beginning.
3634-- Computing.
3735-- Cleaning up.
3836-- Left "oops"
3937--
38+-- /Note:/ unlike the 'Control.Monad.Trans.Except.ExceptT' monad transformer
39+-- from the @transformers@ library, the order in which you handle the 'Error'
40+-- effect with regard to other stateful effects does not matter. Consider the
41+-- following:
42+--
43+-- >>> import qualified Control.Monad.State.Strict as T
44+-- >>> import qualified Control.Monad.Except as T
45+--
46+-- >>> let m1 = (T.modify (++ " there!") >> T.throwError "oops") `T.catchError` \_ -> pure ()
47+--
48+-- >>> (`T.runStateT` "Hi") . T.runExceptT $ m1
49+-- (Right (),"Hi there!")
50+--
51+-- >>> T.runExceptT . (`T.runStateT` "Hi") $ m1
52+-- Right ((),"Hi")
53+--
54+-- Here, whether state modifications within the @catchError@ block are lost or
55+-- not depends on the shape of the monad transformer stack, which is surprising
56+-- and can be a source of subtle bugs. On the other hand:
57+--
58+-- >>> import Effectful.State.Local
59+--
60+-- >>> let m2 = (modify (++ " there!") >> throwError "oops") `catchError` \_ (_::String) -> pure ()
61+--
62+-- >>> runEff . runState "Hi" . runError @String $ m2
63+-- (Right (),"Hi there!")
64+--
65+-- >>> runEff . runError @String . runState "Hi" $ m2
66+-- Right ((),"Hi there!")
67+--
68+-- Here, no matter the order of effects, state modifications within the
69+-- @catchError@ block always persist, giving predictable behavior.
4070module Effectful.Error
4171 ( Error
4272 , runError
@@ -72,7 +102,7 @@ runError
72102runError m = unsafeEff $ \es0 -> mask $ \release -> do
73103 eid <- newErrorId
74104 size0 <- sizeEnv es0
75− es <- unsafeConsEnv (IdE (Error @e eid)) noRelinker es0
105+ es <- unsafeConsEnv (DataA (Error @e eid)) noRelinker es0
76106 r <- tryErrorIO release eid es `onException` unsafeTailEnv size0 es
77107 unsafeTailEnv size0 es
78108 pure r
@@ -89,7 +119,7 @@ throwError
89119 -- ^ The error.
90120 -> Eff es a
91121throwError e = unsafeEff $ \es -> do
92− IdE (Error eid) <- getEnv @(Error e) es
122+ DataA (Error eid) <- getEnv @(Error e) es
93123 throwIO $ ErrorEx eid callStack e
94124
95125-- | Handle an error of type @e@.
@@ -101,7 +131,7 @@ catchError
101131 -- ^ A handler for errors in the inner computation.
102132 -> Eff es a
103133catchError m handler = unsafeEff $ \es -> do
104− IdE (Error eid) <- getEnv @(Error e) es
134+ DataA (Error eid) <- getEnv @(Error e) es
105135 size <- sizeEnv es
106136 catchErrorIO eid (unEff m es) $ \cs e -> do
107137 checkSizeEnv size es
effectful-core/src/Effectful/Internal/Effect.hs+4−20
@@ -8,10 +8,6 @@
88module Effectful.Internal.Effect
99 ( Effect
1010 , (:>)(..)
11− , IdE(..)
12−
13− -- * Utils
14− , SuffixOf
1511
1612 -- * Re-exports
1713 , Type
@@ -23,20 +19,16 @@ import GHC.TypeLits
2319-- | The kind of effects.
2420type Effect = (Type -> Type) -> Type -> Type
2521
26−-- | An adapter for statically dispatched effects.
27−newtype IdE :: Effect -> Type where
28− IdE :: (forall m r. e m r) -> IdE e
29−
3022-- | A constraint that requires that a particular effect @e@ is a member of the
3123-- type-level list @es@. This is used to parameterize an 'Effectful.Monad.Eff'
32−-- operation over an arbitrary list of effects, so long as @e@ is /somewhere/ in
33−-- the list.
24+-- computation over an arbitrary list of effects, so long as @e@ is /somewhere/
25+-- in the list.
3426--
35−-- For example, an operation that only needs access to a mutable value of type
27+-- For example, a computation that only needs access to a mutable value of type
3628-- 'Integer' would likely use the following type:
3729--
3830-- @
39−-- 'Effectful.State.State' 'Integer' ':>' es => 'Effectful.Monad.Eff' es ()
31+-- 'Effectful.State.Local.State' 'Integer' ':>' es => 'Effectful.Monad.Eff' es ()
4032-- @
4133class (e :: Effect) :> (es :: [Effect]) where
4234 -- | Get the position of @e@ in @es@.
@@ -59,11 +51,3 @@ instance {-# OVERLAPPING #-} e :> (e : es) where
5951
6052instance e :> es => e :> (x : es) where
6153 reifyIndex = 1 + reifyIndex @e @es
62−
63−-- | Require that the second list of effects is a suffix of the first one.
64−--
65−-- In other words, 'SuffixOf' @es@ @baseEs@ means "a suffix of @es@ is
66−-- @baseEs@".
67−type family SuffixOf (es :: [Effect]) (baseEs :: [Effect]) :: Constraint where
68− SuffixOf baseEs baseEs = ()
69− SuffixOf (e : es) baseEs = SuffixOf es baseEs
effectful-core/src/Effectful/Internal/Env.hs+25−20
@@ -1,3 +1,4 @@
1+{-# LANGUAGE MagicHash #-}
12{-# OPTIONS_HADDOCK not-home #-}
23-- | The environment for the 'Effectful.Internal.Monad.Eff' monad.
34--
@@ -42,7 +43,7 @@ import Control.Monad
4243import Control.Monad.Primitive
4344import Data.IORef
4445import Data.Primitive.SmallArray
45−import GHC.Exts (Any)
46+import GHC.Exts (Any, SmallMutableArray#)
4647import GHC.Stack (HasCallStack)
4748import Unsafe.Coerce
4849import qualified Data.IntMap.Strict as IM
@@ -417,43 +418,43 @@ unsafeTailEnv len (Env fork gref _) = case fork of
417418
418419-- | Extract a specific data type from the environment.
419420getEnv
420− :: forall e es adapter. e :> es
421+ :: forall e adapter es. e :> es
421422 => Env es
422423 -> IO (adapter e)
423424getEnv env = do
424− (i, es) <- getLocation (reifyIndex @e @es) env
425+ Location i es <- getLocation (reifyIndex @e @es) env
425426 fromAny <$> readSmallArray es i
426427
427428-- | Replace the data type in the environment with a new value (in place).
428429putEnv
429− :: forall e es adapter. e :> es
430+ :: forall e adapter es. e :> es
430431 => Env es
431432 -> adapter e
432433 -> IO ()
433434putEnv env e = do
434− (i, es) <- getLocation (reifyIndex @e @es) env
435+ Location i es <- getLocation (reifyIndex @e @es) env
435436 e `seq` writeSmallArray es i (toAny e)
436437
437438-- | Modify the data type in the environment (in place) and return a value.
438439stateEnv
439− :: forall e es adapter a. e :> es
440+ :: forall e adapter es a. e :> es
440441 => Env es
441442 -> (adapter e -> (a, adapter e))
442443 -> IO a
443444stateEnv env f = do
444− (i, es) <- getLocation (reifyIndex @e @es) env
445+ Location i es <- getLocation (reifyIndex @e @es) env
445446 (a, e) <- f . fromAny <$> readSmallArray es i
446447 e `seq` writeSmallArray es i (toAny e)
447448 pure a
448449
449450-- | Modify the data type in the environment (in place).
450451modifyEnv
451− :: forall e es adapter. e :> es
452+ :: forall e adapter es. e :> es
452453 => Env es
453454 -> (adapter e -> adapter e)
454455 -> IO ()
455456modifyEnv env f = do
456− (i, es) <- getLocation (reifyIndex @e @es) env
457+ Location i es <- getLocation (reifyIndex @e @es) env
457458 e <- f . fromAny <$> readSmallArray es i
458459 e `seq` writeSmallArray es i (toAny e)
459460
@@ -469,13 +470,17 @@ emptyEnvRef = do
469470 fs <- newSmallArray 0 undefinedData
470471 newIORef $ EnvRef 0 es fs
471472
473+-- | Location with unboxed fields to make GHC generate Core without unnecessary
474+-- reboxing around the local 'go' function from 'getLocation'.
475+data Location = Location !Int !(SmallMutableArray RealWorld Any)
476+
472477-- | Determine location of the data type in the environment.
473478getLocation
474479 :: Int
475480 -> Env es
476− -> IO (Int, SmallMutableArray RealWorld Any)
481+ -> IO Location
477482getLocation ixE (Env fork ref _) = do
478− EnvRef n es _ <- readIORef ref
483+ EnvRef n es@(SmallMutableArray es#) _ <- readIORef ref
479484 -- Optimized for the common access pattern:
480485 --
481486 -- - Most application code has no access to forks, in which case we look at
@@ -484,25 +489,25 @@ getLocation ixE (Env fork ref _) = do
484489 -- - Effect handlers will most likely access the newest fork with handler
485490 -- specific effects for reinterpretation.
486491 case fork of
487− NoFork -> pure (ix n, es)
492+ NoFork -> pure $ Location (ix n) es
488493 Forks _ baseIx lref forks -> do
489494 EnvRef ln les _ <- readIORef lref
490495 let i = ix (baseIx + ln)
491496 if i >= baseIx
492− then pure (i - baseIx, les)
493− else go es i forks
497+ then pure $ Location (i - baseIx) les
498+ else go es# i forks
494499 where
495− go :: SmallMutableArray RealWorld Any
500+ go :: SmallMutableArray# RealWorld Any
496501 -> Int
497502 -> Forks
498− -> IO (Int, SmallMutableArray RealWorld Any)
499− go es i = \case
500− NoFork -> pure (i, es)
503+ -> IO Location
504+ go es# i = \case
505+ NoFork -> pure $ Location i (SmallMutableArray es#)
501506 Forks _ baseIx lref forks -> do
502507 EnvRef _ les _ <- readIORef lref
503508 if i >= baseIx
504− then pure (i - baseIx, les)
505− else go es i forks
509+ then pure $ Location (i - baseIx) les
510+ else go es# i forks
506511
507512 ix :: Int -> Int
508513 ix n = n - ixE - 1
effectful-core/src/Effectful/Internal/Monad.hs+91−78
@@ -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(..)
46− , Handler(..)
46+ , HandlerA(..)
4747 , runHandler
48−
49− -- *** Sending operations
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)
@@ -84,44 +79,43 @@ import Effectful.Internal.Unlift
8479
8580type role Eff nominal representational
8681
87−-- | The 'Eff' monad provides the implementation of an operation that performs
82+-- | The 'Eff' monad provides the implementation of a computation that performs
8883-- 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
90−-- operation that produces an 'Integer' by consuming a 'String' from the global
91−-- environment and acting upon a single mutable value of type 'Bool' would have
92−-- the following type:
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:
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
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:
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:
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--
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
109103-- perform other effects, and it also allows the effects to be handled in any
110104-- order.
111105newtype Eff (es :: [Effect]) a = Eff (Env es -> IO a)
112106 deriving (Monoid, Semigroup)
113107
114−-- | Run a pure 'Eff' operation.
108+-- | Run a pure 'Eff' computation.
115109--
116−-- For running operations with side effects see 'runEff'.
110+-- 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----------------------------------------
@@ -134,14 +128,14 @@ unEff (Eff m) = m
134128-- | Access the underlying 'IO' monad along with the environment.
135129--
136130-- 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.
138132unsafeEff :: (Env es -> IO a) -> Eff es a
139133unsafeEff m = Eff (oneShot m)
140134
141135-- | Access the underlying 'IO' monad.
142136--
143137-- 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.
145139unsafeEff_ :: IO a -> Eff es a
146140unsafeEff_ m = unsafeEff $ \_ -> m
147141
@@ -151,12 +145,12 @@ unsafeEff_ m = unsafeEff $ \_ -> m
151145-- | Get the current 'UnliftStrategy'.
152146unliftStrategy :: IOE :> es => Eff es UnliftStrategy
153147unliftStrategy = do
154− IdE (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 $ \_ -> IdE (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 $ \_ -> IdE (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)
@@ -264,15 +258,15 @@ instance Fail :> es => MonadFail (Eff es) where
264258----------------------------------------
265259-- IO
266260
267−-- | Run arbitrary 'IO' operations via 'MonadIO' or 'MonadUnliftIO'.
261+-- | Run arbitrary 'IO' computations via 'MonadIO' or 'MonadUnliftIO'.
268262newtype IOE :: Effect where
269263 IOE :: UnliftStrategy -> IOE m r
270264
271−-- | Run an 'Eff' operation with side effects.
265+-- | Run an 'Eff' computation with side effects.
272266--
273−-- For running pure operations see 'runPureEff'.
267+-- For running pure computations see 'runPureEff'.
274268runEff :: 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
276270
277271instance IOE :> es => MonadIO (Eff es) where
278272 liftIO = unsafeEff_
@@ -300,16 +294,16 @@ instance IOE :> es => MonadBaseControl IO (Eff es) where
300294data Prim :: Effect where
301295 Prim :: Prim m r
302296
303−-- | Run an 'Eff' operation with primitive state-transformer actions.
297+-- | Run an 'Eff' computation with primitive state-transformer actions.
304298runPrim :: IOE :> es => Eff (Prim : es) a -> Eff es a
305−runPrim = evalEffect (IdE 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
@@ -325,78 +319,97 @@ newtype LocalEnv (localEs :: [Effect]) (handlerEs :: [Effect]) = LocalEnv (Env l
325319type EffectHandler e es
326320 = forall a localEs. HasCallStack
327321 => LocalEnv localEs es
328− -- ^ Capture of the local environment for handling local 'Eff' operations when
329− -- @e@ is a higher order effect.
322+ -- ^ Capture of the local environment for handling local 'Eff' computations
323+ -- when @e@ is a higher order effect.
330324 -> e (Eff localEs) a
331325 -- ^ The effect performed in the local environment.
332326 -> Eff es a
333327
334328-- | An adapter for dynamically dispatched effects.
335329--
336−-- Represents the effect handler bundled along with its environment.
337−data Handler :: Effect -> Type where
338− Handler :: !(Env es) -> !(EffectHandler e es) -> Handler e
330+-- Represents the effect handler bundled with its environment.
331+data HandlerA :: Effect -> Type where
332+ HandlerA :: !(Env es) -> !(EffectHandler e es) -> HandlerA e
339333
340−-- | Run the effect with the given handler.
341−runHandler :: Handler e -> Eff (e : es) a -> Eff es a
334+-- | Run a dynamically dispatched effect with the given handler.
335+runHandler :: HandlerA e -> Eff (e : es) a -> Eff es a
342336runHandler e m = unsafeEff $ \es0 -> do
343337 size0 <- sizeEnv es0
344338 E.bracket (unsafeConsEnv e relinker es0)
345339 (unsafeTailEnv size0)
346340 (\es -> unEff m es)
347341 where
348− relinker :: Relinker Handler e
349− relinker = Relinker $ \relink (Handler handlerEs handle) -> do
342+ relinker :: Relinker HandlerA e
343+ relinker = Relinker $ \relink (HandlerA handlerEs handle) -> do
350344 newHandlerEs <- relink handlerEs
351− pure $ Handler newHandlerEs handle
345+ pure $ HandlerA newHandlerEs handle
352346
353347-- | Send an operation of the given effect to its handler for execution.
354348send :: (HasCallStack, e :> es) => e (Eff es) a -> Eff es a
355349send op = unsafeEff $ \es -> do
356− Handler handlerEs handle <- getEnv es
350+ HandlerA handlerEs handle <- getEnv es
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)
effectful-core/src/Effectful/Internal/Unlift.hs+2−2
@@ -35,7 +35,7 @@ import Effectful.Internal.Utils
3535----------------------------------------
3636-- Unlift strategies
3737
38−-- | The strategy to use when unlifting 'Eff' operations via 'withRunInIO',
38+-- | The strategy to use when unlifting 'Eff' computations via 'withRunInIO',
3939-- 'Effectful.Monad.withEffToIO' or the 'Effectful.Dispatch.Dynamic.localUnlift'
4040-- family.
4141data UnliftStrategy
@@ -105,7 +105,7 @@ seqUnlift k es unEff = do
105105 if tid `eqThreadId` tid0
106106 then unEff m es
107107 else error
108− $ "If you want to use the unlifting function to run Eff operations "
108+ $ "If you want to use the unlifting function to run Eff computations "
109109 ++ "in multiple threads, have a look at UnliftStrategy (ConcUnlift)."
110110
111111-- | Concurrent unlift for various strategies and limits.
effectful-core/src/Effectful/Reader.hs+3−3
@@ -20,12 +20,12 @@ runReader
2020 :: r -- ^ An initial environment.
2121 -> Eff (Reader r : es) a
2222 -> Eff es a
23−runReader r = evalEffect (IdE (Reader r))
23+runReader r = evalData (DataA (Reader r))
2424
2525-- | Fetch the value of the environment.
2626ask :: Reader r :> es => Eff es r
2727ask = do
28− IdE (Reader r) <- getEffect
28+ DataA (Reader r) <- getData
2929 pure r
3030
3131-- | Retrieve a function of the current environment.
@@ -46,4 +46,4 @@ local
4646 => (r -> r) -- ^ The function to modify the environment.
4747 -> Eff es a
4848 -> Eff es a
49−local f = localEffect $ \(IdE (Reader r)) -> IdE (Reader (f r))
49+local f = localData $ \(DataA (Reader r)) -> DataA (Reader (f r))
effectful-core/src/Effectful/State/Local.hs+31−8
@@ -5,6 +5,25 @@
55--
66-- - very fast.
77--
8+-- /Note:/ unlike the 'Control.Monad.Trans.State.StateT' monad transformer from
9+-- the @transformers@ library, the 'State' effect doesn't lose state
10+-- modifications when an exception is received:
11+--
12+-- >>> import qualified Control.Monad.Trans.State.Strict as S
13+--
14+-- >>> :{
15+-- (`S.execStateT` "Hi") . handle (\(_::ErrorCall) -> pure ()) $ do
16+-- S.modify (++ " there!")
17+-- error "oops"
18+-- :}
19+-- "Hi"
20+--
21+-- >>> :{
22+-- runEff . execState "Hi" . handle (\(_::ErrorCall) -> pure ()) $ do
23+-- modify (++ " there!")
24+-- error "oops"
25+-- :}
26+-- "Hi there!"
827module Effectful.State.Local
928 ( State
1029 , runState
@@ -33,7 +52,7 @@ runState
3352 -> Eff (State s : es) a
3453 -> Eff es (a, s)
3554runState s0 m = do
36− (a, IdE (State s)) <- runEffect (IdE (State s0)) m
55+ (a, DataA (State s)) <- runData (DataA (State s0)) m
3756 pure (a, s)
3857
3958-- | Run a 'State' effect with the given initial state and return the final
@@ -42,7 +61,7 @@ evalState
4261 :: s -- ^ An initial state.
4362 -> Eff (State s : es) a
4463 -> Eff es a -- ^ A return value.
45−evalState s = evalEffect (IdE (State s))
64+evalState s = evalData (DataA (State s))
4665
4766-- | Run a 'State' effect with the given initial state and return the final
4867-- state, discarding the final value.
@@ -51,13 +70,13 @@ execState
5170 -> Eff (State s : es) a
5271 -> Eff es s
5372execState s0 m = do
54− IdE (State s) <- execEffect (IdE (State s0)) m
73+ DataA (State s) <- execData (DataA (State s0)) m
5574 pure s
5675
5776-- | Fetch the current value of the state.
5877get :: State s :> es => Eff es s
5978get = do
60− IdE (State s) <- getEffect
79+ DataA (State s) <- getData
6180 pure s
6281
6382-- | Get a function of the current state.
@@ -71,14 +90,14 @@ gets f = f <$> get
7190
7291-- | Set the current state to the given value.
7392put :: State s :> es => s -> Eff es ()
74−put s = putEffect (IdE (State s))
93+put s = putData (DataA (State s))
7594
7695-- | Apply the function to the current state and return a value.
7796state
7897 :: State s :> es
7998 => (s -> (a, s)) -- ^ The function to modify the state.
8099 -> Eff es a
81−state f = stateEffect $ \(IdE (State s0)) -> let (a, s) = f s0 in (a, IdE (State s))
100+state f = stateData $ \(DataA (State s0)) -> let (a, s) = f s0 in (a, DataA (State s))
82101
83102-- | Apply the function to the current state.
84103--
@@ -94,9 +113,9 @@ stateM
94113 :: State s :> es
95114 => (s -> Eff es (a, s)) -- ^ The function to modify the state.
96115 -> Eff es a
97−stateM f = stateEffectM $ \(IdE (State s0)) -> do
116+stateM f = stateDataM $ \(DataA (State s0)) -> do
98117 (a, s) <- f s0
99− pure (a, IdE (State s))
118+ pure (a, DataA (State s))
100119
101120-- | Apply the monadic function to the current state.
102121--
@@ -106,3 +125,7 @@ modifyM
106125 => (s -> Eff es s) -- ^ The monadic function to modify the state.
107126 -> Eff es ()
108127modifyM f = stateM (\s -> ((), ) <$> f s)
128+
129+-- $setup
130+-- >>> import Control.Exception (ErrorCall)
131+-- >>> import Control.Monad.Catch
effectful-core/src/Effectful/State/Shared.hs+34−11
@@ -1,10 +1,29 @@
11-- | Support for access to a mutable value of a particular type, which is:
22--
3−-- - shareable between multiple threads (if you want each thead to manage their
4−-- own version of the value, see "Effectful.State.Local").
3+-- - shared between multiple threads (if you want each thead to manage its own
4+-- version of the value, see "Effectful.State.Local").
55--
6−-- - slower than "Effectful.State.Local".
6+-- - slightly slower than "Effectful.State.Local".
77--
8+-- /Note:/ unlike the 'Control.Monad.Trans.State.StateT' monad transformer from
9+-- the @transformers@ library, the 'State' effect doesn't lose state
10+-- modifications when an exception is received:
11+--
12+-- >>> import qualified Control.Monad.Trans.State.Strict as S
13+--
14+-- >>> :{
15+-- (`S.execStateT` "Hi") . handle (\(_::ErrorCall) -> pure ()) $ do
16+-- S.modify (++ " there!")
17+-- error "oops"
18+-- :}
19+-- "Hi"
20+--
21+-- >>> :{
22+-- runEff . execState "Hi" . handle (\(_::ErrorCall) -> pure ()) $ do
23+-- modify (++ " there!")
24+-- error "oops"
25+-- :}
26+-- "Hi there!"
827module Effectful.State.Shared
928 ( State
1029 , runState
@@ -24,7 +43,7 @@ import Control.Concurrent.MVar
2443import Effectful.Dispatch.Static
2544import Effectful.Monad
2645
27−-- | Provide access to a strict (WHNF), shareable, mutable value of type @s@.
46+-- | Provide access to a strict (WHNF), shared, mutable value of type @s@.
2847newtype State s :: Effect where
2948 State :: MVar s -> State s m r
3049
@@ -33,7 +52,7 @@ newtype State s :: Effect where
3352runState :: s -> Eff (State s : es) a -> Eff es (a, s)
3453runState s m = do
3554 v <- unsafeEff_ $ newMVar s
36− a <- evalEffect (IdE (State v)) m
55+ a <- evalData (DataA (State v)) m
3756 (a, ) <$> unsafeEff_ (readMVar v)
3857
3958-- | Run a 'State' effect with the given initial state and return the final
@@ -41,20 +60,20 @@ runState s m = do
4160evalState :: s -> Eff (State s : es) a -> Eff es a
4261evalState s m = do
4362 v <- unsafeEff_ $ newMVar s
44− evalEffect (IdE (State v)) m
63+ evalData (DataA (State v)) m
4564
4665-- | Run a 'State' effect with the given initial state and return the final
4766-- state, discarding the final value.
4867execState :: s -> Eff (State s : es) a -> Eff es s
4968execState s m = do
5069 v <- unsafeEff_ $ newMVar s
51− _ <- evalEffect (IdE (State v)) m
70+ _ <- evalData (DataA (State v)) m
5271 unsafeEff_ $ readMVar v
5372
5473-- | Fetch the current value of the state.
5574get :: State s :> es => Eff es s
5675get = unsafeEff $ \es -> do
57− IdE (State v) <- getEnv es
76+ DataA (State v) <- getEnv es
5877 readMVar v
5978
6079-- | Get a function of the current state.
@@ -66,7 +85,7 @@ gets f = f <$> get
6685-- | Set the current state to the given value.
6786put :: State s :> es => s -> Eff es ()
6887put s = unsafeEff $ \es -> do
69− IdE (State v) <- getEnv es
88+ DataA (State v) <- getEnv es
7089 modifyMVar_ v $ \_ -> s `seq` pure s
7190
7291-- | Apply the function to the current state and return a value.
@@ -74,7 +93,7 @@ put s = unsafeEff $ \es -> do
7493-- /Note:/ this function gets an exclusive access to the state for its duration.
7594state :: State s :> es => (s -> (a, s)) -> Eff es a
7695state f = unsafeEff $ \es -> do
77− IdE (State v) <- getEnv es
96+ DataA (State v) <- getEnv es
7897 modifyMVar v $ \s0 -> let (a, s) = f s0 in s `seq` pure (s, a)
7998
8099-- | Apply the function to the current state.
@@ -90,7 +109,7 @@ modify f = state (\s -> ((), f s))
90109-- /Note:/ this function gets an exclusive access to the state for its duration.
91110stateM :: State s :> es => (s -> Eff es (a, s)) -> Eff es a
92111stateM f = unsafeEff $ \es -> do
93− IdE (State v) <- getEnv es
112+ DataA (State v) <- getEnv es
94113 modifyMVar v $ \s0 -> do
95114 (a, s) <- unEff (f s0) es
96115 s `seq` pure (s, a)
@@ -102,3 +121,7 @@ stateM f = unsafeEff $ \es -> do
102121-- /Note:/ this function gets an exclusive access to the state for its duration.
103122modifyM :: State s :> es => (s -> Eff es s) -> Eff es ()
104123modifyM f = stateM (\s -> ((), ) <$> f s)
124+
125+-- $setup
126+-- >>> import Control.Exception (ErrorCall)
127+-- >>> import Control.Monad.Catch
effectful-core/src/Effectful/Writer/Local.hs+24−9
@@ -24,7 +24,7 @@ module Effectful.Writer.Local
2424 , listens
2525 ) where
2626
27−import Control.Exception
27+import Control.Exception (onException, mask)
2828
2929import Effectful.Dispatch.Static
3030import Effectful.Monad
@@ -38,35 +38,46 @@ newtype Writer w :: Effect where
3838-- output.
3939runWriter :: Monoid w => Eff (Writer w : es) a -> Eff es (a, w)
4040runWriter m = do
41− (a, IdE (Writer w)) <- runEffect (IdE (Writer mempty)) m
41+ (a, DataA (Writer w)) <- runData (DataA (Writer mempty)) m
4242 pure (a, w)
4343
4444-- | Run a 'Writer' effect and return the final output, discarding the final
4545-- value.
4646execWriter :: Monoid w => Eff (Writer w : es) a -> Eff es w
4747execWriter m = do
48− IdE (Writer w) <- execEffect (IdE (Writer mempty)) m
48+ DataA (Writer w) <- execData (DataA (Writer mempty)) m
4949 pure w
5050
5151-- | Append the given output to the overall output of the 'Writer'.
5252tell :: (Writer w :> es, Monoid w) => w -> Eff es ()
53−tell w = stateEffect $ \(IdE (Writer w0)) -> ((), IdE (Writer (w0 <> w)))
53+tell w = stateData $ \(DataA (Writer w0)) -> ((), DataA (Writer (w0 <> w)))
5454
5555-- | Execute an action and append its output to the overall output of the
5656-- 'Writer'.
5757--
58−-- /Note:/ if a runtime exception is received while the action is executed, the
59−-- partial output of the action will still be appended to the overall output of
60−-- the 'Writer'.
58+-- /Note:/ if an exception is received while the action is executed, the partial
59+-- output of the action will still be appended to the overall output of the
60+-- 'Writer':
61+--
62+-- >>> :{
63+-- runEff . execWriter @String $ do
64+-- tell "Hi"
65+-- handle (\(_::ErrorCall) -> pure ((), "")) $ do
66+-- tell " there"
67+-- listen $ do
68+-- tell "!"
69+-- error "oops"
70+-- :}
71+-- "Hi there!"
6172listen :: (Writer w :> es, Monoid w) => Eff es a -> Eff es (a, w)
6273listen m = unsafeEff $ \es -> mask $ \restore -> do
63− w0 <- stateEnv es $ \(IdE (Writer w)) -> (w, IdE (Writer mempty))
74+ w0 <- stateEnv es $ \(DataA (Writer w)) -> (w, DataA (Writer mempty))
6475 a <- restore (unEff m es) `onException` merge es w0
6576 (a, ) <$> merge es w0
6677 where
6778 merge es w0 =
6879 -- If an exception is thrown, restore w0 and keep parts of w1.
69− stateEnv es $ \(IdE (Writer w1)) -> (w1, IdE (Writer (w0 <> w1)))
80+ stateEnv es $ \(DataA (Writer w1)) -> (w1, DataA (Writer (w0 <> w1)))
7081
7182-- | Execute an action and append its output to the overall output of the
7283-- 'Writer', then return the final value along with a function of the recorded
@@ -77,3 +88,7 @@ listens :: (Writer w :> es, Monoid w) => (w -> b) -> Eff es a -> Eff es (a, b)
7788listens f m = do
7889 (a, w) <- listen m
7990 pure (a, f w)
91+
92+-- $setup
93+-- >>> import Control.Exception (ErrorCall)
94+-- >>> import Control.Monad.Catch
effectful-core/src/Effectful/Writer/Shared.hs+33−13
@@ -1,9 +1,9 @@
11-- | Support for access to a write only value of a particular type, which is:
22--
3−-- - shareable between multiple threads (if you want each thead to manage their
4−-- own version of the value, see "Effectful.Writer.Local").
3+-- - shared between multiple threads (if you want each thead to manage its own
4+-- version of the value, see "Effectful.Writer.Local").
55--
6−-- - slower than "Effectful.Writer.Local".
6+-- - slightly slower than "Effectful.Writer.Local".
77--
88-- /Warning:/ 'Writer'\'s state will be accumulated via __left-associated__ uses
99-- of '<>', which makes it unsuitable for use with types for which such pattern
@@ -25,12 +25,12 @@ module Effectful.Writer.Shared
2525 ) where
2626
2727import Control.Concurrent.MVar
28−import Control.Exception
28+import Control.Exception (onException, uninterruptibleMask)
2929
3030import Effectful.Dispatch.Static
3131import Effectful.Monad
3232
33−-- | Provide access to a strict (WHNF), shareable, write only value of type @w@.
33+-- | Provide access to a strict (WHNF), shared, write only value of type @w@.
3434newtype Writer w :: Effect where
3535 Writer :: MVar w -> Writer w m r
3636
@@ -39,7 +39,7 @@ newtype Writer w :: Effect where
3939runWriter :: Monoid w => Eff (Writer w : es) a -> Eff es (a, w)
4040runWriter m = do
4141 v <- unsafeEff_ $ newMVar mempty
42− a <- evalEffect (IdE (Writer v)) m
42+ a <- evalData (DataA (Writer v)) m
4343 (a, ) <$> unsafeEff_ (readMVar v)
4444
4545-- | Run a 'Writer' effect and return the final output, discarding the final
@@ -47,21 +47,32 @@ runWriter m = do
4747execWriter :: Monoid w => Eff (Writer w : es) a -> Eff es w
4848execWriter m = do
4949 v <- unsafeEff_ $ newMVar mempty
50− _ <- evalEffect (IdE (Writer v)) m
50+ _ <- evalData (DataA (Writer v)) m
5151 unsafeEff_ $ readMVar v
5252
5353-- | Append the given output to the overall output of the 'Writer'.
5454tell :: (Writer w :> es, Monoid w) => w -> Eff es ()
5555tell w1 = unsafeEff $ \es -> do
56− IdE (Writer v) <- getEnv es
56+ DataA (Writer v) <- getEnv es
5757 modifyMVar_ v $ \w0 -> let w = w0 <> w1 in w `seq` pure w
5858
5959-- | Execute an action and append its output to the overall output of the
6060-- 'Writer'.
6161--
62−-- /Note:/ if a runtime exception is received while the action is executed, the
63−-- partial output of the action will still be appended to the overall output of
64−-- the 'Writer'.
62+-- /Note:/ if an exception is received while the action is executed, the partial
63+-- output of the action will still be appended to the overall output of the
64+-- 'Writer':
65+--
66+-- >>> :{
67+-- runEff . execWriter @String $ do
68+-- tell "Hi"
69+-- handle (\(_::ErrorCall) -> pure ((), "")) $ do
70+-- tell " there"
71+-- listen $ do
72+-- tell "!"
73+-- error "oops"
74+-- :}
75+-- "Hi there!"
6576listen :: (Writer w :> es, Monoid w) => Eff es a -> Eff es (a, w)
6677listen m = unsafeEff $ \es -> do
6778 -- The mask is uninterruptible because modifyMVar_ v0 in the merge function
@@ -70,19 +81,28 @@ listen m = unsafeEff $ \es -> do
7081 uninterruptibleMask $ \restore -> do
7182 v1 <- newMVar mempty
7283 -- Replace thread local MVar with a fresh one for isolated listening.
73− v0 <- stateEnv es $ \(IdE (Writer v)) -> (v, IdE (Writer v1))
84+ v0 <- stateEnv es $ \(DataA (Writer v)) -> (v, DataA (Writer v1))
7485 a <- restore (unEff m es) `onException` merge es v0 v1
7586 (a, ) <$> merge es v0 v1
7687 where
7788 -- Merge results accumulated in the local MVar with the mainline. If an
7889 -- exception was received while listening, merge results recorded so far.
7990 merge es v0 v1 = do
80− putEnv es $ IdE (Writer v0)
91+ putEnv es $ DataA (Writer v0)
8192 w1 <- readMVar v1
8293 modifyMVar_ v0 $ \w0 -> let w = w0 <> w1 in w `seq` pure w
8394 pure w1
8495
96+-- | Execute an action and append its output to the overall output of the
97+-- 'Writer', then return the final value along with a function of the recorded
98+-- output.
99+--
100+-- @'listens' f m ≡ 'Data.Bifunctor.second' f '<$>' 'listen' m@
85101listens :: (Writer w :> es, Monoid w) => (w -> b) -> Eff es a -> Eff es (a, b)
86102listens f m = do
87103 (a, w) <- listen m
88104 pure (a, f w)
105+
106+-- $setup
107+-- >>> import Control.Exception (ErrorCall)
108+-- >>> import Control.Monad.Catch
effectful/bench/Main.hs+3−3
@@ -97,11 +97,11 @@ filesize n = bgroup (show n)
9797#ifdef VERSION_eff
9898 , bench "eff" $ nfAppIO eff_calculateFileSizesDeep (take n files)
9999#endif
100−#ifdef VERSION_polysemy
101− , bench "polysemy" $ nfAppIO poly_calculateFileSizesDeep (take n files)
102−#endif
103100#ifdef VERSION_mtl
104101 , bench "mtl" $ nfAppIO mtl_calculateFileSizesDeep (take n files)
102+#endif
103+#ifdef VERSION_polysemy
104+ , bench "polysemy" $ nfAppIO poly_calculateFileSizesDeep (take n files)
105105#endif
106106 ]
107107 ]
effectful/effectful.cabal+1−1
@@ -20,7 +20,7 @@ description: An easy to use, performant extensible effects library with seamless
2020
2121extra-source-files: CHANGELOG.md
2222
23−tested-with: GHC ==8.8.4 || ==8.10.7 || ==9.0.1 || ==9.2.1
23+tested-with: GHC ==8.8.4 || ==8.10.7 || ==9.2.1
2424
2525bug-reports: https://github.com/arybczak/effectful/issues
2626source-repository head
effectful/src/Effectful/Concurrent.hs+15−2
@@ -1,6 +1,19 @@
1+-- | Lifted version of "Control.Concurrent".
2+--
3+-- For functions that spawn threads, the order of preference for their usage is
4+-- recommended as follows:
5+--
6+-- 1) High level functions from "Effectful.Concurrent.Async" such as
7+-- 'Effectful.Concurrent.Async.withAsync',
8+-- 'Effectful.Concurrent.Async.concurrently' or
9+-- 'Effectful.Concurrent.Async.mapConcurrently'.
10+--
11+-- 2) Low level functions from "Effectful.Concurrent.Async" such as
12+-- 'Effectful.Concurrent.Async.async'.
13+--
14+-- 3) Low level functions from "Effectful.Concurrent" such as 'forkIO'.
115module Effectful.Concurrent
2− ( -- * The effect
3− Concurrent
16+ ( Concurrent
417 , runConcurrent
518
619 -- * Basic concurrency operations
effectful/src/Effectful/Concurrent/Async.hs+2−2
@@ -1,7 +1,7 @@
11{-# LANGUAGE UndecidableInstances #-}
2+-- | Lifted version of "Control.Concurrent.Async".
23module Effectful.Concurrent.Async
3− ( -- * The effect
4− Concurrent
4+ ( Concurrent
55 , runConcurrent
66
77 -- * Asynchronous actions
effectful/src/Effectful/Concurrent/Chan.hs+1−2
@@ -1,7 +1,6 @@
11-- | Lifted version of "Control.Concurrent.Chan".
22module Effectful.Concurrent.Chan
3− ( -- * The effect
4− Concurrent
3+ ( Concurrent
54 , runConcurrent
65
76 -- * Chan
effectful/src/Effectful/Concurrent/Effect.hs+64−5
@@ -6,15 +6,74 @@ module Effectful.Concurrent.Effect
66import Effectful.Dispatch.Static
77import Effectful.Monad
88
9−-- | Run 'Eff' operations asynchronously via the @async@ library.
9+-- | Provide the ability to run 'Eff' computations concurrently in multiple
10+-- threads and communicate between them.
1011--
11−-- /Note:/ thread local state changes in 'Eff' operations run asynchronously
12−-- will not affect the parent thread.
12+-- /Warning:/ unless you stick to high level functions from the
13+-- 'Effectful.Concurrent.Async.withAsync' family, the 'Concurrent' effect makes
14+-- it possible to escape the scope of any scoped effect operation. Consider the
15+-- following:
16+--
17+-- >>> import qualified Effectful.Reader as R
18+-- >>> let printAsk msg = liftIO . putStrLn . (msg ++) . (": " ++) =<< R.ask
19+-- >>> :{
20+-- runEff . R.runReader "GLOBAL" . runConcurrent $ do
21+-- a <- R.local (const "LOCAL") $ do
22+-- a <- async $ do
23+-- printAsk "child (first)"
24+-- threadDelay 20000
25+-- printAsk "child (second)"
26+-- threadDelay 10000
27+-- printAsk "parent (inside)"
28+-- pure a
29+-- printAsk "parent (outside)"
30+-- wait a
31+-- :}
32+-- child (first): LOCAL
33+-- parent (inside): LOCAL
34+-- parent (outside): GLOBAL
35+-- child (second): LOCAL
36+--
37+-- Note that the asynchronous computation doesn't respect the scope of
38+-- 'Effectful.Reader.local', i.e. the child thread still behaves like it's
39+-- inside the 'Effectful.Reader.local' block, even though the parent thread
40+-- already got out of it.
41+--
42+-- This is because the value provided by the 'Effectful.Reader.Reader' effect is
43+-- thread local, i.e. each thread manages its own version of it. For the
44+-- 'Effectful.Reader.Reader' it is the only reasonable behavior, it wouldn't be
45+-- very useful if its "read only" value was affected by calls to
46+-- 'Effectful.Reader.local' from its parent or child threads.
47+--
48+-- However, the cut isn't so clear if it comes to effects that provide access to
49+-- a mutable state. That's why primitive @State@ and @Writer@ effects come in
50+-- two flavors, local and shared:
51+--
52+-- >>> import qualified Effectful.State.Local as SL
53+-- >>> :{
54+-- runEff . SL.execState "Hi" . runConcurrent $ do
55+-- replicateConcurrently_ 3 $ SL.modify (++ "!")
56+-- :}
57+-- "Hi"
58+--
59+-- >>> import qualified Effectful.State.Shared as SS
60+-- >>> :{
61+-- runEff . SS.execState "Hi" . runConcurrent $ do
62+-- replicateConcurrently_ 3 $ SS.modify (++ "!")
63+-- :}
64+-- "Hi!!!"
65+--
66+-- In the first example state modification made concurrently are not reflected
67+-- in the parent thread because the value is thread local, but in the second
68+-- example they are, because the value is shared.
1369--
14−-- /TODO:/ write about 'Concurrent' not respecting scoped operations.
1570data Concurrent :: Effect where
1671 Concurrent :: Concurrent m r
1772
1873-- | Run the 'Concurrent' effect.
1974runConcurrent :: IOE :> es => Eff (Concurrent : es) a -> Eff es a
20−runConcurrent = evalEffect (IdE Concurrent)
75+runConcurrent = evalData (DataA Concurrent)
76+
77+-- $setup
78+-- >>> import Effectful.Concurrent
79+-- >>> import Effectful.Concurrent.Async
effectful/src/Effectful/Concurrent/MVar.hs+1−2
@@ -1,7 +1,6 @@
11-- | Lifted version of "Control.Concurrent.MVar".
22module Effectful.Concurrent.MVar
3− ( -- * The effect
4− Concurrent
3+ ( Concurrent
54 , runConcurrent
65
76 -- * MVar
effectful/src/Effectful/Concurrent/QSem.hs+2−3
@@ -1,7 +1,6 @@
1−-- | Lifted "Control.Concurrent.QSem".
1+-- | Lifted version of "Control.Concurrent.QSem".
22module Effectful.Concurrent.QSem
3− ( -- * The effect
4− Concurrent
3+ ( Concurrent
54 , runConcurrent
65
76 -- * QSem
effectful/src/Effectful/Concurrent/QSemN.hs+2−3
@@ -1,7 +1,6 @@
1−-- | Lifted "Control.Concurrent.QSemN".
1+-- | Lifted version of "Control.Concurrent.QSemN".
22module Effectful.Concurrent.QSemN
3− ( -- * The effect
4− Concurrent
3+ ( Concurrent
54 , runConcurrent
65
76 -- * QSemN
effectful/src/Effectful/Concurrent/STM.hs+1−2
@@ -1,7 +1,6 @@
11-- | Lifted version of "Control.Concurrent.STM".
22module Effectful.Concurrent.STM
3− ( -- * The effect
4− Concurrent
3+ ( Concurrent
54 , runConcurrent
65
76 -- * Core
effectful/src/Effectful/Environment.hs+2−3
@@ -1,6 +1,5 @@
11module Effectful.Environment
2− ( -- * The effect
3− Environment
2+ ( Environment
43 , runEnvironment
54
65 -- * Querying the environment
@@ -29,7 +28,7 @@ data Environment :: Effect where
2928
3029-- | Run the 'Environment' effect.
3130runEnvironment :: IOE :> es => Eff (Environment : es) a -> Eff es a
32−runEnvironment = evalEffect (IdE Environment)
31+runEnvironment = evalData (DataA Environment)
3332
3433-- | Lifted 'E.getArgs'.
3534getArgs :: Environment :> es => Eff es [String]
effectful/src/Effectful/FileSystem.hs+89−90
@@ -1,93 +1,92 @@
11module Effectful.FileSystem
2− ( -- * The effect
3− FileSystem
4− , runFileSystem
5−
6− -- * Actions on directories
7− , createDirectory
8− , createDirectoryIfMissing
9− , removeDirectory
10− , removeDirectoryRecursive
11− , removePathForcibly
12− , renameDirectory
13− , listDirectory
14− , getDirectoryContents
15−
16− -- ** Current working directory
17− , getCurrentDirectory
18− , setCurrentDirectory
19− , withCurrentDirectory
20−
21− -- * Pre-defined directories
22− , getHomeDirectory
23− , getXdgDirectory
24− , getXdgDirectoryList
25− , getAppUserDataDirectory
26− , getUserDocumentsDirectory
27− , getTemporaryDirectory
28−
29− -- * Actions on files
30− , removeFile
31− , renameFile
32− , renamePath
33− , copyFile
34− , copyFileWithMetadata
35− , getFileSize
36− , canonicalizePath
37− , makeAbsolute
38− , makeRelativeToCurrentDirectory
39−
40− -- * Existence tests
41− , doesPathExist
42− , doesFileExist
43− , doesDirectoryExist
44− , findExecutable
45− , findExecutables
46− , findExecutablesInDirectories
47− , findFile
48− , findFiles
49− , findFileWith
50− , findFilesWith
51−
52− -- * Symbolic links
53− , createFileLink
54− , createDirectoryLink
55− , removeDirectoryLink
56− , pathIsSymbolicLink
57− , getSymbolicLinkTarget
58−
59− -- * Permissions
60− , getPermissions
61− , setPermissions
62− , copyPermissions
63−
64− -- * Timestamps
65− , getAccessTime
66− , getModificationTime
67− , setAccessTime
68− , setModificationTime
69−
70− -- * Re-exports
71−
72− -- ** Pre-defined directories
73− , D.XdgDirectory(..)
74− , D.XdgDirectoryList(..)
75−
76− -- ** Existence tests
77− , D.exeExtension
78−
79− -- ** Permissions
80− , D.Permissions
81− , D.emptyPermissions
82− , D.readable
83− , D.writable
84− , D.executable
85− , D.searchable
86− , D.setOwnerReadable
87− , D.setOwnerWritable
88− , D.setOwnerExecutable
89− , D.setOwnerSearchable
90− ) where
2+ ( FileSystem
3+ , runFileSystem
4+
5+ -- * Actions on directories
6+ , createDirectory
7+ , createDirectoryIfMissing
8+ , removeDirectory
9+ , removeDirectoryRecursive
10+ , removePathForcibly
11+ , renameDirectory
12+ , listDirectory
13+ , getDirectoryContents
14+
15+ -- ** Current working directory
16+ , getCurrentDirectory
17+ , setCurrentDirectory
18+ , withCurrentDirectory
19+
20+ -- * Pre-defined directories
21+ , getHomeDirectory
22+ , getXdgDirectory
23+ , getXdgDirectoryList
24+ , getAppUserDataDirectory
25+ , getUserDocumentsDirectory
26+ , getTemporaryDirectory
27+
28+ -- * Actions on files
29+ , removeFile
30+ , renameFile
31+ , renamePath
32+ , copyFile
33+ , copyFileWithMetadata
34+ , getFileSize
35+ , canonicalizePath
36+ , makeAbsolute
37+ , makeRelativeToCurrentDirectory
38+
39+ -- * Existence tests
40+ , doesPathExist
41+ , doesFileExist
42+ , doesDirectoryExist
43+ , findExecutable
44+ , findExecutables
45+ , findExecutablesInDirectories
46+ , findFile
47+ , findFiles
48+ , findFileWith
49+ , findFilesWith
50+
51+ -- * Symbolic links
52+ , createFileLink
53+ , createDirectoryLink
54+ , removeDirectoryLink
55+ , pathIsSymbolicLink
56+ , getSymbolicLinkTarget
57+
58+ -- * Permissions
59+ , getPermissions
60+ , setPermissions
61+ , copyPermissions
62+
63+ -- * Timestamps
64+ , getAccessTime
65+ , getModificationTime
66+ , setAccessTime
67+ , setModificationTime
68+
69+ -- * Re-exports
70+
71+ -- ** Pre-defined directories
72+ , D.XdgDirectory(..)
73+ , D.XdgDirectoryList(..)
74+
75+ -- ** Existence tests
76+ , D.exeExtension
77+
78+ -- ** Permissions
79+ , D.Permissions
80+ , D.emptyPermissions
81+ , D.readable
82+ , D.writable
83+ , D.executable
84+ , D.searchable
85+ , D.setOwnerReadable
86+ , D.setOwnerWritable
87+ , D.setOwnerExecutable
88+ , D.setOwnerSearchable
89+ ) where
9190
9291import Data.Time (UTCTime)
9392import qualified System.Directory as D
@@ -101,7 +100,7 @@ data FileSystem :: Effect where
101100
102101-- | Run the 'FileSystem' effect.
103102runFileSystem :: IOE :> es => Eff (FileSystem : es) a -> Eff es a
104−runFileSystem = evalEffect (IdE FileSystem)
103+runFileSystem = evalData (DataA FileSystem)
105104
106105----------------------------------------
107106-- Actions on directories
effectful/src/Effectful/FileSystem/Effect.hs+1−1
@@ -12,4 +12,4 @@ data FileSystem :: Effect where
1212
1313-- | Run the 'FileSystem' effect.
1414runFileSystem :: IOE :> es => Eff (FileSystem : es) a -> Eff es a
15−runFileSystem = evalEffect (IdE FileSystem)
15+runFileSystem = evalData (DataA FileSystem)
effectful/src/Effectful/FileSystem/IO.hs+1−2
@@ -1,7 +1,6 @@
11-- | Lifted version of "System.IO".
22module Effectful.FileSystem.IO
3− ( -- * The effect
4− FileSystem
3+ ( FileSystem
54 , runFileSystem
65
76 -- * IO
effectful/src/Effectful/Process.hs+2−3
@@ -1,8 +1,7 @@
11{-# LANGUAGE CPP #-}
22-- | Lifted version of "System.Process".
33module Effectful.Process
4− ( -- * The effect
5− Process
4+ ( Process
65 , runProcess
76
87 -- * Running sub-processes
@@ -59,7 +58,7 @@ data Process :: Effect where
5958 Process :: Process m r
6059
6160runProcess :: IOE :> es => Eff (Process : es) a -> Eff es a
62−runProcess = evalEffect (IdE Process)
61+runProcess = evalData (DataA Process)
6362
6463----------------------------------------
6564-- Running sub-processes
effectful/src/Effectful/Resource.hs+4−4
@@ -37,7 +37,7 @@ runResource m = unsafeEff $ \es0 -> do
3737 size0 <- sizeEnv es0
3838 istate <- R.createInternalState
3939 mask $ \restore -> do
40− es <- unsafeConsEnv (IdE (Resource istate)) noRelinker es0
40+ es <- unsafeConsEnv (DataA (Resource istate)) noRelinker es0
4141 a <- restore (unEff m es) `catch` \e -> do
4242 unsafeTailEnv size0 es
4343 RI.stateCleanupChecked (Just e) istate
@@ -52,18 +52,18 @@ runResource m = unsafeEff $ \es0 -> do
5252-- | Get the 'R.InternalState' of the current 'Resource' effect.
5353getInternalState :: Resource :> es => Eff es R.InternalState
5454getInternalState = do
55− IdE (Resource istate) <- getEffect
55+ DataA (Resource istate) <- getData
5656 pure istate
5757
5858-- | Run the 'Resource' effect with existing 'R.InternalState'.
5959--
6060-- /Note:/ the 'R.InternalState' will not be closed at the end.
6161runInternalState :: R.InternalState -> Eff (Resource : es) a -> Eff es a
62−runInternalState istate = evalEffect $ IdE (Resource istate)
62+runInternalState istate = evalData (DataA (Resource istate))
6363
6464----------------------------------------
6565-- Orphan instance
6666
6767instance (IOE :> es, Resource :> es) => R.MonadResource (Eff es) where
6868 liftResourceT (RI.ResourceT m) = unsafeEff $ \es -> do
69− getEnv es >>= \(IdE (Resource istate)) -> m istate
69+ getEnv es >>= \(DataA (Resource istate)) -> m istate
effectful/src/Effectful/Temporary.hs+1−1
@@ -19,7 +19,7 @@ data Temporary :: Effect where
1919
2020-- | Run the 'Temporary' effect.
2121runTemporary :: IOE :> es => Eff (Temporary : es) a -> Eff es a
22−runTemporary = evalEffect (IdE Temporary)
22+runTemporary = evalData (DataA Temporary)
2323
2424-- | Lifted 'T.withSystemTempFile'.
2525withSystemTempFile
effectful/src/Effectful/Timeout.hs+1−1
@@ -15,7 +15,7 @@ data Timeout :: Effect where
1515
1616-- | Run the 'Timeout' effect.
1717runTimeout :: IOE :> es => Eff (Timeout : es) a -> Eff es a
18−runTimeout = evalEffect (IdE Timeout)
18+runTimeout = evalData (DataA Timeout)
1919
2020-- | Lifted 'T.timeout'.
2121timeout