{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 702
{-# LANGUAGE Safe #-}
#endif
#if __GLASGOW_HASKELL__ >= 710
{-# LANGUAGE AutoDeriveTypeable #-}
#endif
module Control.Monad.Trans.RWS.CPS.Internal (
RWS,
rws,
runRWS,
evalRWS,
execRWS,
mapRWS,
withRWS,
RWST(..),
rwsT,
runRWST,
evalRWST,
execRWST,
mapRWST,
withRWST,
reader,
ask,
local,
asks,
writer,
tell,
listen,
listens,
pass,
censor,
state,
get,
put,
modify,
gets,
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
type RWS r w s = RWST r w s Identity
rws :: Monoid w => (r -> s -> (a, s, w)) -> RWS r w s a
rws f = RWST (\r s w -> let (a, s', w') = f r s; wt = w `mappend` w' in wt `seq` return (a, s', wt))
{-# INLINE rws #-}
runRWS :: Monoid w => RWS r w s a -> r -> s -> (a, s, w)
runRWS m r s = runIdentity (runRWST m r s)
{-# INLINE runRWS #-}
evalRWS :: Monoid w
=> RWS r w s a
-> r
-> s
-> (a, w)
evalRWS m r s = let
(a, _, w) = runRWS m r s
in (a, w)
{-# INLINE evalRWS #-}
execRWS :: Monoid w
=> RWS r w s a
-> r
-> s
-> (s, w)
execRWS m r s = let
(_, s', w) = runRWS m r s
in (s', w)
{-# INLINE execRWS #-}
mapRWS :: (Monoid w, Monoid w') => ((a, s, w) -> (b, s, w')) -> RWS r w s a -> RWS r w' s b
mapRWS f = mapRWST (Identity . f . runIdentity)
{-# INLINE mapRWS #-}
withRWS :: (r' -> s -> (r, s)) -> RWS r w s a -> RWS r' w s a
withRWS = withRWST
{-# INLINE withRWS #-}
newtype RWST r w s m a = RWST { unRWST :: r -> s -> w -> m (a, s, w) }
rwsT :: (Functor m, Monoid w) => (r -> s -> m (a, s, w)) -> RWST r w s m a
rwsT f = RWST $ \r s w -> (\(a, s', w') -> let wt = w `mappend` w' in wt `seq` (a, s', wt)) <$> f r s
{-# INLINE rwsT #-}
runRWST :: Monoid w => RWST r w s m a -> r -> s -> m (a, s, w)
runRWST m r s = unRWST m r s mempty
{-# INLINE runRWST #-}
evalRWST :: (Monad m, Monoid w)
=> RWST r w s m a
-> r
-> s
-> m (a, w)
evalRWST m r s = do
(a, _, w) <- runRWST m r s
return (a, w)
{-# INLINE evalRWST #-}
execRWST :: (Monad m, Monoid w)
=> RWST r w s m a
-> r
-> s
-> m (s, w)
execRWST m r s = do
(_, s', w) <- runRWST m r s
return (s', w)
{-# INLINE execRWST #-}
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 f m = RWST $ \r s w -> do
(a, s', w') <- f (runRWST m r s)
let wt = w `mappend` w'
wt `seq` return (a, s', wt)
{-# INLINE mapRWST #-}
withRWST :: (r' -> s -> (r, s)) -> RWST r w s m a -> RWST r' w s m a
withRWST f m = RWST $ \r s -> uncurry (unRWST m) (f r s)
{-# INLINE withRWST #-}
instance Functor m => Functor (RWST r w s m) where
fmap f m = RWST $ \r s w -> (\(a, s', w') -> (f a, s', w')) <$> unRWST m r s w
{-# INLINE fmap #-}
instance (Functor m, Monad m) => Applicative (RWST r w s m) where
pure a = RWST $ \_ s w -> return (a, s, w)
{-# INLINE pure #-}
RWST mf <*> RWST mx = RWST $ \r s w -> do
(f, s', w') <- mf r s 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 = RWST $ \_ _ _ -> mzero
{-# INLINE empty #-}
RWST m <|> RWST n = RWST $ \r s w -> m r s w `mplus` n r s 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
m >>= k = RWST $ \r s w -> do
(a, s', w') <- unRWST m r s w
unRWST (k a) r s' w'
{-# INLINE (>>=) #-}
fail msg = RWST $ \_ _ _ -> fail msg
{-# INLINE fail #-}
#if MIN_VERSION_base(4,9,0)
instance Fail.MonadFail m => Fail.MonadFail (RWST r w s m) where
fail msg = RWST $ \_ _ _ -> Fail.fail msg
{-# INLINE fail #-}
#endif
instance (Functor m, MonadPlus m) => MonadPlus (RWST r w s m) where
mzero = empty
{-# INLINE mzero #-}
mplus = (<|>)
{-# INLINE mplus #-}
instance MonadFix m => MonadFix (RWST r w s m) where
mfix f = RWST $ \r s w -> mfix $ \ ~(a, _, _) -> unRWST (f a) r s w
{-# INLINE mfix #-}
instance MonadTrans (RWST r w s) where
lift m = RWST $ \_ s w -> do
a <- m
return (a, s, w)
{-# INLINE lift #-}
instance MonadIO m => MonadIO (RWST r w s m) where
liftIO = lift . liftIO
{-# INLINE liftIO #-}
reader :: Monad m => (r -> a) -> RWST r w s m a
reader = asks
{-# INLINE reader #-}
ask :: Monad m => RWST r w s m r
ask = asks id
{-# INLINE ask #-}
local :: (r -> r) -> RWST r w s m a -> RWST r w s m a
local f m = RWST $ \r s w -> unRWST m (f r) s w
{-# INLINE local #-}
asks :: Monad m => (r -> a) -> RWST r w s m a
asks f = RWST $ \r s w -> return (f r, s, w)
{-# INLINE asks #-}
writer :: (Monoid w, Monad m) => (a, w) -> RWST r w s m a
writer (a, w') = RWST $ \_ s w -> let wt = w `mappend` w' in wt `seq` return (a, s, wt)
{-# INLINE writer #-}
tell :: (Monoid w, Monad m) => w -> RWST r w s m ()
tell w' = writer ((), w')
{-# INLINE tell #-}
listen :: (Monoid w, Monad m) => RWST r w s m a -> RWST r w s m (a, w)
listen = listens id
{-# INLINE listen #-}
listens :: (Monoid w, Monad m) => (w -> b) -> RWST r w s m a -> RWST r w s m (a, b)
listens f m = RWST $ \r s w -> do
(a, s', w') <- runRWST m r s
let wt = w `mappend` w'
wt `seq` return ((a, f w'), s', wt)
{-# INLINE listens #-}
pass :: (Monoid w, Monoid w', Monad m) => RWST r w s m (a, w -> w') -> RWST r w' s m a
pass m = RWST $ \r s w -> do
((a, f), s', w') <- runRWST m r s
let wt = w `mappend` f w'
wt `seq` return (a, s', wt)
{-# INLINE pass #-}
censor :: (Monoid w, Monad m) => (w -> w) -> RWST r w s m a -> RWST r w s m a
censor f m = RWST $ \r s w -> do
(a, s', w') <- runRWST m r s
let wt = w `mappend` f w'
wt `seq` return (a, s', wt)
{-# INLINE censor #-}
state :: Monad m => (s -> (a, s)) -> RWST r w s m a
state f = RWST $ \_ s w -> let (a, s') = f s in return (a, s', w)
{-# INLINE state #-}
get :: Monad m => RWST r w s m s
get = gets id
{-# INLINE get #-}
put :: Monad m => s -> RWST r w s m ()
put s = RWST $ \_ _ w -> return ((), s, w)
{-# INLINE put #-}
modify :: Monad m => (s -> s) -> RWST r w s m ()
modify f = RWST $ \_ s w -> return ((), f s, w)
{-# INLINE modify #-}
gets :: Monad m => (s -> a) -> RWST r w s m a
gets f = RWST $ \_ s w -> return (f s, s, w)
{-# INLINE gets #-}
liftCallCC :: CallCC m (a,s,w) (b,s,w) -> CallCC (RWST r w s m) a b
liftCallCC callCC f = RWST $ \r s w ->
callCC $ \c -> unRWST (f (\a -> RWST $ \_ _ _ -> c (a, s, w))) r s w
{-# INLINE liftCallCC #-}
liftCallCC' :: CallCC m (a,s,w) (b,s,w) -> CallCC (RWST r w s m) a b
liftCallCC' callCC f = RWST $ \r s w ->
callCC $ \c -> unRWST (f (\a -> RWST $ \_ s' _ -> c (a, s', w))) r s w
{-# INLINE liftCallCC' #-}
liftCatch :: Catch e m (a,s,w) -> Catch e (RWST r w s m) a
liftCatch catchE m h =
RWST $ \r s w -> unRWST m r s w `catchE` \e -> unRWST (h e) r s w
{-# INLINE liftCatch #-}