7 ms·
> Haskell is an amazing language for writing your own compiler. If you are writing a compiler in another language you should genuinely consider switching. Uhm,
by sklogic 11y ago
> Haskell is an amazing language for writing your own compiler. If you are writing a compiler in another language you should genuinely consider switching.
Uhm, no, thank you, but no. Haskell is poorly equipped for implementing compilers, because of its lack of unrestricted compile-time metaprogramming.
I'm spoiled. This is what I want from a language for implementing compilers:
1) Cheap, boilerplate-free definition for chains of slightly different ASTs and IRs. Haskell and ML are notoriously weak in this area (see the "Expression Problem" thing)
2) Boilerplate-free generation of various (potentially complex) visitors for these ASTs and IRs. For Haskell, Scrap Your Boilerplate library is the closest thing to what I want, but it is still far too clumsy.
3) I need efficient embedded Prolog and Datalog (yes, both, the latter allows some cool optimisations not possible for the former). Not feasible without compile-time metaprogramming. Both are important for simplifying compiler construction.
4) Parsing is not very important. But yet, it is necessary. The best Haskell got on offer is Parsec. Sorry, not good enough for my requirements.
- fs111 11y agoOut of curiosity, what language, if any, has those properties?
- sklogic 11y agoAny language that allows compile-time metaprogramming (and not as restricted as TH) should allow to implement all the components I want. One example of such a system is the Nanopass framework [1], used alongside with cKanren [2]. Another example is my own system [3]. [1] https://github.com/akeep/nanopass-framework https://github.com/akeep/nanopass-framework http://www.cs.indiana.edu/~dyb/pubs/nano-jfp.pdf http://www.cs.indiana.edu/~dyb/pubs/nano-jfp.pdf [2] http://scheme2011.ucombinator.org/papers/Alvis2011.pdf http://scheme2011.ucombinator.org/papers/Alvis2011.pdf [3] https://github.com/combinatorylogic/mbase https://github.com/combinatorylogic/mbase
- charlieflowers 11y ago@sklogic, I have been looking for something like MBase. Is there something like a beginner's tutorial anywhere?
- sklogic 11y agoThere is a bit outdated tutorial in https://github.com/combinatorylogic/mbase/tree/master/misc/demos https://github.com/combinatorylogic/mbase/tree/master/misc/d... Also, CLike project was supposed to be a sort of a tutorial. For a more thoroughly documented, production-ready language workbench you can also try Racket alongside with Nanopass framework.
- jberryman 11y agoWhat are your parsing requirements?
- sklogic 11y agoPretty basic: graceful error recovery, arbitrarily complex error reporting, arbitrary actions (potentially with some side effects). I.e., everything that is needed for the IDE integration and for the professional compiler frontends with usable, friendly error messages and recovery suggestions.
- dhess 11y agoTry these: https://hackage.haskell.org/package/trifecta https://hackage.haskell.org/package/trifecta https://hackage.haskell.org/package/parsers https://hackage.haskell.org/package/parsers They are meant to be used together.
- sklogic 11y agoCould you please point at their error recovery functionality (I could not find it by a quick scan)? My gut feeling is that combinator-based parsing is not very well suited for this, but I may be missing something obvious.
- dhess 11y agoThis is the chief drawback of these two libraries: they are poorly documented (and are arguably not documented at all). In general, parsers is for parsing and trifecta is for reporting errors and diagnostics. Trifecta looks especially nice for giving context to error sites, but I haven't used it much yet (only for highlighting, really). By "error recovery," do you mean backtracking? If so, there are several ways of doing that, which can be found here: https://hackage.haskell.org/package/parsers-0.12.2.1/docs/Text-Parser-Combinators.html https://hackage.haskell.org/package/parsers-0.12.2.1/docs/Te... See the 'choice', 'option', and 'try' combinators, and also the '<|>' operator in Control.Applicative. If you need more than that, you can extend parsers' monadic parsing to roll your own error recovery. Currently, the best way to understand how parsers and trifecta work is to look at projects that use them. I will give you some links to the ones I've used and found helpful, if a bit more complicated than the parsers I am currently writing: https://github.com/ekmett/ermine/tree/master/src/Ermine https://github.com/ekmett/ermine/tree/master/src/Ermine https://github.com/idris-lang/Idris-dev/tree/master/src/Idris https://github.com/idris-lang/Idris-dev/tree/master/src/Idri... And here is a relevant Reddit thread, which includes a link to Edward Kmett's slide deck which motivates trifecta/parsers and gives a high-level view of how they work: https://www.reddit.com/r/haskell/comments/2uc6kp/are_there_any_tutorials_or_introductions_to_the/ https://www.reddit.com/r/haskell/comments/2uc6kp/are_there_a... I hope this helps!
- coldtea 11y agoSo "Haskell is poorly equipped for implementing compilers" because it doesn't fit your bizarro requirements, including "efficient embedded Prolog and Datalog"...
- sklogic 11y agoHaskell is poorly equipped for implementing compilers because writing compilers in Haskell requires tons of boilerplate, which is not needed in the languages that are a much better fit for this task. Kinda obvious from a definition of what "poorly equipped" means. And why do you call these basic requirements bizarre? For example, something like a region inference is just a few lines of Prolog vs. dozens or even hundreds of lines of code in Haskell.
- coldtea 11y ago>Haskell is poorly equipped for implementing compilers because writing compilers in Haskell requires tons of boilerplate, which is not needed in the languages that are a much better fit for this task. And yet your "much better languages" are even more niche compared to Haskell, and no major programming language has been written in them. So, it's basically "Haskell is poorly equipment compared to my impossible standards, as implemented by this hodge-podge of Scheme based tools that I alone ever used together for compiler implementation". >And why do you call these basic requirements bizarre? For example, something like a region inference is just a few lines of Prolog vs. dozens or even hundreds of lines of code in Haskell. Because no known programming language resorts to "a few lines of Prolog" to save a few tens of lines for "region inference"?
- sklogic 11y ago> are even more niche Exactly. Compilers construction is a very specific problem domain, which requires specially tailored DSLs, not a "general purpose" language. Haskell is a way too general purpose for this task. > and no major programming language has been written in them. C is even more unsuitable for this than Haskell, and yet most of the compilers are written in C. Technical superiority does not count at all in choosing an implementation platform. > Because no known programming language resorts to "a few lines of Prolog" to save a few tens of lines for "region inference"? JFYI, a prototype implementation of SSA for GCC was written in Prolog (see the Sebastian Pop works [1]). If embedding Prolog in C was technically feasible, it would have stayed this way. Also, to see how much Prolog rocks, check out Harlan [2] In order to avoid unnecessary flame war: I give no crap to any popularity based arguments. Got any technical, well-grounded, rational arguments? Go on, I'd be delighted to comment. Otherwise I'm not interested. And, btw., you're clutching to just one out of four of my points against Haskell. Nothing to say about the other three? [1] http://www.cri.ensmp.fr/classement/doc/A-381.pdf http://www.cri.ensmp.fr/classement/doc/A-381.pdf [2] https://github.com/eholk/harlan https://github.com/eholk/harlan
- zeckalpha 11y ago> 2) Boilerplate-free generation of various (potentially complex) visitors for these ASTs and IRs. For Haskell, Scrap Your Boilerplate library is the closest thing to what I want, but it is still far too clumsy. Have you tried QuickCheck? > 4) Parsing is not very important. But yet, it is necessary. The best Haskell got on offer is Parsec. Sorry, not good enough for my requirements. I've not found much better than (atto-)Parsec, to the point where I miss it in other languages.