/usr/share/doc/libghc-monads-tf-doc/html/Control-Monad-Reader.html is in libghc-monads-tf-doc 0.1.0.2-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Control.Monad.Reader</title><link href="ocean.css" rel="stylesheet" type="text/css" title="Ocean" /><script src="haddock-util.js" type="text/javascript"></script><script type="text/javascript">//<![CDATA[
window.onload = function () {pageLoad();setSynopsis("mini_Control-Monad-Reader.html");};
//]]>
</script></head><body><div id="package-header"><ul class="links" id="page-menu"><li><a href="src/Control-Monad-Reader.html">Source</a></li><li><a href="index.html">Contents</a></li><li><a href="doc-index.html">Index</a></li></ul><p class="caption">monads-tf-0.1.0.2: Monad classes, using type families</p></div><div id="content"><div id="module-header"><table class="info"><tr><th>Copyright</th><td>(c) Andy Gill 2001,
(c) Oregon Graduate Institute of Science and Technology 2001,
(c) Jeff Newbern 2003-2007,
(c) Andriy Palamarchuk 2007</td></tr><tr><th>License</th><td>BSD-style (see the file LICENSE)</td></tr><tr><th>Maintainer</th><td>ross@soi.city.ac.uk</td></tr><tr><th>Stability</th><td>experimental</td></tr><tr><th>Portability</th><td>non-portable (type families)</td></tr><tr><th>Safe Haskell</th><td>Safe</td></tr><tr><th>Language</th><td>Haskell98</td></tr></table><p class="caption">Control.Monad.Reader</p></div><div id="table-of-contents"><p class="caption">Contents</p><ul><li><a href="#g:1">MonadReader class</a></li><li><a href="#g:2">The Reader monad</a></li><li><a href="#g:3">The ReaderT monad transformer</a></li><li><a href="#g:4">Example 1: Simple Reader Usage</a></li><li><a href="#g:5">Example 2: Modifying Reader Content With <code>local</code></a></li><li><a href="#g:6">Example 3: <code>ReaderT</code> Monad Transformer</a></li></ul></div><div id="description"><p class="caption">Description</p><div class="doc"><dl><dt>Computation type:</dt><dd>Computations which read values from a shared environment.</dd><dt>Binding strategy:</dt><dd>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.</dd><dt>Useful for:</dt><dd>Maintaining variable bindings, or other shared environment.</dd><dt>Zero and plus:</dt><dd>None.</dd><dt>Example type:</dt><dd><code><code><a href="Control-Monad-Reader.html#t:Reader">Reader</a></code> [(String,Value)] a</code></dd></dl><p>The <code><a href="Control-Monad-Reader.html#t:Reader">Reader</a></code> 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 <code><a href="Control-Monad-Reader.html#t:Reader">Reader</a></code> monad for such computations is often clearer and easier
than using the <code><a href="Control-Monad-State.html#v:State">State</a></code> monad.</p><p>Inspired by the paper
/Functional Programming with Overloading and
Higher-Order Polymorphism/,
Mark P Jones (<a href="http://web.cecs.pdx.edu/~mpj/">http://web.cecs.pdx.edu/~mpj/</a>)
Advanced School of Functional Programming, 1995.</p></div></div><div id="synopsis"><p id="control.syn" class="caption expander" onclick="toggleSection('syn')">Synopsis</p><ul id="section.syn" class="hide" onclick="toggleSection('syn')"><li class="src short"><span class="keyword">class</span> <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad.html#t:Monad">Monad</a> m => <a href="#t:MonadReader">MonadReader</a> m <span class="keyword">where</span><ul class="subs"><li><span class="keyword">type</span> <a href="#t:EnvType">EnvType</a> m</li><li><a href="#v:ask">ask</a> :: m (<a href="Control-Monad-Reader.html#t:EnvType">EnvType</a> m)</li><li><a href="#v:local">local</a> :: (<a href="Control-Monad-Reader.html#t:EnvType">EnvType</a> m -> <a href="Control-Monad-Reader.html#t:EnvType">EnvType</a> m) -> m a -> m a</li></ul></li><li class="src short"><a href="#v:asks">asks</a> :: <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> m => (<a href="Control-Monad-Reader.html#t:EnvType">EnvType</a> m -> a) -> m a</li><li class="src short"><span class="keyword">type</span> <a href="#t:Reader">Reader</a> r = <a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Data-Functor-Identity.html#t:Identity">Identity</a></li><li class="src short"><a href="#v:runReader">runReader</a> :: <a href="Control-Monad-Reader.html#t:Reader">Reader</a> r a -> r -> a</li><li class="src short"><a href="#v:mapReader">mapReader</a> :: (a -> b) -> <a href="Control-Monad-Reader.html#t:Reader">Reader</a> r a -> <a href="Control-Monad-Reader.html#t:Reader">Reader</a> r b</li><li class="src short"><a href="#v:withReader">withReader</a> :: (r' -> r) -> <a href="Control-Monad-Reader.html#t:Reader">Reader</a> r a -> <a href="Control-Monad-Reader.html#t:Reader">Reader</a> r' a</li><li class="src short"><span class="keyword">newtype</span> <a href="#t:ReaderT">ReaderT</a> r m a :: * -> (* -> *) -> * -> * = <a href="#v:ReaderT">ReaderT</a> {<ul class="subs"><li><a href="#v:runReaderT">runReaderT</a> :: r -> m a</li></ul>}</li><li class="src short"><a href="#v:mapReaderT">mapReaderT</a> :: (m a -> n b) -> <a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m a -> <a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r n b</li><li class="src short"><a href="#v:withReaderT">withReaderT</a> :: (r' -> r) -> <a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m a -> <a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r' m a</li><li class="src short">module <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad.html">Control.Monad</a></li><li class="src short">module <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad-Fix.html">Control.Monad.Fix</a></li><li class="src short">module <a href="Control-Monad-Trans.html">Control.Monad.Trans</a></li></ul></div><div id="interface"><h1 id="g:1">MonadReader class</h1><div class="top"><p class="src"><span class="keyword">class</span> <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad.html#t:Monad">Monad</a> m => <a name="t:MonadReader" class="def">MonadReader</a> m <span class="keyword">where</span> <a href="src/Control-Monad-Reader-Class.html#MonadReader" class="link">Source</a></p><div class="doc"><p>See examples in <a href="Control-Monad-Reader.html">Control.Monad.Reader</a>.
Note, the partially applied function type <code>(->) r</code> is a simple reader monad.
See the <code>instance</code> declaration below.</p></div><div class="subs associated-types"><p class="caption">Associated Types</p><p class="src"><span class="keyword">type</span> <a name="t:EnvType" class="def">EnvType</a> m <a href="src/Control-Monad-Reader-Class.html#EnvType" class="link">Source</a></p></div><div class="subs methods"><p class="caption">Methods</p><p class="src"><a name="v:ask" class="def">ask</a> :: m (<a href="Control-Monad-Reader.html#t:EnvType">EnvType</a> m) <a href="src/Control-Monad-Reader-Class.html#ask" class="link">Source</a></p><div class="doc"><p>Retrieves the monad environment.</p></div><p class="src"><a name="v:local" class="def">local</a> <a href="src/Control-Monad-Reader-Class.html#local" class="link">Source</a></p><div class="subs arguments"><p class="caption">Arguments</p><table><tr><td class="src">:: (<a href="Control-Monad-Reader.html#t:EnvType">EnvType</a> m -> <a href="Control-Monad-Reader.html#t:EnvType">EnvType</a> m)</td><td class="doc"><p>The function to modify the environment.</p></td></tr><tr><td class="src">-> m a</td><td class="doc"><p><code>Reader</code> to run in the modified environment.</p></td></tr><tr><td class="src">-> m a</td><td class="doc empty"> </td></tr></table></div><div class="doc"><p>Executes a computation in a modified environment.</p></div></div><div class="subs instances"><p id="control.i:MonadReader" class="caption collapser" onclick="toggleSection('i:MonadReader')">Instances</p><div id="section.i:MonadReader" class="show"><table><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> ((->) r)</span> <a href="src/Control-Monad-Reader-Class.html#line-92" class="link">Source</a></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> m => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/transformers-0.4.2.0/Control-Monad-Trans-Maybe.html#t:MaybeT">MaybeT</a> m)</span> <a href="src/Control-Monad-Reader-Class.html#line-135" class="link">Source</a></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> m => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> (<a href="Control-Monad-List.html#t:ListT">ListT</a> m)</span> <a href="src/Control-Monad-Reader-Class.html#line-130" class="link">Source</a></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> m => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/transformers-0.4.2.0/Control-Monad-Trans-Identity.html#t:IdentityT">IdentityT</a> m)</span> <a href="src/Control-Monad-Reader-Class.html#line-125" class="link">Source</a></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left">(<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Data-Monoid.html#t:Monoid">Monoid</a> w, <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> m) => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> (<a href="Control-Monad-Writer-Strict.html#t:WriterT">WriterT</a> w m)</span> <a href="src/Control-Monad-Reader-Class.html#line-155" class="link">Source</a></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left">(<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Data-Monoid.html#t:Monoid">Monoid</a> w, <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> m) => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> (<a href="Control-Monad-Writer-Lazy.html#t:WriterT">WriterT</a> w m)</span> <a href="src/Control-Monad-Reader-Class.html#line-150" class="link">Source</a></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left">(<a href="Control-Monad-Error-Class.html#t:Error">Error</a> e, <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> m) => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> (<a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e m)</span> <a href="src/Control-Monad-Reader-Class.html#line-120" class="link">Source</a></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> m => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> (<a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m)</span> <a href="src/Control-Monad-Reader-Class.html#line-145" class="link">Source</a></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> m => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> (<a href="Control-Monad-State-Lazy.html#t:StateT">StateT</a> s m)</span> <a href="src/Control-Monad-Reader-Class.html#line-140" class="link">Source</a></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad.html#t:Monad">Monad</a> m => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</span> <a href="src/Control-Monad-Reader-Class.html#line-97" class="link">Source</a></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> m => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> (<a href="Control-Monad-Cont.html#t:ContT">ContT</a> r m)</span> <a href="src/Control-Monad-Reader-Class.html#line-115" class="link">Source</a></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left">(<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Data-Monoid.html#t:Monoid">Monoid</a> w, <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad.html#t:Monad">Monad</a> m) => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> (<a href="Control-Monad-RWS-Strict.html#t:RWST">RWST</a> r w s m)</span> <a href="src/Control-Monad-Reader-Class.html#line-107" class="link">Source</a></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left">(<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Data-Monoid.html#t:Monoid">Monoid</a> w, <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad.html#t:Monad">Monad</a> m) => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> (<a href="Control-Monad-RWS-Lazy.html#t:RWST">RWST</a> r w s m)</span> <a href="src/Control-Monad-Reader-Class.html#line-102" class="link">Source</a></td><td class="doc empty"> </td></tr></table></div></div></div><div class="top"><p class="src"><a name="v:asks" class="def">asks</a> <a href="src/Control-Monad-Reader-Class.html#asks" class="link">Source</a></p><div class="subs arguments"><p class="caption">Arguments</p><table><tr><td class="src">:: <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> m</td><td class="doc empty"> </td></tr><tr><td class="src">=> (<a href="Control-Monad-Reader.html#t:EnvType">EnvType</a> m -> a)</td><td class="doc"><p>The selector function to apply to the environment.</p></td></tr><tr><td class="src">-> m a</td><td class="doc empty"> </td></tr></table></div><div class="doc"><p>Retrieves a function of the current environment.</p></div></div><h1 id="g:2">The Reader monad</h1><div class="top"><p class="src"><span class="keyword">type</span> <a name="t:Reader" class="def">Reader</a> r = <a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Data-Functor-Identity.html#t:Identity">Identity</a></p><div class="doc"><p>The parameterizable reader monad.</p><p>Computations are functions of a shared environment.</p><p>The <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad.html#v:return">return</a></code> function ignores the environment, while <code>>>=</code> passes
the inherited environment to both subcomputations.</p></div></div><div class="top"><p class="src"><a name="v:runReader" class="def">runReader</a></p><div class="subs arguments"><p class="caption">Arguments</p><table><tr><td class="src">:: <a href="Control-Monad-Reader.html#t:Reader">Reader</a> r a</td><td class="doc"><p>A <code>Reader</code> to run.</p></td></tr><tr><td class="src">-> r</td><td class="doc"><p>An initial environment.</p></td></tr><tr><td class="src">-> a</td><td class="doc empty"> </td></tr></table></div><div class="doc"><p>Runs a <code>Reader</code> and extracts the final value from it.
(The inverse of <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/transformers-0.4.2.0/Control-Monad-Trans-Reader.html#v:reader">reader</a></code>.)</p></div></div><div class="top"><p class="src"><a name="v:mapReader" class="def">mapReader</a> :: (a -> b) -> <a href="Control-Monad-Reader.html#t:Reader">Reader</a> r a -> <a href="Control-Monad-Reader.html#t:Reader">Reader</a> r b</p><div class="doc"><p>Transform the value returned by a <code>Reader</code>.</p><ul><li><pre><code><a href="Control-Monad-Reader.html#v:runReader">runReader</a></code> (<code><a href="Control-Monad-Reader.html#v:mapReader">mapReader</a></code> f m) = f . <code><a href="Control-Monad-Reader.html#v:runReader">runReader</a></code> m</pre></li></ul></div></div><div class="top"><p class="src"><a name="v:withReader" class="def">withReader</a></p><div class="subs arguments"><p class="caption">Arguments</p><table><tr><td class="src">:: (r' -> r)</td><td class="doc"><p>The function to modify the environment.</p></td></tr><tr><td class="src">-> <a href="Control-Monad-Reader.html#t:Reader">Reader</a> r a</td><td class="doc"><p>Computation to run in the modified environment.</p></td></tr><tr><td class="src">-> <a href="Control-Monad-Reader.html#t:Reader">Reader</a> r' a</td><td class="doc empty"> </td></tr></table></div><div class="doc"><p>Execute a computation in a modified environment
(a specialization of <code><a href="Control-Monad-Reader.html#v:withReaderT">withReaderT</a></code>).</p><ul><li><pre><code><a href="Control-Monad-Reader.html#v:runReader">runReader</a></code> (<code><a href="Control-Monad-Reader.html#v:withReader">withReader</a></code> f m) = <code><a href="Control-Monad-Reader.html#v:runReader">runReader</a></code> m . f</pre></li></ul></div></div><h1 id="g:3">The ReaderT monad transformer</h1><div class="top"><p class="src"><span class="keyword">newtype</span> <a name="t:ReaderT" class="def">ReaderT</a> r m a :: * -> (* -> *) -> * -> *</p><div class="doc"><p>The reader monad transformer,
which adds a read-only environment to the given monad.</p><p>The <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad.html#v:return">return</a></code> function ignores the environment, while <code>>>=</code> passes
the inherited environment to both subcomputations.</p></div><div class="subs constructors"><p class="caption">Constructors</p><table><tr><td class="src"><a name="v:ReaderT" class="def">ReaderT</a></td><td class="doc empty"> </td></tr><tr><td colspan="2"><div class="subs fields"><p class="caption">Fields</p><dl><dt class="src"><a name="v:runReaderT" class="def">runReaderT</a> :: r -> m a</dt><dd class="doc empty"> </dd></dl><div class="clear"></div></div></td></tr></table></div><div class="subs instances"><p id="control.i:ReaderT" class="caption collapser" onclick="toggleSection('i:ReaderT')">Instances</p><div id="section.i:ReaderT" class="show"><table><tr><td class="src clearfix"><span class="inst-left"><a href="file:///usr/share/doc/ghc-doc/html/libraries/transformers-0.4.2.0/Control-Monad-Trans-Class.html#t:MonadTrans">MonadTrans</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r)</span></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad.html#t:Monad">Monad</a> m => <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad.html#t:Monad">Monad</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</span></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Data-Functor.html#t:Functor">Functor</a> m => <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Data-Functor.html#t:Functor">Functor</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</span></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad-Fix.html#t:MonadFix">MonadFix</a> m => <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad-Fix.html#t:MonadFix">MonadFix</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</span></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Applicative.html#t:Applicative">Applicative</a> m => <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Applicative.html#t:Applicative">Applicative</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</span></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Applicative.html#t:Alternative">Alternative</a> m => <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Applicative.html#t:Alternative">Alternative</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</span></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad.html#t:MonadPlus">MonadPlus</a> m => <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad.html#t:MonadPlus">MonadPlus</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</span></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="file:///usr/share/doc/ghc-doc/html/libraries/transformers-0.4.2.0/Control-Monad-IO-Class.html#t:MonadIO">MonadIO</a> m => <a href="file:///usr/share/doc/ghc-doc/html/libraries/transformers-0.4.2.0/Control-Monad-IO-Class.html#t:MonadIO">MonadIO</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</span></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-Cont-Class.html#t:MonadCont">MonadCont</a> m => <a href="Control-Monad-Cont-Class.html#t:MonadCont">MonadCont</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</span> <a href="src/Control-Monad-Cont-Class.html#line-111" class="link">Source</a></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-Error-Class.html#t:MonadError">MonadError</a> m => <a href="Control-Monad-Error-Class.html#t:MonadError">MonadError</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</span> <a href="src/Control-Monad-Error-Class.html#line-129" class="link">Source</a></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad.html#t:Monad">Monad</a> m => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</span> <a href="src/Control-Monad-Reader-Class.html#line-97" class="link">Source</a></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-State-Class.html#t:MonadState">MonadState</a> m => <a href="Control-Monad-State-Class.html#t:MonadState">MonadState</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</span> <a href="src/Control-Monad-State-Class.html#line-128" class="link">Source</a></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-Writer-Class.html#t:MonadWriter">MonadWriter</a> m => <a href="Control-Monad-Writer-Class.html#t:MonadWriter">MonadWriter</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</span> <a href="src/Control-Monad-Writer-Class.html#line-118" class="link">Source</a></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><span class="keyword">type</span> <a href="Control-Monad-Error-Class.html#t:ErrorType">ErrorType</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m) = <a href="Control-Monad-Error-Class.html#t:ErrorType">ErrorType</a> m</span> <a href="src/Control-Monad-Error-Class.html#line-130" class="link">Source</a></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><span class="keyword">type</span> <a href="Control-Monad-Reader.html#t:EnvType">EnvType</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m) = r</span> <a href="src/Control-Monad-Reader-Class.html#line-98" class="link">Source</a></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><span class="keyword">type</span> <a href="Control-Monad-State-Class.html#t:StateType">StateType</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m) = <a href="Control-Monad-State-Class.html#t:StateType">StateType</a> m</span> <a href="src/Control-Monad-State-Class.html#line-129" class="link">Source</a></td><td class="doc empty"> </td></tr><tr><td class="src clearfix"><span class="inst-left"><span class="keyword">type</span> <a href="Control-Monad-Writer-Class.html#t:WriterType">WriterType</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m) = <a href="Control-Monad-Writer-Class.html#t:WriterType">WriterType</a> m</span> <a href="src/Control-Monad-Writer-Class.html#line-119" class="link">Source</a></td><td class="doc empty"> </td></tr></table></div></div></div><div class="top"><p class="src"><a name="v:mapReaderT" class="def">mapReaderT</a> :: (m a -> n b) -> <a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m a -> <a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r n b</p><div class="doc"><p>Transform the computation inside a <code>ReaderT</code>.</p><ul><li><pre><code><a href="Control-Monad-Reader.html#v:runReaderT">runReaderT</a></code> (<code><a href="Control-Monad-Reader.html#v:mapReaderT">mapReaderT</a></code> f m) = f . <code><a href="Control-Monad-Reader.html#v:runReaderT">runReaderT</a></code> m</pre></li></ul></div></div><div class="top"><p class="src"><a name="v:withReaderT" class="def">withReaderT</a></p><div class="subs arguments"><p class="caption">Arguments</p><table><tr><td class="src">:: <span class="keyword">forall</span> (m :: * -> *). (r' -> r)</td><td class="doc"><p>The function to modify the environment.</p></td></tr><tr><td class="src">-> <a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m a</td><td class="doc"><p>Computation to run in the modified environment.</p></td></tr><tr><td class="src">-> <a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r' m a</td><td class="doc empty"> </td></tr></table></div><div class="doc"><p>Execute a computation in a modified environment
(a more general version of <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/transformers-0.4.2.0/Control-Monad-Trans-Reader.html#v:local">local</a></code>).</p><ul><li><pre><code><a href="Control-Monad-Reader.html#v:runReaderT">runReaderT</a></code> (<code><a href="Control-Monad-Reader.html#v:withReaderT">withReaderT</a></code> f m) = <code><a href="Control-Monad-Reader.html#v:runReaderT">runReaderT</a></code> m . f</pre></li></ul></div></div><div class="top"><p class="src">module <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad.html">Control.Monad</a></p></div><div class="top"><p class="src">module <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad-Fix.html">Control.Monad.Fix</a></p></div><div class="top"><p class="src">module <a href="Control-Monad-Trans.html">Control.Monad.Trans</a></p></div><h1 id="g:4">Example 1: Simple Reader Usage</h1><div class="doc"><p>In this example the <code>Reader</code> monad provides access to variable bindings.
Bindings are a <code>Map</code> of integer variables.
The variable <code>count</code> contains number of variables in the bindings.
You can see how to run a Reader monad and retrieve data from it
with <code><a href="Control-Monad-Reader.html#v:runReader">runReader</a></code>, how to access the Reader data with <code><a href="Control-Monad-Reader.html#v:ask">ask</a></code> and <code><a href="Control-Monad-Reader.html#v:asks">asks</a></code>.</p><pre> type Bindings = Map String Int;
-- Returns True if the "count" variable contains correct bindings size.
isCountCorrect :: Bindings -> Bool
isCountCorrect bindings = runReader calc_isCountCorrect bindings
-- The Reader monad, which implements this complicated check.
calc_isCountCorrect :: Reader Bindings Bool
calc_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 -> Int
lookupVar name bindings = fromJust (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);</pre></div><h1 id="g:5">Example 2: Modifying Reader Content With <code>local</code></h1><div class="doc"><p>Shows how to modify Reader content with <code><a href="Control-Monad-Reader.html#v:local">local</a></code>.</p><pre>calculateContentLen :: Reader String Int
calculateContentLen = do
content <- ask
return (length content);
-- Calls calculateContentLen after adding a prefix to the Reader content.
calculateModifiedContentLen :: Reader String Int
calculateModifiedContentLen = local ("Prefix " ++) calculateContentLen
main = 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)</pre></div><h1 id="g:6">Example 3: <code>ReaderT</code> Monad Transformer</h1><div class="doc"><p>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 <code><a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a></code> monad transformer.
This example shows how to combine <code>ReaderT</code> with the IO monad.</p><pre>-- 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"</pre></div></div></div><div id="footer"><p>Produced by <a href="http://www.haskell.org/haddock/">Haddock</a> version 2.16.1</p></div></body></html>
|