How to show record syntax?

A while back, I was working on the Show function for EMGM [1] and verifying its output vs. that of GHC's when I discovered (I thought) a minor difference between what was necessary and what GHC did for record syntax. Labeled construction and update, according to the Report [2], have higher precedence than function application. This means that I can do the following
module Main where data A = A {x :: Int} deriving Show main = print $ Just A {x = 5}
without putting parentheses around A {x = 5}. If I run this in GHCi, *Main> main Just (A {x = 5}) I see that the derived show in GHC adds parentheses around the record value. This led to an issue report [3] which developed a few responses. Some don't like the grammar as defined, because, at a glance, it appears too much like function application. Others noted that Hugs does not add the parentheses and wanted to see consistency between compilers. In the spirit of the GHC Bug Sweep [4] and finding resolution, I put the question to the wider community. How should we show record syntax? Should we try to ensure consistency between the compilers? Should we try to print as few parentheses as possible? Should we write a Haskell' proposal to "fix" the grammar and make Just A {x=5} illegal? What do you think? [1] http://hackage.haskell.org/packages/archive/emgm/0.3.1/doc/html/Generics-EMG... [2] http://www.haskell.org/onlinereport/exps.html#sect3 [3] http://hackage.haskell.org/trac/ghc/ticket/2530 [4] http://hackage.haskell.org/trac/ghc/wiki/BugSweep Regards, Sean

On Tue, 2009-11-17 at 17:14 +0100, Sean Leather wrote:
module Main where data A = A {x :: Int} deriving Show main = print $ Just A {x = 5}
This led to an issue report [3] which developed a few responses. Some don't like the grammar as defined, because, at a glance, it appears too much like function application. Others noted that Hugs does not add the parentheses and wanted to see consistency between compilers.
In the spirit of the GHC Bug Sweep [4] and finding resolution, I put the question to the wider community. How should we show record syntax? Should we try to ensure consistency between the compilers? Should we try to print as few parentheses as possible? Should we write a Haskell' proposal to "fix" the grammar and make Just A {x=5} illegal? What do you think?
If you want my opinion, I think consistency between the compilers is probably a good thing. I do not have a very strong opinion on whether the derived Show instances should use the extra brackets here or not. Just pick one. The Show output is not readable in the first place so it does not really matter. However I am opposed to "fixing" the grammar to add lots of unnecessary brackets and to make my programs uglier. Using records for the named function argument idiom is rather nice and should be encouraged not discouraged by making the syntax for it worse. bigChunkyCallWithLotsOfArgs defaultArgs { someArg = 3, someOtherArg = 4, } Lovely! bigChunkyCallWithLotsOfArgs (defaultArgs { someArg = 3, someOtherArg = 4, }) Bleugh! Duncan

Just an aside, derived Show doesn't try to be truly minimal with respect to
superfluous ()'s at this point, for instance it doesn't take into
consideration that when using infixl or infixr you can elide them on one
side, so there is already a precedent for the fact that the ()'s that get
used are not minimal.
That said, I think there is nothing wrong with using the fact that records
bind tighter than application, and 'fixing the grammar' to prevent that
would break a fair bit of code -- I know that I for one use it fairly often.
So, I'm not in favor of proposing a breaking change to the grammar for
Haskell' that removes this functionality.
Eliding the superfluous parens around records in derived Show feels like the
right solution as it imparts less noise in the output.
A very minor concern would be making sure that Read can handle input with or
without the superfluous parentheses, but as I understand things that should
come more or less for free, given that they just wrap the value.
This would let values that were serialized on an older version continue to
be read in the current version, for people who are using Show as a data
storage format.
-Edward Kmett
On Tue, Nov 17, 2009 at 11:14 AM, Sean Leather
A while back, I was working on the Show function for EMGM [1] and verifying its output vs. that of GHC's when I discovered (I thought) a minor difference between what was necessary and what GHC did for record syntax. Labeled construction and update, according to the Report [2], have higher precedence than function application. This means that I can do the following
module Main where data A = A {x :: Int} deriving Show main = print $ Just A {x = 5}
without putting parentheses around A {x = 5}. If I run this in GHCi,
*Main> main Just (A {x = 5})
I see that the derived show in GHC adds parentheses around the record value.
This led to an issue report [3] which developed a few responses. Some don't like the grammar as defined, because, at a glance, it appears too much like function application. Others noted that Hugs does not add the parentheses and wanted to see consistency between compilers.
In the spirit of the GHC Bug Sweep [4] and finding resolution, I put the question to the wider community. How should we show record syntax? Should we try to ensure consistency between the compilers? Should we try to print as few parentheses as possible? Should we write a Haskell' proposal to "fix" the grammar and make Just A {x=5} illegal? What do you think?
[1] http://hackage.haskell.org/packages/archive/emgm/0.3.1/doc/html/Generics-EMG... [2] http://www.haskell.org/onlinereport/exps.html#sect3 [3] http://hackage.haskell.org/trac/ghc/ticket/2530 [4] http://hackage.haskell.org/trac/ghc/wiki/BugSweep
Regards, Sean
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (3)
-
Duncan Coutts
-
Edward Kmett
-
Sean Leather