<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.8">
<title>TypeIndexedValues</title>
<link rel="stylesheet" href="./asciidoc.css" type="text/css">
<link rel="stylesheet" href="./pygments.css" type="text/css">


<script type="text/javascript" src="./asciidoc.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
asciidoc.install();
/*]]>*/
</script>
<link rel="stylesheet" href="./mlton.css" type="text/css"/>
</head>
<body class="article">
<div id="banner">
<div id="banner-home">
<a href="./Home">MLton 20130715</a>
</div>
</div>
<div id="header">
<h1>TypeIndexedValues</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p><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></div>
<div class="paragraph"><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="https://github.com/MLton/mltonlib/blob/master/com/ssh/generic/unstable/README"><span class="monospaced">README</span></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="https://github.com/MLton/mltonlib/blob/master/com/ssh/generic/unstable/public/value/pretty.sig"><span class="monospaced">pretty.sig</span></a>).</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_example_converting_any_sml_value_to_roughly_sml_syntax">Example: Converting any SML value to (roughly) SML syntax</h2>
<div class="sectionbody">
<div class="paragraph"><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&#8217;ll call this function "<span class="monospaced">show</span>".</p></div>
<div class="paragraph"><p>We&#8217;ll do a fairly complete implementation of <span class="monospaced">show</span>.  We do not
distinguish infix and nonfix constructors, but that is not an
intrinsic property of SML datatypes.  We also don&#8217;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&#8217;ll leave these improvements to an actual library
implementation.</p></div>
<div class="paragraph"><p>The following code uses the <a href="Fixpoints">fixpoint framework</a> and other
utilities from an Extended Basis library (see
<a href="https://github.com/MLton/mltonlib/blob/master/com/ssh/extended-basis/unstable/README"><span class="monospaced">README</span></a>).</p></div>
<div class="sect2">
<h3 id="_signature">Signature</h3>
<div class="paragraph"><p>Let&#8217;s consider the design of the <span class="monospaced">SHOW</span> signature:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">infixr</span><span class="w"> </span><span class="n">--&gt;</span><span class="w"></span>

<span class="k">signature</span><span class="w"> </span><span class="n">SHOW</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">sig</span><span class="w"></span>
<span class="w">   </span><span class="k">type</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w">       </span><span class="cm">(* complete type-index *)</span><span class="w"></span>
<span class="w">   </span><span class="k">type</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">s</span><span class="w">       </span><span class="cm">(* incomplete sum *)</span><span class="w"></span>
<span class="w">   </span><span class="k">type</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;k</span><span class="p">)</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="cm">(* incomplete product *)</span><span class="w"></span>
<span class="w">   </span><span class="k">type</span><span class="w"> </span><span class="n">u</span><span class="w">          </span><span class="cm">(* tuple or unlabelled product *)</span><span class="w"></span>
<span class="w">   </span><span class="k">type</span><span class="w"> </span><span class="n">l</span><span class="w">          </span><span class="cm">(* record or labelled product *)</span><span class="w"></span>

<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">show</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">string</span><span class="w"></span>

<span class="w">   </span><span class="cm">(* user-defined types *)</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">inj</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;b</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"></span>

<span class="w">   </span><span class="cm">(* tuples and records *)</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;k</span><span class="p">)</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;b</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;k</span><span class="p">)</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">((</span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">)</span><span class="w"> </span><span class="n">product</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;k</span><span class="p">)</span><span class="w"> </span><span class="n">p</span><span class="w"></span>

<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">U</span><span class="w"> </span><span class="p">:</span><span class="w">           </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">u</span><span class="p">)</span><span class="w"> </span><span class="n">p</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">L</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">l</span><span class="p">)</span><span class="w"> </span><span class="n">p</span><span class="w"></span>

<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">tuple</span><span class="w">  </span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">u</span><span class="p">)</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">record</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">l</span><span class="p">)</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"></span>

<span class="w">   </span><span class="cm">(* datatypes *)</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;b</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">((</span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">)</span><span class="w"> </span><span class="n">sum</span><span class="p">)</span><span class="w"> </span><span class="n">s</span><span class="w"></span>

<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">C0</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="n">s</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">C1</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">s</span><span class="w"></span>

<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">data</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"></span>

<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">Y</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="n">Tie</span><span class="p">.</span><span class="n">t</span><span class="w"></span>

<span class="w">   </span><span class="cm">(* exceptions *)</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">exn</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">exn</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">regExn</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">exn</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">s</span><span class="p">)</span><span class="w"> </span><span class="n">option</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>

<span class="w">   </span><span class="cm">(* some built-in type constructors *)</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">refc</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">array</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">array</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">list</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">list</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">vector</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">vector</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">--&gt;</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;b</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">)</span><span class="w"> </span><span class="n">t</span><span class="w"></span>

<span class="w">   </span><span class="cm">(* some built-in base types *)</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">bool</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">bool</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">char</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">char</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">int</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">int</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">word</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">word</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">real</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">real</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>While some details are shaped by the specific requirements of <span class="monospaced">show</span>,
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 <span class="monospaced">show</span> 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></div>
<div class="sect3">
<h4 id="_arbitrary_user_defined_datatypes">Arbitrary user-defined datatypes</h4>
<div class="paragraph"><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></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">val</span><span class="w"> </span><span class="n">inj</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;b</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><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></div>
<div class="paragraph"><p>To support products (tuples and records) a product combinator is
provided:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">val</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;k</span><span class="p">)</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;b</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;k</span><span class="p">)</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">((</span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">)</span><span class="w"> </span><span class="n">product</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;k</span><span class="p">)</span><span class="w"> </span><span class="n">p</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The second (phantom) type variable <span class="monospaced">'k</span> is there to distinguish
between labelled and unlabelled products and the type <span class="monospaced">p</span>
distinguishes incomplete products from complete type-indices of type
<span class="monospaced">t</span>.  Most type-indexed values do not need to make such distinctions.</p></div>
<div class="paragraph"><p>To support sums (datatypes) a sum combinator is provided:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">val</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;b</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">((</span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">)</span><span class="w"> </span><span class="n">sum</span><span class="p">)</span><span class="w"> </span><span class="n">s</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Again, the purpose of the type <span class="monospaced">s</span> is to distinguish incomplete sums
from complete type-indices of type <span class="monospaced">t</span>, which usually isn&#8217;t necessary.</p></div>
<div class="paragraph"><p>Finally, to support recursive datatypes, including sets of mutually
recursive datatypes, a <a href="Fixpoints">fixpoint tier</a> is provided:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">val</span><span class="w"> </span><span class="n">Y</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="n">Tie</span><span class="p">.</span><span class="n">t</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Together these combinators (with the more domain specific combinators
<span class="monospaced">U</span>, <span class="monospaced">L</span>, <span class="monospaced">tuple</span>, <span class="monospaced">record</span>, <span class="monospaced">C0</span>, <span class="monospaced">C1</span>, and <span class="monospaced">data</span>) enable one to
encode a type-index for any user-defined datatype.</p></div>
</div>
<div class="sect3">
<h4 id="_exceptions">Exceptions</h4>
<div class="paragraph"><p>The <span class="monospaced">exn</span> 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></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">val</span><span class="w"> </span><span class="n">exn</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">exn</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">regExn</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">exn</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">s</span><span class="p">)</span><span class="w"> </span><span class="n">option</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The universal <span class="monospaced">exn</span> 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></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">exception</span><span class="w"> </span><span class="n">Int</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">int</span><span class="w"></span>

<span class="k">local</span><span class="w"></span>
<span class="w">   </span><span class="k">open</span><span class="w"> </span><span class="n">Show</span><span class="w"></span>
<span class="k">in</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">regExn</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">Int</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">SOME</span><span class="w"> </span><span class="p">(</span><span class="n">v</span><span class="p">,</span><span class="w"> </span><span class="n">C1</span><span class="s">&quot;Int&quot;</span><span class="w"> </span><span class="n">int</span><span class="p">)</span><span class="w"></span>
<span class="w">                     </span><span class="p">|</span><span class="w"> </span><span class="p">_</span><span class="w">     </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">NONE</span><span class="p">)</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Note that a single handler may actually handle an arbitrary number of
different exceptions.</p></div>
</div>
<div class="sect3">
<h4 id="_other_types">Other types</h4>
<div class="paragraph"><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></div>
<div class="paragraph"><p>When arrow types are really supported, unlike in this case, they
usually need special treatment due to the contravariance of arguments.</p></div>
<div class="paragraph"><p>Lists and vectors require special treatment in the case of <span class="monospaced">show</span>,
because of their special syntax.  This isn&#8217;t usually the case.</p></div>
<div class="paragraph"><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></div>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_usage">Usage</h2>
<div class="sectionbody">
<div class="paragraph"><p>Before going to the implementation, let&#8217;s look at some examples.  For
the following examples, we&#8217;ll assume a structure binding
<span class="monospaced">Show :&gt; SHOW</span>.  If you want to try the examples immediately, just
skip forward to the implementation.</p></div>
<div class="paragraph"><p>To use <span class="monospaced">show</span>, one first needs a type-index, which is then given to
<span class="monospaced">show</span>.  To show a list of integers, one would use the type-index
<span class="monospaced">list int</span>, which has the type <span class="monospaced">int list Show.t</span>:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">val</span><span class="w"> </span><span class="s">&quot;[3, 1, 4]&quot;</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">    </span><span class="k">let</span><span class="w"> </span><span class="k">open</span><span class="w"> </span><span class="n">Show</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">show</span><span class="w"> </span><span class="p">(</span><span class="n">list</span><span class="w"> </span><span class="n">int</span><span class="p">)</span><span class="w"> </span><span class="k">end</span><span class="w"></span>
<span class="w">       </span><span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">]</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Likewise, to show a list of lists of characters, one would use the
type-index <span class="monospaced">list (list char)</span>, which has the type <span class="monospaced">char list list
Show.t</span>:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">val</span><span class="w"> </span><span class="s">&quot;[[#</span><span class="se">\&quot;</span><span class="s">a</span><span class="se">\&quot;</span><span class="s">, #</span><span class="se">\&quot;</span><span class="s">b</span><span class="se">\&quot;</span><span class="s">, #</span><span class="se">\&quot;</span><span class="s">c</span><span class="se">\&quot;</span><span class="s">], []]&quot;</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">    </span><span class="k">let</span><span class="w"> </span><span class="k">open</span><span class="w"> </span><span class="n">Show</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">show</span><span class="w"> </span><span class="p">(</span><span class="n">list</span><span class="w"> </span><span class="p">(</span><span class="n">list</span><span class="w"> </span><span class="n">char</span><span class="p">))</span><span class="w"> </span><span class="k">end</span><span class="w"></span>
<span class="w">       </span><span class="p">[[#</span><span class="s">&quot;a&quot;</span><span class="p">,</span><span class="w"> </span><span class="p">#</span><span class="s">&quot;b&quot;</span><span class="p">,</span><span class="w"> </span><span class="p">#</span><span class="s">&quot;c&quot;</span><span class="p">],</span><span class="w"> </span><span class="p">[]]</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Handling standard types is not particularly interesting.  It is more
interesting to see how user-defined types can be handled.  Although
the <span class="monospaced">option</span> 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></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">fun</span><span class="w"> </span><span class="n">option</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">let</span><span class="w"></span>
<span class="w">   </span><span class="k">open</span><span class="w"> </span><span class="n">Show</span><span class="w"></span>
<span class="k">in</span><span class="w"></span>
<span class="w">   </span><span class="n">inj</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">NONE</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">INL</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="w">         </span><span class="p">|</span><span class="w"> </span><span class="n">SOME</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">INR</span><span class="w"> </span><span class="n">v</span><span class="p">)</span><span class="w"></span>
<span class="w">       </span><span class="p">(</span><span class="n">data</span><span class="w"> </span><span class="p">(</span><span class="n">C0</span><span class="s">&quot;NONE&quot;</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="n">C1</span><span class="s">&quot;SOME&quot;</span><span class="w"> </span><span class="n">t</span><span class="p">))</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="s">&quot;SOME 5&quot;</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">    </span><span class="k">let</span><span class="w"> </span><span class="k">open</span><span class="w"> </span><span class="n">Show</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">show</span><span class="w"> </span><span class="p">(</span><span class="n">option</span><span class="w"> </span><span class="n">int</span><span class="p">)</span><span class="w"> </span><span class="k">end</span><span class="w"></span>
<span class="w">       </span><span class="p">(</span><span class="n">SOME</span><span class="w"> </span><span class="mi">5</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><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></div>
<div class="paragraph"><p>Using a product, user specified records can be also be encoded easily:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">val</span><span class="w"> </span><span class="n">abc</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">let</span><span class="w"></span>
<span class="w">   </span><span class="k">open</span><span class="w"> </span><span class="n">Show</span><span class="w"></span>
<span class="k">in</span><span class="w"></span>
<span class="w">   </span><span class="n">inj</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">{</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="p">}</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">&amp;</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="n">&amp;</span><span class="w"> </span><span class="n">c</span><span class="p">)</span><span class="w"></span>
<span class="w">       </span><span class="p">(</span><span class="n">record</span><span class="w"> </span><span class="p">(</span><span class="n">L</span><span class="s">&quot;a&quot;</span><span class="w"> </span><span class="p">(</span><span class="n">option</span><span class="w"> </span><span class="n">int</span><span class="p">)</span><span class="w"> </span><span class="n">*</span><span class="w"></span>
<span class="w">                </span><span class="n">L</span><span class="s">&quot;b&quot;</span><span class="w"> </span><span class="n">real</span><span class="w"> </span><span class="n">*</span><span class="w"></span>
<span class="w">                </span><span class="n">L</span><span class="s">&quot;c&quot;</span><span class="w"> </span><span class="n">bool</span><span class="p">))</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="s">&quot;{a = SOME 1, b = 3.0, c = false}&quot;</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">    </span><span class="k">let</span><span class="w"> </span><span class="k">open</span><span class="w"> </span><span class="n">Show</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">show</span><span class="w"> </span><span class="n">abc</span><span class="w"> </span><span class="k">end</span><span class="w"></span>
<span class="w">       </span><span class="p">{</span><span class="n">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">SOME</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mf">3.0</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">false</span><span class="p">}</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>As you can see, both of the above use <span class="monospaced">inj</span> to inject user-defined
types to the general purpose sum and product types.</p></div>
<div class="paragraph"><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></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">datatype</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">graph</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">VTX</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">graph</span><span class="w"> </span><span class="n">list</span><span class="w"> </span><span class="n">ref</span><span class="w"></span>
<span class="k">fun</span><span class="w"> </span><span class="n">arcs</span><span class="w"> </span><span class="p">(</span><span class="n">VTX</span><span class="w"> </span><span class="p">(_,</span><span class="w"> </span><span class="n">r</span><span class="p">))</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">r</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Using the <span class="monospaced">Show</span> combinators, we could first write a new type-index
combinator for <span class="monospaced">graph</span>:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">fun</span><span class="w"> </span><span class="n">graph</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">let</span><span class="w"></span>
<span class="w">   </span><span class="k">open</span><span class="w"> </span><span class="n">Tie</span><span class="w"> </span><span class="n">Show</span><span class="w"></span>
<span class="k">in</span><span class="w"></span>
<span class="w">   </span><span class="n">fix</span><span class="w"> </span><span class="n">Y</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">graph_a</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">             </span><span class="n">inj</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">VTX</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="n">&amp;</span><span class="w"> </span><span class="n">y</span><span class="p">)</span><span class="w"></span>
<span class="w">                 </span><span class="p">(</span><span class="n">data</span><span class="w"> </span><span class="p">(</span><span class="n">C1</span><span class="s">&quot;VTX&quot;</span><span class="w"></span>
<span class="w">                          </span><span class="p">(</span><span class="n">tuple</span><span class="w"> </span><span class="p">(</span><span class="n">U</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">*</span><span class="w"></span>
<span class="w">                                  </span><span class="n">U</span><span class="w"> </span><span class="p">(</span><span class="n">refc</span><span class="w"> </span><span class="p">(</span><span class="n">list</span><span class="w"> </span><span class="n">graph_a</span><span class="p">)))))))</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>To show a graph with integer labels</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">val</span><span class="w"> </span><span class="n">a_graph</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">let</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">VTX</span><span class="w"> </span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="p">[])</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">VTX</span><span class="w"> </span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="p">[])</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">VTX</span><span class="w"> </span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="p">[])</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">d</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">VTX</span><span class="w"> </span><span class="p">(</span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="p">[])</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">e</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">VTX</span><span class="w"> </span><span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="p">[])</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">VTX</span><span class="w"> </span><span class="p">(</span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="p">[])</span><span class="w"></span>
<span class="k">in</span><span class="w"></span>
<span class="w">   </span><span class="n">arcs</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="p">[</span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">d</span><span class="p">]</span><span class="w"></span>
<span class="w"> </span><span class="p">;</span><span class="w"> </span><span class="n">arcs</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="p">[</span><span class="n">c</span><span class="p">,</span><span class="w"> </span><span class="n">e</span><span class="p">]</span><span class="w"></span>
<span class="w"> </span><span class="p">;</span><span class="w"> </span><span class="n">arcs</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="p">[</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">f</span><span class="p">]</span><span class="w"></span>
<span class="w"> </span><span class="p">;</span><span class="w"> </span><span class="n">arcs</span><span class="w"> </span><span class="n">d</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="p">[</span><span class="n">f</span><span class="p">]</span><span class="w"></span>
<span class="w"> </span><span class="p">;</span><span class="w"> </span><span class="n">arcs</span><span class="w"> </span><span class="n">e</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="p">[</span><span class="n">d</span><span class="p">]</span><span class="w"></span>
<span class="w"> </span><span class="p">;</span><span class="w"> </span><span class="n">arcs</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="p">[</span><span class="n">e</span><span class="p">]</span><span class="w"></span>
<span class="w"> </span><span class="p">;</span><span class="w"> </span><span class="n">a</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>we could then simply write</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">val</span><span class="w"> </span><span class="s">&quot;VTX (1, ref [VTX (2, ref [VTX (3, ref [VTX (1, %0), \</span>
<span class="s">    \VTX (6, ref [VTX (5, ref [VTX (4, ref [VTX (6, %3)])])] as %3)]), \</span>
<span class="s">    \VTX (5, ref [VTX (4, ref [VTX (6, ref [VTX (5, %2)])])] as %2)]), \</span>
<span class="s">    \VTX (4, ref [VTX (6, ref [VTX (5, ref [VTX (4, %1)])])] as %1)] as %0)&quot;</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">    </span><span class="k">let</span><span class="w"> </span><span class="k">open</span><span class="w"> </span><span class="n">Show</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">show</span><span class="w"> </span><span class="p">(</span><span class="n">graph</span><span class="w"> </span><span class="n">int</span><span class="p">)</span><span class="w"> </span><span class="k">end</span><span class="w"></span>
<span class="w">       </span><span class="n">a_graph</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>There is a subtle gotcha with cyclic data.  Consider the following code:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">exception</span><span class="w"> </span><span class="n">ExnArray</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">exn</span><span class="w"> </span><span class="n">array</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">let</span><span class="w"></span>
<span class="w">   </span><span class="k">open</span><span class="w"> </span><span class="n">Show</span><span class="w"></span>
<span class="k">in</span><span class="w"></span>
<span class="w">   </span><span class="n">regExn</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">ExnArray</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">              </span><span class="n">SOME</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">C1</span><span class="s">&quot;ExnArray&quot;</span><span class="w"> </span><span class="p">(</span><span class="n">array</span><span class="w"> </span><span class="n">exn</span><span class="p">))</span><span class="w"></span>
<span class="w">            </span><span class="p">|</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">NONE</span><span class="p">)</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="n">a_cycle</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">let</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Array</span><span class="p">.</span><span class="n">fromList</span><span class="w"> </span><span class="p">[</span><span class="n">Empty</span><span class="p">]</span><span class="w"></span>
<span class="k">in</span><span class="w"></span>
<span class="w">   </span><span class="n">Array</span><span class="p">.</span><span class="n">update</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">ExnArray</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="p">;</span><span class="w"> </span><span class="n">a</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Although the above looks innocent enough, the evaluation  of</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">val</span><span class="w"> </span><span class="s">&quot;[|ExnArray %0|] as %0&quot;</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">    </span><span class="k">let</span><span class="w"> </span><span class="k">open</span><span class="w"> </span><span class="n">Show</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">show</span><span class="w"> </span><span class="p">(</span><span class="n">array</span><span class="w"> </span><span class="n">exn</span><span class="p">)</span><span class="w"> </span><span class="k">end</span><span class="w"></span>
<span class="w">       </span><span class="n">a_cycle</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>goes into an infinite loop.  To avoid this problem, the type-index
<span class="monospaced">array exn</span> must be evaluated only once, as in the following:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">val</span><span class="w"> </span><span class="n">array_exn</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">let</span><span class="w"> </span><span class="k">open</span><span class="w"> </span><span class="n">Show</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">array</span><span class="w"> </span><span class="n">exn</span><span class="w"> </span><span class="k">end</span><span class="w"></span>

<span class="k">exception</span><span class="w"> </span><span class="n">ExnArray</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">exn</span><span class="w"> </span><span class="n">array</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">let</span><span class="w"></span>
<span class="w">   </span><span class="k">open</span><span class="w"> </span><span class="n">Show</span><span class="w"></span>
<span class="k">in</span><span class="w"></span>
<span class="w">   </span><span class="n">regExn</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">ExnArray</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">              </span><span class="n">SOME</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">C1</span><span class="s">&quot;ExnArray&quot;</span><span class="w"> </span><span class="n">array_exn</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="p">|</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">NONE</span><span class="p">)</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="n">a_cycle</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">let</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Array</span><span class="p">.</span><span class="n">fromList</span><span class="w"> </span><span class="p">[</span><span class="n">Empty</span><span class="p">]</span><span class="w"></span>
<span class="k">in</span><span class="w"></span>
<span class="w">   </span><span class="n">Array</span><span class="p">.</span><span class="n">update</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">ExnArray</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="p">;</span><span class="w"> </span><span class="n">a</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="s">&quot;[|ExnArray %0|] as %0&quot;</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">    </span><span class="k">let</span><span class="w"> </span><span class="k">open</span><span class="w"> </span><span class="n">Show</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">show</span><span class="w"> </span><span class="n">array_exn</span><span class="w"> </span><span class="k">end</span><span class="w"></span>
<span class="w">       </span><span class="n">a_cycle</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><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 <span class="monospaced">ref</span> or <span class="monospaced">array</span> is called with a
type-index, a new cyclicity checker is instantiated.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_implementation">Implementation</h2>
<div class="sectionbody">
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">structure</span><span class="w"> </span><span class="n">SmlSyntax</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">struct</span><span class="w"></span>
<span class="w">   </span><span class="k">local</span><span class="w"></span>
<span class="w">      </span><span class="k">structure</span><span class="w"> </span><span class="n">CV</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">CharVector</span><span class="w"> </span><span class="k">and</span><span class="w"> </span><span class="n">C</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Char</span><span class="w"></span>
<span class="w">   </span><span class="k">in</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">isSym</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Char</span><span class="p">.</span><span class="n">contains</span><span class="w"> </span><span class="s">&quot;!%&amp;$#+-/:&lt;=&gt;?@</span><span class="se">\\</span><span class="s">~`^|*&quot;</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">isSymId</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="n">&lt;</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="k">andalso</span><span class="w"> </span><span class="n">CV</span><span class="p">.</span><span class="n">all</span><span class="w"> </span><span class="n">isSym</span><span class="w"> </span><span class="n">s</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">isAlphaNumId</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">          </span><span class="mi">0</span><span class="w"> </span><span class="n">&lt;</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="n">s</span><span class="w"></span>
<span class="w">          </span><span class="k">andalso</span><span class="w"> </span><span class="n">C</span><span class="p">.</span><span class="n">isAlpha</span><span class="w"> </span><span class="p">(</span><span class="n">CV</span><span class="p">.</span><span class="n">sub</span><span class="w"> </span><span class="p">(</span><span class="n">s</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">))</span><span class="w"></span>
<span class="w">          </span><span class="k">andalso</span><span class="w"> </span><span class="n">CV</span><span class="p">.</span><span class="n">all</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">C</span><span class="p">.</span><span class="n">isAlphaNum</span><span class="w"> </span><span class="n">c</span><span class="w"></span>
<span class="w">                                  </span><span class="k">orelse</span><span class="w"> </span><span class="p">#</span><span class="s">&quot;&#39;&quot;</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">c</span><span class="w"></span>
<span class="w">                                  </span><span class="k">orelse</span><span class="w"> </span><span class="p">#</span><span class="s">&quot;_&quot;</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">c</span><span class="p">)</span><span class="w"> </span><span class="n">s</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">isNumLabel</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">          </span><span class="mi">0</span><span class="w"> </span><span class="n">&lt;</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="n">s</span><span class="w"></span>
<span class="w">          </span><span class="k">andalso</span><span class="w"> </span><span class="p">#</span><span class="s">&quot;0&quot;</span><span class="w"> </span><span class="n">&lt;&gt;</span><span class="w"> </span><span class="n">CV</span><span class="p">.</span><span class="n">sub</span><span class="w"> </span><span class="p">(</span><span class="n">s</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"></span>
<span class="w">          </span><span class="k">andalso</span><span class="w"> </span><span class="n">CV</span><span class="p">.</span><span class="n">all</span><span class="w"> </span><span class="n">C</span><span class="p">.</span><span class="n">isDigit</span><span class="w"> </span><span class="n">s</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">isId</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">isAlphaNumId</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="k">orelse</span><span class="w"> </span><span class="n">isSymId</span><span class="w"> </span><span class="n">s</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">isLongId</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">List</span><span class="p">.</span><span class="n">all</span><span class="w"> </span><span class="n">isId</span><span class="w"> </span><span class="p">(</span><span class="n">String</span><span class="p">.</span><span class="n">fields</span><span class="w"> </span><span class="p">(#</span><span class="s">&quot;.&quot;</span><span class="w"> </span><span class="n">&lt;\</span><span class="w"> </span><span class="k">op</span><span class="w"> </span><span class="p">=)</span><span class="w"> </span><span class="n">s</span><span class="p">)</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">isLabel</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">isId</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="k">orelse</span><span class="w"> </span><span class="n">isNumLabel</span><span class="w"> </span><span class="n">s</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>

<span class="k">structure</span><span class="w"> </span><span class="n">Show</span><span class="w"> </span><span class="p">:&gt;</span><span class="w"> </span><span class="n">SHOW</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">struct</span><span class="w"></span>
<span class="w">   </span><span class="k">datatype</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">IN</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">exn</span><span class="w"> </span><span class="n">list</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">bool</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">string</span><span class="w"></span>
<span class="w">   </span><span class="k">type</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">   </span><span class="k">type</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;k</span><span class="p">)</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">   </span><span class="k">type</span><span class="w"> </span><span class="n">u</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
<span class="w">   </span><span class="k">type</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>

<span class="w">   </span><span class="k">fun</span><span class="w"> </span><span class="n">show</span><span class="w"> </span><span class="p">(</span><span class="n">IN</span><span class="w"> </span><span class="n">t</span><span class="p">)</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">#</span><span class="mi">2</span><span class="w"> </span><span class="p">(</span><span class="n">t</span><span class="w"> </span><span class="p">([],</span><span class="w"> </span><span class="n">x</span><span class="p">))</span><span class="w"></span>

<span class="w">   </span><span class="cm">(* user-defined types *)</span><span class="w"></span>
<span class="w">   </span><span class="k">fun</span><span class="w"> </span><span class="n">inj</span><span class="w"> </span><span class="n">inj</span><span class="w"> </span><span class="p">(</span><span class="n">IN</span><span class="w"> </span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">IN</span><span class="w"> </span><span class="p">(</span><span class="n">b</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">Pair</span><span class="p">.</span><span class="n">map</span><span class="w"> </span><span class="p">(</span><span class="n">id</span><span class="p">,</span><span class="w"> </span><span class="n">inj</span><span class="p">))</span><span class="w"></span>

<span class="w">   </span><span class="k">local</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">surround</span><span class="w"> </span><span class="n">pre</span><span class="w"> </span><span class="n">suf</span><span class="w"> </span><span class="p">(_,</span><span class="w"> </span><span class="n">s</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="n">false</span><span class="p">,</span><span class="w"> </span><span class="n">concat</span><span class="w"> </span><span class="p">[</span><span class="n">pre</span><span class="p">,</span><span class="w"> </span><span class="n">s</span><span class="p">,</span><span class="w"> </span><span class="n">suf</span><span class="p">])</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">parenthesize</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">#</span><span class="mi">1</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="k">then</span><span class="w"> </span><span class="n">surround</span><span class="w"> </span><span class="s">&quot;(&quot;</span><span class="w"> </span><span class="s">&quot;)&quot;</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="n">x</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">construct</span><span class="w"> </span><span class="n">tag</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">          </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">(_,</span><span class="w"> </span><span class="n">s</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">true</span><span class="p">,</span><span class="w"> </span><span class="n">concat</span><span class="w"> </span><span class="p">[</span><span class="n">tag</span><span class="p">,</span><span class="w"> </span><span class="s">&quot; &quot;</span><span class="p">,</span><span class="w"> </span><span class="n">s</span><span class="p">]))</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">parenthesize</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">check</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="k">then</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">raise</span><span class="w"> </span><span class="n">Fail</span><span class="w"> </span><span class="p">(</span><span class="n">m^s</span><span class="p">)</span><span class="w"></span>
<span class="w">   </span><span class="k">in</span><span class="w"></span>
<span class="w">      </span><span class="cm">(* tuples and records *)</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="p">(</span><span class="n">IN</span><span class="w"> </span><span class="n">l</span><span class="p">)</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">IN</span><span class="w"> </span><span class="n">r</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">          </span><span class="n">IN</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">(</span><span class="n">rs</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">&amp;</span><span class="w"> </span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">                 </span><span class="p">(</span><span class="n">false</span><span class="p">,</span><span class="w"> </span><span class="n">concat</span><span class="w"> </span><span class="p">[#</span><span class="mi">2</span><span class="w"> </span><span class="p">(</span><span class="n">l</span><span class="w"> </span><span class="p">(</span><span class="n">rs</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">)),</span><span class="w"></span>
<span class="w">                                 </span><span class="s">&quot;, &quot;</span><span class="p">,</span><span class="w"></span>
<span class="w">                                 </span><span class="p">#</span><span class="mi">2</span><span class="w"> </span><span class="p">(</span><span class="n">r</span><span class="w"> </span><span class="p">(</span><span class="n">rs</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">))]))</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">U</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">id</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">L</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="n">check</span><span class="w"> </span><span class="n">SmlSyntax</span><span class="p">.</span><span class="n">isLabel</span><span class="w"> </span><span class="s">&quot;Invalid label: &quot;</span><span class="w"> </span><span class="n">l</span><span class="w"></span>
<span class="w">               </span><span class="p">;</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">IN</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">IN</span><span class="w"> </span><span class="p">(</span><span class="n">surround</span><span class="w"> </span><span class="p">(</span><span class="n">l^</span><span class="s">&quot; = &quot;</span><span class="p">)</span><span class="w"> </span><span class="s">&quot;&quot;</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">t</span><span class="p">))</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">tuple</span><span class="w"> </span><span class="p">(</span><span class="n">IN</span><span class="w"> </span><span class="n">t</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">IN</span><span class="w"> </span><span class="p">(</span><span class="n">surround</span><span class="w"> </span><span class="s">&quot;(&quot;</span><span class="w"> </span><span class="s">&quot;)&quot;</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">t</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">record</span><span class="w"> </span><span class="p">(</span><span class="n">IN</span><span class="w"> </span><span class="n">t</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">IN</span><span class="w"> </span><span class="p">(</span><span class="n">surround</span><span class="w"> </span><span class="s">&quot;{&quot;</span><span class="w"> </span><span class="s">&quot;}&quot;</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">t</span><span class="p">)</span><span class="w"></span>

<span class="w">      </span><span class="cm">(* datatypes *)</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="p">(</span><span class="n">IN</span><span class="w"> </span><span class="n">l</span><span class="p">)</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="p">(</span><span class="n">IN</span><span class="w"> </span><span class="n">r</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">IN</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">(</span><span class="n">rs</span><span class="p">,</span><span class="w"> </span><span class="n">INL</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">l</span><span class="w"> </span><span class="p">(</span><span class="n">rs</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="w"></span>
<span class="w">                                 </span><span class="p">|</span><span class="w"> </span><span class="p">(</span><span class="n">rs</span><span class="p">,</span><span class="w"> </span><span class="n">INR</span><span class="w"> </span><span class="n">b</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="p">(</span><span class="n">rs</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">))</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">C0</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="n">check</span><span class="w"> </span><span class="n">SmlSyntax</span><span class="p">.</span><span class="n">isId</span><span class="w"> </span><span class="s">&quot;Invalid constructor: &quot;</span><span class="w"> </span><span class="n">c</span><span class="w"></span>
<span class="w">                </span><span class="p">;</span><span class="w"> </span><span class="n">IN</span><span class="w"> </span><span class="p">(</span><span class="n">const</span><span class="w"> </span><span class="p">(</span><span class="n">false</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="p">)))</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">C1</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">(</span><span class="n">IN</span><span class="w"> </span><span class="n">t</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="n">check</span><span class="w"> </span><span class="n">SmlSyntax</span><span class="p">.</span><span class="n">isId</span><span class="w"> </span><span class="s">&quot;Invalid constructor: &quot;</span><span class="w"> </span><span class="n">c</span><span class="w"></span>
<span class="w">                       </span><span class="p">;</span><span class="w"> </span><span class="n">IN</span><span class="w"> </span><span class="p">(</span><span class="n">construct</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">t</span><span class="p">))</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">data</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">id</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">Y</span><span class="w"> </span><span class="n">?</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Tie</span><span class="p">.</span><span class="n">iso</span><span class="w"> </span><span class="n">Tie</span><span class="p">.</span><span class="n">function</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">IN</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">IN</span><span class="p">)</span><span class="w"> </span><span class="n">?</span><span class="w"></span>

<span class="w">      </span><span class="cm">(* exceptions *)</span><span class="w"></span>
<span class="w">      </span><span class="k">local</span><span class="w"></span>
<span class="w">         </span><span class="k">val</span><span class="w"> </span><span class="n">handlers</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="p">([]</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">exn</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="n">option</span><span class="p">)</span><span class="w"> </span><span class="n">list</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span><span class="k">in</span><span class="w"></span>
<span class="w">         </span><span class="k">val</span><span class="w"> </span><span class="n">exn</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">IN</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">(</span><span class="n">rs</span><span class="p">,</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="k">let</span><span class="w"></span>
<span class="w">                             </span><span class="k">fun</span><span class="w"> </span><span class="n">lp</span><span class="w"> </span><span class="p">[]</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">                                 </span><span class="n">C0</span><span class="p">(</span><span class="n">concat</span><span class="w"> </span><span class="p">[</span><span class="s">&quot;&lt;exn:&quot;</span><span class="p">,</span><span class="w"></span>
<span class="w">                                            </span><span class="n">General</span><span class="p">.</span><span class="n">exnName</span><span class="w"> </span><span class="n">e</span><span class="p">,</span><span class="w"></span>
<span class="w">                                            </span><span class="s">&quot;&gt;&quot;</span><span class="p">])</span><span class="w"></span>
<span class="w">                               </span><span class="p">|</span><span class="w"> </span><span class="n">lp</span><span class="w"> </span><span class="p">(</span><span class="n">f::fs</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">                                 </span><span class="k">case</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="n">e</span><span class="w"></span>
<span class="w">                                  </span><span class="k">of</span><span class="w"> </span><span class="n">NONE</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">lp</span><span class="w"> </span><span class="n">fs</span><span class="w"></span>
<span class="w">                                   </span><span class="p">|</span><span class="w"> </span><span class="n">SOME</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">                             </span><span class="k">val</span><span class="w"> </span><span class="n">IN</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">lp</span><span class="w"> </span><span class="p">(</span><span class="n">!handlers</span><span class="p">)</span><span class="w"></span>
<span class="w">                          </span><span class="k">in</span><span class="w"></span>
<span class="w">                             </span><span class="n">f</span><span class="w"> </span><span class="p">(</span><span class="n">rs</span><span class="p">,</span><span class="w"> </span><span class="p">())</span><span class="w"></span>
<span class="w">                          </span><span class="k">end</span><span class="p">)</span><span class="w"></span>
<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">regExn</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">             </span><span class="n">handlers</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="p">(</span><span class="n">Option</span><span class="p">.</span><span class="n">map</span><span class="w"></span>
<span class="w">                             </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">IN</span><span class="w"> </span><span class="n">f</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">                                 </span><span class="n">IN</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">(</span><span class="n">rs</span><span class="p">,</span><span class="w"> </span><span class="p">())</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">                                        </span><span class="n">f</span><span class="w"> </span><span class="p">(</span><span class="n">rs</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">)))</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">f</span><span class="p">)</span><span class="w"></span>
<span class="w">                         </span><span class="n">::</span><span class="w"> </span><span class="n">!handlers</span><span class="w"></span>
<span class="w">      </span><span class="k">end</span><span class="w"></span>

<span class="w">      </span><span class="cm">(* some built-in type constructors *)</span><span class="w"></span>
<span class="w">      </span><span class="k">local</span><span class="w"></span>
<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">cyclic</span><span class="w"> </span><span class="p">(</span><span class="n">IN</span><span class="w"> </span><span class="n">t</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">let</span><span class="w"></span>
<span class="w">            </span><span class="k">exception</span><span class="w"> </span><span class="n">E</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">&#39;&#39;a</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">bool</span><span class="w"> </span><span class="n">ref</span><span class="w"></span>
<span class="w">         </span><span class="k">in</span><span class="w"></span>
<span class="w">            </span><span class="n">IN</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">(</span><span class="n">rs</span><span class="p">,</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">&#39;&#39;a</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="k">let</span><span class="w"></span>
<span class="w">                      </span><span class="k">val</span><span class="w"> </span><span class="n">idx</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Int</span><span class="p">.</span><span class="n">toString</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">length</span><span class="w"></span>
<span class="w">                      </span><span class="k">fun</span><span class="w"> </span><span class="n">lp</span><span class="w"> </span><span class="p">(</span><span class="n">E</span><span class="w"> </span><span class="p">(</span><span class="n">v&#39;</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="p">)</span><span class="n">::rs</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">                          </span><span class="k">if</span><span class="w"> </span><span class="n">v&#39;</span><span class="w"> </span><span class="n">&lt;&gt;</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="k">then</span><span class="w"> </span><span class="n">lp</span><span class="w"> </span><span class="n">rs</span><span class="w"></span>
<span class="w">                          </span><span class="k">else</span><span class="w"> </span><span class="p">(</span><span class="n">c</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="n">false</span><span class="w"> </span><span class="p">;</span><span class="w"> </span><span class="p">(</span><span class="n">false</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;%&quot;</span><span class="n">^idx</span><span class="w"> </span><span class="n">rs</span><span class="p">))</span><span class="w"></span>
<span class="w">                        </span><span class="p">|</span><span class="w"> </span><span class="n">lp</span><span class="w"> </span><span class="p">(_</span><span class="n">::rs</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">lp</span><span class="w"> </span><span class="n">rs</span><span class="w"></span>
<span class="w">                        </span><span class="p">|</span><span class="w"> </span><span class="n">lp</span><span class="w"> </span><span class="p">[]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">let</span><span class="w"></span>
<span class="w">                             </span><span class="k">val</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="n">true</span><span class="w"></span>
<span class="w">                             </span><span class="k">val</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">(</span><span class="n">E</span><span class="w"> </span><span class="p">(</span><span class="n">v</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="p">)</span><span class="n">::rs</span><span class="p">,</span><span class="w"> </span><span class="n">v</span><span class="p">)</span><span class="w"></span>
<span class="w">                          </span><span class="k">in</span><span class="w"></span>
<span class="w">                             </span><span class="k">if</span><span class="w"> </span><span class="n">!c</span><span class="w"> </span><span class="k">then</span><span class="w"> </span><span class="n">r</span><span class="w"></span>
<span class="w">                             </span><span class="k">else</span><span class="w"> </span><span class="n">surround</span><span class="w"> </span><span class="s">&quot;&quot;</span><span class="w"> </span><span class="p">(</span><span class="s">&quot; as %&quot;</span><span class="n">^idx</span><span class="w"> </span><span class="n">rs</span><span class="p">)</span><span class="w"> </span><span class="n">r</span><span class="w"></span>
<span class="w">                          </span><span class="k">end</span><span class="w"></span>
<span class="w">                   </span><span class="k">in</span><span class="w"></span>
<span class="w">                      </span><span class="n">lp</span><span class="w"> </span><span class="n">rs</span><span class="w"></span>
<span class="w">                   </span><span class="k">end</span><span class="p">)</span><span class="w"></span>
<span class="w">         </span><span class="k">end</span><span class="w"></span>

<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">aggregate</span><span class="w"> </span><span class="n">pre</span><span class="w"> </span><span class="n">suf</span><span class="w"> </span><span class="n">toList</span><span class="w"> </span><span class="p">(</span><span class="n">IN</span><span class="w"> </span><span class="n">t</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">             </span><span class="n">IN</span><span class="w"> </span><span class="p">(</span><span class="n">surround</span><span class="w"> </span><span class="n">pre</span><span class="w"> </span><span class="n">suf</span><span class="w"> </span><span class="n">o</span><span class="w"></span>
<span class="w">                 </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">(</span><span class="n">rs</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">                     </span><span class="p">(</span><span class="n">false</span><span class="p">,</span><span class="w"></span>
<span class="w">                      </span><span class="n">String</span><span class="p">.</span><span class="n">concatWith</span><span class="w"></span>
<span class="w">                         </span><span class="s">&quot;, &quot;</span><span class="w"></span>
<span class="w">                         </span><span class="p">(</span><span class="n">map</span><span class="w"> </span><span class="p">(#</span><span class="mi">2</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">curry</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="n">rs</span><span class="p">)</span><span class="w"></span>
<span class="w">                              </span><span class="p">(</span><span class="n">toList</span><span class="w"> </span><span class="n">a</span><span class="p">)))))</span><span class="w"></span>
<span class="w">      </span><span class="k">in</span><span class="w"></span>
<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">refc</span><span class="w"> </span><span class="n">?</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="n">cyclic</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">inj</span><span class="w"> </span><span class="n">!</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">C1</span><span class="s">&quot;ref&quot;</span><span class="p">)</span><span class="w"> </span><span class="n">?</span><span class="w"></span>
<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">array</span><span class="w"> </span><span class="n">?</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="n">cyclic</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">aggregate</span><span class="w"> </span><span class="s">&quot;[|&quot;</span><span class="w"> </span><span class="s">&quot;|]&quot;</span><span class="w"> </span><span class="p">(</span><span class="n">Array</span><span class="p">.</span><span class="n">foldr</span><span class="w"> </span><span class="k">op</span><span class="n">::</span><span class="w"> </span><span class="p">[]))</span><span class="w"> </span><span class="n">?</span><span class="w"></span>
<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">list</span><span class="w"> </span><span class="n">?</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">aggregate</span><span class="w"> </span><span class="s">&quot;[&quot;</span><span class="w"> </span><span class="s">&quot;]&quot;</span><span class="w"> </span><span class="n">id</span><span class="w"> </span><span class="n">?</span><span class="w"></span>
<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">vector</span><span class="w"> </span><span class="n">?</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">aggregate</span><span class="w"> </span><span class="s">&quot;#[&quot;</span><span class="w"> </span><span class="s">&quot;]&quot;</span><span class="w"> </span><span class="p">(</span><span class="n">Vector</span><span class="p">.</span><span class="n">foldr</span><span class="w"> </span><span class="k">op</span><span class="n">::</span><span class="w"> </span><span class="p">[])</span><span class="w"> </span><span class="n">?</span><span class="w"></span>
<span class="w">      </span><span class="k">end</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="p">(</span><span class="n">IN</span><span class="w"> </span><span class="p">_)</span><span class="w"> </span><span class="n">--&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">IN</span><span class="w"> </span><span class="p">_)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">IN</span><span class="w"> </span><span class="p">(</span><span class="n">const</span><span class="w"> </span><span class="p">(</span><span class="n">false</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;&lt;fn&gt;&quot;</span><span class="p">))</span><span class="w"></span>

<span class="w">      </span><span class="cm">(* some built-in base types *)</span><span class="w"></span>
<span class="w">      </span><span class="k">local</span><span class="w"></span>
<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">mk</span><span class="w"> </span><span class="n">toS</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">false</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">))</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">toS</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">(_,</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span><span class="k">in</span><span class="w"></span>
<span class="w">         </span><span class="k">val</span><span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">             </span><span class="n">IN</span><span class="w"> </span><span class="p">(</span><span class="n">surround</span><span class="w"> </span><span class="s">&quot;</span><span class="se">\&quot;</span><span class="s">&quot;</span><span class="w"> </span><span class="s">&quot;</span><span class="se">\&quot;</span><span class="s">&quot;</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">mk</span><span class="w"> </span><span class="p">(</span><span class="n">String</span><span class="p">.</span><span class="n">translate</span><span class="w"> </span><span class="n">Char</span><span class="p">.</span><span class="n">toString</span><span class="p">))</span><span class="w"></span>
<span class="w">         </span><span class="k">val</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">IN</span><span class="w"> </span><span class="p">(</span><span class="n">mk</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="s">&quot;()&quot;</span><span class="p">))</span><span class="w"></span>
<span class="w">         </span><span class="k">val</span><span class="w"> </span><span class="n">bool</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">IN</span><span class="w"> </span><span class="p">(</span><span class="n">mk</span><span class="w"> </span><span class="n">Bool</span><span class="p">.</span><span class="n">toString</span><span class="p">)</span><span class="w"></span>
<span class="w">         </span><span class="k">val</span><span class="w"> </span><span class="n">char</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">IN</span><span class="w"> </span><span class="p">(</span><span class="n">surround</span><span class="w"> </span><span class="s">&quot;#</span><span class="se">\&quot;</span><span class="s">&quot;</span><span class="w"> </span><span class="s">&quot;</span><span class="se">\&quot;</span><span class="s">&quot;</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">mk</span><span class="w"> </span><span class="n">Char</span><span class="p">.</span><span class="n">toString</span><span class="p">)</span><span class="w"></span>
<span class="w">         </span><span class="k">val</span><span class="w"> </span><span class="n">int</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">IN</span><span class="w"> </span><span class="p">(</span><span class="n">mk</span><span class="w"> </span><span class="n">Int</span><span class="p">.</span><span class="n">toString</span><span class="p">)</span><span class="w"></span>
<span class="w">         </span><span class="k">val</span><span class="w"> </span><span class="n">word</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">IN</span><span class="w"> </span><span class="p">(</span><span class="n">surround</span><span class="w"> </span><span class="s">&quot;0wx&quot;</span><span class="w"> </span><span class="s">&quot;&quot;</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">mk</span><span class="w"> </span><span class="n">Word</span><span class="p">.</span><span class="n">toString</span><span class="p">)</span><span class="w"></span>
<span class="w">         </span><span class="k">val</span><span class="w"> </span><span class="n">real</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">IN</span><span class="w"> </span><span class="p">(</span><span class="n">mk</span><span class="w"> </span><span class="n">Real</span><span class="p">.</span><span class="n">toString</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span><span class="k">end</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>

<span class="cm">(* Handlers for standard top-level exceptions *)</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">let</span><span class="w"></span>
<span class="w">   </span><span class="k">open</span><span class="w"> </span><span class="n">Show</span><span class="w"></span>
<span class="w">   </span><span class="k">fun</span><span class="w"> </span><span class="n">E0</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">SOME</span><span class="w"> </span><span class="p">((),</span><span class="w"> </span><span class="n">C0</span><span class="w"> </span><span class="n">name</span><span class="p">)</span><span class="w"></span>
<span class="k">in</span><span class="w"></span>
<span class="w">   </span><span class="n">regExn</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">Bind</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">E0</span><span class="s">&quot;Bind&quot;</span><span class="w"></span>
<span class="w">            </span><span class="p">|</span><span class="w"> </span><span class="n">Chr</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">E0</span><span class="s">&quot;Chr&quot;</span><span class="w"></span>
<span class="w">            </span><span class="p">|</span><span class="w"> </span><span class="n">Div</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">E0</span><span class="s">&quot;Div&quot;</span><span class="w"></span>
<span class="w">            </span><span class="p">|</span><span class="w"> </span><span class="n">Domain</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">E0</span><span class="s">&quot;Domain&quot;</span><span class="w"></span>
<span class="w">            </span><span class="p">|</span><span class="w"> </span><span class="n">Empty</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">E0</span><span class="s">&quot;Empty&quot;</span><span class="w"></span>
<span class="w">            </span><span class="p">|</span><span class="w"> </span><span class="n">Match</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">E0</span><span class="s">&quot;Match&quot;</span><span class="w"></span>
<span class="w">            </span><span class="p">|</span><span class="w"> </span><span class="n">Option</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">E0</span><span class="s">&quot;Option&quot;</span><span class="w"></span>
<span class="w">            </span><span class="p">|</span><span class="w"> </span><span class="n">Overflow</span><span class="w">  </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">E0</span><span class="s">&quot;Overflow&quot;</span><span class="w"></span>
<span class="w">            </span><span class="p">|</span><span class="w"> </span><span class="n">Size</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">E0</span><span class="s">&quot;Size&quot;</span><span class="w"></span>
<span class="w">            </span><span class="p">|</span><span class="w"> </span><span class="n">Span</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">E0</span><span class="s">&quot;Span&quot;</span><span class="w"></span>
<span class="w">            </span><span class="p">|</span><span class="w"> </span><span class="n">Subscript</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">E0</span><span class="s">&quot;Subscript&quot;</span><span class="w"></span>
<span class="w">            </span><span class="p">|</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">NONE</span><span class="p">)</span><span class="w"></span>
<span class="w"> </span><span class="p">;</span><span class="w"> </span><span class="n">regExn</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">Fail</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">SOME</span><span class="w"> </span><span class="p">(</span><span class="n">s</span><span class="p">,</span><span class="w"> </span><span class="n">C1</span><span class="s">&quot;Fail&quot;</span><span class="w"> </span><span class="n">string</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="p">|</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">NONE</span><span class="p">)</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>
</pre></div></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_also_see">Also see</h2>
<div class="sectionbody">
<div class="paragraph"><p>There are a number of related techniques.  Here are some of them.</p></div>
<div class="ulist"><ul>
<li>
<p>
<a href="Fold">Fold</a>
</p>
</li>
<li>
<p>
<a href="StaticSum">StaticSum</a>
</p>
</li>
</ul></div>
</div>
</div>
</div>
<div id="footnotes"><hr></div>
<div id="footer">
<div id="footer-text">
</div>
<div id="footer-badges">
</div>
</div>
</body>
</html>
