Skip to content

Commit

Permalink
Add utility functions that take the effect handler as the last parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
arybczak committed Aug 8, 2024
1 parent aff86ac commit cb97e86
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 2 deletions.
2 changes: 2 additions & 0 deletions effectful-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# effectful-core-2.4.0.0 (????-??-??)
* Add utility functions for handling effects that take the effect handler as the
last parameter to `Effectful.Dispatch.Dynamic`.
* Add utility functions for handling first order effects to
`Effectful.Dispatch.Dynamic`.

Expand Down
108 changes: 106 additions & 2 deletions effectful-core/src/Effectful/Dispatch/Dynamic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ module Effectful.Dispatch.Dynamic
-- * Handling effects
, EffectHandler
, interpret
, interpretWith
, reinterpret
, reinterpretWith
, interpose
, interposeWith
, impose
, imposeWith

-- ** Handling local 'Eff' computations
, LocalEnv
Expand Down Expand Up @@ -56,9 +60,13 @@ module Effectful.Dispatch.Dynamic
-- ** Utils for first order effects
, EffectHandler_
, interpret_
, interpretWith_
, reinterpret_
, reinterpretWith_
, interpose_
, interposeWith_
, impose_
, imposeWith_

-- * Re-exports
, HasCallStack
Expand Down Expand Up @@ -428,6 +436,17 @@ interpret handler m = unsafeEff $ \es -> do
where
mkHandler es = Handler es (let ?callStack = thawCallStack ?callStack in handler)

-- | 'interpret' with the effect handler as the last argument.
--
-- @since 2.4.0.0
interpretWith
:: DispatchOf e ~ Dynamic
=> Eff (e : es) a
-> EffectHandler e es
-- ^ The effect handler.
-> Eff es a
interpretWith m handler = interpret handler m

-- | Interpret an effect using other, private effects.
--
-- @'interpret' ≡ 'reinterpret' 'id'@
Expand All @@ -445,6 +464,19 @@ reinterpret runHandlerEs handler m = unsafeEff $ \es -> do
where
mkHandler es = Handler es (let ?callStack = thawCallStack ?callStack in handler)

-- | 'reinterpret' with the effect handler as the last argument.
--
-- @since 2.4.0.0
reinterpretWith
:: DispatchOf e ~ Dynamic
=> (Eff handlerEs a -> Eff es b)
-- ^ Introduction of effects encapsulated within the handler.
-> Eff (e : es) a
-> EffectHandler e handlerEs
-- ^ The effect handler.
-> Eff es b
reinterpretWith runHandlerEs m handler = reinterpret runHandlerEs handler m

-- | Replace the handler of an existing effect with a new one.
--
-- /Note:/ this function allows for augmenting handlers with a new functionality
Expand Down Expand Up @@ -499,6 +531,17 @@ interpose handler m = unsafeEff $ \es -> do
where
mkHandler es = Handler es (let ?callStack = thawCallStack ?callStack in handler)

-- | 'interpose' with the effect handler as the last argument.
--
-- @since 2.4.0.0
interposeWith
:: (DispatchOf e ~ Dynamic, e :> es)
=> Eff es a
-> EffectHandler e es
-- ^ The effect handler.
-> Eff es a
interposeWith m handler = interpose handler m

-- | Replace the handler of an existing effect with a new one that uses other,
-- private effects.
--
Expand Down Expand Up @@ -533,6 +576,19 @@ impose runHandlerEs handler m = unsafeEff $ \es -> do
where
mkHandler es = Handler es (let ?callStack = thawCallStack ?callStack in handler)

-- | 'impose' with the effect handler as the last argument.
--
-- @since 2.4.0.0
imposeWith
:: (DispatchOf e ~ Dynamic, e :> es)
=> (Eff handlerEs a -> Eff es b)
-- ^ Introduction of effects encapsulated within the handler.
-> Eff es a
-> EffectHandler e handlerEs
-- ^ The effect handler.
-> Eff es b
imposeWith runHandlerEs m handler = impose runHandlerEs handler m

----------------------------------------
-- First order effects

Expand All @@ -556,6 +612,17 @@ interpret_
-> Eff es a
interpret_ handler = interpret (const handler)

-- | 'interpretWith' for first order effects.
--
-- @since 2.4.0.0
interpretWith_
:: DispatchOf e ~ Dynamic
=> Eff (e : es) a
-> EffectHandler_ e es
-- ^ The effect handler.
-> Eff es a
interpretWith_ m handler = interpretWith m (const handler)

-- | 'reinterpret' for first order effects.
--
-- @since 2.4.0.0
Expand All @@ -569,22 +636,46 @@ reinterpret_
-> Eff es b
reinterpret_ runHandlerEs handler = reinterpret runHandlerEs (const handler)

-- | 'reinterpretWith' for first order effects.
--
-- @since 2.4.0.0
reinterpretWith_
:: DispatchOf e ~ Dynamic
=> (Eff handlerEs a -> Eff es b)
-- ^ Introduction of effects encapsulated within the handler.
-> Eff (e : es) a
-> EffectHandler_ e handlerEs
-- ^ The effect handler.
-> Eff es b
reinterpretWith_ runHandlerEs m handler = reinterpretWith runHandlerEs m (const handler)

-- | 'interpose' for first order effects.
--
-- @since 2.4.0.0
interpose_
:: forall e es a. (DispatchOf e ~ Dynamic, e :> es)
:: (DispatchOf e ~ Dynamic, e :> es)
=> EffectHandler_ e es
-- ^ The effect handler.
-> Eff es a
-> Eff es a
interpose_ handler = interpose (const handler)

-- | 'interposeWith' for first order effects.
--
-- @since 2.4.0.0
interposeWith_
:: (DispatchOf e ~ Dynamic, e :> es)
=> Eff es a
-> EffectHandler_ e es
-- ^ The effect handler.
-> Eff es a
interposeWith_ m handler = interposeWith m (const handler)

-- | 'impose' for first order effects.
--
-- @since 2.4.0.0
impose_
:: forall e es handlerEs a b. (DispatchOf e ~ Dynamic, e :> es)
:: (DispatchOf e ~ Dynamic, e :> es)
=> (Eff handlerEs a -> Eff es b)
-- ^ Introduction of effects encapsulated within the handler.
-> EffectHandler_ e handlerEs
Expand All @@ -593,6 +684,19 @@ impose_
-> Eff es b
impose_ runHandlerEs handler = impose runHandlerEs (const handler)

-- | 'imposeWith' for first order effects.
--
-- @since 2.4.0.0
imposeWith_
:: (DispatchOf e ~ Dynamic, e :> es)
=> (Eff handlerEs a -> Eff es b)
-- ^ Introduction of effects encapsulated within the handler.
-> Eff es a
-> EffectHandler_ e handlerEs
-- ^ The effect handler.
-> Eff es b
imposeWith_ runHandlerEs m handler = imposeWith runHandlerEs m (const handler)

----------------------------------------
-- Unlifts

Expand Down
2 changes: 2 additions & 0 deletions effectful/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# effectful-2.4.0.0 (????-??-??)
* Add utility functions for handling effects that take the effect handler as the
last parameter to `Effectful.Dispatch.Dynamic`.
* Add utility functions for handling first order effects to
`Effectful.Dispatch.Dynamic`.

Expand Down

0 comments on commit cb97e86

Please sign in to comment.