on master help sign in
Shuffle things around
commit3b965edaa8caa28fe914b03fb16ba2eda480ddd3 authorAndrzej Rybczak <andrzej@rybczak.net> dateSun 26 Dec 2021, 23:36 (5 years ago) parentf749af5
5 files changed+65−59
effectful-core/src/Effectful/Dispatch/Dynamic.hs+13−1 effectful-core/src/Effectful/Dispatch/Static.hs+9−12 effectful-core/src/Effectful/Internal/Effect.hs+1−17 effectful-core/src/Effectful/Internal/Env.hs+4−4 effectful-core/src/Effectful/Internal/Monad.hs+38−25
effectful-core/src/Effectful/Dispatch/Dynamic.hs+13−1
@@ -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
@@ -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+9−12
@@ -2,12 +2,12 @@ module Effectful.Dispatch.Static
22 ( -- * Low level API
33 DataA(..)
44
5− -- ** Running
5+ -- ** Extending the environment
66 , runData
77 , evalData
88 , execData
99
10− -- ** Modification
10+ -- ** Data retrieval and update
1111 , getData
1212 , putData
1313 , stateData
@@ -18,6 +18,13 @@ module Effectful.Dispatch.Static
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,18 +46,8 @@ 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
effectful-core/src/Effectful/Internal/Effect.hs+1−17
@@ -8,10 +8,6 @@
88module Effectful.Internal.Effect
99 ( Effect
1010 , (:>)(..)
11− , DataA(..)
12−
13− -- * Utils
14− , SuffixOf
1511
1612 -- * Re-exports
1713 , Type
@@ -23,10 +19,6 @@ import GHC.TypeLits
2319-- | The kind of effects.
2420type Effect = (Type -> Type) -> Type -> Type
2521
26−-- | An adapter for statically dispatched effects.
27−newtype DataA :: Effect -> Type where
28− DataA :: (forall m r. e m r) -> DataA 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'
3224-- computation over an arbitrary list of effects, so long as @e@ is /somewhere/
@@ -36,7 +28,7 @@ newtype DataA :: Effect -> Type where
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+4−4
@@ -417,7 +417,7 @@ unsafeTailEnv len (Env fork gref _) = case fork of
417417
418418-- | Extract a specific data type from the environment.
419419getEnv
420− :: forall e es adapter. e :> es
420+ :: forall e adapter es. e :> es
421421 => Env es
422422 -> IO (adapter e)
423423getEnv env = do
@@ -426,7 +426,7 @@ getEnv env = do
426426
427427-- | Replace the data type in the environment with a new value (in place).
428428putEnv
429− :: forall e es adapter. e :> es
429+ :: forall e adapter es. e :> es
430430 => Env es
431431 -> adapter e
432432 -> IO ()
@@ -436,7 +436,7 @@ putEnv env e = do
436436
437437-- | Modify the data type in the environment (in place) and return a value.
438438stateEnv
439− :: forall e es adapter a. e :> es
439+ :: forall e adapter es a. e :> es
440440 => Env es
441441 -> (adapter e -> (a, adapter e))
442442 -> IO a
@@ -448,7 +448,7 @@ stateEnv env f = do
448448
449449-- | Modify the data type in the environment (in place).
450450modifyEnv
451− :: forall e es adapter. e :> es
451+ :: forall e adapter es. e :> es
452452 => Env es
453453 -> (adapter e -> adapter e)
454454 -> IO ()
effectful-core/src/Effectful/Internal/Monad.hs+38−25
@@ -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,29 +34,24 @@ module Effectful.Internal.Monad
3434 , withUnliftStrategy
3535 , withEffToIO
3636
37− --- *** Low-level helpers
37+ -- ** Low-level unlifts
3838 , seqUnliftIO
3939 , concUnliftIO
4040
4141 -- * Effects
4242
43− -- ** Handler
43+ -- ** Dynamic dispatch
4444 , EffectHandler
4545 , LocalEnv(..)
4646 , HandlerA(..)
4747 , runHandler
48−
49− -- *** Sending operations
5048 , send
5149
52− -- ** Primitive
53−
54− -- *** Running
50+ -- ** Static dispatch
51+ , DataA(..)
5552 , runData
5653 , evalData
5754 , execData
58−
59− -- *** Modification
6055 , getData
6156 , putData
6257 , stateData
@@ -92,17 +87,16 @@ type role Eff nominal representational
9287-- would have the following type:
9388--
9489-- @
95−-- 'Eff' '['Effectful.Reader.Reader' 'String', 'Effectful.State.State' 'Bool'] 'Integer'
90+-- 'Eff' '['Effectful.Reader.Reader' 'String', 'Effectful.State.Local.State' 'Bool'] 'Integer'
9691-- @
9792--
9893-- Normally, a concrete list of effects is not used to parameterize 'Eff'.
9994-- Instead, the '(:>)' type class is used to express constraints on the list of
10095-- effects without coupling a computation to a concrete list of effects. For
101−-- example, the above example would more commonly be expressed with the
102−-- following type:
96+-- example, the above example would be expressed with the following type:
10397--
10498-- @
105−-- ('Effectful.Reader.Reader' 'String' ':>' es, 'Effectful.State.State' 'Bool' ':>' es) => 'Eff' es 'Integer'
99+-- ('Effectful.Reader.Reader' 'String' ':>' es, 'Effectful.State.Local.State' 'Bool' ':>' es) => 'Eff' es 'Integer'
106100-- @
107101--
108102-- This abstraction allows the computation to be used in functions that may
@@ -116,12 +110,12 @@ newtype Eff (es :: [Effect]) a = Eff (Env es -> IO a)
116110-- For running computations with side effects see 'runEff'.
117111runPureEff :: Eff '[] a -> a
118112runPureEff (Eff m) =
119− -- unsafePerformIO is safe here since IOE was not on the stack, so no IO with
120− -- side effects was performed (unless someone sneakily introduced side effects
121− -- with unsafeEff, but then all bets are off).
113+ -- unsafeDupablePerformIO is safe here since IOE was not on the stack, so no
114+ -- IO with side effects was performed (unless someone sneakily introduced side
115+ -- effects with unsafeEff, but then all bets are off).
122116 --
123117 -- Moreover, internals don't allocate any resources that require explicit
124− -- cleanup actions to run, so the "Dupable" part should also be just fine.
118+ -- cleanup actions to run.
125119 unsafeDupablePerformIO $ m =<< emptyEnv
126120
127121----------------------------------------
@@ -164,7 +158,7 @@ withUnliftStrategy unlift = localData $ \_ -> DataA (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)
@@ -309,7 +303,7 @@ instance Prim :> es => PrimMonad (Eff es) where
309303 primitive = unsafeEff_ . IO
310304
311305----------------------------------------
312−-- Handler
306+-- Dynamic dispatch
313307
314308type role LocalEnv nominal nominal
315309
@@ -333,11 +327,11 @@ type EffectHandler e es
333327
334328-- | An adapter for dynamically dispatched effects.
335329--
336−-- Represents the effect handler bundled along with its environment.
330+-- Represents the effect handler bundled with its environment.
337331data HandlerA :: Effect -> Type where
338332 HandlerA :: !(Env es) -> !(EffectHandler e es) -> HandlerA e
339333
340−-- | Run the effect with the given handler.
334+-- | Run a dynamically dispatched effect with the given handler.
341335runHandler :: HandlerA e -> Eff (e : es) a -> Eff es a
342336runHandler e m = unsafeEff $ \es0 -> do
343337 size0 <- sizeEnv es0
@@ -357,8 +351,17 @@ send op = unsafeEff $ \es -> do
357351 unEff (handle (LocalEnv es) op) handlerEs
358352
359353----------------------------------------
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
361362
363+-- | Run a statically dispatched effect with the given initial state and return
364+-- the final value along with the final state.
362365runData :: DataA e -> Eff (e : es) a -> Eff es (a, DataA e)
363366runData e0 m = unsafeEff $ \es0 -> do
364367 size0 <- sizeEnv es0
@@ -366,6 +369,8 @@ runData e0 m = unsafeEff $ \es0 -> do
366369 (unsafeTailEnv size0)
367370 (\es -> (,) <$> unEff m es <*> getEnv es)
368371
372+-- | Run a statically dispatched effect with the given initial state and return
373+-- the final value, discarding the final state.
369374evalData :: DataA e -> Eff (e : es) a -> Eff es a
370375evalData e m = unsafeEff $ \es0 -> do
371376 size0 <- sizeEnv es0
@@ -373,6 +378,8 @@ evalData e m = unsafeEff $ \es0 -> do
373378 (unsafeTailEnv size0)
374379 (\es -> unEff m es)
375380
381+-- | Run a statically dispatched effect with the given initial state and return
382+-- the final state, discarding the final value.
376383execData :: DataA e -> Eff (e : es) a -> Eff es (DataA e)
377384execData e0 m = unsafeEff $ \es0 -> do
378385 size0 <- sizeEnv es0
@@ -380,21 +387,27 @@ execData e0 m = unsafeEff $ \es0 -> do
380387 (unsafeTailEnv size0)
381388 (\es -> unEff m es *> getEnv es)
382389
390+-- | Fetch the current state of the effect.
383391getData :: e :> es => Eff es (DataA e)
384392getData = unsafeEff $ \es -> getEnv es
385393
394+-- | Set the current state of the effect to the given value.
386395putData :: e :> es => DataA e -> Eff es ()
387396putData e = unsafeEff $ \es -> putEnv es e
388397
398+-- | Apply the function to the current state of the effect and return a value.
389399stateData :: e :> es => (DataA e -> (a, DataA e)) -> Eff es a
390400stateData f = unsafeEff $ \es -> stateEnv es f
391401
402+-- | Apply the monadic function to the current state of the effect and return a
403+-- value.
392404stateDataM :: e :> es => (DataA e -> Eff es (a, DataA e)) -> Eff es a
393405stateDataM 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
410+-- | Execute a computation with a temporarily modified state of the effect.
398411localData :: e :> es => (DataA e -> DataA e) -> Eff es a -> Eff es a
399412localData f m = unsafeEff $ \es -> do
400413 E.bracket (stateEnv es $ \e -> (e, f e))