| Copyright | (c) Andy Gill 2001 (c) Oregon Graduate Institute of Science and Technology 2001 (c) Jeff Newbern 2003-2007 (c) Andriy Palamarchuk 2007 |
|---|---|
| License | BSD-style (see the file LICENSE) |
| Maintainer | [email protected] |
| Stability | experimental |
| Portability | non-portable (multi-param classes, functional dependencies) |
| Safe Haskell | Safe |
| Language | Haskell2010 |
Control.Monad.Reader
Description
Reader [(String,Value)] aTheReader monad (also called the Environment monad).Represents a computation, which can read values froma shared environment, pass values from function to function,and execute sub-computations in a modified environment.UsingReader monad for such computations is often clearer and easierthan using theState monad.
Inspired by the paperFunctional Programming with Overloading and Higher-Order Polymorphism, Mark P Jones (http://web.cecs.pdx.edu/~mpj/) Advanced School of Functional Programming, 1995.
classMonad m =>MonadReader r m | m -> rwhereSource#
See examples inControl.Monad.Reader. Note, the partially applied function type(->) r is a simple reader monad. See theinstance declaration below.
Methods
Retrieves the monad environment.
Arguments
| :: (r -> r) | The function to modify the environment. |
| -> m a |
|
| -> m a |
Executes a computation in a modified environment.
Arguments
| :: (r -> a) | The selector function to apply to the environment. |
| -> m a |
Retrieves a function of the current environment.
| MonadReader r m =>MonadReader r (MaybeT m)# | |
| MonadReader r m =>MonadReader r (ListT m)# | |
| (Monoid w,MonadReader r m) =>MonadReader r (WriterT w m)# | |
| (Monoid w,MonadReader r m) =>MonadReader r (WriterT w m)# | |
| MonadReader r m =>MonadReader r (StateT s m)# | |
| MonadReader r m =>MonadReader r (StateT s m)# | |
| MonadReader r m =>MonadReader r (IdentityT m)# | |
| MonadReader r m =>MonadReader r (ExceptT e m)# | Since: mtl-2.2 |
| (Error e,MonadReader r m) =>MonadReader r (ErrorT e m)# | |
| Monad m =>MonadReader r (ReaderT r m)# | |
| MonadReader r' m =>MonadReader r' (ContT r m)# | |
| MonadReader r ((->) r ::Type ->Type)# | |
| (Monad m,Monoid w) =>MonadReader r (RWST r w s m)# | |
| (Monad m,Monoid w) =>MonadReader r (RWST r w s m)# | |
Arguments
| ::MonadReader r m | |
| => (r -> a) | The selector function to apply to the environment. |
| -> m a |
Retrieves a function of the current environment.
typeReader r =ReaderT rIdentitySource#
The parameterizable reader monad.
Computations are functions of a shared environment.
Thereturn function ignores the environment, while>>= passes the inherited environment to both subcomputations.
Arguments
| ::Reader r a | A |
| -> r | An initial environment. |
| -> a |
Runs aReader and extracts the final value from it. (The inverse ofreader.)
Arguments
| :: (r' -> r) | The function to modify the environment. |
| ->Reader r a | Computation to run in the modified environment. |
| ->Reader r' a |
Execute a computation in a modified environment (a specialization ofwithReaderT).
runReader(withReaderf m) =runReaderm . f
newtypeReaderT r (m ::Type ->Type) aSource#
The reader monad transformer, which adds a read-only environment to the given monad.
Thereturn function ignores the environment, while>>= passes the inherited environment to both subcomputations.
Constructors
| ReaderT (r -> m a) |
| MonadError e m =>MonadError e (ReaderT r m)# | |
Instance detailsDefined inControl.Monad.Error.Class Methods throwError :: e ->ReaderT r m aSource# catchError ::ReaderT r m a -> (e ->ReaderT r m a) ->ReaderT r m aSource# | |
| Monad m =>MonadReader r (ReaderT r m)# | |
| MonadState s m =>MonadState s (ReaderT r m)# | |
| MonadWriter w m =>MonadWriter w (ReaderT r m)# | |
| MonadTrans (ReaderT r) | |
| Monad m =>Monad (ReaderT r m) | |
| Functor m =>Functor (ReaderT r m) | |
| MonadFix m =>MonadFix (ReaderT r m) | |
| MonadFail m =>MonadFail (ReaderT r m) | |
| Applicative m =>Applicative (ReaderT r m) | |
| Contravariant m =>Contravariant (ReaderT r m) | |
| MonadZip m =>MonadZip (ReaderT r m) | |
| MonadIO m =>MonadIO (ReaderT r m) | |
| Alternative m =>Alternative (ReaderT r m) | |
| MonadPlus m =>MonadPlus (ReaderT r m) | |
| MonadCont m =>MonadCont (ReaderT r m)# | |
runReaderT ::ReaderT r m a -> r -> m aSource#
mapReaderT :: (m a -> n b) ->ReaderT r m a ->ReaderT r n bSource#
Transform the computation inside aReaderT.
runReaderT(mapReaderTf m) = f .runReaderTm
Arguments
| ::forall r' r (m ::Type ->Type) a. (r' -> r) | The function to modify the environment. |
| ->ReaderT r m a | Computation to run in the modified environment. |
| ->ReaderT r' m a |
Execute a computation in a modified environment (a more general version oflocal).
runReaderT(withReaderTf m) =runReaderTm . f
moduleControl.Monad
moduleControl.Monad.Fix
moduleControl.Monad.Trans
In this example theReader monad provides access to variable bindings.Bindings are aMap of integer variables.The variablecount contains number of variables in the bindings.You can see how to run a Reader monad and retrieve data from itwithrunReader, how to access the Reader data withask andasks.
type Bindings = Map String Int;-- Returns True if the "count" variable contains correct bindings size.isCountCorrect :: Bindings -> BoolisCountCorrect bindings = runReader calc_isCountCorrect bindings-- The Reader monad, which implements this complicated check.calc_isCountCorrect :: Reader Bindings Boolcalc_isCountCorrect = do count <- asks (lookupVar "count") bindings <- ask return (count == (Map.size bindings))-- The selector function to use with 'asks'.-- Returns value of the variable with specified name.lookupVar :: String -> Bindings -> IntlookupVar name bindings = maybe 0 id (Map.lookup name bindings)sampleBindings = Map.fromList [("count",3), ("1",1), ("b",2)]main = do putStr $ "Count is correct for bindings " ++ (show sampleBindings) ++ ": "; putStrLn $ show (isCountCorrect sampleBindings);localShows how to modify Reader content withlocal.
calculateContentLen :: Reader String IntcalculateContentLen = do content <- ask return (length content);-- Calls calculateContentLen after adding a prefix to the Reader content.calculateModifiedContentLen :: Reader String IntcalculateModifiedContentLen = local ("Prefix " ++) calculateContentLenmain = do let s = "12345"; let modifiedLen = runReader calculateModifiedContentLen s let len = runReader calculateContentLen s putStrLn $ "Modified 's' length: " ++ (show modifiedLen) putStrLn $ "Original 's' length: " ++ (show len)ReaderT Monad TransformerNow you are thinking: 'Wow, what a great monad! I wish I could useReader functionality in MyFavoriteComplexMonad!'. Don't worry.This can be easily done with theReaderT monad transformer.This example shows how to combineReaderT with the IO monad.
-- The Reader/IO combined monad, where Reader stores a string.printReaderContent :: ReaderT String IO ()printReaderContent = do content <- ask liftIO $ putStrLn ("The Reader Content: " ++ content)main = do runReaderT printReaderContent "Some Content"Produced byHaddock version 2.23.0