Ticket #1169: Control-Monad-Cont.html

File Control-Monad-Cont.html, 22.0 KB (added by Andriy, 8 years ago)

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