[Haskell-cafe] Obtaining location of subexpressions from Template Haskell

12 views
Skip to first unread message

Alejandro Serrano Mena

unread,
Oct 18, 2016, 8:53:54 AM10/18/16
to haskell-cafe
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

Wizek

unread,
Oct 23, 2016, 5:29:26 AM10/23/16
to Alejandro Serrano Mena, haskell-cafe
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`

Best Regards,
Milán


_______________________________________________
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.
Reply all
Reply to author
Forward
0 new messages