Skip to content

Can't capture classes inside a template haskell type quotation

GHC rejects the following:

{-# LANGUAGE TemplateHaskell #-}
import Language.Haskell.TH
class C a where
data X = X
fmap return $ instanceD (cxt []) [t| C $(conT ''X) |] []

with the error:

test.hs:5:23:
    Class `C' used as a type
    In the Template Haskell quotation [t| C $(conT 'X) |]
    In the second argument of `instanceD', namely `[t| C $(conT 'X) |]'
    In the expression: instanceD (cxt []) [t| C $(conT 'X) |] []

This is frustrating, since that quotation would have resulted in exactly the TH AST fragment I wanted, had the error not occurred. Instead I must resort to:

instanceD (cxt []) (appT (conT ''Storable) (conT ''X)) []

Incidentally, the error message produced is wrong: The error says 'X whereas I wrote ''X.

Trac metadata
Trac field Value
Version 6.12.1
Type FeatureRequest
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