This file is indexed.

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

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?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://code.haskell.org/~malcolm/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-conid'>ContT</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span>
<a name="line-62"></a>    <span class='hs-varid'>runContT</span><span class='hs-layout'>,</span>
<a name="line-63"></a>    <span class='hs-varid'>mapContT</span><span class='hs-layout'>,</span>
<a name="line-64"></a>    <span class='hs-varid'>withContT</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-layout'>,</span>
<a name="line-66"></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-67"></a>    <span class='hs-comment'>-- * Example 1: Simple Continuation Usage</span>
<a name="line-68"></a>    <span class='hs-comment'>-- $simpleContExample</span>
<a name="line-69"></a>
<a name="line-70"></a>    <span class='hs-comment'>-- * Example 2: Using @callCC@</span>
<a name="line-71"></a>    <span class='hs-comment'>-- $callCCExample</span>
<a name="line-72"></a>    
<a name="line-73"></a>    <span class='hs-comment'>-- * Example 3: Using @ContT@ Monad Transformer</span>
<a name="line-74"></a>    <span class='hs-comment'>-- $ContTExample</span>
<a name="line-75"></a>  <span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-76"></a>
<a name="line-77"></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-78"></a>
<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>
<a name="line-80"></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-81"></a>
<a name="line-82"></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-83"></a>
<a name="line-84"></a><span class='hs-comment'>{- $simpleContExample
<a name="line-85"></a>Calculating length of a list continuation-style:
<a name="line-86"></a>
<a name="line-87"></a>&gt;calculateLength :: [a] -&gt; Cont r Int
<a name="line-88"></a>&gt;calculateLength l = return (length l)
<a name="line-89"></a>
<a name="line-90"></a>Here we use @calculateLength@ by making it to pass its result to @print@:
<a name="line-91"></a>
<a name="line-92"></a>&gt;main = do
<a name="line-93"></a>&gt;  runCont (calculateLength "123") print
<a name="line-94"></a>&gt;  -- result: 3
<a name="line-95"></a>
<a name="line-96"></a>It is possible to chain 'Cont' blocks with @&gt;&gt;=@.
<a name="line-97"></a>
<a name="line-98"></a>&gt;double :: Int -&gt; Cont r Int
<a name="line-99"></a>&gt;double n = return (n * 2)
<a name="line-100"></a>&gt;
<a name="line-101"></a>&gt;main = do
<a name="line-102"></a>&gt;  runCont (calculateLength "123" &gt;&gt;= double) print
<a name="line-103"></a>&gt;  -- result: 6
<a name="line-104"></a>-}</span>
<a name="line-105"></a>
<a name="line-106"></a><span class='hs-comment'>{- $callCCExample
<a name="line-107"></a>This example gives a taste of how escape continuations work, shows a typical
<a name="line-108"></a>pattern for their usage.
<a name="line-109"></a>
<a name="line-110"></a>&gt;-- Returns a string depending on the length of the name parameter.
<a name="line-111"></a>&gt;-- If the provided string is empty, returns an error.
<a name="line-112"></a>&gt;-- Otherwise, returns a welcome message.
<a name="line-113"></a>&gt;whatsYourName :: String -&gt; String
<a name="line-114"></a>&gt;whatsYourName name =
<a name="line-115"></a>&gt;  (`runCont` id) $ do                      -- 1
<a name="line-116"></a>&gt;    response &lt;- callCC $ \exit -&gt; do       -- 2
<a name="line-117"></a>&gt;      validateName name exit               -- 3
<a name="line-118"></a>&gt;      return $ "Welcome, " ++ name ++ "!"  -- 4
<a name="line-119"></a>&gt;    return response                        -- 5
<a name="line-120"></a>&gt;
<a name="line-121"></a>&gt;validateName name exit = do
<a name="line-122"></a>&gt;  when (null name) (exit "You forgot to tell me your name!")
<a name="line-123"></a>
<a name="line-124"></a>Here is what this example does:
<a name="line-125"></a>
<a name="line-126"></a>(1) Runs an anonymous 'Cont' block and extracts value from it with
<a name="line-127"></a>@(\`runCont\` id)@. Here @id@ is the continuation, passed to the @Cont@ block.
<a name="line-128"></a>
<a name="line-129"></a>(1) Binds @response@ to the result of the following 'Control.Monad.Cont.Class.callCC' block,
<a name="line-130"></a>binds @exit@ to the continuation.
<a name="line-131"></a>
<a name="line-132"></a>(1) Validates @name@.
<a name="line-133"></a>This approach illustrates advantage of using 'Control.Monad.Cont.Class.callCC' over @return@.
<a name="line-134"></a>We pass the continuation to @validateName@,
<a name="line-135"></a>and interrupt execution of the @Cont@ block from /inside/ of @validateName@.
<a name="line-136"></a>
<a name="line-137"></a>(1) Returns the welcome message from the 'Control.Monad.Cont.Class.callCC' block.
<a name="line-138"></a>This line is not executed if @validateName@ fails.
<a name="line-139"></a>
<a name="line-140"></a>(1) Returns from the @Cont@ block.
<a name="line-141"></a>-}</span>
<a name="line-142"></a>
<a name="line-143"></a><span class='hs-comment'>{-$ContTExample
<a name="line-144"></a>'ContT' can be used to add continuation handling to other monads.
<a name="line-145"></a>Here is an example how to combine it with @IO@ monad:
<a name="line-146"></a>
<a name="line-147"></a>&gt;import Control.Monad.Cont
<a name="line-148"></a>&gt;import System.IO
<a name="line-149"></a>&gt;
<a name="line-150"></a>&gt;main = do
<a name="line-151"></a>&gt;  hSetBuffering stdout NoBuffering
<a name="line-152"></a>&gt;  runContT (callCC askString) reportResult
<a name="line-153"></a>&gt;
<a name="line-154"></a>&gt;askString :: (String -&gt; ContT () IO String) -&gt; ContT () IO String
<a name="line-155"></a>&gt;askString next = do
<a name="line-156"></a>&gt;  liftIO $ putStrLn "Please enter a string"
<a name="line-157"></a>&gt;  s &lt;- liftIO $ getLine
<a name="line-158"></a>&gt;  next s
<a name="line-159"></a>&gt;
<a name="line-160"></a>&gt;reportResult :: String -&gt; IO ()
<a name="line-161"></a>&gt;reportResult s = do
<a name="line-162"></a>&gt;  putStrLn ("You entered: " ++ s)
<a name="line-163"></a>
<a name="line-164"></a>Action @askString@ requests user to enter a string,
<a name="line-165"></a>and passes it to the continuation.
<a name="line-166"></a>@askString@ takes as a parameter a continuation taking a string parameter,
<a name="line-167"></a>and returning @IO ()@.
<a name="line-168"></a>Compare its signature to 'runContT' definition.
<a name="line-169"></a>-}</span>
</pre></body>
</html>