{-# 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.CPS
-- Copyright   :  (c) Daniel Mendler 2016,
--                (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.CPS.WriterT' and
-- 'Control.Monad.Trans.State.Strict.StateT'.
-- This version uses continuation-passing-style for the writer part
-- to achieve constant space usage.
-- For a lazy version with the same interface,
-- see "Control.Monad.Trans.RWS.Lazy".
-----------------------------------------------------------------------------

module Control.Monad.Trans.RWS.CPS (
    -- * The RWS monad
    RWS,
    rws,
    runRWS,
    evalRWS,
    execRWS,
    mapRWS,
    withRWS,
    -- * The RWST monad transformer
    RWST,
    rwsT,
    runRWST,
    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.Applicative
import Control.Monad
import Control.Monad.Fix
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import Control.Monad.Signatures
import Data.Functor.Identity

#if !(MIN_VERSION_base(4,8,0))
import Data.Monoid
#endif

#if MIN_VERSION_base(4,9,0)
import qualified Control.Monad.Fail as Fail
#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 :: (Monoid w) => (r -> s -> (a, s, w)) -> RWS r w s a
rws :: forall w r s a. Monoid w => (r -> s -> (a, s, w)) -> RWS r w s a
rws r -> s -> (a, s, w)
f = (r -> s -> w -> Identity (a, s, w)) -> RWST r w s Identity a
forall r w s (m :: * -> *) a.
(r -> s -> w -> m (a, s, w)) -> RWST r w s m a
RWST ((r -> s -> w -> Identity (a, s, w)) -> RWST r w s Identity a)
-> (r -> s -> w -> Identity (a, s, w)) -> RWST r w s Identity a
forall a b. (a -> b) -> a -> b
$ \ r
r s
s w
w ->
    let (a
a, s
s', w
w') = r -> s -> (a, s, w)
f r
r s
s; wt :: w
wt = w
w w -> w -> w
forall a. Monoid a => a -> a -> a
`mappend` w
w' in w
wt w -> Identity (a, s, w) -> Identity (a, s, w)
forall a b. a -> b -> b
`seq` (a, s, w) -> Identity (a, s, w)
forall a. a -> Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, s
s', w
wt)
{-# INLINE rws #-}

-- | Unwrap an RWS computation as a function.
-- (The inverse of 'rws'.)
runRWS :: (Monoid w) => RWS r w s a -> r -> s -> (a, s, w)
runRWS :: forall w r s a. Monoid w => RWS r w s a -> r -> s -> (a, s, w)
runRWS RWS r w s a
m r
r s
s = Identity (a, s, w) -> (a, s, w)
forall a. Identity a -> a
runIdentity (RWS r w s a -> r -> s -> Identity (a, s, w)
forall w r s (m :: * -> *) a.
Monoid w =>
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 :: (Monoid w)
        => RWS r w s a  -- ^RWS computation to execute
        -> r            -- ^initial environment
        -> s            -- ^initial value
        -> (a, w)       -- ^final value and output
evalRWS :: forall w r s a. Monoid w => 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) = RWS r w s a -> r -> s -> (a, s, w)
forall w r s a. Monoid w => 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 :: (Monoid w)
        => RWS r w s a  -- ^RWS computation to execute
        -> r            -- ^initial environment
        -> s            -- ^initial value
        -> (s, w)       -- ^final state and output
execRWS :: forall w r s a. Monoid w => 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) = RWS r w s a -> r -> s -> (a, s, w)
forall w r s a. Monoid w => 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 :: (Monoid w, Monoid w') => ((a, s, w) -> (b, s, w')) -> RWS r w s a -> RWS r w' s b
mapRWS :: forall w w' a s b r.
(Monoid w, Monoid w') =>
((a, s, w) -> (b, s, w')) -> RWS r w s a -> RWS r w' s b
mapRWS (a, s, w) -> (b, s, w')
f = (Identity (a, s, w) -> Identity (b, s, w'))
-> RWST r w s Identity a -> RWST r w' s Identity b
forall (n :: * -> *) w w' (m :: * -> *) a s b r.
(Monad n, Monoid w, Monoid w') =>
(m (a, s, w) -> n (b, s, w')) -> RWST r w s m a -> RWST r w' s n b
mapRWST ((b, s, w') -> Identity (b, s, w')
forall a. a -> Identity a
Identity ((b, s, w') -> Identity (b, s, w'))
-> (Identity (a, s, w) -> (b, s, w'))
-> Identity (a, s, w)
-> Identity (b, s, w')
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, s, w) -> (b, s, w')
f ((a, s, w) -> (b, s, w'))
-> (Identity (a, s, w) -> (a, s, w))
-> Identity (a, s, w)
-> (b, s, w')
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identity (a, s, w) -> (a, s, w)
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 = (r' -> s -> (r, s))
-> RWST r w s Identity a -> RWST r' w s Identity a
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 -> w -> m (a, s, w)
unRWST :: r -> s -> w -> m (a, s, w) }
#if __GLASGOW_HASKELL__ >= 704
    deriving ((forall x. RWST r w s m a -> Rep (RWST r w s m a) x)
-> (forall x. Rep (RWST r w s m a) x -> RWST r w s m a)
-> Generic (RWST r w s m a)
forall x. Rep (RWST r w s m a) x -> RWST r w s m a
forall x. RWST r w s m a -> Rep (RWST r w s m a) x
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
$cfrom :: forall r w s (m :: * -> *) a x.
RWST r w s m a -> Rep (RWST r w s m a) x
from :: forall 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
to :: forall x. Rep (RWST r w s m a) x -> RWST r w s m a
Generic)
#endif

-- | Construct an RWST computation from a function.
-- (The inverse of 'runRWST'.)
rwsT :: (Functor m, Monoid w) => (r -> s -> m (a, s, w)) -> RWST r w s m a
rwsT :: forall (m :: * -> *) w r s a.
(Functor m, Monoid w) =>
(r -> s -> m (a, s, w)) -> RWST r w s m a
rwsT r -> s -> m (a, s, w)
f = (r -> s -> w -> m (a, s, w)) -> RWST r w s m a
forall r w s (m :: * -> *) a.
(r -> s -> w -> m (a, s, w)) -> RWST r w s m a
RWST ((r -> s -> w -> m (a, s, w)) -> RWST r w s m a)
-> (r -> s -> w -> m (a, s, w)) -> RWST r w s m a
forall a b. (a -> b) -> a -> b
$ \ r
r s
s w
w ->
     (\ (a
a, s
s', w
w') -> let wt :: w
wt = w
w w -> w -> w
forall a. Monoid a => a -> a -> a
`mappend` w
w' in w
wt w -> (a, s, w) -> (a, s, w)
forall a b. a -> b -> b
`seq` (a
a, s
s', w
wt)) ((a, s, w) -> (a, s, w)) -> m (a, s, w) -> m (a, s, w)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> r -> s -> m (a, s, w)
f r
r s
s
{-# INLINE rwsT #-}

-- | Unwrap an RWST computation as a function.
-- (The inverse of 'rwsT'.)
runRWST :: (Monoid w) => RWST r w s m a -> r -> s -> m (a, s, w)
runRWST :: forall w r s (m :: * -> *) a.
Monoid w =>
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWST r w s m a
m r
r s
s = RWST r w s m a -> r -> s -> w -> m (a, s, w)
forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> w -> m (a, s, w)
unRWST RWST r w s m a
m r
r s
s w
forall a. Monoid a => a
mempty
{-# INLINE runRWST #-}

-- | Evaluate a computation with the given initial state and environment,
-- returning the final value and output, discarding the final state.
evalRWST :: (Monad m, Monoid w)
         => 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 :: * -> *) w r s a.
(Monad m, Monoid w) =>
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, _, w) <- RWST r w s m a -> r -> s -> m (a, s, w)
forall w r s (m :: * -> *) a.
Monoid w =>
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWST r w s m a
m r
r s
s
    return (a, 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, Monoid w)
         => 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 :: * -> *) w r s a.
(Monad m, Monoid w) =>
RWST r w s m a -> r -> s -> m (s, w)
execRWST RWST r w s m a
m r
r s
s = do
    (_, s', w) <- RWST r w s m a -> r -> s -> m (a, s, w)
forall w r s (m :: * -> *) a.
Monoid w =>
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWST r w s m a
m r
r s
s
    return (s', 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 :: (Monad n, Monoid w, Monoid w') =>
    (m (a, s, w) -> n (b, s, w')) -> RWST r w s m a -> RWST r w' s n b
mapRWST :: forall (n :: * -> *) w w' (m :: * -> *) a s b r.
(Monad n, Monoid w, Monoid w') =>
(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 = (r -> s -> w' -> n (b, s, w')) -> RWST r w' s n b
forall r w s (m :: * -> *) a.
(r -> s -> w -> m (a, s, w)) -> RWST r w s m a
RWST ((r -> s -> w' -> n (b, s, w')) -> RWST r w' s n b)
-> (r -> s -> w' -> n (b, s, w')) -> RWST r w' s n b
forall a b. (a -> b) -> a -> b
$ \ r
r s
s w'
w -> do
    (a, s', w') <- m (a, s, w) -> n (b, s, w')
f (RWST r w s m a -> r -> s -> m (a, s, w)
forall w r s (m :: * -> *) a.
Monoid w =>
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWST r w s m a
m r
r s
s)
    let wt = w'
w w' -> w' -> w'
forall a. Monoid a => a -> a -> a
`mappend` w'
w'
    wt `seq` return (a, s', wt)
{-# 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 = (r' -> s -> w -> m (a, s, w)) -> RWST r' w s m a
forall r w s (m :: * -> *) a.
(r -> s -> w -> m (a, s, w)) -> RWST r w s m a
RWST ((r' -> s -> w -> m (a, s, w)) -> RWST r' w s m a)
-> (r' -> s -> w -> m (a, s, w)) -> RWST r' w s m a
forall a b. (a -> b) -> a -> b
$ \ r'
r s
s -> (r -> s -> w -> m (a, s, w)) -> (r, s) -> w -> m (a, s, w)
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (RWST r w s m a -> r -> s -> w -> m (a, s, w)
forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> w -> m (a, s, w)
unRWST 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 = (r -> s -> w -> m (b, s, w)) -> RWST r w s m b
forall r w s (m :: * -> *) a.
(r -> s -> w -> m (a, s, w)) -> RWST r w s m a
RWST ((r -> s -> w -> m (b, s, w)) -> RWST r w s m b)
-> (r -> s -> w -> m (b, s, w)) -> RWST r w s m b
forall a b. (a -> b) -> a -> b
$ \ r
r s
s w
w -> (\ (a
a, s
s', w
w') -> (a -> b
f a
a, s
s', w
w')) ((a, s, w) -> (b, s, w)) -> m (a, s, w) -> m (b, s, w)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RWST r w s m a -> r -> s -> w -> m (a, s, w)
forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> w -> m (a, s, w)
unRWST RWST r w s m a
m r
r s
s w
w
    {-# INLINE fmap #-}

instance (Functor m, Monad m) => Applicative (RWST r w s m) where
    pure :: forall a. a -> RWST r w s m a
pure a
a = (r -> s -> w -> m (a, s, w)) -> RWST r w s m a
forall r w s (m :: * -> *) a.
(r -> s -> w -> m (a, s, w)) -> RWST r w s m a
RWST ((r -> s -> w -> m (a, s, w)) -> RWST r w s m a)
-> (r -> s -> w -> m (a, s, w)) -> RWST r w s m a
forall a b. (a -> b) -> a -> b
$ \ r
_ s
s w
w -> (a, s, w) -> m (a, s, w)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, s
s, w
w)
    {-# INLINE pure #-}

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

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

    RWST r -> s -> w -> 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 -> w -> m (a, s, w)
n = (r -> s -> w -> m (a, s, w)) -> RWST r w s m a
forall r w s (m :: * -> *) a.
(r -> s -> w -> m (a, s, w)) -> RWST r w s m a
RWST ((r -> s -> w -> m (a, s, w)) -> RWST r w s m a)
-> (r -> s -> w -> m (a, s, w)) -> RWST r w s m a
forall a b. (a -> b) -> a -> b
$ \ r
r s
s w
w -> r -> s -> w -> m (a, s, w)
m r
r s
s w
w m (a, s, w) -> m (a, s, w) -> m (a, s, w)
forall a. m a -> m a -> m a
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` r -> s -> w -> m (a, s, w)
n r
r s
s w
w
    {-# INLINE (<|>) #-}

instance (Monad m) => Monad (RWST r w s m) where
#if !(MIN_VERSION_base(4,8,0))
    return a = RWST $ \ _ s w -> return (a, s, w)
    {-# 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 = (r -> s -> w -> m (b, s, w)) -> RWST r w s m b
forall r w s (m :: * -> *) a.
(r -> s -> w -> m (a, s, w)) -> RWST r w s m a
RWST ((r -> s -> w -> m (b, s, w)) -> RWST r w s m b)
-> (r -> s -> w -> m (b, s, w)) -> RWST r w s m b
forall a b. (a -> b) -> a -> b
$ \ r
r s
s w
w -> do
        (a, s', w')    <- RWST r w s m a -> r -> s -> w -> m (a, s, w)
forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> w -> m (a, s, w)
unRWST RWST r w s m a
m r
r s
s w
w
        unRWST (k a) r s' 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 (Fail.MonadFail m) => Fail.MonadFail (RWST r w s m) where
    fail :: forall a. String -> RWST r w s m a
fail String
msg = (r -> s -> w -> m (a, s, w)) -> RWST r w s m a
forall r w s (m :: * -> *) a.
(r -> s -> w -> m (a, s, w)) -> RWST r w s m a
RWST ((r -> s -> w -> m (a, s, w)) -> RWST r w s m a)
-> (r -> s -> w -> m (a, s, w)) -> RWST r w s m a
forall a b. (a -> b) -> a -> b
$ \ r
_ s
_ w
_ -> String -> m (a, s, w)
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
Fail.fail String
msg
    {-# INLINE fail #-}
#endif

instance (Functor m, MonadPlus m) => MonadPlus (RWST r w s m) where
    mzero :: forall a. RWST r w s m a
mzero = RWST r w s m a
forall a. RWST r w s m a
forall (f :: * -> *) a. Alternative f => f a
empty
    {-# INLINE mzero #-}
    mplus :: forall a. RWST r w s m a -> RWST r w s m a -> RWST r w s m a
mplus = RWST r w s m a -> RWST r w s m a -> RWST r w s m a
forall a. RWST r w s m a -> RWST r w s m a -> RWST r w s m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
    {-# INLINE mplus #-}

instance (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 = (r -> s -> w -> m (a, s, w)) -> RWST r w s m a
forall r w s (m :: * -> *) a.
(r -> s -> w -> m (a, s, w)) -> RWST r w s m a
RWST ((r -> s -> w -> m (a, s, w)) -> RWST r w s m a)
-> (r -> s -> w -> m (a, s, w)) -> RWST r w s m a
forall a b. (a -> b) -> a -> b
$ \ r
r s
s w
w -> ((a, s, w) -> m (a, s, w)) -> m (a, s, w)
forall a. (a -> m a) -> m a
forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix (((a, s, w) -> m (a, s, w)) -> m (a, s, w))
-> ((a, s, w) -> m (a, s, w)) -> m (a, s, w)
forall a b. (a -> b) -> a -> b
$ \ ~(a
a, s
_, w
_) -> RWST r w s m a -> r -> s -> w -> m (a, s, w)
forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> w -> m (a, s, w)
unRWST (a -> RWST r w s m a
f a
a) r
r s
s w
w
    {-# INLINE mfix #-}

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

instance (MonadIO m) => MonadIO (RWST r w s m) where
    liftIO :: forall a. IO a -> RWST r w s m a
liftIO = m a -> RWST r w s m a
forall (m :: * -> *) a. Monad m => m a -> RWST r w s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> RWST r w s m a) -> (IO a -> m a) -> IO a -> RWST r w s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO a -> m a
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
    {-# INLINE liftIO #-}
-- ---------------------------------------------------------------------------
-- Reader operations

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

-- | Fetch the value of the environment.
ask :: (Monad m) => RWST r w s m r
ask :: forall (m :: * -> *) r w s. Monad m => RWST r w s m r
ask = (r -> r) -> RWST r w s m r
forall (m :: * -> *) r a w s. Monad m => (r -> a) -> RWST r w s m a
asks r -> r
forall a. a -> a
id
{-# 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 = (r -> s -> w -> m (a, s, w)) -> RWST r w s m a
forall r w s (m :: * -> *) a.
(r -> s -> w -> m (a, s, w)) -> RWST r w s m a
RWST ((r -> s -> w -> m (a, s, w)) -> RWST r w s m a)
-> (r -> s -> w -> m (a, s, w)) -> RWST r w s m a
forall a b. (a -> b) -> a -> b
$ \ r
r s
s w
w -> RWST r w s m a -> r -> s -> w -> m (a, s, w)
forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> w -> m (a, s, w)
unRWST RWST r w s m a
m (r -> r
f r
r) s
s w
w
{-# INLINE local #-}

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

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

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

-- | @'tell' w@ is an action that produces the output @w@.
tell :: (Monoid w, Monad m) => w -> RWST r w s m ()
tell :: forall w (m :: * -> *) r s.
(Monoid w, Monad m) =>
w -> RWST r w s m ()
tell w
w' = ((), w) -> RWST r w s m ()
forall w (m :: * -> *) a r s.
(Monoid w, Monad m) =>
(a, w) -> RWST r w s m a
writer ((), 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 :: (Monoid w, Monad m) => 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)
listen = (w -> w) -> RWST r w s m a -> RWST r w s m (a, w)
forall w (m :: * -> *) b r s a.
(Monoid w, Monad m) =>
(w -> b) -> RWST r w s m a -> RWST r w s m (a, b)
listens w -> w
forall a. a -> a
id
{-# 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 :: (Monoid w, Monad m) => (w -> b) -> RWST r w s m a -> RWST r w s m (a, b)
listens :: forall w (m :: * -> *) b r s a.
(Monoid w, 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 = (r -> s -> w -> m ((a, b), s, w)) -> RWST r w s m (a, b)
forall r w s (m :: * -> *) a.
(r -> s -> w -> m (a, s, w)) -> RWST r w s m a
RWST ((r -> s -> w -> m ((a, b), s, w)) -> RWST r w s m (a, b))
-> (r -> s -> w -> m ((a, b), s, w)) -> RWST r w s m (a, b)
forall a b. (a -> b) -> a -> b
$ \ r
r s
s w
w -> do
    (a, s', w') <- RWST r w s m a -> r -> s -> m (a, s, w)
forall w r s (m :: * -> *) a.
Monoid w =>
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWST r w s m a
m r
r s
s
    let wt = w
w w -> w -> w
forall a. Monoid a => a -> a -> a
`mappend` w
w'
    wt `seq` return ((a, f w'), s', wt)
{-# 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 :: (Monoid w, Monoid w', Monad m) => 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
pass RWST r w s m (a, w -> w')
m = (r -> s -> w' -> m (a, s, w')) -> RWST r w' s m a
forall r w s (m :: * -> *) a.
(r -> s -> w -> m (a, s, w)) -> RWST r w s m a
RWST ((r -> s -> w' -> m (a, s, w')) -> RWST r w' s m a)
-> (r -> s -> w' -> m (a, s, w')) -> RWST r w' s m a
forall a b. (a -> b) -> a -> b
$ \ r
r s
s w'
w -> do
    ((a, f), s', w') <- RWST r w s m (a, w -> w') -> r -> s -> m ((a, w -> w'), s, w)
forall w r s (m :: * -> *) a.
Monoid w =>
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
    let wt = w'
w w' -> w' -> w'
forall a. Monoid a => a -> a -> a
`mappend` w -> w'
f w
w'
    wt `seq` return (a, s', wt)
{-# 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 :: (Monoid w, Monad m) => (w -> w) -> RWST r w s m a -> RWST r w s m a
censor :: forall w (m :: * -> *) r s a.
(Monoid w, 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 = (r -> s -> w -> m (a, s, w)) -> RWST r w s m a
forall r w s (m :: * -> *) a.
(r -> s -> w -> m (a, s, w)) -> RWST r w s m a
RWST ((r -> s -> w -> m (a, s, w)) -> RWST r w s m a)
-> (r -> s -> w -> m (a, s, w)) -> RWST r w s m a
forall a b. (a -> b) -> a -> b
$ \ r
r s
s w
w -> do
    (a, s', w') <- RWST r w s m a -> r -> s -> m (a, s, w)
forall w r s (m :: * -> *) a.
Monoid w =>
RWST r w s m a -> r -> s -> m (a, s, w)
runRWST RWST r w s m a
m r
r s
s
    let wt = w
w w -> w -> w
forall a. Monoid a => a -> a -> a
`mappend` w -> w
f w
w'
    wt `seq` return (a, s', wt)
{-# INLINE censor #-}

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

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

-- | Fetch the current value of the state within the monad.
get :: (Monad m) =>RWST r w s m s
get :: forall (m :: * -> *) r w s. Monad m => RWST r w s m s
get = (s -> s) -> RWST r w s m s
forall (m :: * -> *) s a r w. Monad m => (s -> a) -> RWST r w s m a
gets s -> s
forall a. a -> a
id
{-# INLINE get #-}

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

-- | Get a specific component of the state, using a projection function
-- supplied.
--
-- * @'gets' f = 'liftM' f 'get'@
gets :: (Monad m) =>(s -> a) -> RWST r w s m a
gets :: forall (m :: * -> *) s a r w. Monad m => (s -> a) -> RWST r w s m a
gets s -> a
f = (r -> s -> w -> m (a, s, w)) -> RWST r w s m a
forall r w s (m :: * -> *) a.
(r -> s -> w -> m (a, s, w)) -> RWST r w s m a
RWST ((r -> s -> w -> m (a, s, w)) -> RWST r w s m a)
-> (r -> s -> w -> m (a, s, w)) -> RWST r w s m a
forall a b. (a -> b) -> a -> b
$ \ r
_ s
s w
w -> (a, s, w) -> m (a, s, w)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (s -> a
f s
s, s
s, w
w)
{-# 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 :: CallCC m (a,s,w) (b,s,w) -> CallCC (RWST r w s m) a b
liftCallCC :: forall (m :: * -> *) a s w b r.
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 = (r -> s -> w -> m (a, s, w)) -> RWST r w s m a
forall r w s (m :: * -> *) a.
(r -> s -> w -> m (a, s, w)) -> RWST r w s m a
RWST ((r -> s -> w -> m (a, s, w)) -> RWST r w s m a)
-> (r -> s -> w -> m (a, s, w)) -> RWST r w s m a
forall a b. (a -> b) -> a -> b
$ \ r
r s
s w
w ->
    CallCC m (a, s, w) (b, s, w)
callCC CallCC m (a, s, w) (b, s, w) -> CallCC m (a, s, w) (b, s, w)
forall a b. (a -> b) -> a -> b
$ \ (a, s, w) -> m (b, s, w)
c -> RWST r w s m a -> r -> s -> w -> m (a, s, w)
forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> w -> m (a, s, w)
unRWST ((a -> RWST r w s m b) -> RWST r w s m a
f (\ a
a -> (r -> s -> w -> m (b, s, w)) -> RWST r w s m b
forall r w s (m :: * -> *) a.
(r -> s -> w -> m (a, s, w)) -> RWST r w s m a
RWST ((r -> s -> w -> m (b, s, w)) -> RWST r w s m b)
-> (r -> s -> w -> m (b, s, w)) -> RWST r w s m b
forall a b. (a -> b) -> a -> b
$ \ r
_ s
_ w
_ -> (a, s, w) -> m (b, s, w)
c (a
a, s
s, w
w))) r
r s
s w
w
{-# INLINE liftCallCC #-}

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

-- | Lift a @catchE@ operation to the new monad.
-- The uniformity property (see "Control.Monad.Signatures") implies that
-- the lifted @catchE@ discards any output or changes to the state from
-- the body on entering the handler.
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 =
    (r -> s -> w -> m (a, s, w)) -> RWST r w s m a
forall r w s (m :: * -> *) a.
(r -> s -> w -> m (a, s, w)) -> RWST r w s m a
RWST ((r -> s -> w -> m (a, s, w)) -> RWST r w s m a)
-> (r -> s -> w -> m (a, s, w)) -> RWST r w s m a
forall a b. (a -> b) -> a -> b
$ \ r
r s
s w
w -> RWST r w s m a -> r -> s -> w -> m (a, s, w)
forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> w -> m (a, s, w)
unRWST RWST r w s m a
m r
r s
s w
w Catch e m (a, s, w)
`catchE` \ e
e -> RWST r w s m a -> r -> s -> w -> m (a, s, w)
forall r w s (m :: * -> *) a.
RWST r w s m a -> r -> s -> w -> m (a, s, w)
unRWST (e -> RWST r w s m a
h e
e) r
r s
s w
w
{-# INLINE liftCatch #-}