<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.10">
<title>TypeConstructor</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 20201002</a>
</div>
</div>
<div id="header">
<h1>TypeConstructor</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>In <a href="StandardML">Standard ML</a>, a type constructor is a function from
types to types.  Type constructors can be <em>nullary</em>, meaning that
they take no arguments, as in <span class="monospaced">char</span>, <span class="monospaced">int</span>, and <span class="monospaced">real</span>.
Type constructors can be <em>unary</em>, meaning that they take one
argument, as in <span class="monospaced">array</span>, <span class="monospaced">list</span>, and <span class="monospaced">vector</span>.  A program
can define a new type constructor in two ways: a <span class="monospaced">type</span> definition
or a <span class="monospaced">datatype</span> declaration.  User-defined type constructors can
can take any number of arguments.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></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">T</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">int</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">real</span><span class="w">            </span><span class="cm">(* 0 arguments *)</span><span class="w"></span>
<span class="k">type</span><span class="w"> </span><span class="n">&#39;a</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">&#39;a</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">int</span><span class="w">                    </span><span class="cm">(* 1 argument *)</span><span class="w"></span>
<span class="k">datatype</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">)</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">of</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;b</span><span class="w">  </span><span class="cm">(* 2 arguments *)</span><span class="w"></span>
<span class="k">type</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;c</span><span class="p">)</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">&#39;a</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;b</span><span class="w">  </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;c</span><span class="p">)</span><span class="w">  </span><span class="cm">(* 3 arguments *)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Here are the syntax rules for type constructor application.</p></div>
<div class="ulist"><ul>
<li>
<p>
Type constructor application is written in postfix.  So, one writes
 <span class="monospaced">int list</span>, not <span class="monospaced">list int</span>.
</p>
</li>
<li>
<p>
Unary type constructors drop the parens, so one writes
 <span class="monospaced">int list</span>, not <span class="monospaced">(int) list</span>.
</p>
</li>
<li>
<p>
Nullary type constructors drop the argument entirely, so one writes
 <span class="monospaced">int</span>, not <span class="monospaced">() int</span>.
</p>
</li>
<li>
<p>
N-ary type constructors use tuple notation; for example,
 <span class="monospaced">(int, real) t</span>.
</p>
</li>
<li>
<p>
Type constructor application associates to the left.  So,
 <span class="monospaced">int ref list</span> is the same as <span class="monospaced">(int ref) list</span>.
</p>
</li>
</ul></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>
