Skip to content

Two TH pretty-printer bugs when printing PatternSynonyms declarations

Summary

Template Haskell pretty-printer emits invalid syntax for:

  • type signatures of pattern synonym operators, and
  • all but first where clause of bidirectional pattern synonyms

Steps to reproduce

$ ghci -XTemplateHaskell -XPatternSynonyms -XViewPatterns
GHCi, version 9.8.1: https://www.haskell.org/ghc/  :? for help
ghci> Language.Haskell.TH.runQ [d|pattern (:+) :: Int -> Int -> (Int, Int); pattern x :+ y = (x, y)|] >>= putStrLn . Language.Haskell.TH.Ppr.pprint
pattern :+_0 :: GHC.Types.Int ->
                GHC.Types.Int -> (GHC.Types.Int, GHC.Types.Int)
pattern x_1 :+_0 y_2 = (x_1, y_2)
ghci> Language.Haskell.TH.runQ [d|pattern A :: Int -> String; pattern A n <- (read -> n) where {A 0 = "hi"; A 1 = "bye"}|] >>= putStrLn . Language.Haskell.TH.Ppr.pprint
pattern A_0 :: GHC.Types.Int -> GHC.Base.String
pattern A_0 n_1 <- (Text.Read.read -> n_1) where
                       A_0 0 = "hi"
                           1 = "bye"

Expected behavior

The first output should have the operator :+_0 parenthesized like so:

pattern (:+_0) :: GHC.Types.Int ->
                  GHC.Types.Int -> (GHC.Types.Int, GHC.Types.Int)
pattern x_1 :+_0 y_2 = (x_1, y_2)

The second output should repeat the name A_0 of the pattern synonym on the second line as well:

pattern A_0 :: GHC.Types.Int -> GHC.Base.String
pattern A_0 n_1 <- (Text.Read.read -> n_1) where
                       A_0 0 = "hi"
                       A_0 1 = "bye"

Environment

  • GHC version used: 9.8.1
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information