<!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>EqualityType</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>EqualityType</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>An equality type is a type to which <a href="PolymorphicEquality">PolymorphicEquality</a> can be
applied.  The <a href="DefinitionOfStandardML">Definition</a> and the
<a href="BasisLibrary">Basis Library</a> precisely spell out which types are
equality types.</p></div>
<div class="ulist"><ul>
<li>
<p>
<span class="monospaced">bool</span>, <span class="monospaced">char</span>, <span class="monospaced">IntInf.int</span>, <span class="monospaced">Int<em>&lt;N&gt;</em>.int</span>, <span class="monospaced">string</span>, and <span class="monospaced">Word<em>&lt;N&gt;</em>.word</span> are equality types.
</p>
</li>
<li>
<p>
for any <span class="monospaced">t</span>, both <span class="monospaced">t array</span> and <span class="monospaced">t ref</span> are equality types.
</p>
</li>
<li>
<p>
if <span class="monospaced">t</span> is an equality type, then <span class="monospaced">t list</span>, and <span class="monospaced">t vector</span> are equality types.
</p>
</li>
<li>
<p>
if <span class="monospaced">t1</span>, &#8230;, <span class="monospaced">tn</span> are equality types, then <span class="monospaced">t1 * ... * tn</span> and <span class="monospaced">{l1: t1, ..., ln: tn}</span> are equality types.
</p>
</li>
<li>
<p>
if <span class="monospaced">t1</span>, &#8230;, <span class="monospaced">tn</span> are equality types and <span class="monospaced">t</span> <a href="AdmitsEquality">AdmitsEquality</a>, then <span class="monospaced">(t1, ..., tn) t</span> is an equality type.
</p>
</li>
</ul></div>
<div class="paragraph"><p>To check that a type t is an equality type, use the following idiom.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">structure</span><span class="w"> </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">eqtype</span><span class="w"> </span><span class="n">t</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">type</span><span class="w"> </span><span class="n">t</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="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Notably, <span class="monospaced">exn</span> and <span class="monospaced">real</span> are not equality types.  Neither is <span class="monospaced">t1 -&gt; t2</span>, for any <span class="monospaced">t1</span> and <span class="monospaced">t2</span>.</p></div>
<div class="paragraph"><p>Equality on arrays and ref cells is by identity, not structure.
For example, <span class="monospaced">ref 13 = ref 13</span> is <span class="monospaced">false</span>.
On the other hand, equality for lists, strings, and vectors is by
structure, not identity.  For example, the following equalities hold.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><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="p">[</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="n">::</span><span class="w"> </span><span class="p">[</span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">]</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="s">&quot;foo&quot;</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">concat</span><span class="w"> </span><span class="p">[</span><span class="s">&quot;f&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;o&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;o&quot;</span><span class="p">]</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">Vector</span><span class="p">.</span><span class="n">fromList</span><span class="w"> </span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Vector</span><span class="p">.</span><span class="n">tabulate</span><span class="w"> </span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="mi">1</span><span class="p">)</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>
