<!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>Fold</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>Fold</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>This page describes a technique that enables convenient syntax for a
number of language features that are not explicitly supported by
<a href="StandardML">Standard ML</a>, including: variable number of arguments,
<a href="OptionalArguments">optional arguments and labeled arguments</a>,
<a href="ArrayLiteral">array and vector literals</a>,
<a href="FunctionalRecordUpdate">functional record update</a>,
and (seemingly) dependently typed functions like <a href="Printf">printf</a> and scanf.</p></div>
<div class="paragraph"><p>The key idea to <em>fold</em> is to define functions <span class="monospaced">fold</span>, <span class="monospaced">step0</span>,
and <span class="monospaced">$</span> such that the following equation holds.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">fold</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">f</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">step0</span><span class="w"> </span><span class="n">h1</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">step0</span><span class="w"> </span><span class="n">h2</span><span class="p">)</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="p">(</span><span class="n">step0</span><span class="w"> </span><span class="n">hn</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="n">f</span><span class="w"> </span><span class="p">(</span><span class="n">hn</span><span class="w"> </span><span class="p">(...</span><span class="w"> </span><span class="p">(</span><span class="n">h2</span><span class="w"> </span><span class="p">(</span><span class="n">h1</span><span class="w"> </span><span class="n">a</span><span class="p">))))</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The name <span class="monospaced">fold</span> comes because this is like a traditional list fold,
where <span class="monospaced">a</span> is the <em>base element</em>, and each <em>step function</em>,
<span class="monospaced">step0 hi</span>, corresponds to one element of the list and does one
step of the fold.  The name <span class="monospaced">$</span> is chosen to mean "end of
arguments" from its common use in regular-expression syntax.</p></div>
<div class="paragraph"><p>Unlike the usual list fold in which the same function is used to step
over each element in the list, this fold allows the step functions to
be different from each other, and even to be of different types.  Also
unlike the usual list fold, this fold includes a "finishing
function", <span class="monospaced">f</span>, that is applied to the result of the fold.  The
presence of the finishing function may seem odd because there is no
analogy in list fold.  However, the finishing function is essential;
without it, there would be no way for the folder to perform an
arbitrary computation after processing all the arguments.  The
examples below will make this clear.</p></div>
<div class="paragraph"><p>The functions <span class="monospaced">fold</span>, <span class="monospaced">step0</span>, and <span class="monospaced">$</span> are easy to
define.</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">$</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">f</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">a</span><span class="w"></span>
<span class="k">fun</span><span class="w"> </span><span class="n">id</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">structure</span><span class="w"> </span><span class="n">Fold</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">fun</span><span class="w"> </span><span class="n">fold</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">f</span><span class="p">)</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">g</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">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">step0</span><span class="w"> </span><span class="n">h</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">f</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">h</span><span class="w"> </span><span class="n">a</span><span class="p">,</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">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>We&#8217;ve placed <span class="monospaced">fold</span> and <span class="monospaced">step0</span> in the <span class="monospaced">Fold</span> structure
but left <span class="monospaced">$</span> at the toplevel because it is convenient in code to
always have <span class="monospaced">$</span> in scope.  We&#8217;ve also defined the identity
function, <span class="monospaced">id</span>, at the toplevel since we use it so frequently.</p></div>
<div class="paragraph"><p>Plugging in the definitions, it is easy to verify the equation from
above.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">fold</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">f</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">step0</span><span class="w"> </span><span class="n">h1</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">step0</span><span class="w"> </span><span class="n">h2</span><span class="p">)</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="p">(</span><span class="n">step0</span><span class="w"> </span><span class="n">hn</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="n">step0</span><span class="w"> </span><span class="n">h1</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">f</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">step0</span><span class="w"> </span><span class="n">h2</span><span class="p">)</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="p">(</span><span class="n">step0</span><span class="w"> </span><span class="n">hn</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="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">h1</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">f</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">step0</span><span class="w"> </span><span class="n">h2</span><span class="p">)</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="p">(</span><span class="n">step0</span><span class="w"> </span><span class="n">hn</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="n">step0</span><span class="w"> </span><span class="n">h2</span><span class="w"> </span><span class="p">(</span><span class="n">h1</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </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="n">step0</span><span class="w"> </span><span class="n">hn</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="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">h2</span><span class="w"> </span><span class="p">(</span><span class="n">h1</span><span class="w"> </span><span class="n">a</span><span class="p">),</span><span class="w"> </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="n">step0</span><span class="w"> </span><span class="n">hn</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="p">=</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">hn</span><span class="w"> </span><span class="p">(...</span><span class="w"> </span><span class="p">(</span><span class="n">h2</span><span class="w"> </span><span class="p">(</span><span class="n">h1</span><span class="w"> </span><span class="n">a</span><span class="p">))),</span><span class="w"> </span><span class="n">f</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="n">$</span><span class="w"> </span><span class="p">(</span><span class="n">hn</span><span class="w"> </span><span class="p">(...</span><span class="w"> </span><span class="p">(</span><span class="n">h2</span><span class="w"> </span><span class="p">(</span><span class="n">h1</span><span class="w"> </span><span class="n">a</span><span class="p">))),</span><span class="w"> </span><span class="n">f</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="n">hn</span><span class="w"> </span><span class="p">(...</span><span class="w"> </span><span class="p">(</span><span class="n">h2</span><span class="w"> </span><span class="p">(</span><span class="n">h1</span><span class="w"> </span><span class="n">a</span><span class="p">))))</span><span class="w"></span>
</pre></div></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_example_variable_number_of_arguments">Example: variable number of arguments</h2>
<div class="sectionbody">
<div class="paragraph"><p>The simplest example of fold is accepting a variable number of
(curried) arguments.  We&#8217;ll define a function <span class="monospaced">f</span> and argument
<span class="monospaced">a</span> such that all of the following expressions are valid.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">f</span><span class="w"> </span><span class="n">$</span><span class="w"></span>
<span class="n">f</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="n">a</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="n">a</span><span class="w"> </span><span class="n">a</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="n">a</span><span class="w"> </span><span class="n">a</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">a</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">$</span><span class="w"> </span><span class="cm">(* as many a&#39;s as we want *)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Off-hand it may appear impossible that all of the above expressions
are type correct SML&#8201;&#8212;&#8201;how can a function <span class="monospaced">f</span> accept a variable
number of curried arguments?  What could the type of <span class="monospaced">f</span> be?
We&#8217;ll have more to say later on how type checking works.  For now,
once we have supplied the definitions below, you can check that the
expressions are type correct by feeding them to your favorite SML
implementation.</p></div>
<div class="paragraph"><p>It is simple to define <span class="monospaced">f</span> and <span class="monospaced">a</span>.  We define <span class="monospaced">f</span> as a
folder whose base element is <span class="monospaced">()</span> and whose finish function does
nothing.  We define <span class="monospaced">a</span> as the step function that does nothing.
The only trickiness is that we must <a href="EtaExpansion">eta expand</a> the
definition of <span class="monospaced">f</span> and <span class="monospaced">a</span> to work around the ValueRestriction;
we frequently use eta expansion for this purpose without mention.</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">base</span><span class="w"> </span><span class="p">=</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">finish</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="w"></span>
<span class="k">fun</span><span class="w"> </span><span class="n">step</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="w"></span>
<span class="k">val</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">fn</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">base</span><span class="p">,</span><span class="w"> </span><span class="n">finish</span><span class="p">)</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">a</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step0</span><span class="w"> </span><span class="n">step</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>One can easily apply the fold equation to verify by hand that <span class="monospaced">f</span>
applied to any number of <span class="monospaced">a</span>'s evaluates to <span class="monospaced">()</span>.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">f</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">a</span><span class="w"> </span><span class="n">$</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">finish</span><span class="w"> </span><span class="p">(</span><span class="n">step</span><span class="w"> </span><span class="p">(...</span><span class="w"> </span><span class="p">(</span><span class="n">step</span><span class="w"> </span><span class="n">base</span><span class="p">)))</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">finish</span><span class="w"> </span><span class="p">(</span><span class="n">step</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="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">finish</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="w"></span>
</pre></div></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_example_variable_argument_sum">Example: variable-argument sum</h2>
<div class="sectionbody">
<div class="paragraph"><p>Let&#8217;s look at an example that computes something: a variable-argument
function <span class="monospaced">sum</span> and a stepper <span class="monospaced">a</span> such that</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">sum</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="n">i1</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="n">i2</span><span class="p">)</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="n">im</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="n">i1</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="n">i2</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="n">im</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The idea is simple&#8201;&#8212;&#8201;the folder starts with a base accumulator of
<span class="monospaced">0</span> and the stepper adds each element to the accumulator, <span class="monospaced">s</span>,
which the folder simply returns at the end.</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">sum</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="mi">0</span><span class="p">,</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">s</span><span class="p">)</span><span class="w"> </span><span class="n">z</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">i</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step0</span><span class="w"> </span><span class="p">(</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">i</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="n">s</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Using the fold equation, one can verify the following.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">sum</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="mi">3</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="mi">6</span><span class="w"></span>
</pre></div></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_step1">Step1</h2>
<div class="sectionbody">
<div class="paragraph"><p>It is sometimes syntactically convenient to omit the parentheses
around the steps in a fold.  This is easily done by defining a new
function, <span class="monospaced">step1</span>, as follows.</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">Fold</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">open</span><span class="w"> </span><span class="n">Fold</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">step1</span><span class="w"> </span><span class="n">h</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">f</span><span class="p">)</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">h</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">a</span><span class="p">),</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">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>From the definition of <span class="monospaced">step1</span>, we have the following
equivalence.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">fold</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">f</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">step1</span><span class="w"> </span><span class="n">h</span><span class="p">)</span><span class="w"> </span><span class="n">b</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">step1</span><span class="w"> </span><span class="n">h</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">f</span><span class="p">)</span><span class="w"> </span><span class="n">b</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">h</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">a</span><span class="p">),</span><span class="w"> </span><span class="n">f</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Using the above equivalence, we can compute the following equation for
<span class="monospaced">step1</span>.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">fold</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">f</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">step1</span><span class="w"> </span><span class="n">h1</span><span class="p">)</span><span class="w"> </span><span class="n">b1</span><span class="w"> </span><span class="p">(</span><span class="n">step1</span><span class="w"> </span><span class="n">h2</span><span class="p">)</span><span class="w"> </span><span class="n">b2</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="p">(</span><span class="n">step1</span><span class="w"> </span><span class="n">hn</span><span class="p">)</span><span class="w"> </span><span class="n">bn</span><span class="w"> </span><span class="n">$</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">h1</span><span class="w"> </span><span class="p">(</span><span class="n">b1</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">),</span><span class="w"> </span><span class="n">f</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">step1</span><span class="w"> </span><span class="n">h2</span><span class="p">)</span><span class="w"> </span><span class="n">b2</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="p">(</span><span class="n">step1</span><span class="w"> </span><span class="n">hn</span><span class="p">)</span><span class="w"> </span><span class="n">bn</span><span class="w"> </span><span class="n">$</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">h2</span><span class="w"> </span><span class="p">(</span><span class="n">b2</span><span class="p">,</span><span class="w"> </span><span class="n">h1</span><span class="w"> </span><span class="p">(</span><span class="n">b1</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">)),</span><span class="w"> </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="n">step1</span><span class="w"> </span><span class="n">hn</span><span class="p">)</span><span class="w"> </span><span class="n">bn</span><span class="w"> </span><span class="n">$</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">hn</span><span class="w"> </span><span class="p">(</span><span class="n">bn</span><span class="p">,</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="p">(</span><span class="n">h2</span><span class="w"> </span><span class="p">(</span><span class="n">b2</span><span class="p">,</span><span class="w"> </span><span class="n">h1</span><span class="w"> </span><span class="p">(</span><span class="n">b1</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">)))),</span><span class="w"> </span><span class="n">f</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="n">f</span><span class="w"> </span><span class="p">(</span><span class="n">hn</span><span class="w"> </span><span class="p">(</span><span class="n">bn</span><span class="p">,</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="p">(</span><span class="n">h2</span><span class="w"> </span><span class="p">(</span><span class="n">b2</span><span class="p">,</span><span class="w"> </span><span class="n">h1</span><span class="w"> </span><span class="p">(</span><span class="n">b1</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">)))))</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Here is an example using <span class="monospaced">step1</span> to define a variable-argument
product function, <span class="monospaced">prod</span>, with a convenient syntax.</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">prod</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">p</span><span class="p">)</span><span class="w"> </span><span class="n">z</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="k">fn</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step1</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">p</span><span class="p">)</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="n">p</span><span class="p">)</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The functions <span class="monospaced">prod</span> and <span class="monospaced">&grave;</span> satisfy the following equation.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">prod</span><span class="w"> </span><span class="n">`i1</span><span class="w"> </span><span class="n">`i2</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="n">`im</span><span class="w"> </span><span class="n">$</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">i1</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">i2</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">im</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Note that in SML, <span class="monospaced">&grave;i1</span> is two different tokens, <span class="monospaced">&grave;</span> and
<span class="monospaced">i1</span>.  We often use <span class="monospaced">&grave;</span> for an instance of a <span class="monospaced">step1</span> function
because of its syntactic unobtrusiveness and because no space is
required to separate it from an alphanumeric token.</p></div>
<div class="paragraph"><p>Also note that there are no parenthesis around the steps.  That is,
the following expression is not the same as the above one (in fact, it
is not type correct).</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">prod</span><span class="w"> </span><span class="p">(</span><span class="n">`i1</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">`i2</span><span class="p">)</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="p">(</span><span class="n">`im</span><span class="p">)</span><span class="w"> </span><span class="n">$</span><span class="w"></span>
</pre></div></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_example_list_literals">Example: list literals</h2>
<div class="sectionbody">
<div class="paragraph"><p>SML already has a syntax for list literals, e.g. <span class="monospaced">[w, x, y, z]</span>.
However, using fold, we can define our own syntax.</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">list</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">fold</span><span class="w"> </span><span class="p">([],</span><span class="w"> </span><span class="n">rev</span><span class="p">)</span><span class="w"> </span><span class="n">z</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="k">fn</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step1</span><span class="w"> </span><span class="p">(</span><span class="k">op</span><span class="w"> </span><span class="n">::</span><span class="p">)</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The idea is that the folder starts out with the empty list, the steps
accumulate the elements into a list, and then the finishing function
reverses the list at the end.</p></div>
<div class="paragraph"><p>With these definitions one can write a list like:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">list</span><span class="w"> </span><span class="n">`w</span><span class="w"> </span><span class="n">`x</span><span class="w"> </span><span class="n">`y</span><span class="w"> </span><span class="n">`z</span><span class="w"> </span><span class="n">$</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>While the example is not practically useful, it does demonstrate the
need for the finishing function to be incorporated in <span class="monospaced">fold</span>.
Without a finishing function, every use of <span class="monospaced">list</span> would need to be
wrapped in <span class="monospaced">rev</span>, as follows.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">rev</span><span class="w"> </span><span class="p">(</span><span class="n">list</span><span class="w"> </span><span class="n">`w</span><span class="w"> </span><span class="n">`x</span><span class="w"> </span><span class="n">`y</span><span class="w"> </span><span class="n">`z</span><span class="w"> </span><span class="n">$</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The finishing function allows us to incorporate the reversal into the
definition of <span class="monospaced">list</span>, and to treat <span class="monospaced">list</span> as a truly variable
argument function, performing an arbitrary computation after receiving
all of its arguments.</p></div>
<div class="paragraph"><p>See <a href="ArrayLiteral">ArrayLiteral</a> for a similar use of <span class="monospaced">fold</span> that provides a
syntax for array and vector literals, which are not built in to SML.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_fold_right">Fold right</h2>
<div class="sectionbody">
<div class="paragraph"><p>Just as <span class="monospaced">fold</span> is analogous to a fold left, in which the functions
are applied to the accumulator left-to-right, we can define a variant
of <span class="monospaced">fold</span> that is analogous to a fold right, in which the
functions are applied to the accumulator right-to-left.  That is, we
can define functions <span class="monospaced">foldr</span> and <span class="monospaced">step0</span> such that the
following equation holds.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">foldr</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">f</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">step0</span><span class="w"> </span><span class="n">h1</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">step0</span><span class="w"> </span><span class="n">h2</span><span class="p">)</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="p">(</span><span class="n">step0</span><span class="w"> </span><span class="n">hn</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="n">f</span><span class="w"> </span><span class="p">(</span><span class="n">h1</span><span class="w"> </span><span class="p">(</span><span class="n">h2</span><span class="w"> </span><span class="p">(...</span><span class="w"> </span><span class="p">(</span><span class="n">hn</span><span class="w"> </span><span class="n">a</span><span class="p">))))</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The implementation of fold right is easy, using fold.  The idea is for
the fold to start with <span class="monospaced">f</span> and for each step to precompose the
next <span class="monospaced">hi</span>.  Then, the finisher applies the composed function to
the base value, <span class="monospaced">a</span>.  Here is the code.</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">Foldr</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">fun</span><span class="w"> </span><span class="n">foldr</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">f</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">g</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">fun</span><span class="w"> </span><span class="n">step0</span><span class="w"> </span><span class="n">h</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step0</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">h</span><span class="p">)</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Verifying the fold-right equation is straightforward, using the
fold-left equation.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">foldr</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">f</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">Foldr</span><span class="p">.</span><span class="n">step0</span><span class="w"> </span><span class="n">h1</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">Foldr</span><span class="p">.</span><span class="n">step0</span><span class="w"> </span><span class="n">h2</span><span class="p">)</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="p">(</span><span class="n">Foldr</span><span class="p">.</span><span class="n">step0</span><span class="w"> </span><span class="n">hn</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="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">g</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="n">Fold</span><span class="p">.</span><span class="n">step0</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">h1</span><span class="p">))</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="n">Fold</span><span class="p">.</span><span class="n">step0</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">h2</span><span class="p">))</span><span class="w"></span>
<span class="w">    </span><span class="p">...</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="n">Fold</span><span class="p">.</span><span class="n">step0</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">hn</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="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">g</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">g</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">hn</span><span class="p">)</span><span class="w"> </span><span class="p">(...</span><span class="w"> </span><span class="p">((</span><span class="k">fn</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">h2</span><span class="p">)</span><span class="w"> </span><span class="p">((</span><span class="k">fn</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">h1</span><span class="p">)</span><span class="w"> </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="k">fn</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">g</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">g</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">hn</span><span class="p">)</span><span class="w"> </span><span class="p">(...</span><span class="w"> </span><span class="p">((</span><span class="k">fn</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">h2</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">f</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">h1</span><span class="p">))))</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="p">((</span><span class="k">fn</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">hn</span><span class="p">)</span><span class="w"> </span><span class="p">(...</span><span class="w"> </span><span class="p">(</span><span class="n">f</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">h1</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">h2</span><span class="p">)))</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">f</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">h1</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">h2</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">hn</span><span class="p">)</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="n">f</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">h1</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">h2</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">hn</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">f</span><span class="w"> </span><span class="p">(</span><span class="n">h1</span><span class="w"> </span><span class="p">(</span><span class="n">h2</span><span class="w"> </span><span class="p">(...</span><span class="w"> </span><span class="p">(</span><span class="n">hn</span><span class="w"> </span><span class="n">a</span><span class="p">))))</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>One can also define the fold-right analogue of <span class="monospaced">step1</span>.</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">Foldr</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">open</span><span class="w"> </span><span class="n">Foldr</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">step1</span><span class="w"> </span><span class="n">h</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step1</span><span class="w"> </span><span class="p">(</span><span class="k">fn</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">g</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="n">o</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">h</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">a</span><span class="p">)))</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>
</pre></div></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_example_list_literals_via_fold_right">Example: list literals via fold right</h2>
<div class="sectionbody">
<div class="paragraph"><p>Revisiting the list literal example from earlier, we can use fold
right to define a syntax for list literals that doesn&#8217;t do a reversal.</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">list</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Foldr</span><span class="p">.</span><span class="n">foldr</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">l</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">l</span><span class="p">)</span><span class="w"> </span><span class="n">z</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="k">fn</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Foldr</span><span class="p">.</span><span class="n">step1</span><span class="w"> </span><span class="p">(</span><span class="k">op</span><span class="w"> </span><span class="n">::</span><span class="p">)</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>As before, with these definitions, one can write a list like:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">list</span><span class="w"> </span><span class="n">`w</span><span class="w"> </span><span class="n">`x</span><span class="w"> </span><span class="n">`y</span><span class="w"> </span><span class="n">`z</span><span class="w"> </span><span class="n">$</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The difference between the fold-left and fold-right approaches is that
the fold-right approach does not have to reverse the list at the end,
since it accumulates the elements in the correct order.  In practice,
MLton will simplify away all of the intermediate function composition,
so the the fold-right approach will be more efficient.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_mixing_steppers">Mixing steppers</h2>
<div class="sectionbody">
<div class="paragraph"><p>All of the examples so far have used the same step function throughout
a fold.  This need not be the case.  For example, consider the
following.</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">n</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </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="p">)</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">I</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step0</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">2</span><span class="p">)</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">O</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step0</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">2</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="n">z</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Here we have one folder, <span class="monospaced">n</span>, that can be used with two different
steppers, <span class="monospaced">I</span> and <span class="monospaced">O</span>.  By using the fold equation, one can
verify the following equations.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">n</span><span class="w"> </span><span class="n">O</span><span class="w"> </span><span class="n">$</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">0</span><span class="w"></span>
<span class="n">n</span><span class="w"> </span><span class="n">I</span><span class="w"> </span><span class="n">$</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">1</span><span class="w"></span>
<span class="n">n</span><span class="w"> </span><span class="n">I</span><span class="w"> </span><span class="n">O</span><span class="w"> </span><span class="n">$</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">n</span><span class="w"> </span><span class="n">I</span><span class="w"> </span><span class="n">O</span><span class="w"> </span><span class="n">I</span><span class="w"> </span><span class="n">$</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">5</span><span class="w"></span>
<span class="n">n</span><span class="w"> </span><span class="n">I</span><span class="w"> </span><span class="n">I</span><span class="w"> </span><span class="n">I</span><span class="w"> </span><span class="n">O</span><span class="w"> </span><span class="n">$</span><span class="w"> </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>That is, we&#8217;ve defined a syntax for writing binary integer constants.</p></div>
<div class="paragraph"><p>Not only can one use different instances of <span class="monospaced">step0</span> in the same
fold, one can also intermix uses of <span class="monospaced">step0</span> and <span class="monospaced">step1</span>.  For
example, consider the following.</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">n</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </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="p">)</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">O</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step0</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">n</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="mi">8</span><span class="p">)</span><span class="w"> </span><span class="n">z</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="k">fn</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step1</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="mi">8</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Using the straightforward generalization of the fold equation to mixed
steppers, one can verify the following equations.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">n</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="n">$</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">0</span><span class="w"></span>
<span class="n">n</span><span class="w"> </span><span class="n">`</span><span class="mi">3</span><span class="w"> </span><span class="n">O</span><span class="w"> </span><span class="n">$</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">24</span><span class="w"></span>
<span class="n">n</span><span class="w"> </span><span class="n">`</span><span class="mi">1</span><span class="w"> </span><span class="n">O</span><span class="w"> </span><span class="n">`</span><span class="mi">7</span><span class="w"> </span><span class="n">$</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">71</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>That is, we&#8217;ve defined a syntax for writing octal integer constants,
with a special syntax, <span class="monospaced">O</span>, for the zero digit (admittedly
contrived, since one could just write <span class="monospaced">&grave;0</span> instead of <span class="monospaced">O</span>).</p></div>
<div class="paragraph"><p>See <a href="NumericLiteral">NumericLiteral</a> for a practical extension of this approach that
supports numeric constants in any base and of any type.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_seemingly_dependent_types">(Seemingly) dependent types</h2>
<div class="sectionbody">
<div class="paragraph"><p>A normal list fold always returns the same type no matter what
elements are in the list or how long the list is.  Variable-argument
fold is more powerful, because the result type can vary based both on
the arguments that are passed and on their number.  This can provide
the illusion of dependent types.</p></div>
<div class="paragraph"><p>For example, consider the following.</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">f</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">fold</span><span class="w"> </span><span class="p">((),</span><span class="w"> </span><span class="n">id</span><span class="p">)</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">a</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step0</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="s">&quot;hello&quot;</span><span class="p">)</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">b</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step0</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="mi">13</span><span class="p">)</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">c</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step0</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="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">))</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Using the fold equation, one can verify the following equations.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">f</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">$</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="s">&quot;hello&quot;</span><span class="p">:</span><span class="w"> </span><span class="n">string</span><span class="w"></span>
<span class="n">f</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="n">$</span><span class="w"> </span><span class="p">=</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="n">f</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="n">$</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">):</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">int</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>That is, <span class="monospaced">f</span> returns a value of a different type depending on
whether it is applied to argument <span class="monospaced">a</span>, argument <span class="monospaced">b</span>, or
argument <span class="monospaced">c</span>.</p></div>
<div class="paragraph"><p>The following example shows how the type of a fold can depend on the
number of arguments.</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">grow</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">fold</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">l</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">l</span><span class="p">)</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">a</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step0</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="p">[</span><span class="n">x</span><span class="p">])</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Using the fold equation, one can verify the following equations.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">grow</span><span class="w"> </span><span class="n">$</span><span class="w"> </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="n">list</span><span class="w"></span>
<span class="n">grow</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">$</span><span class="w"> </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="n">list</span><span class="w"> </span><span class="n">list</span><span class="w"></span>
<span class="n">grow</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">$</span><span class="w"> </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="n">list</span><span class="w"> </span><span class="n">list</span><span class="w"> </span><span class="n">list</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Clearly, the result type of a call to the variable argument <span class="monospaced">grow</span>
function depends on the number of arguments that are passed.</p></div>
<div class="paragraph"><p>As a reminder, this is well-typed SML.  You can check it out in any
implementation.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_seemingly_dependently_typed_functional_results">(Seemingly) dependently-typed functional results</h2>
<div class="sectionbody">
<div class="paragraph"><p>Fold is especially useful when it returns a curried function whose
arity depends on the number of arguments.  For example, consider the
following.</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">makeSum</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">id</span><span class="p">,</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">I</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step0</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </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="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">f</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">i</span><span class="p">))</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The <span class="monospaced">makeSum</span> folder constructs a function whose arity depends on
the number of <span class="monospaced">I</span> arguments and that adds together all of its
arguments.  For example,
<span class="monospaced">makeSum I $</span> is of type <span class="monospaced">int -&gt; int</span> and
<span class="monospaced">makeSum I I $</span> is of type <span class="monospaced">int -&gt; int -&gt; int</span>.</p></div>
<div class="paragraph"><p>One can use the fold equation to verify that the <span class="monospaced">makeSum</span> works
correctly.  For example, one can easily check by hand the following
equations.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">makeSum</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="p">=</span><span class="w"> </span><span class="mi">1</span><span class="w"></span>
<span class="n">makeSum</span><span class="w"> </span><span class="n">I</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="mi">2</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">3</span><span class="w"></span>
<span class="n">makeSum</span><span class="w"> </span><span class="n">I</span><span class="w"> </span><span class="n">I</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="mi">2</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">6</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Returning a function becomes especially interesting when there are
steppers of different types.  For example, the following <span class="monospaced">makeSum</span>
folder constructs functions that sum integers and reals.</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">makeSum</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Foldr</span><span class="p">.</span><span class="n">foldr</span><span class="w"> </span><span class="p">(</span><span class="n">id</span><span class="p">,</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="mf">0.0</span><span class="p">)</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">I</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Foldr</span><span class="p">.</span><span class="n">step0</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">=&gt;</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="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">f</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">real</span><span class="w"> </span><span class="n">i</span><span class="p">))</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">R</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Foldr</span><span class="p">.</span><span class="n">step0</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="n">x</span><span class="p">:</span><span class="w"> </span><span class="n">real</span><span class="w"> </span><span class="p">=&gt;</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">f</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">r</span><span class="p">))</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>With these definitions, <span class="monospaced">makeSum I R $</span> is of type
<span class="monospaced">int -&gt; real -&gt; real</span> and <span class="monospaced">makeSum R I I $</span> is of type
<span class="monospaced">real -&gt; int -&gt; int -&gt; real</span>.  One can use the foldr equation to
check the following equations.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">makeSum</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="p">=</span><span class="w"> </span><span class="mf">1.0</span><span class="w"></span>
<span class="n">makeSum</span><span class="w"> </span><span class="n">I</span><span class="w"> </span><span class="n">R</span><span class="w"> </span><span class="n">$</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="mf">2.5</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mf">3.5</span><span class="w"></span>
<span class="n">makeSum</span><span class="w"> </span><span class="n">R</span><span class="w"> </span><span class="n">I</span><span class="w"> </span><span class="n">I</span><span class="w"> </span><span class="n">$</span><span class="w"> </span><span class="mf">1.5</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mf">6.5</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>We used <span class="monospaced">foldr</span> instead of <span class="monospaced">fold</span> for this so that the order
in which the specifiers <span class="monospaced">I</span> and <span class="monospaced">R</span> appear is the same as the
order in which the arguments appear.  Had we used <span class="monospaced">fold</span>, things
would have been reversed.</p></div>
<div class="paragraph"><p>An extension of this idea is sufficient to define <a href="Printf">Printf</a>-like
functions in SML.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_an_idiom_for_combining_steps">An idiom for combining steps</h2>
<div class="sectionbody">
<div class="paragraph"><p>It is sometimes useful to combine a number of steps together and name
them as a single step.  As a simple example, suppose that one often
sees an integer follower by a real in the <span class="monospaced">makeSum</span> example above.
One can define a new <em>compound step</em> <span class="monospaced">IR</span> as follows.</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">IR</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">u</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">fold</span><span class="w"> </span><span class="n">u</span><span class="w"> </span><span class="n">I</span><span class="w"> </span><span class="n">R</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>With this definition in place, one can verify the following.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">makeSum</span><span class="w"> </span><span class="n">IR</span><span class="w"> </span><span class="n">IR</span><span class="w"> </span><span class="n">$</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="mf">2.2</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="mf">4.4</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mf">10.6</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>In general, one can combine steps <span class="monospaced">s1</span>, <span class="monospaced">s2</span>, &#8230; <span class="monospaced">sn</span> as</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">fn</span><span class="w"> </span><span class="n">u</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">fold</span><span class="w"> </span><span class="n">u</span><span class="w"> </span><span class="n">s1</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">sn</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The following calculation shows why a compound step behaves as the
composition of its constituent steps.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">fold</span><span class="w"> </span><span class="n">u</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">u</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">u</span><span class="w"> </span><span class="n">s1</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">sn</span><span class="p">)</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">u</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">u</span><span class="w"> </span><span class="n">s1</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">sn</span><span class="p">)</span><span class="w"> </span><span class="n">u</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">u</span><span class="w"> </span><span class="n">s1</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">sn</span><span class="w"></span>
</pre></div></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_post_composition">Post composition</h2>
<div class="sectionbody">
<div class="paragraph"><p>Suppose we already have a function defined via fold,
<span class="monospaced">w = fold (a, f)</span>, and we would like to construct a new fold
function that is like <span class="monospaced">w</span>, but applies <span class="monospaced">g</span> to the result
produced by <span class="monospaced">w</span>.  This is similar to function composition, but we
can&#8217;t just do <span class="monospaced">g o w</span>, because we don&#8217;t want to use <span class="monospaced">g</span> until
<span class="monospaced">w</span> has been applied to all of its arguments and received the
end-of-arguments terminator <span class="monospaced">$</span>.</p></div>
<div class="paragraph"><p>More precisely, we want to define a post-composition function
<span class="monospaced">post</span> that satisfies the following equation.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">post</span><span class="w"> </span><span class="p">(</span><span class="n">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="n">s1</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="n">sn</span><span class="w"> </span><span class="n">$</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="p">(</span><span class="n">w</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">sn</span><span class="w"> </span><span class="n">$</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Here is the definition of <span class="monospaced">post</span>.</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">Fold</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">open</span><span class="w"> </span><span class="n">Fold</span><span class="w"></span>
<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">post</span><span class="w"> </span><span class="p">(</span><span class="n">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="n">s</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">w</span><span class="w"> </span><span class="p">(</span><span class="k">fn</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">h</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">s</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">g</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">h</span><span class="p">))</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The following calculations show that <span class="monospaced">post</span> satisfies the desired
equation, where <span class="monospaced">w = fold (a, f)</span>.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">post</span><span class="w"> </span><span class="p">(</span><span class="n">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="n">s</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">w</span><span class="w"> </span><span class="p">(</span><span class="k">fn</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">h</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">s</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">g</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">h</span><span class="p">))</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">fold</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">f</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="k">fn</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">h</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">s</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">g</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">h</span><span class="p">))</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="k">fn</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">h</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">s</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">g</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">h</span><span class="p">))</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">f</span><span class="p">)</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">s</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">g</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="p">=</span><span class="w"> </span><span class="n">fold</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">g</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="n">s</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Now, suppose <span class="monospaced">si = step0 hi</span> for <span class="monospaced">i</span> from <span class="monospaced">1</span> to <span class="monospaced">n</span>.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">post</span><span class="w"> </span><span class="p">(</span><span class="n">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="n">s1</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">sn</span><span class="w"> </span><span class="n">$</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">fold</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">g</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="n">s1</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">sn</span><span class="w"> </span><span class="n">$</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="n">g</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="p">(</span><span class="n">hn</span><span class="w"> </span><span class="p">(...</span><span class="w"> </span><span class="p">(</span><span class="n">h1</span><span class="w"> </span><span class="n">a</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="p">(</span><span class="n">f</span><span class="w"> </span><span class="p">(</span><span class="n">hn</span><span class="w"> </span><span class="p">(...</span><span class="w"> </span><span class="p">(</span><span class="n">h1</span><span class="w"> </span><span class="n">a</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="p">(</span><span class="n">fold</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">f</span><span class="p">)</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">sn</span><span class="w"> </span><span class="n">$</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="p">(</span><span class="n">w</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">sn</span><span class="w"> </span><span class="n">$</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>For a practical example of post composition, see <a href="ArrayLiteral">ArrayLiteral</a>.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_lift">Lift</h2>
<div class="sectionbody">
<div class="paragraph"><p>We now define a peculiar-looking function, <span class="monospaced">lift0</span>, that is,
equationally speaking, equivalent to the identity function on a step
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">lift0</span><span class="w"> </span><span class="n">s</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">f</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">fold</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">id</span><span class="p">)</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="n">$</span><span class="p">,</span><span class="w"> </span><span class="n">f</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Using the definitions, we can prove the following equation.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">fold</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">f</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">lift0</span><span class="w"> </span><span class="p">(</span><span class="n">step0</span><span class="w"> </span><span class="n">h</span><span class="p">))</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">fold</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">f</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">step0</span><span class="w"> </span><span class="n">h</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Here is the proof.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">fold</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">f</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">lift0</span><span class="w"> </span><span class="p">(</span><span class="n">step0</span><span class="w"> </span><span class="n">h</span><span class="p">))</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">lift0</span><span class="w"> </span><span class="p">(</span><span class="n">step0</span><span class="w"> </span><span class="n">h</span><span class="p">)</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">f</span><span class="p">)</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">fold</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">id</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">step0</span><span class="w"> </span><span class="n">h</span><span class="p">)</span><span class="w"> </span><span class="n">$</span><span class="p">,</span><span class="w"> </span><span class="n">f</span><span class="p">)</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">step0</span><span class="w"> </span><span class="n">h</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">id</span><span class="p">)</span><span class="w"> </span><span class="n">$</span><span class="p">,</span><span class="w"> </span><span class="n">f</span><span class="p">)</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">h</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">id</span><span class="p">)</span><span class="w"> </span><span class="n">$</span><span class="p">,</span><span class="w"> </span><span class="n">f</span><span class="p">)</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">$</span><span class="w"> </span><span class="p">(</span><span class="n">h</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">id</span><span class="p">),</span><span class="w"> </span><span class="n">f</span><span class="p">)</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">id</span><span class="w"> </span><span class="p">(</span><span class="n">h</span><span class="w"> </span><span class="n">a</span><span class="p">),</span><span class="w"> </span><span class="n">f</span><span class="p">)</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">h</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">f</span><span class="p">)</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">step0</span><span class="w"> </span><span class="n">h</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">f</span><span class="p">)</span><span class="w"></span>
<span class="p">=</span><span class="w"> </span><span class="n">fold</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">f</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">step0</span><span class="w"> </span><span class="n">h</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>If <span class="monospaced">lift0</span> is the identity, then why even define it?  The answer
lies in the typing of fold expressions, which we have, until now, left
unexplained.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_typing">Typing</h2>
<div class="sectionbody">
<div class="paragraph"><p>Perhaps the most surprising aspect of fold is that it can be checked
by the SML type system.  The types involved in fold expressions are
complex; fortunately type inference is able to deduce them.
Nevertheless, it is instructive to study the types of fold functions
and steppers.  More importantly, it is essential to understand the
typing aspects of fold in order to write down signatures of functions
defined using fold and step.</p></div>
<div class="paragraph"><p>Here is the <span class="monospaced">FOLD</span> signature, and a recapitulation of the entire
<span class="monospaced">Fold</span> structure, with additional type annotations.</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">FOLD</span><span class="w"> </span><span class="p">=</span><span class="w"></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;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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step</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="p">-&gt;</span><span class="w"> </span><span class="n">&#39;d</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;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">&#39;d</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;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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;d</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;a1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step0</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="p">(</span><span class="n">&#39;a1</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="p">(</span><span class="n">&#39;a2</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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">t</span><span class="p">)</span><span class="w"> </span><span class="n">step</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;a11</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a12</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step1</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="p">(</span><span class="n">&#39;a12</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">&#39;a11</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a2</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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">t</span><span class="p">)</span><span class="w"> </span><span class="n">step</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">fold</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="p">-&gt;</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">&#39;d</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">lift0</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">)</span><span class="w"> </span><span class="n">step0</span><span class="w"></span>
<span class="w">                 </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step0</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">post</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;b</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;c1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;d</span><span class="p">)</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;c1</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;c2</span><span class="p">)</span><span class="w"></span>
<span class="w">                </span><span class="p">-&gt;</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;c2</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;d</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">step0</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a1</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step0</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">step1</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a11</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;a12</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">)</span><span class="w"></span>
<span class="w">                 </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a11</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a12</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step1</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">Fold</span><span class="p">:&gt;</span><span class="w"> </span><span class="n">FOLD</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">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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step</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="p">-&gt;</span><span class="w"> </span><span class="n">&#39;d</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;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">&#39;d</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;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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;d</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;a1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step0</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="p">(</span><span class="n">&#39;a1</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="p">(</span><span class="n">&#39;a2</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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">t</span><span class="p">)</span><span class="w"> </span><span class="n">step</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;a11</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a12</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step1</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="p">(</span><span class="n">&#39;a12</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">&#39;a11</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a2</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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">t</span><span class="p">)</span><span class="w"> </span><span class="n">step</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">fold</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">f</span><span class="p">:</span><span class="w"> </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="w">               </span><span class="p">(</span><span class="n">g</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;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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step</span><span class="p">):</span><span class="w"> </span><span class="n">&#39;d</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="n">g</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">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">step0</span><span class="w"> </span><span class="p">(</span><span class="n">h</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a1</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">)</span><span class="w"></span>
<span class="w">                </span><span class="p">(</span><span class="n">a1</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a1</span><span class="p">,</span><span class="w"> </span><span class="n">f</span><span class="p">:</span><span class="w"> </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="p">(</span><span class="n">&#39;a2</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">&#39;d</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="w">         </span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">h</span><span class="w"> </span><span class="n">a1</span><span class="p">,</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">step1</span><span class="w"> </span><span class="p">(</span><span class="n">h</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a11</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;a12</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">)</span><span class="w"></span>
<span class="w">                </span><span class="p">(</span><span class="n">a12</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a12</span><span class="p">,</span><span class="w"> </span><span class="n">f</span><span class="p">:</span><span class="w"> </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="w">                </span><span class="p">(</span><span class="n">a11</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a11</span><span class="p">):</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a2</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">&#39;d</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="w">         </span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">h</span><span class="w"> </span><span class="p">(</span><span class="n">a11</span><span class="p">,</span><span class="w"> </span><span class="n">a12</span><span class="p">),</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">lift0</span><span class="w"> </span><span class="p">(</span><span class="n">s</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">)</span><span class="w"> </span><span class="n">step0</span><span class="p">)</span><span class="w"></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;a1</span><span class="p">,</span><span class="w"> </span><span class="n">f</span><span class="p">:</span><span class="w"> </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="p">(</span><span class="n">&#39;a2</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">&#39;d</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="w">         </span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">fold</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">id</span><span class="p">)</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="n">$</span><span class="p">,</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">post</span><span class="w"> </span><span class="p">(</span><span class="n">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;b</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;c1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">t</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="n">g</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;c1</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;c2</span><span class="p">)</span><span class="w"></span>
<span class="w">               </span><span class="p">(</span><span class="n">s</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;b</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;c2</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step</span><span class="p">):</span><span class="w"> </span><span class="n">&#39;d</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="n">w</span><span class="w"> </span><span class="p">(</span><span class="k">fn</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">h</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">s</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">g</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">h</span><span class="p">))</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>That&#8217;s a lot to swallow, so let&#8217;s walk through it one step at a time.
First, we have the definition of type <span class="monospaced">Fold.step</span>.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step</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="p">-&gt;</span><span class="w"> </span><span class="n">&#39;d</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>As a fold proceeds over its arguments, it maintains two things: the
accumulator, of type <span class="monospaced">'a</span>, and the finishing function, of type
<span class="monospaced">'b -&gt; 'c</span>.  Each step in the fold is a function that takes those
two pieces (i.e. <span class="monospaced">'a * ('b -&gt; 'c)</span> and does something to them
(i.e. produces <span class="monospaced">'d</span>).  The result type of the step is completely
left open to be filled in by type inference, as it is an arrow type
that is capable of consuming the rest of the arguments to the fold.</p></div>
<div class="paragraph"><p>A folder, of type <span class="monospaced">Fold.t</span>, is a function that consumes a single
step.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></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">&#39;d</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;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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;d</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Expanding out the type, we have:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></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">&#39;d</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;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="p">-&gt;</span><span class="w"> </span><span class="n">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;d</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>This shows that the only thing a folder does is to hand its
accumulator (<span class="monospaced">'a</span>) and finisher (<span class="monospaced">'b -&gt; 'c</span>) to the next step
(<span class="monospaced">'a * ('b -&gt; 'c) -&gt; 'd</span>).  If SML had <a href="FirstClassPolymorphism">first-class polymorphism</a>,
we would write the fold type as follows.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></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">Forall</span><span class="w"> </span><span class="n">&#39;d</span><span class="w"> </span><span class="err">. (&#39;a, &#39;b, &#39;c, &#39;d) step -&gt; &#39;d</span>
</pre></div></div></div>
<div class="paragraph"><p>This type definition shows that a folder had nothing to do with
the rest of the fold, it only deals with the next step.</p></div>
<div class="paragraph"><p>We now can understand the type of <span class="monospaced">fold</span>, which takes the initial
value of the accumulator and the finishing function, and constructs a
folder, i.e. a function awaiting the next step.</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">fold</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="p">-&gt;</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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="k">fun</span><span class="w"> </span><span class="n">fold</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">f</span><span class="p">:</span><span class="w"> </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="w">         </span><span class="p">(</span><span class="n">g</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;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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step</span><span class="p">):</span><span class="w"> </span><span class="n">&#39;d</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="n">g</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">f</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Continuing on, we have the type of step functions.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">type</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step0</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="p">(</span><span class="n">&#39;a1</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="p">(</span><span class="n">&#39;a2</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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">t</span><span class="p">)</span><span class="w"> </span><span class="n">step</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Expanding out the type a bit gives:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">type</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step0</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="n">&#39;a1</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="p">-&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a2</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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>So, a step function takes the accumulator (<span class="monospaced">'a1</span>) and finishing
function (<span class="monospaced">'b -&gt; 'c</span>), which will be passed to it by the previous
folder, and transforms them to a new folder.  This new folder has a
new accumulator (<span class="monospaced">'a2</span>) and the same finishing function.</p></div>
<div class="paragraph"><p>Again, imagining that SML had <a href="FirstClassPolymorphism">first-class polymorphism</a> makes the type
clearer.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">type</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">)</span><span class="w"> </span><span class="n">step0</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="n">Forall</span><span class="w"> </span><span class="p">(</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="err">. (&#39;a1, &#39;b, &#39;c, (&#39;a2, &#39;b, &#39;c) t) step</span>
</pre></div></div></div>
<div class="paragraph"><p>Thus, in essence, a <span class="monospaced">step0</span> function is a wrapper around a
function of type <span class="monospaced">'a1 -&gt; 'a2</span>, which is exactly what the
definition of <span class="monospaced">step0</span> does.</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">step0</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a1</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step0</span><span class="w"></span>
<span class="k">fun</span><span class="w"> </span><span class="n">step0</span><span class="w"> </span><span class="p">(</span><span class="n">h</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a1</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">)</span><span class="w"></span>
<span class="w">          </span><span class="p">(</span><span class="n">a1</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a1</span><span class="p">,</span><span class="w"> </span><span class="n">f</span><span class="p">:</span><span class="w"> </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="p">(</span><span class="n">&#39;a2</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">&#39;d</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="w">   </span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">h</span><span class="w"> </span><span class="n">a1</span><span class="p">,</span><span class="w"> </span><span class="n">f</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>It is not much beyond <span class="monospaced">step0</span> to understand <span class="monospaced">step1</span>.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">type</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a11</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a12</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step1</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="p">(</span><span class="n">&#39;a12</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">&#39;a11</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a2</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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">t</span><span class="p">)</span><span class="w"> </span><span class="n">step</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>A <span class="monospaced">step1</span> function takes the accumulator (<span class="monospaced">'a12</span>) and finisher
(<span class="monospaced">'b -&gt; 'c</span>) passed to it by the previous folder and transforms
them into a function that consumes the next argument (<span class="monospaced">'a11</span>) and
produces a folder that will continue the fold with a new accumulator
(<span class="monospaced">'a2</span>) and the same finisher.</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">step1</span><span class="w"> </span><span class="p">(</span><span class="n">h</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a11</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;a12</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">)</span><span class="w"></span>
<span class="w">          </span><span class="p">(</span><span class="n">a12</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a12</span><span class="p">,</span><span class="w"> </span><span class="n">f</span><span class="p">:</span><span class="w"> </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="w">          </span><span class="p">(</span><span class="n">a11</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;a11</span><span class="p">):</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a2</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">&#39;d</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="w">   </span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">h</span><span class="w"> </span><span class="p">(</span><span class="n">a11</span><span class="p">,</span><span class="w"> </span><span class="n">a12</span><span class="p">),</span><span class="w"> </span><span class="n">f</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>With <a href="FirstClassPolymorphism">first-class polymorphism</a>, a <span class="monospaced">step1</span> function is more clearly
seen as a wrapper around a binary function of type
<span class="monospaced">'a11 * 'a12 -&gt; 'a2</span>.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">type</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a11</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a12</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">)</span><span class="w"> </span><span class="n">step1</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="n">Forall</span><span class="w"> </span><span class="p">(</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="err">. (&#39;a12, &#39;b, &#39;c, &#39;a11 -&gt; (&#39;a2, &#39;b, &#39;c) t) step</span>
</pre></div></div></div>
<div class="paragraph"><p>The type of <span class="monospaced">post</span> is clear: it takes a folder with a finishing
function that produces type <span class="monospaced">'c1</span>, and a function of type
<span class="monospaced">'c1 -&gt; 'c2</span> to postcompose onto the folder.  It returns a new
folder with a finishing function that produces type <span class="monospaced">'c2</span>.</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">post</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;b</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;c1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;d</span><span class="p">)</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;c1</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;c2</span><span class="p">)</span><span class="w"></span>
<span class="w">          </span><span class="p">-&gt;</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;c2</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="k">fun</span><span class="w"> </span><span class="n">post</span><span class="w"> </span><span class="p">(</span><span class="n">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;b</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;c1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">t</span><span class="p">,</span><span class="w"></span>
<span class="w">          </span><span class="n">g</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;c1</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;c2</span><span class="p">)</span><span class="w"></span>
<span class="w">         </span><span class="p">(</span><span class="n">s</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;b</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;c2</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step</span><span class="p">):</span><span class="w"> </span><span class="n">&#39;d</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="n">w</span><span class="w"> </span><span class="p">(</span><span class="k">fn</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">h</span><span class="p">)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">s</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">g</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="n">h</span><span class="p">))</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>We will return to <span class="monospaced">lift0</span> after an example.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_an_example_typing">An example typing</h2>
<div class="sectionbody">
<div class="paragraph"><p>Let&#8217;s type check our simplest example, a variable-argument fold.
Recall that we have a folder <span class="monospaced">f</span> and a stepper <span class="monospaced">a</span> defined as
follows.</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">f</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">fold</span><span class="w"> </span><span class="p">((),</span><span class="w"> </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="p">())</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">a</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step0</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="p">())</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Since the accumulator and finisher are uninteresting, we&#8217;ll use some
abbreviations to simplify things.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">type</span><span class="w"> </span><span class="n">&#39;d</span><span class="w"> </span><span class="n">step</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="n">unit</span><span class="p">,</span><span class="w"> </span><span class="n">unit</span><span class="p">,</span><span class="w"> </span><span class="n">unit</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step</span><span class="w"></span>
<span class="k">type</span><span class="w"> </span><span class="n">&#39;d</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">&#39;d</span><span class="w"> </span><span class="n">step</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;d</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>With these abbreviations, <span class="monospaced">f</span> and <span class="monospaced">a</span> have the following polymorphic
types.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">f</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;d</span><span class="w"> </span><span class="n">fold</span><span class="w"></span>
<span class="n">a</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;d</span><span class="w"> </span><span class="n">step</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Suppose we want to type check</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">f</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">a</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">unit</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>As a reminder, the fully parenthesized expression is</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="p">((((</span><span class="n">f</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="n">$</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The observation that we will use repeatedly is that for any type
<span class="monospaced">z</span>, if <span class="monospaced">f: z fold</span> and <span class="monospaced">s: z step</span>, then <span class="monospaced">f s: z</span>.
So, if we want</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">f</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="n">$:</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>then we must have</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">f</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">a</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="n">fold</span><span class="w"></span>
<span class="n">$:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="n">step</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Applying the observation again, we must have</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">f</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">a</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">fold</span><span class="w"></span>
<span class="n">a</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">step</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Applying the observation two more times leads to the following type
derivation.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">f</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">fold</span><span class="w">  </span><span class="n">a</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">step</span><span class="w"></span>
<span class="n">f</span><span class="w"> </span><span class="n">a</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">fold</span><span class="w">     </span><span class="n">a</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">step</span><span class="w"></span>
<span class="n">f</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">a</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">fold</span><span class="w">        </span><span class="n">a</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">step</span><span class="w"></span>
<span class="n">f</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">a</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="n">fold</span><span class="w">           </span><span class="n">$:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="n">step</span><span class="w"></span>
<span class="n">f</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">a</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">unit</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>So, each application is a fold that consumes the next step, producing
a fold of one smaller type.</p></div>
<div class="paragraph"><p>One can expand some of the type definitions in <span class="monospaced">f</span> to see that it is
indeed a function that takes four curried arguments, each one a step
function.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="n">f</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">step</span><span class="w"></span>
<span class="w">   </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">step</span><span class="w"></span>
<span class="w">   </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="n">fold</span><span class="w"> </span><span class="n">step</span><span class="w"></span>
<span class="w">   </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="n">step</span><span class="w"></span>
<span class="w">   </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>This example shows why we must eta expand uses of <span class="monospaced">fold</span> and <span class="monospaced">step0</span>
to work around the value restriction and make folders and steppers
polymorphic.  The type of a fold function like <span class="monospaced">f</span> depends on the
number of arguments, and so will vary from use to use.  Similarly,
each occurrence of an argument like <span class="monospaced">a</span> has a different type,
depending on the number of remaining arguments.</p></div>
<div class="paragraph"><p>This example also shows that the type of a folder, when fully
expanded, is exponential in the number of arguments: there are as many
nested occurrences of the <span class="monospaced">fold</span> type constructor as there are
arguments, and each occurrence duplicates its type argument.  One can
observe this exponential behavior in a type checker that doesn&#8217;t share
enough of the representation of types (e.g. one that represents types
as trees rather than directed acyclic graphs).</p></div>
<div class="paragraph"><p>Generalizing this type derivation to uses of fold where the
accumulator and finisher are more interesting is straightforward.  One
simply includes the type of the accumulator, which may change, for
each step, and the type of the finisher, which doesn&#8217;t change from
step to step.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_typing_lift">Typing lift</h2>
<div class="sectionbody">
<div class="paragraph"><p>The lack of <a href="FirstClassPolymorphism">first-class polymorphism</a> in SML
causes problems if one wants to use a step in a first-class way.
Consider the following <span class="monospaced">double</span> function, which takes a step, <span class="monospaced">s</span>, and
produces a composite step that does <span class="monospaced">s</span> twice.</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">double</span><span class="w"> </span><span class="n">s</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">u</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">fold</span><span class="w"> </span><span class="n">u</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="n">s</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The definition of <span class="monospaced">double</span> is not type correct.  The problem is that
the type of a step depends on the number of remaining arguments but
that the parameter <span class="monospaced">s</span> is not polymorphic, and so can not be used in
two different positions.</p></div>
<div class="paragraph"><p>Fortunately, we can define a function, <span class="monospaced">lift0</span>, that takes a monotyped
step function and <em>lifts</em> it into a polymorphic step function.  This
is apparent in the type of <span class="monospaced">lift0</span>.</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">lift0</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">)</span><span class="w"> </span><span class="n">step0</span><span class="w"></span>
<span class="w">           </span><span class="p">-&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">step0</span><span class="w"></span>
<span class="k">fun</span><span class="w"> </span><span class="n">lift0</span><span class="w"> </span><span class="p">(</span><span class="n">s</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;a2</span><span class="p">)</span><span class="w"> </span><span class="n">step0</span><span class="p">)</span><span class="w"></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;a1</span><span class="p">,</span><span class="w"> </span><span class="n">f</span><span class="p">:</span><span class="w"> </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="p">(</span><span class="n">&#39;a2</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">&#39;d</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="w">   </span><span class="n">fold</span><span class="w"> </span><span class="p">(</span><span class="n">fold</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">id</span><span class="p">)</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="n">$</span><span class="p">,</span><span class="w"> </span><span class="n">f</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The following definition of <span class="monospaced">double</span> uses <span class="monospaced">lift0</span>, appropriately eta
wrapped, to fix the problem.</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">double</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="k">let</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">s</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">lift0</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="n">z</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">fn</span><span class="w"> </span><span class="n">u</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">fold</span><span class="w"> </span><span class="n">u</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="n">s</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>With that definition of <span class="monospaced">double</span> in place, we can use it as in the
following example.</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">f</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">fold</span><span class="w"> </span><span class="p">((),</span><span class="w"> </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="p">())</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">a</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step0</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="p">())</span><span class="w"> </span><span class="n">z</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="k">fn</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">double</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">z</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="n">f</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">a2</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">a2</span><span class="w"> </span><span class="n">$</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Of course, we must eta wrap the call <span class="monospaced">double</span> in order to use its
result, which is a step function, polymorphically.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_hiding_the_type_of_the_accumulator">Hiding the type of the accumulator</h2>
<div class="sectionbody">
<div class="paragraph"><p>For clarity and to avoid mistakes, it can be useful to hide the type
of the accumulator in a fold.  Reworking the simple variable-argument
example to do this leads to the following.</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">S</span><span class="p">:&gt;</span><span class="w"></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">ac</span><span class="w"></span>
<span class="w">     </span><span class="k">val</span><span class="w"> </span><span class="n">f</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">ac</span><span class="p">,</span><span class="w"> </span><span class="n">ac</span><span class="p">,</span><span class="w"> </span><span class="n">unit</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</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">s</span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">ac</span><span class="p">,</span><span class="w"> </span><span class="n">ac</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">&#39;d</span><span class="p">)</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step0</span><span class="w"></span>
<span class="w">  </span><span class="k">end</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">type</span><span class="w"> </span><span class="n">ac</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
<span class="w">     </span><span class="k">val</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">fn</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">fold</span><span class="w"> </span><span class="p">((),</span><span class="w"> </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="p">())</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
<span class="w">     </span><span class="k">val</span><span class="w"> </span><span class="n">s</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">z</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">Fold</span><span class="p">.</span><span class="n">step0</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="p">())</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
<span class="w">  </span><span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The idea is to name the accumulator type and use opaque signature
matching to make it abstract.  This can prevent improper manipulation
of the accumulator by client code and ensure invariants that the
folder and stepper would like to maintain.</p></div>
<div class="paragraph"><p>For a practical example of this technique, see <a href="ArrayLiteral">ArrayLiteral</a>.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_also_see">Also see</h2>
<div class="sectionbody">
<div class="paragraph"><p>Fold has a number of practical applications.  Here are some of them.</p></div>
<div class="ulist"><ul>
<li>
<p>
<a href="ArrayLiteral">ArrayLiteral</a>
</p>
</li>
<li>
<p>
<a href="Fold01N">Fold01N</a>
</p>
</li>
<li>
<p>
<a href="FunctionalRecordUpdate">FunctionalRecordUpdate</a>
</p>
</li>
<li>
<p>
<a href="NumericLiteral">NumericLiteral</a>
</p>
</li>
<li>
<p>
<a href="OptionalArguments">OptionalArguments</a>
</p>
</li>
<li>
<p>
<a href="Printf">Printf</a>
</p>
</li>
<li>
<p>
<a href="VariableArityPolymorphism">VariableArityPolymorphism</a>
</p>
</li>
</ul></div>
<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="StaticSum">StaticSum</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>
