[MLton] Slightly simpler functional record update

Vesa Karvonen vesa.karvonen@cs.helsinki.fi
Fri, 12 Aug 2005 20:12:03 +0300


Quoting Stephen Weeks <sweeks@sweeks.com>:
> Even with your improvement, I find the construction 
> 
>   r (set3 (f o r) v (v1, v2, v3))
> 
> in the definition of set complicated enough to be worth abstracting
> into a makeSetter3 function.  I like separating the update from the
> conversions, but I think it makes the isomorphism clearer to define
> the set function via makeSetter3 as follows.

I agree. It is better to make the isomorphism explicit. A minor point,
though:

> fun set f =
>    let
>       fun r (a, b, c) = {a = a, b = b, c = c}
>       fun t {a, b, c} = (a, b, c)
>    in
>       makeSetter3 (r, r, t) f
>    end

I prefer to write the above as

 fun set f =
    let
       fun r (v1, v2, v3) = {a = v1, b = v2, c = v3}
       fun t {a = v1, b = v2, c = v3} = (v1, v2, v3)
    in
       makeSetter3 (r, r, t) f
    end

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.

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.

> Besides, it servers a nice reminder to those newer to SML that there
> are things beyond.

:-) Well, I guess that's fair enough.

I'll update the FRU page in a moment.

-Vesa Karvonen