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

{- | A carrier for 'Writer' effects. This carrier performs its append operations strictly and thus avoids the space leaks inherent in lazy writer monads. These appends are left-associative; as such, @[]@ is a poor choice of monoid for computations that entail many calls to 'tell'. The [Seq](http://hackage.haskell.org/package/containersdocs/Data-Sequence.html) or [DList](http://hackage.haskell.org/package/dlist) monoids may be a superior choice.

This implementation is based on a post Gabriel Gonzalez made to the Haskell mailing list: <https://mail.haskell.org/pipermail/libraries/2013-March/019528.html>

@since 1.0.0.0
-}

module Control.Carrier.Writer.Strict
( -- * Writer carrier
  runWriter
, execWriter
, WriterC(WriterC)
  -- * Writer effect
, 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

-- | Run a 'Writer' effect with a 'Monoid'al log, producing the final log alongside the result value.
--
-- @
-- 'runWriter' ('tell' w) = 'pure' (w, ())
-- @
-- @
-- 'runWriter' ('pure' a) = 'pure' ('mempty', a)
-- @
runWriter :: Monoid w => WriterC w m a -> m (w, a)
runWriter :: forall w (m :: * -> *) a. Monoid w => WriterC w m a -> m (w, a)
runWriter (WriterC StateC w m a
m) = 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
{-# INLINE runWriter #-}

-- | Run a 'Writer' effect with a 'Monoid'al log, producing the final log and discarding the result value.
--
-- @
-- 'execWriter' m = 'fmap' 'fst' ('runWriter' m)
-- @
execWriter :: (Monoid w, Functor m) => WriterC w m a -> m w
execWriter :: forall w (m :: * -> *) a.
(Monoid w, Functor m) =>
WriterC w m a -> m w
execWriter = ((w, a) -> w) -> m (w, a) -> m w
forall a b. (a -> b) -> m a -> m b
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 #-}


-- | A space-efficient carrier for 'Writer' effects, implemented atop "Control.Carrier.State.Strict".
--
-- @since 1.0.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 (Applicative (WriterC w m)
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)
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
$cempty :: forall w (m :: * -> *) a. (Alternative m, Monad m) => WriterC w m a
empty :: forall 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
<|> :: forall a. WriterC w m a -> 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]
some :: forall a. 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]
many :: forall a. WriterC w m a -> WriterC w m [a]
Alternative, Functor (WriterC w m)
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)
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
$cpure :: forall w (m :: * -> *) a. Monad m => a -> WriterC w m a
pure :: forall a. a -> WriterC w m a
$c<*> :: forall w (m :: * -> *) a b.
Monad m =>
WriterC w m (a -> b) -> WriterC w m a -> WriterC w m b
<*> :: forall a b. WriterC w m (a -> b) -> WriterC w m a -> WriterC w m b
$cliftA2 :: forall w (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> WriterC w m a -> WriterC w m b -> WriterC w m c
liftA2 :: forall a b c.
(a -> b -> c) -> WriterC w m a -> WriterC w m b -> WriterC w m c
$c*> :: forall w (m :: * -> *) a b.
Monad m =>
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 b
$c<* :: forall w (m :: * -> *) a b.
Monad m =>
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 a
Applicative, (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
$cfmap :: forall w (m :: * -> *) a b.
Functor m =>
(a -> b) -> WriterC w m a -> WriterC w m b
fmap :: forall a b. (a -> b) -> WriterC w m a -> WriterC w m b
$c<$ :: forall w (m :: * -> *) a b.
Functor m =>
a -> WriterC w m b -> WriterC w m a
<$ :: forall a b. a -> WriterC w m b -> WriterC w m a
Functor, Applicative (WriterC w m)
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)
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
$c>>= :: forall w (m :: * -> *) a b.
Monad m =>
WriterC w m a -> (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.
Monad m =>
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 b
$creturn :: forall w (m :: * -> *) a. Monad m => a -> WriterC w m a
return :: forall a. a -> WriterC w m a
Monad, Monad (WriterC w m)
Monad (WriterC w m) =>
(forall a. String -> WriterC w m a) -> MonadFail (WriterC w m)
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
$cfail :: forall w (m :: * -> *) a. MonadFail m => String -> WriterC w m a
fail :: forall a. String -> WriterC w m a
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)
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
$cmfix :: forall w (m :: * -> *) a.
MonadFix m =>
(a -> WriterC w m a) -> WriterC w m a
mfix :: forall a. (a -> WriterC w m a) -> WriterC w m a
MonadFix, Monad (WriterC w m)
Monad (WriterC w m) =>
(forall a. IO a -> WriterC w m a) -> MonadIO (WriterC w m)
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
$cliftIO :: forall w (m :: * -> *) a. MonadIO m => IO a -> WriterC w m a
liftIO :: forall a. IO a -> WriterC w m a
MonadIO, Monad (WriterC w m)
Alternative (WriterC w m)
(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)
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
$cmzero :: forall w (m :: * -> *) a. (Alternative m, Monad m) => WriterC w m a
mzero :: forall 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
mplus :: forall a. WriterC w m a -> WriterC w m a -> WriterC w m a
MonadPlus, (forall (m :: * -> *). Monad m => Monad (WriterC w m)) =>
(forall (m :: * -> *) a. Monad m => m a -> WriterC w m a)
-> MonadTrans (WriterC w)
forall w (m :: * -> *). Monad m => Monad (WriterC w m)
forall w (m :: * -> *) a. Monad m => m a -> WriterC w m a
forall (m :: * -> *). Monad m => Monad (WriterC w m)
forall (m :: * -> *) a. Monad m => m a -> WriterC w m a
forall (t :: (* -> *) -> * -> *).
(forall (m :: * -> *). Monad m => Monad (t m)) =>
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
$clift :: forall w (m :: * -> *) a. Monad m => m a -> WriterC w m a
lift :: forall (m :: * -> *) a. Monad m => m a -> WriterC w m a
MonadTrans)

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