Implement OverloadedLabels
ClosedPublic

Authored by adamgundry on Oct 16 2015, 11:54 AM.

Details

Summary

See https://ghc.haskell.org/trac/ghc/wiki/Records/OverloadedRecordFields/OverloadedLabels
for the big picture (though that wiki page could do with some polishing).

This is almost working, except for in GHCi. For some reason writing #x in GHCi
gives <interactive>:1:2: lexical error at character 'x', whereas it parses
correctly in a module. I don't really understand the lexer and have mostly
copied the ImplicitParameters code successfully, apart from this minor problem.
Perhaps I'm missing something obvious? How does GHCi lex things differently?

Test Plan

New tests in overloadedrecflds.

Diff Detail

Repository
rGHC Glasgow Haskell Compiler
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.
adamgundry retitled this revision from to Implement OverloadedLabels.Oct 16 2015, 11:54 AM
adamgundry updated this object.
adamgundry edited the test plan for this revision. (Show Details)
adamgundry added reviewers: austin, bgamari, simonpj.
austin requested changes to this revision.Oct 16 2015, 3:05 PM

Basically fine from a quick look, but: you also need to add OverloadedLabels to testsuite/tests/driver/T4437.hs (see build failures).

This revision now requires changes to proceed.Oct 16 2015, 3:05 PM

Thanks, will do.

@kosmikus pointed out that the lexing problem is probably due to the # being at the beginning of a line, because there is a special rule for lexing CPP. I guess I may need to actually think about how the lexer works in order to fix this.

@kosmikus pointed out that the lexing problem is probably due to the # being at the beginning of a line, because there is a special rule for lexing CPP. I guess I may need to actually think about how the lexer works in order to fix this.

Quick note from the trenches: I recently needed to make changes to the lexer, but I had no experience at all with alex. Learning alex by reading the manual was a rather short task, and it was then quite straightforward to apply my new knowledge to GHC's lexer. So don't shy away from alex and GHC's lexer -- it's not as bad as you probably think.

adamgundry updated this revision to Diff 4605.Oct 22 2015, 3:08 PM
  • Tweak lexer to accept hash at the beginning of a line
  • Add OverloadedLabels to GHC-only extensions in T4437

Apart from the fact that it could do with some more comments, is it okay to merge this?

I'm planning to put together a separate diff with user documentation for DuplicateRecordFields and OverloadedLabels, but I'd like to get this out of the way first.

Fine. I think some Notes would help, and have suggested some places.

compiler/hsSyn/HsExpr.hs
141

Comments; indeed a Note.

compiler/typecheck/TcExpr.hs
216

Needs a clear Note with an example or two.

libraries/base/GHC/OverloadedLabels.hs
29

Please add a big Note in this module, sketching how this all works and referring to the wiki page.

simonpj accepted this revision.Oct 30 2015, 12:14 PM
thomie added inline comments.Nov 6 2015, 6:58 AM
compiler/prelude/PrelNames.hs
2055

510 is already taken in HEAD.

Lennart commented on the wiki page:

I don't like the type of fromLabel. I'd much rather it had type fromLabel :: Proxy# x -> a, because then it can be used without the explicit type application extension.

I tend to agree with this. I don't have any immediate applications that require user code to be able to invoke fromLabel directly, but it would be nice not to rule them out. And it will make the magic typeclass instances nicer later on. @simonpj does this change sound reasonable to you?

adamgundry updated this revision to Diff 4989.Nov 9 2015, 10:45 AM

Documentation and addition of Proxy# argument

  • Merge remote-tracking branch 'origin/master' into wip/overloaded-labels
  • Rename overloadedLabelClassName and get rid of unnecessary overloadedLabelName
  • Add Proxy# argument to fromLabel, and document
adamgundry updated this revision to Diff 4990.Nov 9 2015, 10:48 AM
  • Merge remote-tracking branch 'origin/master' into wip/overloaded-labels

I can't find the ticket for this feature, so I'll mention it here.

@adamgundry: hsc2hs also gives special meaning to #stuff (e.g. #size, #peek, #ptr, #offset), has this been considered? To refer to a field #x from a .hsc file you'd have to use two pound signs: "To output a literal #, write it twice: ##." See https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/hsc2hs.html#idp35055056.

bgamari accepted this revision.Nov 17 2015, 8:47 AM

This looks good. Given how little available syntax remains in Haskell I think we'll just need to accept the overlap with hsc2hs (given a brief mention in the documentation).

On the note of documentation: this feature could use some. I've spoken with @adamgundry about this and he has agreed to submit another Diff with documentation by the end of the month. Given this, I think this Diff is ready to merge.

Thanks @adamgundry!

This revision was automatically updated to reflect the committed changes.