[MLton] Slightly simpler functional record update
Vesa Karvonen
vesa.karvonen@cs.helsinki.fi
Sat, 13 Aug 2005 00:40:57 +0300
Quoting Stephen Weeks <sweeks@sweeks.com>:
> It occurs to me that there is nothing specific to 3-tuples in the
> concept of converting a tuple-setter into a record-setter. So, we
> could use a generic wrapSet as follows.
[...]
> fun wrapSet (set, r1, r2, t) f z s = r1 (set (f o r2) z (t s))
Good catch! This is probably the way to go. At least until someone
comes up with further simplifications... :)
> > This has the following minor advantages:
> > - The names of fields are only mentioned twice.
> > - The names of fields only appear syntactically as fields.
> > This means that the code is invariant with respect to
> > fixity declarations on the field names.
> > - The patterns/constructions used in the definition of r
> > and t are identical. This makes for slightly faster
> > manual editing, if necessary.
>
> The second point I agree with, although I also agree with its
> "minorness" :-). The first and third may be slightly advantageous if
> one is writing the set functions by hand, although find-replace would
> seem to work well. In any case, as you mention, this code is likely
> auto-generated, in which case due to readability, I think my code is
> preferable.
Well, the symmetry of
fun t2r (v1, v2, v3) = {a = v1, b = v2, c = v3}
fun r2t {a = v1, b = v2, c = v3} = (v1, v2, v3)
does help to see the isomorphism, IMHO. Perhaps I'm biased, but I find this
more readable. A further minor advantage of the above style, which is not
necessarily immediately apparent, is that this style will be shorter (in
terms of the number of characters) once the record field names get longer
than 7 (or 5 if using single character ids) characters on average. So, I'll
generate this style by default in `esml-gen-fru-setter' (unless there is a
technical argument in favor of the other style).
> > BTW, I have started an e-lisp file named `esml-gen.el' as a place
> > for SML code generation functions. I have a function by the name
> > `esml-gen-fru-setter' which generates a `set' function given a simple
> > record pattern or a type. E.g. the above code can be generated in Emacs
> > by typing `M x esml-gen-fru-setter {a, b, c}"'. If you think that it
> > is worth adding to the SVN, I can post it.
>
> Sure. No hurry though. Perhaps to simplify things we should add you
> as a committer, at least to ide/emacs/*.
I have no objection to that. Also, I certainly have no need for wider commit
rights.
> BTW, for easier typing, how about "M x esml-gen-fru-setter a b c"?
Sure, no problem. The idea of allowing a record pattern or a type is that
you can copy-paste it to the minibuffer.
-Vesa Karvonen