Opened 8 years ago

Closed 8 years ago

Last modified 4 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: Difficulty: Easy (less than 1 hour)
Test Case: Blocked By:
Blocking: Related Tickets:

Description

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)
GHC.Base.. GHC.Base.id GHC.Base.id

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@… 8 years ago.

Download all attachments as: .zip

Change History (7)

comment:1 Changed 8 years ago by skata@…

  • Resolution set to fixed
  • Status changed from new to closed

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 8 years ago by simonmar

  • Milestone 6.4.2 deleted

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

comment:3 Changed 8 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
skata@cs.miyazaki-u.ac.jp**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
skata@cs.miyazaki-u.ac.jp**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
}

Context:

[Drop dependency to haskell98 package
Einar Karttunen <ekarttun@cs.helsinki.fi>**20060209224626]
[TAG Initial conversion from CVS complete
John Goerzen <jgoerzen@complete.org>**20060112154138]
Patch bundle hash:
6fe9ad707d47f4b7c0ceb127bf8c0ba1e461a0dc

Changed 8 years ago by skata@…

comment:4 Changed 6 years ago by simonmar

  • Architecture changed from Multiple to Unknown/Multiple

comment:5 Changed 6 years ago by simonmar

  • Operating System changed from Multiple to Unknown/Multiple

comment:6 Changed 4 years ago by simonmar

  • Difficulty changed from Easy (1 hr) to Easy (less than 1 hour)
Note: See TracTickets for help on using tickets.