<!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>PolymorphicEquality - 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">

<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 20051202</a>
    <td style = "
		border: 0px;
		font-size: 150%;
		text-align: center;
		width: 50%;">
      PolymorphicEquality
    <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">
Polymorphic equality is a built-in function in  <a href="StandardML">Standard ML</a> that compares two values of the same type for equality.  It is specified as 
<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> = : ''a * ''a -&gt; bool
</PRE>
<p>
 
</p>
<p>
The <tt>''a</tt> in the specification are  <a href="EqualityTypeVariable">equality type variables</a>, and indicate that polymorphic equality can only be applied to values of an <a href="EqualityType">equality type</a>.  It is not allowed in SML to rebind <tt>=</tt>, so a programmer is guaranteed that <tt>=</tt> always denotes polymorphic equality. 
</p>
<p>
<ol>
<li>
<a href="#head-7a35baa02601d966253d4cd11022720d3c7c85b6">Equality of ground types</a></li>
<li>
<a href="#head-0f3dde7404010ce2eb8acb56157248e74f28e7ac">Equality of reals</a></li>
<li>
<a href="#head-16f64cbf7b943934e1a83e690a579efe57383553">Equality of functions</a></li>
<li>
<a href="#head-57e4aca10e4c8110f7108e37c8e03ee3ceab6eb0">Equality of immutable types</a></li>
<li>
<a href="#head-2b8052d0b12eb9178fa4e234e2ec6108e811460f">Equality of mutable values</a></li>
<li>
<a href="#head-2d02f1ba2142e6814e7dcdf979c4523b6443b90c">Equality of datatypes</a></li>
<li>
<a href="#head-8781d615fd77be9578225c40ac67b9471394cced">Implementation</a></li>
<li>
<a href="#head-a4bc8bf5caf54b18cea9f58e83dd4acb488deb17">Also see</a></li>
</ol>

 
</p>
<h2 id="head-7a35baa02601d966253d4cd11022720d3c7c85b6">Equality of ground types</h2>
<p>
Ground types like <tt>char</tt>, <tt>int</tt>, and <tt>word</tt> may be compared (to values of the same type).  For example, <tt>13&nbsp;=&nbsp;14</tt> is type correct and yields <tt>false</tt>. 
</p>
<h2 id="head-0f3dde7404010ce2eb8acb56157248e74f28e7ac">Equality of reals</h2>
<p>
The one ground type that can not be compared is <tt>real</tt>.  So, <tt>13.0&nbsp;=&nbsp;14.0</tt> is not type correct.  One can use <tt>Real.==</tt> to compare reals for equality, but beware that this has different algebraic properties than polymorphic equality. 
</p>
<p>
See <a class="external" href="http://mlton.org/basis/real.html"><img src="moin-www.png" alt="[WWW]" height="11" width="11">http://mlton.org/basis/real.html</a> for a discussion of why <tt>real</tt> is not an equality type. 
</p>
<h2 id="head-16f64cbf7b943934e1a83e690a579efe57383553">Equality of functions</h2>
<p>
Comparison of functions is not allowed. 
</p>
<h2 id="head-57e4aca10e4c8110f7108e37c8e03ee3ceab6eb0">Equality of immutable types</h2>
<p>
Polymorphic equality can be used on <a href="Immutable">immutable</a> values like tuples, records, lists, and vectors.  For example, 
</p>

<pre>(1, 2, 3) = (4, 5, 6)
</pre><p>
is a type-correct expression yielding <tt>false</tt>, while 
</p>

<pre>[1, 2, 3] = [1, 2, 3]
</pre><p>
is type correct and yields <tt>true</tt>. 
</p>
<p>
Equality on immutable values is computed by structure, which means that values are compared by recursively descending the data structure until ground types are reached, at which point the ground types are compared with primitive equality tests (like comparison of characters).  So, the expression 
</p>

<pre>[1, 2, 3] = [1, 1 + 1, 1 + 1 + 1]
</pre><p>
is guaranteed to yield <tt>true</tt>, even though the lists may occupy different locations in memory. 
</p>
<p>
Because of structural equality, immutable values can only be compared if their components can be compared.  For example, <tt>[1,&nbsp;2,&nbsp;3]</tt> can be compared, but <tt>[1.0,&nbsp;2.0,&nbsp;3.0]</tt> can not.  The SML type system uses <a href="EqualityType">equality types</a> to ensure that structural equality is only applied to valid values. 
</p>
<h2 id="head-2b8052d0b12eb9178fa4e234e2ec6108e811460f">Equality of mutable values</h2>
<p>
In contrast to immutable values, polymorphic equality of <a href="Mutable">mutable</a> values (like ref cells and arrays) is performed by pointer comparison, not by structure.  So, the expression 
</p>

<pre>ref 13 = ref 13
</pre><p>
is guaranteed to yield <tt>false</tt>, even though the ref cells hold the same contents. 
</p>
<p>
Because equality of mutable values is not structural, arrays and refs can be compared <em>even if their components are not equality types</em>. Hence, the following expression is type correct (and yields true). 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">let</FONT></B>
   <B><FONT COLOR="#A020F0">val</FONT></B> r = ref <B><FONT COLOR="#5F9EA0">13.0</FONT></B>
<B><FONT COLOR="#A020F0">in</FONT></B>
   r = r
<B><FONT COLOR="#A020F0">end</FONT></B>
</PRE>
<p>
 
</p>
<h2 id="head-2d02f1ba2142e6814e7dcdf979c4523b6443b90c">Equality of datatypes</h2>
<p>
Polymorphic equality of datatypes is structural.  Two values of the same datatype are equal if they are of the same variant and if the variant's arguments are equal (recursively).  So, with the datatype 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">datatype</FONT></B><FONT COLOR="#228B22"><B> t </FONT></B>=<FONT COLOR="#228B22"><B> <FONT COLOR="#B8860B">A</FONT> </FONT></B>|<FONT COLOR="#228B22"><B> <FONT COLOR="#B8860B">B</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> t
</FONT></B></PRE>
<p>
 
</p>
<p>
then <tt>B&nbsp;(B&nbsp;A)&nbsp;=&nbsp;B&nbsp;A</tt> is type correct and yields <tt>false</tt>, while <tt>A&nbsp;=&nbsp;A</tt> and <tt>B&nbsp;A&nbsp;=&nbsp;B&nbsp;A</tt> yield <tt>true</tt>. 
</p>
<p>
As polymorphic equality descends two values to compare them, it uses pointer equality whenever it reaches a mutable value.  So, with the datatype  
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">datatype</FONT></B><FONT COLOR="#228B22"><B> t </FONT></B>=<FONT COLOR="#228B22"><B> <FONT COLOR="#B8860B">A</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> int ref </FONT></B>|<FONT COLOR="#228B22"><B> </FONT></B>...
</PRE>
<p>
 
</p>
<p>
then <tt>A&nbsp;(ref&nbsp;13)&nbsp;=&nbsp;A&nbsp;(ref&nbsp;13)</tt> is type correct and yields <tt>false</tt>, because the pointer equality on the two ref cells yields <tt>false</tt>. 
</p>
<p>
One weakness of the SML type system is that datatypes do not inherit the special property of the <tt>ref</tt> and <tt>array</tt> type constructors that allows them to be compared regardless of their component type.  For example, after declaring 
<pre class=code>
<B><FONT COLOR="#A020F0">datatype</FONT></B><FONT COLOR="#228B22"><B> 'a t </FONT></B>=<FONT COLOR="#228B22"><B> <FONT COLOR="#B8860B">A</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> 'a ref
</FONT></B></PRE>
 one might expect to be able to compare two values of type  <tt>real&nbsp;t</tt>, because pointer comparison on a ref cell would suffice. Unfortunately, the type system can only express that a user-defined datatype <a href="AdmitsEquality">admits equality</a> or not.  In this case, <tt>t</tt> admits equality, which means that <tt>int&nbsp;t</tt> can be compared but that <tt>real&nbsp;t</tt> can not.  We can confirm this with the program 
<pre class=code>
<B><FONT COLOR="#A020F0">datatype</FONT></B><FONT COLOR="#228B22"><B> 'a t </FONT></B>=<FONT COLOR="#228B22"><B> <FONT COLOR="#B8860B">A</FONT> <B><FONT COLOR="#A020F0">of</FONT></B> 'a ref
</FONT></B><B><FONT COLOR="#A020F0">fun</FONT></B> f (x: real t, y: real t) = x = y
</PRE>
 on which MLton reports the following error. 
<pre>Error: z.sml 2.34.
  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>
</p>
<h2 id="head-8781d615fd77be9578225c40ac67b9471394cced">Implementation</h2>
<p>
Polymorphic equality is implemented by recursively descending the two values being compared, stopping as soon as they are determined to be unequal, or exploring the entire values to determine that they are equal.  Hence, polymorphic equality can take time proportional to the size of the smaller value. 
</p>
<p>
MLton uses some optimizations to improve performance.   
</p>

    <ul>

    <li>
<p>
 When computing structural equality, first do a pointer comparison.  If the comparison yields <tt>true</tt>, then stop and return <tt>true</tt>,  since the structural comparison is guaranteed to do so.  If the  pointer comparison fails, then recursively descend the values. 
</p>
</li>
    <li class="gap">
<p>
 If a datatype is an enum (e.g. <tt>datatype&nbsp;t&nbsp;=&nbsp;A&nbsp;|&nbsp;B&nbsp;|&nbsp;C</tt>),  then a single comparison suffices to compare values of the datatype.  No case dispatch is required to determine whether the two values are  of the same variant. 
</p>
</li>
    <li class="gap">
<p>
 When comparing a known constant non-value-carrying variant, use a  single comparison.  For example, the following code will compile into  a single comparison for <tt>A&nbsp;=&nbsp;x</tt>. 
<pre> datatype t = A | B | C of ...
 ... if A = x then ...</pre>
</p>
</li>
    <li class="gap">
<p>
 When comparing a small constant <tt>IntInf.int</tt> to another <tt>IntInf.int</tt>, use a  single comparison against the constant.  No case dispatch is  required.  
</p>
</li>

    </ul>


<h2 id="head-a4bc8bf5caf54b18cea9f58e83dd4acb488deb17">Also see</h2>

    <ul>

    <li>
<p>
 <a href="AdmitsEquality">AdmitsEquality</a> 
</p>
</li>
    <li>
<p>
 <a href="EqualityType">EqualityType</a> 
</p>
</li>
    <li>
<p>
 <a href="EqualityTypeVariable">EqualityTypeVariable</a> 
</p>
</li>
</ul>

</div>



<p>
<hr>
Last edited on 2005-12-01 23:31:02 by <span title="ppp-71-139-183-221.dsl.snfc21.pacbell.net"><a href="StephenWeeks">StephenWeeks</a></span>.
</body></html>
