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

Wizek 123.wizek at gmail.com
Sun Oct 23 09:29:08 UTC 2016


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-Haskell-TH.html#t:Dec

Best Regards,
Milán


On Tue, Oct 18, 2016 at 2:53 PM Alejandro Serrano Mena <trupill at gmail.com>
wrote:

> 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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20161023/85d29239/attachment.html>


More information about the Haskell-Cafe mailing list