Template Haskell Splicing

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Template Haskell Splicing

satvik chauhan
Is there any way to splice declarations inside where?  If not, then what is the reason for not supporting this?

-Satvik


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Template Haskell Splicing

Michael Sloan
I don't think that there is a particular reason for not supporting quasi-quotes in where clauses..  It should be added!

The reason for /splices/ to not be supported in here statements is that they are run during type checking.  That way calls to "reify" can access type information for things before your splice.  It also allows checking any AST quotes used inside your splice.  Since type-checking comes after renaming, splices can't be used in patterns (because it would affect the lexical scope).

Quasi-quotes, on the other hand, are run in the renamer, and ought to be able to be used in where clauses.  Yet for some reason they can't - I get "parse error (possibly incorrect indentation or mismatched brackets)" when I try to put one under a where.

Good catch!
-Michael


On Fri, Dec 14, 2012 at 11:09 PM, satvik chauhan <[hidden email]> wrote:
Is there any way to splice declarations inside where?  If not, then what is the reason for not supporting this?

-Satvik


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Template Haskell Splicing

satvik chauhan


On Sat, Dec 15, 2012 at 1:30 PM, Michael Sloan <[hidden email]> wrote:
I don't think that there is a particular reason for not supporting quasi-quotes in where clauses..  It should be added!

The reason for /splices/ to not be supported in here statements is that they are run during type checking.  That way calls to "reify" can access type information for things before your splice.  It also allows checking any AST quotes used inside your splice.  Since type-checking comes after renaming, splices can't be used in patterns (because it would affect the lexical scope).

Quasi-quotes, on the other hand, are run in the renamer, and ought to be able to be used in where clauses.  Yet for some reason they can't - I get "parse error (possibly incorrect indentation or mismatched brackets)" when I try to put one under a where.

Good catch!
-Michael

Yeah, that is the problem. I have a function inside which I need to generate some declarations using TH. I can not generate these at the top level as these generations depend on the function's parameters which are local to the function. 

Something like 

f p1 p2= ...
  where 
-- this has to be generated by TH
     g_1 = p1 
     g_2 = p2 
     g_3 = p1 `xor` p2 

something like the above. In the above I have shown only 2 parameters but in my case it is much more. I am able to get the above splice as toplevel declaration but I am still unsuccessful in getting it inside the where. 
I can always make `g` a function and take parameters of `f` as arguments in the top level splice but that will defeat the purpose of optimization here(which I am trying to do), as that would result in a function call every time I use `g` instead of above variables.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Template Haskell Splicing

Adam Vogt
On Sat, Dec 15, 2012 at 9:24 AM, satvik chauhan <[hidden email]> wrote:

> Yeah, that is the problem. I have a function inside which I need to generate
> some declarations using TH. I can not generate these at the top level as
> these generations depend on the function's parameters which are local to the
> function.
>
> Something like
>
> f p1 p2= ...
>   where
> -- this has to be generated by TH
>      g_1 = p1
>      g_2 = p2
>      g_3 = p1 `xor` p2
>
> something like the above. In the above I have shown only 2 parameters but in
> my case it is much more. I am able to get the above splice as toplevel
> declaration but I am still unsuccessful in getting it inside the where.
> I can always make `g` a function and take parameters of `f` as arguments in
> the top level splice but that will defeat the purpose of optimization
> here(which I am trying to do), as that would result in a function call every
> time I use `g` instead of above variables.

Hi Satvik

Perhaps you could put the variables whose evaluations are shared in a 'let':

> f p1 p2 = $(mkG [| g_1 |])
> mkG body = liftM2 LetE [d| g_1 = $(dyn "p1") |] body

The above example won't work exactly because the two occurences of g_1
are different names. But you could replace the [d|  |] with something
that has a [Dec] with variables defined in such a way that they can be
captured.

On a somewhat unrelated note, GHC is less able to infer types for
expression splices than for top level bindings. The issue had
something to do with needing to typecheck all the $( x :: ExpQ ) as a
group, even if the values could be defined in separate modules. It
might not be an issue in your case, but one possible way around it is
have the definition of 'f p1 p2 = ... ' done by template haskell. But
then there is the issue that top level splices are run in order, so


-- this doesn't work:
{-# LANGUAGE TemplateHaskell #-}
[d| y = x |]
[d| x = 1 |]


-- This does work:
{-# LANGUAGE TemplateHaskell #-}
[d| x = 1 |]
[d| y = x |]

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe