{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}

{- | A high-performance, strict, church-encoded carrier for 'Writer'.

This carrier issues left-associated 'mappend's, meaning that 'Monoid's such as @[]@ with poor performance for left-associated 'mappend's are ill-suited for use with this carrier. Alternatives such as 'Data.Monoid.Endo', @Seq@, or @DList@ may be preferred.

@since 1.1.0.0
-}
module Control.Carrier.Writer.Church
( -- * Writer carrier
  runWriter
, execWriter
, WriterC(WriterC)
  -- * Writer effect
, module Control.Effect.Writer
) where

import Control.Algebra
import Control.Applicative (Alternative)
import Control.Carrier.State.Church
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

-- | Run a 'Writer' effect with a 'Monoid'al log, applying a continuation to the final log and result.
--
-- @
-- 'runWriter' k ('pure' a) = k 'mempty' a
-- @
-- @
-- 'runWriter' k ('tell' w) = k w ()
-- @
-- @
-- 'runWriter' k ('listen' ('tell' w)) = k w (w, ())
-- @
-- @
-- 'runWriter' k ('censor' f ('tell' w)) = k (f w) ()
-- @
--
-- @since 1.1.0.0
runWriter :: Monoid w => (w -> a -> m b) -> WriterC w m a -> m b
runWriter :: forall w a (m :: * -> *) b.
Monoid w =>
(w -> a -> m b) -> WriterC w m a -> m b
runWriter w -> a -> m b
k = forall s (m :: * -> *) a b.
(s -> a -> m b) -> s -> StateC s m a -> m b
runState w -> a -> m b
k forall a. Monoid a => a
mempty forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. WriterC w m a -> StateC w m a
runWriterC
{-# INLINE runWriter #-}

-- | Run a 'Writer' effect with a 'Monoid'al log, producing the final log and discarding the result value.
--
-- @
-- 'execWriter' = 'runWriter' ('const' '.' 'pure')
-- @
--
-- @since 1.1.0.0
execWriter :: (Monoid w, Applicative m) => WriterC w m a -> m w
execWriter :: forall w (m :: * -> *) a.
(Monoid w, Applicative m) =>
WriterC w m a -> m w
execWriter = forall w a (m :: * -> *) b.
Monoid w =>
(w -> a -> m b) -> WriterC w m a -> m b
runWriter (forall a b. a -> b -> a
const forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure)
{-# INLINE execWriter #-}

-- | @since 1.1.0.0
newtype WriterC w m a = WriterC { forall w (m :: * -> *) a. WriterC w m a -> StateC w m a
runWriterC :: StateC w m a }
  deriving (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 =>
Applicative (WriterC w m)
forall w (m :: * -> *) a. Alternative m => WriterC w m a
forall w (m :: * -> *) a.
Alternative m =>
WriterC w m a -> WriterC w m [a]
forall w (m :: * -> *) a.
Alternative 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 :: forall a. WriterC w m a -> WriterC w m [a]
$cmany :: forall w (m :: * -> *) a.
Alternative m =>
WriterC w m a -> WriterC w m [a]
some :: forall a. WriterC w m a -> WriterC w m [a]
$csome :: forall w (m :: * -> *) a.
Alternative m =>
WriterC w m a -> WriterC w m [a]
<|> :: forall a. WriterC w m a -> WriterC w m a -> WriterC w m a
$c<|> :: forall w (m :: * -> *) a.
Alternative m =>
WriterC w m a -> WriterC w m a -> WriterC w m a
empty :: forall a. WriterC w m a
$cempty :: forall w (m :: * -> *) a. Alternative m => WriterC w m a
Alternative, 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 :: * -> *}. Functor (WriterC w m)
forall w (m :: * -> *) a. a -> WriterC w m a
forall w (m :: * -> *) a b.
WriterC w m a -> WriterC w m b -> WriterC w m a
forall w (m :: * -> *) a b.
WriterC w m a -> WriterC w m b -> WriterC w m b
forall w (m :: * -> *) a b.
WriterC w m (a -> b) -> WriterC w m a -> WriterC w m b
forall w (m :: * -> *) a b c.
(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
<* :: forall a b. WriterC w m a -> WriterC w m b -> WriterC w m a
$c<* :: forall w (m :: * -> *) 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
$c*> :: forall w (m :: * -> *) a b.
WriterC w m a -> WriterC w m b -> WriterC w m b
liftA2 :: forall a b c.
(a -> b -> c) -> WriterC w m a -> WriterC w m b -> WriterC w m c
$cliftA2 :: forall w (m :: * -> *) 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 -> b) -> WriterC w m a -> WriterC w m b
$c<*> :: forall w (m :: * -> *) a b.
WriterC w m (a -> b) -> WriterC w m a -> WriterC w m b
pure :: forall a. a -> WriterC w m a
$cpure :: forall w (m :: * -> *) a. a -> WriterC w m a
Applicative, 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. a -> WriterC w m b -> WriterC w m a
forall w (m :: * -> *) a b.
(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
<$ :: forall a b. a -> WriterC w m b -> WriterC w m a
$c<$ :: forall w (m :: * -> *) a b. a -> WriterC w m b -> WriterC w m a
fmap :: forall a b. (a -> b) -> WriterC w m a -> WriterC w m b
$cfmap :: forall w (m :: * -> *) a b.
(a -> b) -> WriterC w m a -> WriterC w m b
Functor, 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 :: * -> *). Applicative (WriterC w m)
forall w (m :: * -> *) a. a -> WriterC w m a
forall w (m :: * -> *) a b.
WriterC w m a -> WriterC w m b -> WriterC w m b
forall w (m :: * -> *) a b.
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 :: forall a. a -> WriterC w m a
$creturn :: forall w (m :: * -> *) a. a -> WriterC w m a
>> :: forall a b. WriterC w m a -> WriterC w m b -> WriterC w m b
$c>> :: forall w (m :: * -> *) 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
$c>>= :: forall w (m :: * -> *) a b.
WriterC w m a -> (a -> WriterC w m b) -> WriterC w m b
Monad, 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 :: forall a. String -> WriterC w m a
$cfail :: forall w (m :: * -> *) a. MonadFail m => String -> WriterC w m a
Fail.MonadFail, 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 :: forall a. (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
MonadFix, 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 :: forall a. IO a -> WriterC w m a
$cliftIO :: forall w (m :: * -> *) a. MonadIO m => IO a -> WriterC w m a
MonadIO, 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 :: forall a. 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 :: forall a. WriterC w m a
$cmzero :: forall w (m :: * -> *) a. (Alternative m, Monad m) => WriterC w m a
MonadPlus, 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 :: forall (m :: * -> *) a. Monad m => m a -> WriterC w m a
$clift :: forall w (m :: * -> *) a. Monad m => m a -> WriterC w m a
MonadTrans)

instance (Algebra sig m, Monoid w) => Algebra (Writer w :+: sig) (WriterC w m) where
  alg :: forall (ctx :: * -> *) (n :: * -> *) a.
Functor ctx =>
Handler ctx n (WriterC w m)
-> (:+:) (Writer w) sig n a -> ctx () -> WriterC w m (ctx a)
alg Handler ctx n (WriterC w m)
hdl (:+:) (Writer w) sig n a
sig ctx ()
ctx = forall w (m :: * -> *) a. StateC w m a -> WriterC w m a
WriterC forall a b. (a -> b) -> a -> b
$ case (:+:) (Writer w) sig n a
sig of
    L Writer w n a
writer -> forall s (m :: * -> *) a.
(forall r. (s -> a -> m r) -> s -> m r) -> StateC s m a
StateC forall a b. (a -> b) -> a -> b
$ \ w -> ctx a -> m r
k w
w -> case Writer w n a
writer of
      Tell w
w'    -> do
        let !w'' :: w
w'' = forall a. Monoid a => a -> a -> a
mappend w
w w
w'
        w -> ctx a -> m r
k w
w'' ctx ()
ctx
      Listen   n a
m -> forall w a (m :: * -> *) b.
Monoid w =>
(w -> a -> m b) -> WriterC w m a -> m b
runWriter (\ w
w' ctx a
a -> do
        let !w'' :: w
w'' = forall a. Monoid a => a -> a -> a
mappend w
w w
w'
        w -> ctx a -> m r
k w
w'' ((,) w
w' forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ctx a
a)) (Handler ctx n (WriterC w m)
hdl (n a
m forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ctx ()
ctx))
      Censor w -> w
f n a
m -> forall w a (m :: * -> *) b.
Monoid w =>
(w -> a -> m b) -> WriterC w m a -> m b
runWriter (\ w
w' ctx a
a -> do
        let !w'' :: w
w'' = forall a. Monoid a => a -> a -> a
mappend w
w (w -> w
f w
w')
        w -> ctx a -> m r
k w
w'' ctx a
a) (Handler ctx n (WriterC w m)
hdl (n a
m forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ctx ()
ctx))
    R sig n a
other  -> forall (sig :: (* -> *) -> * -> *) (m :: * -> *) (ctx :: * -> *)
       (n :: * -> *) a.
(Algebra sig m, Functor ctx) =>
Handler ctx n m -> sig n a -> ctx () -> m (ctx a)
alg (forall w (m :: * -> *) a. WriterC w m a -> StateC w m a
runWriterC forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handler ctx n (WriterC w m)
hdl) (forall (f :: (* -> *) -> * -> *) (g :: (* -> *) -> * -> *)
       (m :: * -> *) k.
g m k -> (:+:) f g m k
R sig n a
other) ctx ()
ctx
  {-# INLINE alg #-}