Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 6e7b8b29c9973053a0fbeb2899ecd9cb > files > 155

ghc-mtl-devel-2.0.1.0-8.fc15.i686.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<!-- Generated by HsColour, http://www.cs.york.ac.uk/fp/darcs/hscolour/ -->
<title>Control/Monad/Cont.hs</title>
<link type='text/css' rel='stylesheet' href='hscolour.css' />
</head>
<body>
<pre><a name="line-1"></a><span class='hs-comment'>{- |
<a name="line-2"></a>Module      :  Control.Monad.Cont
<a name="line-3"></a>Copyright   :  (c) The University of Glasgow 2001,
<a name="line-4"></a>               (c) Jeff Newbern 2003-2007,
<a name="line-5"></a>               (c) Andriy Palamarchuk 2007
<a name="line-6"></a>License     :  BSD-style (see the file LICENSE)
<a name="line-7"></a>
<a name="line-8"></a>Maintainer  :  libraries@haskell.org
<a name="line-9"></a>Stability   :  experimental
<a name="line-10"></a>Portability :  portable
<a name="line-11"></a>
<a name="line-12"></a>[Computation type:] Computations which can be interrupted and resumed.
<a name="line-13"></a>
<a name="line-14"></a>[Binding strategy:] Binding a function to a monadic value creates
<a name="line-15"></a>a new continuation which uses the function as the continuation of the monadic
<a name="line-16"></a>computation.
<a name="line-17"></a>
<a name="line-18"></a>[Useful for:] Complex control structures, error handling,
<a name="line-19"></a>and creating co-routines.
<a name="line-20"></a>
<a name="line-21"></a>[Zero and plus:] None.
<a name="line-22"></a>
<a name="line-23"></a>[Example type:] @'Cont' r a@
<a name="line-24"></a>
<a name="line-25"></a>The Continuation monad represents computations in continuation-passing style
<a name="line-26"></a>(CPS).
<a name="line-27"></a>In continuation-passing style function result is not returned,
<a name="line-28"></a>but instead is passed to another function,
<a name="line-29"></a>received as a parameter (continuation).
<a name="line-30"></a>Computations are built up from sequences
<a name="line-31"></a>of nested continuations, terminated by a final continuation (often @id@)
<a name="line-32"></a>which produces the final result.
<a name="line-33"></a>Since continuations are functions which represent the future of a computation,
<a name="line-34"></a>manipulation of the continuation functions can achieve complex manipulations
<a name="line-35"></a>of the future of the computation,
<a name="line-36"></a>such as interrupting a computation in the middle, aborting a portion
<a name="line-37"></a>of a computation, restarting a computation, and interleaving execution of
<a name="line-38"></a>computations.
<a name="line-39"></a>The Continuation monad adapts CPS to the structure of a monad.
<a name="line-40"></a>
<a name="line-41"></a>Before using the Continuation monad, be sure that you have
<a name="line-42"></a>a firm understanding of continuation-passing style
<a name="line-43"></a>and that continuations represent the best solution to your particular
<a name="line-44"></a>design problem.
<a name="line-45"></a>Many algorithms which require continuations in other languages do not require
<a name="line-46"></a>them in Haskell, due to Haskell's lazy semantics.
<a name="line-47"></a>Abuse of the Continuation monad can produce code that is impossible
<a name="line-48"></a>to understand and maintain.
<a name="line-49"></a>-}</span>
<a name="line-50"></a>
<a name="line-51"></a><span class='hs-keyword'>module</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>Cont</span> <span class='hs-layout'>(</span>
<a name="line-52"></a>    <span class='hs-comment'>-- * MonadCont class</span>
<a name="line-53"></a>    <span class='hs-conid'>MonadCont</span><span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span>
<a name="line-54"></a>    <span class='hs-comment'>-- * The Cont monad</span>
<a name="line-55"></a>    <span class='hs-conid'>Cont</span><span class='hs-layout'>,</span>
<a name="line-56"></a>    <span class='hs-varid'>cont</span><span class='hs-layout'>,</span>
<a name="line-57"></a>    <span class='hs-varid'>runCont</span><span class='hs-layout'>,</span>
<a name="line-58"></a>    <span class='hs-varid'>mapCont</span><span class='hs-layout'>,</span>
<a name="line-59"></a>    <span class='hs-varid'>withCont</span><span class='hs-layout'>,</span>
<a name="line-60"></a>    <span class='hs-comment'>-- * The ContT monad transformer</span>
<a name="line-61"></a>    <span class='hs-conid'>ContT</span><span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span>
<a name="line-62"></a>    <span class='hs-varid'>mapContT</span><span class='hs-layout'>,</span>
<a name="line-63"></a>    <span class='hs-varid'>withContT</span><span class='hs-layout'>,</span>
<a name="line-64"></a>    <span class='hs-keyword'>module</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-layout'>,</span>
<a name="line-65"></a>    <span class='hs-keyword'>module</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>Trans</span><span class='hs-layout'>,</span>
<a name="line-66"></a>    <span class='hs-comment'>-- * Example 1: Simple Continuation Usage</span>
<a name="line-67"></a>    <span class='hs-comment'>-- $simpleContExample</span>
<a name="line-68"></a>
<a name="line-69"></a>    <span class='hs-comment'>-- * Example 2: Using @callCC@</span>
<a name="line-70"></a>    <span class='hs-comment'>-- $callCCExample</span>
<a name="line-71"></a>    
<a name="line-72"></a>    <span class='hs-comment'>-- * Example 3: Using @ContT@ Monad Transformer</span>
<a name="line-73"></a>    <span class='hs-comment'>-- $ContTExample</span>
<a name="line-74"></a>  <span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-75"></a>
<a name="line-76"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>Cont</span><span class='hs-varop'>.</span><span class='hs-conid'>Class</span>
<a name="line-77"></a>
<a name="line-78"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>Trans</span>
<a name="line-79"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span><span class='hs-varop'>.</span><span class='hs-conid'>Trans</span><span class='hs-varop'>.</span><span class='hs-conid'>Cont</span>
<a name="line-80"></a>
<a name="line-81"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>Control</span><span class='hs-varop'>.</span><span class='hs-conid'>Monad</span>
<a name="line-82"></a>
<a name="line-83"></a><span class='hs-comment'>{- $simpleContExample
<a name="line-84"></a>Calculating length of a list continuation-style:
<a name="line-85"></a>
<a name="line-86"></a>&gt;calculateLength :: [a] -&gt; Cont r Int
<a name="line-87"></a>&gt;calculateLength l = return (length l)
<a name="line-88"></a>
<a name="line-89"></a>Here we use @calculateLength@ by making it to pass its result to @print@:
<a name="line-90"></a>
<a name="line-91"></a>&gt;main = do
<a name="line-92"></a>&gt;  runCont (calculateLength "123") print
<a name="line-93"></a>&gt;  -- result: 3
<a name="line-94"></a>
<a name="line-95"></a>It is possible to chain 'Cont' blocks with @&gt;&gt;=@.
<a name="line-96"></a>
<a name="line-97"></a>&gt;double :: Int -&gt; Cont r Int
<a name="line-98"></a>&gt;double n = return (n * 2)
<a name="line-99"></a>&gt;
<a name="line-100"></a>&gt;main = do
<a name="line-101"></a>&gt;  runCont (calculateLength "123" &gt;&gt;= double) print
<a name="line-102"></a>&gt;  -- result: 6
<a name="line-103"></a>-}</span>
<a name="line-104"></a>
<a name="line-105"></a><span class='hs-comment'>{- $callCCExample
<a name="line-106"></a>This example gives a taste of how escape continuations work, shows a typical
<a name="line-107"></a>pattern for their usage.
<a name="line-108"></a>
<a name="line-109"></a>&gt;-- Returns a string depending on the length of the name parameter.
<a name="line-110"></a>&gt;-- If the provided string is empty, returns an error.
<a name="line-111"></a>&gt;-- Otherwise, returns a welcome message.
<a name="line-112"></a>&gt;whatsYourName :: String -&gt; String
<a name="line-113"></a>&gt;whatsYourName name =
<a name="line-114"></a>&gt;  (`runCont` id) $ do                      -- 1
<a name="line-115"></a>&gt;    response &lt;- callCC $ \exit -&gt; do       -- 2
<a name="line-116"></a>&gt;      validateName name exit               -- 3
<a name="line-117"></a>&gt;      return $ "Welcome, " ++ name ++ "!"  -- 4
<a name="line-118"></a>&gt;    return response                        -- 5
<a name="line-119"></a>&gt;
<a name="line-120"></a>&gt;validateName name exit = do
<a name="line-121"></a>&gt;  when (null name) (exit "You forgot to tell me your name!")
<a name="line-122"></a>
<a name="line-123"></a>Here is what this example does:
<a name="line-124"></a>
<a name="line-125"></a>(1) Runs an anonymous 'Cont' block and extracts value from it with
<a name="line-126"></a>@(\`runCont\` id)@. Here @id@ is the continuation, passed to the @Cont@ block.
<a name="line-127"></a>
<a name="line-128"></a>(1) Binds @response@ to the result of the following 'Control.Monad.Cont.Class.callCC' block,
<a name="line-129"></a>binds @exit@ to the continuation.
<a name="line-130"></a>
<a name="line-131"></a>(1) Validates @name@.
<a name="line-132"></a>This approach illustrates advantage of using 'Control.Monad.Cont.Class.callCC' over @return@.
<a name="line-133"></a>We pass the continuation to @validateName@,
<a name="line-134"></a>and interrupt execution of the @Cont@ block from /inside/ of @validateName@.
<a name="line-135"></a>
<a name="line-136"></a>(1) Returns the welcome message from the 'Control.Monad.Cont.Class.callCC' block.
<a name="line-137"></a>This line is not executed if @validateName@ fails.
<a name="line-138"></a>
<a name="line-139"></a>(1) Returns from the @Cont@ block.
<a name="line-140"></a>-}</span>
<a name="line-141"></a>
<a name="line-142"></a><span class='hs-comment'>{-$ContTExample
<a name="line-143"></a>'ContT' can be used to add continuation handling to other monads.
<a name="line-144"></a>Here is an example how to combine it with @IO@ monad:
<a name="line-145"></a>
<a name="line-146"></a>&gt;import Control.Monad.Cont
<a name="line-147"></a>&gt;import System.IO
<a name="line-148"></a>&gt;
<a name="line-149"></a>&gt;main = do
<a name="line-150"></a>&gt;  hSetBuffering stdout NoBuffering
<a name="line-151"></a>&gt;  runContT (callCC askString) reportResult
<a name="line-152"></a>&gt;
<a name="line-153"></a>&gt;askString :: (String -&gt; ContT () IO String) -&gt; ContT () IO String
<a name="line-154"></a>&gt;askString next = do
<a name="line-155"></a>&gt;  liftIO $ putStrLn "Please enter a string"
<a name="line-156"></a>&gt;  s &lt;- liftIO $ getLine
<a name="line-157"></a>&gt;  next s
<a name="line-158"></a>&gt;
<a name="line-159"></a>&gt;reportResult :: String -&gt; IO ()
<a name="line-160"></a>&gt;reportResult s = do
<a name="line-161"></a>&gt;  putStrLn ("You entered: " ++ s)
<a name="line-162"></a>
<a name="line-163"></a>Action @askString@ requests user to enter a string,
<a name="line-164"></a>and passes it to the continuation.
<a name="line-165"></a>@askString@ takes as a parameter a continuation taking a string parameter,
<a name="line-166"></a>and returning @IO ()@.
<a name="line-167"></a>Compare its signature to 'runContT' definition.
<a name="line-168"></a>-}</span>
</pre></body>
</html>