on f749af5 help sign in
11 files changed+91−97
since splitsnapshots
unifiedsplit
.github/workflows/haskell-ci.yml+5 effectful-core/effectful-core.cabal+1−1 effectful-core/src/Effectful/Dispatch/Dynamic.hs+1−13 effectful-core/src/Effectful/Dispatch/Static.hs+12−9 effectful-core/src/Effectful/Internal/Effect.hs+17−1 effectful-core/src/Effectful/Internal/Env.hs+20−25 effectful-core/src/Effectful/Internal/Monad.hs+25−38 effectful-core/src/Effectful/Writer/Local.hs+3−3 effectful-core/src/Effectful/Writer/Shared.hs+3−3 effectful/bench/Main.hs+3−3 effectful/effectful.cabal+1−1
.github/workflows/haskell-ci.yml+5
@@ -37,6 +37,11 @@ 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
4045 - compiler: ghc-8.10.7
4146 compilerKind: ghc
4247 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
1818
1919extra-source-files: CHANGELOG.md
2020
21−tested-with: GHC ==8.8.4 || ==8.10.7 || ==9.2.1
21+tested-with: GHC ==8.8.4 || ==8.10.7 || ==9.0.1 || ==9.2.1
2222
2323bug-reports: https://github.com/arybczak/effectful/issues
2424source-repository head
effectful-core/src/Effectful/Dispatch/Dynamic.hs+1−13
@@ -32,8 +32,7 @@ module Effectful.Dispatch.Dynamic
3232 ) where
3333
3434import Control.Monad.IO.Unlift
35−import Data.Kind
36−import GHC.Stack (HasCallStack)
35+import GHC.Stack
3736
3837import Effectful.Internal.Effect
3938import Effectful.Internal.Env
@@ -231,16 +230,5 @@ localLiftUnliftIO (LocalEnv les) strategy k = case strategy of
231230 SeqUnlift -> liftIO $ seqUnliftIO les $ k unsafeEff_
232231 ConcUnlift p l -> liftIO $ concUnliftIO les p l $ k unsafeEff_
233232
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−
245233-- $setup
246234-- >>> import Control.Concurrent
effectful-core/src/Effectful/Dispatch/Static.hs+12−9
@@ -2,12 +2,12 @@ module Effectful.Dispatch.Static
22 ( -- * Low level API
33 DataA(..)
44
5− -- ** Extending the environment
5+ -- ** Running
66 , runData
77 , evalData
88 , execData
99
10− -- ** Data retrieval and update
10+ -- ** Modification
1111 , getData
1212 , putData
1313 , stateData
@@ -18,13 +18,6 @@ module Effectful.Dispatch.Static
1818 , seqUnliftIO
1919 , concUnliftIO
2020
21− -- ** Utils
22− , unEff
23− , unsafeEff
24− , unsafeEff_
25− , unsafeLiftMapIO
26− , unsafeUnliftIO
27−
2821 -- * Primitive API
2922 , Env
3023 , Relinker(..)
@@ -46,8 +39,18 @@ module Effectful.Dispatch.Static
4639 , putEnv
4740 , stateEnv
4841 , modifyEnv
42+
43+ -- * Unsafe 'Eff' operations
44+ , unsafeEff
45+ , unsafeEff_
46+ , unsafeLiftMapIO
47+ , unsafeUnliftIO
48+
49+ -- * Utils
50+ , unEff
4951 ) where
5052
53+import Effectful.Internal.Effect
5154import Effectful.Internal.Env
5255import Effectful.Internal.Monad
5356
effectful-core/src/Effectful/Internal/Effect.hs+17−1
@@ -8,6 +8,10 @@
88module Effectful.Internal.Effect
99 ( Effect
1010 , (:>)(..)
11+ , DataA(..)
12+
13+ -- * Utils
14+ , SuffixOf
1115
1216 -- * Re-exports
1317 , Type
@@ -19,6 +23,10 @@ import GHC.TypeLits
1923-- | The kind of effects.
2024type Effect = (Type -> Type) -> Type -> Type
2125
26+-- | An adapter for statically dispatched effects.
27+newtype DataA :: Effect -> Type where
28+ DataA :: (forall m r. e m r) -> DataA e
29+
2230-- | A constraint that requires that a particular effect @e@ is a member of the
2331-- type-level list @es@. This is used to parameterize an 'Effectful.Monad.Eff'
2432-- computation over an arbitrary list of effects, so long as @e@ is /somewhere/
@@ -28,7 +36,7 @@ type Effect = (Type -> Type) -> Type -> Type
2836-- 'Integer' would likely use the following type:
2937--
3038-- @
31−-- 'Effectful.State.Local.State' 'Integer' ':>' es => 'Effectful.Monad.Eff' es ()
39+-- 'Effectful.State.State' 'Integer' ':>' es => 'Effectful.Monad.Eff' es ()
3240-- @
3341class (e :: Effect) :> (es :: [Effect]) where
3442 -- | Get the position of @e@ in @es@.
@@ -51,3 +59,11 @@ instance {-# OVERLAPPING #-} e :> (e : es) where
5159
5260instance e :> es => e :> (x : es) where
5361 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+20−25
@@ -1,4 +1,3 @@
1−{-# LANGUAGE MagicHash #-}
21{-# OPTIONS_HADDOCK not-home #-}
32-- | The environment for the 'Effectful.Internal.Monad.Eff' monad.
43--
@@ -43,7 +42,7 @@ import Control.Monad
4342import Control.Monad.Primitive
4443import Data.IORef
4544import Data.Primitive.SmallArray
46−import GHC.Exts (Any, SmallMutableArray#)
45+import GHC.Exts (Any)
4746import GHC.Stack (HasCallStack)
4847import Unsafe.Coerce
4948import qualified Data.IntMap.Strict as IM
@@ -418,43 +417,43 @@ unsafeTailEnv len (Env fork gref _) = case fork of
418417
419418-- | Extract a specific data type from the environment.
420419getEnv
421− :: forall e adapter es. e :> es
420+ :: forall e es adapter. e :> es
422421 => Env es
423422 -> IO (adapter e)
424423getEnv env = do
425− Location i es <- getLocation (reifyIndex @e @es) env
424+ (i, es) <- getLocation (reifyIndex @e @es) env
426425 fromAny <$> readSmallArray es i
427426
428427-- | Replace the data type in the environment with a new value (in place).
429428putEnv
430− :: forall e adapter es. e :> es
429+ :: forall e es adapter. e :> es
431430 => Env es
432431 -> adapter e
433432 -> IO ()
434433putEnv env e = do
435− Location i es <- getLocation (reifyIndex @e @es) env
434+ (i, es) <- getLocation (reifyIndex @e @es) env
436435 e `seq` writeSmallArray es i (toAny e)
437436
438437-- | Modify the data type in the environment (in place) and return a value.
439438stateEnv
440− :: forall e adapter es a. e :> es
439+ :: forall e es adapter a. e :> es
441440 => Env es
442441 -> (adapter e -> (a, adapter e))
443442 -> IO a
444443stateEnv env f = do
445− Location i es <- getLocation (reifyIndex @e @es) env
444+ (i, es) <- getLocation (reifyIndex @e @es) env
446445 (a, e) <- f . fromAny <$> readSmallArray es i
447446 e `seq` writeSmallArray es i (toAny e)
448447 pure a
449448
450449-- | Modify the data type in the environment (in place).
451450modifyEnv
452− :: forall e adapter es. e :> es
451+ :: forall e es adapter. e :> es
453452 => Env es
454453 -> (adapter e -> adapter e)
455454 -> IO ()
456455modifyEnv env f = do
457− Location i es <- getLocation (reifyIndex @e @es) env
456+ (i, es) <- getLocation (reifyIndex @e @es) env
458457 e <- f . fromAny <$> readSmallArray es i
459458 e `seq` writeSmallArray es i (toAny e)
460459
@@ -470,17 +469,13 @@ emptyEnvRef = do
470469 fs <- newSmallArray 0 undefinedData
471470 newIORef $ EnvRef 0 es fs
472471
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−
477472-- | Determine location of the data type in the environment.
478473getLocation
479474 :: Int
480475 -> Env es
481− -> IO Location
476+ -> IO (Int, SmallMutableArray RealWorld Any)
482477getLocation ixE (Env fork ref _) = do
483− EnvRef n es@(SmallMutableArray es#) _ <- readIORef ref
478+ EnvRef n es _ <- readIORef ref
484479 -- Optimized for the common access pattern:
485480 --
486481 -- - Most application code has no access to forks, in which case we look at
@@ -489,25 +484,25 @@ getLocation ixE (Env fork ref _) = do
489484 -- - Effect handlers will most likely access the newest fork with handler
490485 -- specific effects for reinterpretation.
491486 case fork of
492− NoFork -> pure $ Location (ix n) es
487+ NoFork -> pure (ix n, es)
493488 Forks _ baseIx lref forks -> do
494489 EnvRef ln les _ <- readIORef lref
495490 let i = ix (baseIx + ln)
496491 if i >= baseIx
497− then pure $ Location (i - baseIx) les
498− else go es# i forks
492+ then pure (i - baseIx, les)
493+ else go es i forks
499494 where
500− go :: SmallMutableArray# RealWorld Any
495+ go :: SmallMutableArray RealWorld Any
501496 -> Int
502497 -> Forks
503− -> IO Location
504− go es# i = \case
505− NoFork -> pure $ Location i (SmallMutableArray es#)
498+ -> IO (Int, SmallMutableArray RealWorld Any)
499+ go es i = \case
500+ NoFork -> pure (i, es)
506501 Forks _ baseIx lref forks -> do
507502 EnvRef _ les _ <- readIORef lref
508503 if i >= baseIx
509− then pure $ Location (i - baseIx) les
510− else go es# i forks
504+ then pure (i - baseIx, les)
505+ else go es i forks
511506
512507 ix :: Int -> Int
513508 ix n = n - ixE - 1
effectful-core/src/Effectful/Internal/Monad.hs+25−38
@@ -6,7 +6,7 @@
66-- This module is intended for internal use only, and may change without warning
77-- in subsequent releases.
88module Effectful.Internal.Monad
9− ( -- * The 'Eff' monad
9+ ( -- * Monad
1010 Eff
1111 , runPureEff
1212
@@ -22,11 +22,11 @@ module Effectful.Internal.Monad
2222 , IOE
2323 , runEff
2424
25− -- * Prim
25+ -- * Primitive
2626 , Prim
2727 , runPrim
2828
29− -- * Unlift strategies
29+ -- ** Unlift strategies
3030 , UnliftStrategy(..)
3131 , Persistence(..)
3232 , Limit(..)
@@ -34,24 +34,29 @@ module Effectful.Internal.Monad
3434 , withUnliftStrategy
3535 , withEffToIO
3636
37− -- ** Low-level unlifts
37+ --- *** Low-level helpers
3838 , seqUnliftIO
3939 , concUnliftIO
4040
4141 -- * Effects
4242
43− -- ** Dynamic dispatch
43+ -- ** Handler
4444 , EffectHandler
4545 , LocalEnv(..)
4646 , HandlerA(..)
4747 , runHandler
48+
49+ -- *** Sending operations
4850 , send
4951
50− -- ** Static dispatch
51− , DataA(..)
52+ -- ** Primitive
53+
54+ -- *** Running
5255 , runData
5356 , evalData
5457 , execData
58+
59+ -- *** Modification
5560 , getData
5661 , putData
5762 , stateData
@@ -87,16 +92,17 @@ type role Eff nominal representational
8792-- would have the following type:
8893--
8994-- @
90−-- 'Eff' '['Effectful.Reader.Reader' 'String', 'Effectful.State.Local.State' 'Bool'] 'Integer'
95+-- 'Eff' '['Effectful.Reader.Reader' 'String', 'Effectful.State.State' 'Bool'] 'Integer'
9196-- @
9297--
9398-- Normally, a concrete list of effects is not used to parameterize 'Eff'.
9499-- Instead, the '(:>)' type class is used to express constraints on the list of
95100-- effects without coupling a computation to a concrete list of effects. For
96−-- example, the above example would be expressed with the following type:
101+-- example, the above example would more commonly be expressed with the
102+-- following type:
97103--
98104-- @
99−-- ('Effectful.Reader.Reader' 'String' ':>' es, 'Effectful.State.Local.State' 'Bool' ':>' es) => 'Eff' es 'Integer'
105+-- ('Effectful.Reader.Reader' 'String' ':>' es, 'Effectful.State.State' 'Bool' ':>' es) => 'Eff' es 'Integer'
100106-- @
101107--
102108-- This abstraction allows the computation to be used in functions that may
@@ -110,12 +116,12 @@ newtype Eff (es :: [Effect]) a = Eff (Env es -> IO a)
110116-- For running computations with side effects see 'runEff'.
111117runPureEff :: Eff '[] a -> a
112118runPureEff (Eff m) =
113− -- unsafeDupablePerformIO is safe here since IOE was not on the stack, so no
114− -- IO with side effects was performed (unless someone sneakily introduced side
115− -- effects with unsafeEff, but then all bets are off).
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).
116122 --
117123 -- Moreover, internals don't allocate any resources that require explicit
118− -- cleanup actions to run.
124+ -- cleanup actions to run, so the "Dupable" part should also be just fine.
119125 unsafeDupablePerformIO $ m =<< emptyEnv
120126
121127----------------------------------------
@@ -158,7 +164,7 @@ withUnliftStrategy unlift = localData $ \_ -> DataA (IOE unlift)
158164-- constraint for accurate stack traces in case an insufficiently powerful
159165-- 'UnliftStrategy' is used and the unlifting function fails.
160166--
161−-- /Note:/ the strategy is reset to 'SeqUnlift' inside the continuation.
167+-- /Note:/ the strategy is reset to 'SeqUnlift' for new threads.
162168withEffToIO
163169 :: (HasCallStack, IOE :> es)
164170 => ((forall r. Eff es r -> IO r) -> IO a)
@@ -303,7 +309,7 @@ instance Prim :> es => PrimMonad (Eff es) where
303309 primitive = unsafeEff_ . IO
304310
305311----------------------------------------
306−-- Dynamic dispatch
312+-- Handler
307313
308314type role LocalEnv nominal nominal
309315
@@ -327,11 +333,11 @@ type EffectHandler e es
327333
328334-- | An adapter for dynamically dispatched effects.
329335--
330−-- Represents the effect handler bundled with its environment.
336+-- Represents the effect handler bundled along with its environment.
331337data HandlerA :: Effect -> Type where
332338 HandlerA :: !(Env es) -> !(EffectHandler e es) -> HandlerA e
333339
334−-- | Run a dynamically dispatched effect with the given handler.
340+-- | Run the effect with the given handler.
335341runHandler :: HandlerA e -> Eff (e : es) a -> Eff es a
336342runHandler e m = unsafeEff $ \es0 -> do
337343 size0 <- sizeEnv es0
@@ -351,17 +357,8 @@ send op = unsafeEff $ \es -> do
351357 unEff (handle (LocalEnv es) op) handlerEs
352358
353359----------------------------------------
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
360+-- Helpers
362361
363−-- | Run a statically dispatched effect with the given initial state and return
364−-- the final value along with the final state.
365362runData :: DataA e -> Eff (e : es) a -> Eff es (a, DataA e)
366363runData e0 m = unsafeEff $ \es0 -> do
367364 size0 <- sizeEnv es0
@@ -369,8 +366,6 @@ runData e0 m = unsafeEff $ \es0 -> do
369366 (unsafeTailEnv size0)
370367 (\es -> (,) <$> unEff m es <*> getEnv es)
371368
372−-- | Run a statically dispatched effect with the given initial state and return
373−-- the final value, discarding the final state.
374369evalData :: DataA e -> Eff (e : es) a -> Eff es a
375370evalData e m = unsafeEff $ \es0 -> do
376371 size0 <- sizeEnv es0
@@ -378,8 +373,6 @@ evalData e m = unsafeEff $ \es0 -> do
378373 (unsafeTailEnv size0)
379374 (\es -> unEff m es)
380375
381−-- | Run a statically dispatched effect with the given initial state and return
382−-- the final state, discarding the final value.
383376execData :: DataA e -> Eff (e : es) a -> Eff es (DataA e)
384377execData e0 m = unsafeEff $ \es0 -> do
385378 size0 <- sizeEnv es0
@@ -387,27 +380,21 @@ execData e0 m = unsafeEff $ \es0 -> do
387380 (unsafeTailEnv size0)
388381 (\es -> unEff m es *> getEnv es)
389382
390−-- | Fetch the current state of the effect.
391383getData :: e :> es => Eff es (DataA e)
392384getData = unsafeEff $ \es -> getEnv es
393385
394−-- | Set the current state of the effect to the given value.
395386putData :: e :> es => DataA e -> Eff es ()
396387putData e = unsafeEff $ \es -> putEnv es e
397388
398−-- | Apply the function to the current state of the effect and return a value.
399389stateData :: e :> es => (DataA e -> (a, DataA e)) -> Eff es a
400390stateData f = unsafeEff $ \es -> stateEnv es f
401391
402−-- | Apply the monadic function to the current state of the effect and return a
403−-- value.
404392stateDataM :: e :> es => (DataA e -> Eff es (a, DataA e)) -> Eff es a
405393stateDataM f = unsafeEff $ \es -> E.mask $ \release -> do
406394 (a, e) <- (\e -> release $ unEff (f e) es) =<< getEnv es
407395 putEnv es e
408396 pure a
409397
410−-- | Execute a computation with a temporarily modified state of the effect.
411398localData :: e :> es => (DataA e -> DataA e) -> Eff es a -> Eff es a
412399localData f m = unsafeEff $ \es -> do
413400 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)))
5555-- | Execute an action and append its output to the overall output of the
5656-- 'Writer'.
5757--
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':
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':
6161--
6262-- >>> :{
6363-- runEff . execWriter @String $ do
effectful-core/src/Effectful/Writer/Shared.hs+3−3
@@ -59,9 +59,9 @@ tell w1 = unsafeEff $ \es -> do
5959-- | Execute an action and append its output to the overall output of the
6060-- 'Writer'.
6161--
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':
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':
6565--
6666-- >>> :{
6767-- runEff . execWriter @String $ do
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_mtl
101− , bench "mtl" $ nfAppIO mtl_calculateFileSizesDeep (take n files)
102−#endif
103100#ifdef VERSION_polysemy
104101 , bench "polysemy" $ nfAppIO poly_calculateFileSizesDeep (take n files)
102+#endif
103+#ifdef VERSION_mtl
104+ , bench "mtl" $ nfAppIO mtl_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.2.1
23+tested-with: GHC ==8.8.4 || ==8.10.7 || ==9.0.1 || ==9.2.1
2424
2525bug-reports: https://github.com/arybczak/effectful/issues
2626source-repository head