<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta name="robots" content="index,nofollow">



<title>TypeIndexedValues - MLton Standard ML Compiler (SML Compiler)</title>
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="all" href="common.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="screen" href="screen.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="print" href="print.css">


<link rel="Start" href="Home">


</head>

<body lang="en" dir="ltr">

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-833377-1";
urchinTracker();
</script>
<table bgcolor = lightblue cellspacing = 0 style = "border: 0px;" width = 100%>
  <tr>
    <td style = "
		border: 0px;
		color: darkblue; 
		font-size: 150%;
		text-align: left;">
      <a class = mltona href="Home">MLton MLTONWIKIVERSION</a>
    <td style = "
		border: 0px;
		font-size: 150%;
		text-align: center;
		width: 50%;">
      TypeIndexedValues
    <td style = "
		border: 0px;
		text-align: right;">
      <table cellspacing = 0 style = "border: 0px">
        <tr style = "vertical-align: middle;">
      </table>
  <tr style = "background-color: white;">
    <td colspan = 3
	style = "
		border: 0px;
		font-size:70%;
		text-align: right;">
      <a href = "Home">Home</a>
      &nbsp;<a href = "Index">Index</a>
      &nbsp;
</table>
<div id="content" lang="en" dir="ltr">
<a href="StandardML">Standard ML</a> does not support ad hoc polymorphism.  This presents a challenge to programmers.  The problem is that at first glance there seems to be no practical way to implement something like a function for converting a value of any type to a string or a function for computing a hash value for a value of any type.  Fortunately there are ways to implement type-indexed values in SML as discussed in <a href = "References#Yang98">Yang98</a>. Various articles such as <a href = "References#Danvy98">Danvy98</a>, <a href = "References#Ramsey03">Ramsey03</a>, <a href = "References#Elsman04">Elsman04</a>, <a href = "References#Kennedy04">Kennedy04</a>, and <a href = "References#Benton05">Benton05</a> also contain examples of type-indexed values. <p>
<strong>NOTE:</strong> The technique used in the following example uses an early (and somewhat broken) variation of the basic technique used in an experimental generic programming library (see <a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mltonlib/trunk/com/ssh/generic/unstable/README?view=markup"><img src="moin-www.png" alt="[WWW]" height="11" width="11">README</a>) that can be found from the MLton repository.  The generic programming library also includes a more advanced generic pretty printing function (see <a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mltonlib/trunk/com/ssh/generic/unstable/public/value/pretty.sig?view=markup"><img src="moin-www.png" alt="[WWW]" height="11" width="11">pretty.sig</a>). 
</p>
<h2 id="head-2ea0b5ca6b0ef3808544cdd88ef0b644bbad42c4">Example: Converting any SML value to (roughly) SML syntax</h2>
<p>
Consider the problem of converting any SML value to a textual presentation that matches the syntax of SML as closely as possible.  One solution is a type-indexed function that maps a given type to a function that maps any value (of the type) to its textual presentation.  A type-indexed function like this can be useful for a variety of purposes.  For example, one could use it to show debugging information.  We'll call this function "<tt>show</tt>". 
</p>
<p>
We'll do a fairly complete implementation of <tt>show</tt>.  We do not distinguish infix and nonfix constructors, but that is not an intrinsic property of SML datatypes.  We also don't reconstruct a type name for the value, although it would be particularly useful for functional values.  To reconstruct type names, some changes would be needed and the reader is encouraged to consider how to do that.  A more realistic implementation would use some pretty printing combinators to compute a layout for the result.  This should be a relatively easy change (given a suitable pretty printing library).  Cyclic values (through references and arrays) do not have a standard textual presentation and it is impossible to convert arbitrary functional values (within SML) to a meaningful textual presentation.  Finally, it would also make sense to show sharing of references and arrays.  We'll leave these improvements to an actual library implementation. 
</p>
<p>
The following code uses the <a href="Fixpoints">fixpoint framework</a> and other utilities from an Extended Basis library (see <a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mltonlib/trunk/com/ssh/extended-basis/unstable/README?view=markup"><img src="moin-www.png" alt="[WWW]" height="11" width="11">README</a>). 
</p>
<h3 id="head-2f32be1dc74166373c988ec03b0bd86f0a576919">Signature</h3>
<p>
Let's consider the design of the <tt>SHOW</tt> signature: 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">infixr</FONT></B> --&gt;

<B><FONT COLOR="#0000FF">signature</FONT></B> SHOW = <B><FONT COLOR="#0000FF">sig</FONT></B>
   <B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> 'a t       <I><FONT COLOR="#B22222">(* complete type-index *)</FONT></I>
   </FONT></B><B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> 'a s       <I><FONT COLOR="#B22222">(* incomplete sum *)</FONT></I>
   </FONT></B><B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> ('a, 'k) p <I><FONT COLOR="#B22222">(* incomplete product *)</FONT></I>
   </FONT></B><B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> u          <I><FONT COLOR="#B22222">(* tuple or unlabelled product *)</FONT></I>
   </FONT></B><B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> l          <I><FONT COLOR="#B22222">(* record or labelled product *)</FONT></I>

   </FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> show : 'a t -&gt; 'a -&gt; string

   <I><FONT COLOR="#B22222">(* user-defined types *)</FONT></I>
   <B><FONT COLOR="#A020F0">val</FONT></B> inj : ('a -&gt; 'b) -&gt; 'b t -&gt; 'a t

   <I><FONT COLOR="#B22222">(* tuples and records *)</FONT></I>
   <B><FONT COLOR="#A020F0">val</FONT></B> * : ('a, 'k) p * ('b, 'k) p -&gt; (('a, 'b) product, 'k) p

   <B><FONT COLOR="#A020F0">val</FONT></B> U :           'a t -&gt; ('a, u) p
   <B><FONT COLOR="#A020F0">val</FONT></B> L : string -&gt; 'a t -&gt; ('a, l) p

   <B><FONT COLOR="#A020F0">val</FONT></B> tuple  : ('a, u) p -&gt; 'a t
   <B><FONT COLOR="#A020F0">val</FONT></B> record : ('a, l) p -&gt; 'a t

   <I><FONT COLOR="#B22222">(* datatypes *)</FONT></I>
   <B><FONT COLOR="#A020F0">val</FONT></B> + : 'a s * 'b s -&gt; (('a, 'b) sum) s

   <B><FONT COLOR="#A020F0">val</FONT></B> C0 : string -&gt; unit s
   <B><FONT COLOR="#A020F0">val</FONT></B> C1 : string -&gt; 'a t -&gt; 'a s

   <B><FONT COLOR="#A020F0">val</FONT></B> data : 'a s -&gt; 'a t

   <B><FONT COLOR="#A020F0">val</FONT></B> Y : 'a t Tie.t

   <I><FONT COLOR="#B22222">(* exceptions *)</FONT></I>
   <B><FONT COLOR="#A020F0">val</FONT></B> exn : exn t
   <B><FONT COLOR="#A020F0">val</FONT></B> regExn : (exn -&gt; ('a * 'a s) option) -&gt; unit

   <I><FONT COLOR="#B22222">(* some built-in type constructors *)</FONT></I>
   <B><FONT COLOR="#A020F0">val</FONT></B> refc : 'a t -&gt; 'a ref t
   <B><FONT COLOR="#A020F0">val</FONT></B> array : 'a t -&gt; 'a array t
   <B><FONT COLOR="#A020F0">val</FONT></B> list : 'a t -&gt; 'a list t
   <B><FONT COLOR="#A020F0">val</FONT></B> vector : 'a t -&gt; 'a vector t
   <B><FONT COLOR="#A020F0">val</FONT></B> --&gt; : 'a t * 'b t -&gt; ('a -&gt; 'b) t

   <I><FONT COLOR="#B22222">(* some built-in base types *)</FONT></I>
   <B><FONT COLOR="#A020F0">val</FONT></B> string : string t
   <B><FONT COLOR="#A020F0">val</FONT></B> unit : unit t
   <B><FONT COLOR="#A020F0">val</FONT></B> bool : bool t
   <B><FONT COLOR="#A020F0">val</FONT></B> char : char t
   <B><FONT COLOR="#A020F0">val</FONT></B> int : int t
   <B><FONT COLOR="#A020F0">val</FONT></B> word : word t
   <B><FONT COLOR="#A020F0">val</FONT></B> real : real t
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
 
</p>
<p>
While some details are shaped by the specific requirements of <tt>show</tt>, there are a number of (design) patterns that translate to other type-indexed values.  The former kind of details are mostly shaped by the syntax of SML values that <tt>show</tt> is designed to produce.  To this end, abstract types and phantom types are used to distinguish incomplete record, tuple, and datatype type-indices from each other and from complete type-indices.  Also, names of record labels and datatype constructors need to be provided by the user. 
</p>
<h4 id="head-671800661391e0ee68700b83d223516c787ea4c9">Arbitrary user-defined datatypes</h4>
<p>
Perhaps the most important pattern is how the design supports arbitrary user-defined datatypes.  A number of combinators together conspire to provide the functionality.  First of all, to support new user-defined types, a combinator taking a conversion function to a previously supported type is provided: 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> inj : ('a -&gt; 'b) -&gt; 'b t -&gt; 'a t
</PRE>
<p>
 
</p>
<p>
An injection function is sufficient in this case, but in the general case, an embedding with injection and projection functions may be needed. 
</p>
<p>
To support products (tuples and records) a product combinator is provided: 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> * : ('a, 'k) p * ('b, 'k) p -&gt; (('a, 'b) product, 'k) p
</PRE>
<p>
 
</p>
<p>
The second (phantom) type variable <tt>'k</tt> is there to distinguish between labelled and unlabelled products and the type <tt>p</tt> distinguishes incomplete products from complete type-indices of type <tt>t</tt>.  Most type-indexed values do not need to make such distinctions. 
</p>
<p>
To support sums (datatypes) a sum combinator is provided: 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> + : 'a s * 'b s -&gt; (('a, 'b) sum) s
</PRE>
<p>
 
</p>
<p>
Again, the purpose of the type <tt>s</tt> is to distinguish incomplete sums from complete type-indices of type <tt>t</tt>, which usually isn't necessary. 
</p>
<p>
Finally, to support recursive datatypes, including sets of mutually recursive datatypes, a <a href="Fixpoints">fixpoint tier</a> is provided: 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> Y : 'a t Tie.t
</PRE>
<p>
 
</p>
<p>
Together these combinators (with the more domain specific combinators <tt>U</tt>, <tt>L</tt>, <tt>tuple</tt>, <tt>record</tt>, <tt>C0</tt>, <tt>C1</tt>, and <tt>data</tt>) enable one to encode a type-index for any user-defined datatype. 
</p>
<h4 id="head-cd3dfbe79fda3a9d92de67b1bd4f1dc9f60ab723">Exceptions</h4>
<p>
The <tt>exn</tt> type in SML is a <a href="UniversalType">universal type</a> into which all types can be embedded.  SML also allows a program to generate new exception variants at run-time.  Thus a mechanism is required to register handlers for particular variants: 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> exn : exn t
<B><FONT COLOR="#A020F0">val</FONT></B> regExn : (exn -&gt; ('a * 'a s) option) -&gt; unit
</PRE>
<p>
 
</p>
<p>
The universal <tt>exn</tt> type-index then makes use of the registered handlers.  The above particular form of handler, which converts an exception value to a value of some type and a type-index for that type (essentially an existential type) is designed to make it convenient to write handlers.  To write a handler, one can conveniently reuse existing type-indices: 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">exception</FONT></B><B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">Int</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> int

</FONT></B><B><FONT COLOR="#0000FF">local</FONT></B>
   <B><FONT COLOR="#0000FF">open</FONT></B> Show
<B><FONT COLOR="#0000FF">in</FONT></B>
   <B><FONT COLOR="#A020F0">val</FONT></B> () = regExn (<B><FONT COLOR="#A020F0">fn</FONT></B> Int v =&gt; SOME (v, C1<B><FONT COLOR="#BC8F8F">&quot;Int&quot;</FONT></B> int)
                     | _     =&gt; NONE)
<B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
 
</p>
<p>
Note that a single handler may actually handle an arbitrary number of different exceptions. 
</p>
<h4 id="head-8c5befcc2e0951ae7c577c186d5015110ab50c93">Other types</h4>
<p>
Some built-in and standard types typically require special treatment due to their special nature.  The most important of these are arrays and references, because cyclic data (ignoring closures) and observable sharing can only be constructed through them. 
</p>
<p>
When arrow types are really supported, unlike in this case, they usually need special treatment due to the contravariance of arguments. 
</p>
<p>
Lists and vectors require special treatment in the case of <tt>show</tt>, because of their special syntax.  This isn't usually the case. 
</p>
<p>
The set of base types to support also needs to be considered unless one exports an interface for constructing type-indices for entirely new base types. 
</p>
<h3 id="head-0bb18642b70b9f8a9c12ccf39487328f306b8e19">Usage</h3>
<p>
Before going to the implementation, let's look at some examples.  For the following examples, we'll assume a structure binding <tt>Show&nbsp;:&gt;&nbsp;SHOW</tt>. If you want to try the examples immediately, just skip forward to the implementation. 
</p>
<p>
To use <tt>show</tt>, one first needs a type-index, which is then given to <tt>show</tt>.  To show a list of integers, one would use the type-index <tt>list&nbsp;int</tt>, which has the type <tt>int&nbsp;list&nbsp;Show.t</tt>: 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;[3, 1, 4]&quot;</FONT></B> =
    <B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#0000FF">open</FONT></B> Show <B><FONT COLOR="#A020F0">in</FONT></B> show (list int) <B><FONT COLOR="#A020F0">end</FONT></B>
       [<B><FONT COLOR="#5F9EA0">3</FONT></B>, <B><FONT COLOR="#5F9EA0">1</FONT></B>, <B><FONT COLOR="#5F9EA0">4</FONT></B>]
</PRE>
<p>
 
</p>
<p>
Likewise, to show a list of lists of characters, one would use the type-index <tt>list&nbsp;(list&nbsp;char)</tt>, which has the type <tt>char&nbsp;list&nbsp;list&nbsp;Show.t</tt>: 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;[[#\&quot;a\&quot;], [#\&quot;b\&quot;, #\&quot;c\&quot;], []]&quot;</FONT></B> =
    <B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#0000FF">open</FONT></B> Show <B><FONT COLOR="#A020F0">in</FONT></B> show (list (list char)) <B><FONT COLOR="#A020F0">end</FONT></B>
       [[#<B><FONT COLOR="#BC8F8F">&quot;a&quot;</FONT></B>], [#<B><FONT COLOR="#BC8F8F">&quot;b&quot;</FONT></B>, #<B><FONT COLOR="#BC8F8F">&quot;c&quot;</FONT></B>], []]
</PRE>
<p>
 
</p>
<p>
Handling standard types is not particularly interesting.  It is more interesting to see how user-defined types can be handled.  Although the <tt>option</tt> datatype is a standard type, it requires no special support, so we can treat it as a user-defined type.  Options can be encoded easily using a sum: 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">fun</FONT></B> option t = <B><FONT COLOR="#A020F0">let</FONT></B>
   <B><FONT COLOR="#0000FF">open</FONT></B> Show
<B><FONT COLOR="#A020F0">in</FONT></B>
   inj (<B><FONT COLOR="#A020F0">fn</FONT></B> NONE =&gt; INL ()
         | SOME v =&gt; INR v)
       (data (C0<B><FONT COLOR="#BC8F8F">&quot;NONE&quot;</FONT></B> + C1<B><FONT COLOR="#BC8F8F">&quot;SOME&quot;</FONT></B> t))
<B><FONT COLOR="#A020F0">end</FONT></B>

<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;SOME 5&quot;</FONT></B> =
    <B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#0000FF">open</FONT></B> Show <B><FONT COLOR="#A020F0">in</FONT></B> show (option int) <B><FONT COLOR="#A020F0">end</FONT></B>
       (SOME <B><FONT COLOR="#5F9EA0">5</FONT></B>)
</PRE>
<p>
 
</p>
<p>
Readers new to type-indexed values might want to type annotate each subexpression of the above example as an exercise.  (Use a compiler to check your annotations.) 
</p>
<p>
Using a product, user specified records can be also be encoded easily: 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> abc = <B><FONT COLOR="#A020F0">let</FONT></B>
   <B><FONT COLOR="#0000FF">open</FONT></B> Show
<B><FONT COLOR="#A020F0">in</FONT></B>
   inj (<B><FONT COLOR="#A020F0">fn</FONT></B> {a, b, c} =&gt; a &amp; b &amp; c)
       (record (L<B><FONT COLOR="#BC8F8F">&quot;a&quot;</FONT></B> (option int) *
                L<B><FONT COLOR="#BC8F8F">&quot;b&quot;</FONT></B> real *
                L<B><FONT COLOR="#BC8F8F">&quot;c&quot;</FONT></B> bool))
<B><FONT COLOR="#A020F0">end</FONT></B>

<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;{a = SOME 1, b = 3.0, c = false}&quot;</FONT></B> =
    <B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#0000FF">open</FONT></B> Show <B><FONT COLOR="#A020F0">in</FONT></B> show abc <B><FONT COLOR="#A020F0">end</FONT></B>
       {a = SOME <B><FONT COLOR="#5F9EA0">1</FONT></B>, b = <B><FONT COLOR="#5F9EA0">3.0</FONT></B>, c = false}
</PRE>
<p>
 
</p>
<p>
As you can see, both of the above use <tt>inj</tt> to inject user-defined types to the general purpose sum and product types. 
</p>
<p>
Of particular interest is whether recursive datatypes and cyclic data can be handled.  For example, how does one write a type-index for a recursive datatype such as a cyclic graph? 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> 'a graph </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">VTX</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> 'a * 'a graph list ref
</FONT></B><B><FONT COLOR="#A020F0">fun</FONT></B> arcs (VTX (_, r)) = r
</PRE>
<p>
 
</p>
<p>
Using the <tt>Show</tt> combinators, we could first write a new type-index combinator for <tt>graph</tt>: 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">fun</FONT></B> graph a = <B><FONT COLOR="#A020F0">let</FONT></B>
   <B><FONT COLOR="#0000FF">open</FONT></B> Tie Show
<B><FONT COLOR="#A020F0">in</FONT></B>
   fix Y (<B><FONT COLOR="#A020F0">fn</FONT></B> graph_a =&gt;
             inj (<B><FONT COLOR="#A020F0">fn</FONT></B> VTX (x, y) =&gt; x &amp; y)
                 (data (C1<B><FONT COLOR="#BC8F8F">&quot;VTX&quot;</FONT></B>
                          (tuple (U a *
                                  U (refc (list graph_a)))))))
<B><FONT COLOR="#A020F0">end</FONT></B>
</PRE>
<p>
 
</p>
<p>
To show a graph with integer labels 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> a_graph = <B><FONT COLOR="#A020F0">let</FONT></B>
   <B><FONT COLOR="#A020F0">val</FONT></B> a = VTX (<B><FONT COLOR="#5F9EA0">1</FONT></B>, ref [])
   <B><FONT COLOR="#A020F0">val</FONT></B> b = VTX (<B><FONT COLOR="#5F9EA0">2</FONT></B>, ref [])
   <B><FONT COLOR="#A020F0">val</FONT></B> c = VTX (<B><FONT COLOR="#5F9EA0">3</FONT></B>, ref [])
   <B><FONT COLOR="#A020F0">val</FONT></B> d = VTX (<B><FONT COLOR="#5F9EA0">4</FONT></B>, ref [])
   <B><FONT COLOR="#A020F0">val</FONT></B> e = VTX (<B><FONT COLOR="#5F9EA0">5</FONT></B>, ref [])
   <B><FONT COLOR="#A020F0">val</FONT></B> f = VTX (<B><FONT COLOR="#5F9EA0">6</FONT></B>, ref [])
<B><FONT COLOR="#A020F0">in</FONT></B>
   arcs a := [b, d]
 ; arcs b := [c, e]
 ; arcs c := [a, f]
 ; arcs d := [f]
 ; arcs e := [d]
 ; arcs f := [e]
 ; a
<B><FONT COLOR="#A020F0">end</FONT></B>
</PRE>
<p>
 
</p>
<p>
we could then simply write 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;VTX (1, ref [VTX (2, ref [VTX (3, ref [VTX (1, %0), \
    \VTX (6, ref [VTX (5, ref [VTX (4, ref [VTX (6, %3)])])] as %3)]), \
    \VTX (5, ref [VTX (4, ref [VTX (6, ref [VTX (5, %2)])])] as %2)]), \
    \VTX (4, ref [VTX (6, ref [VTX (5, ref [VTX (4, %1)])])] as %1)] as %0)&quot;</FONT></B> =
    <B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#0000FF">open</FONT></B> Show <B><FONT COLOR="#A020F0">in</FONT></B> show (graph int) <B><FONT COLOR="#A020F0">end</FONT></B>
       a_graph
</PRE>
<p>
 
</p>
<p>
There is a subtle gotcha with cyclic data.  Consider the following code: 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">exception</FONT></B><B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">ExnArray</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> exn array

</FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> () = <B><FONT COLOR="#A020F0">let</FONT></B>
   <B><FONT COLOR="#0000FF">open</FONT></B> Show
<B><FONT COLOR="#A020F0">in</FONT></B>
   regExn (<B><FONT COLOR="#A020F0">fn</FONT></B> ExnArray a =&gt;
              SOME (a, C1<B><FONT COLOR="#BC8F8F">&quot;ExnArray&quot;</FONT></B> (array exn))
            | _ =&gt; NONE)
<B><FONT COLOR="#A020F0">end</FONT></B>

<B><FONT COLOR="#A020F0">val</FONT></B> a_cycle = <B><FONT COLOR="#A020F0">let</FONT></B>
   <B><FONT COLOR="#A020F0">val</FONT></B> a = Array.fromList [Empty]
<B><FONT COLOR="#A020F0">in</FONT></B>
   Array.update (a, <B><FONT COLOR="#5F9EA0">0</FONT></B>, ExnArray a) ; a
<B><FONT COLOR="#A020F0">end</FONT></B>
</PRE>
<p>
 
</p>
<p>
Although the above looks innocent enough, the evaluation  of 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;[|ExnArray %0|] as %0&quot;</FONT></B> =
    <B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#0000FF">open</FONT></B> Show <B><FONT COLOR="#A020F0">in</FONT></B> show (array exn) <B><FONT COLOR="#A020F0">end</FONT></B>
       a_cycle
</PRE>
<p>
 
</p>
<p>
goes into an infinite loop.  To avoid this problem, the type-index <tt>array&nbsp;exn</tt> must be evaluated only once, as in the following: 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> array_exn = <B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#0000FF">open</FONT></B> Show <B><FONT COLOR="#A020F0">in</FONT></B> array exn <B><FONT COLOR="#A020F0">end</FONT></B>

<B><FONT COLOR="#A020F0">exception</FONT></B><B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">ExnArray</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> exn array

</FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> () = <B><FONT COLOR="#A020F0">let</FONT></B>
   <B><FONT COLOR="#0000FF">open</FONT></B> Show
<B><FONT COLOR="#A020F0">in</FONT></B>
   regExn (<B><FONT COLOR="#A020F0">fn</FONT></B> ExnArray a =&gt;
              SOME (a, C1<B><FONT COLOR="#BC8F8F">&quot;ExnArray&quot;</FONT></B> array_exn)
            | _ =&gt; NONE)
<B><FONT COLOR="#A020F0">end</FONT></B>

<B><FONT COLOR="#A020F0">val</FONT></B> a_cycle = <B><FONT COLOR="#A020F0">let</FONT></B>
   <B><FONT COLOR="#A020F0">val</FONT></B> a = Array.fromList [Empty]
<B><FONT COLOR="#A020F0">in</FONT></B>
   Array.update (a, <B><FONT COLOR="#5F9EA0">0</FONT></B>, ExnArray a) ; a
<B><FONT COLOR="#A020F0">end</FONT></B>

<B><FONT COLOR="#A020F0">val</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;[|ExnArray %0|] as %0&quot;</FONT></B> =
    <B><FONT COLOR="#A020F0">let</FONT></B> <B><FONT COLOR="#0000FF">open</FONT></B> Show <B><FONT COLOR="#A020F0">in</FONT></B> show array_exn <B><FONT COLOR="#A020F0">end</FONT></B>
       a_cycle
</PRE>
<p>
 
</p>
<p>
Cyclic data (excluding closures) in Standard ML can only be constructed imperatively through arrays and references (combined with exceptions or recursive datatypes).  Before recursing to a reference or an array, one needs to check whether that reference or array has already been seen before.  When <tt>ref</tt> or <tt>array</tt> is called with a type-index, a new cyclicity checker is instantiated. 
</p>
<h3 id="head-8781d615fd77be9578225c40ac67b9471394cced">Implementation</h3>

<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> SmlSyntax = <B><FONT COLOR="#0000FF">struct</FONT></B>
   <B><FONT COLOR="#A020F0">local</FONT></B>
      <B><FONT COLOR="#0000FF">structure</FONT></B> CV = CharVector <B><FONT COLOR="#0000FF">and</FONT></B> C = Char
   <B><FONT COLOR="#A020F0">in</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> isSym = Char.contains <B><FONT COLOR="#BC8F8F">&quot;!%&amp;$#+-/:&lt;=&gt;?@\\~`^|*&quot;</FONT></B>

      <B><FONT COLOR="#A020F0">fun</FONT></B> isSymId s = <B><FONT COLOR="#5F9EA0">0</FONT></B> &lt; size s <B><FONT COLOR="#A020F0">andalso</FONT></B> CV.all isSym s

      <B><FONT COLOR="#A020F0">fun</FONT></B> isAlphaNumId s =
          <B><FONT COLOR="#5F9EA0">0</FONT></B> &lt; size s
          <B><FONT COLOR="#A020F0">andalso</FONT></B> C.isAlpha (CV.sub (s, <B><FONT COLOR="#5F9EA0">0</FONT></B>))
          <B><FONT COLOR="#A020F0">andalso</FONT></B> CV.all (<B><FONT COLOR="#A020F0">fn</FONT></B> c =&gt; C.isAlphaNum c
                                  <B><FONT COLOR="#A020F0">orelse</FONT></B> #<B><FONT COLOR="#BC8F8F">&quot;'&quot;</FONT></B> = c
                                  <B><FONT COLOR="#A020F0">orelse</FONT></B> #<B><FONT COLOR="#BC8F8F">&quot;_&quot;</FONT></B> = c) s

      <B><FONT COLOR="#A020F0">fun</FONT></B> isNumLabel s =
          <B><FONT COLOR="#5F9EA0">0</FONT></B> &lt; size s
          <B><FONT COLOR="#A020F0">andalso</FONT></B> #<B><FONT COLOR="#BC8F8F">&quot;0&quot;</FONT></B> &lt;&gt; CV.sub (s, <B><FONT COLOR="#5F9EA0">0</FONT></B>)
          <B><FONT COLOR="#A020F0">andalso</FONT></B> CV.all C.isDigit s

      <B><FONT COLOR="#A020F0">fun</FONT></B> isId s = isAlphaNumId s <B><FONT COLOR="#A020F0">orelse</FONT></B> isSymId s

      <B><FONT COLOR="#A020F0">fun</FONT></B> isLongId s = List.all isId (String.fields (#<B><FONT COLOR="#BC8F8F">&quot;.&quot;</FONT></B> &lt;\ <B><FONT COLOR="#A020F0">op</FONT></B> =) s)

      <B><FONT COLOR="#A020F0">fun</FONT></B> isLabel s = isId s <B><FONT COLOR="#A020F0">orelse</FONT></B> isNumLabel s
   <B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#0000FF">end</FONT></B>

<B><FONT COLOR="#0000FF">structure</FONT></B> Show :&gt; SHOW = <B><FONT COLOR="#0000FF">struct</FONT></B>
   <B><FONT COLOR="#A020F0">datatype</FONT></B><B><FONT COLOR="#228B22"> 'a t </FONT></B>=<B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">IN</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> exn list * 'a -&gt; bool * string
   </FONT></B><B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> 'a s </FONT></B>=<B><FONT COLOR="#228B22"> 'a t
   </FONT></B><B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> ('a, 'k) p </FONT></B>=<B><FONT COLOR="#228B22"> 'a t
   </FONT></B><B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> u </FONT></B>=<B><FONT COLOR="#228B22"> unit
   </FONT></B><B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> l </FONT></B>=<B><FONT COLOR="#228B22"> unit

   </FONT></B><B><FONT COLOR="#A020F0">fun</FONT></B> show (IN t) x = #<B><FONT COLOR="#5F9EA0">2</FONT></B> (t ([], x))

   <I><FONT COLOR="#B22222">(* user-defined types *)</FONT></I>
   <B><FONT COLOR="#A020F0">fun</FONT></B> inj inj (IN b) = IN (b o Pair.map (id, inj))

   <B><FONT COLOR="#A020F0">local</FONT></B>
      <B><FONT COLOR="#A020F0">fun</FONT></B> surround pre suf (_, s) = (false, concat [pre, s, suf])
      <B><FONT COLOR="#A020F0">fun</FONT></B> parenthesize x = <B><FONT COLOR="#A020F0">if</FONT></B> #1 x <B><FONT COLOR="#A020F0">then</FONT></B> surround <B><FONT COLOR="#BC8F8F">&quot;(&quot;</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;)&quot;</FONT></B> x <B><FONT COLOR="#A020F0">else</FONT></B> x
      <B><FONT COLOR="#A020F0">fun</FONT></B> construct tag =
          (<B><FONT COLOR="#A020F0">fn</FONT></B> (_, s) =&gt; (true, concat [tag, <B><FONT COLOR="#BC8F8F">&quot; &quot;</FONT></B>, s])) o parenthesize
      <B><FONT COLOR="#A020F0">fun</FONT></B> check p m s = <B><FONT COLOR="#A020F0">if</FONT></B> p s <B><FONT COLOR="#A020F0">then</FONT></B> () <B><FONT COLOR="#A020F0">else</FONT></B> <B><FONT COLOR="#A020F0">raise</FONT></B> Fail (m^s)
   <B><FONT COLOR="#A020F0">in</FONT></B>
      <I><FONT COLOR="#B22222">(* tuples and records *)</FONT></I>
      <B><FONT COLOR="#A020F0">fun</FONT></B> (IN l) * (IN r) =
          IN (<B><FONT COLOR="#A020F0">fn</FONT></B> (rs, a &amp; b) =&gt;
                 (false, concat [#<B><FONT COLOR="#5F9EA0">2</FONT></B> (l (rs, a)),
                                 <B><FONT COLOR="#BC8F8F">&quot;, &quot;</FONT></B>,
                                 #<B><FONT COLOR="#5F9EA0">2</FONT></B> (r (rs, b))]))

      <B><FONT COLOR="#A020F0">val</FONT></B> U = id
      <B><FONT COLOR="#A020F0">fun</FONT></B> L l = (check SmlSyntax.isLabel <B><FONT COLOR="#BC8F8F">&quot;Invalid label: &quot;</FONT></B> l
               ; <B><FONT COLOR="#A020F0">fn</FONT></B> IN t =&gt; IN (surround (l^<B><FONT COLOR="#BC8F8F">&quot; = &quot;</FONT></B>) <B><FONT COLOR="#BC8F8F">&quot;&quot;</FONT></B> o t))

      <B><FONT COLOR="#A020F0">fun</FONT></B> tuple (IN t) = IN (surround <B><FONT COLOR="#BC8F8F">&quot;(&quot;</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;)&quot;</FONT></B> o t)
      <B><FONT COLOR="#A020F0">fun</FONT></B> record (IN t) = IN (surround <B><FONT COLOR="#BC8F8F">&quot;{&quot;</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;}&quot;</FONT></B> o t)

      <I><FONT COLOR="#B22222">(* datatypes *)</FONT></I>
      <B><FONT COLOR="#A020F0">fun</FONT></B> (IN l) + (IN r) = IN (<B><FONT COLOR="#A020F0">fn</FONT></B> (rs, INL a) =&gt; l (rs, a)
                                 | (rs, INR b) =&gt; r (rs, b))

      <B><FONT COLOR="#A020F0">fun</FONT></B> C0 c = (check SmlSyntax.isId <B><FONT COLOR="#BC8F8F">&quot;Invalid constructor: &quot;</FONT></B> c
                ; IN (const (false, c)))
      <B><FONT COLOR="#A020F0">fun</FONT></B> C1 c (IN t) = (check SmlSyntax.isId <B><FONT COLOR="#BC8F8F">&quot;Invalid constructor: &quot;</FONT></B> c
                       ; IN (construct c o t))

      <B><FONT COLOR="#A020F0">val</FONT></B> data = id

      <B><FONT COLOR="#A020F0">fun</FONT></B> Y ? = Tie.iso Tie.function (<B><FONT COLOR="#A020F0">fn</FONT></B> IN x =&gt; x, IN) ?

      <I><FONT COLOR="#B22222">(* exceptions *)</FONT></I>
      <B><FONT COLOR="#A020F0">local</FONT></B>
         <B><FONT COLOR="#A020F0">val</FONT></B> handlers = ref ([] : (exn -&gt; unit t option) list)
      <B><FONT COLOR="#A020F0">in</FONT></B>
         <B><FONT COLOR="#A020F0">val</FONT></B> exn = IN (<B><FONT COLOR="#A020F0">fn</FONT></B> (rs, e) =&gt; <B><FONT COLOR="#A020F0">let</FONT></B>
                             <B><FONT COLOR="#A020F0">fun</FONT></B> lp [] =
                                 C0(concat [<B><FONT COLOR="#BC8F8F">&quot;&lt;exn:&quot;</FONT></B>,
                                            General.exnName e,
                                            <B><FONT COLOR="#BC8F8F">&quot;&gt;&quot;</FONT></B>])
                               | lp (f::fs) =
                                 <B><FONT COLOR="#A020F0">case</FONT></B> f e
                                  <B><FONT COLOR="#A020F0">of</FONT></B> NONE =&gt; lp fs
                                   | SOME t =&gt; t
                             <B><FONT COLOR="#A020F0">val</FONT></B> IN f = lp (!handlers)
                          <B><FONT COLOR="#A020F0">in</FONT></B>
                             f (rs, ())
                          <B><FONT COLOR="#A020F0">end</FONT></B>)
         <B><FONT COLOR="#A020F0">fun</FONT></B> regExn f =
             handlers := (Option.map
                             (<B><FONT COLOR="#A020F0">fn</FONT></B> (x, IN f) =&gt;
                                 IN (<B><FONT COLOR="#A020F0">fn</FONT></B> (rs, ()) =&gt;
                                        f (rs, x))) o f)
                         :: !handlers
      <B><FONT COLOR="#A020F0">end</FONT></B>

      <I><FONT COLOR="#B22222">(* some built-in type constructors *)</FONT></I>
      <B><FONT COLOR="#A020F0">local</FONT></B>
         <B><FONT COLOR="#A020F0">fun</FONT></B> cyclic (IN t) = <B><FONT COLOR="#A020F0">let</FONT></B>
            <B><FONT COLOR="#A020F0">exception</FONT></B><B><FONT COLOR="#228B22"> <FONT COLOR="#B8860B">E</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> ''a * bool ref
         </FONT></B><B><FONT COLOR="#A020F0">in</FONT></B>
            IN (<B><FONT COLOR="#A020F0">fn</FONT></B> (rs, v : ''a) =&gt; <B><FONT COLOR="#A020F0">let</FONT></B>
                      <B><FONT COLOR="#A020F0">val</FONT></B> idx = Int.toString o length
                      <B><FONT COLOR="#A020F0">fun</FONT></B> lp (E (v', c)::rs) =
                          <B><FONT COLOR="#A020F0">if</FONT></B> v' &lt;&gt; v <B><FONT COLOR="#A020F0">then</FONT></B> lp rs
                          <B><FONT COLOR="#A020F0">else</FONT></B> (c := false ; (false, <B><FONT COLOR="#BC8F8F">&quot;%&quot;</FONT></B>^idx rs))
                        | lp (_::rs) = lp rs
                        | lp [] = <B><FONT COLOR="#A020F0">let</FONT></B>
                             <B><FONT COLOR="#A020F0">val</FONT></B> c = ref true
                             <B><FONT COLOR="#A020F0">val</FONT></B> r = t (E (v, c)::rs, v)
                          <B><FONT COLOR="#A020F0">in</FONT></B>
                             <B><FONT COLOR="#A020F0">if</FONT></B> !c <B><FONT COLOR="#A020F0">then</FONT></B> r
                             <B><FONT COLOR="#A020F0">else</FONT></B> surround <B><FONT COLOR="#BC8F8F">&quot;&quot;</FONT></B> (<B><FONT COLOR="#BC8F8F">&quot; as %&quot;</FONT></B>^idx rs) r
                          <B><FONT COLOR="#A020F0">end</FONT></B>
                   <B><FONT COLOR="#A020F0">in</FONT></B>
                      lp rs
                   <B><FONT COLOR="#A020F0">end</FONT></B>)
         <B><FONT COLOR="#A020F0">end</FONT></B>

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

      <B><FONT COLOR="#A020F0">fun</FONT></B> (IN _) --&gt; (IN _) = IN (const (false, <B><FONT COLOR="#BC8F8F">&quot;&lt;fn&gt;&quot;</FONT></B>))

      <I><FONT COLOR="#B22222">(* some built-in base types *)</FONT></I>
      <B><FONT COLOR="#A020F0">local</FONT></B>
         <B><FONT COLOR="#A020F0">fun</FONT></B> mk toS = (<B><FONT COLOR="#A020F0">fn</FONT></B> x =&gt; (false, x)) o toS o (<B><FONT COLOR="#A020F0">fn</FONT></B> (_, x) =&gt; x)
      <B><FONT COLOR="#A020F0">in</FONT></B>
         <B><FONT COLOR="#A020F0">val</FONT></B> string =
             IN (surround <B><FONT COLOR="#BC8F8F">&quot;\&quot;&quot;</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;\&quot;&quot;</FONT></B> o mk (String.translate Char.toString))
         <B><FONT COLOR="#A020F0">val</FONT></B> unit = IN (mk (<B><FONT COLOR="#A020F0">fn</FONT></B> () =&gt; <B><FONT COLOR="#BC8F8F">&quot;()&quot;</FONT></B>))
         <B><FONT COLOR="#A020F0">val</FONT></B> bool = IN (mk Bool.toString)
         <B><FONT COLOR="#A020F0">val</FONT></B> char = IN (surround <B><FONT COLOR="#BC8F8F">&quot;#\&quot;&quot;</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;\&quot;&quot;</FONT></B> o mk Char.toString)
         <B><FONT COLOR="#A020F0">val</FONT></B> int = IN (mk Int.toString)
         <B><FONT COLOR="#A020F0">val</FONT></B> word = IN (surround <B><FONT COLOR="#BC8F8F">&quot;0wx&quot;</FONT></B> <B><FONT COLOR="#BC8F8F">&quot;&quot;</FONT></B> o mk Word.toString)
         <B><FONT COLOR="#A020F0">val</FONT></B> real = IN (mk Real.toString)
      <B><FONT COLOR="#A020F0">end</FONT></B>
   <B><FONT COLOR="#A020F0">end</FONT></B>
<B><FONT COLOR="#0000FF">end</FONT></B>

<I><FONT COLOR="#B22222">(* Handlers for standard top-level exceptions *)</FONT></I>
<B><FONT COLOR="#A020F0">val</FONT></B> () = <B><FONT COLOR="#A020F0">let</FONT></B>
   <B><FONT COLOR="#0000FF">open</FONT></B> Show
   <B><FONT COLOR="#A020F0">fun</FONT></B> E0 name = SOME ((), C0 name)
<B><FONT COLOR="#A020F0">in</FONT></B>
   regExn (<B><FONT COLOR="#A020F0">fn</FONT></B> Bind =&gt; E0<B><FONT COLOR="#BC8F8F">&quot;Bind&quot;</FONT></B>
            | Chr =&gt; E0<B><FONT COLOR="#BC8F8F">&quot;Chr&quot;</FONT></B>
            | Div =&gt; E0<B><FONT COLOR="#BC8F8F">&quot;Div&quot;</FONT></B>
            | Domain =&gt; E0<B><FONT COLOR="#BC8F8F">&quot;Domain&quot;</FONT></B>
            | Empty =&gt; E0<B><FONT COLOR="#BC8F8F">&quot;Empty&quot;</FONT></B>
            | Match =&gt; E0<B><FONT COLOR="#BC8F8F">&quot;Match&quot;</FONT></B>
            | Option =&gt; E0<B><FONT COLOR="#BC8F8F">&quot;Option&quot;</FONT></B>
            | Overflow  =&gt; E0<B><FONT COLOR="#BC8F8F">&quot;Overflow&quot;</FONT></B>
            | Size =&gt; E0<B><FONT COLOR="#BC8F8F">&quot;Size&quot;</FONT></B>
            | Span =&gt; E0<B><FONT COLOR="#BC8F8F">&quot;Span&quot;</FONT></B>
            | Subscript =&gt; E0<B><FONT COLOR="#BC8F8F">&quot;Subscript&quot;</FONT></B>
            | _ =&gt; NONE)
 ; regExn (<B><FONT COLOR="#A020F0">fn</FONT></B> Fail s =&gt; SOME (s, C1<B><FONT COLOR="#BC8F8F">&quot;Fail&quot;</FONT></B> string)
            | _ =&gt; NONE)
<B><FONT COLOR="#A020F0">end</FONT></B>
</PRE>
<p>
 
</p>
</div>



<p>
<hr>
Last edited on 2007-08-26 02:10:40 by <span title="cs27019070.pp.htv.fi"><a href="VesaKarvonen">VesaKarvonen</a></span>.
</body></html>
