<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.8">
<title>Fold01N</title>
<link rel="stylesheet" href="./asciidoc.css" type="text/css">
<link rel="stylesheet" href="./pygments.css" type="text/css">


<script type="text/javascript" src="./asciidoc.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
asciidoc.install();
/*]]>*/
</script>
<link rel="stylesheet" href="./mlton.css" type="text/css"/>
</head>
<body class="article">
<div id="banner">
<div id="banner-home">
<a href="./Home">MLton 20130715</a>
</div>
</div>
<div id="header">
<h1>Fold01N</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>A common use pattern of <a href="Fold">Fold</a> is to define a variable-arity
function that combines multiple arguments together using a binary
function.  It is slightly tricky to do this directly using fold,
because of the special treatment required for the case of zero or one
argument.  Here is a structure, <span class="monospaced">Fold01N</span>, that solves the problem
once and for all, and eases the definition of such functions.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">structure</span><span class="w"> </span><span class="n">Fold01N</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">finish</span><span class="p">,</span><span class="w"> </span><span class="n">start</span><span class="p">,</span><span class="w"> </span><span class="n">zero</span><span class="p">}</span><span class="w"> </span><span class="p">=</span><span class="w"></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="n">finish</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="n">zero</span><span class="p">,</span><span class="w"> </span><span class="n">start</span><span class="p">),</span><span class="w"></span>
<span class="w">                    </span><span class="k">fn</span><span class="w"> </span><span class="p">(</span><span class="n">finish</span><span class="p">,</span><span class="w"> </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">_)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">finish</span><span class="w"> </span><span class="p">(</span><span class="n">p</span><span class="w"> </span><span class="p">()))</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">step0</span><span class="w"> </span><span class="p">{</span><span class="n">combine</span><span class="p">,</span><span class="w"> </span><span class="n">input</span><span class="p">}</span><span class="w"> </span><span class="p">=</span><span class="w"></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="n">finish</span><span class="p">,</span><span class="w"> </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">=&gt;</span><span class="w"></span>
<span class="w">                     </span><span class="p">(</span><span class="n">finish</span><span class="p">,</span><span class="w"></span>
<span class="w">                      </span><span class="n">finish</span><span class="p">,</span><span class="w"></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="n">f</span><span class="w"> </span><span class="n">input</span><span class="p">,</span><span class="w"></span>
<span class="w">                      </span><span class="k">fn</span><span class="w"> </span><span class="n">x&#39;</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">combine</span><span class="w"> </span><span class="p">(</span><span class="n">f</span><span class="w"> </span><span class="n">input</span><span class="p">,</span><span class="w"> </span><span class="n">x&#39;</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">combine</span><span class="p">}</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="n">input</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="n">step0</span><span class="w"> </span><span class="p">{</span><span class="n">combine</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">combine</span><span class="p">,</span><span class="w"> </span><span class="n">input</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">input</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>If one has a value <span class="monospaced">zero</span>, and functions <span class="monospaced">start</span>, <span class="monospaced">c</span>, and <span class="monospaced">finish</span>,
then one can define a variable-arity function <span class="monospaced">f</span> and stepper
<span class="monospaced">&grave;</span> as follows.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><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">Fold01N</span><span class="p">.</span><span class="n">fold</span><span class="w"> </span><span class="p">{</span><span class="n">finish</span><span class="w"> </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">start</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">start</span><span class="p">,</span><span class="w"> </span><span class="n">zero</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">zero</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">Fold01N</span><span class="p">.</span><span class="n">step1</span><span class="w"> </span><span class="p">{</span><span class="n">combine</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">c</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>One can then use the fold equation to prove the following equations.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="n">f</span><span class="w"> </span><span class="n">$</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">zero</span><span class="w"></span>
<span class="n">f</span><span class="w"> </span><span class="n">`a1</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">start</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="w"> </span><span class="n">`a1</span><span class="w"> </span><span class="n">`a2</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">c</span><span class="w"> </span><span class="p">(</span><span class="n">start</span><span class="w"> </span><span class="n">a1</span><span class="p">,</span><span class="w"> </span><span class="n">a2</span><span class="p">))</span><span class="w"></span>
<span class="n">f</span><span class="w"> </span><span class="n">`a1</span><span class="w"> </span><span class="n">`a2</span><span class="w"> </span><span class="n">`a3</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">c</span><span class="w"> </span><span class="p">(</span><span class="n">c</span><span class="w"> </span><span class="p">(</span><span class="n">start</span><span class="w"> </span><span class="n">a1</span><span class="p">,</span><span class="w"> </span><span class="n">a2</span><span class="p">),</span><span class="w"> </span><span class="n">a3</span><span class="p">))</span><span class="w"></span>
<span class="p">...</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>For an example of <span class="monospaced">Fold01N</span>, see <a href="VariableArityPolymorphism">VariableArityPolymorphism</a>.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_typing_fold01n">Typing Fold01N</h2>
<div class="sectionbody">
<div class="paragraph"><p>Here is the signature for <span class="monospaced">Fold01N</span>.  We use a trick to avoid having
to duplicate the definition of some rather complex types in both the
signature and the structure.  We first define the types in a
structure.  Then, we define them via type re-definitions in the
signature, and via <span class="monospaced">open</span> in the full structure.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">structure</span><span class="w"> </span><span class="n">Fold01N</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;input</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;accum1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;accum2</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;answer</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;zero</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;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">&#39;e</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="p">((</span><span class="n">&#39;zero</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;zero</span><span class="p">)</span><span class="w"></span>
<span class="w">          </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;accum2</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;answer</span><span class="p">)</span><span class="w"></span>
<span class="w">          </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;zero</span><span class="p">)</span><span class="w"></span>
<span class="w">          </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;input</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;accum1</span><span class="p">),</span><span class="w"></span>
<span class="w">          </span><span class="p">(</span><span class="n">&#39;a</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">)</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;c</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="p">)</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;d</span><span class="p">,</span><span class="w"></span>
<span class="w">          </span><span class="n">&#39;b</span><span class="p">,</span><span class="w"></span>
<span class="w">          </span><span class="n">&#39;e</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">type</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;input1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;accum1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;input2</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;accum2</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;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">&#39;e</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;f</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;a</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;b</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;c</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;input1</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;accum1</span><span class="p">),</span><span class="w"></span>
<span class="w">          </span><span class="n">&#39;b</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;b</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;accum1</span><span class="p">)</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;input2</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;accum2</span><span class="p">),</span><span class="w"></span>
<span class="w">          </span><span class="n">&#39;d</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;e</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;f</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">type</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;accum1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;input</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;accum2</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;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">&#39;e</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;f</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;g</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;a</span><span class="p">,</span><span class="w"></span>
<span class="w">          </span><span class="n">&#39;b</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;c</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;d</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;a</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;accum1</span><span class="p">),</span><span class="w"></span>
<span class="w">          </span><span class="n">&#39;c</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;c</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;accum1</span><span class="p">)</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">&#39;input</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;accum2</span><span class="p">),</span><span class="w"></span>
<span class="w">          </span><span class="n">&#39;e</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;f</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;g</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="w">   </span><span class="k">end</span><span class="w"></span>

<span class="k">signature</span><span class="w"> </span><span class="n">FOLD_01N</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">&#39;e</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;f</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;g</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;h</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;i</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;j</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="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">&#39;e</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;f</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;g</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;h</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;i</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;j</span><span class="p">)</span><span class="w"> </span><span class="n">Fold01N</span><span class="p">.</span><span class="n">t</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">&#39;e</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;f</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;g</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;h</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;i</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;j</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;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">&#39;e</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;f</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;g</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;h</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;i</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;j</span><span class="p">)</span><span class="w"> </span><span class="n">Fold01N</span><span class="p">.</span><span class="n">step0</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">&#39;e</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;f</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;g</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;h</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;i</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;j</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;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">&#39;e</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;f</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;g</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;h</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;i</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;j</span><span class="p">)</span><span class="w"> </span><span class="n">Fold01N</span><span class="p">.</span><span class="n">step1</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="w">         </span><span class="p">{</span><span class="n">finish</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;accum2</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;answer</span><span class="p">,</span><span class="w"></span>
<span class="w">          </span><span class="n">start</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;input</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;accum1</span><span class="p">,</span><span class="w"></span>
<span class="w">          </span><span class="n">zero</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;zero</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;input</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;accum1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;accum2</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;answer</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;zero</span><span class="p">,</span><span class="w"></span>
<span class="w">             </span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;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">&#39;e</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="w">         </span><span class="p">{</span><span class="n">combine</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;accum1</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;input2</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;accum2</span><span class="p">,</span><span class="w"></span>
<span class="w">          </span><span class="n">input</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;input1</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;input1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;accum1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;input2</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;accum2</span><span class="p">,</span><span class="w"></span>
<span class="w">             </span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;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">&#39;e</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;f</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="w">         </span><span class="p">{</span><span class="n">combine</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;accum1</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">&#39;input</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;accum2</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;accum1</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;input</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;accum2</span><span class="p">,</span><span class="w"></span>
<span class="w">             </span><span class="n">&#39;a</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;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">&#39;e</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;f</span><span class="p">,</span><span class="w"> </span><span class="n">&#39;g</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">Fold01N</span><span class="p">:</span><span class="w"> </span><span class="n">FOLD_01N</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">Fold01N</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">finish</span><span class="p">,</span><span class="w"> </span><span class="n">start</span><span class="p">,</span><span class="w"> </span><span class="n">zero</span><span class="p">}</span><span class="w"> </span><span class="p">=</span><span class="w"></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="n">finish</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="n">zero</span><span class="p">,</span><span class="w"> </span><span class="n">start</span><span class="p">),</span><span class="w"></span>
<span class="w">                    </span><span class="k">fn</span><span class="w"> </span><span class="p">(</span><span class="n">finish</span><span class="p">,</span><span class="w"> </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">_)</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">finish</span><span class="w"> </span><span class="p">(</span><span class="n">p</span><span class="w"> </span><span class="p">()))</span><span class="w"></span>

<span class="w">      </span><span class="k">fun</span><span class="w"> </span><span class="n">step0</span><span class="w"> </span><span class="p">{</span><span class="n">combine</span><span class="p">,</span><span class="w"> </span><span class="n">input</span><span class="p">}</span><span class="w"> </span><span class="p">=</span><span class="w"></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="n">finish</span><span class="p">,</span><span class="w"> </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">=&gt;</span><span class="w"></span>
<span class="w">                     </span><span class="p">(</span><span class="n">finish</span><span class="p">,</span><span class="w"></span>
<span class="w">                      </span><span class="n">finish</span><span class="p">,</span><span class="w"></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="n">f</span><span class="w"> </span><span class="n">input</span><span class="p">,</span><span class="w"></span>
<span class="w">                      </span><span class="k">fn</span><span class="w"> </span><span class="n">x&#39;</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">combine</span><span class="w"> </span><span class="p">(</span><span class="n">f</span><span class="w"> </span><span class="n">input</span><span class="p">,</span><span class="w"> </span><span class="n">x&#39;</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">combine</span><span class="p">}</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="n">input</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">         </span><span class="n">step0</span><span class="w"> </span><span class="p">{</span><span class="n">combine</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">combine</span><span class="p">,</span><span class="w"> </span><span class="n">input</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">input</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>
</div>
</div>
<div id="footnotes"><hr></div>
<div id="footer">
<div id="footer-text">
</div>
<div id="footer-badges">
</div>
</div>
</body>
</html>
