This file is indexed.

/usr/share/doc/libghc-mtl-doc/html/Control-Monad-Cont.html is in libghc-mtl-doc 2.2.1-2.

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
<!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.Cont</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-Cont.html");};
//]]>
</script></head><body><div id="package-header"><ul class="links" id="page-menu"><li><a href="src/Control-Monad-Cont.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.2.1: Monad classes, using functional dependencies</p></div><div id="content"><div id="module-header"><table class="info"><tr><th>Copyright</th><td>(c) The University of Glasgow 2001,
               (c) Jeff Newbern 2003-2007,
               (c) Andriy Palamarchuk 2007</td></tr><tr><th>License</th><td>BSD-style (see the file LICENSE)</td></tr><tr><th>Maintainer</th><td>libraries@haskell.org</td></tr><tr><th>Stability</th><td>experimental</td></tr><tr><th>Portability</th><td>portable</td></tr><tr><th>Safe Haskell</th><td>Safe</td></tr><tr><th>Language</th><td>Haskell98</td></tr></table><p class="caption">Control.Monad.Cont</p></div><div id="table-of-contents"><p class="caption">Contents</p><ul><li><a href="#g:1">MonadCont class</a></li><li><a href="#g:2">The Cont monad</a></li><li><a href="#g:3">The ContT monad transformer</a></li><li><a href="#g:4">Example 1: Simple Continuation Usage</a></li><li><a href="#g:5">Example 2: Using <code>callCC</code></a></li><li><a href="#g:6">Example 3: Using <code>ContT</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 can be interrupted and resumed.</dd><dt>Binding strategy:</dt><dd>Binding a function to a monadic value creates
a new continuation which uses the function as the continuation of the monadic
computation.</dd><dt>Useful for:</dt><dd>Complex control structures, error handling,
and creating co-routines.</dd><dt>Zero and plus:</dt><dd>None.</dd><dt>Example type:</dt><dd><code><code><a href="Control-Monad-Cont.html#t:Cont">Cont</a></code> r a</code></dd></dl><p>The Continuation monad represents computations in continuation-passing style
(CPS).
In continuation-passing style function result is not returned,
but instead is passed to another function,
received as a parameter (continuation).
Computations are built up from sequences
of nested continuations, terminated by a final continuation (often <code>id</code>)
which produces the final result.
Since continuations are functions which represent the future of a computation,
manipulation of the continuation functions can achieve complex manipulations
of the future of the computation,
such as interrupting a computation in the middle, aborting a portion
of a computation, restarting a computation, and interleaving execution of
computations.
The Continuation monad adapts CPS to the structure of a monad.</p><p>Before using the Continuation monad, be sure that you have
a firm understanding of continuation-passing style
and that continuations represent the best solution to your particular
design problem.
Many algorithms which require continuations in other languages do not require
them in Haskell, due to Haskell's lazy semantics.
Abuse of the Continuation monad can produce code that is impossible
to understand and maintain.</p></div></div><div id="synopsis"><p id="control.syn" class="caption expander" onclick="toggleSection('syn')">Synopsis</p><ul id="section.syn" class="hide" onclick="toggleSection('syn')"><li class="src short"><span class="keyword">class</span> <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad.html#t:Monad">Monad</a> m =&gt; <a href="#t:MonadCont">MonadCont</a> m <span class="keyword">where</span><ul class="subs"><li><a href="#v:callCC">callCC</a> :: ((a -&gt; m b) -&gt; m a) -&gt; m a</li></ul></li><li class="src short"><span class="keyword">type</span> <a href="#t:Cont">Cont</a> r = <a href="Control-Monad-Cont.html#t:ContT">ContT</a> r <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Data-Functor-Identity.html#t:Identity">Identity</a></li><li class="src short"><a href="#v:cont">cont</a> :: ((a -&gt; r) -&gt; r) -&gt; <a href="Control-Monad-Cont.html#t:Cont">Cont</a> r a</li><li class="src short"><a href="#v:runCont">runCont</a> :: <a href="Control-Monad-Cont.html#t:Cont">Cont</a> r a -&gt; (a -&gt; r) -&gt; r</li><li class="src short"><a href="#v:mapCont">mapCont</a> :: (r -&gt; r) -&gt; <a href="Control-Monad-Cont.html#t:Cont">Cont</a> r a -&gt; <a href="Control-Monad-Cont.html#t:Cont">Cont</a> r a</li><li class="src short"><a href="#v:withCont">withCont</a> :: ((b -&gt; r) -&gt; a -&gt; r) -&gt; <a href="Control-Monad-Cont.html#t:Cont">Cont</a> r a -&gt; <a href="Control-Monad-Cont.html#t:Cont">Cont</a> r b</li><li class="src short"><span class="keyword">newtype</span> <a href="#t:ContT">ContT</a> r m a :: * -&gt; (* -&gt; *) -&gt; * -&gt; * = <a href="#v:ContT">ContT</a> {<ul class="subs"><li><a href="#v:runContT">runContT</a> :: (a -&gt; m r) -&gt; m r</li></ul>}</li><li class="src short"><a href="#v:runContT">runContT</a> :: <a href="Control-Monad-Cont.html#t:ContT">ContT</a> r m a -&gt; (a -&gt; m r) -&gt; m r</li><li class="src short"><a href="#v:mapContT">mapContT</a> :: (m r -&gt; m r) -&gt; <a href="Control-Monad-Cont.html#t:ContT">ContT</a> r m a -&gt; <a href="Control-Monad-Cont.html#t:ContT">ContT</a> r m a</li><li class="src short"><a href="#v:withContT">withContT</a> :: ((b -&gt; m r) -&gt; a -&gt; m r) -&gt; <a href="Control-Monad-Cont.html#t:ContT">ContT</a> r m a -&gt; <a href="Control-Monad-Cont.html#t:ContT">ContT</a> r m b</li><li class="src short">module <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad.html">Control.Monad</a></li><li class="src short">module <a href="Control-Monad-Trans.html">Control.Monad.Trans</a></li></ul></div><div id="interface"><h1 id="g:1">MonadCont class</h1><div class="top"><p class="src"><span class="keyword">class</span> <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad.html#t:Monad">Monad</a> m =&gt; <a name="t:MonadCont" class="def">MonadCont</a> m <span class="keyword">where</span> <a href="src/Control-Monad-Cont-Class.html#MonadCont" class="link">Source</a></p><div class="subs methods"><p class="caption">Methods</p><p class="src"><a name="v:callCC" class="def">callCC</a> :: ((a -&gt; m b) -&gt; m a) -&gt; m a <a href="src/Control-Monad-Cont-Class.html#callCC" class="link">Source</a></p><div class="doc"><p><code>callCC</code> (call-with-current-continuation)
    calls a function with the current continuation as its argument.
    Provides an escape continuation mechanism for use with Continuation monads.
    Escape continuations allow to abort the current computation and return
    a value immediately.
    They achieve a similar effect to <code><a href="Control-Monad-Error.html#v:throwError">throwError</a></code>
    and <code><a href="Control-Monad-Error.html#v:catchError">catchError</a></code>
    within an <code><a href="Control-Monad-Error.html#v:Error">Error</a></code> monad.
    Advantage of this function over calling <code>return</code> is that it makes
    the continuation explicit,
    allowing more flexibility and better control
    (see examples in <a href="Control-Monad-Cont.html">Control.Monad.Cont</a>).</p><p>The standard idiom used with <code>callCC</code> is to provide a lambda-expression
    to name the continuation. Then calling the named continuation anywhere
    within its scope will escape from the computation,
    even if it is many layers deep within nested computations.</p></div></div><div class="subs instances"><p id="control.i:MonadCont" class="caption collapser" onclick="toggleSection('i:MonadCont')">Instances</p><div id="section.i:MonadCont" class="show"><table><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> m =&gt; <a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/transformers-0.4.2.0/Control-Monad-Trans-Maybe.html#t:MaybeT">MaybeT</a> m)</span> <a href="src/Control-Monad-Cont-Class.html#line-112" class="link">Source</a></td><td class="doc empty">&nbsp;</td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> m =&gt; <a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> (<a href="Control-Monad-List.html#t:ListT">ListT</a> m)</span> <a href="src/Control-Monad-Cont-Class.html#line-109" class="link">Source</a></td><td class="doc empty">&nbsp;</td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> m =&gt; <a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> (<a href="file:///usr/share/doc/ghc-doc/html/libraries/transformers-0.4.2.0/Control-Monad-Trans-Identity.html#t:IdentityT">IdentityT</a> m)</span> <a href="src/Control-Monad-Cont-Class.html#line-106" class="link">Source</a></td><td class="doc empty">&nbsp;</td></tr><tr><td class="src clearfix"><span class="inst-left">(<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Data-Monoid.html#t:Monoid">Monoid</a> w, <a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> m) =&gt; <a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> (<a href="Control-Monad-Writer-Strict.html#t:WriterT">WriterT</a> w m)</span> <a href="src/Control-Monad-Cont-Class.html#line-133" class="link">Source</a></td><td class="doc empty">&nbsp;</td></tr><tr><td class="src clearfix"><span class="inst-left">(<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Data-Monoid.html#t:Monoid">Monoid</a> w, <a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> m) =&gt; <a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> (<a href="Control-Monad-Writer-Lazy.html#t:WriterT">WriterT</a> w m)</span> <a href="src/Control-Monad-Cont-Class.html#line-130" class="link">Source</a></td><td class="doc empty">&nbsp;</td></tr><tr><td class="src clearfix"><span class="inst-left">(<a href="Control-Monad-Error-Class.html#t:Error">Error</a> e, <a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> m) =&gt; <a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> (<a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e m)</span> <a href="src/Control-Monad-Cont-Class.html#line-100" class="link">Source</a></td><td class="doc empty">&nbsp;</td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> m =&gt; <a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> (<a href="Control-Monad-Except.html#t:ExceptT">ExceptT</a> e m)</span> <a href="src/Control-Monad-Cont-Class.html#line-103" class="link">Source</a></td><td class="doc empty">&nbsp;</td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> m =&gt; <a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> (<a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m)</span> <a href="src/Control-Monad-Cont-Class.html#line-127" class="link">Source</a></td><td class="doc empty">&nbsp;</td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> m =&gt; <a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> (<a href="Control-Monad-State-Lazy.html#t:StateT">StateT</a> s m)</span> <a href="src/Control-Monad-Cont-Class.html#line-124" class="link">Source</a></td><td class="doc empty">&nbsp;</td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> m =&gt; <a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</span> <a href="src/Control-Monad-Cont-Class.html#line-115" class="link">Source</a></td><td class="doc empty">&nbsp;</td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> (<a href="Control-Monad-Cont.html#t:ContT">ContT</a> r m)</span> <a href="src/Control-Monad-Cont-Class.html#line-94" class="link">Source</a></td><td class="doc empty">&nbsp;</td></tr><tr><td class="src clearfix"><span class="inst-left">(<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Data-Monoid.html#t:Monoid">Monoid</a> w, <a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> m) =&gt; <a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> (<a href="Control-Monad-RWS-Strict.html#t:RWST">RWST</a> r w s m)</span> <a href="src/Control-Monad-Cont-Class.html#line-121" class="link">Source</a></td><td class="doc empty">&nbsp;</td></tr><tr><td class="src clearfix"><span class="inst-left">(<a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Data-Monoid.html#t:Monoid">Monoid</a> w, <a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> m) =&gt; <a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> (<a href="Control-Monad-RWS-Lazy.html#t:RWST">RWST</a> r w s m)</span> <a href="src/Control-Monad-Cont-Class.html#line-118" class="link">Source</a></td><td class="doc empty">&nbsp;</td></tr></table></div></div></div><h1 id="g:2">The Cont monad</h1><div class="top"><p class="src"><span class="keyword">type</span> <a name="t:Cont" class="def">Cont</a> r = <a href="Control-Monad-Cont.html#t:ContT">ContT</a> r <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Data-Functor-Identity.html#t:Identity">Identity</a></p><div class="doc"><p>Continuation monad.
<code>Cont r a</code> is a CPS computation that produces an intermediate result
of type <code>a</code> within a CPS computation whose final result type is <code>r</code>.</p><p>The <code>return</code> function simply creates a continuation which passes the value on.</p><p>The <code>&gt;&gt;=</code> operator adds the bound function into the continuation chain.</p></div></div><div class="top"><p class="src"><a name="v:cont" class="def">cont</a> :: ((a -&gt; r) -&gt; r) -&gt; <a href="Control-Monad-Cont.html#t:Cont">Cont</a> r a</p><div class="doc"><p>Construct a continuation-passing computation from a function.
 (The inverse of <code><a href="Control-Monad-Cont.html#v:runCont">runCont</a></code>)</p></div></div><div class="top"><p class="src"><a name="v:runCont" class="def">runCont</a></p><div class="subs arguments"><p class="caption">Arguments</p><table><tr><td class="src">:: <a href="Control-Monad-Cont.html#t:Cont">Cont</a> r a</td><td class="doc"><p>continuation computation (<code>Cont</code>).</p></td></tr><tr><td class="src">-&gt; (a -&gt; r)</td><td class="doc"><p>the final continuation, which produces
 the final result (often <code><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Data-Function.html#v:id">id</a></code>).</p></td></tr><tr><td class="src">-&gt; r</td><td class="doc empty">&nbsp;</td></tr></table></div><div class="doc"><p>The result of running a CPS computation with a given final continuation.
 (The inverse of <code><a href="Control-Monad-Cont.html#v:cont">cont</a></code>)</p></div></div><div class="top"><p class="src"><a name="v:mapCont" class="def">mapCont</a> :: (r -&gt; r) -&gt; <a href="Control-Monad-Cont.html#t:Cont">Cont</a> r a -&gt; <a href="Control-Monad-Cont.html#t:Cont">Cont</a> r a</p><div class="doc"><p>Apply a function to transform the result of a continuation-passing
 computation.</p><ul><li><pre><code><a href="Control-Monad-Cont.html#v:runCont">runCont</a></code> (<code><a href="Control-Monad-Cont.html#v:mapCont">mapCont</a></code> f m) = f . <code><a href="Control-Monad-Cont.html#v:runCont">runCont</a></code> m</pre></li></ul></div></div><div class="top"><p class="src"><a name="v:withCont" class="def">withCont</a> :: ((b -&gt; r) -&gt; a -&gt; r) -&gt; <a href="Control-Monad-Cont.html#t:Cont">Cont</a> r a -&gt; <a href="Control-Monad-Cont.html#t:Cont">Cont</a> r b</p><div class="doc"><p>Apply a function to transform the continuation passed to a CPS
 computation.</p><ul><li><pre><code><a href="Control-Monad-Cont.html#v:runCont">runCont</a></code> (<code><a href="Control-Monad-Cont.html#v:withCont">withCont</a></code> f m) = <code><a href="Control-Monad-Cont.html#v:runCont">runCont</a></code> m . f</pre></li></ul></div></div><h1 id="g:3">The ContT monad transformer</h1><div class="top"><p class="src"><span class="keyword">newtype</span> <a name="t:ContT" class="def">ContT</a> r m a :: * -&gt; (* -&gt; *) -&gt; * -&gt; *</p><div class="doc"><p>The continuation monad transformer.
 Can be used to add continuation handling to other monads.</p></div><div class="subs constructors"><p class="caption">Constructors</p><table><tr><td class="src"><a name="v:ContT" class="def">ContT</a></td><td class="doc empty">&nbsp;</td></tr><tr><td colspan="2"><div class="subs fields"><p class="caption">Fields</p><dl><dt class="src"><a name="v:runContT" class="def">runContT</a> :: (a -&gt; m r) -&gt; m r</dt><dd class="doc empty">&nbsp;</dd></dl><div class="clear"></div></div></td></tr></table></div><div class="subs instances"><p id="control.i:ContT" class="caption collapser" onclick="toggleSection('i:ContT')">Instances</p><div id="section.i:ContT" class="show"><table><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-State-Class.html#t:MonadState">MonadState</a> s m =&gt; <a href="Control-Monad-State-Class.html#t:MonadState">MonadState</a> s (<a href="Control-Monad-Cont.html#t:ContT">ContT</a> r m)</span> <a href="src/Control-Monad-State-Class.html#line-125" class="link">Source</a></td><td class="doc empty">&nbsp;</td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-Reader-Class.html#t:MonadReader">MonadReader</a> r' m =&gt; <a href="Control-Monad-Reader-Class.html#t:MonadReader">MonadReader</a> r' (<a href="Control-Monad-Cont.html#t:ContT">ContT</a> r m)</span> <a href="src/Control-Monad-Reader-Class.html#line-127" class="link">Source</a></td><td class="doc empty">&nbsp;</td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="file:///usr/share/doc/ghc-doc/html/libraries/transformers-0.4.2.0/Control-Monad-Trans-Class.html#t:MonadTrans">MonadTrans</a> (<a href="Control-Monad-Cont.html#t:ContT">ContT</a> r)</span></td><td class="doc empty">&nbsp;</td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad.html#t:Monad">Monad</a> (<a href="Control-Monad-Cont.html#t:ContT">ContT</a> r m)</span></td><td class="doc empty">&nbsp;</td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Data-Functor.html#t:Functor">Functor</a> (<a href="Control-Monad-Cont.html#t:ContT">ContT</a> r m)</span></td><td class="doc empty">&nbsp;</td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Applicative.html#t:Applicative">Applicative</a> (<a href="Control-Monad-Cont.html#t:ContT">ContT</a> r m)</span></td><td class="doc empty">&nbsp;</td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="file:///usr/share/doc/ghc-doc/html/libraries/transformers-0.4.2.0/Control-Monad-IO-Class.html#t:MonadIO">MonadIO</a> m =&gt; <a href="file:///usr/share/doc/ghc-doc/html/libraries/transformers-0.4.2.0/Control-Monad-IO-Class.html#t:MonadIO">MonadIO</a> (<a href="Control-Monad-Cont.html#t:ContT">ContT</a> r m)</span></td><td class="doc empty">&nbsp;</td></tr><tr><td class="src clearfix"><span class="inst-left"><a href="Control-Monad-Cont.html#t:MonadCont">MonadCont</a> (<a href="Control-Monad-Cont.html#t:ContT">ContT</a> r m)</span> <a href="src/Control-Monad-Cont-Class.html#line-94" class="link">Source</a></td><td class="doc empty">&nbsp;</td></tr></table></div></div></div><div class="top"><p class="src"><a name="v:runContT" class="def">runContT</a> :: <a href="Control-Monad-Cont.html#t:ContT">ContT</a> r m a -&gt; (a -&gt; m r) -&gt; m r</p></div><div class="top"><p class="src"><a name="v:mapContT" class="def">mapContT</a> :: (m r -&gt; m r) -&gt; <a href="Control-Monad-Cont.html#t:ContT">ContT</a> r m a -&gt; <a href="Control-Monad-Cont.html#t:ContT">ContT</a> r m a</p><div class="doc"><p>Apply a function to transform the result of a continuation-passing
 computation.</p><ul><li><pre><code><a href="Control-Monad-Cont.html#v:runContT">runContT</a></code> (<code><a href="Control-Monad-Cont.html#v:mapContT">mapContT</a></code> f m) = f . <code><a href="Control-Monad-Cont.html#v:runContT">runContT</a></code> m</pre></li></ul></div></div><div class="top"><p class="src"><a name="v:withContT" class="def">withContT</a> :: ((b -&gt; m r) -&gt; a -&gt; m r) -&gt; <a href="Control-Monad-Cont.html#t:ContT">ContT</a> r m a -&gt; <a href="Control-Monad-Cont.html#t:ContT">ContT</a> r m b</p><div class="doc"><p>Apply a function to transform the continuation passed to a CPS
 computation.</p><ul><li><pre><code><a href="Control-Monad-Cont.html#v:runContT">runContT</a></code> (<code><a href="Control-Monad-Cont.html#v:withContT">withContT</a></code> f m) = <code><a href="Control-Monad-Cont.html#v:runContT">runContT</a></code> m . f</pre></li></ul></div></div><div class="top"><p class="src">module <a href="file:///usr/share/doc/ghc-doc/html/libraries/base-4.8.2.0/Control-Monad.html">Control.Monad</a></p></div><div class="top"><p class="src">module <a href="Control-Monad-Trans.html">Control.Monad.Trans</a></p></div><h1 id="g:4">Example 1: Simple Continuation Usage</h1><div class="doc"><p>Calculating length of a list continuation-style:</p><pre>calculateLength :: [a] -&gt; Cont r Int
calculateLength l = return (length l)</pre><p>Here we use <code>calculateLength</code> by making it to pass its result to <code>print</code>:</p><pre>main = do
  runCont (calculateLength &quot;123&quot;) print
  -- result: 3</pre><p>It is possible to chain <code><a href="Control-Monad-Cont.html#t:Cont">Cont</a></code> blocks with <code>&gt;&gt;=</code>.</p><pre>double :: Int -&gt; Cont r Int
double n = return (n * 2)

main = do
  runCont (calculateLength &quot;123&quot; &gt;&gt;= double) print
  -- result: 6</pre></div><h1 id="g:5">Example 2: Using <code>callCC</code></h1><div class="doc"><p>This example gives a taste of how escape continuations work, shows a typical
pattern for their usage.</p><pre>-- Returns a string depending on the length of the name parameter.
-- If the provided string is empty, returns an error.
-- Otherwise, returns a welcome message.
whatsYourName :: String -&gt; String
whatsYourName name =
  (`runCont` id) $ do                      -- 1
    response &lt;- callCC $ \exit -&gt; do       -- 2
      validateName name exit               -- 3
      return $ &quot;Welcome, &quot; ++ name ++ &quot;!&quot;  -- 4
    return response                        -- 5

validateName name exit = do
  when (null name) (exit &quot;You forgot to tell me your name!&quot;)</pre><p>Here is what this example does:</p><ol><li>Runs an anonymous <code><a href="Control-Monad-Cont.html#t:Cont">Cont</a></code> block and extracts value from it with
<code>(`runCont` id)</code>. Here <code>id</code> is the continuation, passed to the <code>Cont</code> block.</li><li>Binds <code>response</code> to the result of the following <code><a href="Control-Monad-Cont.html#v:callCC">callCC</a></code> block,
binds <code>exit</code> to the continuation.</li><li>Validates <code>name</code>.
This approach illustrates advantage of using <code><a href="Control-Monad-Cont.html#v:callCC">callCC</a></code> over <code>return</code>.
We pass the continuation to <code>validateName</code>,
and interrupt execution of the <code>Cont</code> block from <em>inside</em> of <code>validateName</code>.</li><li>Returns the welcome message from the <code><a href="Control-Monad-Cont.html#v:callCC">callCC</a></code> block.
This line is not executed if <code>validateName</code> fails.</li><li>Returns from the <code>Cont</code> block.</li></ol></div><h1 id="g:6">Example 3: Using <code>ContT</code> Monad Transformer</h1><div class="doc"><p><code><a href="Control-Monad-Cont.html#t:ContT">ContT</a></code> can be used to add continuation handling to other monads.
Here is an example how to combine it with <code>IO</code> monad:</p><pre>import Control.Monad.Cont
import System.IO

main = do
  hSetBuffering stdout NoBuffering
  runContT (callCC askString) reportResult

askString :: (String -&gt; ContT () IO String) -&gt; ContT () IO String
askString next = do
  liftIO $ putStrLn &quot;Please enter a string&quot;
  s &lt;- liftIO $ getLine
  next s

reportResult :: String -&gt; IO ()
reportResult s = do
  putStrLn (&quot;You entered: &quot; ++ s)</pre><p>Action <code>askString</code> requests user to enter a string,
and passes it to the continuation.
<code>askString</code> takes as a parameter a continuation taking a string parameter,
and returning <code>IO ()</code>.
Compare its signature to <code><a href="Control-Monad-Cont.html#v:runContT">runContT</a></code> definition.</p></div></div></div><div id="footer"><p>Produced by <a href="http://www.haskell.org/haddock/">Haddock</a> version 2.16.1</p></div></body></html>