Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > d07d7ab417d79053e7e0155c99e1a1c8 > files > 2487

mlton-20100608-3.fc15.i686.rpm

<!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>TypeChecking - 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%;">
      TypeChecking
    <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 = "TitleIndex">Index</a>
      &nbsp;
</table>
<div id="content" lang="en" dir="ltr">
MLton's type checker follows the <a href="DefinitionOfStandardML">Definition</a> closely, so you may find differences between MLton and other SML compilers that do not follow the Definition so closely.  In particular, SML/NJ has many deviations from the Definition -- please see <a href="SMLNJDeviations">SMLNJDeviations</a> for those that we are aware of. <p>
In some respects MLton's type checker is more powerful than other SML compilers, so there are programs that MLton accepts that are rejected by some other SML compilers.  These kinds of programs fall into a few simple categories. 
</p>

    <ul>

    <li>
<p>
 MLton resolves flexible record patterns using a larger context than  many other SML compilers.  For example, MLton accepts the  following. 
<pre class=code>
<B><FONT COLOR="#A020F0">fun</FONT></B> f {x, ...} = x
<B><FONT COLOR="#A020F0">val</FONT></B> _ = f {x = <B><FONT COLOR="#5F9EA0">13</FONT></B>, y = <B><FONT COLOR="#BC8F8F">&quot;foo&quot;</FONT></B>}
</PRE>
 
</p>
</li>
    <li class="gap">
<p>
 MLton uses as large a context as possible to resolve the type of  variables constrained by the value restriction to be monotypes.  For  example, MLton accepts the following. 
<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> S:
   <B><FONT COLOR="#0000FF">sig</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> f: int -&gt; int
   <B><FONT COLOR="#0000FF">end</FONT></B> =
   <B><FONT COLOR="#0000FF">struct</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> f = (<B><FONT COLOR="#A020F0">fn</FONT></B> x =&gt; x) (<B><FONT COLOR="#A020F0">fn</FONT></B> y =&gt; y)
   <B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
 
</p>
</li>

    </ul>


<h2 id="head-05f93ee06dde2d7fa78009c59b64a585bd6ab4ca">Type error messages</h2>
<p>
To aid in the understanding of type errors, MLton's type checker displays type errors differently than other SML compilers.  In particular, when two types are different, it is important for the programmer to easily understand why they are different.  So, MLton displays only the differences between two types that don't match, using underscores for the parts that match.  For example, if a function expects <tt>real&nbsp;*&nbsp;int</tt> but gets <tt>real&nbsp;*&nbsp;real</tt>, the type error message would look like 
</p>

<pre>expects: _ * [int]
but got: _ * [real]
</pre><p>
As another aid to spotting differences, MLton places brackets <tt>[]</tt> around the parts of the types that don't match.  A common situation is when a function receives a different number of arguments than it expects, in which case you might see an error like 
</p>

<pre>expects: [int * real]
but got: [int * real * string]
</pre><p>
The brackets make it easy to see that the problem is that the tuples have different numbers of components -- not that the components don't match.  Contrast that with a case where a function receives the right number of arguments, but in the wrong order. 
</p>

<pre>expects: [int] * [real]
but got: [real] * [int]
</pre><p>
Here the brackets make it easy to see that the components do not match. 
</p>
<p>
We appreciate feedback on any type error messages that you find confusing, or suggestions you may have for improvements to error messages. 
</p>
<h2 id="head-e849875c2946f3612529e7b5c9eeeb137490f29b">The shortest/most-recent rule for type names</h2>
<p>
In a type error message, MLton often has a number of choices in deciding what name to use for a type.  For example, in the following type-incorrect program 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> int
</FONT></B><B><FONT COLOR="#A020F0">fun</FONT></B> f (x: t) = x
<B><FONT COLOR="#A020F0">val</FONT></B> _ = f <B><FONT COLOR="#BC8F8F">&quot;foo&quot;</FONT></B>
</PRE>
<p>
 
</p>
<p>
MLton reports 
</p>

<pre>Error: z.sml 3.9.
  Function applied to incorrect argument.
    expects: [t]
    but got: [string]
    in: f "foo"
</pre><p>
MLton could have reported <tt>expects:&nbsp;[int]</tt> instead of  <tt>expects:&nbsp;[t]</tt>.  However, MLton uses the shortest/most-recent rule in order to decide what type name to display.  This rule means that, at the point of the error, MLton first looks for the shortest name for a type in terms of number of structure identifiers (e.g. <tt>foobar</tt> is shorter than <tt>A.t</tt>).  Next, if there are multiple names of the same length, then MLton uses the most recently defined name.  It is this tiebreaker that causes MLton to prefer <tt>t</tt> to <tt>int</tt> in the above example. 
</p>
<p>
In signature matching, most recently defined is taken to include all of the definitions introduced by the structure.  For example 
</p>

<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> S:
   <B><FONT COLOR="#0000FF">sig</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> x: int
   <B><FONT COLOR="#0000FF">end</FONT></B> =
   <B><FONT COLOR="#0000FF">struct</FONT></B>
      <B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> int
      </FONT></B><B><FONT COLOR="#A020F0">val</FONT></B> x = <B><FONT COLOR="#BC8F8F">&quot;foo&quot;</FONT></B>
   <B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
 
</p>
<p>
MLton reports the error message 
</p>

<pre>Error: z.sml 2.4.
  Variable type in structure disagrees with signature.
    variable: x
    structure: [string]
    signature: [t]
</pre><p>
in which the <tt>[t]</tt> refers to the type defined in the structure, since that is more recent than the definition of <tt>int</tt>. 
</p>
<p>
In signatures with type equations, this can be somewhat confusing. For example. 
</p>

<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> S:
   <B><FONT COLOR="#0000FF">sig</FONT></B>
      <B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> t
      </FONT></B><B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> u </FONT></B>=<B><FONT COLOR="#228B22"> t
   </FONT></B><B><FONT COLOR="#0000FF">end</FONT></B> =
   <B><FONT COLOR="#0000FF">struct</FONT></B>
      <B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> t </FONT></B>=<B><FONT COLOR="#228B22"> int
      </FONT></B><B><FONT COLOR="#A020F0">type</FONT></B><B><FONT COLOR="#228B22"> u </FONT></B>=<B><FONT COLOR="#228B22"> char
   </FONT></B><B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
 
</p>
<p>
MLton reports the error 
</p>

<pre>Error: z.sml 2.4.
  Type definition in structure disagrees with signature.
    type: u
    structure: [u]
    signature: [t]
</pre><p>
This error reflects the fact that the signature requires type <tt>u</tt> to equal <tt>t</tt>, but that in the structure, <tt>u</tt> is defined to be <tt>char</tt>, whose most-recent name is <tt>u</tt>, while the signature requires <tt>u</tt> to be <tt>int</tt>, whose most-recent name is <tt>t</tt>. 
</p>
</div>



<p>
<hr>
Last edited on 2007-08-15 22:07:31 by <span title="fenrir.uchicago.edu"><a href="MatthewFluet">MatthewFluet</a></span>.
</body></html>