Skip to content

Empty constraint tuple error with typeclass instance in TemplateHaskellQuotes

Take the following module:

{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE Haskell2010 #-}

import Language.Haskell.TH.Syntax

class Foo a

instance () => Foo [a]

$([d| instance () => Foo Char |])

$([d| instance $(pure $ TupleT 0) => Foo Int |])

main = pure ()

I would expect all of this to compile without an error. But in practice this I get this error with GHC 9.6:

T.hs:10:2: error: [GHC-77539]
    • Illegal tuple constraint: () :: Constraint
    • In the instance declaration for ‘Foo Int’
    Suggested fix: Perhaps you intended to use ConstraintKinds
   |
10 | $([d| instance $(pure $ TupleT 0) => Foo Int |])
   |  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The InstanceD constructor has a [Type] field to represent the list of constraints, but it looks like splicing requires a Q Type. Using type tuples either using [t|...|] or using TupleT seems to lead to this ConstraintKinds error.

It should be possible to do this without ConstraintKinds given that the non-TH instance works fine.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information