-
Notifications
You must be signed in to change notification settings - Fork 84
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
Happy and template haskell #149
Comments
I have a prototype of an EDSL to generate Happy grammars:
With visible
The next thing is to make For that, I also have a branch... but I'd need to dust it off. If anyone wants to collaborate on this, let me know. |
Realistically, I'll probably be too busy to do much for a bit (still rushing to get multi-package done for 8.10 but maybe I should let that go; you already saw me get preempted from the pattern binds thing :)). But I would definitely like to collaborate on this long term.
Could we retroactively add an TTG-style extra variant to data Exp' a = .... | XExp a
data Exp = Exp' Void I think that could just squeeze by without a change to GHC itself. Then one can use the TH pretty printer with your own custom nodes (which just need to be pretty printed). I believe this would be the ideal way to support such a thing.
Do open a WIP PR branch, even if neither of us has time to complete the dusting! |
I had quite a bit of catch up to do, so sorry for the late response. In general, I'm actually quite happy with the EDSL provided by happy. Sure, the syntax carries historic cruft from 4 decades, but I'm not sure how you generally want to improve on what it does. It's essentially an external -1 stage.
What kind of "convention" do you have in mind? Just using rule numbers (e.g.
I don't follow. How is separation of rule actions affecting the grammar? How is this even desirable, given that slight refactorings to the grammar may have non-local effect on the state machine? To me, that's exactly the beauty of keeping the control component opaque: We don't have to worry about how inlining, new syntax etc. changes the state machine. Any attempts to "modify the grammar before parser codegen" risks changing the parse, possibly even the language. That's exactly why I'm not too fond of parser combinators, by the way: Although intriguingly direct and simple, the programmer directly meddles with the control component and it's hard to tell if the accepted language is still the same after a non-trivial change. I think the reason for my gut feeling is that I rather try to comprehend formal grammars (as they are defined in a language report) rather than code using parser combinators (which necessarily will intertwine hacks and semantic actions with the relevant bits), and it's nigh impossible to make the connection from parser combinators back to the formal grammar. With parser generators, you get all of these guarantees for free!
That's nice and all, and will probably attract users that are unfamiliar happy syntax and unwilling to learn the basics. And I'd say it's definitely an improvement over happy syntax and worth a new frontend, if at one point it allows for all the customisation that happy does (operator precedence, custom monads, usage of language extensions inside splices, to name a few). But to me it's rather the same as using happy directly, as far as GHC is concerned: Grammars seldomly fit on one page and it will always be hard to find where an ambiguity stems from. And once your eye is trained to parse happy syntax, I'm compelled to think that reading
I don't understand this point and why it entails changing happy's backend, but I'm also largely unfamiliar with its codebase... |
Type-checking the source file instead of the generated file is actually the primary reason that I think an EDSL is superior to a generator. |
@sgraf812 You can think of what I'd like to do with the separate grammars being "grammar combinators" rather than "parser combinators". There's still a complete phase separation between code that manipulates the grammar and the parser itself, so the evalulated grammar gets all the same ambiguity/conflicts checks (which are non-local just like state machine changes). |
@int-index still interested in seeing your branch. I just toyed with master...Ericson2314:template-haskell, making me very sad over just how few places Template Haskell allows splices. |
@Ericson2314 I just checked a couple of places where my local copy of |
Indeed, and in my branch I was using custom combinators to produce the code. It didn't look all that good. Maybe we could try extending TH quotes first, so that Haskell has better macro facilities |
@int-index Yeah I think basically writing the code we want to write, and then enumerating the missing features, would be an excellent way to make clear to others some of the biggest problems TH actually faces. I just worry though that the proper order of yak shaves is to do something about GHC's CI first, as with the current level of productivity GHC developer time is too scarce for a "side quest" of this magnitude :/. |
I hope via ghc-proposals/ghc-proposals#243 to make GHC able to use TH, but as GHC is not the sole user of happy, I figured I can get the ball rolling on brainstorming ideas now.
The first change I think would be to allow separating the grammar from the user written code. Happy could turn a
.y
files without any Haskell in between the braces into its representation of the grammar. Then, in another module, one writes all the production eliminators, named according to some convention. Finally, one imports happy as a library along with that grammar, and has a splice to generate what comes out of.y
today.Now, while the separation of grammar and eliminator seems like classic nice separating concerns, I'll wholly admit that using multiple completely separate ASTs with the same grammar is not likely to be useful in practice---it would be better to write a polymorphic AST and single set of polymorphic production eliminators.
The real benefit is now we have a chance to programmatically modify the grammar before parser codegen. Things like parameterized/macro rules and inlining (#148) could perhaps be done without as much, or any, support from Happy itself. User manipulation of the grammar could also allow it to be nicely split between multiple files, or more broadly, be compositionally constructed.
CC @sgraf812 @harpocrates
The text was updated successfully, but these errors were encountered: