Opened 11 years ago

Closed 11 years ago

Last modified 8 years ago

#779 closed bug (fixed)

small bugs in Language.Haskell.TH.Ppr.pprint

Reported by: skata@… Owned by:
Priority: normal Milestone:
Component: Template Haskell Version: 6.5
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


Thank you very much for developing GHC. I would like to report two small bugs and my patch for them.

(These problems apply at least to 6.4.1, 6.4.2, and the current 6.5 in the darcs repository)

Problem 1: pprint does not deal with parentheses of types of higher-order functions correctly, i.e.:

[skata@mermaid]~% ghci -fth -v0
Prelude> Language.Haskell.TH.runQ [t| (Int->Int)->Int |] >>= \e -> putStrLn (Language.Haskell.TH.pprint e)
Loading package template-haskell-1.0 ... linking ... done.
GHC.Base.Int -> GHC.Base.Int -> GHC.Base.Int

I believe '(GHC.Base.Int -> GHC.Base.Int) -> GHC.Base.Int' should be printed.

Problem 2: pprint does not parenthesize operators used as functions, e.g.:

Prelude> Language.Haskell.TH.runQ [| (.) id id |] >>= \e -> putStrLn (Language.Haskell.TH.pprint e)

I think 'GHC.Base..' should be parenthesized.

Patch: Hopefully the following patch works (at least for me):

== running darcs whatsnew in libraries/template-haskell
hunk ./Language/Haskell/TH/Ppr.hs 42
- ppr v = pprName v -- text (show v)
+ ppr v = case nameBase v of c:_ | c elem "!#$%&~=|+*<>?-^@:./\\"
+ -> parens (pprName v)
+ _ -> pprName v
hunk ./Language/Haskell/TH/Ppr.hs 289
-pprTyApp (ArrowT, [arg1,arg2]) = sep [ppr arg1 <+> text "->", ppr arg2]
+pprTyApp (ArrowT, [arg1,arg2]) = sep [ppr' arg1 <+> text "->", ppr arg2]
+ where ppr' t = case split t of (ArrowT, [_,_]) -> parens (ppr t)
+ _ -> ppr t

Best regards,

Attachments (1)

pprint.patch (1.5 KB) - added by skata@… 11 years ago.

Download all attachments as: .zip

Change History (7)

comment:1 Changed 11 years ago by skata@…

Resolution: fixed
Status: newclosed

I have just fixed a stupid bug in my patch and executed "darcs send". (Well, I guess I didn't understand this bug tracking system.)

comment:2 Changed 11 years ago by simonmar

Milestone: 6.4.2

I don't seem to have received your patch. could you send it again, or attach it to this bug? Thanks.

comment:3 Changed 11 years ago by skata@…

Seemingly I am not allowed to post to libraries@…, but because the patch is not long I paste the result of "darcs send --output FILE" here. Thank you.

New patches:

[bugfix for TH.pprint not printing parentheses of higher-order functions**20060605070154
 fix for Problem 1 of Ticket #779
] {
hunk ./Language/Haskell/TH/Ppr.hs 287
-pprTyApp (ArrowT, [arg1,arg2]) = sep [ppr arg1 <+> text "->", ppr arg2]
+pprTyApp (ArrowT, [arg1,arg2]) = sep [ppr' arg1 <+> text "->", ppr arg2]
+  where ppr' t@(AppT (AppT ArrowT _) _) = parens (ppr t)
+        ppr' t                         = ppr t

[bugfix for TH.pprint not parenthesizing operators used as functions**20060605070623
 fixes Problem 2 of Ticket #779
] {
hunk ./Language/Haskell/TH/Ppr.hs 79
+isOp :: Exp -> Bool
+isOp (VarE v) = case nameBase v of c:_ | c `elem` "!#$%&~=|+*<>?-^@:./\\"
+                                                    -> True
+                                   _                -> False
+isOp (ConE c) = case nameBase c of ':':_ -> True
+                                   _     -> False
+isOp _        = False
hunk ./Language/Haskell/TH/Ppr.hs 91
-pprExp i (AppE e1 e2) = parensIf (i >= appPrec) $ pprExp opPrec e1
-                                              <+> pprExp appPrec e2
+pprExp i (AppE e1 e2)
+ = parensIf (i >= appPrec) $ parensIf (isOp e1) (pprExp opPrec e1)
+                         <+> pprExp appPrec e2


[Drop dependency to haskell98 package
Einar Karttunen <>**20060209224626]
[TAG Initial conversion from CVS complete
John Goerzen <>**20060112154138]
Patch bundle hash:

Changed 11 years ago by skata@…

Attachment: pprint.patch added

comment:4 Changed 9 years ago by simonmar

Architecture: MultipleUnknown/Multiple

comment:5 Changed 9 years ago by simonmar

Operating System: MultipleUnknown/Multiple

comment:6 Changed 8 years ago by simonmar

difficulty: Easy (1 hr)Easy (less than 1 hour)
Note: See TracTickets for help on using tickets.