Obtaining location of subexpressions from Template Haskell

Dear Haskell-café, I am trying to write a small TH module which manipulates Haskell code. Basically, I have a function `transform :: Exp -> Q Exp` which I call this way:
g = $(transform [| map (+1) [1,2,3] |]) Since this is a source-to-source transformation, I would like to generate also {-# LINE ... #-} pragmas to point any error back to their original location. My question is: is there any way to obtain the location of sub-expressions inside a `Exp` or `Q Exp`?
Thanks in advance, Alejandro

Dear Alejandro,
I'd also be interested in this.
I haven't had the chance to use `{-# LINE ... #-}` pragmas before, but I do
have some experience with TH.
It might help us to get closer to a solution if you described what the
transformation is actually doing, and/or include some examples of before
and after transformation. It could at the very least help me understand
more of the context.
Thinking further about the question, it seems to me that as soon as our
expression has already been transformed to `Exp` (or `Q Exp`) all
positional and formatting information is lost.
But maybe there is still hope.
Have you considered using the 'haskell-src-meta' package? And/or using
QuasiQuotes to transform "String -> Q Exp"? I have a vague memory that
there is a haskell-parsing package or project out there (even if it is not
specifically 'haskell-src-meta') that does retain the positional
information that you are looking for when parsing from String (or `IsString
a`).
Once we do have line numbers, including them as pragmas seems fairly
straightforward as `PragmaD (LineP Int String) :: Dec`
Source:
https://hackage.haskell.org/package/template-haskell-2.11.0.0/docs/Language-...
Best Regards,
Milán
On Tue, Oct 18, 2016 at 2:53 PM Alejandro Serrano Mena
Dear Haskell-café, I am trying to write a small TH module which manipulates Haskell code. Basically, I have a function `transform :: Exp -> Q Exp` which I call this way:
g = $(transform [| map (+1) [1,2,3] |]) Since this is a source-to-source transformation, I would like to generate also {-# LINE ... #-} pragmas to point any error back to their original location. My question is: is there any way to obtain the location of sub-expressions inside a `Exp` or `Q Exp`?
Thanks in advance, Alejandro _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
participants (2)
-
Alejandro Serrano Mena
-
Wizek