@@ -3,6 +3,7 @@ module Effectful.Handler
33 send
44
55 -- * Handling effects
6+ , EffectHandler
67 , interpret
78 , reinterpret
89
@@ -43,13 +44,18 @@ import Effectful.Internal.Monad
4344-- handler.
4445newtype LocalEnv es = LocalEnv (Env es)
4546
46−data Handler (e :: Effect) = forall handlerEs. Handler
47− { _env :: Env handlerEs
48− , _handle :: forall a localEs. HasCallStack
49− => LocalEnv localEs
50− -> e (Eff localEs) a
51− -> Eff handlerEs a
52− }
47+-- | Type signature of the effect handler.
48+type EffectHandler e es
49+ = forall a localEs. HasCallStack
50+ => LocalEnv localEs
51+ -- ^ The environment for handling local 'Eff' operations with functions from
52+ -- the 'localUnlift' family when given a higher order effect.
53+ -> e (Eff localEs) a
54+ -- ^ The effect in the local environment.
55+ -> Eff es a
56+
57+data Handler :: Effect -> Type where
58+ Handler :: Env es -> EffectHandler e es -> Handler e
5359
5460runHandler :: Env es -> Handler e -> Eff (e : es) a -> IO a
5561runHandler es0 e m = do
@@ -59,9 +65,9 @@ runHandler es0 e m = do
5965 (\es -> unEff m es)
6066 where
6167 relinker :: Relinker Handler e
62− relinker = Relinker $ \relink Handler{..} -> do
63− env <- relink _env
64− pure Handler { _env = env, .. }
68+ relinker = Relinker $ \relink (Handler env handle) -> do
69+ newEnv <- relink env
70+ pure $ Handler newEnv handle
6571
6672----------------------------------------
6773-- Sending operations
@@ -69,18 +75,15 @@ runHandler es0 e m = do
6975-- | Send an operation of a given effect to its handler for execution.
7076send :: (HasCallStack, e :> es) => e (Eff es) a -> Eff es a
7177send op = unsafeEff $ \es -> do
72− Handler{..} <- getEnv es
73− unEff (_handle (LocalEnv es) op) _env
78+ Handler env handle <- getEnv es
79+ unEff (handle (LocalEnv es) op) env
7480
7581----------------------------------------
7682-- Interpretation
7783
7884-- | Interpret an effect.
79−--
80−-- /Note:/ 'LocalEnv' is for handling local 'Eff' operations using a function
81−-- from the 'localUnlift' family.
8285interpret
83− :: (forall r localEs. HasCallStack => LocalEnv localEs -> e (Eff localEs) r -> Eff es r)
86+ :: EffectHandler e es
8487 -- ^ The effect handler.
8588 -> Eff (e : es) a
8689 -> Eff es a
@@ -92,13 +95,10 @@ interpret handler m = unsafeEff $ \es -> do
9295-- Reinterpretation
9396
9497-- | Interpret an effect using other effects.
95−--
96−-- /Note:/ 'LocalEnv' is for handling local 'Eff' operations using a function
97−-- from the 'localUnlift' family.
9898reinterpret
9999 :: (Eff handlerEs a -> Eff es b)
100100 -- ^ Introduction of effects encapsulated within the handler.
101− -> (forall r localEs. HasCallStack => LocalEnv localEs -> e (Eff localEs) r -> Eff handlerEs r)
101+ -> EffectHandler e handlerEs
102102 -- ^ The effect handler.
103103 -> Eff (e : es) a
104104 -> Eff es b