{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE UndecidableInstances #-}
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 __GLASGOW_HASKELL__ >= 707
{-# MINIMAL (ask | reader), local #-}
#endif
ask :: m r
ask = forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
reader forall a. a -> a
id
local :: (r -> r)
-> m a
-> m a
reader :: (r -> a)
-> m a
reader r -> a
f = do
r
r <- forall r (m :: * -> *). MonadReader r m => m r
ask
forall (m :: * -> *) a. Monad m => a -> m a
return (r -> a
f r
r)
asks :: MonadReader r m
=> (r -> a)
-> m a
asks :: forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks = forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
reader
instance MonadReader r ((->) r) where
ask :: r -> r
ask = forall a. a -> a
id
local :: forall a. (r -> r) -> (r -> a) -> r -> a
local r -> r
f r -> a
m = r -> a
m forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> r
f
reader :: forall a. (r -> a) -> r -> a
reader = forall a. a -> a
id
instance Monad m => MonadReader r (ReaderT r m) where
ask :: ReaderT r m r
ask = forall (m :: * -> *) r. Monad m => ReaderT r m r
ReaderT.ask
local :: forall a. (r -> r) -> ReaderT r m a -> ReaderT r m a
local = forall r (m :: * -> *) a.
(r -> r) -> ReaderT r m a -> ReaderT r m a
ReaderT.local
reader :: forall a. (r -> a) -> ReaderT r m a
reader = forall (m :: * -> *) r a. Monad m => (r -> a) -> ReaderT r m a
ReaderT.reader
instance (Monad m, Monoid w) => MonadReader r (LazyRWS.RWST r w s m) where
ask :: RWST r w s m r
ask = forall w (m :: * -> *) r s. (Monoid w, Monad m) => RWST r w s m r
LazyRWS.ask
local :: forall a. (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
LazyRWS.local
reader :: forall a. (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
LazyRWS.reader
instance (Monad m, Monoid w) => MonadReader r (StrictRWS.RWST r w s m) where
ask :: RWST r w s m r
ask = forall w (m :: * -> *) r s. (Monoid w, Monad m) => RWST r w s m r
StrictRWS.ask
local :: forall a. (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
StrictRWS.local
reader :: forall a. (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
StrictRWS.reader
instance MonadReader r' m => MonadReader r' (ContT r m) where
ask :: ContT r m r'
ask = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall r (m :: * -> *). MonadReader r m => m r
ask
local :: forall a. (r' -> r') -> ContT r m a -> ContT r m a
local = forall (m :: * -> *) r' r a.
Monad m =>
m r'
-> ((r' -> r') -> m r -> m r)
-> (r' -> r')
-> ContT r m a
-> ContT r m a
Cont.liftLocal forall r (m :: * -> *). MonadReader r m => m r
ask forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local
reader :: forall a. (r' -> a) -> ContT r m a
reader = 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 r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
reader
instance (Error e, MonadReader r m) => MonadReader r (ErrorT e m) where
ask :: ErrorT e m r
ask = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall r (m :: * -> *). MonadReader r m => m r
ask
local :: forall a. (r -> r) -> ErrorT e m a -> ErrorT e m a
local = forall (m :: * -> *) e a (n :: * -> *) e' b.
(m (Either e a) -> n (Either e' b))
-> ErrorT e m a -> ErrorT e' n b
mapErrorT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local
reader :: forall a. (r -> a) -> ErrorT e m a
reader = 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 r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
reader
instance MonadReader r m => MonadReader r (ExceptT e m) where
ask :: ExceptT e m r
ask = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall r (m :: * -> *). MonadReader r m => m r
ask
local :: forall a. (r -> r) -> ExceptT e m a -> ExceptT e m a
local = forall (m :: * -> *) e a (n :: * -> *) e' b.
(m (Either e a) -> n (Either e' b))
-> ExceptT e m a -> ExceptT e' n b
mapExceptT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local
reader :: forall a. (r -> a) -> ExceptT e m a
reader = 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 r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
reader
instance MonadReader r m => MonadReader r (IdentityT m) where
ask :: IdentityT m r
ask = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall r (m :: * -> *). MonadReader r m => m r
ask
local :: forall a. (r -> r) -> IdentityT m a -> IdentityT m a
local = forall {k1} {k2} (m :: k1 -> *) (a :: k1) (n :: k2 -> *) (b :: k2).
(m a -> n b) -> IdentityT m a -> IdentityT n b
mapIdentityT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local
reader :: forall a. (r -> a) -> IdentityT m a
reader = 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 r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
reader
instance MonadReader r m => MonadReader r (ListT m) where
ask :: ListT m r
ask = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall r (m :: * -> *). MonadReader r m => m r
ask
local :: forall a. (r -> r) -> ListT m a -> ListT m a
local = forall (m :: * -> *) a (n :: * -> *) b.
(m [a] -> n [b]) -> ListT m a -> ListT n b
mapListT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local
reader :: forall a. (r -> a) -> ListT m a
reader = 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 r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
reader
instance MonadReader r m => MonadReader r (MaybeT m) where
ask :: MaybeT m r
ask = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall r (m :: * -> *). MonadReader r m => m r
ask
local :: forall a. (r -> r) -> MaybeT m a -> MaybeT m a
local = forall (m :: * -> *) a (n :: * -> *) b.
(m (Maybe a) -> n (Maybe b)) -> MaybeT m a -> MaybeT n b
mapMaybeT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local
reader :: forall a. (r -> a) -> MaybeT m a
reader = 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 r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
reader
instance MonadReader r m => MonadReader r (Lazy.StateT s m) where
ask :: StateT s m r
ask = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall r (m :: * -> *). MonadReader r m => m r
ask
local :: forall a. (r -> r) -> StateT s m a -> StateT s m a
local = forall (m :: * -> *) a s (n :: * -> *) b.
(m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b
Lazy.mapStateT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local
reader :: forall a. (r -> a) -> StateT s m a
reader = 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 r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
reader
instance MonadReader r m => MonadReader r (Strict.StateT s m) where
ask :: StateT s m r
ask = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall r (m :: * -> *). MonadReader r m => m r
ask
local :: forall a. (r -> r) -> StateT s m a -> StateT s m a
local = forall (m :: * -> *) a s (n :: * -> *) b.
(m (a, s) -> n (b, s)) -> StateT s m a -> StateT s n b
Strict.mapStateT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local
reader :: forall a. (r -> a) -> StateT s m a
reader = 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 r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
reader
instance (Monoid w, MonadReader r m) => MonadReader r (Lazy.WriterT w m) where
ask :: WriterT w m r
ask = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall r (m :: * -> *). MonadReader r m => m r
ask
local :: forall a. (r -> r) -> WriterT w m a -> WriterT w m a
local = forall (m :: * -> *) a w (n :: * -> *) b w'.
(m (a, w) -> n (b, w')) -> WriterT w m a -> WriterT w' n b
Lazy.mapWriterT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local
reader :: forall a. (r -> a) -> WriterT w m a
reader = 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 r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
reader
instance (Monoid w, MonadReader r m) => MonadReader r (Strict.WriterT w m) where
ask :: WriterT w m r
ask = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall r (m :: * -> *). MonadReader r m => m r
ask
local :: forall a. (r -> r) -> WriterT w m a -> WriterT w m a
local = forall (m :: * -> *) a w (n :: * -> *) b w'.
(m (a, w) -> n (b, w')) -> WriterT w m a -> WriterT w' n b
Strict.mapWriterT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local
reader :: forall a. (r -> a) -> WriterT w m a
reader = 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 r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
reader