Locate errors in TH for Haskell code

I have a task to convert one Haskell expression to another. And I'd like to report errors properly. [interpret| -- error reported here valid haskell code invalid code <- error |] I can parse it with haskell-src-exts or haskell-src-meta. but if I have error in the code (type-check) error is positioned on the first line of the QQ-expression. Do you know is it possible to report error at the line where it has happened or to keep original source code location in the Exp. In the function I do transformation Exp -> Exp. Or maybe there is a better way to do it instead of QQ?

Hi Anton, I don't think you're going to be able to do what you want here. :( Template Haskell has no facility to keep track of locations of bits of code. So, when you've processed your Exp and then spliced it in, there are no locations to record, other than the location of the entire splice (which is poor, I know). This has been requested before: https://gitlab.haskell.org/ghc/ghc/-/issues/10330. The problem is that the fix is really quite invasive (it would require adding location information to every constructor in TH, affecting every TH user), and so we've been hesitant to plow forward. If you were to collect a chorus of voices who all wanted this (and were willing to help flesh out the design), it's something we can do -- but I'd want to know it was a popular direction before imposing it on every TH user out there. Richard
On Nov 6, 2021, at 5:48 AM, Anton Kholomiov
wrote: I have a task to convert one Haskell expression to another. And I'd like to report errors properly.
[interpret| -- error reported here valid haskell code invalid code <- error |]
I can parse it with haskell-src-exts or haskell-src-meta. but if I have error in the code (type-check) error is positioned on the first line of the QQ-expression.
Do you know is it possible to report error at the line where it has happened or to keep original source code location in the Exp.
In the function I do transformation Exp -> Exp.
Or maybe there is a better way to do it instead of QQ? _______________________________________________ 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.

Isn't the promise of trees-that-grow work to use the same Expr type for TH as well? Then we could get the source locations, don't we? - Oleg On 10.11.2021 21.26, Richard Eisenberg wrote:
Hi Anton,
I don't think you're going to be able to do what you want here. :(
Template Haskell has no facility to keep track of locations of bits of code. So, when you've processed your Exp and then spliced it in, there are no locations to record, other than the location of the entire splice (which is poor, I know).
This has been requested before: https://gitlab.haskell.org/ghc/ghc/-/issues/10330. The problem is that the fix is really quite invasive (it would require adding location information to every constructor in TH, affecting every TH user), and so we've been hesitant to plow forward. If you were to collect a chorus of voices who all wanted this (and were willing to help flesh out the design), it's something we can do -- but I'd want to know it was a popular direction before imposing it on every TH user out there.
Richard
On Nov 6, 2021, at 5:48 AM, Anton Kholomiov
wrote: I have a task to convert one Haskell expression to another. And I'd like to report errors properly.
[interpret| -- error reported here valid haskell code invalid code <- error |]
I can parse it with haskell-src-exts or haskell-src-meta. but if I have error in the code (type-check) error is positioned on the first line of the QQ-expression.
Do you know is it possible to report error at the line where it has happened or to keep original source code location in the Exp.
In the function I do transformation Exp -> Exp.
Or maybe there is a better way to do it instead of QQ? _______________________________________________ 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.
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.

I'd like to know more about what you are doing, I feel like I've set out to
do something similar in the past and ended up going a different way.
On Sat, Nov 6, 2021 at 2:51 AM Anton Kholomiov
I have a task to convert one Haskell expression to another. And I'd like to report errors properly.
[interpret| -- error reported here valid haskell code invalid code <- error |]
I can parse it with haskell-src-exts or haskell-src-meta. but if I have error in the code (type-check) error is positioned on the first line of the QQ-expression.
Do you know is it possible to report error at the line where it has happened or to keep original source code location in the Exp.
In the function I do transformation Exp -> Exp.
Or maybe there is a better way to do it instead of QQ? _______________________________________________ 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.

I'm doing basic transformation of expressions like:
```
and [a, b, c]
```
to
```
if a then (if b then c else False) else False
```
And similar for or.
So I transform the haskell code. I need it for optimisation of Plutus
execution which features different model of execution although it's valid
haskell code.
@Richard thanks for the detailed answer!
I could achieve something for my own language embeded in QQ but it was
because I had my own type-checker for that and kept the locations.
In haskell source code I could not find that and now I know that it's
intentional.
чт, 11 нояб. 2021 г. в 03:19, David Fox
I'd like to know more about what you are doing, I feel like I've set out to do something similar in the past and ended up going a different way.
On Sat, Nov 6, 2021 at 2:51 AM Anton Kholomiov
wrote: I have a task to convert one Haskell expression to another. And I'd like to report errors properly.
[interpret| -- error reported here valid haskell code invalid code <- error |]
I can parse it with haskell-src-exts or haskell-src-meta. but if I have error in the code (type-check) error is positioned on the first line of the QQ-expression.
Do you know is it possible to report error at the line where it has happened or to keep original source code location in the Exp.
In the function I do transformation Exp -> Exp.
Or maybe there is a better way to do it instead of QQ? _______________________________________________ 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 (4)
-
Anton Kholomiov
-
David Fox
-
Oleg Grenrus
-
Richard Eisenberg