{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Control.Carrier.Writer.Strict
(
runWriter
, execWriter
, WriterC(WriterC)
, module Control.Effect.Writer
) where
import Control.Algebra
import Control.Applicative (Alternative)
import Control.Carrier.State.Strict
import Control.Effect.Writer
import Control.Monad (MonadPlus)
import Control.Monad.Fail as Fail
import Control.Monad.Fix
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
runWriter :: Monoid w => WriterC w m a -> m (w, a)
runWriter (WriterC m) = runState mempty m
{-# INLINE runWriter #-}
execWriter :: (Monoid w, Functor m) => WriterC w m a -> m w
execWriter = fmap fst . runWriter
{-# INLINE execWriter #-}
newtype WriterC w m a = WriterC { runWriterC :: StateC w m a }
deriving (Alternative, Applicative, Functor, Monad, Fail.MonadFail, MonadFix, MonadIO, MonadPlus, MonadTrans)
instance (Monoid w, Algebra sig m) => Algebra (Writer w :+: sig) (WriterC w m) where
alg hdl sig ctx = WriterC $ case sig of
L writer -> StateC $ \ w -> case writer of
Tell w' -> do
let !w'' = mappend w w'
pure (w'', ctx)
Listen m -> do
(w', a) <- runWriter (hdl (m <$ ctx))
let !w'' = mappend w w'
pure (w'', (,) w' <$> a)
Censor f m -> do
(w', a) <- runWriter (hdl (m <$ ctx))
let !w'' = mappend w (f w')
pure (w'', a)
R other -> alg (runWriterC . hdl) (R other) ctx
{-# INLINE alg #-}