{-# LANGUAGE AllowAmbiguousTypes #-}
module Data.Effect.Writer where
data Tell w a where
Tell :: w -> Tell w ()
data WriterH w f a where
Listen :: f a -> WriterH w f (w, a)
Censor :: (w -> w) -> f a -> WriterH w f a
makeEffect [''Tell] [''WriterH]
pass :: (Tell w <: m, WriterH w <<: m, Monad m) => m (w -> w, a) -> m a
pass :: forall w (m :: * -> *) a.
(Tell w <: m, WriterH w <<: m, Monad m) =>
m (w -> w, a) -> m a
pass m (w -> w, a)
m = do
(w
w, (w -> w
f, a
a)) <- m (w -> w, a) -> m (w, (w -> w, a))
forall a w (f :: * -> *). SendHOE (WriterH w) f => f a -> f (w, a)
listen m (w -> w, a)
m
w -> m ()
forall w (f :: * -> *). SendFOE (Tell w) f => w -> f ()
tell (w -> m ()) -> w -> m ()
forall a b. (a -> b) -> a -> b
$ w -> w
f w
w
a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a