Opened 2 years ago

Last modified 16 months ago

#14032 new bug

Can't splice TH quote with infix declaration for name in two different namespaces

Reported by: RyanGlScott Owned by: RyanGlScott
Priority: normal Milestone:
Component: Template Haskell Version: 8.0.1
Keywords: GHCProposal 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:


Spun off from #13799 (and This code compiles:

{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators #-}

infix 5 :*:
data a :*: b = a :*: b

But this code does not:

{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators #-}

$([d| infix 5 :*:
      data a :*: b = a :*: b
$ /opt/ghc/8.2.1/bin/ghc -ddump-splices Bug.hs
[1 of 1] Compiling Main             ( Bug.hs, Bug.o )
Bug.hs:(4,3)-(6,6): Splicing declarations
    [d| infix 5 :*:_anM, :*:_anL
        data a_anN :*:_anL b_anO = a_anN :*:_anM b_anO |]
    infix 5 :*:_a3IL
    infix 5 :*:_a3IK
    data (:*:_a3IK) a_a3IM b_a3IN = a_a3IM :*:_a3IL b_a3IN

Bug.hs:4:3: error:
    Multiple fixity declarations for ‘:*:_a3IL’
    also at  Bug.hs:(4,3)-(6,6)
4 | $([d| infix 5 :*:
  |   ^^^^^^^^^^^^^^^...

Inspecting the -ddump-splices output reveals why: when infix 5 :*: is renamed, because :*: refers to two different names in both the value and type namespaces, it actually gets renamed to infix 5 :*:_anM, :*:_anL, where :*:_anM and :*:_anL are the same name with different uniques.

Normally, this isn't a problem, since feeding infix 5 :*:_anM, :*:_anL to the typechecker works fine. However, when it's spliced in via Template Haskell, it gets fed back into the renamer, where it believes that :*:_anM and :*:_anL are duplicate names, causing the error.

Really, this is just a symptom of the fact that infix declarations are insufficiently powerful to encode information for identical names in different namespaces. But until GHC gains this ability, we need to find some workaround for this problem. My hunch is that we'll need to give GHC the power to recognize these sorts of duplicate fixity declarations in Convert and only emit one actual fixity declaration per set of duplicates. (Disclaimer: I haven't worked out all the fiddly details yet.)

Change History (4)

comment:1 Changed 2 years ago by goldfire

I'm not feeling like adding yet another ghc-proposal to my queue at the moment, but this really should be fixed at the language level. I propose type infix 5 :*: to mean a fixity declaration on a type, and then to deprecate and eventually eliminate the current behavior of a fixity declaration that applies to both levels simultaneously (while respecting the 3-release policy). If you (meaning you, the reader of this comment) agree and have the time, make this into a proper proposal! :)

comment:2 Changed 2 years ago by RyanGlScott

Owner: set to RyanGlScott

I'll draft up a proposal when I get a chance.

comment:3 Changed 2 years ago by RyanGlScott

comment:4 Changed 16 months ago by RyanGlScott

Keywords: GHCProposal added
Note: See TracTickets for help on using tickets.