Skip to content

Inconsistent multiple declarations error in Template Haskell quotations

Summary

Consider this code:

{-# LANGUAGE QuasiQuotes, TemplateHaskell, DuplicateRecordFields #-}

import Language.Haskell.TH

left = undefined

([] <$) $ runIO . print =<< [d|
  data Tree
    = Node { left :: Tree, right :: Tree }
    | Leaf { value :: Int }
    deriving Show
  |]

It produces an error about multiple declarations of left:

THAG.hs:9:14: error:
    • Multiple declarations of ‘left’
      Declared at: THAG.hs:5:1
                   THAG.hs:9:14
    • In the Template Haskell quotation
        [d| data Tree
              = Node {left :: Tree, right :: Tree} | Leaf {value :: Int}
              deriving Show |]
  |
9 |     = Node { left :: Tree, right :: Tree }
  |              ^^^^

I think this should not happen because the quotation doesn't actually bring anything into scope. And this similar code does not produce an error:

{-# LANGUAGE QuasiQuotes, TemplateHaskell, DuplicateRecordFields #-}

import Language.Haskell.TH

data Tree
  = Node { left :: Tree, right :: Tree }
  | Leaf { value :: Int }
  deriving Show

([] <$) $ runIO . print =<< [d|
  data Tree
    = Node { left :: Tree, right :: Tree }
    | Leaf { value :: Int }
    deriving Show
  |]

Either both should produce errors or neither should. I would prefer neither because I would like to exploit this for implementing something akin to duplicate record fields using Template Haskell.

In fact I don't think quotations should produce any errors except for parsing errors. A related problem I encountered is that the quotation [d| foo :: Bool |] will produce an error about a missing accompanying binding, but what if I want to define that binding automatically using Template Haskell? Should I open another issue for this?

Environment

  • GHC version used: 8.10.4

Optional:

  • Operating System: Debian 10
  • System Architecture: x86_64
Edited by Jaro Reinders
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information