[Haskell-cafe] Extensible records & mandatory/optional record fields

Anthony Clayden anthony_clayden at clear.net.nz
Thu Feb 16 01:52:19 UTC 2017


> On 15 February 2017 at 23:06 Harendra Kumar wrote:

>> On 16 February 2017 at 02:31, Anthony Clayden wrote:
> 
> 
> I am aware of the ghc "overloaded records" proposal. rawr
> provides anonymous extensible records using the overloaded
> labels feature of ghc 8. Records can be merged or
> partitioned. I believe, the key difference between
> "overloaded records" and rawr is that the latter provides
> extensible records while the former does not.

Yes. But you seem to be talking about a different 'feature'
of rawr.

For gathering params to a function, I see no need for
extensible records.
The params are known at compile time.
If for a DSL you want 'optional' fields, make them Maybe's,
with a default of Nothing.

> 
> I am trying to write a program which provides a friendly
> high level DSL to the user.

'user' here seems to mean programmer,
if you're looking for static guarantees.

> I want a pure function like
> API but instead of passing positional parameters I want
> the user to be able to specify arguments based on keywords
> and be able to skip any optional arguments. Something like
> the following, name is mandatory and email is optional:
> 
> maintainer  (#name  := "Harendra Kumar",  #email :=
> "xyz at gmail.com")
> 
> I can achieve this using rawr. The argument to the
> function is an anonymous record ...

OK. But I'm not seeing why it has to be anonymous.
Again, if it's for a specific function, it needs specific
parameters.
An anonymous record might provide keywords not valid,
as well as fail to provide keywords expected.

> and we can pattern match
> partially using the mandatory fields in the record to
> statically check that those fields are present. 

OK. That's the critical feature you want, then.
It wasn't clear from your O.P.

> 
> I was only trying to say that this is a pretty
> useful feature in python and I guess in some other
> imperative languages too.

(I don't think it's anything to do with programming
paradigm.)

> It allows you to write self
> documenting code where necessary.

No the code you posted was not self-documenting.
(The comments were useful, thank you.)
Only a python-charmer would claim code full of graphic chars
is 'self-documenting'.
I have no idea what it was that made #name mandatory
but #email not.

> It will be nice if we have a way to achieve something like
this.
> 

There's nothing in the standard language currently.

> > [example]

> This is the first approach that I tried, this is commonly
> used in many libraries. The only drawback with this is
> that I cannot enforce mandatory/optional fields
> statically. All fields are optional in this case.

So your DSL is going to be compiled?
Not intepreted?
Including putting in values like #name?

I'd expect #name would come from 
end-user input at run time(?)

Yes the approach I illustrated is common in many libraries.
Perhaps authors of those libraries are listening in,
and could comment.

AntC



More information about the Haskell-Cafe mailing list