mtl-2.2.2: Monad classes, using functional dependencies

Copyright (c) Andy Gill 2001(c) Oregon Graduate Institute of Science and Technology 2001(c) Jeff Newbern 2003-2007(c) Andriy Palamarchuk 2007 BSD-style (see the file LICENSE) [email protected] experimental non-portable (multi-param classes, functional dependencies) Safe Haskell2010

Description

Computation type:
Computations which read values from a shared environment.
Binding strategy:
Monad values are functions from the environment to a value. The bound function is applied to the bound value, and both have access to the shared environment.
Useful for:
Maintaining variable bindings, or other shared environment.
Zero and plus:
None.
Example type:
Reader [(String,Value)] a

The Reader monad (also called the Environment monad). Represents a computation, which can read values from a shared environment, pass values from function to function, and execute sub-computations in a modified environment. Using Reader monad for such computations is often clearer and easier than using the State monad.

Inspired by the paper Functional Programming with Overloading and Higher-Order Polymorphism, Mark P Jones (http://web.cecs.pdx.edu/~mpj/) Advanced School of Functional Programming, 1995.

Synopsis

See examples in Control.Monad.Reader. Note, the partially applied function type (->) r is a simple reader monad. See the instance declaration below.

Minimal complete definition

Methods

ask :: m r Source #

Arguments

 :: (r -> r) The function to modify the environment. -> m a Reader to run in the modified environment. -> 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.

Instances

Arguments

 :: MonadReader r m => (r -> a) The selector function to apply to the environment. -> m a

Retrieves a function of the current environment.

Computations are functions of a shared environment.

The return function ignores the environment, while >>= passes the inherited environment to both subcomputations.

Arguments

 :: Reader r a A Reader to run. -> r An initial environment. -> a

Runs a Reader and extracts the final value from it. (The inverse of reader.)

Transform the value returned by a Reader.

• runReader (mapReader f m) = f . runReader m

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 of withReaderT).

• runReader (withReader f m) = runReader m . f

newtype ReaderT r (m :: k -> *) (a :: k) :: forall k. * -> (k -> *) -> k -> * Source #

The return function ignores the environment, while >>= passes the inherited environment to both subcomputations.
putStrLn $"Original 's' length: " ++ (show len) # Example 3: ReaderT Monad Transformer Now you are thinking: 'Wow, what a great monad! I wish I could use Reader functionality in MyFavoriteComplexMonad!'. Don't worry. This can be easily done with the ReaderT monad transformer. This example shows how to combine ReaderT 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)
runReaderT printReaderContent "Some Content"