/usr/share/doc/libghc-mtl-doc/html/Control-Monad-Reader.html is in libghc-mtl-doc 2.1.1-1.
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | <!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">mtl-2.1.1: Monad classes, using functional dependencies</p></div><div id="content"><div id="module-header"><table class="info"><tr><th>Portability</th><td>non-portable (multi-param classes, functional dependencies)</td></tr><tr><th>Stability</th><td>experimental</td></tr><tr><th>Maintainer</th><td>libraries@haskell.org</td></tr><tr><th>Safe Haskell</th><td>Safe-Infered</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#t:State">State</a></code> monad.
</p><p>Inspired by the paper
<em>Functional Programming with Overloading and Higher-Order Polymorphism</em>,
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="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Control-Monad.html#t:Monad">Monad</a> m => <a href="#t:MonadReader">MonadReader</a> r m | m -> r <span class="keyword">where</span><ul class="subs"><li><a href="#v:ask">ask</a> :: m r</li><li><a href="#v:local">local</a> :: (r -> r) -> m a -> m a</li><li><a href="#v:reader">reader</a> :: (r -> 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> r m => (r -> 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="/usr/share/doc/libghc-transformers-doc/html/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="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Control-Monad.html">Control.Monad</a></li><li class="src short">module <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.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="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Control-Monad.html#t:Monad">Monad</a> m => <a name="t:MonadReader" class="def">MonadReader</a> r m | m -> r <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 methods"><p class="caption">Methods</p><p class="src"><a name="v:ask" class="def">ask</a> :: m r<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">:: (r -> r)</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><p class="src"><a name="v:reader" class="def">reader</a><a href="src/Control-Monad-Reader-Class.html#reader" class="link">Source</a></p><div class="subs arguments"><p class="caption">Arguments</p><table><tr><td class="src">:: (r -> 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><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"><a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r m => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r (<a href="/usr/share/doc/libghc-transformers-doc/html/Control-Monad-Trans-Maybe.html#t:MaybeT">MaybeT</a> m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r m => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r (<a href="Control-Monad-List.html#t:ListT">ListT</a> m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r m => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r (<a href="/usr/share/doc/libghc-transformers-doc/html/Control-Monad-Trans-Identity.html#t:IdentityT">IdentityT</a> m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r ((->) r)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Data-Monoid.html#t:Monoid">Monoid</a> w, <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r m) => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r (<a href="Control-Monad-Writer-Strict.html#t:WriterT">WriterT</a> w m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Data-Monoid.html#t:Monoid">Monoid</a> w, <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r m) => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r (<a href="Control-Monad-Writer-Lazy.html#t:WriterT">WriterT</a> w m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r m => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r (<a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r m => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r (<a href="Control-Monad-State-Lazy.html#t:StateT">StateT</a> s m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="Control-Monad-Error-Class.html#t:Error">Error</a> e, <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r m) => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r (<a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r' m => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r' (<a href="Control-Monad-Cont.html#t:ContT">ContT</a> r m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Control-Monad.html#t:Monad">Monad</a> m => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Control-Monad.html#t:Monad">Monad</a> m, <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Data-Monoid.html#t:Monoid">Monoid</a> w) => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r (<a href="Control-Monad-RWS-Strict.html#t:RWST">RWST</a> r w s m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Control-Monad.html#t:Monad">Monad</a> m, <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Data-Monoid.html#t:Monoid">Monoid</a> w) => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r (<a href="Control-Monad-RWS-Lazy.html#t:RWST">RWST</a> r w s m)</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> r m</td><td class="doc empty"> </td></tr><tr><td class="src">=> (r -> 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="/usr/share/doc/libghc-transformers-doc/html/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="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.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="/usr/share/doc/libghc-transformers-doc/html/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="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.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"><p>The underlying computation, as a function of the environment.
</p></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"><a href="Control-Monad-Writer-Class.html#t:MonadWriter">MonadWriter</a> w m => <a href="Control-Monad-Writer-Class.html#t:MonadWriter">MonadWriter</a> w (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-State-Class.html#t:MonadState">MonadState</a> s m => <a href="Control-Monad-State-Class.html#t:MonadState">MonadState</a> s (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Control-Monad.html#t:Monad">Monad</a> m => <a href="Control-Monad-Reader.html#t:MonadReader">MonadReader</a> r (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-Error-Class.html#t:MonadError">MonadError</a> e m => <a href="Control-Monad-Error-Class.html#t:MonadError">MonadError</a> e (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="/usr/share/doc/libghc-transformers-doc/html/Control-Monad-Trans-Class.html#t:MonadTrans">MonadTrans</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Control-Monad.html#t:Monad">Monad</a> m => <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Control-Monad.html#t:Monad">Monad</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Control-Monad.html#t:Functor">Functor</a> m => <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Control-Monad.html#t:Functor">Functor</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Control-Monad-Fix.html#t:MonadFix">MonadFix</a> m => <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Control-Monad-Fix.html#t:MonadFix">MonadFix</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Control-Monad.html#t:MonadPlus">MonadPlus</a> m => <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Control-Monad.html#t:MonadPlus">MonadPlus</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Control-Applicative.html#t:Applicative">Applicative</a> m => <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Control-Applicative.html#t:Applicative">Applicative</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Control-Applicative.html#t:Alternative">Alternative</a> m => <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Control-Applicative.html#t:Alternative">Alternative</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="/usr/share/doc/libghc-transformers-doc/html/Control-Monad-IO-Class.html#t:MonadIO">MonadIO</a> m => <a href="/usr/share/doc/libghc-transformers-doc/html/Control-Monad-IO-Class.html#t:MonadIO">MonadIO</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</td><td class="doc empty"> </td></tr><tr><td class="src"><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)</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">:: (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="/usr/share/doc/libghc-transformers-doc/html/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="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.0/Control-Monad.html">Control.Monad</a></p></div><div class="top"><p class="src">module <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.5.0.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.10.0</p></div></body></html>
|