{-# LANGUAGE DeriveFunctor, ExistentialQuantification, FlexibleContexts, FlexibleInstances, GeneralizedNewtypeDeriving, MultiParamTypeClasses, ScopedTypeVariables, StandaloneDeriving, TypeOperators, UndecidableInstances #-}
module Control.Effect.Writer
(
Writer(..)
, tell
, listen
, listens
, censor
, runWriter
, execWriter
, WriterC(..)
, Carrier
, Member
, run
) where
import Control.Applicative (Alternative(..))
import Control.Effect.Carrier
import Control.Effect.State
import Control.Monad (MonadPlus(..))
import qualified Control.Monad.Fail as Fail
import Control.Monad.Fix
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
data Writer w m k
= Tell w (m k)
| forall a . Listen (m a) (w -> a -> m k)
| forall a . Censor (w -> w) (m a) (a -> m k)
deriving instance Functor m => Functor (Writer w m)
instance HFunctor (Writer w) where
hmap :: (forall x. m x -> n x) -> Writer w m a -> Writer w n a
hmap f :: forall x. m x -> n x
f (Tell w :: w
w k :: m a
k) = w -> n a -> Writer w n a
forall w (m :: * -> *) k. w -> m k -> Writer w m k
Tell w
w (m a -> n a
forall x. m x -> n x
f m a
k)
hmap f :: forall x. m x -> n x
f (Listen m :: m a
m k :: w -> a -> m a
k) = n a -> (w -> a -> n a) -> Writer w n a
forall w (m :: * -> *) k a. m a -> (w -> a -> m k) -> Writer w m k
Listen (m a -> n a
forall x. m x -> n x
f m a
m) ((m a -> n a
forall x. m x -> n x
f (m a -> n a) -> (a -> m a) -> a -> n a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((a -> m a) -> a -> n a) -> (w -> a -> m a) -> w -> a -> n a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. w -> a -> m a
k)
hmap f :: forall x. m x -> n x
f (Censor g :: w -> w
g m :: m a
m k :: a -> m a
k) = (w -> w) -> n a -> (a -> n a) -> Writer w n a
forall w (m :: * -> *) k a.
(w -> w) -> m a -> (a -> m k) -> Writer w m k
Censor w -> w
g (m a -> n a
forall x. m x -> n x
f m a
m) (m a -> n a
forall x. m x -> n x
f (m a -> n a) -> (a -> m a) -> a -> n a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m a
k)
{-# INLINE hmap #-}
instance Effect (Writer w) where
handle :: f ()
-> (forall x. f (m x) -> n (f x))
-> Writer w m a
-> Writer w n (f a)
handle state :: f ()
state handler :: forall x. f (m x) -> n (f x)
handler (Tell w :: w
w k :: m a
k) = w -> n (f a) -> Writer w n (f a)
forall w (m :: * -> *) k. w -> m k -> Writer w m k
Tell w
w (f (m a) -> n (f a)
forall x. f (m x) -> n (f x)
handler (m a
k m a -> f () -> f (m a)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f ()
state))
handle state :: f ()
state handler :: forall x. f (m x) -> n (f x)
handler (Listen m :: m a
m k :: w -> a -> m a
k) = n (f a) -> (w -> f a -> n (f a)) -> Writer w n (f a)
forall w (m :: * -> *) k a. m a -> (w -> a -> m k) -> Writer w m k
Listen (f (m a) -> n (f a)
forall x. f (m x) -> n (f x)
handler (m a
m m a -> f () -> f (m a)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f ()
state)) ((f (m a) -> n (f a)) -> (f a -> f (m a)) -> f a -> n (f a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap f (m a) -> n (f a)
forall x. f (m x) -> n (f x)
handler ((f a -> f (m a)) -> f a -> n (f a))
-> (w -> f a -> f (m a)) -> w -> f a -> n (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> m a) -> f a -> f (m a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> m a) -> f a -> f (m a))
-> (w -> a -> m a) -> w -> f a -> f (m a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. w -> a -> m a
k)
handle state :: f ()
state handler :: forall x. f (m x) -> n (f x)
handler (Censor f :: w -> w
f m :: m a
m k :: a -> m a
k) = (w -> w) -> n (f a) -> (f a -> n (f a)) -> Writer w n (f a)
forall w (m :: * -> *) k a.
(w -> w) -> m a -> (a -> m k) -> Writer w m k
Censor w -> w
f (f (m a) -> n (f a)
forall x. f (m x) -> n (f x)
handler (m a
m m a -> f () -> f (m a)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ f ()
state)) (f (m a) -> n (f a)
forall x. f (m x) -> n (f x)
handler (f (m a) -> n (f a)) -> (f a -> f (m a)) -> f a -> n (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> m a) -> f a -> f (m a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> m a
k)
{-# INLINE handle #-}
tell :: (Member (Writer w) sig, Carrier sig m) => w -> m ()
tell :: w -> m ()
tell w :: w
w = Writer w m () -> m ()
forall (effect :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member effect sig, Carrier sig m) =>
effect m a -> m a
send (w -> m () -> Writer w m ()
forall w (m :: * -> *) k. w -> m k -> Writer w m k
Tell w
w (() -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()))
{-# INLINE tell #-}
listen :: (Member (Writer w) sig, Carrier sig m) => m a -> m (w, a)
listen :: m a -> m (w, a)
listen m :: m a
m = Writer w m (w, a) -> m (w, a)
forall (effect :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member effect sig, Carrier sig m) =>
effect m a -> m a
send (m a -> (w -> a -> m (w, a)) -> Writer w m (w, a)
forall w (m :: * -> *) k a. m a -> (w -> a -> m k) -> Writer w m k
Listen m a
m (((w, a) -> m (w, a)) -> w -> a -> m (w, a)
forall a b c. ((a, b) -> c) -> a -> b -> c
curry (w, a) -> m (w, a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure))
{-# INLINE listen #-}
listens :: (Member (Writer w) sig, Carrier sig m) => (w -> b) -> m a -> m (b, a)
listens :: (w -> b) -> m a -> m (b, a)
listens f :: w -> b
f m :: m a
m = Writer w m (b, a) -> m (b, a)
forall (effect :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member effect sig, Carrier sig m) =>
effect m a -> m a
send (m a -> (w -> a -> m (b, a)) -> Writer w m (b, a)
forall w (m :: * -> *) k a. m a -> (w -> a -> m k) -> Writer w m k
Listen m a
m (((b, a) -> m (b, a)) -> b -> a -> m (b, a)
forall a b c. ((a, b) -> c) -> a -> b -> c
curry (b, a) -> m (b, a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (b -> a -> m (b, a)) -> (w -> b) -> w -> a -> m (b, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. w -> b
f))
{-# INLINE listens #-}
censor :: (Member (Writer w) sig, Carrier sig m) => (w -> w) -> m a -> m a
censor :: (w -> w) -> m a -> m a
censor f :: w -> w
f m :: m a
m = Writer w m a -> m a
forall (effect :: (* -> *) -> * -> *) (sig :: (* -> *) -> * -> *)
(m :: * -> *) a.
(Member effect sig, Carrier sig m) =>
effect m a -> m a
send ((w -> w) -> m a -> (a -> m a) -> Writer w m a
forall w (m :: * -> *) k a.
(w -> w) -> m a -> (a -> m k) -> Writer w m k
Censor w -> w
f m a
m a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure)
{-# INLINE censor #-}
runWriter :: Monoid w => WriterC w m a -> m (w, a)
runWriter :: WriterC w m a -> m (w, a)
runWriter = w -> StateC w m a -> m (w, a)
forall s (m :: * -> *) a. s -> StateC s m a -> m (s, a)
runState w
forall a. Monoid a => a
mempty (StateC w m a -> m (w, a))
-> (WriterC w m a -> StateC w m a) -> WriterC w m a -> m (w, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WriterC w m a -> StateC w m a
forall w (m :: * -> *) a. WriterC w m a -> StateC w m a
runWriterC
{-# INLINE runWriter #-}
execWriter :: (Monoid w, Functor m) => WriterC w m a -> m w
execWriter :: WriterC w m a -> m w
execWriter = ((w, a) -> w) -> m (w, a) -> m w
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (w, a) -> w
forall a b. (a, b) -> a
fst (m (w, a) -> m w)
-> (WriterC w m a -> m (w, a)) -> WriterC w m a -> m w
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WriterC w m a -> m (w, a)
forall w (m :: * -> *) a. Monoid w => WriterC w m a -> m (w, a)
runWriter
{-# INLINE execWriter #-}
newtype WriterC w m a = WriterC { WriterC w m a -> StateC w m a
runWriterC :: StateC w m a }
deriving (Applicative (WriterC w m)
WriterC w m a
Applicative (WriterC w m) =>
(forall a. WriterC w m a)
-> (forall a. WriterC w m a -> WriterC w m a -> WriterC w m a)
-> (forall a. WriterC w m a -> WriterC w m [a])
-> (forall a. WriterC w m a -> WriterC w m [a])
-> Alternative (WriterC w m)
WriterC w m a -> WriterC w m a -> WriterC w m a
WriterC w m a -> WriterC w m [a]
WriterC w m a -> WriterC w m [a]
forall a. WriterC w m a
forall a. WriterC w m a -> WriterC w m [a]
forall a. WriterC w m a -> WriterC w m a -> WriterC w m a
forall w (m :: * -> *).
(Alternative m, Monad m) =>
Applicative (WriterC w m)
forall w (m :: * -> *) a. (Alternative m, Monad m) => WriterC w m a
forall w (m :: * -> *) a.
(Alternative m, Monad m) =>
WriterC w m a -> WriterC w m [a]
forall w (m :: * -> *) a.
(Alternative m, Monad m) =>
WriterC w m a -> WriterC w m a -> WriterC w m a
forall (f :: * -> *).
Applicative f =>
(forall a. f a)
-> (forall a. f a -> f a -> f a)
-> (forall a. f a -> f [a])
-> (forall a. f a -> f [a])
-> Alternative f
many :: WriterC w m a -> WriterC w m [a]
$cmany :: forall w (m :: * -> *) a.
(Alternative m, Monad m) =>
WriterC w m a -> WriterC w m [a]
some :: WriterC w m a -> WriterC w m [a]
$csome :: forall w (m :: * -> *) a.
(Alternative m, Monad m) =>
WriterC w m a -> WriterC w m [a]
<|> :: WriterC w m a -> WriterC w m a -> WriterC w m a
$c<|> :: forall w (m :: * -> *) a.
(Alternative m, Monad m) =>
WriterC w m a -> WriterC w m a -> WriterC w m a
empty :: WriterC w m a
$cempty :: forall w (m :: * -> *) a. (Alternative m, Monad m) => WriterC w m a
$cp1Alternative :: forall w (m :: * -> *).
(Alternative m, Monad m) =>
Applicative (WriterC w m)
Alternative, Functor (WriterC w m)
a -> WriterC w m a
Functor (WriterC w m) =>
(forall a. a -> WriterC w m a)
-> (forall a b.
WriterC w m (a -> b) -> WriterC w m a -> WriterC w m b)
-> (forall a b c.
(a -> b -> c) -> WriterC w m a -> WriterC w m b -> WriterC w m c)
-> (forall a b. WriterC w m a -> WriterC w m b -> WriterC w m b)
-> (forall a b. WriterC w m a -> WriterC w m b -> WriterC w m a)
-> Applicative (WriterC w m)
WriterC w m a -> WriterC w m b -> WriterC w m b
WriterC w m a -> WriterC w m b -> WriterC w m a
WriterC w m (a -> b) -> WriterC w m a -> WriterC w m b
(a -> b -> c) -> WriterC w m a -> WriterC w m b -> WriterC w m c
forall a. a -> WriterC w m a
forall a b. WriterC w m a -> WriterC w m b -> WriterC w m a
forall a b. WriterC w m a -> WriterC w m b -> WriterC w m b
forall a b. WriterC w m (a -> b) -> WriterC w m a -> WriterC w m b
forall a b c.
(a -> b -> c) -> WriterC w m a -> WriterC w m b -> WriterC w m c
forall w (m :: * -> *). Monad m => Functor (WriterC w m)
forall w (m :: * -> *) a. Monad m => a -> WriterC w m a
forall w (m :: * -> *) a b.
Monad m =>
WriterC w m a -> WriterC w m b -> WriterC w m a
forall w (m :: * -> *) a b.
Monad m =>
WriterC w m a -> WriterC w m b -> WriterC w m b
forall w (m :: * -> *) a b.
Monad m =>
WriterC w m (a -> b) -> WriterC w m a -> WriterC w m b
forall w (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> WriterC w m a -> WriterC w m b -> WriterC w m c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: WriterC w m a -> WriterC w m b -> WriterC w m a
$c<* :: forall w (m :: * -> *) a b.
Monad m =>
WriterC w m a -> WriterC w m b -> WriterC w m a
*> :: WriterC w m a -> WriterC w m b -> WriterC w m b
$c*> :: forall w (m :: * -> *) a b.
Monad m =>
WriterC w m a -> WriterC w m b -> WriterC w m b
liftA2 :: (a -> b -> c) -> WriterC w m a -> WriterC w m b -> WriterC w m c
$cliftA2 :: forall w (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> WriterC w m a -> WriterC w m b -> WriterC w m c
<*> :: WriterC w m (a -> b) -> WriterC w m a -> WriterC w m b
$c<*> :: forall w (m :: * -> *) a b.
Monad m =>
WriterC w m (a -> b) -> WriterC w m a -> WriterC w m b
pure :: a -> WriterC w m a
$cpure :: forall w (m :: * -> *) a. Monad m => a -> WriterC w m a
$cp1Applicative :: forall w (m :: * -> *). Monad m => Functor (WriterC w m)
Applicative, a -> WriterC w m b -> WriterC w m a
(a -> b) -> WriterC w m a -> WriterC w m b
(forall a b. (a -> b) -> WriterC w m a -> WriterC w m b)
-> (forall a b. a -> WriterC w m b -> WriterC w m a)
-> Functor (WriterC w m)
forall a b. a -> WriterC w m b -> WriterC w m a
forall a b. (a -> b) -> WriterC w m a -> WriterC w m b
forall w (m :: * -> *) a b.
Functor m =>
a -> WriterC w m b -> WriterC w m a
forall w (m :: * -> *) a b.
Functor m =>
(a -> b) -> WriterC w m a -> WriterC w m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> WriterC w m b -> WriterC w m a
$c<$ :: forall w (m :: * -> *) a b.
Functor m =>
a -> WriterC w m b -> WriterC w m a
fmap :: (a -> b) -> WriterC w m a -> WriterC w m b
$cfmap :: forall w (m :: * -> *) a b.
Functor m =>
(a -> b) -> WriterC w m a -> WriterC w m b
Functor, Applicative (WriterC w m)
a -> WriterC w m a
Applicative (WriterC w m) =>
(forall a b.
WriterC w m a -> (a -> WriterC w m b) -> WriterC w m b)
-> (forall a b. WriterC w m a -> WriterC w m b -> WriterC w m b)
-> (forall a. a -> WriterC w m a)
-> Monad (WriterC w m)
WriterC w m a -> (a -> WriterC w m b) -> WriterC w m b
WriterC w m a -> WriterC w m b -> WriterC w m b
forall a. a -> WriterC w m a
forall a b. WriterC w m a -> WriterC w m b -> WriterC w m b
forall a b. WriterC w m a -> (a -> WriterC w m b) -> WriterC w m b
forall w (m :: * -> *). Monad m => Applicative (WriterC w m)
forall w (m :: * -> *) a. Monad m => a -> WriterC w m a
forall w (m :: * -> *) a b.
Monad m =>
WriterC w m a -> WriterC w m b -> WriterC w m b
forall w (m :: * -> *) a b.
Monad m =>
WriterC w m a -> (a -> WriterC w m b) -> WriterC w m b
forall (m :: * -> *).
Applicative m =>
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: a -> WriterC w m a
$creturn :: forall w (m :: * -> *) a. Monad m => a -> WriterC w m a
>> :: WriterC w m a -> WriterC w m b -> WriterC w m b
$c>> :: forall w (m :: * -> *) a b.
Monad m =>
WriterC w m a -> WriterC w m b -> WriterC w m b
>>= :: WriterC w m a -> (a -> WriterC w m b) -> WriterC w m b
$c>>= :: forall w (m :: * -> *) a b.
Monad m =>
WriterC w m a -> (a -> WriterC w m b) -> WriterC w m b
$cp1Monad :: forall w (m :: * -> *). Monad m => Applicative (WriterC w m)
Monad, Monad (WriterC w m)
Monad (WriterC w m) =>
(forall a. String -> WriterC w m a) -> MonadFail (WriterC w m)
String -> WriterC w m a
forall a. String -> WriterC w m a
forall w (m :: * -> *). MonadFail m => Monad (WriterC w m)
forall w (m :: * -> *) a. MonadFail m => String -> WriterC w m a
forall (m :: * -> *).
Monad m =>
(forall a. String -> m a) -> MonadFail m
fail :: String -> WriterC w m a
$cfail :: forall w (m :: * -> *) a. MonadFail m => String -> WriterC w m a
$cp1MonadFail :: forall w (m :: * -> *). MonadFail m => Monad (WriterC w m)
Fail.MonadFail, Monad (WriterC w m)
Monad (WriterC w m) =>
(forall a. (a -> WriterC w m a) -> WriterC w m a)
-> MonadFix (WriterC w m)
(a -> WriterC w m a) -> WriterC w m a
forall a. (a -> WriterC w m a) -> WriterC w m a
forall w (m :: * -> *). MonadFix m => Monad (WriterC w m)
forall w (m :: * -> *) a.
MonadFix m =>
(a -> WriterC w m a) -> WriterC w m a
forall (m :: * -> *).
Monad m =>
(forall a. (a -> m a) -> m a) -> MonadFix m
mfix :: (a -> WriterC w m a) -> WriterC w m a
$cmfix :: forall w (m :: * -> *) a.
MonadFix m =>
(a -> WriterC w m a) -> WriterC w m a
$cp1MonadFix :: forall w (m :: * -> *). MonadFix m => Monad (WriterC w m)
MonadFix, Monad (WriterC w m)
Monad (WriterC w m) =>
(forall a. IO a -> WriterC w m a) -> MonadIO (WriterC w m)
IO a -> WriterC w m a
forall a. IO a -> WriterC w m a
forall w (m :: * -> *). MonadIO m => Monad (WriterC w m)
forall w (m :: * -> *) a. MonadIO m => IO a -> WriterC w m a
forall (m :: * -> *).
Monad m =>
(forall a. IO a -> m a) -> MonadIO m
liftIO :: IO a -> WriterC w m a
$cliftIO :: forall w (m :: * -> *) a. MonadIO m => IO a -> WriterC w m a
$cp1MonadIO :: forall w (m :: * -> *). MonadIO m => Monad (WriterC w m)
MonadIO, Monad (WriterC w m)
Alternative (WriterC w m)
WriterC w m a
(Alternative (WriterC w m), Monad (WriterC w m)) =>
(forall a. WriterC w m a)
-> (forall a. WriterC w m a -> WriterC w m a -> WriterC w m a)
-> MonadPlus (WriterC w m)
WriterC w m a -> WriterC w m a -> WriterC w m a
forall a. WriterC w m a
forall a. WriterC w m a -> WriterC w m a -> WriterC w m a
forall w (m :: * -> *).
(Alternative m, Monad m) =>
Monad (WriterC w m)
forall w (m :: * -> *).
(Alternative m, Monad m) =>
Alternative (WriterC w m)
forall w (m :: * -> *) a. (Alternative m, Monad m) => WriterC w m a
forall w (m :: * -> *) a.
(Alternative m, Monad m) =>
WriterC w m a -> WriterC w m a -> WriterC w m a
forall (m :: * -> *).
(Alternative m, Monad m) =>
(forall a. m a) -> (forall a. m a -> m a -> m a) -> MonadPlus m
mplus :: WriterC w m a -> WriterC w m a -> WriterC w m a
$cmplus :: forall w (m :: * -> *) a.
(Alternative m, Monad m) =>
WriterC w m a -> WriterC w m a -> WriterC w m a
mzero :: WriterC w m a
$cmzero :: forall w (m :: * -> *) a. (Alternative m, Monad m) => WriterC w m a
$cp2MonadPlus :: forall w (m :: * -> *).
(Alternative m, Monad m) =>
Monad (WriterC w m)
$cp1MonadPlus :: forall w (m :: * -> *).
(Alternative m, Monad m) =>
Alternative (WriterC w m)
MonadPlus, m a -> WriterC w m a
(forall (m :: * -> *) a. Monad m => m a -> WriterC w m a)
-> MonadTrans (WriterC w)
forall w (m :: * -> *) a. Monad m => m a -> WriterC w m a
forall (m :: * -> *) a. Monad m => m a -> WriterC w m a
forall (t :: (* -> *) -> * -> *).
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
lift :: m a -> WriterC w m a
$clift :: forall w (m :: * -> *) a. Monad m => m a -> WriterC w m a
MonadTrans)
instance (Monoid w, Carrier sig m, Effect sig) => Carrier (Writer w :+: sig) (WriterC w m) where
eff :: (:+:) (Writer w) sig (WriterC w m) a -> WriterC w m a
eff (L (Tell w :: w
w k :: WriterC w m a
k)) = StateC w m a -> WriterC w m a
forall w (m :: * -> *) a. StateC w m a -> WriterC w m a
WriterC (StateC w m a -> WriterC w m a) -> StateC w m a -> WriterC w m a
forall a b. (a -> b) -> a -> b
$ do
(w -> w) -> StateC w m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
(Member (State s) sig, Carrier sig m) =>
(s -> s) -> m ()
modify (w -> w -> w
forall a. Monoid a => a -> a -> a
`mappend` w
w)
WriterC w m a -> StateC w m a
forall w (m :: * -> *) a. WriterC w m a -> StateC w m a
runWriterC WriterC w m a
k
eff (L (Listen m :: WriterC w m a
m k :: w -> a -> WriterC w m a
k)) = StateC w m a -> WriterC w m a
forall w (m :: * -> *) a. StateC w m a -> WriterC w m a
WriterC (StateC w m a -> WriterC w m a) -> StateC w m a -> WriterC w m a
forall a b. (a -> b) -> a -> b
$ do
w
w <- StateC w m w
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
(Member (State s) sig, Carrier sig m) =>
m s
get
w -> StateC w m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
(Member (State s) sig, Carrier sig m) =>
s -> m ()
put (w
forall a. Monoid a => a
mempty :: w)
a
a <- WriterC w m a -> StateC w m a
forall w (m :: * -> *) a. WriterC w m a -> StateC w m a
runWriterC WriterC w m a
m
w
w' <- StateC w m w
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
(Member (State s) sig, Carrier sig m) =>
m s
get
(w -> w) -> StateC w m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
(Member (State s) sig, Carrier sig m) =>
(s -> s) -> m ()
modify (w -> w -> w
forall a. Monoid a => a -> a -> a
mappend (w
w :: w))
WriterC w m a -> StateC w m a
forall w (m :: * -> *) a. WriterC w m a -> StateC w m a
runWriterC (w -> a -> WriterC w m a
k w
w' a
a)
eff (L (Censor f :: w -> w
f m :: WriterC w m a
m k :: a -> WriterC w m a
k)) = StateC w m a -> WriterC w m a
forall w (m :: * -> *) a. StateC w m a -> WriterC w m a
WriterC (StateC w m a -> WriterC w m a) -> StateC w m a -> WriterC w m a
forall a b. (a -> b) -> a -> b
$ do
w
w <- StateC w m w
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
(Member (State s) sig, Carrier sig m) =>
m s
get
w -> StateC w m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
(Member (State s) sig, Carrier sig m) =>
s -> m ()
put (w
forall a. Monoid a => a
mempty :: w)
a
a <- WriterC w m a -> StateC w m a
forall w (m :: * -> *) a. WriterC w m a -> StateC w m a
runWriterC WriterC w m a
m
(w -> w) -> StateC w m ()
forall s (sig :: (* -> *) -> * -> *) (m :: * -> *).
(Member (State s) sig, Carrier sig m) =>
(s -> s) -> m ()
modify (w -> w -> w
forall a. Monoid a => a -> a -> a
mappend w
w (w -> w) -> (w -> w) -> w -> w
forall b c a. (b -> c) -> (a -> b) -> a -> c
. w -> w
f)
WriterC w m a -> StateC w m a
forall w (m :: * -> *) a. WriterC w m a -> StateC w m a
runWriterC (a -> WriterC w m a
k a
a)
eff (R other :: sig (WriterC w m) a
other) = StateC w m a -> WriterC w m a
forall w (m :: * -> *) a. StateC w m a -> WriterC w m a
WriterC ((:+:) (State w) sig (StateC w m) a -> StateC w m a
forall (sig :: (* -> *) -> * -> *) (m :: * -> *) a.
Carrier sig m =>
sig m a -> m a
eff (sig (StateC w m) a -> (:+:) (State w) sig (StateC w m) a
forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
(m :: * -> *) k.
g m k -> (:+:) f g m k
R (sig (WriterC w m) a -> sig (StateC w m) a
forall (sig :: (* -> *) -> * -> *) (f :: * -> *) (g :: * -> *) a.
(HFunctor sig, Functor f, Coercible f g) =>
sig f a -> sig g a
handleCoercible sig (WriterC w m) a
other)))
{-# INLINE eff #-}