<!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>FunctionalRecordUpdate</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>FunctionalRecordUpdate</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>Functional record update is the copying of a record while replacing
the values of some of the fields.  <a href="StandardML">Standard ML</a> does not
have explicit syntax for functional record update.  We will show below
how to implement functional record update in SML, with a little
boilerplate code.</p></div>
<div class="paragraph"><p>As an example, the functional update of the record</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="p">{</span><span class="n">a</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">b</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">14</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">15</span><span class="p">}</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>with <span class="monospaced">c = 16</span> yields a new record</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="p">{</span><span class="n">a</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">b</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">14</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">16</span><span class="p">}</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Functional record update also makes sense with multiple simultaneous
updates.  For example, the functional update of the record above with
<span class="monospaced">a = 18, c = 19</span> yields a new record</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="p">{</span><span class="n">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">18</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="mi">14</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">19</span><span class="p">}</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>One could easily imagine an extension of the SML that supports
functional record update.  For example</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="n">e</span><span class="w"> </span><span class="k">with</span><span class="w"> </span><span class="p">{</span><span class="n">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">16</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="mi">17</span><span class="p">}</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>would create a copy of the record denoted by <span class="monospaced">e</span> with field <span class="monospaced">a</span>
replaced with <span class="monospaced">16</span> and <span class="monospaced">b</span> replaced with <span class="monospaced">17</span>.</p></div>
<div class="paragraph"><p>Since there is no such syntax in SML, we now show how to implement
functional record update directly.  We first give a simple
implementation that has a number of problems.  We then give an
advanced implementation, that, while complex underneath, is a reusable
library that admits simple use.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_simple_implementation">Simple implementation</h2>
<div class="sectionbody">
<div class="paragraph"><p>To support functional record update on the record type</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><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">b</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;b</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="p">:</span><span class="w"> </span><span class="n">&#39;c</span><span class="p">}</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>first, define an update function for each component.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">fun</span><span class="w"> </span><span class="n">withA</span><span class="w"> </span><span class="p">({</span><span class="n">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">_,</span><span class="w"> </span><span class="n">b</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">a</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="p">=</span><span class="w"> </span><span class="n">a</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">b</span><span class="p">,</span><span class="w"> </span><span class="n">c</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="k">fun</span><span class="w"> </span><span class="n">withB</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">b</span><span class="w"> </span><span class="p">=</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">b</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="p">=</span><span class="w"> </span><span class="n">a</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">b</span><span class="p">,</span><span class="w"> </span><span class="n">c</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="k">fun</span><span class="w"> </span><span class="n">withC</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">b</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">=</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="p">=</span><span class="w"> </span><span class="p">{</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="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">b</span><span class="p">,</span><span class="w"> </span><span class="n">c</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>
</pre></div></div></div>
<div class="paragraph"><p>Then, one can express <span class="monospaced">e with {a = 16, b = 17}</span> as</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="n">withB</span><span class="w"> </span><span class="p">(</span><span class="n">withA</span><span class="w"> </span><span class="p">(</span><span class="n">e</span><span class="p">,</span><span class="w"> </span><span class="mi">16</span><span class="p">),</span><span class="w"> </span><span class="mi">17</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>With infix notation</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">infix</span><span class="w"> </span><span class="n">withA</span><span class="w"> </span><span class="n">withB</span><span class="w"> </span><span class="n">withC</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>the syntax is almost as concise as a language extension.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="n">e</span><span class="w"> </span><span class="n">withA</span><span class="w"> </span><span class="mi">16</span><span class="w"> </span><span class="n">withB</span><span class="w"> </span><span class="mi">17</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>This approach suffers from the fact that the amount of boilerplate
code is quadratic in the number of record fields.  Furthermore,
changing, adding, or deleting a field requires time proportional to
the number of fields (because each <span class="monospaced">with<em>&lt;L&gt;</em></span> function must be
changed).  It is also annoying to have to define a <span class="monospaced">with<em>&lt;L&gt;</em></span>
function, possibly with a fixity declaration, for each field.</p></div>
<div class="paragraph"><p>Fortunately, there is a solution to these problems.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_advanced_implementation">Advanced implementation</h2>
<div class="sectionbody">
<div class="paragraph"><p>Using <a href="Fold">Fold</a> one can define a family of <span class="monospaced">makeUpdate<em>&lt;N&gt;</em></span>
functions and single <em>update</em> operator <span class="monospaced">U</span> so that one can define a
functional record update function for any record type simply by
specifying a (trivial) isomorphism between that type and function
argument list.  For example, suppose that we would like to do
functional record update on records with fields <span class="monospaced">a</span> and <span class="monospaced">b</span>.  Then one
defines a function <span class="monospaced">updateAB</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">updateAB</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">fn</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="p">=&gt;</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">fun</span><span class="w"> </span><span class="n">from</span><span class="w"> </span><span class="n">v1</span><span class="w"> </span><span class="n">v2</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="p">=</span><span class="w"> </span><span class="n">v1</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">v2</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">to</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">{</span><span class="n">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">v1</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">v2</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">v1</span><span class="w"> </span><span class="n">v2</span><span class="w"></span>
<span class="w">   </span><span class="k">in</span><span class="w"></span>
<span class="w">      </span><span class="n">makeUpdate2</span><span class="w"> </span><span class="p">(</span><span class="n">from</span><span class="p">,</span><span class="w"> </span><span class="n">from</span><span class="p">,</span><span class="w"> </span><span class="n">to</span><span class="p">)</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></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">from</span> (think <em>from function arguments</em>) and <span class="monospaced">to</span> (think
<em>to function arguements</em>) specify an isomorphism between <span class="monospaced">a</span>,<span class="monospaced">b</span>
records and function arguments.  There is a second use of <span class="monospaced">from</span> to
work around the lack of
<a href="FirstClassPolymorphism">first-class polymorphism</a> in SML.</p></div>
<div class="paragraph"><p>With the definition of <span class="monospaced">updateAB</span> in place, the following expressions
are valid.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="n">updateAB</span><span class="w"> </span><span class="p">{</span><span class="n">a</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">b</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="p">(</span><span class="n">set</span><span class="p">#</span><span class="n">b</span><span class="w"> </span><span class="s">&quot;goodbye&quot;</span><span class="p">)</span><span class="w"> </span><span class="n">$</span><span class="w"></span>
<span class="n">updateAB</span><span class="w"> </span><span class="p">{</span><span class="n">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mf">13.5</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">true</span><span class="p">}</span><span class="w"> </span><span class="p">(</span><span class="n">set</span><span class="p">#</span><span class="n">b</span><span class="w"> </span><span class="n">false</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">set</span><span class="p">#</span><span class="n">a</span><span class="w"> </span><span class="mf">12.5</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>As another example, suppose that we would like to do functional record
update on records with fields <span class="monospaced">b</span>, <span class="monospaced">c</span>, and <span class="monospaced">d</span>.  Then one defines a
function <span class="monospaced">updateBCD</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">updateBCD</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">fn</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="p">=&gt;</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">fun</span><span class="w"> </span><span class="n">from</span><span class="w"> </span><span class="n">v1</span><span class="w"> </span><span class="n">v2</span><span class="w"> </span><span class="n">v3</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">{</span><span class="n">b</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">v1</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">v2</span><span class="p">,</span><span class="w"> </span><span class="n">d</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">v3</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">to</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">{</span><span class="n">b</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">v1</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">v2</span><span class="p">,</span><span class="w"> </span><span class="n">d</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">v3</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">v1</span><span class="w"> </span><span class="n">v2</span><span class="w"> </span><span class="n">v3</span><span class="w"></span>
<span class="w">   </span><span class="k">in</span><span class="w"></span>
<span class="w">      </span><span class="n">makeUpdate3</span><span class="w"> </span><span class="p">(</span><span class="n">from</span><span class="p">,</span><span class="w"> </span><span class="n">from</span><span class="p">,</span><span class="w"> </span><span class="n">to</span><span class="p">)</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>
<span class="w">   </span><span class="n">z</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>With the definition of <span class="monospaced">updateBCD</span> in place, the following expression
is valid.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="n">updateBCD</span><span class="w"> </span><span class="p">{</span><span class="n">b</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">c</span><span class="w"> </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">d</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">3</span><span class="p">}</span><span class="w"> </span><span class="p">(</span><span class="n">set</span><span class="p">#</span><span class="n">c</span><span class="w"> </span><span class="mi">4</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">set</span><span class="p">#</span><span class="n">c</span><span class="w"> </span><span class="mi">5</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>Note that not all fields need be updated and that the same field may
be updated multiple times.  Further note that the same <span class="monospaced">set</span> operator
is used for all update functions (in the above, for both <span class="monospaced">updateAB</span>
and <span class="monospaced">updateBCD</span>).</p></div>
<div class="paragraph"><p>In general, to define a functional-record-update function on records
with fields <span class="monospaced">f1</span>, <span class="monospaced">f2</span>, &#8230;, <span class="monospaced">fN</span>, use the following template.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">val</span><span class="w"> </span><span class="n">update</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">fn</span><span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="p">=&gt;</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">fun</span><span class="w"> </span><span class="n">from</span><span class="w"> </span><span class="n">v1</span><span class="w"> </span><span class="n">v2</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="n">vn</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">{</span><span class="n">f1</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">v1</span><span class="p">,</span><span class="w"> </span><span class="n">f2</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">v2</span><span class="p">,</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="n">vn</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">to</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">{</span><span class="n">f1</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">v1</span><span class="p">,</span><span class="w"> </span><span class="n">f2</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">v2</span><span class="p">,</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="n">vn</span><span class="p">}</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">v1</span><span class="w"> </span><span class="n">v2</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="n">vn</span><span class="w"></span>
<span class="w">   </span><span class="k">in</span><span class="w"></span>
<span class="w">      </span><span class="n">makeUpdateN</span><span class="w"> </span><span class="p">(</span><span class="n">from</span><span class="p">,</span><span class="w"> </span><span class="n">from</span><span class="p">,</span><span class="w"> </span><span class="n">to</span><span class="p">)</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>
<span class="w">   </span><span class="n">z</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>With this, one can update a record as follows.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="n">update</span><span class="w"> </span><span class="p">{</span><span class="n">f1</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">v1</span><span class="p">,</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="n">vn</span><span class="p">}</span><span class="w"> </span><span class="p">(</span><span class="n">set</span><span class="p">#</span><span class="n">fi1</span><span class="w"> </span><span class="n">vi1</span><span class="p">)</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="p">(</span><span class="n">set</span><span class="p">#</span><span class="n">fim</span><span class="w"> </span><span class="n">vim</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="_the_span_class_monospaced_functionalrecordupdate_span_structure">The <span class="monospaced">FunctionalRecordUpdate</span> structure</h2>
<div class="sectionbody">
<div class="paragraph"><p>Here is the implementation of functional record update.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">structure</span><span class="w"> </span><span class="n">FunctionalRecordUpdate</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">local</span><span class="w"></span>
<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">next</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="p">,</span><span class="w"> </span><span class="n">z</span><span class="p">)</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="p">(</span><span class="n">f</span><span class="w"> </span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">z</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">f1</span><span class="w"> </span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="w"> </span><span class="n">z</span><span class="p">)</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">f</span><span class="w"> </span><span class="p">(</span><span class="n">z</span><span class="w"> </span><span class="n">x</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">f2</span><span class="w">  </span><span class="n">z</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">next</span><span class="w"> </span><span class="n">f1</span><span class="w">  </span><span class="n">z</span><span class="w"></span>
<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">f3</span><span class="w">  </span><span class="n">z</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">next</span><span class="w"> </span><span class="n">f2</span><span class="w">  </span><span class="n">z</span><span class="w"></span>

<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">c0</span><span class="w">  </span><span class="n">from</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">from</span><span class="w"></span>
<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">c1</span><span class="w">  </span><span class="n">from</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">c0</span><span class="w">  </span><span class="n">from</span><span class="w"> </span><span class="n">f1</span><span class="w"></span>
<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">c2</span><span class="w">  </span><span class="n">from</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">c1</span><span class="w">  </span><span class="n">from</span><span class="w"> </span><span class="n">f2</span><span class="w"></span>
<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">c3</span><span class="w">  </span><span class="n">from</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">c2</span><span class="w">  </span><span class="n">from</span><span class="w"> </span><span class="n">f3</span><span class="w"></span>

<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">makeUpdate</span><span class="w"> </span><span class="n">cX</span><span class="w"> </span><span class="p">(</span><span class="n">from</span><span class="p">,</span><span class="w"> </span><span class="n">from&#39;</span><span class="p">,</span><span class="w"> </span><span class="n">to</span><span class="p">)</span><span class="w"> </span><span class="n">record</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">fun</span><span class="w"> </span><span class="n">ops</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">cX</span><span class="w"> </span><span class="n">from&#39;</span><span class="w"></span>
<span class="w">               </span><span class="k">fun</span><span class="w"> </span><span class="n">vars</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">to</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="n">record</span><span class="w"></span>
<span class="w">            </span><span class="k">in</span><span class="w"></span>
<span class="w">               </span><span class="n">Fold</span><span class="p">.</span><span class="n">fold</span><span class="w"> </span><span class="p">((</span><span class="n">vars</span><span class="p">,</span><span class="w"> </span><span class="n">ops</span><span class="p">),</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="p">(</span><span class="n">vars</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">vars</span><span class="w"> </span><span class="n">from</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="k">end</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">fun</span><span class="w"> </span><span class="n">makeUpdate0</span><span class="w">  </span><span class="n">z</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">makeUpdate</span><span class="w"> </span><span class="n">c0</span><span class="w">  </span><span class="n">z</span><span class="w"></span>
<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">makeUpdate1</span><span class="w">  </span><span class="n">z</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">makeUpdate</span><span class="w"> </span><span class="n">c1</span><span class="w">  </span><span class="n">z</span><span class="w"></span>
<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">makeUpdate2</span><span class="w">  </span><span class="n">z</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">makeUpdate</span><span class="w"> </span><span class="n">c2</span><span class="w">  </span><span class="n">z</span><span class="w"></span>
<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">makeUpdate3</span><span class="w">  </span><span class="n">z</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">makeUpdate</span><span class="w"> </span><span class="n">c3</span><span class="w">  </span><span class="n">z</span><span class="w"></span>

<span class="w">         </span><span class="k">fun</span><span class="w"> </span><span class="n">upd</span><span class="w"> </span><span class="n">z</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">step2</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">s</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">vars</span><span class="p">,</span><span class="w"> </span><span class="n">ops</span><span class="p">))</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">out</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">vars</span><span class="w"> </span><span class="p">(</span><span class="n">s</span><span class="w"> </span><span class="p">(</span><span class="n">ops</span><span class="w"> </span><span class="p">())</span><span class="w"> </span><span class="p">(</span><span class="n">out</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">ops</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">fun</span><span class="w"> </span><span class="n">set</span><span class="w"> </span><span class="n">z</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">step2</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">s</span><span class="p">,</span><span class="w"> </span><span class="n">v</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">vars</span><span class="p">,</span><span class="w"> </span><span class="n">ops</span><span class="p">))</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="k">fn</span><span class="w"> </span><span class="n">out</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">vars</span><span class="w"> </span><span class="p">(</span><span class="n">s</span><span class="w"> </span><span class="p">(</span><span class="n">ops</span><span class="w"> </span><span class="p">())</span><span class="w"> </span><span class="p">(</span><span class="n">out</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">v</span><span class="p">)),</span><span class="w"> </span><span class="n">ops</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>
<span class="w">   </span><span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>The idea of <span class="monospaced">makeUpdate</span> is to build a record of functions which can
replace the contents of one argument out of a list of arguments.  The
functions <span class="monospaced">f<em>&lt;X&gt;</em></span> replace the 0th, 1st, &#8230; argument with their
argument <span class="monospaced">z</span>. The <span class="monospaced">c<em>&lt;X&gt;</em></span> functions pass the first <em>X</em> <span class="monospaced">f</span>
functions to the record constructor.</p></div>
<div class="paragraph"><p>The <span class="monospaced">#field</span> notation of Standard ML allows us to select the map
function which replaces the corresponding argument. By converting the
record to an argument list, feeding that list through the selected map
function and piping the list into the record constructor, functional
record update is achieved.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_efficiency">Efficiency</h2>
<div class="sectionbody">
<div class="paragraph"><p>With MLton, the efficiency of this approach is as good as one would
expect with the special syntax.  Namely a sequence of updates will be
optimized into a single record construction that copies the unchanged
fields and fills in the changed fields with their new values.</p></div>
<div class="paragraph"><p>Before Sep 14, 2009, this page advocated an alternative implementation
of <a href="FunctionalRecordUpdate">FunctionalRecordUpdate</a>.  However, the old structure caused
exponentially increasing compile times.  We advise you to switch to
the newer version.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_applications">Applications</h2>
<div class="sectionbody">
<div class="paragraph"><p>Functional record update can be used to implement labelled
<a href="OptionalArguments">optional arguments</a>.</p></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>
