[Haskell-cafe] Template Haskell newbie questions

Hugo Pacheco hpacheco at gmail.com
Sat Sep 22 13:56:08 EDT 2007


Hi all,
I'm try to write some function in TH that I don't even know if it is
possible.


Consider the example from the tutorials

sel 1 2 = [| \(x,_) -> x |]

sel 2 2 = [| \(_,x) -> x |]


If I want to write some function that will dynamically create a selection
function according to its arguments and do something with it.


fsel :: Int -> Int -> x
fsel x y = $(sel x y) ...

Imagine a stupid case for

fsel :: Int -> Int -> Int
fsel x y = $(sel x y) (x,y)

The compiler complains that

GHC stage restriction: `x'
      is used in a top-level splice, and must be imported, not defined
locally
    In the first argument of `sel', namely `x'
    In the expression: $[splice](sel x y) (x, y)
    In the definition of `fsel': fsel x y = $[splice](sel x y) (x, y)

what, indeed, makes sense.

However, is there a possible way to generate Haskell functions from TH
without having to instanciate its arguments?

Thanks,
hugo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20070922/602b107f/attachment.htm


More information about the Haskell-Cafe mailing list