File: FunctionalRecordUpdate

package info (click to toggle)
mlton 20100608-5.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 36,628 kB
  • ctags: 70,047
  • sloc: ansic: 18,441; lisp: 2,879; makefile: 1,572; sh: 1,326; pascal: 256; asm: 97
file content (268 lines) | stat: -rw-r--r-- 12,953 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta name="robots" content="index,nofollow">



<title>FunctionalRecordUpdate - MLton Standard ML Compiler (SML Compiler)</title>
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="all" href="common.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="screen" href="screen.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="print" href="print.css">


<link rel="Start" href="Home">


</head>

<body lang="en" dir="ltr">

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-833377-1";
urchinTracker();
</script>
<table bgcolor = lightblue cellspacing = 0 style = "border: 0px;" width = 100%>
  <tr>
    <td style = "
		border: 0px;
		color: darkblue; 
		font-size: 150%;
		text-align: left;">
      <a class = mltona href="Home">MLton MLTONWIKIVERSION</a>
    <td style = "
		border: 0px;
		font-size: 150%;
		text-align: center;
		width: 50%;">
      FunctionalRecordUpdate
    <td style = "
		border: 0px;
		text-align: right;">
      <table cellspacing = 0 style = "border: 0px">
        <tr style = "vertical-align: middle;">
      </table>
  <tr style = "background-color: white;">
    <td colspan = 3
	style = "
		border: 0px;
		font-size:70%;
		text-align: right;">
      <a href = "Home">Home</a>
      &nbsp;<a href = "TitleIndex">Index</a>
      &nbsp;
</table>
<div id="content" lang="en" dir="ltr">
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>
As an example, the functional update of the record 
<pre class=code>
{a = <B><FONT COLOR="#5F9EA0">13</FONT></B>, b = <B><FONT COLOR="#5F9EA0">14</FONT></B>, c = <B><FONT COLOR="#5F9EA0">15</FONT></B>} 
</PRE>
 with <tt>c&nbsp;=&nbsp;16</tt> yields a new record  
<pre class=code>
{a = <B><FONT COLOR="#5F9EA0">13</FONT></B>, b = <B><FONT COLOR="#5F9EA0">14</FONT></B>, c = <B><FONT COLOR="#5F9EA0">16</FONT></B>}
</PRE>
 Functional record update also makes sense with multiple simultaneous updates.  For example, the functional update of the record above with <tt>a&nbsp;=&nbsp;18,&nbsp;c&nbsp;=&nbsp;19</tt> yields a new record  
<pre class=code>
{a = <B><FONT COLOR="#5F9EA0">18</FONT></B>, b = <B><FONT COLOR="#5F9EA0">14</FONT></B>, c = <B><FONT COLOR="#5F9EA0">19</FONT></B>}
</PRE>
 
</p>
<p>
One could easily imagine an extension of the SML that supports functional record update.  For example 
<pre class=code>
e <B><FONT COLOR="#A020F0">with</FONT></B> {a = <B><FONT COLOR="#5F9EA0">16</FONT></B>, b = <B><FONT COLOR="#5F9EA0">17</FONT></B>}
</PRE>
 would create a copy of the record denoted by <tt>e</tt> with field <tt>a</tt> replaced with <tt>16</tt> and <tt>b</tt> replaced with <tt>17</tt>. 
</p>
<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>
<h2 id="head-fa2179e8b7b0b4d2c689f9612a22a231e6cd9bf2">Simple implementation</h2>
<p>
To support functional record update on the record type 
<pre class=code>
{a: 'a, b: 'b, c: 'c} 
</PRE>
 first, define an update function for each component. 
<pre class=code>
<B><FONT COLOR="#A020F0">fun</FONT></B> withA ({a = _, b, c}, a) = {a = a, b = b, c = c}
<B><FONT COLOR="#A020F0">fun</FONT></B> withB ({a, b = _, c}, b) = {a = a, b = b, c = c}
<B><FONT COLOR="#A020F0">fun</FONT></B> withC ({a, b, c = _}, c) = {a = a, b = b, c = c}
</PRE>
 Then, one can express <tt>e&nbsp;with&nbsp;{a&nbsp;=&nbsp;16,&nbsp;b&nbsp;=&nbsp;17}&nbsp;</tt> as 
<pre class=code>
withB (withA (e, <B><FONT COLOR="#5F9EA0">16</FONT></B>), <B><FONT COLOR="#5F9EA0">17</FONT></B>)
</PRE>
 With infix notation 
<pre class=code>
<B><FONT COLOR="#A020F0">infix</FONT></B> withA withB withC
</PRE>
 the syntax is almost as concise as a language extension. 
<pre class=code>
e withA <B><FONT COLOR="#5F9EA0">16</FONT></B> withB <B><FONT COLOR="#5F9EA0">17</FONT></B>
</PRE>
 
</p>
<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 <tt>with</tt> function must be changed).  It is also annoying to have to define a <tt>with</tt> function, possibly with a fixity declaration, for each field. 
</p>
<p>
Fortunately, there is a solution to these problems. 
</p>
<h2 id="head-02713f1aab596f254ab1dd3551f0c5a015b6e40b">Advanced implementation</h2>
<p>
Using <a href="Fold">Fold</a> one can define a family of <tt>makeUpdate&lt;N&gt;</tt> functions and single <em>update</em> operator <tt>U</tt> 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 <tt>a</tt> and <tt>b</tt>.  Then one defines a function <tt>updateAB</tt> as follows. 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> updateAB =
   <B><FONT COLOR="#A020F0">fn</FONT></B> z =&gt;
   <B><FONT COLOR="#A020F0">let</FONT></B>
      <B><FONT COLOR="#A020F0">fun</FONT></B> from v1 v2 = {a = v1, b = v2}
      <B><FONT COLOR="#A020F0">fun</FONT></B> to f {a = v1, b = v2} = f v1 v2
   <B><FONT COLOR="#A020F0">in</FONT></B>
      makeUpdate2 (from, from, to)
   <B><FONT COLOR="#A020F0">end</FONT></B>
   z
</PRE>
<p>
 
</p>
<p>
The functions <tt>from</tt> (think <em>from function arguments</em>) and <tt>to</tt> (think <em>to function arguements</em>) specify an isomorphism between  <tt>a,b</tt> records and function arguments.  There is a second use of <tt>from</tt> to work around the lack of <a href="FirstClassPolymorphism">first-class polymorphism</a> in SML. 
</p>
<p>
With the definition of <tt>updateAB</tt> in place, the following expressions are valid. 
</p>

<pre class=code>
updateAB {a = <B><FONT COLOR="#5F9EA0">13</FONT></B>, b = <B><FONT COLOR="#BC8F8F">&quot;hello&quot;</FONT></B>} (set#b <B><FONT COLOR="#BC8F8F">&quot;goodbye&quot;</FONT></B>) $
updateAB {a = <B><FONT COLOR="#5F9EA0">13.5</FONT></B>, b = true} (set#b false) (set#a <B><FONT COLOR="#5F9EA0">12.5</FONT></B>) $
</PRE>
<p>
 
</p>
<p>
As another example, suppose that we would like to do functional record update on records with fields <tt>b</tt>, <tt>c</tt>, and <tt>d</tt>.  Then one defines a function <tt>updateBCD</tt> as follows. 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> updateBCD =
   <B><FONT COLOR="#A020F0">fn</FONT></B> z =&gt;
   <B><FONT COLOR="#A020F0">let</FONT></B>
      <B><FONT COLOR="#A020F0">fun</FONT></B> from v1 v2 v3 = {b = v1, c = v2, d = v3}
      <B><FONT COLOR="#A020F0">fun</FONT></B> to f {b = v1, c = v2, d = v3} = f v1 v2 v3
   <B><FONT COLOR="#A020F0">in</FONT></B>
      makeUpdate3 (from, from, to)
   <B><FONT COLOR="#A020F0">end</FONT></B>
   z
</PRE>
<p>
 
</p>
<p>
With the definition of <tt>updateBCD</tt> in place, the following expression is valid. 
</p>

<pre class=code>
updateBCD {b = <B><FONT COLOR="#5F9EA0">1</FONT></B>, c = <B><FONT COLOR="#5F9EA0">2</FONT></B>, d = <B><FONT COLOR="#5F9EA0">3</FONT></B>} (set#c <B><FONT COLOR="#5F9EA0">4</FONT></B>) (set#c <B><FONT COLOR="#5F9EA0">5</FONT></B>) $
</PRE>
<p>
 
</p>
<p>
Note that not all fields need be updated and that the same field may be updated multiple times.  Further note that the same <tt>set</tt> operator is used for all update functions (in the above, for both <tt>updateAB</tt> and <tt>updateBCD</tt>). 
</p>
<p>
In general, to define a functional-record-update function on records with fields <tt>f1</tt>, <tt>f2</tt>, ..., <tt>fN</tt>, use the following template. 
</p>

<pre class=code>
<B><FONT COLOR="#A020F0">val</FONT></B> update =
   <B><FONT COLOR="#A020F0">fn</FONT></B> z =&gt;
   <B><FONT COLOR="#A020F0">let</FONT></B> 
      <B><FONT COLOR="#A020F0">fun</FONT></B> from v1 v2 ... vn = {f1 = v1, f2 = v2, ..., <B><FONT COLOR="#A020F0">fn</FONT></B> = vn}
      <B><FONT COLOR="#A020F0">fun</FONT></B> to f {f1 = v1, f2 = v2, ..., <B><FONT COLOR="#A020F0">fn</FONT></B> = vn} = v1 v2 ... vn
   <B><FONT COLOR="#A020F0">in</FONT></B>
      makeUpdateN (from, from, to)
   <B><FONT COLOR="#A020F0">end</FONT></B>
   z
</PRE>
<p>
 
</p>
<p>
With this, one can update a record as follows. 
<pre class=code>
update {f1 = v1, ..., <B><FONT COLOR="#A020F0">fn</FONT></B> = vn} (set#fi1 vi1) ... (set#fim vim) $
</PRE>
 
</p>
<h2 id="head-f7b6e4357c7be44d1408bdf8f0ac43469123a056">The FunctionalRecordUpdate structure</h2>
<p>
Here is the implementation of functional record update. 
</p>

<pre class=code>
<B><FONT COLOR="#0000FF">structure</FONT></B> FunctionalRecordUpdate =
   <B><FONT COLOR="#0000FF">struct</FONT></B>
      <B><FONT COLOR="#A020F0">local</FONT></B>
         <B><FONT COLOR="#A020F0">fun</FONT></B> next g (f, z) x = g (f x, z)
         <B><FONT COLOR="#A020F0">fun</FONT></B> f1 (f, z) x = f (z x)
         <B><FONT COLOR="#A020F0">fun</FONT></B> f2  z = next f1  z
         <B><FONT COLOR="#A020F0">fun</FONT></B> f3  z = next f2  z

         <B><FONT COLOR="#A020F0">fun</FONT></B> c0  from = from
         <B><FONT COLOR="#A020F0">fun</FONT></B> c1  from = c0  from f1
         <B><FONT COLOR="#A020F0">fun</FONT></B> c2  from = c1  from f2
         <B><FONT COLOR="#A020F0">fun</FONT></B> c3  from = c2  from f3

         <B><FONT COLOR="#A020F0">fun</FONT></B> makeUpdate cX (from, from', to) record =
            <B><FONT COLOR="#A020F0">let</FONT></B>
               <B><FONT COLOR="#A020F0">fun</FONT></B> ops () = cX from'
               <B><FONT COLOR="#A020F0">fun</FONT></B> vars f = to f record
            <B><FONT COLOR="#A020F0">in</FONT></B>
               Fold.fold ((vars, ops), <B><FONT COLOR="#A020F0">fn</FONT></B> (vars, _) =&gt; vars from)
            <B><FONT COLOR="#A020F0">end</FONT></B>
      <B><FONT COLOR="#A020F0">in</FONT></B>
         <B><FONT COLOR="#A020F0">fun</FONT></B> makeUpdate0  z = makeUpdate c0  z
         <B><FONT COLOR="#A020F0">fun</FONT></B> makeUpdate1  z = makeUpdate c1  z
         <B><FONT COLOR="#A020F0">fun</FONT></B> makeUpdate2  z = makeUpdate c2  z
         <B><FONT COLOR="#A020F0">fun</FONT></B> makeUpdate3  z = makeUpdate c3  z

         <B><FONT COLOR="#A020F0">fun</FONT></B> upd z = Fold.step2 (<B><FONT COLOR="#A020F0">fn</FONT></B> (s, f, (vars, ops)) =&gt; (<B><FONT COLOR="#A020F0">fn</FONT></B> out =&gt; vars (s (ops ()) (out, f)), ops)) z
         <B><FONT COLOR="#A020F0">fun</FONT></B> set z = Fold.step2 (<B><FONT COLOR="#A020F0">fn</FONT></B> (s, v, (vars, ops)) =&gt; (<B><FONT COLOR="#A020F0">fn</FONT></B> out =&gt; vars (s (ops ()) (out, <B><FONT COLOR="#A020F0">fn</FONT></B> _ =&gt; v)), ops)) z
      <B><FONT COLOR="#A020F0">end</FONT></B>
   <B><FONT COLOR="#0000FF">end</FONT></B>
</PRE>
<p>
 
</p>
<p>
The idea of <tt>makeUpdate</tt> is to build a record of functions which  can replace the contents of one argument out of a list of arguments. The functions <tt>fX</tt> replace the 0th, 1st, ... argument with their argument <tt>z</tt>. The <tt>cX</tt> functions pass the first X <tt>f</tt> functions to the record constructor. 
</p>
<p>
The <tt>#field</tt> 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>
<h2 id="head-12c4ffe1d987164a4bedf6ab2aac597a113e181c">Efficiency</h2>
<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>
<p>
Before Sep 14, 2009, this page advocated an alternative implementation of FunctionalRecordUpdate. However, the old structure caused exponentially increasing compile times. We advise you to switch to the newer version. 
</p>
<h2 id="head-76c74bd071dd73f01696fddbbffc77712a479faf">Applications</h2>
<p>
Functional record update can be used to implement labelled <a href="OptionalArguments">optional arguments</a>. 
</p>
</div>



<p>
<hr>
Last edited on 2009-09-14 20:23:55 by <span title="ottawa.dvs.informatik.tu-darmstadt.de"><a href="WesleyTerpstra">WesleyTerpstra</a></span>.
</body></html>