Ticket #1169: Control-Monad-Cont.2.html

File Control-Monad-Cont.2.html, 18.5 KB (added by guest, 7 years ago)

Version 4 of the 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.Cont</TITLE
8>
9<style TYPE="text/css">
10/* -------- Global things --------- */
11
12BODY { 
13  background-color: #ffffff;
14  color: #000000;
15  font-family: sans-serif;
16  } 
17
18A:link    { color: #0000e0; text-decoration: none }
19A:visited { color: #0000a0; text-decoration: none }
20A:hover   { background-color: #e0e0ff; text-decoration: none }
21
22TABLE.vanilla {
23  width: 100%;
24  border-width: 0px;
25  /* I can't seem to specify cellspacing or cellpadding properly using CSS... */
26}
27
28TABLE.vanilla2 {
29  border-width: 0px;
30}
31
32/* <TT> font is a little too small in MSIE */
33TT  { font-size: 100%; }
34PRE { font-size: 100%; }
35
36LI P { margin: 0pt } 
37
38TD {
39  border-width: 0px;
40}
41
42TABLE.narrow {
43  border-width: 0px;
44}
45
46TD.s8  {  height: 8px;  }
47TD.s15 {  height: 15px; }
48
49SPAN.keyword { text-decoration: underline; }
50
51/* Resize the buttom image to match the text size */
52IMG.coll { width : 0.75em; height: 0.75em; margin-bottom: 0; margin-right: 0.5em }
53
54/* --------- Contents page ---------- */
55
56DIV.node {
57  padding-left: 3em;
58}
59
60DIV.cnode {
61  padding-left: 1.75em;
62}
63
64SPAN.pkg {
65  position: absolute;
66  left: 50em;
67}
68
69/* --------- Documentation elements ---------- */
70
71TD.children {
72  padding-left: 25px;
73  }
74
75TD.synopsis {
76  padding: 2px;
77  background-color: #f0f0f0;
78  font-family: monospace
79 }
80
81TD.decl { 
82  padding: 2px;
83  background-color: #f0f0f0; 
84  font-family: monospace;
85  vertical-align: top;
86  }
87
88/*
89  arg is just like decl, except that wrapping is not allowed.  It is
90  used for function and constructor arguments which have a text box
91  to the right, where if wrapping is allowed the text box squashes up
92  the declaration by wrapping it.
93*/
94TD.arg { 
95  padding: 2px;
96  background-color: #f0f0f0; 
97  font-family: monospace;
98  vertical-align: top;
99  white-space: nowrap;
100  }
101
102TD.recfield { padding-left: 20px }
103
104TD.doc  { 
105  padding-top: 2px;
106  padding-left: 10px;
107  }
108
109TD.ndoc  { 
110  padding: 2px;
111  }
112
113TD.rdoc  { 
114  padding: 2px;
115  padding-left: 10px;
116  width: 100%;
117  }
118
119TD.body  { 
120  padding-left: 10px
121  }
122
123TD.pkg {
124  width: 100%;
125  padding-left: 10px
126}
127
128TD.indexentry {
129  vertical-align: top;
130  padding-right: 10px
131  }
132
133TD.indexannot {
134  vertical-align: top;
135  padding-left: 20px;
136  white-space: nowrap
137  }
138
139TD.indexlinks {
140  width: 100%
141  }
142
143/* ------- Section Headings ------- */
144
145TD.section1 {
146  padding-top: 15px;
147  font-weight: bold;
148  font-size: 150%
149  }
150
151TD.section2 {
152  padding-top: 10px;
153  font-weight: bold;
154  font-size: 130%
155  }
156
157TD.section3 {
158  padding-top: 5px;
159  font-weight: bold;
160  font-size: 110%
161  }
162
163TD.section4 {
164  font-weight: bold;
165  font-size: 100%
166  }
167
168/* -------------- The title bar at the top of the page */
169
170TD.infohead {
171  color: #ffffff;
172  font-weight: bold;
173  padding-right: 10px;
174  text-align: left;
175}
176
177TD.infoval {
178  color: #ffffff;
179  padding-right: 10px;
180  text-align: left;
181}
182
183TD.topbar {
184  background-color: #000099;
185  padding: 5px;
186}
187
188TD.title {
189  color: #ffffff;
190  padding-left: 10px;
191  width: 100%
192  }
193
194TD.topbut {
195  padding-left: 5px;
196  padding-right: 5px;
197  border-left-width: 1px;
198  border-left-color: #ffffff;
199  border-left-style: solid;
200  white-space: nowrap;
201  }
202
203TD.topbut A:link {
204  color: #ffffff
205  }
206
207TD.topbut A:visited {
208  color: #ffff00
209  }
210
211TD.topbut A:hover {
212  background-color: #6060ff;
213  }
214
215TD.topbut:hover {
216  background-color: #6060ff
217  }
218
219TD.modulebar { 
220  background-color: #0077dd;
221  padding: 5px;
222  border-top-width: 1px;
223  border-top-color: #ffffff;
224  border-top-style: solid;
225  }
226
227/* --------- The page footer --------- */
228
229TD.botbar {
230  background-color: #000099;
231  color: #ffffff;
232  padding: 5px
233  }
234TD.botbar A:link {
235  color: #ffffff;
236  text-decoration: underline
237  }
238TD.botbar A:visited {
239  color: #ffff00
240  }
241TD.botbar A:hover {
242  background-color: #6060ff
243  }
244</style>
245
246<SCRIPT SRC="haddock.js" TYPE="text/javascript"
247></SCRIPT
248></HEAD
249><BODY
250><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
251><TR
252><TD CLASS="topbar"
253><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
254><TR
255><TD
256><IMG SRC="haskell_icon.gif" WIDTH="16" HEIGHT="16" ALT=" "
257></TD
258><TD CLASS="title"
259></TD
260><TD CLASS="topbut"
261><A HREF="index.html"
262>Contents</A
263></TD
264><TD CLASS="topbut"
265><A HREF="doc-index.html"
266>Index</A
267></TD
268></TR
269></TABLE
270></TD
271></TR
272><TR
273><TD CLASS="modulebar"
274><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
275><TR
276><TD
277><FONT SIZE="6"
278>Control.Monad.Cont</FONT
279></TD
280><TD ALIGN="right"
281><TABLE CLASS="narrow" CELLSPACING="0" CELLPADDING="0"
282><TR
283><TD CLASS="infohead"
284>Portability</TD
285><TD CLASS="infoval"
286>non-portable (multi-parameter type classes)</TD
287></TR
288><TR
289><TD CLASS="infohead"
290>Stability</TD
291><TD CLASS="infoval"
292>experimental</TD
293></TR
294><TR
295><TD CLASS="infohead"
296>Maintainer</TD
297><TD CLASS="infoval"
298>libraries@haskell.org</TD
299></TR
300></TABLE
301></TD
302></TR
303></TABLE
304></TD
305></TR
306><TR
307><TD CLASS="s15"
308></TD
309></TR
310><TR
311><TD
312><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
313><TR
314><TD CLASS="section4"
315><B
316>Contents</B
317></TD
318></TR
319><TR
320><TD
321><DL
322><DT
323><A HREF="#1"
324>Example 1: Simple Continuation Usage
325</A
326></DT
327><DT
328><A HREF="#2"
329>Example 2: Using <TT
330>callCC</TT
331>
332</A
333></DT
334><DT
335><A HREF="#3"
336>Example 3: Using <TT
337>ContT</TT
338> Monad Transformer
339</A
340></DT
341></DL
342></TD
343></TR
344></TABLE
345></TD
346></TR
347><TR
348><TD CLASS="s15"
349></TD
350></TR
351><TR
352><TD CLASS="section1"
353>Description</TD
354></TR
355><TR
356><TD CLASS="doc"
357><DL
358><DT
359>Computation type:</DT
360><DD
361> Computations which can be interrupted and resumed.
362</DD
363><DT
364>Binding strategy:</DT
365><DD
366> Binding a function to a monadic value creates
367a new continuation which uses the function as the continuation of the monadic
368computation.
369</DD
370><DT
371>Useful for:</DT
372><DD
373> Complex control structures, error handling,
374and creating co-routines.
375</DD
376><DT
377>Zero and plus:</DT
378><DD
379> None.
380</DD
381><DT
382>Example type:</DT
383><DD
384> <TT
385><TT
386><A HREF="Control-Monad-Cont.html#t%3ACont"
387>Cont</A
388></TT
389> r a</TT
390>
391</DD
392></DL
393><P
394>The Continuation monad represents computations in continuation-passing style
395(CPS).
396In continuation-passing style function result is not returned,
397but instead is passed to another function,
398received as a parameter (continuation).
399Computations are built up from sequences
400of nested continuations, terminated by a final continuation (often <TT
401>id</TT
402>)
403which produces the final result.
404Since continuations are functions which represent the future of a computation,
405manipulation of the continuation functions can achieve complex manipulations
406of the future of the computation,
407such as interrupting a computation in the middle, aborting a portion
408of a computation, restarting a computation, and interleaving execution of
409computations.
410The Continuation monad adapts CPS to the structure of a monad.
411</P
412><P
413>Before using the Continuation monad, be sure that you have
414a firm understanding of continuation-passing style
415and that continuations represent the best solution to your particular
416design problem.
417Many algorithms which require continuations in other languages do not require
418them in Haskell, due to Haskell's lazy semantics.
419Abuse of the Continuation monad can produce code that is impossible
420to understand and maintain.
421</P
422></TD
423></TR
424><TR
425><TD CLASS="s15"
426></TD
427></TR
428><TR
429><TD CLASS="section1"
430>Synopsis</TD
431></TR
432><TR
433><TD CLASS="s15"
434></TD
435></TR
436><TR
437><TD CLASS="body"
438><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
439><TR
440><TD CLASS="decl"
441>module <A HREF="Control-Monad-Cont-Class.html"
442>Control.Monad.Cont.Class</A
443></TD
444></TR
445><TR
446><TD CLASS="s8"
447></TD
448></TR
449><TR
450><TD CLASS="decl"
451><SPAN CLASS="keyword"
452>newtype</SPAN
453> <A HREF="#t%3ACont"
454>Cont</A
455> r a = <A HREF="#v%3ACont"
456>Cont</A
457> {<TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
458><TR
459><TD CLASS="recfield"
460><A HREF="#v%3ArunCont"
461>runCont</A
462> :: ((a -&gt; r) -&gt; r)</TD
463></TR
464></TABLE
465>}</TD
466></TR
467><TR
468><TD CLASS="s8"
469></TD
470></TR
471><TR
472><TD CLASS="decl"
473><A HREF="#v%3AmapCont"
474>mapCont</A
475> :: (r -&gt; r) -&gt; <A HREF="Control-Monad-Cont.html#t%3ACont"
476>Cont</A
477> r a -&gt; <A HREF="Control-Monad-Cont.html#t%3ACont"
478>Cont</A
479> r a</TD
480></TR
481><TR
482><TD CLASS="s8"
483></TD
484></TR
485><TR
486><TD CLASS="decl"
487><A HREF="#v%3AwithCont"
488>withCont</A
489> :: ((b -&gt; r) -&gt; a -&gt; r) -&gt; <A HREF="Control-Monad-Cont.html#t%3ACont"
490>Cont</A
491> r a -&gt; <A HREF="Control-Monad-Cont.html#t%3ACont"
492>Cont</A
493> r b</TD
494></TR
495><TR
496><TD CLASS="s8"
497></TD
498></TR
499><TR
500><TD CLASS="decl"
501><SPAN CLASS="keyword"
502>newtype</SPAN
503> <A HREF="#t%3AContT"
504>ContT</A
505> r m a = <A HREF="#v%3AContT"
506>ContT</A
507> {<TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
508><TR
509><TD CLASS="recfield"
510><A HREF="#v%3ArunContT"
511>runContT</A
512> :: ((a -&gt; m r) -&gt; m r)</TD
513></TR
514></TABLE
515>}</TD
516></TR
517><TR
518><TD CLASS="s8"
519></TD
520></TR
521><TR
522><TD CLASS="decl"
523><A HREF="#v%3AmapContT"
524>mapContT</A
525> :: (m r -&gt; m r) -&gt; <A HREF="Control-Monad-Cont.html#t%3AContT"
526>ContT</A
527> r m a -&gt; <A HREF="Control-Monad-Cont.html#t%3AContT"
528>ContT</A
529> r m a</TD
530></TR
531><TR
532><TD CLASS="s8"
533></TD
534></TR
535><TR
536><TD CLASS="decl"
537><A HREF="#v%3AwithContT"
538>withContT</A
539> :: ((b -&gt; m r) -&gt; a -&gt; m r) -&gt; <A HREF="Control-Monad-Cont.html#t%3AContT"
540>ContT</A
541> r m a -&gt; <A HREF="Control-Monad-Cont.html#t%3AContT"
542>ContT</A
543> r m b</TD
544></TR
545></TABLE
546></TD
547></TR
548><TR
549><TD CLASS="s15"
550></TD
551></TR
552><TR
553><TD CLASS="section1"
554>Documentation</TD
555></TR
556><TR
557><TD CLASS="s15"
558></TD
559></TR
560><TR
561><TD CLASS="decl"
562>module <A HREF="Control-Monad-Cont-Class.html"
563>Control.Monad.Cont.Class</A
564></TD
565></TR
566><TR
567><TD CLASS="s15"
568></TD
569></TR
570><TR
571><TD CLASS="decl"
572><SPAN CLASS="keyword"
573>newtype</SPAN
574> <A NAME="t%3ACont"
575></A
576><B
577>Cont</B
578> r a</TD
579></TR
580><TR
581><TD CLASS="body"
582><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
583><TR
584><TD CLASS="ndoc"
585><P
586>Continuation monad.
587<TT
588>Cont r a</TT
589> is a CPS computation that produces an intermediate result
590of type <TT
591>a</TT
592> within a CPS computation whose final result type is <TT
593>r</TT
594>.
595</P
596><P
597>The <TT
598>return</TT
599> function simply creates a continuation which passes the value on.
600</P
601><P
602>The <TT
603>&gt;&gt;=</TT
604> operator adds the bound function into the continuation chain.
605</P
606></TD
607></TR
608><TR
609><TD CLASS="section4"
610>Constructors</TD
611></TR
612><TR
613><TD CLASS="body"
614><TABLE CLASS="vanilla" CELLSPACING="5" CELLPADDING="0"
615><TR
616><TD CLASS="arg"
617><A NAME="v%3ACont"
618></A
619><B
620>Cont</B
621></TD
622><TD CLASS="rdoc"
623></TD
624></TR
625><TR
626><TD CLASS="body" COLSPAN="2"
627><TABLE CLASS="vanilla" CELLSPACING="1" CELLPADDING="0"
628><TR
629><TD CLASS="arg"
630><A NAME="v%3ArunCont"
631></A
632><B
633>runCont</B
634> :: ((a -&gt; r) -&gt; r)</TD
635><TD CLASS="rdoc"
636><P
637>Runs a CPS computation, returns its result after applying
638    the final continuation to it.
639    Parameters:
640</P
641><UL
642><LI
643> a continuation computation (<TT
644>Cont</TT
645>).
646</LI
647><LI
648> the final continuation, which produces the final result (often <TT
649>id</TT
650>).
651</LI
652></UL
653></TD
654></TR
655></TABLE
656></TD
657></TR
658></TABLE
659></TD
660></TR
661><TR
662><TD CLASS="section4"
663><IMG SRC="minus.gif" CLASS="coll" ONCLICK="toggle(this,'i:Cont')" ALT="show/hide"
664> Instances</TD
665></TR
666><TR
667><TD CLASS="body"
668><DIV ID="i:Cont" STYLE="display:block;"
669><TABLE CLASS="vanilla" CELLSPACING="1" CELLPADDING="0"
670><TR
671><TD CLASS="decl"
672>Functor (<A HREF="Control-Monad-Cont.html#t%3ACont"
673>Cont</A
674> r)</TD
675></TR
676><TR
677><TD CLASS="decl"
678>Monad (<A HREF="Control-Monad-Cont.html#t%3ACont"
679>Cont</A
680> r)</TD
681></TR
682><TR
683><TD CLASS="decl"
684><A HREF="Control-Monad-Cont-Class.html#t%3AMonadCont"
685>MonadCont</A
686> (<A HREF="Control-Monad-Cont.html#t%3ACont"
687>Cont</A
688> r)</TD
689></TR
690></TABLE
691></DIV
692></TD
693></TR
694></TABLE
695></TD
696></TR
697><TR
698><TD CLASS="s15"
699></TD
700></TR
701><TR
702><TD CLASS="decl"
703><A NAME="v%3AmapCont"
704></A
705><B
706>mapCont</B
707> :: (r -&gt; r) -&gt; <A HREF="Control-Monad-Cont.html#t%3ACont"
708>Cont</A
709> r a -&gt; <A HREF="Control-Monad-Cont.html#t%3ACont"
710>Cont</A
711> r a</TD
712></TR
713><TR
714><TD CLASS="s15"
715></TD
716></TR
717><TR
718><TD CLASS="decl"
719><A NAME="v%3AwithCont"
720></A
721><B
722>withCont</B
723> :: ((b -&gt; r) -&gt; a -&gt; r) -&gt; <A HREF="Control-Monad-Cont.html#t%3ACont"
724>Cont</A
725> r a -&gt; <A HREF="Control-Monad-Cont.html#t%3ACont"
726>Cont</A
727> r b</TD
728></TR
729><TR
730><TD CLASS="s15"
731></TD
732></TR
733><TR
734><TD CLASS="decl"
735><SPAN CLASS="keyword"
736>newtype</SPAN
737> <A NAME="t%3AContT"
738></A
739><B
740>ContT</B
741> r m a</TD
742></TR
743><TR
744><TD CLASS="body"
745><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
746><TR
747><TD CLASS="ndoc"
748>The continuation monad transformer.
749Can be used to add continuation handling to other monads.
750</TD
751></TR
752><TR
753><TD CLASS="section4"
754>Constructors</TD
755></TR
756><TR
757><TD CLASS="body"
758><TABLE CLASS="vanilla" CELLSPACING="5" CELLPADDING="0"
759><TR
760><TD CLASS="arg"
761><A NAME="v%3AContT"
762></A
763><B
764>ContT</B
765></TD
766><TD CLASS="rdoc"
767></TD
768></TR
769><TR
770><TD CLASS="body" COLSPAN="2"
771><TABLE CLASS="vanilla" CELLSPACING="1" CELLPADDING="0"
772><TR
773><TD CLASS="arg"
774><A NAME="v%3ArunContT"
775></A
776><B
777>runContT</B
778> :: ((a -&gt; m r) -&gt; m r)</TD
779><TD CLASS="rdoc"
780></TD
781></TR
782></TABLE
783></TD
784></TR
785></TABLE
786></TD
787></TR
788><TR
789><TD CLASS="section4"
790><IMG SRC="minus.gif" CLASS="coll" ONCLICK="toggle(this,'i:ContT')" ALT="show/hide"
791> Instances</TD
792></TR
793><TR
794><TD CLASS="body"
795><DIV ID="i:ContT" STYLE="display:block;"
796><TABLE CLASS="vanilla" CELLSPACING="1" CELLPADDING="0"
797><TR
798><TD CLASS="decl"
799>MonadReader r' m =&gt; MonadReader r' (<A HREF="Control-Monad-Cont.html#t%3AContT"
800>ContT</A
801> r m)</TD
802></TR
803><TR
804><TD CLASS="decl"
805>MonadState s m =&gt; MonadState s (<A HREF="Control-Monad-Cont.html#t%3AContT"
806>ContT</A
807> r m)</TD
808></TR
809><TR
810><TD CLASS="decl"
811>MonadTrans (<A HREF="Control-Monad-Cont.html#t%3AContT"
812>ContT</A
813> r)</TD
814></TR
815><TR
816><TD CLASS="decl"
817>Monad m =&gt; Functor (<A HREF="Control-Monad-Cont.html#t%3AContT"
818>ContT</A
819> r m)</TD
820></TR
821><TR
822><TD CLASS="decl"
823>Monad m =&gt; Monad (<A HREF="Control-Monad-Cont.html#t%3AContT"
824>ContT</A
825> r m)</TD
826></TR
827><TR
828><TD CLASS="decl"
829>Monad m =&gt; <A HREF="Control-Monad-Cont-Class.html#t%3AMonadCont"
830>MonadCont</A
831> (<A HREF="Control-Monad-Cont.html#t%3AContT"
832>ContT</A
833> r m)</TD
834></TR
835><TR
836><TD CLASS="decl"
837>MonadIO m =&gt; MonadIO (<A HREF="Control-Monad-Cont.html#t%3AContT"
838>ContT</A
839> r m)</TD
840></TR
841></TABLE
842></DIV
843></TD
844></TR
845></TABLE
846></TD
847></TR
848><TR
849><TD CLASS="s15"
850></TD
851></TR
852><TR
853><TD CLASS="decl"
854><A NAME="v%3AmapContT"
855></A
856><B
857>mapContT</B
858> :: (m r -&gt; m r) -&gt; <A HREF="Control-Monad-Cont.html#t%3AContT"
859>ContT</A
860> r m a -&gt; <A HREF="Control-Monad-Cont.html#t%3AContT"
861>ContT</A
862> r m a</TD
863></TR
864><TR
865><TD CLASS="s15"
866></TD
867></TR
868><TR
869><TD CLASS="decl"
870><A NAME="v%3AwithContT"
871></A
872><B
873>withContT</B
874> :: ((b -&gt; m r) -&gt; a -&gt; m r) -&gt; <A HREF="Control-Monad-Cont.html#t%3AContT"
875>ContT</A
876> r m a -&gt; <A HREF="Control-Monad-Cont.html#t%3AContT"
877>ContT</A
878> r m b</TD
879></TR
880><TR
881><TD CLASS="s15"
882></TD
883></TR
884><TR
885><TD CLASS="section1"
886><A NAME="1"
887>Example 1: Simple Continuation Usage
888</A
889></TD
890></TR
891><TR
892><TD CLASS="s15"
893></TD
894></TR
895><TR
896><TD CLASS="doc"
897><P
898>Calculating length of a list continuation-style:
899</P
900><PRE
901>calculateLength :: [a] -&gt; Cont r Int
902calculateLength l = return (length l)
903</PRE
904><P
905>Here we use <TT
906>calculateLength</TT
907> by making it to pass its result to <TT
908>print</TT
909>:
910</P
911><PRE
912>main = do
913  runCont (calculateLength &quot;123&quot;) print
914  -- result: 3
915</PRE
916><P
917>It is possible to chain <TT
918><A HREF="Control-Monad-Cont.html#t%3ACont"
919>Cont</A
920></TT
921> blocks with <TT
922>&gt;&gt;=</TT
923>.
924</P
925><PRE
926>double :: Int -&gt; Cont r Int
927double n = return (n * 2)
928
929main = do
930  runCont (calculateLength &quot;123&quot; &gt;&gt;= double) print
931  -- result: 6
932</PRE
933></TD
934></TR
935><TR
936><TD CLASS="s15"
937></TD
938></TR
939><TR
940><TD CLASS="section1"
941><A NAME="2"
942>Example 2: Using <TT
943>callCC</TT
944>
945</A
946></TD
947></TR
948><TR
949><TD CLASS="s15"
950></TD
951></TR
952><TR
953><TD CLASS="doc"
954><P
955>This example gives a taste of how escape continuations work, shows a typical
956pattern for their usage.
957</P
958><PRE
959>-- Returns a string depending on the length of the name parameter.
960-- If the provided string is empty, returns an error.
961-- Otherwise, returns a welcome message.
962whatsYourName :: String -&gt; String
963whatsYourName name =
964  (`runCont` id) $ do                      -- 1
965    response &lt;- callCC $ \exit -&gt; do       -- 2
966      validateName name exit               -- 3
967      return $ &quot;Welcome, &quot; ++ name ++ &quot;!&quot;  -- 4
968    return response                        -- 5
969
970validateName name exit = do
971  when (null name) (exit &quot;You forgot to tell me your name!&quot;)
972</PRE
973><P
974>Here is what this example does:
975</P
976><OL
977><LI
978> Runs an anonymous <TT
979><A HREF="Control-Monad-Cont.html#t%3ACont"
980>Cont</A
981></TT
982> block and extracts value from it with
983<TT
984>(`runCont` id)</TT
985>. Here <TT
986>id</TT
987> is the continuation, passed to the <TT
988>Cont</TT
989> block.
990</LI
991><LI
992> Binds <TT
993>response</TT
994> to the result of the following <TT
995><A HREF="Control-Monad-Cont-Class.html#v%3AcallCC"
996>callCC</A
997></TT
998> block,
999binds <TT
1000>exit</TT
1001> to the continuation.
1002</LI
1003><LI
1004> Validates <TT
1005>name</TT
1006>.
1007This approach illustrates advantage of using <TT
1008><A HREF="Control-Monad-Cont-Class.html#v%3AcallCC"
1009>callCC</A
1010></TT
1011> over <TT
1012>return</TT
1013>.
1014We pass the continuation to <TT
1015>validateName</TT
1016>,
1017and interrupt execution of the <TT
1018>Cont</TT
1019> block from <EM
1020>inside</EM
1021> of <TT
1022>validateName</TT
1023>.
1024</LI
1025><LI
1026> Returns the welcome message from the <TT
1027>callCC</TT
1028> block.
1029This line is not executed if <TT
1030>validateName</TT
1031> fails.
1032</LI
1033><LI
1034> Returns from the <TT
1035>Cont</TT
1036> block.
1037</LI
1038></OL
1039></TD
1040></TR
1041><TR
1042><TD CLASS="s15"
1043></TD
1044></TR
1045><TR
1046><TD CLASS="section1"
1047><A NAME="3"
1048>Example 3: Using <TT
1049>ContT</TT
1050> Monad Transformer
1051</A
1052></TD
1053></TR
1054><TR
1055><TD CLASS="s15"
1056></TD
1057></TR
1058><TR
1059><TD CLASS="doc"
1060><P
1061><TT
1062><A HREF="Control-Monad-Cont.html#t%3AContT"
1063>ContT</A
1064></TT
1065> can be used to add continuation handling to other monads.
1066Here is an example how to combine it with <TT
1067>IO</TT
1068> monad:
1069</P
1070><PRE
1071>import Control.Monad.Cont
1072import System.IO
1073
1074main = do
1075  hSetBuffering stdout NoBuffering
1076  runContT (callCC askString) reportResult
1077
1078askString :: (String -&gt; ContT () IO String) -&gt; ContT () IO String
1079askString next = do
1080  liftIO $ putStrLn &quot;Please enter a string&quot;
1081  s &lt;- liftIO $ getLine
1082  next s
1083
1084reportResult :: String -&gt; IO ()
1085reportResult s = do
1086  putStrLn (&quot;You entered: &quot; ++ s)
1087</PRE
1088><P
1089>Action <TT
1090>askString</TT
1091> requests user to enter a string,
1092and passes it to the continuation.
1093<TT
1094>askString</TT
1095> takes as a parameter a continuation taking a string parameter,
1096and returning <TT
1097>IO ()</TT
1098>.
1099Compare its signature to <TT
1100><A HREF="Control-Monad-Cont.html#v%3ArunContT"
1101>runContT</A
1102></TT
1103> definition.
1104</P
1105></TD
1106></TR
1107><TR
1108><TD CLASS="s15"
1109></TD
1110></TR
1111><TR
1112><TD CLASS="botbar"
1113>Produced by <A HREF="http://www.haskell.org/haddock/"
1114>Haddock</A
1115> version 0.7</TD
1116></TR
1117></TABLE
1118></BODY
1119></HTML
1120>