[Haskell-cafe] Scraping boilerplate deriving?

Sean Leather leather at cs.uu.nl
Tue Sep 14 04:58:22 EDT 2010


On Tue, Sep 14, 2010 at 10:24, Kevin Jardine wrote:

> I have a set of wrapper newtypes that are always of the same format:
>
> newtype MyType = MyType Obj deriving (A,B,C,D)
>
> where Obj, A, B, C, and D are always the same. Only MyType varies.
>
> A, B, C, and D are automagically derived by GHC using the
>
> {-# LANGUAGE GeneralizedNewtypeDeriving #-}
>
> feature.
>
> I would like to use some macro system (perhaps Template Haskell?) to
> reduce this to something like
>
> defObj MyType
>
> I've read through some Template Haskell documentation and examples,
> but I find it intimidatingly hard to follow. Does anyone has some code
> suggestions or pointers to something similar?
>

This works in TH:

> [d|newtype Blah = Blah Int deriving (Num,Show,Eq)|]

But the parameterized variations on this theme do not:

> derive1 name = [d|newtype $name = Blah Int deriving (Num,Show,Eq)|]
Malformed head of type or class declaration

> derive2 name = [d|newtype Blah = $name Int deriving (Num,Show,Eq)|]
parse error in data/newtype declaration

I think it has something to do with the type of the splice. Perhaps you can
look into further:
http://www.haskell.org/ghc/docs/6.12.2/html/users_guide/template-haskell.html

Sean
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100914/88fb1669/attachment.html


More information about the Haskell-Cafe mailing list