<!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>EqualityTypeVariable</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>EqualityTypeVariable</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>An equality type variable is a type variable that starts with two or
more primes, as in <span class="monospaced">''a</span> or <span class="monospaced">''b</span>.  The canonical use of equality type
variables is in specifying the type of the <a href="PolymorphicEquality">PolymorphicEquality</a>
function, which is <span class="monospaced">''a * ''a -&gt; bool</span>.  Equality type variables
ensure that polymorphic equality is only used on
<a href="EqualityType">equality types</a>, by requiring that at every use of a
polymorphic value, equality type variables are instantiated by
equality types.</p></div>
<div class="paragraph"><p>For example, the following program is type correct because polymorphic
equality is applied to variables of type <span class="monospaced">''a</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">f</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">&#39;&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">y</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="n">bool</span><span class="w"> </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="n">y</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>On the other hand, the following program is not type correct, because
polymorphic equality is applied to variables of type <span class="monospaced">'a</span>, which is
not an equality type.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">fun</span><span class="w"> </span><span class="n">f</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">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="p">):</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">x</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">y</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>MLton reports the following error, indicating that polymorphic
equality expects equality types, but didn&#8217;t get them.</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>Error: z.sml 1.32.
  Function applied to incorrect argument.
    expects: [&lt;equality&gt;] * [&lt;equality&gt;]
    but got: [&lt;non-equality&gt;] * [&lt;non-equality&gt;]
    in: = (x, y)</pre>
</div></div>
<div class="paragraph"><p>As an example of using such a function that requires equality types,
suppose that <span class="monospaced">f</span> has polymorphic type <span class="monospaced">''a -&gt; unit</span>.  Then, <span class="monospaced">f 13</span> is
type correct because <span class="monospaced">int</span> is an equality type.  On the other hand,
<span class="monospaced">f 13.0</span> and <span class="monospaced">f (fn x =&gt; x)</span> are not type correct, because <span class="monospaced">real</span> and
arrow types are not equality types.  We can test these facts with the
following short programs.  First, we verify that such an <span class="monospaced">f</span> can be
applied to integers.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">functor</span><span class="w"> </span><span class="n">Ok</span><span class="w"> </span><span class="p">(</span><span class="k">val</span><span class="w"> </span><span class="n">f</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;&#39;a</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="p">):</span><span class="w"> </span><span class="k">sig</span><span class="w"> </span><span class="k">end</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">struct</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">f</span><span class="w"> </span><span class="mi">13</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">f</span><span class="w"> </span><span class="mi">14</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>We can do better, and verify that such an <span class="monospaced">f</span> can be applied to
any integer.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">functor</span><span class="w"> </span><span class="n">Ok</span><span class="w"> </span><span class="p">(</span><span class="k">val</span><span class="w"> </span><span class="n">f</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;&#39;a</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="p">):</span><span class="w"> </span><span class="k">sig</span><span class="w"> </span><span class="k">end</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">struct</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">g</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">int</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="n">x</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Even better, we don&#8217;t need to introduce a dummy function name; we can
use a type constraint.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">functor</span><span class="w"> </span><span class="n">Ok</span><span class="w"> </span><span class="p">(</span><span class="k">val</span><span class="w"> </span><span class="n">f</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;&#39;a</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="p">):</span><span class="w"> </span><span class="k">sig</span><span class="w"> </span><span class="k">end</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">struct</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">f</span><span class="p">:</span><span class="w"> </span><span class="n">int</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="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Even better, we can use a signature constraint.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">functor</span><span class="w"> </span><span class="n">Ok</span><span class="w"> </span><span class="p">(</span><span class="n">S</span><span class="p">:</span><span class="w"> </span><span class="k">sig</span><span class="w"> </span><span class="k">val</span><span class="w"> </span><span class="n">f</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;&#39;a</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="k">end</span><span class="p">):</span><span class="w"></span>
<span class="w">   </span><span class="k">sig</span><span class="w"> </span><span class="k">val</span><span class="w"> </span><span class="n">f</span><span class="p">:</span><span class="w"> </span><span class="n">int</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="k">end</span><span class="w"> </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>This functor concisely verifies that a function of polymorphic type
<span class="monospaced">''a -&gt; unit</span> can be safely used as a function of type <span class="monospaced">int -&gt; unit</span>.</p></div>
<div class="paragraph"><p>As above, we can verify that such an <span class="monospaced">f</span> can not be used at non
equality types.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">functor</span><span class="w"> </span><span class="n">Bad</span><span class="w"> </span><span class="p">(</span><span class="n">S</span><span class="p">:</span><span class="w"> </span><span class="k">sig</span><span class="w"> </span><span class="k">val</span><span class="w"> </span><span class="n">f</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;&#39;a</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="k">end</span><span class="p">):</span><span class="w"></span>
<span class="w">   </span><span class="k">sig</span><span class="w"> </span><span class="k">val</span><span class="w"> </span><span class="n">f</span><span class="p">:</span><span class="w"> </span><span class="n">real</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="k">end</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">S</span><span class="w"></span>
</pre></div></div></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">functor</span><span class="w"> </span><span class="n">Bad</span><span class="w"> </span><span class="p">(</span><span class="n">S</span><span class="p">:</span><span class="w"> </span><span class="k">sig</span><span class="w"> </span><span class="k">val</span><span class="w"> </span><span class="n">f</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;&#39;a</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="k">end</span><span class="p">):</span><span class="w"></span>
<span class="w">   </span><span class="k">sig</span><span class="w"> </span><span class="k">val</span><span class="w"> </span><span class="n">f</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;a</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="k">end</span><span class="w"> </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>For each of these programs, MLton reports the following error.</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>Error: z.sml 2.4.
  Variable type in structure disagrees with signature.
    variable: f
    structure: [&lt;equality&gt;] -&gt; _
    signature: [&lt;non-equality&gt;] -&gt; _</pre>
</div></div>
</div>
</div>
<div class="sect1">
<h2 id="_equality_type_variables_in_type_and_datatype_declarations">Equality type variables in type and datatype declarations</h2>
<div class="sectionbody">
<div class="paragraph"><p>Equality type variables can be used in type and datatype declarations;
however they play no special role.  For example,</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><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="p">=</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">int</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>is completely identical to</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">type</span><span class="w"> </span><span class="n">&#39;&#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">&#39;&#39;a</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">int</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>In particular, such a definition does <em>not</em> require that <span class="monospaced">t</span> only be
applied to equality types.</p></div>
<div class="paragraph"><p>Similarly,</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">t</span><span class="w"> </span><span class="p">=</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">B</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>is completely identical to</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;&#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">A</span><span class="w"> </span><span class="p">|</span><span class="w"> </span><span class="n">B</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>
</pre></div></div></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>
