{-# LANGUAGE Safe #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE UndecidableInstances #-}
-- Search for UndecidableInstances to see why this is needed

-----------------------------------------------------------------------------
-- |
-- Module      :  Control.Monad.Writer.Class
-- Copyright   :  (c) Andy Gill 2001,
--                (c) Oregon Graduate Institute of Science and Technology, 2001
-- License     :  BSD-style (see the file LICENSE)
--
-- Maintainer  :  libraries@haskell.org
-- Stability   :  experimental
-- Portability :  non-portable (multi-param classes, functional dependencies)
--
-- The MonadWriter class.
--
--      Inspired by the paper
--      /Functional Programming with Overloading and Higher-Order Polymorphism/,
--        Mark P Jones (<http://web.cecs.pdx.edu/~mpj/pubs/springschool.html>)
--          Advanced School of Functional Programming, 1995.
-----------------------------------------------------------------------------

module Control.Monad.Writer.Class (
    MonadWriter(..),
    listens,
    censor,
  ) where

import Control.Monad.Trans.Except (ExceptT)
import qualified Control.Monad.Trans.Except as Except
import Control.Monad.Trans.Identity (IdentityT)
import qualified Control.Monad.Trans.Identity as Identity
import Control.Monad.Trans.Maybe (MaybeT)
import qualified Control.Monad.Trans.Maybe as Maybe
import Control.Monad.Trans.Reader (ReaderT, mapReaderT)
import qualified Control.Monad.Trans.RWS.Lazy as LazyRWS 
import qualified Control.Monad.Trans.RWS.Strict as StrictRWS 
import qualified Control.Monad.Trans.State.Lazy as Lazy
import qualified Control.Monad.Trans.State.Strict as Strict
import qualified Control.Monad.Trans.Writer.Lazy as Lazy
import qualified Control.Monad.Trans.Writer.Strict as Strict 
import Control.Monad.Trans.Accum (AccumT)
import qualified Control.Monad.Trans.Accum as Accum
import qualified Control.Monad.Trans.RWS.CPS as CPSRWS
import qualified Control.Monad.Trans.Writer.CPS as CPS
import Control.Monad.Trans.Class (lift)

-- ---------------------------------------------------------------------------
-- MonadWriter class
--
-- tell is like tell on the MUD's it shouts to monad
-- what you want to be heard. The monad carries this 'packet'
-- upwards, merging it if needed (hence the Monoid requirement).
--
-- listen listens to a monad acting, and returns what the monad "said".
--
-- pass lets you provide a writer transformer which changes internals of
-- the written object.

class (Monoid w, Monad m) => MonadWriter w m | m -> w where
    {-# MINIMAL (writer | tell), listen, pass #-}
    -- | @'writer' (a,w)@ embeds a simple writer action.
    writer :: (a,w) -> m a
    writer ~(a
a, w
w) = do
      forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell w
w
      forall (m :: * -> *) a. Monad m => a -> m a
return a
a

    -- | @'tell' w@ is an action that produces the output @w@.
    tell   :: w -> m ()
    tell w
w = forall w (m :: * -> *) a. MonadWriter w m => (a, w) -> m a
writer ((),w
w)

    -- | @'listen' m@ is an action that executes the action @m@ and adds
    -- its output to the value of the computation.
    listen :: m a -> m (a, w)
    -- | @'pass' m@ is an action that executes the action @m@, which
    -- returns a value and a function, and returns the value, applying
    -- the function to the output.
    pass   :: m (a, w -> w) -> m a

-- | @'listens' f m@ is an action that executes the action @m@ and adds
-- the result of applying @f@ to the output to the value of the computation.
--
-- * @'listens' f m = 'liftM' (id *** f) ('listen' m)@
listens :: MonadWriter w m => (w -> b) -> m a -> m (a, b)
listens :: forall w (m :: * -> *) b a.
MonadWriter w m =>
(w -> b) -> m a -> m (a, b)
listens w -> b
f m a
m = do
    ~(a
a, w
w) <- forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen m a
m
    forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, w -> b
f w
w)

-- | @'censor' f m@ is an action that executes the action @m@ and
-- applies the function @f@ to its output, leaving the return value
-- unchanged.
--
-- * @'censor' f m = 'pass' ('liftM' (\\x -> (x,f)) m)@
censor :: MonadWriter w m => (w -> w) -> m a -> m a
censor :: forall w (m :: * -> *) a. MonadWriter w m => (w -> w) -> m a -> m a
censor w -> w
f m a
m = forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass forall a b. (a -> b) -> a -> b
$ do
    a
a <- m a
m
    forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, w -> w
f)

-- | @since 2.2.2
instance (Monoid w) => MonadWriter w ((,) w) where
  writer :: forall a. (a, w) -> (w, a)
writer ~(a
a, w
w) = (w
w, a
a)
  tell :: w -> (w, ())
tell w
w = (w
w, ())
  listen :: forall a. (w, a) -> (w, (a, w))
listen ~(w
w, a
a) = (w
w, (a
a, w
w))
  pass :: forall a. (w, (a, w -> w)) -> (w, a)
pass ~(w
w, (a
a, w -> w
f)) = (w -> w
f w
w, a
a)

-- | @since 2.3
instance (Monoid w, Monad m) => MonadWriter w (CPS.WriterT w m) where
    writer :: forall a. (a, w) -> WriterT w m a
writer = forall w (m :: * -> *) a.
(Monoid w, Monad m) =>
(a, w) -> WriterT w m a
CPS.writer
    tell :: w -> WriterT w m ()
tell   = forall w (m :: * -> *). (Monoid w, Monad m) => w -> WriterT w m ()
CPS.tell
    listen :: forall a. WriterT w m a -> WriterT w m (a, w)
listen = forall w (m :: * -> *) a.
(Monoid w, Monad m) =>
WriterT w m a -> WriterT w m (a, w)
CPS.listen
    pass :: forall a. WriterT w m (a, w -> w) -> WriterT w m a
pass   = forall w w' (m :: * -> *) a.
(Monoid w, Monoid w', Monad m) =>
WriterT w m (a, w -> w') -> WriterT w' m a
CPS.pass

instance (Monoid w, Monad m) => MonadWriter w (Lazy.WriterT w m) where
    writer :: forall a. (a, w) -> WriterT w m a
writer = forall (m :: * -> *) a w. Monad m => (a, w) -> WriterT w m a
Lazy.writer
    tell :: w -> WriterT w m ()
tell   = forall (m :: * -> *) w. Monad m => w -> WriterT w m ()
Lazy.tell
    listen :: forall a. WriterT w m a -> WriterT w m (a, w)
listen = forall (m :: * -> *) w a.
Monad m =>
WriterT w m a -> WriterT w m (a, w)
Lazy.listen
    pass :: forall a. WriterT w m (a, w -> w) -> WriterT w m a
pass   = forall (m :: * -> *) w a.
Monad m =>
WriterT w m (a, w -> w) -> WriterT w m a
Lazy.pass

instance (Monoid w, Monad m) => MonadWriter w (Strict.WriterT w m) where
    writer :: forall a. (a, w) -> WriterT w m a
writer = forall (m :: * -> *) a w. Monad m => (a, w) -> WriterT w m a
Strict.writer
    tell :: w -> WriterT w m ()
tell   = forall (m :: * -> *) w. Monad m => w -> WriterT w m ()
Strict.tell
    listen :: forall a. WriterT w m a -> WriterT w m (a, w)
listen = forall (m :: * -> *) w a.
Monad m =>
WriterT w m a -> WriterT w m (a, w)
Strict.listen
    pass :: forall a. WriterT w m (a, w -> w) -> WriterT w m a
pass   = forall (m :: * -> *) w a.
Monad m =>
WriterT w m (a, w -> w) -> WriterT w m a
Strict.pass

-- | @since 2.3
instance (Monoid w, Monad m) => MonadWriter w (CPSRWS.RWST r w s m) where
    writer :: forall a. (a, w) -> RWST r w s m a
writer = forall w (m :: * -> *) a r s.
(Monoid w, Monad m) =>
(a, w) -> RWST r w s m a
CPSRWS.writer
    tell :: w -> RWST r w s m ()
tell   = forall w (m :: * -> *) r s.
(Monoid w, Monad m) =>
w -> RWST r w s m ()
CPSRWS.tell
    listen :: forall a. RWST r w s m a -> RWST r w s m (a, w)
listen = forall w (m :: * -> *) r s a.
(Monoid w, Monad m) =>
RWST r w s m a -> RWST r w s m (a, w)
CPSRWS.listen
    pass :: forall a. RWST r w s m (a, w -> w) -> RWST r w s m a
pass   = forall w w' (m :: * -> *) r s a.
(Monoid w, Monoid w', Monad m) =>
RWST r w s m (a, w -> w') -> RWST r w' s m a
CPSRWS.pass

instance (Monoid w, Monad m) => MonadWriter w (LazyRWS.RWST r w s m) where
    writer :: forall a. (a, w) -> RWST r w s m a
writer = forall (m :: * -> *) a w r s. Monad m => (a, w) -> RWST r w s m a
LazyRWS.writer
    tell :: w -> RWST r w s m ()
tell   = forall (m :: * -> *) w r s. Monad m => w -> RWST r w s m ()
LazyRWS.tell
    listen :: forall a. RWST r w s m a -> RWST r w s m (a, w)
listen = forall (m :: * -> *) r w s a.
Monad m =>
RWST r w s m a -> RWST r w s m (a, w)
LazyRWS.listen
    pass :: forall a. RWST r w s m (a, w -> w) -> RWST r w s m a
pass   = forall (m :: * -> *) r w s a.
Monad m =>
RWST r w s m (a, w -> w) -> RWST r w s m a
LazyRWS.pass

instance (Monoid w, Monad m) => MonadWriter w (StrictRWS.RWST r w s m) where
    writer :: forall a. (a, w) -> RWST r w s m a
writer = forall (m :: * -> *) a w r s. Monad m => (a, w) -> RWST r w s m a
StrictRWS.writer
    tell :: w -> RWST r w s m ()
tell   = forall (m :: * -> *) w r s. Monad m => w -> RWST r w s m ()
StrictRWS.tell
    listen :: forall a. RWST r w s m a -> RWST r w s m (a, w)
listen = forall (m :: * -> *) r w s a.
Monad m =>
RWST r w s m a -> RWST r w s m (a, w)
StrictRWS.listen
    pass :: forall a. RWST r w s m (a, w -> w) -> RWST r w s m a
pass   = forall (m :: * -> *) r w s a.
Monad m =>
RWST r w s m (a, w -> w) -> RWST r w s m a
StrictRWS.pass

-- ---------------------------------------------------------------------------
-- Instances for other mtl transformers
--
-- All of these instances need UndecidableInstances,
-- because they do not satisfy the coverage condition.

-- | @since 2.2
instance MonadWriter w m => MonadWriter w (ExceptT e m) where
    writer :: forall a. (a, w) -> ExceptT e m a
writer = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. MonadWriter w m => (a, w) -> m a
writer
    tell :: w -> ExceptT e m ()
tell   = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell
    listen :: forall a. ExceptT e m a -> ExceptT e m (a, w)
listen = forall (m :: * -> *) w e a.
Monad m =>
Listen w m (Either e a) -> Listen w (ExceptT e m) a
Except.liftListen forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen
    pass :: forall a. ExceptT e m (a, w -> w) -> ExceptT e m a
pass   = forall (m :: * -> *) w e a.
Monad m =>
Pass w m (Either e a) -> Pass w (ExceptT e m) a
Except.liftPass forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass

instance MonadWriter w m => MonadWriter w (IdentityT m) where
    writer :: forall a. (a, w) -> IdentityT m a
writer = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. MonadWriter w m => (a, w) -> m a
writer
    tell :: w -> IdentityT m ()
tell   = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell
    listen :: forall a. IdentityT m a -> IdentityT m (a, w)
listen = forall {k1} {k2} (m :: k1 -> *) (a :: k1) (n :: k2 -> *) (b :: k2).
(m a -> n b) -> IdentityT m a -> IdentityT n b
Identity.mapIdentityT forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen
    pass :: forall a. IdentityT m (a, w -> w) -> IdentityT m a
pass   = forall {k1} {k2} (m :: k1 -> *) (a :: k1) (n :: k2 -> *) (b :: k2).
(m a -> n b) -> IdentityT m a -> IdentityT n b
Identity.mapIdentityT forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass

instance MonadWriter w m => MonadWriter w (MaybeT m) where
    writer :: forall a. (a, w) -> MaybeT m a
writer = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. MonadWriter w m => (a, w) -> m a
writer
    tell :: w -> MaybeT m ()
tell   = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell
    listen :: forall a. MaybeT m a -> MaybeT m (a, w)
listen = forall (m :: * -> *) w a.
Monad m =>
Listen w m (Maybe a) -> Listen w (MaybeT m) a
Maybe.liftListen forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen
    pass :: forall a. MaybeT m (a, w -> w) -> MaybeT m a
pass   = forall (m :: * -> *) w a.
Monad m =>
Pass w m (Maybe a) -> Pass w (MaybeT m) a
Maybe.liftPass forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass

instance MonadWriter w m => MonadWriter w (ReaderT r m) where
    writer :: forall a. (a, w) -> ReaderT r m a
writer = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. MonadWriter w m => (a, w) -> m a
writer
    tell :: w -> ReaderT r m ()
tell   = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell
    listen :: forall a. ReaderT r m a -> ReaderT r m (a, w)
listen = forall (m :: * -> *) a (n :: * -> *) b r.
(m a -> n b) -> ReaderT r m a -> ReaderT r n b
mapReaderT forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen
    pass :: forall a. ReaderT r m (a, w -> w) -> ReaderT r m a
pass   = forall (m :: * -> *) a (n :: * -> *) b r.
(m a -> n b) -> ReaderT r m a -> ReaderT r n b
mapReaderT forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass

instance MonadWriter w m => MonadWriter w (Lazy.StateT s m) where
    writer :: forall a. (a, w) -> StateT s m a
writer = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. MonadWriter w m => (a, w) -> m a
writer
    tell :: w -> StateT s m ()
tell   = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell
    listen :: forall a. StateT s m a -> StateT s m (a, w)
listen = forall (m :: * -> *) w a s.
Monad m =>
Listen w m (a, s) -> Listen w (StateT s m) a
Lazy.liftListen forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen
    pass :: forall a. StateT s m (a, w -> w) -> StateT s m a
pass   = forall (m :: * -> *) w a s.
Monad m =>
Pass w m (a, s) -> Pass w (StateT s m) a
Lazy.liftPass forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass

instance MonadWriter w m => MonadWriter w (Strict.StateT s m) where
    writer :: forall a. (a, w) -> StateT s m a
writer = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. MonadWriter w m => (a, w) -> m a
writer
    tell :: w -> StateT s m ()
tell   = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell
    listen :: forall a. StateT s m a -> StateT s m (a, w)
listen = forall (m :: * -> *) w a s.
Monad m =>
Listen w m (a, s) -> Listen w (StateT s m) a
Strict.liftListen forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen
    pass :: forall a. StateT s m (a, w -> w) -> StateT s m a
pass   = forall (m :: * -> *) w a s.
Monad m =>
Pass w m (a, s) -> Pass w (StateT s m) a
Strict.liftPass forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass

-- | There are two valid instances for 'AccumT'. It could either:
--
--   1. Lift the operations to the inner @MonadWriter@
--   2. Handle the operations itself, à la a @WriterT@.
--
--   This instance chooses (1), reflecting that the intent
--   of 'AccumT' as a type is different than that of @WriterT@.
--
-- @since 2.3
instance
  ( Monoid w'
  , MonadWriter w m
  ) => MonadWriter w (AccumT w' m) where
    writer :: forall a. (a, w) -> AccumT w' m a
writer = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. MonadWriter w m => (a, w) -> m a
writer
    tell :: w -> AccumT w' m ()
tell   = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell
    listen :: forall a. AccumT w' m a -> AccumT w' m (a, w)
listen = forall (m :: * -> *) w a s.
Monad m =>
Listen w m (a, s) -> Listen w (AccumT s m) a
Accum.liftListen forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen
    pass :: forall a. AccumT w' m (a, w -> w) -> AccumT w' m a
pass   = forall (m :: * -> *) w a s.
Monad m =>
Pass w m (a, s) -> Pass w (AccumT s m) a
Accum.liftPass forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass