| 1 |
= FunctionalRecordUpdate |
| 2 |
|
| 3 |
Functional record update is the copying of a record while replacing |
| 4 |
the values of some of the fields. <<StandardML#,Standard ML>> does not |
| 5 |
have explicit syntax for functional record update. We will show below |
| 6 |
how to implement functional record update in SML, with a little |
| 7 |
boilerplate code. |
| 8 |
|
| 9 |
As an example, the functional update of the record |
| 10 |
|
| 11 |
[source,sml] |
| 12 |
---- |
| 13 |
{a = 13, b = 14, c = 15} |
| 14 |
---- |
| 15 |
|
| 16 |
with `c = 16` yields a new record |
| 17 |
|
| 18 |
[source,sml] |
| 19 |
---- |
| 20 |
{a = 13, b = 14, c = 16} |
| 21 |
---- |
| 22 |
|
| 23 |
Functional record update also makes sense with multiple simultaneous |
| 24 |
updates. For example, the functional update of the record above with |
| 25 |
`a = 18, c = 19` yields a new record |
| 26 |
|
| 27 |
[source,sml] |
| 28 |
---- |
| 29 |
{a = 18, b = 14, c = 19} |
| 30 |
---- |
| 31 |
|
| 32 |
|
| 33 |
One could easily imagine an extension of the SML that supports |
| 34 |
functional record update. For example |
| 35 |
|
| 36 |
[source,sml] |
| 37 |
---- |
| 38 |
e with {a = 16, b = 17} |
| 39 |
---- |
| 40 |
|
| 41 |
would create a copy of the record denoted by `e` with field `a` |
| 42 |
replaced with `16` and `b` replaced with `17`. |
| 43 |
|
| 44 |
Since there is no such syntax in SML, we now show how to implement |
| 45 |
functional record update directly. We first give a simple |
| 46 |
implementation that has a number of problems. We then give an |
| 47 |
advanced implementation, that, while complex underneath, is a reusable |
| 48 |
library that admits simple use. |
| 49 |
|
| 50 |
|
| 51 |
== Simple implementation |
| 52 |
|
| 53 |
To support functional record update on the record type |
| 54 |
|
| 55 |
[source,sml] |
| 56 |
---- |
| 57 |
{a: 'a, b: 'b, c: 'c} |
| 58 |
---- |
| 59 |
|
| 60 |
first, define an update function for each component. |
| 61 |
|
| 62 |
[source,sml] |
| 63 |
---- |
| 64 |
fun withA ({a = _, b, c}, a) = {a = a, b = b, c = c} |
| 65 |
fun withB ({a, b = _, c}, b) = {a = a, b = b, c = c} |
| 66 |
fun withC ({a, b, c = _}, c) = {a = a, b = b, c = c} |
| 67 |
---- |
| 68 |
|
| 69 |
Then, one can express `e with {a = 16, b = 17}` as |
| 70 |
|
| 71 |
[source,sml] |
| 72 |
---- |
| 73 |
withB (withA (e, 16), 17) |
| 74 |
---- |
| 75 |
|
| 76 |
With infix notation |
| 77 |
|
| 78 |
[source,sml] |
| 79 |
---- |
| 80 |
infix withA withB withC |
| 81 |
---- |
| 82 |
|
| 83 |
the syntax is almost as concise as a language extension. |
| 84 |
|
| 85 |
[source,sml] |
| 86 |
---- |
| 87 |
e withA 16 withB 17 |
| 88 |
---- |
| 89 |
|
| 90 |
This approach suffers from the fact that the amount of boilerplate |
| 91 |
code is quadratic in the number of record fields. Furthermore, |
| 92 |
changing, adding, or deleting a field requires time proportional to |
| 93 |
the number of fields (because each ``with__<L>__`` function must be |
| 94 |
changed). It is also annoying to have to define a ``with__<L>__`` |
| 95 |
function, possibly with a fixity declaration, for each field. |
| 96 |
|
| 97 |
Fortunately, there is a solution to these problems. |
| 98 |
|
| 99 |
|
| 100 |
== Advanced implementation |
| 101 |
|
| 102 |
Using <<Fold#>> one can define a family of ``makeUpdate__<N>__`` |
| 103 |
functions and single _update_ operator `U` so that one can define a |
| 104 |
functional record update function for any record type simply by |
| 105 |
specifying a (trivial) isomorphism between that type and function |
| 106 |
argument list. For example, suppose that we would like to do |
| 107 |
functional record update on records with fields `a` and `b`. Then one |
| 108 |
defines a function `updateAB` as follows. |
| 109 |
|
| 110 |
[source,sml] |
| 111 |
---- |
| 112 |
val updateAB = |
| 113 |
fn z => |
| 114 |
let |
| 115 |
fun from v1 v2 = {a = v1, b = v2} |
| 116 |
fun to f {a = v1, b = v2} = f v1 v2 |
| 117 |
in |
| 118 |
makeUpdate2 (from, from, to) |
| 119 |
end |
| 120 |
z |
| 121 |
---- |
| 122 |
|
| 123 |
The functions `from` (think _from function arguments_) and `to` (think |
| 124 |
_to function arguements_) specify an isomorphism between `a`,`b` |
| 125 |
records and function arguments. There is a second use of `from` to |
| 126 |
work around the lack of |
| 127 |
<<FirstClassPolymorphism#,first-class polymorphism>> in SML. |
| 128 |
|
| 129 |
With the definition of `updateAB` in place, the following expressions |
| 130 |
are valid. |
| 131 |
|
| 132 |
[source,sml] |
| 133 |
---- |
| 134 |
updateAB {a = 13, b = "hello"} (set#b "goodbye") $ |
| 135 |
updateAB {a = 13.5, b = true} (set#b false) (set#a 12.5) $ |
| 136 |
---- |
| 137 |
|
| 138 |
As another example, suppose that we would like to do functional record |
| 139 |
update on records with fields `b`, `c`, and `d`. Then one defines a |
| 140 |
function `updateBCD` as follows. |
| 141 |
|
| 142 |
[source,sml] |
| 143 |
---- |
| 144 |
val updateBCD = |
| 145 |
fn z => |
| 146 |
let |
| 147 |
fun from v1 v2 v3 = {b = v1, c = v2, d = v3} |
| 148 |
fun to f {b = v1, c = v2, d = v3} = f v1 v2 v3 |
| 149 |
in |
| 150 |
makeUpdate3 (from, from, to) |
| 151 |
end |
| 152 |
z |
| 153 |
---- |
| 154 |
|
| 155 |
With the definition of `updateBCD` in place, the following expression |
| 156 |
is valid. |
| 157 |
|
| 158 |
[source,sml] |
| 159 |
---- |
| 160 |
updateBCD {b = 1, c = 2, d = 3} (set#c 4) (set#c 5) $ |
| 161 |
---- |
| 162 |
|
| 163 |
Note that not all fields need be updated and that the same field may |
| 164 |
be updated multiple times. Further note that the same `set` operator |
| 165 |
is used for all update functions (in the above, for both `updateAB` |
| 166 |
and `updateBCD`). |
| 167 |
|
| 168 |
In general, to define a functional-record-update function on records |
| 169 |
with fields `f1`, `f2`, ..., `fN`, use the following template. |
| 170 |
|
| 171 |
[source,sml] |
| 172 |
---- |
| 173 |
val update = |
| 174 |
fn z => |
| 175 |
let |
| 176 |
fun from v1 v2 ... vn = {f1 = v1, f2 = v2, ..., fn = vn} |
| 177 |
fun to f {f1 = v1, f2 = v2, ..., fn = vn} = v1 v2 ... vn |
| 178 |
in |
| 179 |
makeUpdateN (from, from, to) |
| 180 |
end |
| 181 |
z |
| 182 |
---- |
| 183 |
|
| 184 |
With this, one can update a record as follows. |
| 185 |
|
| 186 |
[source,sml] |
| 187 |
---- |
| 188 |
update {f1 = v1, ..., fn = vn} (set#fi1 vi1) ... (set#fim vim) $ |
| 189 |
---- |
| 190 |
|
| 191 |
|
| 192 |
== The `FunctionalRecordUpdate` structure |
| 193 |
|
| 194 |
Here is the implementation of functional record update. |
| 195 |
|
| 196 |
[source,sml] |
| 197 |
---- |
| 198 |
structure FunctionalRecordUpdate = |
| 199 |
struct |
| 200 |
local |
| 201 |
fun next g (f, z) x = g (f x, z) |
| 202 |
fun f1 (f, z) x = f (z x) |
| 203 |
fun f2 z = next f1 z |
| 204 |
fun f3 z = next f2 z |
| 205 |
|
| 206 |
fun c0 from = from |
| 207 |
fun c1 from = c0 from f1 |
| 208 |
fun c2 from = c1 from f2 |
| 209 |
fun c3 from = c2 from f3 |
| 210 |
|
| 211 |
fun makeUpdate cX (from, from', to) record = |
| 212 |
let |
| 213 |
fun ops () = cX from' |
| 214 |
fun vars f = to f record |
| 215 |
in |
| 216 |
Fold.fold ((vars, ops), fn (vars, _) => vars from) |
| 217 |
end |
| 218 |
in |
| 219 |
fun makeUpdate0 z = makeUpdate c0 z |
| 220 |
fun makeUpdate1 z = makeUpdate c1 z |
| 221 |
fun makeUpdate2 z = makeUpdate c2 z |
| 222 |
fun makeUpdate3 z = makeUpdate c3 z |
| 223 |
|
| 224 |
fun upd z = Fold.step2 (fn (s, f, (vars, ops)) => (fn out => vars (s (ops ()) (out, f)), ops)) z |
| 225 |
fun set z = Fold.step2 (fn (s, v, (vars, ops)) => (fn out => vars (s (ops ()) (out, fn _ => v)), ops)) z |
| 226 |
end |
| 227 |
end |
| 228 |
---- |
| 229 |
|
| 230 |
The idea of `makeUpdate` is to build a record of functions which can |
| 231 |
replace the contents of one argument out of a list of arguments. The |
| 232 |
functions ``f__<X>__`` replace the 0th, 1st, ... argument with their |
| 233 |
argument `z`. The ``c__<X>__`` functions pass the first __X__ `f` |
| 234 |
functions to the record constructor. |
| 235 |
|
| 236 |
The `#field` notation of Standard ML allows us to select the map |
| 237 |
function which replaces the corresponding argument. By converting the |
| 238 |
record to an argument list, feeding that list through the selected map |
| 239 |
function and piping the list into the record constructor, functional |
| 240 |
record update is achieved. |
| 241 |
|
| 242 |
|
| 243 |
== Efficiency |
| 244 |
|
| 245 |
With MLton, the efficiency of this approach is as good as one would |
| 246 |
expect with the special syntax. Namely a sequence of updates will be |
| 247 |
optimized into a single record construction that copies the unchanged |
| 248 |
fields and fills in the changed fields with their new values. |
| 249 |
|
| 250 |
Before Sep 14, 2009, this page advocated an alternative implementation |
| 251 |
of <<FunctionalRecordUpdate#>>. However, the old structure caused |
| 252 |
exponentially increasing compile times. We advise you to switch to |
| 253 |
the newer version. |
| 254 |
|
| 255 |
|
| 256 |
== Applications |
| 257 |
|
| 258 |
Functional record update can be used to implement labelled |
| 259 |
<<OptionalArguments#,optional arguments>>. |