on master help sign in
.github/workflows/haskell-ci.yml−5
since splitsnapshots
unifiedsplit
@@ -37,11 +37,6 @@ jobs:
37 compilerVersion: 9.2.1
37 compilerVersion: 9.2.1
38 setup-method: ghcup
38 setup-method: ghcup
39 allow-failure: false
39 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
45 - compiler: ghc-8.10.7
40 - compiler: ghc-8.10.7
46 compilerKind: ghc
41 compilerKind: ghc
47 compilerVersion: 8.10.7
42 compilerVersion: 8.10.7
effectful-core/effectful-core.cabal+1−1
@@ -18,7 +18,7 @@ description: An easy to use, performant extensible effects library with seamless
18
18
19extra-source-files: CHANGELOG.md
19extra-source-files: CHANGELOG.md
20
20
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
22
22
23bug-reports: https://github.com/arybczak/effectful/issues
23bug-reports: https://github.com/arybczak/effectful/issues
24source-repository head
24source-repository head
effectful-core/src/Effectful/Dispatch/Dynamic.hs+13−1
@@ -32,7 +32,8 @@ module Effectful.Dispatch.Dynamic
32 ) where
32 ) where
33
33
34import Control.Monad.IO.Unlift
34import Control.Monad.IO.Unlift
35−import GHC.Stack
35+import Data.Kind
36+import GHC.Stack (HasCallStack)
36
37
37import Effectful.Internal.Effect
38import Effectful.Internal.Effect
38import Effectful.Internal.Env
39import Effectful.Internal.Env
@@ -230,5 +231,16 @@ localLiftUnliftIO (LocalEnv les) strategy k = case strategy of
230 SeqUnlift -> liftIO $ seqUnliftIO les $ k unsafeEff_
231 SeqUnlift -> liftIO $ seqUnliftIO les $ k unsafeEff_
231 ConcUnlift p l -> liftIO $ concUnliftIO les p l $ k unsafeEff_
232 ConcUnlift p l -> liftIO $ concUnliftIO les p l $ k unsafeEff_
232
233
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+
233-- $setup
245-- $setup
234-- >>> import Control.Concurrent
246-- >>> import Control.Concurrent
effectful-core/src/Effectful/Dispatch/Static.hs+9−12
@@ -2,12 +2,12 @@ module Effectful.Dispatch.Static
2 ( -- * Low level API
2 ( -- * Low level API
3 DataA(..)
3 DataA(..)
4
4
5− -- ** Running
5+ -- ** Extending the environment
6 , runData
6 , runData
7 , evalData
7 , evalData
8 , execData
8 , execData
9
9
10− -- ** Modification
10+ -- ** Data retrieval and update
11 , getData
11 , getData
12 , putData
12 , putData
13 , stateData
13 , stateData
@@ -18,6 +18,13 @@ module Effectful.Dispatch.Static
18 , seqUnliftIO
18 , seqUnliftIO
19 , concUnliftIO
19 , concUnliftIO
20
20
21+ -- ** Utils
22+ , unEff
23+ , unsafeEff
24+ , unsafeEff_
25+ , unsafeLiftMapIO
26+ , unsafeUnliftIO
27+
21 -- * Primitive API
28 -- * Primitive API
22 , Env
29 , Env
23 , Relinker(..)
30 , Relinker(..)
@@ -39,18 +46,8 @@ module Effectful.Dispatch.Static
39 , putEnv
46 , putEnv
40 , stateEnv
47 , stateEnv
41 , modifyEnv
48 , modifyEnv
42−
43− -- * Unsafe 'Eff' operations
44− , unsafeEff
45− , unsafeEff_
46− , unsafeLiftMapIO
47− , unsafeUnliftIO
48−
49− -- * Utils
50− , unEff
51 ) where
49 ) where
52
50
53−import Effectful.Internal.Effect
54import Effectful.Internal.Env
51import Effectful.Internal.Env
55import Effectful.Internal.Monad
52import Effectful.Internal.Monad
56
53
effectful-core/src/Effectful/Internal/Effect.hs+1−17
@@ -8,10 +8,6 @@
8module Effectful.Internal.Effect
8module Effectful.Internal.Effect
9 ( Effect
9 ( Effect
10 , (:>)(..)
10 , (:>)(..)
11− , DataA(..)
12−
13− -- * Utils
14− , SuffixOf
15
11
16 -- * Re-exports
12 -- * Re-exports
17 , Type
13 , Type
@@ -23,10 +19,6 @@ import GHC.TypeLits
23-- | The kind of effects.
19-- | The kind of effects.
24type Effect = (Type -> Type) -> Type -> Type
20type Effect = (Type -> Type) -> Type -> Type
25
21
26−-- | An adapter for statically dispatched effects.
27−newtype DataA :: Effect -> Type where
28− DataA :: (forall m r. e m r) -> DataA e
29−
30-- | A constraint that requires that a particular effect @e@ is a member of the
22-- | A constraint that requires that a particular effect @e@ is a member of the
31-- type-level list @es@. This is used to parameterize an 'Effectful.Monad.Eff'
23-- type-level list @es@. This is used to parameterize an 'Effectful.Monad.Eff'
32-- computation over an arbitrary list of effects, so long as @e@ is /somewhere/
24-- computation over an arbitrary list of effects, so long as @e@ is /somewhere/
@@ -36,7 +28,7 @@ newtype DataA :: Effect -> Type where
36-- 'Integer' would likely use the following type:
28-- 'Integer' would likely use the following type:
37--
29--
38-- @
30-- @
39−-- 'Effectful.State.State' 'Integer' ':>' es => 'Effectful.Monad.Eff' es ()
31+-- 'Effectful.State.Local.State' 'Integer' ':>' es => 'Effectful.Monad.Eff' es ()
40-- @
32-- @
41class (e :: Effect) :> (es :: [Effect]) where
33class (e :: Effect) :> (es :: [Effect]) where
42 -- | Get the position of @e@ in @es@.
34 -- | Get the position of @e@ in @es@.
@@ -59,11 +51,3 @@ instance {-# OVERLAPPING #-} e :> (e : es) where
59
51
60instance e :> es => e :> (x : es) where
52instance e :> es => e :> (x : es) where
61 reifyIndex = 1 + reifyIndex @e @es
53 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 #-}
1{-# OPTIONS_HADDOCK not-home #-}
2{-# OPTIONS_HADDOCK not-home #-}
2-- | The environment for the 'Effectful.Internal.Monad.Eff' monad.
3-- | The environment for the 'Effectful.Internal.Monad.Eff' monad.
3--
4--
@@ -42,7 +43,7 @@ import Control.Monad
42import Control.Monad.Primitive
43import Control.Monad.Primitive
43import Data.IORef
44import Data.IORef
44import Data.Primitive.SmallArray
45import Data.Primitive.SmallArray
45−import GHC.Exts (Any)
46+import GHC.Exts (Any, SmallMutableArray#)
46import GHC.Stack (HasCallStack)
47import GHC.Stack (HasCallStack)
47import Unsafe.Coerce
48import Unsafe.Coerce
48import qualified Data.IntMap.Strict as IM
49import qualified Data.IntMap.Strict as IM
@@ -417,43 +418,43 @@ unsafeTailEnv len (Env fork gref _) = case fork of
417
418
418-- | Extract a specific data type from the environment.
419-- | Extract a specific data type from the environment.
419getEnv
420getEnv
420− :: forall e es adapter. e :> es
421+ :: forall e adapter es. e :> es
421 => Env es
422 => Env es
422 -> IO (adapter e)
423 -> IO (adapter e)
423getEnv env = do
424getEnv env = do
424− (i, es) <- getLocation (reifyIndex @e @es) env
425+ Location i es <- getLocation (reifyIndex @e @es) env
425 fromAny <$> readSmallArray es i
426 fromAny <$> readSmallArray es i
426
427
427-- | Replace the data type in the environment with a new value (in place).
428-- | Replace the data type in the environment with a new value (in place).
428putEnv
429putEnv
429− :: forall e es adapter. e :> es
430+ :: forall e adapter es. e :> es
430 => Env es
431 => Env es
431 -> adapter e
432 -> adapter e
432 -> IO ()
433 -> IO ()
433putEnv env e = do
434putEnv env e = do
434− (i, es) <- getLocation (reifyIndex @e @es) env
435+ Location i es <- getLocation (reifyIndex @e @es) env
435 e `seq` writeSmallArray es i (toAny e)
436 e `seq` writeSmallArray es i (toAny e)
436
437
437-- | Modify the data type in the environment (in place) and return a value.
438-- | Modify the data type in the environment (in place) and return a value.
438stateEnv
439stateEnv
439− :: forall e es adapter a. e :> es
440+ :: forall e adapter es a. e :> es
440 => Env es
441 => Env es
441 -> (adapter e -> (a, adapter e))
442 -> (adapter e -> (a, adapter e))
442 -> IO a
443 -> IO a
443stateEnv env f = do
444stateEnv env f = do
444− (i, es) <- getLocation (reifyIndex @e @es) env
445+ Location i es <- getLocation (reifyIndex @e @es) env
445 (a, e) <- f . fromAny <$> readSmallArray es i
446 (a, e) <- f . fromAny <$> readSmallArray es i
446 e `seq` writeSmallArray es i (toAny e)
447 e `seq` writeSmallArray es i (toAny e)
447 pure a
448 pure a
448
449
449-- | Modify the data type in the environment (in place).
450-- | Modify the data type in the environment (in place).
450modifyEnv
451modifyEnv
451− :: forall e es adapter. e :> es
452+ :: forall e adapter es. e :> es
452 => Env es
453 => Env es
453 -> (adapter e -> adapter e)
454 -> (adapter e -> adapter e)
454 -> IO ()
455 -> IO ()
455modifyEnv env f = do
456modifyEnv env f = do
456− (i, es) <- getLocation (reifyIndex @e @es) env
457+ Location i es <- getLocation (reifyIndex @e @es) env
457 e <- f . fromAny <$> readSmallArray es i
458 e <- f . fromAny <$> readSmallArray es i
458 e `seq` writeSmallArray es i (toAny e)
459 e `seq` writeSmallArray es i (toAny e)
459
460
@@ -469,13 +470,17 @@ emptyEnvRef = do
469 fs <- newSmallArray 0 undefinedData
470 fs <- newSmallArray 0 undefinedData
470 newIORef $ EnvRef 0 es fs
471 newIORef $ EnvRef 0 es fs
471
472
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+
472-- | Determine location of the data type in the environment.
477-- | Determine location of the data type in the environment.
473getLocation
478getLocation
474 :: Int
479 :: Int
475 -> Env es
480 -> Env es
476− -> IO (Int, SmallMutableArray RealWorld Any)
481+ -> IO Location
477getLocation ixE (Env fork ref _) = do
482getLocation ixE (Env fork ref _) = do
478− EnvRef n es _ <- readIORef ref
483+ EnvRef n es@(SmallMutableArray es#) _ <- readIORef ref
479 -- Optimized for the common access pattern:
484 -- Optimized for the common access pattern:
480 --
485 --
481 -- - Most application code has no access to forks, in which case we look at
486 -- - Most application code has no access to forks, in which case we look at
@@ -484,25 +489,25 @@ getLocation ixE (Env fork ref _) = do
484 -- - Effect handlers will most likely access the newest fork with handler
489 -- - Effect handlers will most likely access the newest fork with handler
485 -- specific effects for reinterpretation.
490 -- specific effects for reinterpretation.
486 case fork of
491 case fork of
487− NoFork -> pure (ix n, es)
492+ NoFork -> pure $ Location (ix n) es
488 Forks _ baseIx lref forks -> do
493 Forks _ baseIx lref forks -> do
489 EnvRef ln les _ <- readIORef lref
494 EnvRef ln les _ <- readIORef lref
490 let i = ix (baseIx + ln)
495 let i = ix (baseIx + ln)
491 if i >= baseIx
496 if i >= baseIx
492− then pure (i - baseIx, les)
497+ then pure $ Location (i - baseIx) les
493− else go es i forks
498+ else go es# i forks
494 where
499 where
495− go :: SmallMutableArray RealWorld Any
500+ go :: SmallMutableArray# RealWorld Any
496 -> Int
501 -> Int
497 -> Forks
502 -> Forks
498− -> IO (Int, SmallMutableArray RealWorld Any)
503+ -> IO Location
499− go es i = \case
504+ go es# i = \case
500− NoFork -> pure (i, es)
505+ NoFork -> pure $ Location i (SmallMutableArray es#)
501 Forks _ baseIx lref forks -> do
506 Forks _ baseIx lref forks -> do
502 EnvRef _ les _ <- readIORef lref
507 EnvRef _ les _ <- readIORef lref
503 if i >= baseIx
508 if i >= baseIx
504− then pure (i - baseIx, les)
509+ then pure $ Location (i - baseIx) les
505− else go es i forks
510+ else go es# i forks
506
511
507 ix :: Int -> Int
512 ix :: Int -> Int
508 ix n = n - ixE - 1
513 ix n = n - ixE - 1
effectful-core/src/Effectful/Internal/Monad.hs+38−25
@@ -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
10 Eff
10 Eff
11 , runPureEff
11 , runPureEff
12
12
@@ -22,11 +22,11 @@ module Effectful.Internal.Monad
22 , IOE
22 , IOE
23 , runEff
23 , runEff
24
24
25− -- * Primitive
25+ -- * Prim
26 , Prim
26 , Prim
27 , runPrim
27 , runPrim
28
28
29− -- ** Unlift strategies
29+ -- * Unlift strategies
30 , UnliftStrategy(..)
30 , UnliftStrategy(..)
31 , Persistence(..)
31 , Persistence(..)
32 , Limit(..)
32 , Limit(..)
@@ -34,29 +34,24 @@ module Effectful.Internal.Monad
34 , withUnliftStrategy
34 , withUnliftStrategy
35 , withEffToIO
35 , withEffToIO
36
36
37− --- *** Low-level helpers
37+ -- ** Low-level unlifts
38 , seqUnliftIO
38 , seqUnliftIO
39 , concUnliftIO
39 , concUnliftIO
40
40
41 -- * Effects
41 -- * Effects
42
42
43− -- ** Handler
43+ -- ** Dynamic dispatch
44 , EffectHandler
44 , EffectHandler
45 , LocalEnv(..)
45 , LocalEnv(..)
46 , HandlerA(..)
46 , HandlerA(..)
47 , runHandler
47 , runHandler
48−
49− -- *** Sending operations
50 , send
48 , send
51
49
52− -- ** Primitive
50+ -- ** Static dispatch
53−
51+ , DataA(..)
54− -- *** Running
55 , runData
52 , runData
56 , evalData
53 , evalData
57 , execData
54 , execData
58−
59− -- *** Modification
60 , getData
55 , getData
61 , putData
56 , putData
62 , stateData
57 , stateData
@@ -92,17 +87,16 @@ type role Eff nominal representational
92-- would have the following type:
87-- would have the following type:
93--
88--
94-- @
89-- @
95−-- 'Eff' '['Effectful.Reader.Reader' 'String', 'Effectful.State.State' 'Bool'] 'Integer'
90+-- 'Eff' '['Effectful.Reader.Reader' 'String', 'Effectful.State.Local.State' 'Bool'] 'Integer'
96-- @
91-- @
97--
92--
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 a computation 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:
102−-- following type:
103--
97--
104-- @
98-- @
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'
106-- @
100-- @
107--
101--
108-- This abstraction allows the computation to be used in functions that may
102-- 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)
116-- For running computations 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).
122 --
116 --
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
126
120
127----------------------------------------
121----------------------------------------
@@ -164,7 +158,7 @@ withUnliftStrategy unlift = localData $ \_ -> DataA (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.
166--
160--
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)
@@ -309,7 +303,7 @@ instance Prim :> es => PrimMonad (Eff es) where
309 primitive = unsafeEff_ . IO
303 primitive = unsafeEff_ . IO
310
304
311----------------------------------------
305----------------------------------------
312−-- Handler
306+-- Dynamic dispatch
313
307
314type role LocalEnv nominal nominal
308type role LocalEnv nominal nominal
315
309
@@ -333,11 +327,11 @@ type EffectHandler e es
333
327
334-- | An adapter for dynamically dispatched effects.
328-- | An adapter for dynamically dispatched effects.
335--
329--
336−-- Represents the effect handler bundled along with its environment.
330+-- Represents the effect handler bundled with its environment.
337data HandlerA :: Effect -> Type where
331data HandlerA :: Effect -> Type where
338 HandlerA :: !(Env es) -> !(EffectHandler e es) -> HandlerA e
332 HandlerA :: !(Env es) -> !(EffectHandler e es) -> HandlerA e
339
333
340−-- | Run the effect with the given handler.
334+-- | Run a dynamically dispatched effect with the given handler.
341runHandler :: HandlerA e -> Eff (e : es) a -> Eff es a
335runHandler :: 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
@@ -357,8 +351,17 @@ send op = unsafeEff $ \es -> do
357 unEff (handle (LocalEnv es) op) handlerEs
351 unEff (handle (LocalEnv es) op) handlerEs
358
352
359----------------------------------------
353----------------------------------------
360−-- Helpers
354+-- Static dispatch
355+
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
361
362
363+-- | Run a statically dispatched effect with the given initial state and return
364+-- the final value along with the final state.
362runData :: DataA e -> Eff (e : es) a -> Eff es (a, DataA e)
365runData :: DataA e -> Eff (e : es) a -> Eff es (a, DataA e)
363runData e0 m = unsafeEff $ \es0 -> do
366runData e0 m = unsafeEff $ \es0 -> do
364 size0 <- sizeEnv es0
367 size0 <- sizeEnv es0
@@ -366,6 +369,8 @@ runData e0 m = unsafeEff $ \es0 -> do
366 (unsafeTailEnv size0)
369 (unsafeTailEnv size0)
367 (\es -> (,) <$> unEff m es <*> getEnv es)
370 (\es -> (,) <$> unEff m es <*> getEnv es)
368
371
372+-- | Run a statically dispatched effect with the given initial state and return
373+-- the final value, discarding the final state.
369evalData :: DataA e -> Eff (e : es) a -> Eff es a
374evalData :: DataA e -> Eff (e : es) a -> Eff es a
370evalData e m = unsafeEff $ \es0 -> do
375evalData e m = unsafeEff $ \es0 -> do
371 size0 <- sizeEnv es0
376 size0 <- sizeEnv es0
@@ -373,6 +378,8 @@ evalData e m = unsafeEff $ \es0 -> do
373 (unsafeTailEnv size0)
378 (unsafeTailEnv size0)
374 (\es -> unEff m es)
379 (\es -> unEff m es)
375
380
381+-- | Run a statically dispatched effect with the given initial state and return
382+-- the final state, discarding the final value.
376execData :: DataA e -> Eff (e : es) a -> Eff es (DataA e)
383execData :: DataA e -> Eff (e : es) a -> Eff es (DataA e)
377execData e0 m = unsafeEff $ \es0 -> do
384execData e0 m = unsafeEff $ \es0 -> do
378 size0 <- sizeEnv es0
385 size0 <- sizeEnv es0
@@ -380,21 +387,27 @@ execData e0 m = unsafeEff $ \es0 -> do
380 (unsafeTailEnv size0)
387 (unsafeTailEnv size0)
381 (\es -> unEff m es *> getEnv es)
388 (\es -> unEff m es *> getEnv es)
382
389
390+-- | Fetch the current state of the effect.
383getData :: e :> es => Eff es (DataA e)
391getData :: e :> es => Eff es (DataA e)
384getData = unsafeEff $ \es -> getEnv es
392getData = unsafeEff $ \es -> getEnv es
385
393
394+-- | Set the current state of the effect to the given value.
386putData :: e :> es => DataA e -> Eff es ()
395putData :: e :> es => DataA e -> Eff es ()
387putData e = unsafeEff $ \es -> putEnv es e
396putData e = unsafeEff $ \es -> putEnv es e
388
397
398+-- | Apply the function to the current state of the effect and return a value.
389stateData :: e :> es => (DataA e -> (a, DataA e)) -> Eff es a
399stateData :: e :> es => (DataA e -> (a, DataA e)) -> Eff es a
390stateData f = unsafeEff $ \es -> stateEnv es f
400stateData f = unsafeEff $ \es -> stateEnv es f
391
401
402+-- | Apply the monadic function to the current state of the effect and return a
403+-- value.
392stateDataM :: e :> es => (DataA e -> Eff es (a, DataA e)) -> Eff es a
404stateDataM :: e :> es => (DataA e -> Eff es (a, DataA e)) -> Eff es a
393stateDataM f = unsafeEff $ \es -> E.mask $ \release -> do
405stateDataM 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
396 pure a
408 pure a
397
409
410+-- | Execute a computation with a temporarily modified state of the effect.
398localData :: e :> es => (DataA e -> DataA e) -> Eff es a -> Eff es a
411localData :: e :> es => (DataA e -> DataA e) -> Eff es a -> Eff es a
399localData f m = unsafeEff $ \es -> do
412localData f m = unsafeEff $ \es -> do
400 E.bracket (stateEnv es $ \e -> (e, f e))
413 E.bracket (stateEnv es $ \e -> (e, f e))
effectful-core/src/Effectful/Writer/Local.hs+3−3
@@ -55,9 +55,9 @@ tell w = stateData $ \(DataA (Writer w0)) -> ((), DataA (Writer (w0 <> w)))
55-- | Execute an action and append its output to the overall output of the
55-- | Execute an action and append its output to the overall output of the
56-- 'Writer'.
56-- 'Writer'.
57--
57--
58−-- /Note:/ if a runtime exception is received while the action is executed, the
58+-- /Note:/ if an exception is received while the action is executed, the partial
59−-- partial output of the action will still be appended to the overall output of
59+-- output of the action will still be appended to the overall output of the
60−-- the 'Writer':
60+-- 'Writer':
61--
61--
62-- >>> :{
62-- >>> :{
63-- runEff . execWriter @String $ do
63-- runEff . execWriter @String $ do
effectful-core/src/Effectful/Writer/Shared.hs+3−3
@@ -59,9 +59,9 @@ tell w1 = unsafeEff $ \es -> do
59-- | Execute an action and append its output to the overall output of the
59-- | Execute an action and append its output to the overall output of the
60-- 'Writer'.
60-- 'Writer'.
61--
61--
62−-- /Note:/ if a runtime exception is received while the action is executed, the
62+-- /Note:/ if an exception is received while the action is executed, the partial
63−-- partial output of the action will still be appended to the overall output of
63+-- output of the action will still be appended to the overall output of the
64−-- the 'Writer':
64+-- 'Writer':
65--
65--
66-- >>> :{
66-- >>> :{
67-- runEff . execWriter @String $ do
67-- runEff . execWriter @String $ do
effectful/bench/Main.hs+3−3
@@ -97,11 +97,11 @@ filesize n = bgroup (show n)
97#ifdef VERSION_eff
97#ifdef VERSION_eff
98 , bench "eff" $ nfAppIO eff_calculateFileSizesDeep (take n files)
98 , bench "eff" $ nfAppIO eff_calculateFileSizesDeep (take n files)
99#endif
99#endif
100−#ifdef VERSION_polysemy
101− , bench "polysemy" $ nfAppIO poly_calculateFileSizesDeep (take n files)
102−#endif
103#ifdef VERSION_mtl
100#ifdef VERSION_mtl
104 , bench "mtl" $ nfAppIO mtl_calculateFileSizesDeep (take n files)
101 , bench "mtl" $ nfAppIO mtl_calculateFileSizesDeep (take n files)
102+#endif
103+#ifdef VERSION_polysemy
104+ , bench "polysemy" $ nfAppIO poly_calculateFileSizesDeep (take n files)
105#endif
105#endif
106 ]
106 ]
107 ]
107 ]
effectful/effectful.cabal+1−1
@@ -20,7 +20,7 @@ description: An easy to use, performant extensible effects library with seamless
20
20
21extra-source-files: CHANGELOG.md
21extra-source-files: CHANGELOG.md
22
22
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
24
24
25bug-reports: https://github.com/arybczak/effectful/issues
25bug-reports: https://github.com/arybczak/effectful/issues
26source-repository head
26source-repository head