Movatterモバイル変換


[0]ホーム

URL:


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
LicenseBSD-style (see the file LICENSE)
Maintainer[email protected]
Stabilityexperimental
Portabilitynon-portable (multi-param classes, functional dependencies)
Safe HaskellSafe
LanguageHaskell2010

Control.Monad.Reader

Contents

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 accessto the shared environment.
Useful for:
Maintaining variable bindings, or other shared environment.
Zero and plus:
None.
Example type:
Reader [(String,Value)] a

TheReader 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.

Synopsis

MonadReader class

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.

Minimal complete definition

(ask |reader),local

Methods

ask :: m rSource#

Retrieves the monad environment.

localSource#

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.

readerSource#

Arguments

:: (r -> a)

The selector function to apply to the environment.

-> m a 

Retrieves a function of the current environment.

Instances

Instances details
MonadReader r m =>MonadReader r (MaybeT m)# 
Instance details

Defined inControl.Monad.Reader.Class

Methods

ask ::MaybeT m rSource#

local :: (r -> r) ->MaybeT m a ->MaybeT m aSource#

reader :: (r -> a) ->MaybeT m aSource#

MonadReader r m =>MonadReader r (ListT m)# 
Instance details

Defined inControl.Monad.Reader.Class

Methods

ask ::ListT m rSource#

local :: (r -> r) ->ListT m a ->ListT m aSource#

reader :: (r -> a) ->ListT m aSource#

(Monoid w,MonadReader r m) =>MonadReader r (WriterT w m)# 
Instance details

Defined inControl.Monad.Reader.Class

Methods

ask ::WriterT w m rSource#

local :: (r -> r) ->WriterT w m a ->WriterT w m aSource#

reader :: (r -> a) ->WriterT w m aSource#

(Monoid w,MonadReader r m) =>MonadReader r (WriterT w m)# 
Instance details

Defined inControl.Monad.Reader.Class

Methods

ask ::WriterT w m rSource#

local :: (r -> r) ->WriterT w m a ->WriterT w m aSource#

reader :: (r -> a) ->WriterT w m aSource#

MonadReader r m =>MonadReader r (StateT s m)# 
Instance details

Defined inControl.Monad.Reader.Class

Methods

ask ::StateT s m rSource#

local :: (r -> r) ->StateT s m a ->StateT s m aSource#

reader :: (r -> a) ->StateT s m aSource#

MonadReader r m =>MonadReader r (StateT s m)# 
Instance details

Defined inControl.Monad.Reader.Class

Methods

ask ::StateT s m rSource#

local :: (r -> r) ->StateT s m a ->StateT s m aSource#

reader :: (r -> a) ->StateT s m aSource#

MonadReader r m =>MonadReader r (IdentityT m)# 
Instance details

Defined inControl.Monad.Reader.Class

Methods

ask ::IdentityT m rSource#

local :: (r -> r) ->IdentityT m a ->IdentityT m aSource#

reader :: (r -> a) ->IdentityT m aSource#

MonadReader r m =>MonadReader r (ExceptT e m)#

Since: mtl-2.2

Instance details

Defined inControl.Monad.Reader.Class

Methods

ask ::ExceptT e m rSource#

local :: (r -> r) ->ExceptT e m a ->ExceptT e m aSource#

reader :: (r -> a) ->ExceptT e m aSource#

(Error e,MonadReader r m) =>MonadReader r (ErrorT e m)# 
Instance details

Defined inControl.Monad.Reader.Class

Methods

ask ::ErrorT e m rSource#

local :: (r -> r) ->ErrorT e m a ->ErrorT e m aSource#

reader :: (r -> a) ->ErrorT e m aSource#

Monad m =>MonadReader r (ReaderT r m)# 
Instance details

Defined inControl.Monad.Reader.Class

Methods

ask ::ReaderT r m rSource#

local :: (r -> r) ->ReaderT r m a ->ReaderT r m aSource#

reader :: (r -> a) ->ReaderT r m aSource#

MonadReader r' m =>MonadReader r' (ContT r m)# 
Instance details

Defined inControl.Monad.Reader.Class

Methods

ask ::ContT r m r'Source#

local :: (r' -> r') ->ContT r m a ->ContT r m aSource#

reader :: (r' -> a) ->ContT r m aSource#

MonadReader r ((->) r ::Type ->Type)# 
Instance details

Defined inControl.Monad.Reader.Class

Methods

ask :: r -> rSource#

local :: (r -> r) -> (r -> a) -> r -> aSource#

reader :: (r -> a) -> r -> aSource#

(Monad m,Monoid w) =>MonadReader r (RWST r w s m)# 
Instance details

Defined inControl.Monad.Reader.Class

Methods

ask ::RWST r w s m rSource#

local :: (r -> r) ->RWST r w s m a ->RWST r w s m aSource#

reader :: (r -> a) ->RWST r w s m aSource#

(Monad m,Monoid w) =>MonadReader r (RWST r w s m)# 
Instance details

Defined inControl.Monad.Reader.Class

Methods

ask ::RWST r w s m rSource#

local :: (r -> r) ->RWST r w s m a ->RWST r w s m aSource#

reader :: (r -> a) ->RWST r w s m aSource#

asksSource#

Arguments

::MonadReader r m 
=> (r -> a)

The selector function to apply to the environment.

-> m a 

Retrieves a function of the current environment.

The Reader monad

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.

runReaderSource#

Arguments

::Reader r a

AReader to run.

-> r

An initial environment.

-> a 

Runs aReader and extracts the final value from it. (The inverse ofreader.)

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

Transform the value returned by aReader.

withReaderSource#

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

The ReaderT monad transformer

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) 

Instances

Instances details
MonadError e m =>MonadError e (ReaderT r m)# 
Instance details

Defined 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)# 
Instance details

Defined inControl.Monad.Reader.Class

Methods

ask ::ReaderT r m rSource#

local :: (r -> r) ->ReaderT r m a ->ReaderT r m aSource#

reader :: (r -> a) ->ReaderT r m aSource#

MonadState s m =>MonadState s (ReaderT r m)# 
Instance details

Defined inControl.Monad.State.Class

Methods

get ::ReaderT r m sSource#

put :: s ->ReaderT r m ()Source#

state :: (s -> (a, s)) ->ReaderT r m aSource#

MonadWriter w m =>MonadWriter w (ReaderT r m)# 
Instance details

Defined inControl.Monad.Writer.Class

Methods

writer :: (a, w) ->ReaderT r m aSource#

tell :: w ->ReaderT r m ()Source#

listen ::ReaderT r m a ->ReaderT r m (a, w)Source#

pass ::ReaderT r m (a, w -> w) ->ReaderT r m aSource#

MonadTrans (ReaderT r) 
Instance details

Defined inControl.Monad.Trans.Reader

Methods

lift ::Monad m => m a ->ReaderT r m aSource#

Monad m =>Monad (ReaderT r m) 
Instance details

Defined inControl.Monad.Trans.Reader

Methods

(>>=) ::ReaderT r m a -> (a ->ReaderT r m b) ->ReaderT r m bSource#

(>>) ::ReaderT r m a ->ReaderT r m b ->ReaderT r m bSource#

return :: a ->ReaderT r m aSource#

Functor m =>Functor (ReaderT r m) 
Instance details

Defined inControl.Monad.Trans.Reader

Methods

fmap :: (a -> b) ->ReaderT r m a ->ReaderT r m bSource#

(<$) :: a ->ReaderT r m b ->ReaderT r m aSource#

MonadFix m =>MonadFix (ReaderT r m) 
Instance details

Defined inControl.Monad.Trans.Reader

Methods

mfix :: (a ->ReaderT r m a) ->ReaderT r m aSource#

MonadFail m =>MonadFail (ReaderT r m) 
Instance details

Defined inControl.Monad.Trans.Reader

Methods

fail ::String ->ReaderT r m aSource#

Applicative m =>Applicative (ReaderT r m) 
Instance details

Defined inControl.Monad.Trans.Reader

Methods

pure :: a ->ReaderT r m aSource#

(<*>) ::ReaderT r m (a -> b) ->ReaderT r m a ->ReaderT r m bSource#

liftA2 :: (a -> b -> c) ->ReaderT r m a ->ReaderT r m b ->ReaderT r m cSource#

(*>) ::ReaderT r m a ->ReaderT r m b ->ReaderT r m bSource#

(<*) ::ReaderT r m a ->ReaderT r m b ->ReaderT r m aSource#

Contravariant m =>Contravariant (ReaderT r m) 
Instance details

Defined inControl.Monad.Trans.Reader

Methods

contramap :: (a -> b) ->ReaderT r m b ->ReaderT r m aSource#

(>$) :: b ->ReaderT r m b ->ReaderT r m aSource#

MonadZip m =>MonadZip (ReaderT r m) 
Instance details

Defined inControl.Monad.Trans.Reader

Methods

mzip ::ReaderT r m a ->ReaderT r m b ->ReaderT r m (a, b)Source#

mzipWith :: (a -> b -> c) ->ReaderT r m a ->ReaderT r m b ->ReaderT r m cSource#

munzip ::ReaderT r m (a, b) -> (ReaderT r m a,ReaderT r m b)Source#

MonadIO m =>MonadIO (ReaderT r m) 
Instance details

Defined inControl.Monad.Trans.Reader

Methods

liftIO ::IO a ->ReaderT r m aSource#

Alternative m =>Alternative (ReaderT r m) 
Instance details

Defined inControl.Monad.Trans.Reader

Methods

empty ::ReaderT r m aSource#

(<|>) ::ReaderT r m a ->ReaderT r m a ->ReaderT r m aSource#

some ::ReaderT r m a ->ReaderT r m [a]Source#

many ::ReaderT r m a ->ReaderT r m [a]Source#

MonadPlus m =>MonadPlus (ReaderT r m) 
Instance details

Defined inControl.Monad.Trans.Reader

Methods

mzero ::ReaderT r m aSource#

mplus ::ReaderT r m a ->ReaderT r m a ->ReaderT r m aSource#

MonadCont m =>MonadCont (ReaderT r m)# 
Instance details

Defined inControl.Monad.Cont.Class

Methods

callCC :: ((a ->ReaderT r m b) ->ReaderT r m a) ->ReaderT r m aSource#

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.

withReaderTSource#

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

moduleControl.Monad

moduleControl.Monad.Fix

moduleControl.Monad.Trans

Example 1: Simple Reader Usage

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

Example 2: Modifying Reader Content Withlocal

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

Example 3:ReaderT Monad Transformer

Now 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


[8]ページ先頭

©2009-2026 Movatter.jp