/usr/share/doc/mlton/guide/MLtonContIsolateImplementation is in mlton-doc 20100608-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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta name="robots" content="index,nofollow">
<title>MLtonContIsolateImplementation - MLton Standard ML Compiler (SML Compiler)</title>
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="all" href="common.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="screen" href="screen.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="print" href="print.css">
<link rel="Start" href="Home">
</head>
<body lang="en" dir="ltr">
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-833377-1";
urchinTracker();
</script>
<table bgcolor = lightblue cellspacing = 0 style = "border: 0px;" width = 100%>
<tr>
<td style = "
border: 0px;
color: darkblue;
font-size: 150%;
text-align: left;">
<a class = mltona href="Home">MLton MLTONWIKIVERSION</a>
<td style = "
border: 0px;
font-size: 150%;
text-align: center;
width: 50%;">
MLtonContIsolateImplementation
<td style = "
border: 0px;
text-align: right;">
<table cellspacing = 0 style = "border: 0px">
<tr style = "vertical-align: middle;">
</table>
<tr style = "background-color: white;">
<td colspan = 3
style = "
border: 0px;
font-size:70%;
text-align: right;">
<a href = "Home">Home</a>
<a href = "TitleIndex">Index</a>
</table>
<div id="content" lang="en" dir="ltr">
As noted before, it is fairly easy to get the operational behavior of <tt>isolate</tt> with just <tt>callcc</tt> and <tt>throw</tt>, but establishing the right space behavior is trickier. Here, we show how to start from the obvious, but inefficient, implementation of <tt>isolate</tt> using only <tt>callcc</tt> and <tt>throw</tt>, and 'derive' an equivalent, but more efficient, implementation of <tt>isolate</tt> using MLton's primitive stack capture and copy operations. This isn't a formal derivation, as we are not formally showing the equivalence of the programs (though I believe that they are all equivalent, modulo the space behavior). <p>
Here is a direct implementation of isolate using only <tt>callcc</tt> and <tt>throw</tt>:
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> isolate: ('a -> unit) -> 'a t =
<B><FONT COLOR="#A020F0">fn</FONT></B> (f: 'a -> unit) =>
callcc
(<B><FONT COLOR="#A020F0">fn</FONT></B> k1 =>
<B><FONT COLOR="#0000FF">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> x = callcc (<B><FONT COLOR="#A020F0">fn</FONT></B> k2 => throw (k1, k2))
<B><FONT COLOR="#A020F0">val</FONT></B> _ = (f x ; Exit.topLevelSuffix ())
<B><FONT COLOR="#A020F0">handle</FONT></B> exn => MLtonExn.topLevelHandler exn
<B><FONT COLOR="#0000FF">in</FONT></B>
<B><FONT COLOR="#A020F0">raise</FONT></B> Fail <B><FONT COLOR="#BC8F8F">"MLton.Cont.isolate: return from (wrapped) func"</FONT></B>
<B><FONT COLOR="#0000FF">end</FONT></B>)
</PRE>
<p>
</p>
<p>
We use the standard nested <tt>callcc</tt> trick to return a continuation that is ready to receive an argument, execute the isolated function, and exit the program. Both <tt>Exit.topLevelSuffix</tt> and <tt>MLtonExn.topLevelHandler</tt> will terminate the program.
</p>
<p>
Throwing to an isolated function will execute the function in a 'semantically' empty context, in the sense that we never re-execute the 'original' continuation of the call to isolate (i.e., the context that was in place at the time <tt>isolate</tt> was called). However, we assume that the compiler isn't able to recognize that the 'original' continuation is unused; for example, while we (the programmer) know that <tt>Exit.topLevelSuffix</tt> and <tt>MLtonExn.topLevelHandler</tt> will terminate the program, the compiler may only see opaque calls to unknown foreign-functions. So, that original continuation (in its entirety) is part of the continuation returned by <tt>isolate</tt> and throwing to the continuation returned by <tt>isolate</tt> will execute <tt>f x</tt> (with the exit wrapper) in the context of that original continuation. Thus, the garbage collector will retain everything reachable from that original continuation during the evaluation of <tt>f x</tt>, even though it is 'semantically' garbage.
</p>
<p>
Note that this space-leak is independent of the implementation of continuations (it arises in both MLton's stack copying implementation of continuations and would arise in SML/NJ's CPS-translation implementation); we are only assuming that the implementation can't 'see' the program termination, and so must retain the original continuation (and anything reachable from it).
</p>
<p>
So, we need an 'empty' continuation in which to execute <tt>f x</tt>. (No surprise there, as that is the written description of <tt>isolate</tt>.) To do this, we capture a top-level continuation and throw to that in order to execute <tt>f x</tt>:
</p>
<pre class=code>
<B><FONT COLOR="#0000FF">local</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> base: (unit -> unit) t =
callcc
(<B><FONT COLOR="#A020F0">fn</FONT></B> k1 =>
<B><FONT COLOR="#0000FF">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> th = callcc (<B><FONT COLOR="#A020F0">fn</FONT></B> k2 => throw (k1, k2))
<B><FONT COLOR="#A020F0">val</FONT></B> _ = (th () ; Exit.topLevelSuffix ())
<B><FONT COLOR="#A020F0">handle</FONT></B> exn => MLtonExn.topLevelHandler exn
<B><FONT COLOR="#0000FF">in</FONT></B>
<B><FONT COLOR="#A020F0">raise</FONT></B> Fail <B><FONT COLOR="#BC8F8F">"MLton.Cont.isolate: return from (wrapped) func"</FONT></B>
<B><FONT COLOR="#0000FF">end</FONT></B>)
<B><FONT COLOR="#0000FF">in</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> isolate: ('a -> unit) -> 'a t =
<B><FONT COLOR="#A020F0">fn</FONT></B> (f: 'a -> unit) =>
callcc
(<B><FONT COLOR="#A020F0">fn</FONT></B> k1 =>
<B><FONT COLOR="#0000FF">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> x = callcc (<B><FONT COLOR="#A020F0">fn</FONT></B> k2 => throw (k1, k2))
<B><FONT COLOR="#0000FF">in</FONT></B>
throw (base, <B><FONT COLOR="#A020F0">fn</FONT></B> () => f x)
<B><FONT COLOR="#0000FF">end</FONT></B>)
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
</p>
<p>
We presume that <tt>base</tt> is evaluated 'early' in the program. There is a subtlety here, because one needs to believe that this <tt>base</tt> continuation (which technically corresponds to the entire rest of the program evaluation) 'works' as an empty context; in particular, we want it to be the case that executing <tt>f x</tt> in the <tt>base</tt> context retains less space than executing <tt>f x</tt> in the context in place at the call to <tt>isolate</tt> (as occurred in the previous implementation of <tt>isolate</tt>). This isn't particularly easy to believe if one takes a normal substitution-based operational semantics, because it seems that the context captured and bound to <tt>base</tt> is arbitrarily large. However, this context is mostly unevaluated code; the only heap-allocated values that are reachable from it are those that were evaluated before the evaluation of <tt>base</tt> (and used in the program after the evaluation of <tt>base</tt>). Assuming that <tt>base</tt> is evaluated 'early' in the program, we conclude that there are few heap-allocated values reachable from its continuation. In contrast, the previous implementation of <tt>isolate</tt> could capture a context that has many heap-allocated values reachable from it (because we could evaluate <tt>isolate f</tt> 'late' in the program and 'deep' in a call stack), which would all remain reachable during the evaluation of <tt>f x</tt>. [We'll return to this point later, as it is taking a slightly MLton-esque view of the evaluation of a program, and may not apply as strongly to other implementations (e.g., SML/NJ).]
</p>
<p>
Now, once we throw to <tt>base</tt> and begin executing <tt>f x</tt>, only the heap-allocated values reachable from <tt>f</tt> and <tt>x</tt> and the few heap-allocated values reachable from <tt>base</tt> are retained by the garbage collector. So, it seems that <tt>base</tt> 'works' as an empty context.
</p>
<p>
But, what about the continuation returned from <tt>isolate f</tt>? Note that the continuation returned by <tt>isolate</tt> is one that receives an argument <tt>x</tt> and then throws to <tt>base</tt> to evaluate <tt>f x</tt>. If we used a CPS-translation implementation (and assume sufficient beta-contractions to eliminate administrative redexes), then the original continuation passed to <tt>isolate</tt> (i.e., the continuation bound to <tt>k1</tt>) will not be free in the continuation returned by <tt>isolate f</tt>. Rather, the only free variables in the continuation returned by <tt>isolate f</tt> will be <tt>base</tt> and <tt>f</tt>, so the only heap-allocated values reachable from the continuation returned by <tt>isolate f</tt> will be those values reachable from <tt>base</tt> (assumed to be few) and those values reachable from <tt>f</tt> (necessary in order to execute <tt>f</tt> at some later point).
</p>
<p>
But, MLton doesn't use a CPS-translation implementation. Rather, at each call to <tt>callcc</tt> in the body of <tt>isolate</tt>, MLton will copy the current execution stack. Thus, <tt>k2</tt> (the continuation returned by <tt>isolate f</tt>) will include execution stack at the time of the call to <tt>isolate f</tt> -- that is, it will include the 'original' continuation of the call to <tt>isolate f</tt>. Thus, the heap-allocated values reachable from the continuation returned by <tt>isolate f</tt> will include those values reachable from <tt>base</tt>, those values reachable from <tt>f</tt>, and those values reachable from the original continuation of the call to <tt>isolate f</tt>. So, just holding on to the continuation returned by <tt>isolate f</tt> will retain all of the heap-allocated values live at the time <tt>isolate f</tt> was called. This leaks space, since, 'semantically', the continuation returned by <tt>isolate f</tt> only needs the heap-allocated values reachable from <tt>f</tt> (and <tt>base</tt>).
</p>
<p>
In practice, this probably isn't a significant issue. A common use of <tt>isolate</tt> is implement <tt>abort</tt>:
<pre class=code>
<B><FONT COLOR="#A020F0">fun</FONT></B> abort th = throw (isolate th, ())
</PRE>
The continuation returned by <tt>isolate th</tt> is dead immediately after being thrown to -- the continuation isn't retained, so neither is the 'semantic' garbage it would have retained.
</p>
<p>
But, it is easy enough to 'move' onto the 'empty' context <tt>base</tt> the capturing of the context that we want to be returned by <tt>isolate f</tt>:
</p>
<pre class=code>
<B><FONT COLOR="#0000FF">local</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> base: (unit -> unit) t =
callcc
(<B><FONT COLOR="#A020F0">fn</FONT></B> k1 =>
<B><FONT COLOR="#0000FF">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> th = callcc (<B><FONT COLOR="#A020F0">fn</FONT></B> k2 => throw (k1, k2))
<B><FONT COLOR="#A020F0">val</FONT></B> _ = (th () ; Exit.topLevelSuffix ())
<B><FONT COLOR="#A020F0">handle</FONT></B> exn => MLtonExn.topLevelHandler exn
<B><FONT COLOR="#0000FF">in</FONT></B>
<B><FONT COLOR="#A020F0">raise</FONT></B> Fail <B><FONT COLOR="#BC8F8F">"MLton.Cont.isolate: return from (wrapped) func"</FONT></B>
<B><FONT COLOR="#0000FF">end</FONT></B>)
<B><FONT COLOR="#0000FF">in</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> isolate: ('a -> unit) -> 'a t =
<B><FONT COLOR="#A020F0">fn</FONT></B> (f: 'a -> unit) =>
callcc
(<B><FONT COLOR="#A020F0">fn</FONT></B> k1 =>
throw (base, <B><FONT COLOR="#A020F0">fn</FONT></B> () =>
<B><FONT COLOR="#0000FF">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> x = callcc (<B><FONT COLOR="#A020F0">fn</FONT></B> k2 => throw (k1, k2))
<B><FONT COLOR="#0000FF">in</FONT></B>
throw (base, <B><FONT COLOR="#A020F0">fn</FONT></B> () => f x)
<B><FONT COLOR="#0000FF">end</FONT></B>))
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
</p>
<p>
This implementation now has the right space behavior; the continuation returned by <tt>isolate f</tt> will only retain the heap-allocated values reachable from <tt>f</tt> and from <tt>base</tt>. (Technically, the continuation will retain two copies of the stack that was in place at the time <tt>base</tt> was evaluated, but we are assuming that that stack small.)
</p>
<p>
One minor inefficiency of this implementation (given MLton's implementation of continuations) is that every <tt>callcc</tt> and <tt>throw</tt> entails copying a stack (albeit, some of them are small). We can avoid this in the evaluation of <tt>base</tt> by using a reference cell, because <tt>base</tt> is evaluated at the top-level:
</p>
<pre class=code>
<B><FONT COLOR="#0000FF">local</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> base: (unit -> unit) option t =
<B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> baseRef: (unit -> unit) option t option ref = ref NONE
<B><FONT COLOR="#A020F0">val</FONT></B> th = callcc (<B><FONT COLOR="#A020F0">fn</FONT></B> k => (base := SOME k; NONE))
<B><FONT COLOR="#A020F0">in</FONT></B>
<B><FONT COLOR="#A020F0">case</FONT></B> th <B><FONT COLOR="#A020F0">of</FONT></B>
NONE => (<B><FONT COLOR="#A020F0">case</FONT></B> !baseRef <B><FONT COLOR="#A020F0">of</FONT></B>
NONE => <B><FONT COLOR="#A020F0">raise</FONT></B> Fail <B><FONT COLOR="#BC8F8F">"MLton.Cont.isolate: missing base"</FONT></B>
| SOME base => base)
| SOME th => <B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> _ = (th () ; Exit.topLevelSuffix ())
<B><FONT COLOR="#A020F0">handle</FONT></B> exn => MLtonExn.topLevelHandler exn
<B><FONT COLOR="#A020F0">in</FONT></B>
<B><FONT COLOR="#A020F0">raise</FONT></B> Fail <B><FONT COLOR="#BC8F8F">"MLton.Cont.isolate: return from (wrapped)
func"</FONT></B>
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#0000FF">in</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> isolate: ('a -> unit) -> 'a t =
<B><FONT COLOR="#A020F0">fn</FONT></B> (f: 'a -> unit) =>
callcc
(<B><FONT COLOR="#A020F0">fn</FONT></B> k1 =>
throw (base, SOME (<B><FONT COLOR="#A020F0">fn</FONT></B> () =>
<B><FONT COLOR="#0000FF">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> x = callcc (<B><FONT COLOR="#A020F0">fn</FONT></B> k2 => throw (k1, k2))
<B><FONT COLOR="#0000FF">in</FONT></B>
throw (base, SOME (<B><FONT COLOR="#A020F0">fn</FONT></B> () => f x))
<B><FONT COLOR="#0000FF">end</FONT></B>)))
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
</p>
<p>
Now, to evaluate <tt>base</tt>, we only copy the stack once (instead of 3 times). Because we don't have a dummy continuation around to initialize the reference cell, the reference cell holds a continuation <tt>option</tt>. To distinguish between the original evaluation of <tt>base</tt> (when we want to return the continuation) and the subsequent evaluations of <tt>base</tt> (when we want to evaluate a thunk), we capture a <tt>(unit -> unit) option</tt> continuation.
</p>
<p>
This seems to be as far as we can go without exploiting the concrete implementation of continuations in <a href="MLtonCont">MLtonCont</a>. Examining the implementation, we note that the type of continuations is given by
<pre class=code>
<B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> 'a t </FONT></B>=<B><FONT COLOR="#228B22"> (unit -> 'a) -> unit
</FONT></B></PRE>
and the implementation of <tt>throw</tt> is given by
<pre class=code>
<B><FONT COLOR="#A020F0">fun</FONT></B> ('a, 'b) throw' (k: 'a t, v: unit -> 'a): 'b =
(k v; <B><FONT COLOR="#A020F0">raise</FONT></B> Fail <B><FONT COLOR="#BC8F8F">"MLton.Cont.throw': return from continuation"</FONT></B>)
<B><FONT COLOR="#A020F0">fun</FONT></B> ('a, 'b) throw (k: 'a t, v: 'a): 'b = throw' (k, <B><FONT COLOR="#A020F0">fn</FONT></B> () => v)
</PRE>
</p>
<p>
Suffice to say, a continuation is simply a function that accepts a thunk to yield the thrown value and the body of the function performs the actual throw. Using this knowledge, we can create a dummy continuation to initialize <tt>baseRef</tt> and greatly simplify the body of <tt>isolate</tt>:
</p>
<pre class=code>
<B><FONT COLOR="#0000FF">local</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> base: (unit -> unit) option t =
<B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> baseRef: (unit -> unit) option t ref =
ref (<B><FONT COLOR="#A020F0">fn</FONT></B> _ => <B><FONT COLOR="#A020F0">raise</FONT></B> Fail <B><FONT COLOR="#BC8F8F">"MLton.Cont.isolate: missing base"</FONT></B>)
<B><FONT COLOR="#A020F0">val</FONT></B> th = callcc (<B><FONT COLOR="#A020F0">fn</FONT></B> k => (baseRef := k; NONE))
<B><FONT COLOR="#A020F0">in</FONT></B>
<B><FONT COLOR="#A020F0">case</FONT></B> th <B><FONT COLOR="#A020F0">of</FONT></B>
NONE => !baseRef
| SOME th => <B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> _ = (th () ; Exit.topLevelSuffix ())
<B><FONT COLOR="#A020F0">handle</FONT></B> exn => MLtonExn.topLevelHandler exn
<B><FONT COLOR="#A020F0">in</FONT></B>
<B><FONT COLOR="#A020F0">raise</FONT></B> Fail <B><FONT COLOR="#BC8F8F">"MLton.Cont.isolate: return from (wrapped)
func"</FONT></B>
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#0000FF">in</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> isolate: ('a -> unit) -> 'a t =
<B><FONT COLOR="#A020F0">fn</FONT></B> (f: 'a -> unit) =>
<B><FONT COLOR="#A020F0">fn</FONT></B> (v: unit -> 'a) =>
throw (base, SOME (f o v))
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
</p>
<p>
Note that this implementation of <tt>isolate</tt> makes it clear that the continuation returned by <tt>isolate f</tt> only retains the heap-allocated values reachable from <tt>f</tt> and <tt>base</tt>. It also retains only one copy of the stack that was in place at the time <tt>base</tt> was evaluated. Finally, it completely avoids making any copies of the stack that is in place at the time <tt>isolate f</tt> is evaluated; indeed, <tt>isolate f</tt> is a constant-time operation.
</p>
<p>
Next, suppose we limited ourselves to capturing <tt>unit</tt> continuations with <tt>callcc</tt>. We can't pass th thunk to be evaluated in the 'empty' context directly, but we can use a reference cell.
</p>
<pre class=code>
<B><FONT COLOR="#0000FF">local</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> thRef: (unit -> unit) option ref = ref NONE
<B><FONT COLOR="#A020F0">val</FONT></B> base: unit t =
<B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> baseRef: unit t ref =
ref (<B><FONT COLOR="#A020F0">fn</FONT></B> _ => <B><FONT COLOR="#A020F0">raise</FONT></B> Fail <B><FONT COLOR="#BC8F8F">"MLton.Cont.isolate: missing base"</FONT></B>)
<B><FONT COLOR="#A020F0">val</FONT></B> () = callcc (<B><FONT COLOR="#A020F0">fn</FONT></B> k => baseRef := k)
<B><FONT COLOR="#A020F0">in</FONT></B>
<B><FONT COLOR="#A020F0">case</FONT></B> !thRef <B><FONT COLOR="#A020F0">of</FONT></B>
NONE => !baseRef
| SOME th =>
<B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> _ = thRef := NONE
<B><FONT COLOR="#A020F0">val</FONT></B> _ = (th () ; Exit.topLevelSuffix ())
<B><FONT COLOR="#A020F0">handle</FONT></B> exn => MLtonExn.topLevelHandler exn
<B><FONT COLOR="#A020F0">in</FONT></B>
<B><FONT COLOR="#A020F0">raise</FONT></B> Fail <B><FONT COLOR="#BC8F8F">"MLton.Cont.isolate: return from (wrapped) func"</FONT></B>
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#0000FF">in</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> isolate: ('a -> unit) -> 'a t =
<B><FONT COLOR="#A020F0">fn</FONT></B> (f: 'a -> unit) =>
<B><FONT COLOR="#A020F0">fn</FONT></B> (v: unit -> 'a) =>
<B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> () = thRef := SOME (f o v)
<B><FONT COLOR="#A020F0">in</FONT></B>
throw (base, ())
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
</p>
<p>
Note that it is important to set <tt>thRef</tt> to <tt>NONE</tt> before evaluating the thunk, so that the garbage collector doesn't retain all the heap-allocated values reachable from <tt>f</tt> and <tt>v</tt> during the evaluation of <tt>f (v ())</tt>. This is because <tt>thRef</tt> is still live during the evaluation of the thunk; in particular, it was allocated before the evaluation of <tt>base</tt> (and used after), and so is retained by continuation on which the thunk is evaluated.
</p>
<p>
This implementation can be easily adapted to use MLton's primitive stack copying operations.
</p>
<pre class=code>
<B><FONT COLOR="#0000FF">local</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> thRef: (unit -> unit) option ref = ref NONE
<B><FONT COLOR="#A020F0">val</FONT></B> base: Thread.preThread =
<B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> () = Thread.copyCurrent ()
<B><FONT COLOR="#A020F0">in</FONT></B>
<B><FONT COLOR="#A020F0">case</FONT></B> !thRef <B><FONT COLOR="#A020F0">of</FONT></B>
NONE => Thread.savedPre ()
| SOME th =>
<B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> () = thRef := NONE
<B><FONT COLOR="#A020F0">val</FONT></B> _ = (th () ; Exit.topLevelSuffix ())
<B><FONT COLOR="#A020F0">handle</FONT></B> exn => MLtonExn.topLevelHandler exn
<B><FONT COLOR="#A020F0">in</FONT></B>
<B><FONT COLOR="#A020F0">raise</FONT></B> Fail <B><FONT COLOR="#BC8F8F">"MLton.Cont.isolate: return from (wrapped) func"</FONT></B>
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#0000FF">in</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> isolate: ('a -> unit) -> 'a t =
<B><FONT COLOR="#A020F0">fn</FONT></B> (f: 'a -> unit) =>
<B><FONT COLOR="#A020F0">fn</FONT></B> (v: unit -> 'a) =>
<B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> () = thRef := SOME (f o v)
<B><FONT COLOR="#A020F0">val</FONT></B> new = Thread.copy base
<B><FONT COLOR="#A020F0">in</FONT></B>
Thread.switchTo new
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
</p>
<p>
In essence, <tt>Thread.copyCurrent</tt> copies the current execution stack and stores it in an implicit reference cell in the runtime system, which is fetchable with <tt>Thread.savedPre</tt>. When we are ready to throw to the isolated function, <tt>Thread.copy</tt> copies the saved execution stack (because the stack is modified in place during execution, we need to retain a pristine copy in case the isolated function itself throws to other isolated functions) and <tt>Thread.switchTo</tt> abandons the current execution stack, installing the newly copied execution stack.
</p>
<p>
The actual implementation of <tt>MLton.Cont.isolate</tt> simply adds some <tt>Thread.atomicBegin</tt> and <tt>Thread.atomicEnd</tt> commands, which effectively protect the global <tt>thRef</tt> and accommodate the fact that <tt>Thread.switchTo</tt> does an implicit <tt>Thread.atomicEnd</tt> (used for leaving a signal handler thread).
</p>
<pre class=code>
<B><FONT COLOR="#0000FF">local</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> thRef: (unit -> unit) option ref = ref NONE
<B><FONT COLOR="#A020F0">val</FONT></B> base: Thread.preThread =
<B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> () = Thread.copyCurrent ()
<B><FONT COLOR="#A020F0">in</FONT></B>
<B><FONT COLOR="#A020F0">case</FONT></B> !thRef <B><FONT COLOR="#A020F0">of</FONT></B>
NONE => Thread.savedPre ()
| SOME th =>
<B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> () = thRef := NONE
<B><FONT COLOR="#A020F0">val</FONT></B> _ = MLton.atomicEnd <I><FONT COLOR="#B22222">(* Match 1 *)</FONT></I>
<B><FONT COLOR="#A020F0">val</FONT></B> _ = (th () ; Exit.topLevelSuffix ())
<B><FONT COLOR="#A020F0">handle</FONT></B> exn => MLtonExn.topLevelHandler exn
<B><FONT COLOR="#A020F0">in</FONT></B>
<B><FONT COLOR="#A020F0">raise</FONT></B> Fail <B><FONT COLOR="#BC8F8F">"MLton.Cont.isolate: return from (wrapped) func"</FONT></B>
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#0000FF">in</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> isolate: ('a -> unit) -> 'a t =
<B><FONT COLOR="#A020F0">fn</FONT></B> (f: 'a -> unit) =>
<B><FONT COLOR="#A020F0">fn</FONT></B> (v: unit -> 'a) =>
<B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> _ = MLton.atomicBegin <I><FONT COLOR="#B22222">(* Match 1 *)</FONT></I>
<B><FONT COLOR="#A020F0">val</FONT></B> () = thRef := SOME (f o v)
<B><FONT COLOR="#A020F0">val</FONT></B> new = Thread.copy base
<B><FONT COLOR="#A020F0">val</FONT></B> _ = MLton.atomicBegin <I><FONT COLOR="#B22222">(* Match 2 *)</FONT></I>
<B><FONT COLOR="#A020F0">in</FONT></B>
Thread.switchTo new <I><FONT COLOR="#B22222">(* Match 2 *)</FONT></I>
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
</p>
<p>
It is perhaps interesting to note that the above implementation was originally 'derived' by specializing implementations of the <a href="MLtonThread">MLtonThread</a> <tt>new</tt>, <tt>prepare</tt>, and <tt>switch</tt> functions as if their only use was in the following implementation of <tt>isolate</tt>:
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> isolate: ('a -> unit) -> 'a t =
<B><FONT COLOR="#A020F0">fn</FONT></B> (f: 'a -> unit) =>
<B><FONT COLOR="#A020F0">fn</FONT></B> (v: unit -> 'a) =>
<B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> th = (f (v ()) ; Exit.topLevelSuffix ())
<B><FONT COLOR="#A020F0">handle</FONT></B> exn => MLtonExn.topLevelHandler exn
<B><FONT COLOR="#A020F0">val</FONT></B> t = MLton.Thread.prepare (MLton.Thread.new th, ())
<B><FONT COLOR="#A020F0">in</FONT></B>
MLton.Thread.switch (<B><FONT COLOR="#A020F0">fn</FONT></B> _ => t)
<B><FONT COLOR="#A020F0">end</FONT></B>
</PRE>
<p>
</p>
<p>
It was pleasant to discover that it could equally well be 'derived' starting from the <tt>callcc</tt> and <tt>throw</tt> implementation.
</p>
<p>
As a final comment, we noted that the degree to which the context of <tt>base</tt> could be considered 'empty' (i.e., retaining few heap-allocated values) depended upon a slightly MLton-esque view. In particular, MLton does not heap allocate executable code. So, although the <tt>base</tt> context keeps a lot of unevaluated code 'live', such code is not heap allocated. In a system like SML/NJ, that does heap allocate executable code, one might want it to be the case that after throwing to an isolated function, the garbage collector retains only the code necessary to evaluate the function, and not any code that was necessary to evaluate the <tt>base</tt> context.
</p>
</div>
<p>
<hr>
Last edited on 2008-05-02 03:05:42 by <span title="c-71-57-91-146.hsd1.il.comcast.net"><a href="MatthewFluet">MatthewFluet</a></span>.
</body></html>
|