4

Say I have this:

f x = x + 1

tt2 name o = sequence [valD (varP (mkName name)) (normalB [| f $(varE o) |]) []]

I'd like to convert tt2 to tt:

tt name o = [d| ??? = f $(varE o) |]

I cannot figure out what ??? should be. This is probably because I don't understand how TH works yet. Can someone help?

me2
  • 3,933
  • 4
  • 17
  • 16

1 Answers1

2

You're trying to splice a name or a pattern. Unfortunately, this is not possible. Template Haskell only allows splicing expressions, types and declarations, so you're stuck with doing it manually as in your original code.

See GHC #1476 for some of the reasons why pattern splices are not allowed.

hammar
  • 138,522
  • 17
  • 304
  • 385
  • Okay. Do you suppose there is a more concise way to write what I have written? – me2 May 02 '13 at 07:34
  • @me2: Not really. You could define a helper like `simpleDecl name body = sequence [valD (varP (mkName name)) (normalB body) []]` if you need to do this in multiple places, but that's about it. – hammar May 02 '13 at 07:40