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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
|
<!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>TypeIndexedValues - 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%;">
TypeIndexedValues
<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">
<a href="StandardML">Standard ML</a> does not support ad hoc polymorphism. This presents a challenge to programmers. The problem is that at first glance there seems to be no practical way to implement something like a function for converting a value of any type to a string or a function for computing a hash value for a value of any type. Fortunately there are ways to implement type-indexed values in SML as discussed in <a href = "References#Yang98">Yang98</a>. Various articles such as <a href = "References#Danvy98">Danvy98</a>, <a href = "References#Ramsey03">Ramsey03</a>, <a href = "References#Elsman04">Elsman04</a>, <a href = "References#Kennedy04">Kennedy04</a>, and <a href = "References#Benton05">Benton05</a> also contain examples of type-indexed values. <p>
<strong>NOTE:</strong> The technique used in the following example uses an early (and somewhat broken) variation of the basic technique used in an experimental generic programming library (see <a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mltonlib/trunk/com/ssh/generic/unstable/README?view=markup"><img src="moin-www.png" alt="[WWW]" height="11" width="11">README</a>) that can be found from the MLton repository. The generic programming library also includes a more advanced generic pretty printing function (see <a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mltonlib/trunk/com/ssh/generic/unstable/public/value/pretty.sig?view=markup"><img src="moin-www.png" alt="[WWW]" height="11" width="11">pretty.sig</a>).
</p>
<h2 id="head-2ea0b5ca6b0ef3808544cdd88ef0b644bbad42c4">Example: Converting any SML value to (roughly) SML syntax</h2>
<p>
Consider the problem of converting any SML value to a textual presentation that matches the syntax of SML as closely as possible. One solution is a type-indexed function that maps a given type to a function that maps any value (of the type) to its textual presentation. A type-indexed function like this can be useful for a variety of purposes. For example, one could use it to show debugging information. We'll call this function "<tt>show</tt>".
</p>
<p>
We'll do a fairly complete implementation of <tt>show</tt>. We do not distinguish infix and nonfix constructors, but that is not an intrinsic property of SML datatypes. We also don't reconstruct a type name for the value, although it would be particularly useful for functional values. To reconstruct type names, some changes would be needed and the reader is encouraged to consider how to do that. A more realistic implementation would use some pretty printing combinators to compute a layout for the result. This should be a relatively easy change (given a suitable pretty printing library). Cyclic values (through references and arrays) do not have a standard textual presentation and it is impossible to convert arbitrary functional values (within SML) to a meaningful textual presentation. Finally, it would also make sense to show sharing of references and arrays. We'll leave these improvements to an actual library implementation.
</p>
<p>
The following code uses the <a href="Fixpoints">fixpoint framework</a> and other utilities from an Extended Basis library (see <a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mltonlib/trunk/com/ssh/extended-basis/unstable/README?view=markup"><img src="moin-www.png" alt="[WWW]" height="11" width="11">README</a>).
</p>
<h3 id="head-2f32be1dc74166373c988ec03b0bd86f0a576919">Signature</h3>
<p>
Let's consider the design of the <tt>SHOW</tt> signature:
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">infixr</FONT></B> -->
<B><FONT COLOR="#0000FF">signature</FONT></B> SHOW = <B><FONT COLOR="#0000FF">sig</FONT></B>
<B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> 'a t <I><FONT COLOR="#B22222">(* complete type-index *)</FONT></I>
</FONT></B><B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> 'a s <I><FONT COLOR="#B22222">(* incomplete sum *)</FONT></I>
</FONT></B><B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> ('a, 'k) p <I><FONT COLOR="#B22222">(* incomplete product *)</FONT></I>
</FONT></B><B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> u <I><FONT COLOR="#B22222">(* tuple or unlabelled product *)</FONT></I>
</FONT></B><B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> l <I><FONT COLOR="#B22222">(* record or labelled product *)</FONT></I>
</FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> show : 'a t -> 'a -> string
<I><FONT COLOR="#B22222">(* user-defined types *)</FONT></I>
<B><FONT COLOR="#A020F0">val</FONT></B> inj : ('a -> 'b) -> 'b t -> 'a t
<I><FONT COLOR="#B22222">(* tuples and records *)</FONT></I>
<B><FONT COLOR="#A020F0">val</FONT></B> * : ('a, 'k) p * ('b, 'k) p -> (('a, 'b) product, 'k) p
<B><FONT COLOR="#A020F0">val</FONT></B> U : 'a t -> ('a, u) p
<B><FONT COLOR="#A020F0">val</FONT></B> L : string -> 'a t -> ('a, l) p
<B><FONT COLOR="#A020F0">val</FONT></B> tuple : ('a, u) p -> 'a t
<B><FONT COLOR="#A020F0">val</FONT></B> record : ('a, l) p -> 'a t
<I><FONT COLOR="#B22222">(* datatypes *)</FONT></I>
<B><FONT COLOR="#A020F0">val</FONT></B> + : 'a s * 'b s -> (('a, 'b) sum) s
<B><FONT COLOR="#A020F0">val</FONT></B> C0 : string -> unit s
<B><FONT COLOR="#A020F0">val</FONT></B> C1 : string -> 'a t -> 'a s
<B><FONT COLOR="#A020F0">val</FONT></B> data : 'a s -> 'a t
<B><FONT COLOR="#A020F0">val</FONT></B> Y : 'a t Tie.t
<I><FONT COLOR="#B22222">(* exceptions *)</FONT></I>
<B><FONT COLOR="#A020F0">val</FONT></B> exn : exn t
<B><FONT COLOR="#A020F0">val</FONT></B> regExn : (exn -> ('a * 'a s) option) -> unit
<I><FONT COLOR="#B22222">(* some built-in type constructors *)</FONT></I>
<B><FONT COLOR="#A020F0">val</FONT></B> refc : 'a t -> 'a ref t
<B><FONT COLOR="#A020F0">val</FONT></B> array : 'a t -> 'a array t
<B><FONT COLOR="#A020F0">val</FONT></B> list : 'a t -> 'a list t
<B><FONT COLOR="#A020F0">val</FONT></B> vector : 'a t -> 'a vector t
<B><FONT COLOR="#A020F0">val</FONT></B> --> : 'a t * 'b t -> ('a -> 'b) t
<I><FONT COLOR="#B22222">(* some built-in base types *)</FONT></I>
<B><FONT COLOR="#A020F0">val</FONT></B> string : string t
<B><FONT COLOR="#A020F0">val</FONT></B> unit : unit t
<B><FONT COLOR="#A020F0">val</FONT></B> bool : bool t
<B><FONT COLOR="#A020F0">val</FONT></B> char : char t
<B><FONT COLOR="#A020F0">val</FONT></B> int : int t
<B><FONT COLOR="#A020F0">val</FONT></B> word : word t
<B><FONT COLOR="#A020F0">val</FONT></B> real : real t
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
</p>
<p>
While some details are shaped by the specific requirements of <tt>show</tt>, there are a number of (design) patterns that translate to other type-indexed values. The former kind of details are mostly shaped by the syntax of SML values that <tt>show</tt> is designed to produce. To this end, abstract types and phantom types are used to distinguish incomplete record, tuple, and datatype type-indices from each other and from complete type-indices. Also, names of record labels and datatype constructors need to be provided by the user.
</p>
<h4 id="head-671800661391e0ee68700b83d223516c787ea4c9">Arbitrary user-defined datatypes</h4>
<p>
Perhaps the most important pattern is how the design supports arbitrary user-defined datatypes. A number of combinators together conspire to provide the functionality. First of all, to support new user-defined types, a combinator taking a conversion function to a previously supported type is provided:
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> inj : ('a -> 'b) -> 'b t -> 'a t
</PRE>
<p>
</p>
<p>
An injection function is sufficient in this case, but in the general case, an embedding with injection and projection functions may be needed.
</p>
<p>
To support products (tuples and records) a product combinator is provided:
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> * : ('a, 'k) p * ('b, 'k) p -> (('a, 'b) product, 'k) p
</PRE>
<p>
</p>
<p>
The second (phantom) type variable <tt>'k</tt> is there to distinguish between labelled and unlabelled products and the type <tt>p</tt> distinguishes incomplete products from complete type-indices of type <tt>t</tt>. Most type-indexed values do not need to make such distinctions.
</p>
<p>
To support sums (datatypes) a sum combinator is provided:
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> + : 'a s * 'b s -> (('a, 'b) sum) s
</PRE>
<p>
</p>
<p>
Again, the purpose of the type <tt>s</tt> is to distinguish incomplete sums from complete type-indices of type <tt>t</tt>, which usually isn't necessary.
</p>
<p>
Finally, to support recursive datatypes, including sets of mutually recursive datatypes, a <a href="Fixpoints">fixpoint tier</a> is provided:
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> Y : 'a t Tie.t
</PRE>
<p>
</p>
<p>
Together these combinators (with the more domain specific combinators <tt>U</tt>, <tt>L</tt>, <tt>tuple</tt>, <tt>record</tt>, <tt>C0</tt>, <tt>C1</tt>, and <tt>data</tt>) enable one to encode a type-index for any user-defined datatype.
</p>
<h4 id="head-cd3dfbe79fda3a9d92de67b1bd4f1dc9f60ab723">Exceptions</h4>
<p>
The <tt>exn</tt> type in SML is a <a href="UniversalType">universal type</a> into which all types can be embedded. SML also allows a program to generate new exception variants at run-time. Thus a mechanism is required to register handlers for particular variants:
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> exn : exn t
<B><FONT COLOR="#A020F0">val</FONT></B> regExn : (exn -> ('a * 'a s) option) -> unit
</PRE>
<p>
</p>
<p>
The universal <tt>exn</tt> type-index then makes use of the registered handlers. The above particular form of handler, which converts an exception value to a value of some type and a type-index for that type (essentially an existential type) is designed to make it convenient to write handlers. To write a handler, one can conveniently reuse existing type-indices:
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">exception</FONT></B><B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">Int</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> int
</FONT></B><B><FONT COLOR="#0000FF">local</FONT></B>
<B><FONT COLOR="#0000FF">open</FONT></B> Show
<B><FONT COLOR="#0000FF">in</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> () = regExn (<B><FONT COLOR="#A020F0">fn</FONT></B> Int v => SOME (v, C1<B><FONT COLOR="#BC8F8F">"Int"</FONT></B> int)
| _ => NONE)
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
</p>
<p>
Note that a single handler may actually handle an arbitrary number of different exceptions.
</p>
<h4 id="head-8c5befcc2e0951ae7c577c186d5015110ab50c93">Other types</h4>
<p>
Some built-in and standard types typically require special treatment due to their special nature. The most important of these are arrays and references, because cyclic data (ignoring closures) and observable sharing can only be constructed through them.
</p>
<p>
When arrow types are really supported, unlike in this case, they usually need special treatment due to the contravariance of arguments.
</p>
<p>
Lists and vectors require special treatment in the case of <tt>show</tt>, because of their special syntax. This isn't usually the case.
</p>
<p>
The set of base types to support also needs to be considered unless one exports an interface for constructing type-indices for entirely new base types.
</p>
<h3 id="head-0bb18642b70b9f8a9c12ccf39487328f306b8e19">Usage</h3>
<p>
Before going to the implementation, let's look at some examples. For the following examples, we'll assume a structure binding <tt>Show :> SHOW</tt>. If you want to try the examples immediately, just skip forward to the implementation.
</p>
<p>
To use <tt>show</tt>, one first needs a type-index, which is then given to <tt>show</tt>. To show a list of integers, one would use the type-index <tt>list int</tt>, which has the type <tt>int list Show.t</tt>:
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#BC8F8F">"[3, 1, 4]"</FONT></B> =
<B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#0000FF">open</FONT></B> Show <B><FONT COLOR="#A020F0">in</FONT></B> show (list int) <B><FONT COLOR="#A020F0">end</FONT></B>
[<B><FONT COLOR="#5F9EA0">3</FONT></B>, <B><FONT COLOR="#5F9EA0">1</FONT></B>, <B><FONT COLOR="#5F9EA0">4</FONT></B>]
</PRE>
<p>
</p>
<p>
Likewise, to show a list of lists of characters, one would use the type-index <tt>list (list char)</tt>, which has the type <tt>char list list Show.t</tt>:
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#BC8F8F">"[[#\"a\"], [#\"b\", #\"c\"], []]"</FONT></B> =
<B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#0000FF">open</FONT></B> Show <B><FONT COLOR="#A020F0">in</FONT></B> show (list (list char)) <B><FONT COLOR="#A020F0">end</FONT></B>
[[#<B><FONT COLOR="#BC8F8F">"a"</FONT></B>], [#<B><FONT COLOR="#BC8F8F">"b"</FONT></B>, #<B><FONT COLOR="#BC8F8F">"c"</FONT></B>], []]
</PRE>
<p>
</p>
<p>
Handling standard types is not particularly interesting. It is more interesting to see how user-defined types can be handled. Although the <tt>option</tt> datatype is a standard type, it requires no special support, so we can treat it as a user-defined type. Options can be encoded easily using a sum:
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">fun</FONT></B> option t = <B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#0000FF">open</FONT></B> Show
<B><FONT COLOR="#A020F0">in</FONT></B>
inj (<B><FONT COLOR="#A020F0">fn</FONT></B> NONE => INL ()
| SOME v => INR v)
(data (C0<B><FONT COLOR="#BC8F8F">"NONE"</FONT></B> + C1<B><FONT COLOR="#BC8F8F">"SOME"</FONT></B> t))
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#BC8F8F">"SOME 5"</FONT></B> =
<B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#0000FF">open</FONT></B> Show <B><FONT COLOR="#A020F0">in</FONT></B> show (option int) <B><FONT COLOR="#A020F0">end</FONT></B>
(SOME <B><FONT COLOR="#5F9EA0">5</FONT></B>)
</PRE>
<p>
</p>
<p>
Readers new to type-indexed values might want to type annotate each subexpression of the above example as an exercise. (Use a compiler to check your annotations.)
</p>
<p>
Using a product, user specified records can be also be encoded easily:
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> abc = <B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#0000FF">open</FONT></B> Show
<B><FONT COLOR="#A020F0">in</FONT></B>
inj (<B><FONT COLOR="#A020F0">fn</FONT></B> {a, b, c} => a & b & c)
(record (L<B><FONT COLOR="#BC8F8F">"a"</FONT></B> (option int) *
L<B><FONT COLOR="#BC8F8F">"b"</FONT></B> real *
L<B><FONT COLOR="#BC8F8F">"c"</FONT></B> bool))
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#BC8F8F">"{a = SOME 1, b = 3.0, c = false}"</FONT></B> =
<B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#0000FF">open</FONT></B> Show <B><FONT COLOR="#A020F0">in</FONT></B> show abc <B><FONT COLOR="#A020F0">end</FONT></B>
{a = SOME <B><FONT COLOR="#5F9EA0">1</FONT></B>, b = <B><FONT COLOR="#5F9EA0">3.0</FONT></B>, c = false}
</PRE>
<p>
</p>
<p>
As you can see, both of the above use <tt>inj</tt> to inject user-defined types to the general purpose sum and product types.
</p>
<p>
Of particular interest is whether recursive datatypes and cyclic data can be handled. For example, how does one write a type-index for a recursive datatype such as a cyclic graph?
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> 'a graph </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">VTX</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> 'a * 'a graph list ref
</FONT></B><B><FONT COLOR="#A020F0">fun</FONT></B> arcs (VTX (_, r)) = r
</PRE>
<p>
</p>
<p>
Using the <tt>Show</tt> combinators, we could first write a new type-index combinator for <tt>graph</tt>:
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">fun</FONT></B> graph a = <B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#0000FF">open</FONT></B> Tie Show
<B><FONT COLOR="#A020F0">in</FONT></B>
fix Y (<B><FONT COLOR="#A020F0">fn</FONT></B> graph_a =>
inj (<B><FONT COLOR="#A020F0">fn</FONT></B> VTX (x, y) => x & y)
(data (C1<B><FONT COLOR="#BC8F8F">"VTX"</FONT></B>
(tuple (U a *
U (refc (list graph_a)))))))
<B><FONT COLOR="#A020F0">end</FONT></B>
</PRE>
<p>
</p>
<p>
To show a graph with integer labels
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> a_graph = <B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> a = VTX (<B><FONT COLOR="#5F9EA0">1</FONT></B>, ref [])
<B><FONT COLOR="#A020F0">val</FONT></B> b = VTX (<B><FONT COLOR="#5F9EA0">2</FONT></B>, ref [])
<B><FONT COLOR="#A020F0">val</FONT></B> c = VTX (<B><FONT COLOR="#5F9EA0">3</FONT></B>, ref [])
<B><FONT COLOR="#A020F0">val</FONT></B> d = VTX (<B><FONT COLOR="#5F9EA0">4</FONT></B>, ref [])
<B><FONT COLOR="#A020F0">val</FONT></B> e = VTX (<B><FONT COLOR="#5F9EA0">5</FONT></B>, ref [])
<B><FONT COLOR="#A020F0">val</FONT></B> f = VTX (<B><FONT COLOR="#5F9EA0">6</FONT></B>, ref [])
<B><FONT COLOR="#A020F0">in</FONT></B>
arcs a := [b, d]
; arcs b := [c, e]
; arcs c := [a, f]
; arcs d := [f]
; arcs e := [d]
; arcs f := [e]
; a
<B><FONT COLOR="#A020F0">end</FONT></B>
</PRE>
<p>
</p>
<p>
we could then simply write
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#BC8F8F">"VTX (1, ref [VTX (2, ref [VTX (3, ref [VTX (1, %0), \
\VTX (6, ref [VTX (5, ref [VTX (4, ref [VTX (6, %3)])])] as %3)]), \
\VTX (5, ref [VTX (4, ref [VTX (6, ref [VTX (5, %2)])])] as %2)]), \
\VTX (4, ref [VTX (6, ref [VTX (5, ref [VTX (4, %1)])])] as %1)] as %0)"</FONT></B> =
<B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#0000FF">open</FONT></B> Show <B><FONT COLOR="#A020F0">in</FONT></B> show (graph int) <B><FONT COLOR="#A020F0">end</FONT></B>
a_graph
</PRE>
<p>
</p>
<p>
There is a subtle gotcha with cyclic data. Consider the following code:
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">exception</FONT></B><B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">ExnArray</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> exn array
</FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> () = <B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#0000FF">open</FONT></B> Show
<B><FONT COLOR="#A020F0">in</FONT></B>
regExn (<B><FONT COLOR="#A020F0">fn</FONT></B> ExnArray a =>
SOME (a, C1<B><FONT COLOR="#BC8F8F">"ExnArray"</FONT></B> (array exn))
| _ => NONE)
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> a_cycle = <B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> a = Array.fromList [Empty]
<B><FONT COLOR="#A020F0">in</FONT></B>
Array.update (a, <B><FONT COLOR="#5F9EA0">0</FONT></B>, ExnArray a) ; a
<B><FONT COLOR="#A020F0">end</FONT></B>
</PRE>
<p>
</p>
<p>
Although the above looks innocent enough, the evaluation of
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#BC8F8F">"[|ExnArray %0|] as %0"</FONT></B> =
<B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#0000FF">open</FONT></B> Show <B><FONT COLOR="#A020F0">in</FONT></B> show (array exn) <B><FONT COLOR="#A020F0">end</FONT></B>
a_cycle
</PRE>
<p>
</p>
<p>
goes into an infinite loop. To avoid this problem, the type-index <tt>array exn</tt> must be evaluated only once, as in the following:
</p>
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> array_exn = <B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#0000FF">open</FONT></B> Show <B><FONT COLOR="#A020F0">in</FONT></B> array exn <B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#A020F0">exception</FONT></B><B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">ExnArray</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> exn array
</FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> () = <B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#0000FF">open</FONT></B> Show
<B><FONT COLOR="#A020F0">in</FONT></B>
regExn (<B><FONT COLOR="#A020F0">fn</FONT></B> ExnArray a =>
SOME (a, C1<B><FONT COLOR="#BC8F8F">"ExnArray"</FONT></B> array_exn)
| _ => NONE)
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> a_cycle = <B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> a = Array.fromList [Empty]
<B><FONT COLOR="#A020F0">in</FONT></B>
Array.update (a, <B><FONT COLOR="#5F9EA0">0</FONT></B>, ExnArray a) ; a
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#BC8F8F">"[|ExnArray %0|] as %0"</FONT></B> =
<B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#0000FF">open</FONT></B> Show <B><FONT COLOR="#A020F0">in</FONT></B> show array_exn <B><FONT COLOR="#A020F0">end</FONT></B>
a_cycle
</PRE>
<p>
</p>
<p>
Cyclic data (excluding closures) in Standard ML can only be constructed imperatively through arrays and references (combined with exceptions or recursive datatypes). Before recursing to a reference or an array, one needs to check whether that reference or array has already been seen before. When <tt>ref</tt> or <tt>array</tt> is called with a type-index, a new cyclicity checker is instantiated.
</p>
<h3 id="head-8781d615fd77be9578225c40ac67b9471394cced">Implementation</h3>
<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> SmlSyntax = <B><FONT COLOR="#0000FF">struct</FONT></B>
<B><FONT COLOR="#A020F0">local</FONT></B>
<B><FONT COLOR="#0000FF">structure</FONT></B> CV = CharVector <B><FONT COLOR="#0000FF">and</FONT></B> C = Char
<B><FONT COLOR="#A020F0">in</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> isSym = Char.contains <B><FONT COLOR="#BC8F8F">"!%&$#+-/:<=>?@\\~`^|*"</FONT></B>
<B><FONT COLOR="#A020F0">fun</FONT></B> isSymId s = <B><FONT COLOR="#5F9EA0">0</FONT></B> < size s <B><FONT COLOR="#A020F0">andalso</FONT></B> CV.all isSym s
<B><FONT COLOR="#A020F0">fun</FONT></B> isAlphaNumId s =
<B><FONT COLOR="#5F9EA0">0</FONT></B> < size s
<B><FONT COLOR="#A020F0">andalso</FONT></B> C.isAlpha (CV.sub (s, <B><FONT COLOR="#5F9EA0">0</FONT></B>))
<B><FONT COLOR="#A020F0">andalso</FONT></B> CV.all (<B><FONT COLOR="#A020F0">fn</FONT></B> c => C.isAlphaNum c
<B><FONT COLOR="#A020F0">orelse</FONT></B> #<B><FONT COLOR="#BC8F8F">"'"</FONT></B> = c
<B><FONT COLOR="#A020F0">orelse</FONT></B> #<B><FONT COLOR="#BC8F8F">"_"</FONT></B> = c) s
<B><FONT COLOR="#A020F0">fun</FONT></B> isNumLabel s =
<B><FONT COLOR="#5F9EA0">0</FONT></B> < size s
<B><FONT COLOR="#A020F0">andalso</FONT></B> #<B><FONT COLOR="#BC8F8F">"0"</FONT></B> <> CV.sub (s, <B><FONT COLOR="#5F9EA0">0</FONT></B>)
<B><FONT COLOR="#A020F0">andalso</FONT></B> CV.all C.isDigit s
<B><FONT COLOR="#A020F0">fun</FONT></B> isId s = isAlphaNumId s <B><FONT COLOR="#A020F0">orelse</FONT></B> isSymId s
<B><FONT COLOR="#A020F0">fun</FONT></B> isLongId s = List.all isId (String.fields (#<B><FONT COLOR="#BC8F8F">"."</FONT></B> <\ <B><FONT COLOR="#A020F0">op</FONT></B> =) s)
<B><FONT COLOR="#A020F0">fun</FONT></B> isLabel s = isId s <B><FONT COLOR="#A020F0">orelse</FONT></B> isNumLabel s
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#0000FF">end</FONT></B>
<B><FONT COLOR="#0000FF">structure</FONT></B> Show :> SHOW = <B><FONT COLOR="#0000FF">struct</FONT></B>
<B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> 'a t </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">IN</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> exn list * 'a -> bool * string
</FONT></B><B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> 'a s </FONT></B>=<B><FONT COLOR="#228B22"> 'a t
</FONT></B><B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> ('a, 'k) p </FONT></B>=<B><FONT COLOR="#228B22"> 'a t
</FONT></B><B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> u </FONT></B>=<B><FONT COLOR="#228B22"> unit
</FONT></B><B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> l </FONT></B>=<B><FONT COLOR="#228B22"> unit
</FONT></B><B><FONT COLOR="#A020F0">fun</FONT></B> show (IN t) x = #<B><FONT COLOR="#5F9EA0">2</FONT></B> (t ([], x))
<I><FONT COLOR="#B22222">(* user-defined types *)</FONT></I>
<B><FONT COLOR="#A020F0">fun</FONT></B> inj inj (IN b) = IN (b o Pair.map (id, inj))
<B><FONT COLOR="#A020F0">local</FONT></B>
<B><FONT COLOR="#A020F0">fun</FONT></B> surround pre suf (_, s) = (false, concat [pre, s, suf])
<B><FONT COLOR="#A020F0">fun</FONT></B> parenthesize x = <B><FONT COLOR="#A020F0">if</FONT></B> #1 x <B><FONT COLOR="#A020F0">then</FONT></B> surround <B><FONT COLOR="#BC8F8F">"("</FONT></B> <B><FONT COLOR="#BC8F8F">")"</FONT></B> x <B><FONT COLOR="#A020F0">else</FONT></B> x
<B><FONT COLOR="#A020F0">fun</FONT></B> construct tag =
(<B><FONT COLOR="#A020F0">fn</FONT></B> (_, s) => (true, concat [tag, <B><FONT COLOR="#BC8F8F">" "</FONT></B>, s])) o parenthesize
<B><FONT COLOR="#A020F0">fun</FONT></B> check p m s = <B><FONT COLOR="#A020F0">if</FONT></B> p s <B><FONT COLOR="#A020F0">then</FONT></B> () <B><FONT COLOR="#A020F0">else</FONT></B> <B><FONT COLOR="#A020F0">raise</FONT></B> Fail (m^s)
<B><FONT COLOR="#A020F0">in</FONT></B>
<I><FONT COLOR="#B22222">(* tuples and records *)</FONT></I>
<B><FONT COLOR="#A020F0">fun</FONT></B> (IN l) * (IN r) =
IN (<B><FONT COLOR="#A020F0">fn</FONT></B> (rs, a & b) =>
(false, concat [#<B><FONT COLOR="#5F9EA0">2</FONT></B> (l (rs, a)),
<B><FONT COLOR="#BC8F8F">", "</FONT></B>,
#<B><FONT COLOR="#5F9EA0">2</FONT></B> (r (rs, b))]))
<B><FONT COLOR="#A020F0">val</FONT></B> U = id
<B><FONT COLOR="#A020F0">fun</FONT></B> L l = (check SmlSyntax.isLabel <B><FONT COLOR="#BC8F8F">"Invalid label: "</FONT></B> l
; <B><FONT COLOR="#A020F0">fn</FONT></B> IN t => IN (surround (l^<B><FONT COLOR="#BC8F8F">" = "</FONT></B>) <B><FONT COLOR="#BC8F8F">""</FONT></B> o t))
<B><FONT COLOR="#A020F0">fun</FONT></B> tuple (IN t) = IN (surround <B><FONT COLOR="#BC8F8F">"("</FONT></B> <B><FONT COLOR="#BC8F8F">")"</FONT></B> o t)
<B><FONT COLOR="#A020F0">fun</FONT></B> record (IN t) = IN (surround <B><FONT COLOR="#BC8F8F">"{"</FONT></B> <B><FONT COLOR="#BC8F8F">"}"</FONT></B> o t)
<I><FONT COLOR="#B22222">(* datatypes *)</FONT></I>
<B><FONT COLOR="#A020F0">fun</FONT></B> (IN l) + (IN r) = IN (<B><FONT COLOR="#A020F0">fn</FONT></B> (rs, INL a) => l (rs, a)
| (rs, INR b) => r (rs, b))
<B><FONT COLOR="#A020F0">fun</FONT></B> C0 c = (check SmlSyntax.isId <B><FONT COLOR="#BC8F8F">"Invalid constructor: "</FONT></B> c
; IN (const (false, c)))
<B><FONT COLOR="#A020F0">fun</FONT></B> C1 c (IN t) = (check SmlSyntax.isId <B><FONT COLOR="#BC8F8F">"Invalid constructor: "</FONT></B> c
; IN (construct c o t))
<B><FONT COLOR="#A020F0">val</FONT></B> data = id
<B><FONT COLOR="#A020F0">fun</FONT></B> Y ? = Tie.iso Tie.function (<B><FONT COLOR="#A020F0">fn</FONT></B> IN x => x, IN) ?
<I><FONT COLOR="#B22222">(* exceptions *)</FONT></I>
<B><FONT COLOR="#A020F0">local</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> handlers = ref ([] : (exn -> unit t option) list)
<B><FONT COLOR="#A020F0">in</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> exn = IN (<B><FONT COLOR="#A020F0">fn</FONT></B> (rs, e) => <B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#A020F0">fun</FONT></B> lp [] =
C0(concat [<B><FONT COLOR="#BC8F8F">"<exn:"</FONT></B>,
General.exnName e,
<B><FONT COLOR="#BC8F8F">">"</FONT></B>])
| lp (f::fs) =
<B><FONT COLOR="#A020F0">case</FONT></B> f e
<B><FONT COLOR="#A020F0">of</FONT></B> NONE => lp fs
| SOME t => t
<B><FONT COLOR="#A020F0">val</FONT></B> IN f = lp (!handlers)
<B><FONT COLOR="#A020F0">in</FONT></B>
f (rs, ())
<B><FONT COLOR="#A020F0">end</FONT></B>)
<B><FONT COLOR="#A020F0">fun</FONT></B> regExn f =
handlers := (Option.map
(<B><FONT COLOR="#A020F0">fn</FONT></B> (x, IN f) =>
IN (<B><FONT COLOR="#A020F0">fn</FONT></B> (rs, ()) =>
f (rs, x))) o f)
:: !handlers
<B><FONT COLOR="#A020F0">end</FONT></B>
<I><FONT COLOR="#B22222">(* some built-in type constructors *)</FONT></I>
<B><FONT COLOR="#A020F0">local</FONT></B>
<B><FONT COLOR="#A020F0">fun</FONT></B> cyclic (IN t) = <B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#A020F0">exception</FONT></B><B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">E</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> ''a * bool ref
</FONT></B><B><FONT COLOR="#A020F0">in</FONT></B>
IN (<B><FONT COLOR="#A020F0">fn</FONT></B> (rs, v : ''a) => <B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> idx = Int.toString o length
<B><FONT COLOR="#A020F0">fun</FONT></B> lp (E (v', c)::rs) =
<B><FONT COLOR="#A020F0">if</FONT></B> v' <> v <B><FONT COLOR="#A020F0">then</FONT></B> lp rs
<B><FONT COLOR="#A020F0">else</FONT></B> (c := false ; (false, <B><FONT COLOR="#BC8F8F">"%"</FONT></B>^idx rs))
| lp (_::rs) = lp rs
| lp [] = <B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> c = ref true
<B><FONT COLOR="#A020F0">val</FONT></B> r = t (E (v, c)::rs, v)
<B><FONT COLOR="#A020F0">in</FONT></B>
<B><FONT COLOR="#A020F0">if</FONT></B> !c <B><FONT COLOR="#A020F0">then</FONT></B> r
<B><FONT COLOR="#A020F0">else</FONT></B> surround <B><FONT COLOR="#BC8F8F">""</FONT></B> (<B><FONT COLOR="#BC8F8F">" as %"</FONT></B>^idx rs) r
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#A020F0">in</FONT></B>
lp rs
<B><FONT COLOR="#A020F0">end</FONT></B>)
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#A020F0">fun</FONT></B> aggregate pre suf toList (IN t) =
IN (surround pre suf o
(<B><FONT COLOR="#A020F0">fn</FONT></B> (rs, a) =>
(false,
String.concatWith
<B><FONT COLOR="#BC8F8F">", "</FONT></B>
(map (#2 o curry t rs)
(toList a)))))
<B><FONT COLOR="#A020F0">in</FONT></B>
<B><FONT COLOR="#A020F0">fun</FONT></B> refc ? = (cyclic o inj ! o C1<B><FONT COLOR="#BC8F8F">"ref"</FONT></B>) ?
<B><FONT COLOR="#A020F0">fun</FONT></B> array ? = (cyclic o aggregate <B><FONT COLOR="#BC8F8F">"[|"</FONT></B> <B><FONT COLOR="#BC8F8F">"|]"</FONT></B> (Array.foldr <B><FONT COLOR="#A020F0">op</FONT></B>:: [])) ?
<B><FONT COLOR="#A020F0">fun</FONT></B> list ? = aggregate <B><FONT COLOR="#BC8F8F">"["</FONT></B> <B><FONT COLOR="#BC8F8F">"]"</FONT></B> id ?
<B><FONT COLOR="#A020F0">fun</FONT></B> vector ? = aggregate <B><FONT COLOR="#BC8F8F">"#["</FONT></B> <B><FONT COLOR="#BC8F8F">"]"</FONT></B> (Vector.foldr <B><FONT COLOR="#A020F0">op</FONT></B>:: []) ?
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#A020F0">fun</FONT></B> (IN _) --> (IN _) = IN (const (false, <B><FONT COLOR="#BC8F8F">"<fn>"</FONT></B>))
<I><FONT COLOR="#B22222">(* some built-in base types *)</FONT></I>
<B><FONT COLOR="#A020F0">local</FONT></B>
<B><FONT COLOR="#A020F0">fun</FONT></B> mk toS = (<B><FONT COLOR="#A020F0">fn</FONT></B> x => (false, x)) o toS o (<B><FONT COLOR="#A020F0">fn</FONT></B> (_, x) => x)
<B><FONT COLOR="#A020F0">in</FONT></B>
<B><FONT COLOR="#A020F0">val</FONT></B> string =
IN (surround <B><FONT COLOR="#BC8F8F">"\""</FONT></B> <B><FONT COLOR="#BC8F8F">"\""</FONT></B> o mk (String.translate Char.toString))
<B><FONT COLOR="#A020F0">val</FONT></B> unit = IN (mk (<B><FONT COLOR="#A020F0">fn</FONT></B> () => <B><FONT COLOR="#BC8F8F">"()"</FONT></B>))
<B><FONT COLOR="#A020F0">val</FONT></B> bool = IN (mk Bool.toString)
<B><FONT COLOR="#A020F0">val</FONT></B> char = IN (surround <B><FONT COLOR="#BC8F8F">"#\""</FONT></B> <B><FONT COLOR="#BC8F8F">"\""</FONT></B> o mk Char.toString)
<B><FONT COLOR="#A020F0">val</FONT></B> int = IN (mk Int.toString)
<B><FONT COLOR="#A020F0">val</FONT></B> word = IN (surround <B><FONT COLOR="#BC8F8F">"0wx"</FONT></B> <B><FONT COLOR="#BC8F8F">""</FONT></B> o mk Word.toString)
<B><FONT COLOR="#A020F0">val</FONT></B> real = IN (mk Real.toString)
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#0000FF">end</FONT></B>
<I><FONT COLOR="#B22222">(* Handlers for standard top-level exceptions *)</FONT></I>
<B><FONT COLOR="#A020F0">val</FONT></B> () = <B><FONT COLOR="#A020F0">let</FONT></B>
<B><FONT COLOR="#0000FF">open</FONT></B> Show
<B><FONT COLOR="#A020F0">fun</FONT></B> E0 name = SOME ((), C0 name)
<B><FONT COLOR="#A020F0">in</FONT></B>
regExn (<B><FONT COLOR="#A020F0">fn</FONT></B> Bind => E0<B><FONT COLOR="#BC8F8F">"Bind"</FONT></B>
| Chr => E0<B><FONT COLOR="#BC8F8F">"Chr"</FONT></B>
| Div => E0<B><FONT COLOR="#BC8F8F">"Div"</FONT></B>
| Domain => E0<B><FONT COLOR="#BC8F8F">"Domain"</FONT></B>
| Empty => E0<B><FONT COLOR="#BC8F8F">"Empty"</FONT></B>
| Match => E0<B><FONT COLOR="#BC8F8F">"Match"</FONT></B>
| Option => E0<B><FONT COLOR="#BC8F8F">"Option"</FONT></B>
| Overflow => E0<B><FONT COLOR="#BC8F8F">"Overflow"</FONT></B>
| Size => E0<B><FONT COLOR="#BC8F8F">"Size"</FONT></B>
| Span => E0<B><FONT COLOR="#BC8F8F">"Span"</FONT></B>
| Subscript => E0<B><FONT COLOR="#BC8F8F">"Subscript"</FONT></B>
| _ => NONE)
; regExn (<B><FONT COLOR="#A020F0">fn</FONT></B> Fail s => SOME (s, C1<B><FONT COLOR="#BC8F8F">"Fail"</FONT></B> string)
| _ => NONE)
<B><FONT COLOR="#A020F0">end</FONT></B>
</PRE>
<p>
</p>
<h2 id="head-a4bc8bf5caf54b18cea9f58e83dd4acb488deb17">Also see</h2>
<p>
There are a number of related techniques. Here are some of them.
</p>
<ul>
<li>
<p>
<a href="Fold">Fold</a>
</p>
</li>
<li>
<p>
<a href="StaticSum">StaticSum</a>
</p>
</li>
</ul>
</div>
<p>
<hr>
Last edited on 2009-06-10 19:24:04 by <span title="fenrir.uchicago.edu"><a href="MatthewFluet">MatthewFluet</a></span>.
</body></html>
|