Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extensible Records? #18

Open
rabipelais opened this issue Mar 25, 2015 · 4 comments
Open

Extensible Records? #18

rabipelais opened this issue Mar 25, 2015 · 4 comments

Comments

@rabipelais
Copy link
Contributor

I think it would be nice to be able to do something like this:

type Person = [r| {name :: String, age :: Int} |]
type HappyPerson = [r| {name :: String, age :: Int, favouriteFood :: String} |]

saruman :: Person
saruman = [r| {name = "Saruman", age = 2019} |]

happy :: Person -> HappyPerson
happy p = p <+> f
  where
    f ::  [r| {favouriteFood :: String} |]
    f = [r| {favouriteFood = "Lemon Pie"} |]

I think it could be done with a type class

class Extensible a b c where
   (<+>) :: a -> b -> c

which could be instantiated with Template Haskell (and that would be an ungodly amount of instances...). But perhaps there is some hairy type extension that could help?

A possible problem would be the ordering of the fields.

Do you know how the Elm people do it?

@nikita-volkov
Copy link
Owner

A possible problem would be the ordering of the fields.

Yes, and unfortunately I just can't imagine a solution to this, which does not involve making this a special case in the compiler.

@aavogt
Copy link
Contributor

aavogt commented Mar 26, 2015

With ghc >= 7.8 you have GHC.TypeLits.CmpSymbol to sort. https://gist.github.com/aavogt/035d8e6374e50e400035 seems to work

@nikita-volkov
Copy link
Owner

Seems very intriguing. I'll check it out later. Thanks.

@aavogt
Copy link
Contributor

aavogt commented Mar 27, 2015

I've cleaned up/commented that file a bit and put it in https://github.com/aavogt/record/blob/append/library/Record/Append.hs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants