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

Anthony Clayden anthony_clayden at clear.net.nz
Wed Feb 15 21:01:06 UTC 2017


> On Tue, 14 Feb 2017 at 18:50, Harendra Kumar said:

> In my application, I want some of the fields in a record
to have default
> values while others must always be specified by the user. 
I can implement
> this using the rawr records library
(http://hackage.haskell.org/package/rawr)

Hi Harendra. I believe rawr builds on some of the work in
'overloaded records'.

That's a suite of features being developed right now look
here for what's coming soon
https://github.com/adamgundry/ghc-proposals/blob/overloaded-record-fields/proposals/0000-overloaded-record-fields.rst

> like so:
>
> def :: ("b" := String) -> R ("a" := Int, "b" := String)
> def t = (R (#a := 0) :*: R t) -- merge with defaults
>
> let r = def (#b := "hello")

That looks like some sort of anonymous/extensible records
system.
What is data constructor `R`; where is it defined?
There's a few mentions of 'anonymous' records on that link
above.
(And mention of rawr.)

It's not clear what you're trying to do.
Do you need anonymous/extensible records?
Why use `let` to bind a value for `r`?

> Here field "a" has a default value (i.e. it is optional
when "def" is used)
> but field "b" does not have a default value and has to be
explicitly
> specified by the user. This is made possible by record
merging feature of
> rawr, making the record extensible.

> I could not find an equivalent way of achieving this using
overloaded
> records
(http://hackage.haskell.org/package/overloaded-records). 

So far, overloaded records is based on standard-ish Haskell
record decls.

So you must declare a distinct record constructor and field
labels.
Like

    data MyR = MyR { a :: Int, b :: String };

If you want default values:

    myRdef = MyR{ a = 0 };  -- don't have to give b

Then bind some value, to incorp defaults.

    r = myRdef { b = "hello" };   -- takes the defult for a

> Is record merging possible or can be made possible 

It looks to me like you don't need '"record merging".
What do you think it is; why do you think you need it?

> using overloaded-records or with the ghc overloaded
records poposal? 
> If not, is there a plan to make something like this
possible in future? 

I'd say it's on the radar, but a long way off.


> Or will this never be possible with the in built record
syntax?

I think that built-in record syntax will fit your
requirements.

> Note that, this is handy to simulate mandatory and
optional keyword
> arguments to a function by passing them in the form of a
record. 

Sure. Keyword arguments to a function are fixed.
No need for building dynamic/anonymous structures.
Yes it's good (and standard practice) to pass as a record.
So you can use a standard built-in Haskell record decl,
as I used above.

> Something which is available in other languages like
python.

It looks to me that you're trying to write Python code in
Haskell.

Not a good idea. Use built-in/static features first.
Think about the difference between a compiled vs interpreted
language.
Only when static features really really aren't working,
consider dynamic/extensible.


AntC


More information about the Haskell-Cafe mailing list