<!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>DefineTypeBeforeUse</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>DefineTypeBeforeUse</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p><a href="StandardML">Standard ML</a> requires types to be defined before they are
used.  Because of type inference, the use of a type can be implicit;
hence, this requirement is more subtle than it might appear.  For
example, the following program is not type correct, because the type
of <span class="monospaced">r</span> is <span class="monospaced">t option ref</span>, but <span class="monospaced">t</span> is defined after <span class="monospaced">r</span>.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">val</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="n">NONE</span><span class="w"></span>
<span class="k">datatype</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">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">r</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="n">SOME</span><span class="w"> </span><span class="n">A</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>MLton reports the following error, indicating that the type defined on
line 2 is used on line 1.</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>Error: z.sml 1.1.
  Type escapes the scope of its definition at z.sml 2.10.
    type: t
    in: val r = ref NONE</pre>
</div></div>
<div class="paragraph"><p>While the above example is benign, the following example shows how to
cast an integer to a function by (implicitly) using a type before it
is defined.  In the example, the ref cell <span class="monospaced">r</span> is of type
<span class="monospaced">t option ref</span>, where <span class="monospaced">t</span> is defined <em>after</em> <span class="monospaced">r</span>, as a parameter to
functor <span class="monospaced">F</span>.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">val</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">ref</span><span class="w"> </span><span class="n">NONE</span><span class="w"></span>
<span class="k">functor</span><span class="w"> </span><span class="n">F</span><span class="w"> </span><span class="p">(</span><span class="k">type</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">           </span><span class="k">val</span><span class="w"> </span><span class="n">x</span><span class="p">:</span><span class="w"> </span><span class="n">t</span><span class="p">)</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">r</span><span class="w"> </span><span class="n">:=</span><span class="w"> </span><span class="n">SOME</span><span class="w"> </span><span class="n">x</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">get</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">valOf</span><span class="w"> </span><span class="p">(</span><span class="n">!r</span><span class="p">)</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>
<span class="k">structure</span><span class="w"> </span><span class="n">S1</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">F</span><span class="w"> </span><span class="p">(</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="n">unit</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">val</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">())</span><span class="w"></span>
<span class="k">structure</span><span class="w"> </span><span class="n">S2</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">F</span><span class="w"> </span><span class="p">(</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="n">int</span><span class="w"></span>
<span class="w">                  </span><span class="k">val</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">13</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">S1</span><span class="p">.</span><span class="n">get</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>MLton reports the following error.</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>Warning: z.sml 1.1.
  Unable to locally determine type of variable: r.
    type: ??? option ref
    in: val r = ref NONE
Error: z.sml 1.1.
  Type escapes the scope of its definition at z.sml 2.17.
    type: t
    in: val r = ref NONE</pre>
</div></div>
<div class="paragraph"><p><a href="PolyML">PolyML</a> 4.1.3 to seg faults, but <a href="PolyML">PolyML</a> 5.4 reports
the following error.</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>Warning- in 'z.sml', line 13.
The type of (r) contains a free type variable. Setting it to a unique
   monotype.
Error- in 'z.sml', line 5.
Type error in function application.
   Function: := : _a option ref * _a option -&gt; unit
   Argument: (r, SOME x) : _a option ref * t option
   Reason:
      Can't unify _a (*Constructed from a free type variable.*) with t
      (Different type constructors)
Found near r := SOME x
Error- in 'z.sml', line 12.
Type error in function application.
   Function: S1.get () : _a
   Argument: () : unit
   Reason: Value being applied does not have a function type
Found near S1.get () ()</pre>
</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>
