<!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>StaticSum</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>StaticSum</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>While SML makes it impossible to write functions whose types would
depend on the values of their arguments, or so called dependently
typed functions, it is possible, and arguably commonplace, to write
functions whose types depend on the types of their arguments.  Indeed,
the types of parametrically polymorphic functions like <span class="monospaced">map</span> and
<span class="monospaced">foldl</span> can be said to depend on the types of their arguments.  What
is less commonplace, however, is to write functions whose behavior
would depend on the types of their arguments.  Nevertheless, there are
several techniques for writing such functions.
<a href="TypeIndexedValues">Type-indexed values</a> and <a href="Fold">fold</a> are two such
techniques.  This page presents another such technique dubbed static
sums.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_ordinary_sums">Ordinary Sums</h2>
<div class="sectionbody">
<div class="paragraph"><p>Consider the sum type as defined below:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">structure</span><span class="w"> </span><span class="n">Sum</span><span class="w"> </span><span class="p">=</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="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">INL</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="p">|</span><span class="w"> </span><span class="n">INR</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">&#39;b</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>While a generic sum type such as defined above is very useful, it has
a number of limitations.  As an example, we could write the function
<span class="monospaced">out</span> to extract the value from a sum as follows:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">fun</span><span class="w"> </span><span class="n">out</span><span class="w"> </span><span class="p">(</span><span class="n">s</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="p">,</span><span class="w"> </span><span class="n">&#39;a</span><span class="p">)</span><span class="w"> </span><span class="n">Sum</span><span class="p">.</span><span class="n">t</span><span class="p">)</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="p">=</span><span class="w"></span>
<span class="w">    </span><span class="k">case</span><span class="w"> </span><span class="n">s</span><span class="w"></span>
<span class="w">     </span><span class="k">of</span><span class="w"> </span><span class="n">Sum</span><span class="p">.</span><span class="n">INL</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">a</span><span class="w"></span>
<span class="w">      </span><span class="p">|</span><span class="w"> </span><span class="n">Sum</span><span class="p">.</span><span class="n">INR</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">a</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>As can be seen from the type of <span class="monospaced">out</span>, it is limited in the sense that
it requires both variants of the sum to have the same type.  So, <span class="monospaced">out</span>
cannot be used to extract the value of a sum of two different types,
such as the type <span class="monospaced">(int, real) Sum.t</span>.  As another example of a
limitation, consider the following attempt at a <span class="monospaced">succ</span> function:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">fun</span><span class="w"> </span><span class="n">succ</span><span class="w"> </span><span class="p">(</span><span class="n">s</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">int</span><span class="p">,</span><span class="w"> </span><span class="n">real</span><span class="p">)</span><span class="w"> </span><span class="n">Sum</span><span class="p">.</span><span class="n">t</span><span class="p">)</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">???</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">s</span><span class="w"></span>
<span class="w">     </span><span class="k">of</span><span class="w"> </span><span class="n">Sum</span><span class="p">.</span><span class="n">INL</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="mi">1</span><span class="w"></span>
<span class="w">      </span><span class="p">|</span><span class="w"> </span><span class="n">Sum</span><span class="p">.</span><span class="n">INR</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Real</span><span class="p">.</span><span class="n">nextAfter</span><span class="w"> </span><span class="p">(</span><span class="n">r</span><span class="p">,</span><span class="w"> </span><span class="n">Real</span><span class="p">.</span><span class="n">posInf</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The above definition of <span class="monospaced">succ</span> cannot be typed, because there is no
type for the codomain within SML.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_static_sums">Static Sums</h2>
<div class="sectionbody">
<div class="paragraph"><p>Interestingly, it is possible to define values <span class="monospaced">inL</span>, <span class="monospaced">inR</span>, and
<span class="monospaced">match</span> that satisfy the laws</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>match (inL x) (f, g) = f x
match (inR x) (f, g) = g x</pre>
</div></div>
<div class="paragraph"><p>and do not suffer from the same limitions.  The definitions are
actually quite trivial:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">structure</span><span class="w"> </span><span class="n">StaticSum</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">struct</span><span class="w"></span>
<span class="w">   </span><span class="k">fun</span><span class="w"> </span><span class="n">inL</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">(</span><span class="n">f</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="n">x</span><span class="w"></span>
<span class="w">   </span><span class="k">fun</span><span class="w"> </span><span class="n">inR</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">(_,</span><span class="w"> </span><span class="n">g</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">g</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">match</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">x</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Now, given the <span class="monospaced">succ</span> function defined as</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">fun</span><span class="w"> </span><span class="n">succ</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">    </span><span class="n">StaticSum</span><span class="p">.</span><span class="n">match</span><span class="w"> </span><span class="n">s</span><span class="w"></span>
<span class="w">       </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="k">fn</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Real</span><span class="p">.</span><span class="n">nextAfter</span><span class="w"> </span><span class="p">(</span><span class="n">r</span><span class="p">,</span><span class="w"> </span><span class="n">Real</span><span class="p">.</span><span class="n">posInf</span><span class="p">))</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>we get</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">succ</span><span class="w"> </span><span class="p">(</span><span class="n">StaticSum</span><span class="p">.</span><span class="n">inL</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">2</span><span class="w"></span>
<span class="n">succ</span><span class="w"> </span><span class="p">(</span><span class="n">StaticSum</span><span class="p">.</span><span class="n">inR</span><span class="w"> </span><span class="n">Real</span><span class="p">.</span><span class="n">maxFinite</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Real</span><span class="p">.</span><span class="n">posInf</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>To better understand how this works, consider the following signature
for static sums:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">structure</span><span class="w"> </span><span class="n">StaticSum</span><span class="w"> </span><span class="p">:&gt;</span><span class="w"> </span><span class="k">sig</span><span class="w"></span>
<span class="w">   </span><span class="k">type</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;dL</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;cL</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;dR</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;cR</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="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">inL</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">&#39;dL</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;dL</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;cL</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;dR</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;cR</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;cL</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">val</span><span class="w"> </span><span class="n">inR</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">&#39;dR</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;dL</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;cL</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;dR</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;cR</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;cR</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">val</span><span class="w"> </span><span class="n">match</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;dL</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;cL</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;dR</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;cR</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">-&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;dL</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;cL</span><span class="p">)</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;dR</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;cR</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;c</span><span class="w"></span>
<span class="k">end</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">struct</span><span class="w"></span>
<span class="w">   </span><span class="k">type</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;dL</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;cL</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;dR</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;cR</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="p">(</span><span class="n">&#39;dL</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;cL</span><span class="p">)</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;dR</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;cR</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;c</span><span class="w"></span>
<span class="w">   </span><span class="k">open</span><span class="w"> </span><span class="n">StaticSum</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Above, <span class="monospaced">'d</span> stands for domain and <span class="monospaced">'c</span> for codomain.  The key
difference between an ordinary sum type, like <span class="monospaced">(int, real) Sum.t</span>, and
a static sum type, like <span class="monospaced">(int, real, real, int, real) StaticSum.t</span>, is
that the ordinary sum type says nothing about the type of the result
of deconstructing a sum while the static sum type specifies the type.</p></div>
<div class="paragraph"><p>With the sealed static sum module, we get the type</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">val</span><span class="w"> </span><span class="n">succ</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">int</span><span class="p">,</span><span class="w"> </span><span class="n">int</span><span class="p">,</span><span class="w"> </span><span class="n">real</span><span class="p">,</span><span class="w"> </span><span class="n">real</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">StaticSum</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">&#39;a</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>for the previously defined <span class="monospaced">succ</span> function.  The type specifies that
<span class="monospaced">succ</span> maps a left <span class="monospaced">int</span> to an <span class="monospaced">int</span> and a right <span class="monospaced">real</span> to a <span class="monospaced">real</span>.
For example, the type of <span class="monospaced">StaticSum.inL 1</span> is
<span class="monospaced">(int, 'cL, 'dR, 'cR, 'cL) StaticSum.t</span>.  Unifying this with the
argument type of <span class="monospaced">succ</span> gives the type <span class="monospaced">(int, int, real, real, int)
StaticSum.t -&gt; int</span>.</p></div>
<div class="paragraph"><p>The <span class="monospaced">out</span> function is quite useful on its own.  Here is how it can be
defined:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">structure</span><span class="w"> </span><span class="n">StaticSum</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">struct</span><span class="w"></span>
<span class="w">   </span><span class="k">open</span><span class="w"> </span><span class="n">StaticSum</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">out</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="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="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">-&gt;</span><span class="w"> </span><span class="n">&#39;c</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">match</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="p">(</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="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="p">)</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Due to the value restriction, lack of first class polymorphism and
polymorphic recursion, the usefulness and convenience of static sums
is somewhat limited in SML.  So, don&#8217;t throw away the ordinary sum
type just yet.  Static sums can nevertheless be quite useful.</p></div>
<div class="sect2">
<h3 id="_example_send_and_receive_with_argument_type_dependent_result_types">Example: Send and Receive with Argument Type Dependent Result Types</h3>
<div class="paragraph"><p>In some situations it would seem useful to define functions whose
result type would depend on some of the arguments.  Traditionally such
functions have been thought to be impossible in SML and the solution
has been to define multiple functions.  For example, the
<a href="http://www.standardml.org/Basis/socket.html"><span class="monospaced">Socket</span> structure</a> of the
Basis library defines 16 <span class="monospaced">send</span> and 16 <span class="monospaced">recv</span> functions.  In contrast,
the Net structure
(<a href="https://github.com/MLton/mltonlib/blob/master/com/sweeks/basic/unstable/net.sig"><span class="monospaced">net.sig</span></a>) of the
Basic library designed by Stephen Weeks defines only a single <span class="monospaced">send</span>
and a single <span class="monospaced">receive</span> and the result types of the functions depend on
their arguments.  The implementation
(<a href="https://github.com/MLton/mltonlib/blob/master/com/sweeks/basic/unstable/net.sml"><span class="monospaced">net.sml</span></a>) uses
static sums (with a slighly different signature:
<a href="https://github.com/MLton/mltonlib/blob/master/com/sweeks/basic/unstable/static-sum.sig"><span class="monospaced">static-sum.sig</span></a>).</p></div>
</div>
<div class="sect2">
<h3 id="_example_picking_monad_results">Example: Picking Monad Results</h3>
<div class="paragraph"><p>Suppose that we need to write a parser that accepts a pair of integers
and returns their sum given a monadic parsing combinator library.  A
part of the signature of such library could look like this</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">signature</span><span class="w"> </span><span class="n">PARSING</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">sig</span><span class="w"></span>
<span class="w">   </span><span class="k">include</span><span class="w"> </span><span class="n">MONAD</span><span class="w"></span>
<span class="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">int</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">int</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">lparen</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">unit</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">rparen</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">unit</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">comma</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">   </span><span class="cm">(* ... *)</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>where the <span class="monospaced">MONAD</span> signature could be defined as</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">signature</span><span class="w"> </span><span class="n">MONAD</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">sig</span><span class="w"></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="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">return</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="p">-&gt;</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="w">   </span><span class="k">val</span><span class="w"> </span><span class="n">&gt;&gt;=</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">t</span><span class="w"> </span><span class="n">*</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">&#39;b</span><span class="w"> </span><span class="n">t</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;b</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="k">end</span><span class="w"></span>
<span class="k">infix</span><span class="w"> </span><span class="n">&gt;&gt;=</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The straightforward, but tedious, way to write the desired parser is:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">val</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">lparen</span><span class="w"> </span><span class="n">&gt;&gt;=</span><span class="w"> </span><span class="p">(</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="w">        </span><span class="n">int</span><span class="w">    </span><span class="n">&gt;&gt;=</span><span class="w"> </span><span class="p">(</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="w">        </span><span class="n">comma</span><span class="w">  </span><span class="n">&gt;&gt;=</span><span class="w"> </span><span class="p">(</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="w">        </span><span class="n">int</span><span class="w">    </span><span class="n">&gt;&gt;=</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">        </span><span class="n">rparen</span><span class="w"> </span><span class="n">&gt;&gt;=</span><span class="w"> </span><span class="p">(</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="w">        </span><span class="n">return</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="n">y</span><span class="p">))))))</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>In Haskell, the parser could be written using the <span class="monospaced">do</span> notation
considerably less verbosely as:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="nf">p</span> <span class="ow">=</span> <span class="kr">do</span> <span class="p">{</span> <span class="n">lparen</span> <span class="p">;</span> <span class="n">x</span> <span class="ow">&lt;-</span> <span class="n">int</span> <span class="p">;</span> <span class="n">comma</span> <span class="p">;</span> <span class="n">y</span> <span class="ow">&lt;-</span> <span class="n">int</span> <span class="p">;</span> <span class="n">rparen</span> <span class="p">;</span> <span class="n">return</span> <span class="o">$</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="p">}</span>
</pre></div></div></div>
<div class="paragraph"><p>SML doesn&#8217;t provide a <span class="monospaced">do</span> notation, so we need another solution.</p></div>
<div class="paragraph"><p>Suppose we would have a "pick" notation for monads that would allows
us to write the parser as</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">val</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">`lparen</span><span class="w"> </span><span class="n">^</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">`comma</span><span class="w"> </span><span class="n">^</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">`rparen</span><span class="w"> </span><span class="n">@</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="n">&amp;</span><span class="w"> </span><span class="n">y</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="n">+</span><span class="w"> </span><span class="n">y</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>using four auxiliary combinators: <span class="monospaced">&grave;</span>, <span class="monospaced">\</span>, <span class="monospaced">^</span>, and <span class="monospaced">@</span>.</p></div>
<div class="paragraph"><p>Roughly speaking</p></div>
<div class="ulist"><ul>
<li>
<p>
<span class="monospaced">&grave;p</span> means that the result of <span class="monospaced">p</span> is dropped,
</p>
</li>
<li>
<p>
<span class="monospaced">\p</span> means that the result of <span class="monospaced">p</span> is taken,
</p>
</li>
<li>
<p>
<span class="monospaced">p ^ q</span> means that results of <span class="monospaced">p</span> and <span class="monospaced">q</span> are taken as a product, and
</p>
</li>
<li>
<p>
<span class="monospaced">p @ a</span> means that the results of <span class="monospaced">p</span> are passed to the function <span class="monospaced">a</span> and that result is returned.
</p>
</li>
</ul></div>
<div class="paragraph"><p>The difficulty is in implementing the concatenation combinator <span class="monospaced">^</span>.
The type of the result of the concatenation depends on the types of
the arguments.</p></div>
<div class="paragraph"><p>Using static sums and the <a href="ProductType">product type</a>, the pick
notation for monads can be implemented as follows:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">functor</span><span class="w"> </span><span class="n">MkMonadPick</span><span class="w"> </span><span class="p">(</span><span class="k">include</span><span class="w"> </span><span class="n">MONAD</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">let</span><span class="w"></span>
<span class="w">   </span><span class="k">open</span><span class="w"> </span><span class="n">StaticSum</span><span class="w"></span>
<span class="k">in</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">fun</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">inL</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="n">&gt;&gt;=</span><span class="w"> </span><span class="p">(</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="n">return</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="n">\</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">inR</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">@</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">out</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">&gt;&gt;=</span><span class="w"> </span><span class="p">(</span><span class="n">return</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">f</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">^</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">          </span><span class="p">(</span><span class="n">match</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">match</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="w"></span>
<span class="w">             </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">                 </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">inL</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="n">&gt;&gt;=</span><span class="w"> </span><span class="p">(</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="n">b</span><span class="p">)),</span><span class="w"></span>
<span class="w">                  </span><span class="k">fn</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">inR</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="n">&gt;&gt;=</span><span class="w"> </span><span class="p">(</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="n">b</span><span class="p">))),</span><span class="w"></span>
<span class="w">              </span><span class="k">fn</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"></span>
<span class="w">                 </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">inR</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="n">&gt;&gt;=</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="n">&gt;&gt;=</span><span class="w"> </span><span class="p">(</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="n">return</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">fn</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">inR</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="n">&gt;&gt;=</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="n">&gt;&gt;=</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">return</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="n">&amp;</span><span class="w"> </span><span class="n">b</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">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The above implementation is inefficient, however.  It uses many more
bind operations, <span class="monospaced">&gt;&gt;=</span>, than necessary.  That can be solved with an
additional level of abstraction:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">functor</span><span class="w"> </span><span class="n">MkMonadPick</span><span class="w"> </span><span class="p">(</span><span class="k">include</span><span class="w"> </span><span class="n">MONAD</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">let</span><span class="w"></span>
<span class="w">   </span><span class="k">open</span><span class="w"> </span><span class="n">StaticSum</span><span class="w"></span>
<span class="k">in</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">fun</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">inL</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">&gt;&gt;=</span><span class="w"> </span><span class="p">(</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="n">b</span><span class="w"> </span><span class="p">()))</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</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">inR</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">&gt;&gt;=</span><span class="w"> </span><span class="n">b</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">@</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">out</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">(</span><span class="n">return</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">f</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">^</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">          </span><span class="p">(</span><span class="n">match</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">match</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="w"></span>
<span class="w">             </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">inL</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">(</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="n">b</span><span class="w"> </span><span class="n">c</span><span class="p">)),</span><span class="w"></span>
<span class="w">                       </span><span class="k">fn</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">inR</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">(</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="n">b</span><span class="w"> </span><span class="n">c</span><span class="p">))),</span><span class="w"></span>
<span class="w">              </span><span class="k">fn</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">inR</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">(</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="n">c</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">fn</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">inR</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="n">&amp;</span><span class="w"> </span><span class="n">b</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">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>After instantiating and opening either of the above monad pick
implementations, the previously given definition of <span class="monospaced">p</span> can be
compiled and results in a parser whose result is of type <span class="monospaced">int</span>.  Here
is a functor to test the theory:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">functor</span><span class="w"> </span><span class="n">Test</span><span class="w"> </span><span class="p">(</span><span class="n">Arg</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">PARSING</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">struct</span><span class="w"></span>
<span class="w">   </span><span class="k">local</span><span class="w"></span>
<span class="w">      </span><span class="k">structure</span><span class="w"> </span><span class="n">Pick</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">MkMonadPick</span><span class="w"> </span><span class="p">(</span><span class="n">Arg</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span><span class="k">open</span><span class="w"> </span><span class="n">Pick</span><span class="w"> </span><span class="n">Arg</span><span class="w"></span>
<span class="w">   </span><span class="k">in</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">int</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">          </span><span class="n">`lparen</span><span class="w"> </span><span class="n">^</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">`comma</span><span class="w"> </span><span class="n">^</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">`rparen</span><span class="w"> </span><span class="n">@</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="n">&amp;</span><span class="w"> </span><span class="n">y</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="n">+</span><span class="w"> </span><span class="n">y</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">end</span><span class="w"></span>
</pre></div></div></div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_also_see">Also see</h2>
<div class="sectionbody">
<div class="paragraph"><p>There are a number of related techniques.  Here are some of them.</p></div>
<div class="ulist"><ul>
<li>
<p>
<a href="Fold">Fold</a>
</p>
</li>
<li>
<p>
<a href="TypeIndexedValues">TypeIndexedValues</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>
