<?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>>calculateLength :: [a] -> Cont r Int <a name="line-87"></a>>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>>main = do <a name="line-92"></a>> runCont (calculateLength "123") print <a name="line-93"></a>> -- result: 3 <a name="line-94"></a> <a name="line-95"></a>It is possible to chain 'Cont' blocks with @>>=@. <a name="line-96"></a> <a name="line-97"></a>>double :: Int -> Cont r Int <a name="line-98"></a>>double n = return (n * 2) <a name="line-99"></a>> <a name="line-100"></a>>main = do <a name="line-101"></a>> runCont (calculateLength "123" >>= double) print <a name="line-102"></a>> -- 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>>-- Returns a string depending on the length of the name parameter. <a name="line-110"></a>>-- If the provided string is empty, returns an error. <a name="line-111"></a>>-- Otherwise, returns a welcome message. <a name="line-112"></a>>whatsYourName :: String -> String <a name="line-113"></a>>whatsYourName name = <a name="line-114"></a>> (`runCont` id) $ do -- 1 <a name="line-115"></a>> response <- callCC $ \exit -> do -- 2 <a name="line-116"></a>> validateName name exit -- 3 <a name="line-117"></a>> return $ "Welcome, " ++ name ++ "!" -- 4 <a name="line-118"></a>> return response -- 5 <a name="line-119"></a>> <a name="line-120"></a>>validateName name exit = do <a name="line-121"></a>> 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>>import Control.Monad.Cont <a name="line-147"></a>>import System.IO <a name="line-148"></a>> <a name="line-149"></a>>main = do <a name="line-150"></a>> hSetBuffering stdout NoBuffering <a name="line-151"></a>> runContT (callCC askString) reportResult <a name="line-152"></a>> <a name="line-153"></a>>askString :: (String -> ContT () IO String) -> ContT () IO String <a name="line-154"></a>>askString next = do <a name="line-155"></a>> liftIO $ putStrLn "Please enter a string" <a name="line-156"></a>> s <- liftIO $ getLine <a name="line-157"></a>> next s <a name="line-158"></a>> <a name="line-159"></a>>reportResult :: String -> IO () <a name="line-160"></a>>reportResult s = do <a name="line-161"></a>> 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>