Ticket #1410: Control-Monad-Reader.html

File Control-Monad-Reader.html, 19.3 KB (added by guest, 8 years ago)

Control.Monad.Reader haddock output.

Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<!--Rendered using the Haskell Html Library v0.2-->
3<HTML
4><HEAD
5><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"
6><TITLE
7>Control.Monad.Reader</TITLE
8>
9
10<style type="text/css">
11/* -------- Global things --------- */
12
13BODY { 
14  background-color: #ffffff;
15  color: #000000;
16  font-family: sans-serif;
17  } 
18
19A:link    { color: #0000e0; text-decoration: none }
20A:visited { color: #0000a0; text-decoration: none }
21A:hover   { background-color: #e0e0ff; text-decoration: none }
22
23TABLE.vanilla {
24  width: 100%;
25  border-width: 0px;
26  /* I can't seem to specify cellspacing or cellpadding properly using CSS... */
27}
28
29TABLE.vanilla2 {
30  border-width: 0px;
31}
32
33/* <TT> font is a little too small in MSIE */
34TT  { font-size: 100%; }
35PRE { font-size: 100%; }
36
37LI P { margin: 0pt } 
38
39TD {
40  border-width: 0px;
41}
42
43TABLE.narrow {
44  border-width: 0px;
45}
46
47TD.s8  {  height: 8px;  }
48TD.s15 {  height: 15px; }
49
50SPAN.keyword { text-decoration: underline; }
51
52/* Resize the buttom image to match the text size */
53IMG.coll { width : 0.75em; height: 0.75em; margin-bottom: 0; margin-right: 0.5em }
54
55/* --------- Contents page ---------- */
56
57DIV.node {
58  padding-left: 3em;
59}
60
61DIV.cnode {
62  padding-left: 1.75em;
63}
64
65SPAN.pkg {
66  position: absolute;
67  left: 50em;
68}
69
70/* --------- Documentation elements ---------- */
71
72TD.children {
73  padding-left: 25px;
74  }
75
76TD.synopsis {
77  padding: 2px;
78  background-color: #f0f0f0;
79  font-family: monospace
80 }
81
82TD.decl { 
83  padding: 2px;
84  background-color: #f0f0f0; 
85  font-family: monospace;
86  vertical-align: top;
87  }
88
89/*
90  arg is just like decl, except that wrapping is not allowed.  It is
91  used for function and constructor arguments which have a text box
92  to the right, where if wrapping is allowed the text box squashes up
93  the declaration by wrapping it.
94*/
95TD.arg { 
96  padding: 2px;
97  background-color: #f0f0f0; 
98  font-family: monospace;
99  vertical-align: top;
100  white-space: nowrap;
101  }
102
103TD.recfield { padding-left: 20px }
104
105TD.doc  { 
106  padding-top: 2px;
107  padding-left: 10px;
108  }
109
110TD.ndoc  { 
111  padding: 2px;
112  }
113
114TD.rdoc  { 
115  padding: 2px;
116  padding-left: 10px;
117  width: 100%;
118  }
119
120TD.body  { 
121  padding-left: 10px
122  }
123
124TD.pkg {
125  width: 100%;
126  padding-left: 10px
127}
128
129TD.indexentry {
130  vertical-align: top;
131  padding-right: 10px
132  }
133
134TD.indexannot {
135  vertical-align: top;
136  padding-left: 20px;
137  white-space: nowrap
138  }
139
140TD.indexlinks {
141  width: 100%
142  }
143
144/* ------- Section Headings ------- */
145
146TD.section1 {
147  padding-top: 15px;
148  font-weight: bold;
149  font-size: 150%
150  }
151
152TD.section2 {
153  padding-top: 10px;
154  font-weight: bold;
155  font-size: 130%
156  }
157
158TD.section3 {
159  padding-top: 5px;
160  font-weight: bold;
161  font-size: 110%
162  }
163
164TD.section4 {
165  font-weight: bold;
166  font-size: 100%
167  }
168
169/* -------------- The title bar at the top of the page */
170
171TD.infohead {
172  color: #ffffff;
173  font-weight: bold;
174  padding-right: 10px;
175  text-align: left;
176}
177
178TD.infoval {
179  color: #ffffff;
180  padding-right: 10px;
181  text-align: left;
182}
183
184TD.topbar {
185  background-color: #000099;
186  padding: 5px;
187}
188
189TD.title {
190  color: #ffffff;
191  padding-left: 10px;
192  width: 100%
193  }
194
195TD.topbut {
196  padding-left: 5px;
197  padding-right: 5px;
198  border-left-width: 1px;
199  border-left-color: #ffffff;
200  border-left-style: solid;
201  white-space: nowrap;
202  }
203
204TD.topbut A:link {
205  color: #ffffff
206  }
207
208TD.topbut A:visited {
209  color: #ffff00
210  }
211
212TD.topbut A:hover {
213  background-color: #6060ff;
214  }
215
216TD.topbut:hover {
217  background-color: #6060ff
218  }
219
220TD.modulebar { 
221  background-color: #0077dd;
222  padding: 5px;
223  border-top-width: 1px;
224  border-top-color: #ffffff;
225  border-top-style: solid;
226  }
227
228/* --------- The page footer --------- */
229
230TD.botbar {
231  background-color: #000099;
232  color: #ffffff;
233  padding: 5px
234  }
235TD.botbar A:link {
236  color: #ffffff;
237  text-decoration: underline
238  }
239TD.botbar A:visited {
240  color: #ffff00
241  }
242TD.botbar A:hover {
243  background-color: #6060ff
244  }
245
246</style>
247
248<SCRIPT SRC="haddock.js" TYPE="text/javascript"
249></SCRIPT
250></HEAD
251><BODY
252><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
253><TR
254><TD CLASS="topbar"
255><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
256><TR
257><TD
258><IMG SRC="haskell_icon.gif" WIDTH="16" HEIGHT="16" ALT=" "
259></TD
260><TD CLASS="title"
261></TD
262><TD CLASS="topbut"
263><A HREF="index.html"
264>Contents</A
265></TD
266><TD CLASS="topbut"
267><A HREF="doc-index.html"
268>Index</A
269></TD
270></TR
271></TABLE
272></TD
273></TR
274><TR
275><TD CLASS="modulebar"
276><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
277><TR
278><TD
279><FONT SIZE="6"
280>Control.Monad.Reader</FONT
281></TD
282><TD ALIGN="right"
283><TABLE CLASS="narrow" CELLSPACING="0" CELLPADDING="0"
284><TR
285><TD CLASS="infohead"
286>Portability</TD
287><TD CLASS="infoval"
288>non-portable (multi-param classes, functional dependencies)</TD
289></TR
290><TR
291><TD CLASS="infohead"
292>Stability</TD
293><TD CLASS="infoval"
294>experimental</TD
295></TR
296><TR
297><TD CLASS="infohead"
298>Maintainer</TD
299><TD CLASS="infoval"
300>[email protected]</TD
301></TR
302></TABLE
303></TD
304></TR
305></TABLE
306></TD
307></TR
308><TR
309><TD CLASS="s15"
310></TD
311></TR
312><TR
313><TD
314><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
315><TR
316><TD CLASS="section4"
317><B
318>Contents</B
319></TD
320></TR
321><TR
322><TD
323><DL
324><DT
325><A HREF="#1"
326>Example 1: Simple Reader Usage
327</A
328></DT
329><DT
330><A HREF="#2"
331>Example 2: Modifying Reader Content With <TT
332>local</TT
333>
334</A
335></DT
336><DT
337><A HREF="#3"
338>Example 3: <TT
339>ReaderT</TT
340> Monad Transformer
341</A
342></DT
343></DL
344></TD
345></TR
346></TABLE
347></TD
348></TR
349><TR
350><TD CLASS="s15"
351></TD
352></TR
353><TR
354><TD CLASS="section1"
355>Description</TD
356></TR
357><TR
358><TD CLASS="doc"
359><DL
360><DT
361>Computation type:</DT
362><DD
363> Computations which read values from a shared environment.
364</DD
365><DT
366>Binding strategy:</DT
367><DD
368> Monad values are functions from the environment to a value.
369The bound function is applied to the bound value, and both have access
370to the shared environment.
371</DD
372><DT
373>Useful for:</DT
374><DD
375> Maintaining variable bindings, or other shared environment.
376</DD
377><DT
378>Zero and plus:</DT
379><DD
380> None.
381</DD
382><DT
383>Example type:</DT
384><DD
385> <TT
386><TT
387><A HREF="Control-Monad-Reader.html#t%3AReader"
388>Reader</A
389></TT
390> [(String,Value)] a</TT
391>
392</DD
393></DL
394><P
395>The <TT
396><A HREF="Control-Monad-Reader.html#t%3AReader"
397>Reader</A
398></TT
399> monad (also called the Environment monad).
400Represents a computation, which can read values from
401a shared environment, pass values from function to function,
402and execute sub-computations in a modified environment.
403Using <TT
404><A HREF="Control-Monad-Reader.html#t%3AReader"
405>Reader</A
406></TT
407> monad for such computations is often clearer and easier
408than using the <TT
409><A HREF="Control-Monad-State.html#t%3AState"
410>State</A
411></TT
412> monad.
413</P
414><P
415>Inspired by the paper
416  <EM
417>Functional Programming with Overloading and
418      Higher-Order Polymorphism</EM
419>,
420    Mark P Jones (<A HREF="http://www.cse.ogi.edu/~mpj/"
421>http://www.cse.ogi.edu/~mpj/</A
422>)
423    Advanced School of Functional Programming, 1995.
424</P
425></TD
426></TR
427><TR
428><TD CLASS="s15"
429></TD
430></TR
431><TR
432><TD CLASS="section1"
433>Synopsis</TD
434></TR
435><TR
436><TD CLASS="s15"
437></TD
438></TR
439><TR
440><TD CLASS="body"
441><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
442><TR
443><TD CLASS="decl"
444>module <A HREF="Control-Monad-Reader-Class.html"
445>Control.Monad.Reader.Class</A
446></TD
447></TR
448><TR
449><TD CLASS="s8"
450></TD
451></TR
452><TR
453><TD CLASS="decl"
454><SPAN CLASS="keyword"
455>newtype</SPAN
456> <A HREF="#t%3AReader"
457>Reader</A
458> r a = <A HREF="#v%3AReader"
459>Reader</A
460> {<TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
461><TR
462><TD CLASS="recfield"
463><A HREF="#v%3ArunReader"
464>runReader</A
465> :: (r -&gt; a)</TD
466></TR
467></TABLE
468>}</TD
469></TR
470><TR
471><TD CLASS="s8"
472></TD
473></TR
474><TR
475><TD CLASS="decl"
476><A HREF="#v%3AmapReader"
477>mapReader</A
478> :: (a -&gt; b) -&gt; <A HREF="Control-Monad-Reader.html#t%3AReader"
479>Reader</A
480> r a -&gt; <A HREF="Control-Monad-Reader.html#t%3AReader"
481>Reader</A
482> r b</TD
483></TR
484><TR
485><TD CLASS="s8"
486></TD
487></TR
488><TR
489><TD CLASS="decl"
490><A HREF="#v%3AwithReader"
491>withReader</A
492> :: (r' -&gt; r) -&gt; <A HREF="Control-Monad-Reader.html#t%3AReader"
493>Reader</A
494> r a -&gt; <A HREF="Control-Monad-Reader.html#t%3AReader"
495>Reader</A
496> r' a</TD
497></TR
498><TR
499><TD CLASS="s8"
500></TD
501></TR
502><TR
503><TD CLASS="decl"
504><SPAN CLASS="keyword"
505>newtype</SPAN
506> <A HREF="#t%3AReaderT"
507>ReaderT</A
508> r m a = <A HREF="#v%3AReaderT"
509>ReaderT</A
510> {<TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
511><TR
512><TD CLASS="recfield"
513><A HREF="#v%3ArunReaderT"
514>runReaderT</A
515> :: (r -&gt; m a)</TD
516></TR
517></TABLE
518>}</TD
519></TR
520><TR
521><TD CLASS="s8"
522></TD
523></TR
524><TR
525><TD CLASS="decl"
526><A HREF="#v%3AmapReaderT"
527>mapReaderT</A
528> :: (m a -&gt; n b) -&gt; <A HREF="Control-Monad-Reader.html#t%3AReaderT"
529>ReaderT</A
530> w m a -&gt; <A HREF="Control-Monad-Reader.html#t%3AReaderT"
531>ReaderT</A
532> w n b</TD
533></TR
534><TR
535><TD CLASS="s8"
536></TD
537></TR
538><TR
539><TD CLASS="decl"
540><A HREF="#v%3AwithReaderT"
541>withReaderT</A
542> :: (r' -&gt; r) -&gt; <A HREF="Control-Monad-Reader.html#t%3AReaderT"
543>ReaderT</A
544> r m a -&gt; <A HREF="Control-Monad-Reader.html#t%3AReaderT"
545>ReaderT</A
546> r' m a</TD
547></TR
548><TR
549><TD CLASS="s8"
550></TD
551></TR
552><TR
553><TD CLASS="decl"
554>module <A HREF="Control-Monad-Trans.html"
555>Control.Monad.Trans</A
556></TD
557></TR
558></TABLE
559></TD
560></TR
561><TR
562><TD CLASS="s15"
563></TD
564></TR
565><TR
566><TD CLASS="section1"
567>Documentation</TD
568></TR
569><TR
570><TD CLASS="s15"
571></TD
572></TR
573><TR
574><TD CLASS="decl"
575>module <A HREF="Control-Monad-Reader-Class.html"
576>Control.Monad.Reader.Class</A
577></TD
578></TR
579><TR
580><TD CLASS="s15"
581></TD
582></TR
583><TR
584><TD CLASS="decl"
585><SPAN CLASS="keyword"
586>newtype</SPAN
587> <A NAME="t%3AReader"
588></A
589><B
590>Reader</B
591> r a</TD
592></TR
593><TR
594><TD CLASS="body"
595><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
596><TR
597><TD CLASS="ndoc"
598><P
599>The parameterizable reader monad.
600</P
601><P
602>The <TT
603>return</TT
604> function creates a <TT
605>Reader</TT
606> that ignores the environment,
607and produces the given value.
608</P
609><P
610>The binding operator <TT
611>&gt;&gt;=</TT
612> produces a <TT
613>Reader</TT
614> that uses the environment
615to extract the value its left-hand side,
616and then applies the bound function to that value in the same environment.
617</P
618></TD
619></TR
620><TR
621><TD CLASS="section4"
622>Constructors</TD
623></TR
624><TR
625><TD CLASS="body"
626><TABLE CLASS="vanilla" CELLSPACING="5" CELLPADDING="0"
627><TR
628><TD CLASS="arg"
629><A NAME="v%3AReader"
630></A
631><B
632>Reader</B
633></TD
634><TD CLASS="rdoc"
635></TD
636></TR
637><TR
638><TD CLASS="body" COLSPAN="2"
639><TABLE CLASS="vanilla" CELLSPACING="1" CELLPADDING="0"
640><TR
641><TD CLASS="arg"
642><A NAME="v%3ArunReader"
643></A
644><B
645>runReader</B
646> :: (r -&gt; a)</TD
647><TD CLASS="rdoc"
648><P
649>Runs <TT
650>Reader</TT
651> and extracts the final value from it.
652    To extract the value apply <TT
653>(runReader reader)</TT
654> to an environment value. 
655    Parameters:
656</P
657><UL
658><LI
659> A <TT
660>Reader</TT
661> to run.
662</LI
663><LI
664> An initial environment.
665</LI
666></UL
667></TD
668></TR
669></TABLE
670></TD
671></TR
672></TABLE
673></TD
674></TR
675><TR
676><TD CLASS="section4"
677><IMG SRC="minus.gif" CLASS="coll" ONCLICK="toggle(this,'i:Reader')" ALT="show/hide"
678> Instances</TD
679></TR
680><TR
681><TD CLASS="body"
682><DIV ID="i:Reader" STYLE="display:block;"
683><TABLE CLASS="vanilla" CELLSPACING="1" CELLPADDING="0"
684><TR
685><TD CLASS="decl"
686><A HREF="Control-Monad-Reader-Class.html#t%3AMonadReader"
687>MonadReader</A
688> r (<A HREF="Control-Monad-Reader.html#t%3AReader"
689>Reader</A
690> r)</TD
691></TR
692><TR
693><TD CLASS="decl"
694>Functor (<A HREF="Control-Monad-Reader.html#t%3AReader"
695>Reader</A
696> r)</TD
697></TR
698><TR
699><TD CLASS="decl"
700>Monad (<A HREF="Control-Monad-Reader.html#t%3AReader"
701>Reader</A
702> r)</TD
703></TR
704><TR
705><TD CLASS="decl"
706>MonadFix (<A HREF="Control-Monad-Reader.html#t%3AReader"
707>Reader</A
708> r)</TD
709></TR
710></TABLE
711></DIV
712></TD
713></TR
714></TABLE
715></TD
716></TR
717><TR
718><TD CLASS="s15"
719></TD
720></TR
721><TR
722><TD CLASS="decl"
723><A NAME="v%3AmapReader"
724></A
725><B
726>mapReader</B
727> :: (a -&gt; b) -&gt; <A HREF="Control-Monad-Reader.html#t%3AReader"
728>Reader</A
729> r a -&gt; <A HREF="Control-Monad-Reader.html#t%3AReader"
730>Reader</A
731> r b</TD
732></TR
733><TR
734><TD CLASS="s15"
735></TD
736></TR
737><TR
738><TD CLASS="decl"
739><A NAME="v%3AwithReader"
740></A
741><B
742>withReader</B
743> :: (r' -&gt; r) -&gt; <A HREF="Control-Monad-Reader.html#t%3AReader"
744>Reader</A
745> r a -&gt; <A HREF="Control-Monad-Reader.html#t%3AReader"
746>Reader</A
747> r' a</TD
748></TR
749><TR
750><TD CLASS="doc"
751>A more general version of <TT
752><A HREF="Control-Monad-Reader-Class.html#v%3Alocal"
753>local</A
754></TT
755>.
756</TD
757></TR
758><TR
759><TD CLASS="s15"
760></TD
761></TR
762><TR
763><TD CLASS="decl"
764><SPAN CLASS="keyword"
765>newtype</SPAN
766> <A NAME="t%3AReaderT"
767></A
768><B
769>ReaderT</B
770> r m a</TD
771></TR
772><TR
773><TD CLASS="body"
774><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
775><TR
776><TD CLASS="ndoc"
777>The reader monad transformer.
778Can be used to add environment reading functionality to other monads.
779</TD
780></TR
781><TR
782><TD CLASS="section4"
783>Constructors</TD
784></TR
785><TR
786><TD CLASS="body"
787><TABLE CLASS="vanilla" CELLSPACING="5" CELLPADDING="0"
788><TR
789><TD CLASS="arg"
790><A NAME="v%3AReaderT"
791></A
792><B
793>ReaderT</B
794></TD
795><TD CLASS="rdoc"
796></TD
797></TR
798><TR
799><TD CLASS="body" COLSPAN="2"
800><TABLE CLASS="vanilla" CELLSPACING="1" CELLPADDING="0"
801><TR
802><TD CLASS="arg"
803><A NAME="v%3ArunReaderT"
804></A
805><B
806>runReaderT</B
807> :: (r -&gt; m a)</TD
808><TD CLASS="rdoc"
809></TD
810></TR
811></TABLE
812></TD
813></TR
814></TABLE
815></TD
816></TR
817><TR
818><TD CLASS="section4"
819><IMG SRC="minus.gif" CLASS="coll" ONCLICK="toggle(this,'i:ReaderT')" ALT="show/hide"
820> Instances</TD
821></TR
822><TR
823><TD CLASS="body"
824><DIV ID="i:ReaderT" STYLE="display:block;"
825><TABLE CLASS="vanilla" CELLSPACING="1" CELLPADDING="0"
826><TR
827><TD CLASS="decl"
828><A HREF="Control-Monad-Error-Class.html#t%3AMonadError"
829>MonadError</A
830> e m =&gt; <A HREF="Control-Monad-Error-Class.html#t%3AMonadError"
831>MonadError</A
832> e (<A HREF="Control-Monad-Reader.html#t%3AReaderT"
833>ReaderT</A
834> r m)</TD
835></TR
836><TR
837><TD CLASS="decl"
838>Monad m =&gt; <A HREF="Control-Monad-Reader-Class.html#t%3AMonadReader"
839>MonadReader</A
840> r (<A HREF="Control-Monad-Reader.html#t%3AReaderT"
841>ReaderT</A
842> r m)</TD
843></TR
844><TR
845><TD CLASS="decl"
846>MonadState s m =&gt; MonadState s (<A HREF="Control-Monad-Reader.html#t%3AReaderT"
847>ReaderT</A
848> r m)</TD
849></TR
850><TR
851><TD CLASS="decl"
852>MonadWriter w m =&gt; MonadWriter w (<A HREF="Control-Monad-Reader.html#t%3AReaderT"
853>ReaderT</A
854> r m)</TD
855></TR
856><TR
857><TD CLASS="decl"
858><A HREF="Control-Monad-Trans.html#t%3AMonadTrans"
859>MonadTrans</A
860> (<A HREF="Control-Monad-Reader.html#t%3AReaderT"
861>ReaderT</A
862> r)</TD
863></TR
864><TR
865><TD CLASS="decl"
866>Monad m =&gt; Functor (<A HREF="Control-Monad-Reader.html#t%3AReaderT"
867>ReaderT</A
868> r m)</TD
869></TR
870><TR
871><TD CLASS="decl"
872>Monad m =&gt; Monad (<A HREF="Control-Monad-Reader.html#t%3AReaderT"
873>ReaderT</A
874> r m)</TD
875></TR
876><TR
877><TD CLASS="decl"
878><A HREF="Control-Monad-Cont-Class.html#t%3AMonadCont"
879>MonadCont</A
880> m =&gt; <A HREF="Control-Monad-Cont-Class.html#t%3AMonadCont"
881>MonadCont</A
882> (<A HREF="Control-Monad-Reader.html#t%3AReaderT"
883>ReaderT</A
884> r m)</TD
885></TR
886><TR
887><TD CLASS="decl"
888>MonadFix m =&gt; MonadFix (<A HREF="Control-Monad-Reader.html#t%3AReaderT"
889>ReaderT</A
890> r m)</TD
891></TR
892><TR
893><TD CLASS="decl"
894><A HREF="Control-Monad-Trans.html#t%3AMonadIO"
895>MonadIO</A
896> m =&gt; <A HREF="Control-Monad-Trans.html#t%3AMonadIO"
897>MonadIO</A
898> (<A HREF="Control-Monad-Reader.html#t%3AReaderT"
899>ReaderT</A
900> r m)</TD
901></TR
902><TR
903><TD CLASS="decl"
904>MonadPlus m =&gt; MonadPlus (<A HREF="Control-Monad-Reader.html#t%3AReaderT"
905>ReaderT</A
906> r m)</TD
907></TR
908></TABLE
909></DIV
910></TD
911></TR
912></TABLE
913></TD
914></TR
915><TR
916><TD CLASS="s15"
917></TD
918></TR
919><TR
920><TD CLASS="decl"
921><A NAME="v%3AmapReaderT"
922></A
923><B
924>mapReaderT</B
925> :: (m a -&gt; n b) -&gt; <A HREF="Control-Monad-Reader.html#t%3AReaderT"
926>ReaderT</A
927> w m a -&gt; <A HREF="Control-Monad-Reader.html#t%3AReaderT"
928>ReaderT</A
929> w n b</TD
930></TR
931><TR
932><TD CLASS="s15"
933></TD
934></TR
935><TR
936><TD CLASS="decl"
937><A NAME="v%3AwithReaderT"
938></A
939><B
940>withReaderT</B
941> :: (r' -&gt; r) -&gt; <A HREF="Control-Monad-Reader.html#t%3AReaderT"
942>ReaderT</A
943> r m a -&gt; <A HREF="Control-Monad-Reader.html#t%3AReaderT"
944>ReaderT</A
945> r' m a</TD
946></TR
947><TR
948><TD CLASS="s15"
949></TD
950></TR
951><TR
952><TD CLASS="decl"
953>module <A HREF="Control-Monad-Trans.html"
954>Control.Monad.Trans</A
955></TD
956></TR
957><TR
958><TD CLASS="s15"
959></TD
960></TR
961><TR
962><TD CLASS="section1"
963><A NAME="1"
964>Example 1: Simple Reader Usage
965</A
966></TD
967></TR
968><TR
969><TD CLASS="s15"
970></TD
971></TR
972><TR
973><TD CLASS="doc"
974><P
975>In this example the <TT
976>Reader</TT
977> monad provides access to variable bindings.
978Bindings are a Map of integer variables.
979The variable <TT
980>count</TT
981> contains number of variables in the bindings.
982You can see how to run a Reader monad and retrieve data from it
983with <TT
984><A HREF="Control-Monad-Reader.html#v%3ArunReader"
985>runReader</A
986></TT
987>, how to access the Reader data with <TT
988><A HREF="Control-Monad-Reader-Class.html#v%3Aask"
989>ask</A
990></TT
991> and <TT
992><A HREF="Control-Monad-Reader-Class.html#v%3Aasks"
993>asks</A
994></TT
995>.
996</P
997><PRE
998> type Bindings = Map String Int;
999
1000-- Returns True if the &quot;count&quot; variable contains correct bindings size.
1001isCountCorrect :: Bindings -&gt; Bool
1002isCountCorrect bindings = runReader calc_isCountCorrect bindings
1003
1004-- The Reader monad, which implements this complicated check.
1005calc_isCountCorrect :: Reader Bindings Bool
1006calc_isCountCorrect = do
1007    count &lt;- asks (lookupVar &quot;count&quot;)
1008    bindings &lt;- ask
1009    return (count == (Map.size bindings))
1010
1011-- The selector function to  use with 'asks'.
1012-- Returns value of the variable with specified name.
1013lookupVar :: String -&gt; Bindings -&gt; Int
1014lookupVar name bindings = fromJust (Map.lookup name bindings)
1015
1016sampleBindings = Map.fromList [(&quot;count&quot;,3), (&quot;1&quot;,1), (&quot;b&quot;,2)]
1017
1018main = do
1019    putStr $ &quot;Count is correct for bindings &quot; ++ (show sampleBindings) ++ &quot;: &quot;;
1020    putStrLn $ show (isCountCorrect sampleBindings);
1021</PRE
1022></TD
1023></TR
1024><TR
1025><TD CLASS="s15"
1026></TD
1027></TR
1028><TR
1029><TD CLASS="section1"
1030><A NAME="2"
1031>Example 2: Modifying Reader Content With <TT
1032>local</TT
1033>
1034</A
1035></TD
1036></TR
1037><TR
1038><TD CLASS="s15"
1039></TD
1040></TR
1041><TR
1042><TD CLASS="doc"
1043><P
1044>Shows how to modify Reader content with <TT
1045><A HREF="Control-Monad-Reader-Class.html#v%3Alocal"
1046>local</A
1047></TT
1048>.
1049</P
1050><PRE
1051>calculateContentLen :: Reader String Int
1052calculateContentLen = do
1053    content &lt;- ask
1054    return (length content);
1055
1056-- Calls calculateContentLen after adding a prefix to the Reader content.
1057calculateModifiedContentLen :: Reader String Int
1058calculateModifiedContentLen = local (&quot;Prefix &quot; ++) calculateContentLen
1059
1060main = do
1061    let s = &quot;12345&quot;;
1062    let modifiedLen = runReader calculateModifiedContentLen s
1063    let len = runReader calculateContentLen s
1064    putStrLn $ &quot;Modified 's' length: &quot; ++ (show modifiedLen)
1065    putStrLn $ &quot;Original 's' length: &quot; ++ (show len)
1066</PRE
1067></TD
1068></TR
1069><TR
1070><TD CLASS="s15"
1071></TD
1072></TR
1073><TR
1074><TD CLASS="section1"
1075><A NAME="3"
1076>Example 3: <TT
1077>ReaderT</TT
1078> Monad Transformer
1079</A
1080></TD
1081></TR
1082><TR
1083><TD CLASS="s15"
1084></TD
1085></TR
1086><TR
1087><TD CLASS="doc"
1088><P
1089>Now you are thinking: 'Wow, what a great monad! I wish I could use
1090Reader functionality in MyFavoriteComplexMonad!'. Don't worry.
1091This can be easy done with the <TT
1092><A HREF="Control-Monad-Reader.html#t%3AReaderT"
1093>ReaderT</A
1094></TT
1095> monad transformer.
1096This example shows how to combine <TT
1097>ReaderT</TT
1098> with the IO monad.
1099</P
1100><PRE
1101>-- The Reader/IO combined monad, where Reader stores a string.
1102printReaderContent :: ReaderT String IO ()
1103printReaderContent = do
1104    content &lt;- ask
1105    liftIO $ putStrLn (&quot;The Reader Content: &quot; ++ content)
1106
1107main = do
1108    runReaderT printReaderContent &quot;Some Content&quot;
1109</PRE
1110></TD
1111></TR
1112><TR
1113><TD CLASS="s15"
1114></TD
1115></TR
1116><TR
1117><TD CLASS="botbar"
1118>Produced by <A HREF="http://www.haskell.org/haddock/"
1119>Haddock</A
1120> version 0.7</TD
1121></TR
1122></TABLE
1123></BODY
1124></HTML
1125>