module Control.Monad.Reader.Class (
MonadReader(..),
asks,
) where
import Control.Monad.Trans.Cont as Cont
import Control.Monad.Trans.Except
import Control.Monad.Trans.Error
import Control.Monad.Trans.Identity
import Control.Monad.Trans.List
import Control.Monad.Trans.Maybe
import Control.Monad.Trans.Reader (ReaderT)
import qualified Control.Monad.Trans.Reader as ReaderT (ask, local, reader)
import qualified Control.Monad.Trans.RWS.Lazy as LazyRWS (RWST, ask, local, reader)
import qualified Control.Monad.Trans.RWS.Strict as StrictRWS (RWST, ask, local, reader)
import Control.Monad.Trans.State.Lazy as Lazy
import Control.Monad.Trans.State.Strict as Strict
import Control.Monad.Trans.Writer.Lazy as Lazy
import Control.Monad.Trans.Writer.Strict as Strict
import Control.Monad.Trans.Class (lift)
import Control.Monad
import Data.Monoid
class Monad m => MonadReader r m | m -> r where
#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 707
#endif
ask :: m r
ask = reader id
local :: (r -> r)
-> m a
-> m a
reader :: (r -> a)
-> m a
reader f = do
r <- ask
return (f r)
asks :: MonadReader r m
=> (r -> a)
-> m a
asks = reader
instance MonadReader r ((->) r) where
ask = id
local f m = m . f
reader = id
instance Monad m => MonadReader r (ReaderT r m) where
ask = ReaderT.ask
local = ReaderT.local
reader = ReaderT.reader
instance (Monad m, Monoid w) => MonadReader r (LazyRWS.RWST r w s m) where
ask = LazyRWS.ask
local = LazyRWS.local
reader = LazyRWS.reader
instance (Monad m, Monoid w) => MonadReader r (StrictRWS.RWST r w s m) where
ask = StrictRWS.ask
local = StrictRWS.local
reader = StrictRWS.reader
instance MonadReader r' m => MonadReader r' (ContT r m) where
ask = lift ask
local = Cont.liftLocal ask local
reader = lift . reader
instance (Error e, MonadReader r m) => MonadReader r (ErrorT e m) where
ask = lift ask
local = mapErrorT . local
reader = lift . reader
instance MonadReader r m => MonadReader r (ExceptT e m) where
ask = lift ask
local = mapExceptT . local
reader = lift . reader
instance MonadReader r m => MonadReader r (IdentityT m) where
ask = lift ask
local = mapIdentityT . local
reader = lift . reader
instance MonadReader r m => MonadReader r (ListT m) where
ask = lift ask
local = mapListT . local
reader = lift . reader
instance MonadReader r m => MonadReader r (MaybeT m) where
ask = lift ask
local = mapMaybeT . local
reader = lift . reader
instance MonadReader r m => MonadReader r (Lazy.StateT s m) where
ask = lift ask
local = Lazy.mapStateT . local
reader = lift . reader
instance MonadReader r m => MonadReader r (Strict.StateT s m) where
ask = lift ask
local = Strict.mapStateT . local
reader = lift . reader
instance (Monoid w, MonadReader r m) => MonadReader r (Lazy.WriterT w m) where
ask = lift ask
local = Lazy.mapWriterT . local
reader = lift . reader
instance (Monoid w, MonadReader r m) => MonadReader r (Strict.WriterT w m) where
ask = lift ask
local = Strict.mapWriterT . local
reader = lift . reader