1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
<!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">
<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 20061025</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>
<a href = "Index">Index</a>
</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> <B><FONT COLOR="#5F9EA0">=</FONT></B> : ''a <B><FONT COLOR="#5F9EA0">*</FONT></B> ''a <B><FONT COLOR="#5F9EA0">-</FONT></B><B><FONT COLOR="#5F9EA0">></FONT></B> <B><FONT COLOR="#228B22">bool</FONT></B>
</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 = 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 = 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, 2, 3]</tt> can be compared, but <tt>[1.0, 2.0, 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> t <B><FONT COLOR="#5F9EA0">=</FONT></B> A <B><FONT COLOR="#5F9EA0">|</FONT></B> B <B><FONT COLOR="#A020F0">of</FONT></B> t
</PRE>
<p>
</p>
<p>
then <tt>B (B A) = B A</tt> is type correct and yields <tt>false</tt>, while <tt>A = A</tt> and <tt>B A = B 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> t <B><FONT COLOR="#5F9EA0">=</FONT></B> A <B><FONT COLOR="#A020F0">of</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> <B><FONT COLOR="#A020F0">ref</FONT></B> <B><FONT COLOR="#5F9EA0">|</FONT></B> ...
</PRE>
<p>
</p>
<p>
then <tt>A (ref 13) = A (ref 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> 'a t <B><FONT COLOR="#5F9EA0">=</FONT></B> A <B><FONT COLOR="#A020F0">of</FONT></B> 'a <B><FONT COLOR="#A020F0">ref</FONT></B>
</PRE>
one might expect to be able to compare two values of type <tt>real 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 t</tt> can be compared but that <tt>real t</tt> can not. We can confirm this with the program
<pre class=code>
<B><FONT COLOR="#A020F0">datatype</FONT></B> 'a t <B><FONT COLOR="#5F9EA0">=</FONT></B> A <B><FONT COLOR="#A020F0">of</FONT></B> 'a <B><FONT COLOR="#A020F0">ref</FONT></B>
<B><FONT COLOR="#A020F0">fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">f</FONT></I></B></FONT></B> (x: <B><FONT COLOR="#228B22">real</FONT></B> t, y: <B><FONT COLOR="#228B22">real</FONT></B> t) <B><FONT COLOR="#5F9EA0">=</FONT></B> x <B><FONT COLOR="#5F9EA0">=</FONT></B> y
</PRE>
on which MLton reports the following error.
<pre>Error: z.sml 2.34.
Function applied to incorrect argument.
expects: [<equality>] * [<equality>]
but got: [<non-equality>] * [<non-equality>]
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 t = A | B | 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 = 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>
|