[Template-haskell] Generating function implementations from a list of patterns

Lars Kotthoff larsko at cs.st-andrews.ac.uk
Sun Nov 29 11:27:45 EST 2009


Hi list,

 I'm trying to generate function implementations given a list of patters to
match the function argument. For example, given

data Foo = Foo Int | Bar Int
foobar (Foo i) = i
foobar (Bar i) = i

The body of the "foobar" function is the same for all the patterns, therefore
I'd like to generate it. In particular, I'd like to be able to write something
like (pseudo-template-haskell)

$(genFunc ''foobar [(''Foo i), (''Bar i)] i)

i.e. have a function genFunc which takes the name of the function to define, the
list of patterns and the function body as arguments. I've played around with the
example for deriving Show on the wiki, but I didn't really get anywhere.

Any pointers how to do this?

Thanks,

Lars


More information about the template-haskell mailing list