Records in Haskell
anthony_clayden at clear.net.nz
Tue Feb 28 01:52:29 CET 2012
Greg Weber <greg <at> gregweber.info> writes:
> > What on earth do you mean by "not automatically abstract
> > over fields"?
> Abstraction over fields is the ability to write a function that works
> on two records with the same field label.
Thanks Greg, I see you've put something on the wiki about "abstract over
fields". The example code doesn't seem to be right, so I'm still confused what
you mean ...
The example has:
getA = r.a
I think that should be:
getA r = r.a
(Otherwise how does `getA` know which record to extract the `a` from?)
And you give no type signature for getA so (at a wild guess) by "abstract" you
mean that `getA` extracts the `a` from any record `r` with an `a` field(?)
Under SORF: r.a desugars to a call to the `get` method.
Under DORF: r.a desugars to (a r), then we can eta-reduce:
getA r = r.a ==> (a r)
getA = a
Either way, I don't see that `getA` is adding anything over plain field `a`.
There _is_ a difference between DORF and SORF here. In DORF I can declare:
getF f r = r.f -- works for any field of any record
-- RHS desugars to (f r), so getF === ($)
And can use it, for example:
getF lastName cust1
getF fullName person2
I don't think you can do this is SORF (but please check with SPJ). In
particular, I don't think you could call this function and pass an argument
into it for the field name.
That's because in SORF the dot notation is desugarred at the point it occurs
(according to the wiki on SORF), and the `f` appearing on the RHS is a bound
variable, not a field name as such. (In fact, I wonder if SORF would take the
dot notation and try to desugar it to field "f", then either get a type
failure or (by accident) extract the wrong field.)
Please correct this on the wiki.
More information about the Glasgow-haskell-users