module Effectful.Writer.Dynamic
(
Writer(..)
, runWriterLocal
, execWriterLocal
, runWriterShared
, execWriterShared
, tell
, listen
, listens
) where
import Effectful
import Effectful.Dispatch.Dynamic
import qualified Effectful.Writer.Static.Local as L
import qualified Effectful.Writer.Static.Shared as S
data Writer w :: Effect where
Tell :: w -> Writer w m ()
Listen :: m a -> Writer w m (a, w)
type instance DispatchOf (Writer w) = Dynamic
runWriterLocal :: Monoid w => Eff (Writer w : es) a -> Eff es (a, w)
runWriterLocal :: forall w (es :: [(Type -> Type) -> Type -> Type]) a.
Monoid w =>
Eff (Writer w : es) a -> Eff es (a, w)
runWriterLocal = forall (e :: (Type -> Type) -> Type -> Type)
(handlerEs :: [(Type -> Type) -> Type -> Type]) a
(es :: [(Type -> Type) -> Type -> Type]) b.
(DispatchOf e ~ 'Dynamic) =>
(Eff handlerEs a -> Eff es b)
-> EffectHandler e handlerEs -> Eff (e : es) a -> Eff es b
reinterpret forall w (es :: [(Type -> Type) -> Type -> Type]) a.
Monoid w =>
Eff (Writer w : es) a -> Eff es (a, w)
L.runWriter forall w (es :: [(Type -> Type) -> Type -> Type])
(localEs :: [(Type -> Type) -> Type -> Type]) a.
(Writer w :> es, Monoid w) =>
LocalEnv localEs es -> Writer w (Eff localEs) a -> Eff es a
localWriter
execWriterLocal :: Monoid w => Eff (Writer w : es) a -> Eff es w
execWriterLocal :: forall w (es :: [(Type -> Type) -> Type -> Type]) a.
Monoid w =>
Eff (Writer w : es) a -> Eff es w
execWriterLocal = forall (e :: (Type -> Type) -> Type -> Type)
(handlerEs :: [(Type -> Type) -> Type -> Type]) a
(es :: [(Type -> Type) -> Type -> Type]) b.
(DispatchOf e ~ 'Dynamic) =>
(Eff handlerEs a -> Eff es b)
-> EffectHandler e handlerEs -> Eff (e : es) a -> Eff es b
reinterpret forall w (es :: [(Type -> Type) -> Type -> Type]) a.
Monoid w =>
Eff (Writer w : es) a -> Eff es w
L.execWriter forall w (es :: [(Type -> Type) -> Type -> Type])
(localEs :: [(Type -> Type) -> Type -> Type]) a.
(Writer w :> es, Monoid w) =>
LocalEnv localEs es -> Writer w (Eff localEs) a -> Eff es a
localWriter
localWriter
:: (L.Writer w :> es, Monoid w)
=> LocalEnv localEs es
-> Writer w (Eff localEs) a
-> Eff es a
localWriter :: forall w (es :: [(Type -> Type) -> Type -> Type])
(localEs :: [(Type -> Type) -> Type -> Type]) a.
(Writer w :> es, Monoid w) =>
LocalEnv localEs es -> Writer w (Eff localEs) a -> Eff es a
localWriter LocalEnv localEs es
env = \case
Tell w
w -> forall w (es :: [(Type -> Type) -> Type -> Type]).
(Writer w :> es, Monoid w) =>
w -> Eff es ()
L.tell w
w
Listen Eff localEs a
m -> forall (es :: [(Type -> Type) -> Type -> Type])
(handlerEs :: [(Type -> Type) -> Type -> Type])
(localEs :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, SharedSuffix es handlerEs) =>
LocalEnv localEs handlerEs
-> ((forall r. Eff localEs r -> Eff es r) -> Eff es a) -> Eff es a
localSeqUnlift LocalEnv localEs es
env forall a b. (a -> b) -> a -> b
$ \forall r. Eff localEs r -> Eff es r
unlift -> forall w (es :: [(Type -> Type) -> Type -> Type]) a.
(Writer w :> es, Monoid w) =>
Eff es a -> Eff es (a, w)
L.listen (forall r. Eff localEs r -> Eff es r
unlift Eff localEs a
m)
runWriterShared :: Monoid w => Eff (Writer w : es) a -> Eff es (a, w)
runWriterShared :: forall w (es :: [(Type -> Type) -> Type -> Type]) a.
Monoid w =>
Eff (Writer w : es) a -> Eff es (a, w)
runWriterShared = forall (e :: (Type -> Type) -> Type -> Type)
(handlerEs :: [(Type -> Type) -> Type -> Type]) a
(es :: [(Type -> Type) -> Type -> Type]) b.
(DispatchOf e ~ 'Dynamic) =>
(Eff handlerEs a -> Eff es b)
-> EffectHandler e handlerEs -> Eff (e : es) a -> Eff es b
reinterpret forall w (es :: [(Type -> Type) -> Type -> Type]) a.
Monoid w =>
Eff (Writer w : es) a -> Eff es (a, w)
S.runWriter forall w (es :: [(Type -> Type) -> Type -> Type])
(localEs :: [(Type -> Type) -> Type -> Type]) a.
(Writer w :> es, Monoid w) =>
LocalEnv localEs es -> Writer w (Eff localEs) a -> Eff es a
sharedWriter
execWriterShared :: Monoid w => Eff (Writer w : es) a -> Eff es w
execWriterShared :: forall w (es :: [(Type -> Type) -> Type -> Type]) a.
Monoid w =>
Eff (Writer w : es) a -> Eff es w
execWriterShared = forall (e :: (Type -> Type) -> Type -> Type)
(handlerEs :: [(Type -> Type) -> Type -> Type]) a
(es :: [(Type -> Type) -> Type -> Type]) b.
(DispatchOf e ~ 'Dynamic) =>
(Eff handlerEs a -> Eff es b)
-> EffectHandler e handlerEs -> Eff (e : es) a -> Eff es b
reinterpret forall w (es :: [(Type -> Type) -> Type -> Type]) a.
Monoid w =>
Eff (Writer w : es) a -> Eff es w
S.execWriter forall w (es :: [(Type -> Type) -> Type -> Type])
(localEs :: [(Type -> Type) -> Type -> Type]) a.
(Writer w :> es, Monoid w) =>
LocalEnv localEs es -> Writer w (Eff localEs) a -> Eff es a
sharedWriter
sharedWriter
:: (S.Writer w :> es, Monoid w)
=> LocalEnv localEs es
-> Writer w (Eff localEs) a
-> Eff es a
sharedWriter :: forall w (es :: [(Type -> Type) -> Type -> Type])
(localEs :: [(Type -> Type) -> Type -> Type]) a.
(Writer w :> es, Monoid w) =>
LocalEnv localEs es -> Writer w (Eff localEs) a -> Eff es a
sharedWriter LocalEnv localEs es
env = \case
Tell w
w -> forall w (es :: [(Type -> Type) -> Type -> Type]).
(Writer w :> es, Monoid w) =>
w -> Eff es ()
S.tell w
w
Listen Eff localEs a
m -> forall (es :: [(Type -> Type) -> Type -> Type])
(handlerEs :: [(Type -> Type) -> Type -> Type])
(localEs :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, SharedSuffix es handlerEs) =>
LocalEnv localEs handlerEs
-> ((forall r. Eff localEs r -> Eff es r) -> Eff es a) -> Eff es a
localSeqUnlift LocalEnv localEs es
env forall a b. (a -> b) -> a -> b
$ \forall r. Eff localEs r -> Eff es r
unlift -> forall w (es :: [(Type -> Type) -> Type -> Type]) a.
(Writer w :> es, Monoid w) =>
Eff es a -> Eff es (a, w)
S.listen (forall r. Eff localEs r -> Eff es r
unlift Eff localEs a
m)
tell
:: (HasCallStack, Writer w :> es)
=> w
-> Eff es ()
tell :: forall w (es :: [(Type -> Type) -> Type -> Type]).
(HasCallStack, Writer w :> es) =>
w -> Eff es ()
tell = forall (e :: (Type -> Type) -> Type -> Type)
(es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, DispatchOf e ~ 'Dynamic, e :> es) =>
e (Eff es) a -> Eff es a
send forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: Type -> Type). w -> Writer w m ()
Tell
listen
:: (HasCallStack, Writer w :> es)
=> Eff es a
-> Eff es (a, w)
listen :: forall w (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, Writer w :> es) =>
Eff es a -> Eff es (a, w)
listen = forall (e :: (Type -> Type) -> Type -> Type)
(es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, DispatchOf e ~ 'Dynamic, e :> es) =>
e (Eff es) a -> Eff es a
send forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: Type -> Type) a w. m a -> Writer w m (a, w)
Listen
listens
:: (HasCallStack, Writer w :> es)
=> (w -> b)
-> Eff es a
-> Eff es (a, b)
listens :: forall w (es :: [(Type -> Type) -> Type -> Type]) b a.
(HasCallStack, Writer w :> es) =>
(w -> b) -> Eff es a -> Eff es (a, b)
listens w -> b
f Eff es a
m = do
(a
a, w
w) <- forall w (es :: [(Type -> Type) -> Type -> Type]) a.
(HasCallStack, Writer w :> es) =>
Eff es a -> Eff es (a, w)
listen Eff es a
m
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (a
a, w -> b
f w
w)