precedence bug with derived instances

Simon Peyton-Jones simonpj@microsoft.com
Fri, 18 Oct 2002 14:34:12 +0100


| In GHC 5.04.1, derived instances of Show mishandle precedence:
|=20
| Prelude> putStrLn (showsPrec 10 (Just 0) "")
| Just 0
|=20
| The result should be:   (Just 0)


I think it's a bug in the Report, not in GHC, actually.  The Report says
(Section D.4)

  "The function 'showsPrec d x r' accepts a precedence level 'd'
  (a number from 0 to 10), a value 'x', and a string 'r'.
     ....
  "The representation will be enclosed in parentheses if the precedence
  of the top-level constructor operator in 'x' is less than 'd'.  Thus,
  if 'd' is 0 then the result is never surrounded in parentheses; if
  'd' is 10 it is always surrounded in parentheses, unless it is an
  atomic expression."

But in fact, the precedence of function application is 10, as the
syntax makes clear.  Indeed, the example code at the end of Appendix D
shows a call to showsPrec with an argument of 11 (it's written
"app_prec + 1"), for precisely this reason.

So, I think that I have to make these changes to the Report:

(A) The last sentence should say "..; if 'd' is 11 it is always
surrounded in parentheses, unless it is an atomic expression (recall,
function application has precedence 10)".

(B) The earlier parenthesis should say "(a number from 0 to 11)".


Does anyone disagree?

Simon