1 -----------------------------------------------------------------------------
3 -- Module : Control.Monad.Reader
4 -- Copyright : (c) Andy Gill 2001,
5 -- (c) Oregon Graduate Institute of Science and Technology, 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : libraries@haskell.org
9 -- Stability : experimental
10 -- Portability : non-portable (multi-param classes, functional dependencies)
12 -- The definition of the reader monad transformer.
14 -- Inspired by the paper
15 -- /Functional Programming with Overloading and
16 -- Higher-Order Polymorphism/,
17 -- Mark P Jones (<http://www.cse.ogi.edu/~mpj/>)
18 -- Advanced School of Functional Programming, 1995.
19 -----------------------------------------------------------------------------
21 module Control.Monad.X.ReaderT (
30 import Prelude (Monad(..),Functor(..),const)
31 import Control.Monad (MonadPlus(..),liftM)
33 import Control.Monad.X.Trans as T
34 import Control.Monad.X.Utils
35 import Control.Monad.X.Types(ReaderT(..))
39 -- ---------------------------------------------------------------------------
42 instance MonadTrans (ReaderT r) where
45 instance HasBaseMonad m n => HasBaseMonad (ReaderT r m) n where
48 instance Monad m => Functor (ReaderT r m) where
51 instance Monad m => Monad (ReaderT r m) where
54 m >>= f = R (\r -> (m $$ r) >>= (\a -> (f a $$ r)))
59 -- | Remove a reader layer by providing a specific value for the
61 runReader :: r -> ReaderT r m a -> m a
62 runReader r m = m $$ r
64 -- | Same as 'runReader' but with the arguments the other way around.
65 -- For backwards compatability.
66 runReaderT :: ReaderT r m a -> r -> m a
69 -- | Apply a function to underlying monad.
70 -- NOTE: SHOULD THIS BE EXPORTED?
71 mapReaderT :: (m a -> n b) -> ReaderT w m a -> ReaderT w n b
72 mapReaderT f m = R (\r -> f (m $$ r))
74 -- | A more general version of 'local' when the reader is the
76 withReaderT :: (r' -> r) -> ReaderT r m a -> ReaderT r' m a
77 withReaderT f m = R (\r -> m $$ f r)
83 -- how the features are implemented for readers
85 instance (Monad m) => MonadReader r (ReaderT r m) where
89 instance MonadWriter w m => MonadWriter w (ReaderT r m) where
91 listen = listen2' R unR (\w a -> (a,w))
93 instance MonadState s m => MonadState s (ReaderT r m) where
97 instance MonadError e m => MonadError e (ReaderT r m) where
98 throwError = throwError'
99 catchError = catchError2' R unR
101 instance MonadPlus m => MonadPlus (ReaderT r m) where
103 mplus = mplus2' R unR
105 instance (MonadNondet m) => MonadNondet (ReaderT r m) where
106 findAll = mapReaderT findAll
107 commit = mapReaderT commit
109 instance MonadResume m => MonadResume (ReaderT r m) where
110 delay = mapReaderT delay
111 force = mapReaderT force
113 instance MonadCont m => MonadCont (ReaderT r m) where
114 callCC = callCC2' R unR const