<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.26">
<title>GenerativeException</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
<link rel="stylesheet" href="./asciidoctor.css">
<link rel="stylesheet" href="./mlton.css">

</head>
<body class="article">
<div id="mlton-header">
<div id="mlton-header-text">
<h2>
<a href="./Home">
MLton
20241230+git20251029+dfsg-5
</a>
</h2>
</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">
<pre class="rouge highlight"><code data-lang="sml">exception E
val e1 = E
fun isE1 (e: exn): bool =
   case e of
      E =&gt; true
    | _ =&gt; false
exception E
val e2 = E
fun isE2 (e: exn): bool =
   case e of
      E =&gt; true
    | _ =&gt; false
fun pb (b: bool): unit =
   print (concat [Bool.toString b, "\n"])
val () = (pb (isE1 e1)
          ;pb (isE1 e2)
          ; pb (isE2 e1)
          ; pb (isE2 e2))</code></pre>
</div>
</div>
<div class="paragraph">
<p>In the above program, two different exception declarations declare an
exception <code>E</code> and a corresponding function that returns <code>true</code> only on
that exception.  Although declared by syntactically identical
exception declarations, <code>e1</code> and <code>e2</code> are different exceptions.  The
program, when run, prints <code>true</code>, <code>false</code>, <code>false</code>, <code>true</code>.</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">
<pre class="rouge highlight"><code data-lang="sml">fun f (): exn * (exn -&gt; bool) =
   let
      exception E
   in
      (E, fn E =&gt; true | _ =&gt; false)
   end
val (e1, isE1) = f ()
val (e2, isE2) = f ()
fun pb (b: bool): unit =
   print (concat [Bool.toString b, "\n"])
val () = (pb (isE1 e1)
          ; pb (isE1 e2)
          ; pb (isE2 e1)
          ; pb (isE2 e2))</code></pre>
</div>
</div>
<div class="paragraph">
<p>Each call to <code>f</code> yields a new exception and a function that returns
<code>true</code> only on that exception.  The program, when run, prints <code>true</code>,
<code>false</code>, <code>false</code>, <code>true</code>.</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">
<pre class="rouge highlight"><code data-lang="sml">fun f (): ('a -&gt; exn) * (exn -&gt; 'a) =
   let
      exception E of 'a
   in
      (E, fn E x =&gt; x | _ =&gt; raise Fail "f")
   end
fun cast (a: 'a): 'b =
   let
      val (make: 'a -&gt; exn, _) = f ()
      val (_, get: exn -&gt; 'b) = f ()
   in
      get (make a)
   end
val _ = ((cast 13): int -&gt; int) 14</code></pre>
</div>
</div>
<div class="paragraph">
<p>If exceptions weren&#8217;t generative, then each call <code>f ()</code> would yield
the same exception constructor <code>E</code>.  Then, our <code>cast</code> function could
use <code>make: 'a -&gt; exn</code> to convert any value into an exception and then
<code>get: exn -&gt; 'b</code> to convert that exception to a value of arbitrary
type.  If <code>cast</code> worked, then we could cast an integer as a function
and apply.  Of course, because of generative exceptions, this program
raises <code>Fail "f"</code>.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_applications">Applications</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The <code>exn</code> 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>
</body>
</html>