Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 1f2b142b9d2ef4849a6f5316fa1c5b12 > files > 2587

ghc-6.10.4-1mdv2010.0.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--Rendered using the Haskell Html Library v0.2-->
<HTML
><HEAD
><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"
><TITLE
>Control.Monad.Cont</TITLE
><LINK HREF="haddock.css" REL="stylesheet" TYPE="text/css"
><SCRIPT SRC="haddock-util.js" TYPE="text/javascript"
></SCRIPT
><SCRIPT TYPE="text/javascript"
>window.onload = function () {setSynopsis("mini_Control-Monad-Cont.html")};</SCRIPT
></HEAD
><BODY
><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
><TR
><TD CLASS="topbar"
><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
><TR
><TD
><IMG SRC="haskell_icon.gif" WIDTH="16" HEIGHT="16" ALT=" "
></TD
><TD CLASS="title"
>mtl-1.1.0.2: Monad transformer library</TD
><TD CLASS="topbut"
><A HREF="index.html"
>Contents</A
></TD
><TD CLASS="topbut"
><A HREF="doc-index.html"
>Index</A
></TD
></TR
></TABLE
></TD
></TR
><TR
><TD CLASS="modulebar"
><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
><TR
><TD
><FONT SIZE="6"
>Control.Monad.Cont</FONT
></TD
><TD ALIGN="right"
><TABLE CLASS="narrow" CELLSPACING="0" CELLPADDING="0"
><TR
><TD CLASS="infohead"
>Portability</TD
><TD CLASS="infoval"
>non-portable (multi-parameter type classes)</TD
></TR
><TR
><TD CLASS="infohead"
>Stability</TD
><TD CLASS="infoval"
>experimental</TD
></TR
><TR
><TD CLASS="infohead"
>Maintainer</TD
><TD CLASS="infoval"
>libraries@haskell.org</TD
></TR
></TABLE
></TD
></TR
></TABLE
></TD
></TR
><TR
><TD CLASS="s15"
></TD
></TR
><TR
><TD
><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
><TR
><TD CLASS="section4"
><B
>Contents</B
></TD
></TR
><TR
><TD
><DL
><DT
><A HREF="#1"
>Example 1: Simple Continuation Usage
</A
></DT
><DT
><A HREF="#2"
>Example 2: Using <TT
>callCC</TT
>
</A
></DT
><DT
><A HREF="#3"
>Example 3: Using <TT
>ContT</TT
> Monad Transformer
</A
></DT
></DL
></TD
></TR
></TABLE
></TD
></TR
><TR
><TD CLASS="s15"
></TD
></TR
><TR
><TD CLASS="section1"
>Description</TD
></TR
><TR
><TD 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
> <TT
><TT
><A HREF="Control-Monad-Cont.html#t%3ACont"
>Cont</A
></TT
> r a</TT
>
</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 <TT
>id</TT
>)
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
></TD
></TR
><TR
><TD CLASS="s15"
></TD
></TR
><TR
><TD CLASS="section1"
>Synopsis</TD
></TR
><TR
><TD CLASS="s15"
></TD
></TR
><TR
><TD CLASS="body"
><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
><TR
><TD CLASS="decl"
>module <A HREF="Control-Monad-Cont-Class.html"
>Control.Monad.Cont.Class</A
></TD
></TR
><TR
><TD CLASS="s8"
></TD
></TR
><TR
><TD CLASS="decl"
><SPAN CLASS="keyword"
>newtype</SPAN
>  <A HREF="#t%3ACont"
>Cont</A
> r a = <A HREF="#v%3ACont"
>Cont</A
> {<TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
><TR
><TD CLASS="recfield"
><A HREF="#v%3ArunCont"
>runCont</A
> :: (a -&gt; r) -&gt; r</TD
></TR
></TABLE
>}</TD
></TR
><TR
><TD CLASS="s8"
></TD
></TR
><TR
><TD CLASS="decl"
><A HREF="#v%3AmapCont"
>mapCont</A
> ::  (r -&gt; r) -&gt; <A HREF="Control-Monad-Cont.html#t%3ACont"
>Cont</A
> r a -&gt; <A HREF="Control-Monad-Cont.html#t%3ACont"
>Cont</A
> r a</TD
></TR
><TR
><TD CLASS="s8"
></TD
></TR
><TR
><TD CLASS="decl"
><A HREF="#v%3AwithCont"
>withCont</A
> ::  ((b -&gt; r) -&gt; a -&gt; r) -&gt; <A HREF="Control-Monad-Cont.html#t%3ACont"
>Cont</A
> r a -&gt; <A HREF="Control-Monad-Cont.html#t%3ACont"
>Cont</A
> r b</TD
></TR
><TR
><TD CLASS="s8"
></TD
></TR
><TR
><TD CLASS="decl"
><SPAN CLASS="keyword"
>newtype</SPAN
>  <A HREF="#t%3AContT"
>ContT</A
> r m a = <A HREF="#v%3AContT"
>ContT</A
> {<TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
><TR
><TD CLASS="recfield"
><A HREF="#v%3ArunContT"
>runContT</A
> :: (a -&gt; m r) -&gt; m r</TD
></TR
></TABLE
>}</TD
></TR
><TR
><TD CLASS="s8"
></TD
></TR
><TR
><TD CLASS="decl"
><A HREF="#v%3AmapContT"
>mapContT</A
> ::  (m r -&gt; m r) -&gt; <A HREF="Control-Monad-Cont.html#t%3AContT"
>ContT</A
> r m a -&gt; <A HREF="Control-Monad-Cont.html#t%3AContT"
>ContT</A
> r m a</TD
></TR
><TR
><TD CLASS="s8"
></TD
></TR
><TR
><TD CLASS="decl"
><A HREF="#v%3AwithContT"
>withContT</A
> ::  ((b -&gt; m r) -&gt; a -&gt; m r) -&gt; <A HREF="Control-Monad-Cont.html#t%3AContT"
>ContT</A
> r m a -&gt; <A HREF="Control-Monad-Cont.html#t%3AContT"
>ContT</A
> r m b</TD
></TR
><TR
><TD CLASS="s8"
></TD
></TR
><TR
><TD CLASS="decl"
>module <A HREF="../base/Control-Monad.html"
>Control.Monad</A
></TD
></TR
><TR
><TD CLASS="s8"
></TD
></TR
><TR
><TD CLASS="decl"
>module <A HREF="Control-Monad-Trans.html"
>Control.Monad.Trans</A
></TD
></TR
></TABLE
></TD
></TR
><TR
><TD CLASS="s15"
></TD
></TR
><TR
><TD CLASS="section1"
>Documentation</TD
></TR
><TR
><TD CLASS="s15"
></TD
></TR
><TR
><TD CLASS="decl"
>module <A HREF="Control-Monad-Cont-Class.html"
>Control.Monad.Cont.Class</A
></TD
></TR
><TR
><TD CLASS="s15"
></TD
></TR
><TR
><TD CLASS="decl"
><SPAN CLASS="keyword"
>newtype</SPAN
>  <A NAME="t:Cont"
><A NAME="t%3ACont"
></A
></A
><B
>Cont</B
> r a </TD
></TR
><TR
><TD CLASS="body"
><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
><TR
><TD CLASS="ndoc"
><P
>Continuation monad.
<TT
>Cont r a</TT
> is a CPS computation that produces an intermediate result
of type <TT
>a</TT
> within a CPS computation whose final result type is <TT
>r</TT
>.
</P
><P
>The <TT
>return</TT
> function simply creates a continuation which passes the value on.
</P
><P
>The <TT
>&gt;&gt;=</TT
> operator adds the bound function into the continuation chain.
</P
></TD
></TR
><TR
><TD CLASS="section4"
>Constructors</TD
></TR
><TR
><TD CLASS="body"
><TABLE CLASS="vanilla" CELLSPACING="5" CELLPADDING="0"
><TR
><TD CLASS="arg"
><A NAME="v:Cont"
><A NAME="v%3ACont"
></A
></A
><B
>Cont</B
></TD
><TD CLASS="rdoc"
></TD
></TR
><TR
><TD CLASS="body" COLSPAN="2"
><TABLE CLASS="vanilla" CELLSPACING="1" CELLPADDING="0"
><TR
><TD CLASS="arg"
><A NAME="v:runCont"
><A NAME="v%3ArunCont"
></A
></A
><B
>runCont</B
> :: (a -&gt; r) -&gt; r</TD
><TD CLASS="rdoc"
><P
>Runs a CPS computation, returns its result after applying
    the final continuation to it.
    Parameters:
</P
><UL
><LI
> a continuation computation (<TT
>Cont</TT
>).
</LI
><LI
> the final continuation, which produces the final result (often <TT
>id</TT
>).
</LI
></UL
></TD
></TR
></TABLE
></TD
></TR
></TABLE
></TD
></TR
><TR
><TD CLASS="section4"
><IMG SRC="minus.gif" CLASS="coll" ONCLICK="toggle(this,'i:Cont')" ALT="show/hide"
> Instances</TD
></TR
><TR
><TD CLASS="body"
><DIV ID="i:Cont" STYLE="display:block;"
><TABLE CLASS="vanilla" CELLSPACING="1" CELLPADDING="0"
><TR
><TD CLASS="decl"
><A HREF="../base/Control-Monad.html#t%3AMonad"
>Monad</A
> (<A HREF="Control-Monad-Cont.html#t%3ACont"
>Cont</A
> r)</TD
></TR
><TR
><TD CLASS="decl"
><A HREF="../base/Control-Monad.html#t%3AFunctor"
>Functor</A
> (<A HREF="Control-Monad-Cont.html#t%3ACont"
>Cont</A
> r)</TD
></TR
><TR
><TD CLASS="decl"
><A HREF="Control-Monad-Cont-Class.html#t%3AMonadCont"
>MonadCont</A
> (<A HREF="Control-Monad-Cont.html#t%3ACont"
>Cont</A
> r)</TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
></TD
></TR
><TR
><TD CLASS="s15"
></TD
></TR
><TR
><TD CLASS="decl"
><A NAME="v:mapCont"
><A NAME="v%3AmapCont"
></A
></A
><B
>mapCont</B
> ::  (r -&gt; r) -&gt; <A HREF="Control-Monad-Cont.html#t%3ACont"
>Cont</A
> r a -&gt; <A HREF="Control-Monad-Cont.html#t%3ACont"
>Cont</A
> r a</TD
></TR
><TR
><TD CLASS="s15"
></TD
></TR
><TR
><TD CLASS="decl"
><A NAME="v:withCont"
><A NAME="v%3AwithCont"
></A
></A
><B
>withCont</B
> ::  ((b -&gt; r) -&gt; a -&gt; r) -&gt; <A HREF="Control-Monad-Cont.html#t%3ACont"
>Cont</A
> r a -&gt; <A HREF="Control-Monad-Cont.html#t%3ACont"
>Cont</A
> r b</TD
></TR
><TR
><TD CLASS="s15"
></TD
></TR
><TR
><TD CLASS="decl"
><SPAN CLASS="keyword"
>newtype</SPAN
>  <A NAME="t:ContT"
><A NAME="t%3AContT"
></A
></A
><B
>ContT</B
> r m a </TD
></TR
><TR
><TD CLASS="body"
><TABLE CLASS="vanilla" CELLSPACING="0" CELLPADDING="0"
><TR
><TD CLASS="ndoc"
>The continuation monad transformer.
Can be used to add continuation handling to other monads.
</TD
></TR
><TR
><TD CLASS="section4"
>Constructors</TD
></TR
><TR
><TD CLASS="body"
><TABLE CLASS="vanilla" CELLSPACING="5" CELLPADDING="0"
><TR
><TD CLASS="arg"
><A NAME="v:ContT"
><A NAME="v%3AContT"
></A
></A
><B
>ContT</B
></TD
><TD CLASS="rdoc"
></TD
></TR
><TR
><TD CLASS="body" COLSPAN="2"
><TABLE CLASS="vanilla" CELLSPACING="1" CELLPADDING="0"
><TR
><TD CLASS="arg"
><A NAME="v:runContT"
><A NAME="v%3ArunContT"
></A
></A
><B
>runContT</B
> :: (a -&gt; m r) -&gt; m r</TD
><TD CLASS="rdoc"
></TD
></TR
></TABLE
></TD
></TR
></TABLE
></TD
></TR
><TR
><TD CLASS="section4"
><IMG SRC="minus.gif" CLASS="coll" ONCLICK="toggle(this,'i:ContT')" ALT="show/hide"
> Instances</TD
></TR
><TR
><TD CLASS="body"
><DIV ID="i:ContT" STYLE="display:block;"
><TABLE CLASS="vanilla" CELLSPACING="1" CELLPADDING="0"
><TR
><TD CLASS="decl"
><A HREF="Control-Monad-State-Class.html#t%3AMonadState"
>MonadState</A
> s m =&gt; <A HREF="Control-Monad-State-Class.html#t%3AMonadState"
>MonadState</A
> s (<A HREF="Control-Monad-Cont.html#t%3AContT"
>ContT</A
> r m)</TD
></TR
><TR
><TD CLASS="decl"
><A HREF="Control-Monad-Reader-Class.html#t%3AMonadReader"
>MonadReader</A
> r' m =&gt; <A HREF="Control-Monad-Reader-Class.html#t%3AMonadReader"
>MonadReader</A
> r' (<A HREF="Control-Monad-Cont.html#t%3AContT"
>ContT</A
> r m)</TD
></TR
><TR
><TD CLASS="decl"
><A HREF="Control-Monad-Trans.html#t%3AMonadTrans"
>MonadTrans</A
> (<A HREF="Control-Monad-Cont.html#t%3AContT"
>ContT</A
> r)</TD
></TR
><TR
><TD CLASS="decl"
><A HREF="../base/Control-Monad.html#t%3AMonad"
>Monad</A
> m =&gt; <A HREF="../base/Control-Monad.html#t%3AMonad"
>Monad</A
> (<A HREF="Control-Monad-Cont.html#t%3AContT"
>ContT</A
> r m)</TD
></TR
><TR
><TD CLASS="decl"
><A HREF="../base/Control-Monad.html#t%3AMonad"
>Monad</A
> m =&gt; <A HREF="../base/Control-Monad.html#t%3AFunctor"
>Functor</A
> (<A HREF="Control-Monad-Cont.html#t%3AContT"
>ContT</A
> r m)</TD
></TR
><TR
><TD CLASS="decl"
><A HREF="Control-Monad-Trans.html#t%3AMonadIO"
>MonadIO</A
> m =&gt; <A HREF="Control-Monad-Trans.html#t%3AMonadIO"
>MonadIO</A
> (<A HREF="Control-Monad-Cont.html#t%3AContT"
>ContT</A
> r m)</TD
></TR
><TR
><TD CLASS="decl"
><A HREF="../base/Control-Monad.html#t%3AMonad"
>Monad</A
> m =&gt; <A HREF="Control-Monad-Cont-Class.html#t%3AMonadCont"
>MonadCont</A
> (<A HREF="Control-Monad-Cont.html#t%3AContT"
>ContT</A
> r m)</TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
></TD
></TR
><TR
><TD CLASS="s15"
></TD
></TR
><TR
><TD CLASS="decl"
><A NAME="v:mapContT"
><A NAME="v%3AmapContT"
></A
></A
><B
>mapContT</B
> ::  (m r -&gt; m r) -&gt; <A HREF="Control-Monad-Cont.html#t%3AContT"
>ContT</A
> r m a -&gt; <A HREF="Control-Monad-Cont.html#t%3AContT"
>ContT</A
> r m a</TD
></TR
><TR
><TD CLASS="s15"
></TD
></TR
><TR
><TD CLASS="decl"
><A NAME="v:withContT"
><A NAME="v%3AwithContT"
></A
></A
><B
>withContT</B
> ::  ((b -&gt; m r) -&gt; a -&gt; m r) -&gt; <A HREF="Control-Monad-Cont.html#t%3AContT"
>ContT</A
> r m a -&gt; <A HREF="Control-Monad-Cont.html#t%3AContT"
>ContT</A
> r m b</TD
></TR
><TR
><TD CLASS="s15"
></TD
></TR
><TR
><TD CLASS="decl"
>module <A HREF="../base/Control-Monad.html"
>Control.Monad</A
></TD
></TR
><TR
><TD CLASS="s15"
></TD
></TR
><TR
><TD CLASS="decl"
>module <A HREF="Control-Monad-Trans.html"
>Control.Monad.Trans</A
></TD
></TR
><TR
><TD CLASS="s15"
></TD
></TR
><TR
><TD CLASS="section1"
><A NAME="1"
><A NAME="1"
>Example 1: Simple Continuation Usage
</A
></A
></TD
></TR
><TR
><TD CLASS="s15"
></TD
></TR
><TR
><TD 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 <TT
>calculateLength</TT
> by making it to pass its result to <TT
>print</TT
>:
</P
><PRE
>main = do
  runCont (calculateLength &quot;123&quot;) print
  -- result: 3
</PRE
><P
>It is possible to chain <TT
><A HREF="Control-Monad-Cont.html#t%3ACont"
>Cont</A
></TT
> blocks with <TT
>&gt;&gt;=</TT
>.
</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
></TD
></TR
><TR
><TD CLASS="s15"
></TD
></TR
><TR
><TD CLASS="section1"
><A NAME="2"
><A NAME="2"
>Example 2: Using <TT
>callCC</TT
>
</A
></A
></TD
></TR
><TR
><TD CLASS="s15"
></TD
></TR
><TR
><TD 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 <TT
><A HREF="Control-Monad-Cont.html#t%3ACont"
>Cont</A
></TT
> block and extracts value from it with
<TT
>(`runCont` id)</TT
>. Here <TT
>id</TT
> is the continuation, passed to the <TT
>Cont</TT
> block.
</LI
><LI
> Binds <TT
>response</TT
> to the result of the following <TT
><A HREF="Control-Monad-Cont-Class.html#v%3AcallCC"
>callCC</A
></TT
> block,
binds <TT
>exit</TT
> to the continuation.
</LI
><LI
> Validates <TT
>name</TT
>.
This approach illustrates advantage of using <TT
><A HREF="Control-Monad-Cont-Class.html#v%3AcallCC"
>callCC</A
></TT
> over <TT
>return</TT
>.
We pass the continuation to <TT
>validateName</TT
>,
and interrupt execution of the <TT
>Cont</TT
> block from <EM
>inside</EM
> of <TT
>validateName</TT
>.
</LI
><LI
> Returns the welcome message from the <TT
>callCC</TT
> block.
This line is not executed if <TT
>validateName</TT
> fails.
</LI
><LI
> Returns from the <TT
>Cont</TT
> block.
</LI
></OL
></TD
></TR
><TR
><TD CLASS="s15"
></TD
></TR
><TR
><TD CLASS="section1"
><A NAME="3"
><A NAME="3"
>Example 3: Using <TT
>ContT</TT
> Monad Transformer
</A
></A
></TD
></TR
><TR
><TD CLASS="s15"
></TD
></TR
><TR
><TD CLASS="doc"
><P
><TT
><A HREF="Control-Monad-Cont.html#t%3AContT"
>ContT</A
></TT
> can be used to add continuation handling to other monads.
Here is an example how to combine it with <TT
>IO</TT
> 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 <TT
>askString</TT
> requests user to enter a string,
and passes it to the continuation.
<TT
>askString</TT
> takes as a parameter a continuation taking a string parameter,
and returning <TT
>IO ()</TT
>.
Compare its signature to <TT
><A HREF="Control-Monad-Cont.html#v%3ArunContT"
>runContT</A
></TT
> definition.
</P
></TD
></TR
><TR
><TD CLASS="s15"
></TD
></TR
><TR
><TD CLASS="botbar"
>Produced by <A HREF="http://www.haskell.org/haddock/"
>Haddock</A
> version 2.4.2</TD
></TR
></TABLE
></BODY
></HTML
>