From cb97e86fcce18bc459a4d13c3e0bab6a788a804e Mon Sep 17 00:00:00 2001 From: Andrzej Rybczak Date: Thu, 8 Aug 2024 11:54:09 +0200 Subject: [PATCH] Add utility functions that take the effect handler as the last parameter --- effectful-core/CHANGELOG.md | 2 + .../src/Effectful/Dispatch/Dynamic.hs | 108 +++++++++++++++++- effectful/CHANGELOG.md | 2 + 3 files changed, 110 insertions(+), 2 deletions(-) diff --git a/effectful-core/CHANGELOG.md b/effectful-core/CHANGELOG.md index 0ab8843..8aae557 100644 --- a/effectful-core/CHANGELOG.md +++ b/effectful-core/CHANGELOG.md @@ -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`. diff --git a/effectful-core/src/Effectful/Dispatch/Dynamic.hs b/effectful-core/src/Effectful/Dispatch/Dynamic.hs index 753f5c8..76a5168 100644 --- a/effectful-core/src/Effectful/Dispatch/Dynamic.hs +++ b/effectful-core/src/Effectful/Dispatch/Dynamic.hs @@ -23,9 +23,13 @@ module Effectful.Dispatch.Dynamic -- * Handling effects , EffectHandler , interpret + , interpretWith , reinterpret + , reinterpretWith , interpose + , interposeWith , impose + , imposeWith -- ** Handling local 'Eff' computations , LocalEnv @@ -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 @@ -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'@ @@ -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 @@ -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. -- @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/effectful/CHANGELOG.md b/effectful/CHANGELOG.md index 0100331..4321cdc 100644 --- a/effectful/CHANGELOG.md +++ b/effectful/CHANGELOG.md @@ -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`.