<!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>GenerativeException</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>GenerativeException</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>In <a href="StandardML">Standard ML</a>, exception declarations are said to be
<em>generative</em>, because each time an exception declaration is evaluated,
it yields a new exception.</p></div>
<div class="paragraph"><p>The following program demonstrates the generativity of exceptions.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">exception</span><span class="w"> </span><span class="n">E</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">e1</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">E</span><span class="w"></span>
<span class="k">fun</span><span class="w"> </span><span class="n">isE1</span><span class="w"> </span><span class="p">(</span><span class="n">e</span><span class="p">:</span><span class="w"> </span><span class="n">exn</span><span class="p">):</span><span class="w"> </span><span class="n">bool</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">case</span><span class="w"> </span><span class="n">e</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">      </span><span class="n">E</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">true</span><span class="w"></span>
<span class="w">    </span><span class="p">|</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">false</span><span class="w"></span>
<span class="k">exception</span><span class="w"> </span><span class="n">E</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">e2</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">E</span><span class="w"></span>
<span class="k">fun</span><span class="w"> </span><span class="n">isE2</span><span class="w"> </span><span class="p">(</span><span class="n">e</span><span class="p">:</span><span class="w"> </span><span class="n">exn</span><span class="p">):</span><span class="w"> </span><span class="n">bool</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">case</span><span class="w"> </span><span class="n">e</span><span class="w"> </span><span class="k">of</span><span class="w"></span>
<span class="w">      </span><span class="n">E</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">true</span><span class="w"></span>
<span class="w">    </span><span class="p">|</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">false</span><span class="w"></span>
<span class="k">fun</span><span class="w"> </span><span class="n">pb</span><span class="w"> </span><span class="p">(</span><span class="n">b</span><span class="p">:</span><span class="w"> </span><span class="n">bool</span><span class="p">):</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="n">print</span><span class="w"> </span><span class="p">(</span><span class="n">concat</span><span class="w"> </span><span class="p">[</span><span class="n">Bool</span><span class="p">.</span><span class="n">toString</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</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="p">(</span><span class="n">pb</span><span class="w"> </span><span class="p">(</span><span class="n">isE1</span><span class="w"> </span><span class="n">e1</span><span class="p">)</span><span class="w"></span>
<span class="w">          </span><span class="p">;</span><span class="n">pb</span><span class="w"> </span><span class="p">(</span><span class="n">isE1</span><span class="w"> </span><span class="n">e2</span><span class="p">)</span><span class="w"></span>
<span class="w">          </span><span class="p">;</span><span class="w"> </span><span class="n">pb</span><span class="w"> </span><span class="p">(</span><span class="n">isE2</span><span class="w"> </span><span class="n">e1</span><span class="p">)</span><span class="w"></span>
<span class="w">          </span><span class="p">;</span><span class="w"> </span><span class="n">pb</span><span class="w"> </span><span class="p">(</span><span class="n">isE2</span><span class="w"> </span><span class="n">e2</span><span class="p">))</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>In the above program, two different exception declarations declare an
exception <span class="monospaced">E</span> and a corresponding function that returns <span class="monospaced">true</span> only on
that exception.  Although declared by syntactically identical
exception declarations, <span class="monospaced">e1</span> and <span class="monospaced">e2</span> are different exceptions.  The
program, when run, prints <span class="monospaced">true</span>, <span class="monospaced">false</span>, <span class="monospaced">false</span>, <span class="monospaced">true</span>.</p></div>
<div class="paragraph"><p>A slight modification of the above program shows that even a single
exception declaration yields a new exception each time it is
evaluated.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">fun</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">():</span><span class="w"> </span><span class="n">exn</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">exn</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">bool</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">let</span><span class="w"></span>
<span class="w">      </span><span class="k">exception</span><span class="w"> </span><span class="n">E</span><span class="w"></span>
<span class="w">   </span><span class="k">in</span><span class="w"></span>
<span class="w">      </span><span class="p">(</span><span class="n">E</span><span class="p">,</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">E</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">true</span><span class="w"> </span><span class="p">|</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">false</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">val</span><span class="w"> </span><span class="p">(</span><span class="n">e1</span><span class="p">,</span><span class="w"> </span><span class="n">isE1</span><span class="p">)</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="n">e2</span><span class="p">,</span><span class="w"> </span><span class="n">isE2</span><span class="p">)</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">fun</span><span class="w"> </span><span class="n">pb</span><span class="w"> </span><span class="p">(</span><span class="n">b</span><span class="p">:</span><span class="w"> </span><span class="n">bool</span><span class="p">):</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="n">print</span><span class="w"> </span><span class="p">(</span><span class="n">concat</span><span class="w"> </span><span class="p">[</span><span class="n">Bool</span><span class="p">.</span><span class="n">toString</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</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="p">(</span><span class="n">pb</span><span class="w"> </span><span class="p">(</span><span class="n">isE1</span><span class="w"> </span><span class="n">e1</span><span class="p">)</span><span class="w"></span>
<span class="w">          </span><span class="p">;</span><span class="w"> </span><span class="n">pb</span><span class="w"> </span><span class="p">(</span><span class="n">isE1</span><span class="w"> </span><span class="n">e2</span><span class="p">)</span><span class="w"></span>
<span class="w">          </span><span class="p">;</span><span class="w"> </span><span class="n">pb</span><span class="w"> </span><span class="p">(</span><span class="n">isE2</span><span class="w"> </span><span class="n">e1</span><span class="p">)</span><span class="w"></span>
<span class="w">          </span><span class="p">;</span><span class="w"> </span><span class="n">pb</span><span class="w"> </span><span class="p">(</span><span class="n">isE2</span><span class="w"> </span><span class="n">e2</span><span class="p">))</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Each call to <span class="monospaced">f</span> yields a new exception and a function that returns
<span class="monospaced">true</span> only on that exception.  The program, when run, prints <span class="monospaced">true</span>,
<span class="monospaced">false</span>, <span class="monospaced">false</span>, <span class="monospaced">true</span>.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_type_safety">Type Safety</h2>
<div class="sectionbody">
<div class="paragraph"><p>Exception generativity is required for type safety.  Consider the
following valid SML program.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">fun</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="n">&#39;a</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">exn</span><span class="p">)</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">exn</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">let</span><span class="w"></span>
<span class="w">      </span><span class="k">exception</span><span class="w"> </span><span class="n">E</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="w">   </span><span class="k">in</span><span class="w"></span>
<span class="w">      </span><span class="p">(</span><span class="n">E</span><span class="p">,</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">E</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><span class="p">|</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="k">raise</span><span class="w"> </span><span class="n">Fail</span><span class="w"> </span><span class="s">&quot;f&quot;</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">fun</span><span class="w"> </span><span class="n">cast</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="p">):</span><span class="w"> </span><span class="n">&#39;b</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">let</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="p">(</span><span class="n">make</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">exn</span><span class="p">,</span><span class="w"> </span><span class="p">_)</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="w">      </span><span class="k">val</span><span class="w"> </span><span class="p">(_,</span><span class="w"> </span><span class="n">get</span><span class="p">:</span><span class="w"> </span><span class="n">exn</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">)</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="w">   </span><span class="k">in</span><span class="w"></span>
<span class="w">      </span><span class="n">get</span><span class="w"> </span><span class="p">(</span><span class="n">make</span><span class="w"> </span><span class="n">a</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">val</span><span class="w"> </span><span class="p">_</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">((</span><span class="n">cast</span><span class="w"> </span><span class="mi">13</span><span class="p">):</span><span class="w"> </span><span class="n">int</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">int</span><span class="p">)</span><span class="w"> </span><span class="mi">14</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>If exceptions weren&#8217;t generative, then each call <span class="monospaced">f ()</span> would yield
the same exception constructor <span class="monospaced">E</span>.  Then, our <span class="monospaced">cast</span> function could
use <span class="monospaced">make: 'a -&gt; exn</span> to convert any value into an exception and then
<span class="monospaced">get: exn -&gt; 'b</span> to convert that exception to a value of arbitrary
type.  If <span class="monospaced">cast</span> worked, then we could cast an integer as a function
and apply.  Of course, because of generative exceptions, this program
raises <span class="monospaced">Fail "f"</span>.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_applications">Applications</h2>
<div class="sectionbody">
<div class="paragraph"><p>The <span class="monospaced">exn</span> type is effectively a <a href="UniversalType">universal type</a>.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_also_see">Also see</h2>
<div class="sectionbody">
<div class="ulist"><ul>
<li>
<p>
<a href="GenerativeDatatype">GenerativeDatatype</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>
