{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Safe #-}
{-# LANGUAGE DeriveGeneric #-}
#endif
#if __GLASGOW_HASKELL__ >= 710 && __GLASGOW_HASKELL__ < 802
{-# LANGUAGE AutoDeriveTypeable #-}
#endif
-----------------------------------------------------------------------------
-- |
-- Module      :  Control.Monad.Trans.RWS.Strict
-- Copyright   :  (c) Andy Gill 2001,
--                (c) Oregon Graduate Institute of Science and Technology, 2001
-- License     :  BSD-style (see the file LICENSE)
--
-- Maintainer  :  R.Paterson@city.ac.uk
-- Stability   :  experimental
-- Portability :  portable
--
-- A monad transformer that combines 'Control.Monad.Trans.Reader.ReaderT',
-- 'Control.Monad.Trans.Writer.Strict.WriterT' and
-- 'Control.Monad.Trans.State.Strict.StateT'.
-- This version is strict; for a lazy version with the same interface,
-- see "Control.Monad.Trans.RWS.Lazy".
-- Although the output is built strictly, it is not possible to
-- achieve constant space behaviour with this transformer: for that,
-- use "Control.Monad.Trans.RWS.CPS" instead.
-----------------------------------------------------------------------------

module Control.Monad.Trans.RWS.Strict (
    -- * The RWS monad
    RWS,
    rws,
    runRWS,
    evalRWS,
    execRWS,
    mapRWS,
    withRWS,
    -- * The RWST monad transformer
    RWST(..),
    evalRWST,
    execRWST,
    mapRWST,
    withRWST,
    -- * Reader operations
    reader,
    ask,
    local,
    asks,
    -- * Writer operations
    writer,
    tell,
    listen,
    listens,
    pass,
    censor,
    -- * State operations
    state,
    get,
    put,
    modify,
    gets,
    -- * Lifting other operations
    liftCallCC,
    liftCallCC',
    liftCatch,
  ) where

import Control.Monad.IO.Class
import Control.Monad.Signatures
import Control.Monad.Trans.Class
#if MIN_VERSION_base(4,12,0)
import Data.Functor.Contravariant
#endif
import Data.Functor.Identity

import Control.Applicative
import Control.Monad
#if MIN_VERSION_base(4,9,0)
import qualified Control.Monad.Fail as Fail
#endif
import Control.Monad.Fix
#if !(MIN_VERSION_base(4,8,0))
import Data.Monoid
#endif
#if __GLASGOW_HASKELL__ >= 704
import GHC.Generics
#endif

-- | A monad containing an environment of type @r@, output of type @w@
-- and an updatable state of type @s@.
type RWS r w s = RWST r w s Identity

-- | Construct an RWS computation from a function.
-- (The inverse of 'runRWS'.)
rws :: (r -> s -> (a, s, w)) -> RWS r w s a
rws :: forall r s a w. (r -> s -> (a, s, w)) -> RWS r w s a
rws r -> s -> (a, s, w)
f = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST (\ r
r s
s -> forall a. a -> Identity a
Identity (r -> s -> (a, s, w)
f r
r s
s))
{-# INLINE rws #-}

-- | Unwrap an RWS computation as a function.
-- (The inverse of 'rws'.)
runRWS :: RWS r w s a -> r -> s -> (a, s, w)
runRWS :: forall r w s a. RWS r w s a -> r -> s -> (a, s, w)
runRWS RWS r w s a
m r
r s
s = forall a. Identity a -> a
runIdentity (forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWS r w s a
m r
r s
s)
{-# INLINE runRWS #-}

-- | Evaluate a computation with the given initial state and environment,
-- returning the final value and output, discarding the final state.
evalRWS :: RWS r w s a  -- ^RWS computation to execute
        -> r            -- ^initial environment
        -> s            -- ^initial value
        -> (a, w)       -- ^final value and output
evalRWS :: forall r w s a. RWS r w s a -> r -> s -> (a, w)
evalRWS RWS r w s a
m r
r s
s = let
    (a
a, s
_, w
w) = forall r w s a. RWS r w s a -> r -> s -> (a, s, w)
runRWS RWS r w s a
m r
r s
s
    in (a
a, w
w)
{-# INLINE evalRWS #-}

-- | Evaluate a computation with the given initial state and environment,
-- returning the final state and output, discarding the final value.
execRWS :: RWS r w s a  -- ^RWS computation to execute
        -> r            -- ^initial environment
        -> s            -- ^initial value
        -> (s, w)       -- ^final state and output
execRWS :: forall r w s a. RWS r w s a -> r -> s -> (s, w)
execRWS RWS r w s a
m r
r s
s = let
    (a
_, s
s', w
w) = forall r w s a. RWS r w s a -> r -> s -> (a, s, w)
runRWS RWS r w s a
m r
r s
s
    in (s
s', w
w)
{-# INLINE execRWS #-}

-- | Map the return value, final state and output of a computation using
-- the given function.
--
-- * @'runRWS' ('mapRWS' f m) r s = f ('runRWS' m r s)@
mapRWS :: ((a, s, w) -> (b, s, w')) -> RWS r w s a -> RWS r w' s b
mapRWS :: forall a s w b w' r.
((a, s, w) -> (b, s, w')) -> RWS r w s a -> RWS r w' s b
mapRWS (a, s, w) -> (b, s, w')
f = forall (m :: * -> *) a s w (n :: * -> *) b w' r.
(m (a, s, w) -> n (b, s, w')) -> RWST r w s m a -> RWST r w' s n b
mapRWST (forall a. a -> Identity a
Identity forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, s, w) -> (b, s, w')
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Identity a -> a
runIdentity)
{-# INLINE mapRWS #-}

-- | @'withRWS' f m@ executes action @m@ with an initial environment
-- and state modified by applying @f@.
--
-- * @'runRWS' ('withRWS' f m) r s = 'uncurry' ('runRWS' m) (f r s)@
withRWS :: (r' -> s -> (r, s)) -> RWS r w s a -> RWS r' w s a
withRWS :: forall r' s r w a.
(r' -> s -> (r, s)) -> RWS r w s a -> RWS r' w s a
withRWS = forall r' s r w (m :: * -> *) a.
(r' -> s -> (r, s)) -> RWST r w s m a -> RWST r' w s m a
withRWST
{-# INLINE withRWS #-}

-- ---------------------------------------------------------------------------
-- | A monad transformer adding reading an environment of type @r@,
-- collecting an output of type @w@ and updating a state of type @s@
-- to an inner monad @m@.
newtype RWST r w s m a = RWST { forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST :: r -> s -> m (a, s, w) }
#if __GLASGOW_HASKELL__ >= 704
    deriving (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall r w s (m :: * -> *) a x.
Rep (RWST r w s m a) x -> RWST r w s m a
forall r w s (m :: * -> *) a x.
RWST r w s m a -> Rep (RWST r w s m a) x
$cto :: forall r w s (m :: * -> *) a x.
Rep (RWST r w s m a) x -> RWST r w s m a
$cfrom :: forall r w s (m :: * -> *) a x.
RWST r w s m a -> Rep (RWST r w s m a) x
Generic)
#endif

-- | Evaluate a computation with the given initial state and environment,
-- returning the final value and output, discarding the final state.
evalRWST :: (Monad m)
         => RWST r w s m a      -- ^computation to execute
         -> r                   -- ^initial environment
         -> s                   -- ^initial value
         -> m (a, w)            -- ^computation yielding final value and output
evalRWST :: forall (m :: * -> *) r w s a.
Monad m =>
RWST r w s m a -> r -> s -> m (a, w)
evalRWST RWST r w s m a
m r
r s
s = do
    (a
a, s
_, w
w) <- forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWST r w s m a
m r
r s
s
    forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, w
w)
{-# INLINE evalRWST #-}

-- | Evaluate a computation with the given initial state and environment,
-- returning the final state and output, discarding the final value.
execRWST :: (Monad m)
         => RWST r w s m a      -- ^computation to execute
         -> r                   -- ^initial environment
         -> s                   -- ^initial value
         -> m (s, w)            -- ^computation yielding final state and output
execRWST :: forall (m :: * -> *) r w s a.
Monad m =>
RWST r w s m a -> r -> s -> m (s, w)
execRWST RWST r w s m a
m r
r s
s = do
    (a
_, s
s', w
w) <- forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWST r w s m a
m r
r s
s
    forall (m :: * -> *) a. Monad m => a -> m a
return (s
s', w
w)
{-# INLINE execRWST #-}

-- | Map the inner computation using the given function.
--
-- * @'runRWST' ('mapRWST' f m) r s = f ('runRWST' m r s)@
mapRWST :: (m (a, s, w) -> n (b, s, w')) -> RWST r w s m a -> RWST r w' s n b
mapRWST :: forall (m :: * -> *) a s w (n :: * -> *) b w' r.
(m (a, s, w) -> n (b, s, w')) -> RWST r w s m a -> RWST r w' s n b
mapRWST m (a, s, w) -> n (b, s, w')
f RWST r w s m a
m = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
r s
s -> m (a, s, w) -> n (b, s, w')
f (forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWST r w s m a
m r
r s
s)
{-# INLINE mapRWST #-}

-- | @'withRWST' f m@ executes action @m@ with an initial environment
-- and state modified by applying @f@.
--
-- * @'runRWST' ('withRWST' f m) r s = 'uncurry' ('runRWST' m) (f r s)@
withRWST :: (r' -> s -> (r, s)) -> RWST r w s m a -> RWST r' w s m a
withRWST :: forall r' s r w (m :: * -> *) a.
(r' -> s -> (r, s)) -> RWST r w s m a -> RWST r' w s m a
withRWST r' -> s -> (r, s)
f RWST r w s m a
m = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r'
r s
s -> forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWST r w s m a
m) (r' -> s -> (r, s)
f r'
r s
s)
{-# INLINE withRWST #-}

instance (Functor m) => Functor (RWST r w s m) where
    fmap :: forall a b. (a -> b) -> RWST r w s m a -> RWST r w s m b
fmap a -> b
f RWST r w s m a
m = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
r s
s ->
        forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\ (a
a, s
s', w
w) -> (a -> b
f a
a, s
s', w
w)) forall a b. (a -> b) -> a -> b
$ forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWST r w s m a
m r
r s
s
    {-# INLINE fmap #-}

instance (Monoid w, Functor m, Monad m) => Applicative (RWST r w s m) where
    pure :: forall a. a -> RWST r w s m a
pure a
a = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
_ s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, s
s, forall a. Monoid a => a
mempty)
    {-# INLINE pure #-}
    RWST r -> s -> m (a -> b, s, w)
mf <*> :: forall a b.
RWST r w s m (a -> b) -> RWST r w s m a -> RWST r w s m b
<*> RWST r -> s -> m (a, s, w)
mx = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
r s
s -> do
        (a -> b
f, s
s', w
w)  <- r -> s -> m (a -> b, s, w)
mf r
r s
s
        (a
x, s
s'',w
w') <- r -> s -> m (a, s, w)
mx r
r s
s'
        forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b
f a
x, s
s'', w
w forall a. Monoid a => a -> a -> a
`mappend` w
w')
    {-# INLINE (<*>) #-}

instance (Monoid w, Functor m, MonadPlus m) => Alternative (RWST r w s m) where
    empty :: forall a. RWST r w s m a
empty = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
_ s
_ -> forall (m :: * -> *) a. MonadPlus m => m a
mzero
    {-# INLINE empty #-}
    RWST r -> s -> m (a, s, w)
m <|> :: forall a. RWST r w s m a -> RWST r w s m a -> RWST r w s m a
<|> RWST r -> s -> m (a, s, w)
n = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
r s
s -> r -> s -> m (a, s, w)
m r
r s
s forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` r -> s -> m (a, s, w)
n r
r s
s
    {-# INLINE (<|>) #-}

instance (Monoid w, Monad m) => Monad (RWST r w s m) where
#if !(MIN_VERSION_base(4,8,0))
    return a = RWST $ \ _ s -> return (a, s, mempty)
    {-# INLINE return #-}
#endif
    RWST r w s m a
m >>= :: forall a b.
RWST r w s m a -> (a -> RWST r w s m b) -> RWST r w s m b
>>= a -> RWST r w s m b
k  = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
r s
s -> do
        (a
a, s
s', w
w)  <- forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWST r w s m a
m r
r s
s
        (b
b, s
s'',w
w') <- forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST (a -> RWST r w s m b
k a
a) r
r s
s'
        forall (m :: * -> *) a. Monad m => a -> m a
return (b
b, s
s'', w
w forall a. Monoid a => a -> a -> a
`mappend` w
w')
    {-# INLINE (>>=) #-}
#if !(MIN_VERSION_base(4,13,0))
    fail msg = RWST $ \ _ _ -> fail msg
    {-# INLINE fail #-}
#endif

#if MIN_VERSION_base(4,9,0)
instance (Monoid w, Fail.MonadFail m) => Fail.MonadFail (RWST r w s m) where
    fail :: forall a. String -> RWST r w s m a
fail String
msg = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
_ s
_ -> forall (m :: * -> *) a. MonadFail m => String -> m a
Fail.fail String
msg
    {-# INLINE fail #-}
#endif

instance (Monoid w, MonadPlus m) => MonadPlus (RWST r w s m) where
    mzero :: forall a. RWST r w s m a
mzero = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
_ s
_ -> forall (m :: * -> *) a. MonadPlus m => m a
mzero
    {-# INLINE mzero #-}
    RWST r -> s -> m (a, s, w)
m mplus :: forall a. RWST r w s m a -> RWST r w s m a -> RWST r w s m a
`mplus` RWST r -> s -> m (a, s, w)
n = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
r s
s -> r -> s -> m (a, s, w)
m r
r s
s forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` r -> s -> m (a, s, w)
n r
r s
s
    {-# INLINE mplus #-}

instance (Monoid w, MonadFix m) => MonadFix (RWST r w s m) where
    mfix :: forall a. (a -> RWST r w s m a) -> RWST r w s m a
mfix a -> RWST r w s m a
f = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
r s
s -> forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix forall a b. (a -> b) -> a -> b
$ \ ~(a
a, s
_, w
_) -> forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST (a -> RWST r w s m a
f a
a) r
r s
s
    {-# INLINE mfix #-}

instance (Monoid w) => MonadTrans (RWST r w s) where
    lift :: forall (m :: * -> *) a. Monad m => m a -> RWST r w s m a
lift m a
m = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
_ s
s -> do
        a
a <- m a
m
        forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, s
s, forall a. Monoid a => a
mempty)
    {-# INLINE lift #-}

instance (Monoid w, MonadIO m) => MonadIO (RWST r w s m) where
    liftIO :: forall a. IO a -> RWST r w s m a
liftIO = 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 (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
    {-# INLINE liftIO #-}

#if MIN_VERSION_base(4,12,0)
instance Contravariant m => Contravariant (RWST r w s m) where
    contramap :: forall a' a. (a' -> a) -> RWST r w s m a -> RWST r w s m a'
contramap a' -> a
f RWST r w s m a
m = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \r
r s
s ->
      forall (f :: * -> *) a' a.
Contravariant f =>
(a' -> a) -> f a -> f a'
contramap (\ (a'
a, s
s', w
w) -> (a' -> a
f a'
a, s
s', w
w)) forall a b. (a -> b) -> a -> b
$ forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWST r w s m a
m r
r s
s
    {-# INLINE contramap #-}
#endif

-- ---------------------------------------------------------------------------
-- Reader operations

-- | Constructor for computations in the reader monad (equivalent to 'asks').
reader :: (Monoid w, Monad m) => (r -> a) -> RWST r w s m a
reader :: forall w (m :: * -> *) r a s.
(Monoid w, Monad m) =>
(r -> a) -> RWST r w s m a
reader = forall w (m :: * -> *) r a s.
(Monoid w, Monad m) =>
(r -> a) -> RWST r w s m a
asks
{-# INLINE reader #-}

-- | Fetch the value of the environment.
ask :: (Monoid w, Monad m) => RWST r w s m r
ask :: forall w (m :: * -> *) r s. (Monoid w, Monad m) => RWST r w s m r
ask = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
r s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return (r
r, s
s, forall a. Monoid a => a
mempty)
{-# INLINE ask #-}

-- | Execute a computation in a modified environment
--
-- * @'runRWST' ('local' f m) r s = 'runRWST' m (f r) s@
local :: (r -> r) -> RWST r w s m a -> RWST r w s m a
local :: forall r w s (m :: * -> *) a.
(r -> r) -> RWST r w s m a -> RWST r w s m a
local r -> r
f RWST r w s m a
m = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
r s
s -> forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWST r w s m a
m (r -> r
f r
r) s
s
{-# INLINE local #-}

-- | Retrieve a function of the current environment.
--
-- * @'asks' f = 'liftM' f 'ask'@
asks :: (Monoid w, Monad m) => (r -> a) -> RWST r w s m a
asks :: forall w (m :: * -> *) r a s.
(Monoid w, Monad m) =>
(r -> a) -> RWST r w s m a
asks r -> a
f = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
r s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return (r -> a
f r
r, s
s, forall a. Monoid a => a
mempty)
{-# INLINE asks #-}

-- ---------------------------------------------------------------------------
-- Writer operations

-- | Construct a writer computation from a (result, output) pair.
writer :: (Monad m) => (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
writer (a
a, w
w) = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
_ s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, s
s, w
w)
{-# INLINE writer #-}

-- | @'tell' w@ is an action that produces the output @w@.
tell :: (Monad m) => w -> RWST r w s m ()
tell :: forall (m :: * -> *) w r s. Monad m => w -> RWST r w s m ()
tell w
w = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
_ s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return ((),s
s,w
w)
{-# INLINE tell #-}

-- | @'listen' m@ is an action that executes the action @m@ and adds its
-- output to the value of the computation.
--
-- * @'runRWST' ('listen' m) r s = 'liftM' (\\ (a, w) -> ((a, w), w)) ('runRWST' m r s)@
listen :: (Monad m) => 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)
listen RWST r w s m a
m = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
r s
s -> do
    (a
a, s
s', w
w) <- forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWST r w s m a
m r
r s
s
    forall (m :: * -> *) a. Monad m => a -> m a
return ((a
a, w
w), s
s', w
w)
{-# INLINE listen #-}

-- | @'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)@
--
-- * @'runRWST' ('listens' f m) r s = 'liftM' (\\ (a, w) -> ((a, f w), w)) ('runRWST' m r s)@
listens :: (Monad m) => (w -> b) -> RWST r w s m a -> RWST r w s m (a, b)
listens :: forall (m :: * -> *) w b r s a.
Monad m =>
(w -> b) -> RWST r w s m a -> RWST r w s m (a, b)
listens w -> b
f RWST r w s m a
m = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
r s
s -> do
    (a
a, s
s', w
w) <- forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWST r w s m a
m r
r s
s
    forall (m :: * -> *) a. Monad m => a -> m a
return ((a
a, w -> b
f w
w), s
s', w
w)
{-# INLINE listens #-}

-- | @'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.
--
-- * @'runRWST' ('pass' m) r s = 'liftM' (\\ ((a, f), w) -> (a, f w)) ('runRWST' m r s)@
pass :: (Monad m) => 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
pass RWST r w s m (a, w -> w)
m = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
r s
s -> do
    ((a
a, w -> w
f), s
s', w
w) <- forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWST r w s m (a, w -> w)
m r
r s
s
    forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, s
s', w -> w
f w
w)
{-# INLINE pass #-}

-- | @'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)@
--
-- * @'runRWST' ('censor' f m) r s = 'liftM' (\\ (a, w) -> (a, f w)) ('runRWST' m r s)@
censor :: (Monad m) => (w -> w) -> RWST r w s m a -> RWST r w s m a
censor :: forall (m :: * -> *) w r s a.
Monad m =>
(w -> w) -> RWST r w s m a -> RWST r w s m a
censor w -> w
f RWST r w s m a
m = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
r s
s -> do
    (a
a, s
s', w
w) <- forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWST r w s m a
m r
r s
s
    forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, s
s', w -> w
f w
w)
{-# INLINE censor #-}

-- ---------------------------------------------------------------------------
-- State operations

-- | Construct a state monad computation from a state transformer function.
state :: (Monoid w, Monad m) => (s -> (a,s)) -> RWST r w s m a
state :: forall w (m :: * -> *) s a r.
(Monoid w, Monad m) =>
(s -> (a, s)) -> RWST r w s m a
state s -> (a, s)
f = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
_ s
s -> case s -> (a, s)
f s
s of (a
a,s
s') -> forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, s
s', forall a. Monoid a => a
mempty)
{-# INLINE state #-}

-- | Fetch the current value of the state within the monad.
get :: (Monoid w, Monad m) => RWST r w s m s
get :: forall w (m :: * -> *) r s. (Monoid w, Monad m) => RWST r w s m s
get = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
_ s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return (s
s, s
s, forall a. Monoid a => a
mempty)
{-# INLINE get #-}

-- | @'put' s@ sets the state within the monad to @s@.
put :: (Monoid w, Monad m) => s -> RWST r w s m ()
put :: forall w (m :: * -> *) s r.
(Monoid w, Monad m) =>
s -> RWST r w s m ()
put s
s = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
_ s
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ((), s
s, forall a. Monoid a => a
mempty)
{-# INLINE put #-}

-- | @'modify' f@ is an action that updates the state to the result of
-- applying @f@ to the current state.
--
-- * @'modify' f = 'get' >>= ('put' . f)@
modify :: (Monoid w, Monad m) => (s -> s) -> RWST r w s m ()
modify :: forall w (m :: * -> *) s r.
(Monoid w, Monad m) =>
(s -> s) -> RWST r w s m ()
modify s -> s
f = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
_ s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return ((), s -> s
f s
s, forall a. Monoid a => a
mempty)
{-# INLINE modify #-}

-- | Get a specific component of the state, using a projection function
-- supplied.
--
-- * @'gets' f = 'liftM' f 'get'@
gets :: (Monoid w, Monad m) => (s -> a) -> RWST r w s m a
gets :: forall w (m :: * -> *) s a r.
(Monoid w, Monad m) =>
(s -> a) -> RWST r w s m a
gets s -> a
f = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
_ s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return (s -> a
f s
s, s
s, forall a. Monoid a => a
mempty)
{-# INLINE gets #-}

-- | Uniform lifting of a @callCC@ operation to the new monad.
-- This version rolls back to the original state on entering the
-- continuation.
liftCallCC :: (Monoid w) =>
    CallCC m (a,s,w) (b,s,w) -> CallCC (RWST r w s m) a b
liftCallCC :: forall w (m :: * -> *) a s b r.
Monoid w =>
CallCC m (a, s, w) (b, s, w) -> CallCC (RWST r w s m) a b
liftCallCC CallCC m (a, s, w) (b, s, w)
callCC (a -> RWST r w s m b) -> RWST r w s m a
f = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
r s
s ->
    CallCC m (a, s, w) (b, s, w)
callCC forall a b. (a -> b) -> a -> b
$ \ (a, s, w) -> m (b, s, w)
c ->
    forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST ((a -> RWST r w s m b) -> RWST r w s m a
f (\ a
a -> forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
_ s
_ -> (a, s, w) -> m (b, s, w)
c (a
a, s
s, forall a. Monoid a => a
mempty))) r
r s
s
{-# INLINE liftCallCC #-}

-- | In-situ lifting of a @callCC@ operation to the new monad.
-- This version uses the current state on entering the continuation.
liftCallCC' :: (Monoid w) =>
    CallCC m (a,s,w) (b,s,w) -> CallCC (RWST r w s m) a b
liftCallCC' :: forall w (m :: * -> *) a s b r.
Monoid w =>
CallCC m (a, s, w) (b, s, w) -> CallCC (RWST r w s m) a b
liftCallCC' CallCC m (a, s, w) (b, s, w)
callCC (a -> RWST r w s m b) -> RWST r w s m a
f = forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
r s
s ->
    CallCC m (a, s, w) (b, s, w)
callCC forall a b. (a -> b) -> a -> b
$ \ (a, s, w) -> m (b, s, w)
c ->
    forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST ((a -> RWST r w s m b) -> RWST r w s m a
f (\ a
a -> forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
_ s
s' -> (a, s, w) -> m (b, s, w)
c (a
a, s
s', forall a. Monoid a => a
mempty))) r
r s
s
{-# INLINE liftCallCC' #-}

-- | Lift a @catchE@ operation to the new monad.
liftCatch :: Catch e m (a,s,w) -> Catch e (RWST r w s m) a
liftCatch :: forall e (m :: * -> *) a s w r.
Catch e m (a, s, w) -> Catch e (RWST r w s m) a
liftCatch Catch e m (a, s, w)
catchE RWST r w s m a
m e -> RWST r w s m a
h =
    forall r w s (m :: * -> *) a.
(r -> s -> m (a, s, w)) -> RWST r w s m a
RWST forall a b. (a -> b) -> a -> b
$ \ r
r s
s -> forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWST r w s m a
m r
r s
s Catch e m (a, s, w)
`catchE` \ e
e -> forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST (e -> RWST r w s m a
h e
e) r
r s
s
{-# INLINE liftCatch #-}