Skip to content

More incorrect Template Haskell parenthesization

The latest installment of "RyanGlScott finds bugs in Template Haskell pretty-printing". Here is what's featured on today's episode:

{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -ddump-splices #-}
module Bug where

import Data.Functor.Identity
import Language.Haskell.TH

$([d| f = \(Identity x) -> x |])
$([d| g = $(pure $ VarE '(+) `AppE` LitE (IntegerL (-1)) `AppE` (LitE (IntegerL (-1)))) |])

Running this with GHC 8.2 or later yields some incorrectly parenthesized output:

$ /opt/ghc/8.2.2/bin/ghc Bug.hs
[1 of 1] Compiling Bug              ( Bug.hs, Bug.o )
Bug.hs:8:3-31: Splicing declarations
    [d| f_azO = \ (Identity x_azP) -> x_azP |]
  ======>
    f_a3Nx = \ Identity x_a3Ny -> x_a3Ny
Bug.hs:9:3-90: Splicing declarations
    [d| g_a3NU
          = $(pure
                $ VarE '(+) `AppE` LitE (IntegerL (- 1))
                    `AppE` (LitE (IntegerL (- 1)))) |]
    pending(rn) [<splice_a3NV, pure
                                 $ VarE '(+) `AppE` LitE (IntegerL (- 1))
                                     `AppE` (LitE (IntegerL (- 1)))>]
  ======>
    g_a4dU = ((+) -1) -1

In particular, look at these two lines:

f_a3Nx = \ Identity x_a3Ny -> x_a3Ny
g_a4dU = ((+) -1) -1

These should be:

f_a3Nx = \ (Identity x_a3Ny) -> x_a3Ny
g_a4dU = ((+) (-1)) (-1)
Trac metadata
Trac field Value
Version 8.2.2
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Template Haskell
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information