Template Haskell newbie questions

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

2007/9/22, Hugo Pacheco
Hi all,
If I want to write some function that will dynamically create a selection function according to its arguments and do something with it. You cannot dynamically create function, all you can is to create it at compile-time (that's what TH lets you to do).
However, is there a possible way to generate Haskell functions from TH without having to instanciate its arguments? First of all, how do you intend to use your fsel function? In fact if we ignore stage restirctions expression $(sel x y) will have different types depending on fsel parameters. Even if we can generate code at runtime, we cannot assign a type to the $(sel x y) expression (except something like Dynamic, but doubt if it makes sence). Even if we can assign a type to the $(sel x y) expression, the $(sel x y) (x, y) expression is well-typed only if $(sel x y) has type (a, b) -> c.
-- WBR, Max Vasin JID: maxvasin@jabber.ru

The sel function was just the simpliest example I remembered of. Yes, I would need to generate code at runtime according since the generated code would depend on the function arguments, but have already guessed It wouldn't be possible. Anyway, thanks for the clarification, hugo

Hello Hugo, Tuesday, September 25, 2007, 1:05:28 PM, you wrote:
Yes, I would need to generate code at runtime according since the
you have selected improper instrument for it. look at GHC-as-a-lbrary and hs-plugins by Donald Stewart -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

hs-plugins does look promising. thanks for the hint, hugo
participants (3)
-
Bulat Ziganshin
-
Hugo Pacheco
-
Max Vasin