[Haskell-cafe] parametrized data types and Template Haskell

Maarten Faddegon haskell-cafe at maartenfaddegon.nl
Thu Dec 26 11:36:03 UTC 2013


Dear Cafe,

Hope you all had a nice Christmas.

I have been playing with generating method instances using Template 
Haskell but am a bit stuck now trying to generate an instance for a 
parametrized data type.

I would like to generate the following:

 > instance (MyClass a) => MyClass (Tree a) where
 >       mymethod _  = "todo"

I defined a genMyClassInstance that is working fine for unparametrized 
data types, but clearly there is nothing here that inserts the '(MyClass 
a) =>' part here. My first question is: how should I instruct Template 
Haskell to insert the beforementioned code when appropriate?

 > genMyClassInstance :: Name -> Q [Dec]
 > genMyClassInstance name
 >        = [d|instance MyClass $(conT name) where
 >                mymethod _ = "todo"
 >          |]

My second question is how to pass the Name of a parametrized data type? 
I tried the following, but GHC does not seem to like that: "Not in 
scope: type constructor or class `Tree a' Perhaps you meant `Tree'"

 > $(genMyInstance (mkName "Tree a"))

Thank you!

Maarten Faddegon


More information about the Haskell-Cafe mailing list