New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement a lexer for the R language in Haskell #369
Comments
Unless R has a gnarly lexical structure, I would go for this. The manual makes it look pretty civilized, even if you have to write a parser as well as a lexer. |
Happening upon this thread only by luck... but would it make sense to run R in a separate process and then send the code to it via a pipe of some sort? That would sound like it would respect R's sensitive runtime while not reimplementing something annoying. |
Yep, that is a feasible approach as well. Thanks for bringing it up! |
Would that work on windows? |
It could be made to work in windows. Another thing to consider is that if we go with a lexer (one that only scans enough to get quoted variables), parsing errors would be deferred until runtime. Is this acceptable? A full lexer and a parser do look like they will take some more trouble to get right. |
@facundominguez it seems to work just fine with GHC 9 and the latest stackage nightly, both locally and on the CI: https://github.com/tweag/HaskellR/runs/4627191190?check_suite_focus=true I can't test locally with GHC 8.10.6 unfortunately; with nix it fails with So I'm inclined to close this unless someone can reproduce it with GHC 9+. |
Hello! Perhaps you could reproduce it by reverting this commit: 97e99d0 I checked that the GHC modifications that broke inline-r are still present in ghc-9.2.1. |
Given that TH splices don't run in the main thread still, I think we are being unlucky. If the failure isn't visible at runtime, we have at least a design flaw that still would deserve being fixed. Also, one thing to try is running ghc with |
I just checked the source code of ghc-9.0.1, which is the actual version that the The 9.0.2 release also includes the breaking changes: https://www.haskell.org/ghc/blog/20211225-ghc-9.0.2-released.html |
@facundominguez @idontgetoutmuch I still cannot reproduce the issue on GHC 9.2.1, either locally or on CI, including on Mac OS (see e.g. this job). However, I have now made a change which uses an external R interpreter process instead of an embedded one; it's currently on the ghc-9.2.1 branch. Could you please test it and see if it fixes the issue for you? |
My way of reproducing was CI. So given that CI is green, your fix is looking good. I had a look at fe9b5bd, and it matches what I expected. One thing I didn't anticipated was the need to write a temporary file with the quasiquote contents, but likely this is necessary to avoid escaping issues if one were to construct an R string instead. Perhaps there is some way to feed stdin as a file: https://stackoverflow.com/questions/9370609/piping-stdin-to-r |
Well, the CI was green even before the fix — see the link above.
The problem isn't so much to read the input from stdin, but the fact that I'm feeding the R script itself through stdin to the interpreter. The alternative would be to introduce a run-time dependency (with the help of Cabal) on the installed |
Right, I was reading the git history backwards. In that case looks like I can't help much reproducing it. I'm trying ghc-9.0.2 here though.
I see. In that case, and in the absence of further issues, I'd keep it as is. |
I think I managed to reproduce the failure with ghc-9.0.2 in this build.
|
For me this now works on 9.2.1. |
We discovered in #368 that
inline-r
stopped working withghc-8.10.6
in MacOS.Abstracting over low-level details, the R runtime started complaining that the Template Haskell code is running in an inappropriate thread.
inline-r
uses quasiquotes to embed R code, and then uses Template Haskell to generate the calls to execute the code in the R runtime. One of the tasks that Template Haskell accomplishes is parsing the R code to extract from it the quoted variables that must be marshaled from the surrounding Haskell context.The parser is implemented itself in the R runtime, so we have Template Haskell invoke the R runtime to do the parsing. Now the R runtime is very picky and wants to run in the main thread of the program invoking it, and it turns out that a patch in
ghc-8.10.6
started running TH splices in a forked thread.I imagine that we could embrace this constraint, and demand of ghc that it provides some flag to recover the behavior up-to
ghc-8.10.4
. But perhaps it would work better to just give up trying to run the R runtime in Template Haskell, and instead do our lexing in Haskell. After all, we don't expect the tokens of the R language to change a lot over time. What do you think?The text was updated successfully, but these errors were encountered: