mtl-tf-0.2.1.0: Monad Transformer Library with Type Families

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 libraries/base/LICENSE) libraries@haskell.org 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://www.cse.ogi.edu/~mpj/) Advanced School of Functional Programming, 1995.

Synopsis

# Documentation

type Reader r = ReaderT * r Identity #

Computations are functions of a shared environment.

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

mapReader :: (a -> b) -> Reader r a -> Reader r b #

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 k r (m :: k -> *) (a :: k) :: forall k. * -> (k -> *) -> k -> * #

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

Constructors

 ReaderT FieldsrunReaderT :: r -> m a

Instances

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 easy 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)

main = do
runReaderT printReaderContent "Some Content"