<!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>GenerativeDatatype</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>GenerativeDatatype</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>In <a href="StandardML">Standard ML</a>, datatype declarations are said to be
<em>generative</em>, because each time a datatype declaration is evaluated,
it yields a new type.  Thus, any attempt to mix the types will lead to
a type error at compile-time.  The following program, which does not
type check, demonstrates this.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">functor</span><span class="w"> </span><span class="n">F</span><span class="w"> </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">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="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="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="w"></span>
<span class="k">val</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">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">S2</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">x</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Generativity also means that two different datatype declarations
define different types, even if they define identical constructors.
The following program does not type check due to this.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><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="n">a1</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">A</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="n">a2</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">A</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="k">if</span><span class="w"> </span><span class="n">true</span><span class="w"> </span><span class="k">then</span><span class="w"> </span><span class="n">a1</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="n">a2</span><span class="w"></span>
</pre></div></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_also_see">Also see</h2>
<div class="sectionbody">
<div class="ulist"><ul>
<li>
<p>
<a href="GenerativeException">GenerativeException</a>
</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>
