7 ms·
To people saying that writing a compiler is almost always an overkill - that's because you and author mean different things by the word "compiler". Or, in other
by TeMPOraL 7y ago
To people saying that writing a compiler is almost always an overkill - that's because you and author mean different things by the word "compiler". Or, in other words, that's because your intuition is based on the inflexible languages you're working with.
It's not an accident that the talk/article is about Clojure[0]. A Lisp. In Lisp, "writing a compiler" is a fancier way of saying "writing a possibly code-walking macro", which is still just a fancy way of saying "writing a function that transforms a tree into another tree, and having it run compile-time on parsed program source as the input". 99% of the usual compiler work - tokenizing, building an AST, generating machine/bytecode, optimizing - is handled for you by the host Lisp compiler. All the mechanics are there, and all you have to do is to add the brains you need.
--
[0] - Clojure itself actually makes this slightly more difficult than it looks, because of the decisions it made that makes the syntax slightly less obviously homoiconic.
- invalidOrTaken 7y ago+1 to this. I did this literally last week for a toy project and it was hilarious to be reminded of how easy it was. I know I'm just rehashing the above comment, but: if your target language is complicated, and your host language is complicated, sure, it can be a pain. If they're both lisps, you can almost do it in your sleep.
- LandR 7y agoThere is a joke in Lisp / Lisp dialects that they don't write their code in Lisp, they write the language they want to write their code in in Lisp. Then solve the problem in that language. It's often not true, but writing DSLs in a Lisp is technically much simpler than other languages I reckon, if your problem domain actually requires such a thing.
- stevekemp 7y agoLisp is not alone in that regard, to be honest I suspect it is even more true in FORTH. In FORTH you define words, which you then write your program in. These words very quickly become problem/domain-specific.
- macintux 7y agoSpeaking as someone who has written maybe 100 lines of code in both languages, thus someone who should not be taken too seriously, there seem to be a lot of similarities between Lisp and Forth.
- daniel-cussen 7y agoSpeaking as someone who has written maybe 10000 lines of code in both languages, and may be worth taking a little more seriously, the similarities are there but the macros in Forth are just somehow off. There is extremely little written online about Forth macros. The best book I've found on the subject is Let Over Lambda, whose latter chapters talk about Forth macros and how it's possibly the best macro system second only to Lisp. And yet I don't know of anyone, outside possibly Chuck Moore in his "Glow" system for CAD for circuits, that can exploit layer upon layer of Forth macros. The beauty of Lisp macros, really, when you really dive to the bottom of it, is not what it appears to be. It's not about being able to turn one thing into another, no matter how much flexibility it allows in doing so. Any language can do that. Javascript for one, just use strings and eval and you can create interesting macros. It's not about a domain-specific language: every fucking language lets you define words in it with which you'll do the work, and often a syntax too derived from it (operator overloading). No. And it's not about brevity: because of S-expressions, Lisp is really not that brief compared to other languages (like C) (when you are doing everything right). The beauty of Lisp macros is simply and only that you can do all these things over and over again, layer upon layer, "ten or twenty layers of macros deep" (to paraphrase Paul Graham), with each layer making the next layer no more difficult to work with. Nothing else does that. Not even Forth. And a note on homoiconicity: it appears standard Forth is homoiconic...maybe. Mostly. Not transparently so like Lisp (see above), where no matter how much you exploit homoiconicity your code doesn't degrade in structure, preventing you from doing so n+1 times [1]. No, in Forth, you get mostly homoiconicity, but there's caveats. However, F18 code, featured in the GA144 chip, is basically homoiconic both because it's forth with pointers and because it's assembly, and it does allow a lot of macro-like behavior, including an 18-bit self-replicating virus. It's nice. But it's still not Lisp.
- kerkeslager 7y agoWriting a compiler is still almost always overkill, because implementing the compiler is one of the smallest parts of creating an effective programming language. Let's say you're starting a project, and you're choosing a language to write it in. You have two options: Language A: 1. Has multiple mature compiler/interpreter implementations. 2. Has lots of tooling built around it. 3. Has extensive documentation. 4. Has a diverse community to hire programmers from. 5. Fits the problem domain okay. Language B: 1. Has one buggy implementation that returns mangled stack traces on errors. See Kernighan's Lever[1]. 2. Has no tooling. 3. The documentation is the compiler. So... no documentation. 4. The only programmer that knows it is the one who wrote the compiler. 5. Fits the problem domain as well as the programmer understood the problem domain when he started writing the compiler. I know which language Dan McKinley[2] would choose. :) You might say, "But my language uses the same homoiconic syntax of Lisp, so the tooling of Lisp carries over, and it doesn't require more documentation than just normal functions, and Lisp programmers can pick it up easily." To which I would respond, "Sounds like you took the long way to implementing a library." I'd level this criticism against a lot of Racket languages, which basically are just a shorthand for a bunch of require statements. I'd rather copy/paste those same require statements. The fact that Lisps make creating a compiler so easy is actually a downside, because it leads people to write compilers without thinking through the full secondary effects of doing so. This is one of many heads of the Hydra that is The Lisp Curse[3]. EDIT: Decided to say more. EDIT 2: Decided to say less. EDIT 3: Always be editing. [1] https://blogs.msdn.microsoft.com/alfredth/2009/01/08/are-you-smart-enough-to-debug-your-own-code/ https://blogs.msdn.microsoft.com/alfredth/2009/01/08/are-you... [2] https://mcfunley.com/choose-boring-technology https://mcfunley.com/choose-boring-technology [3] http://www.winestockwebdesign.com/Essays/Lisp_Curse.html http://www.winestockwebdesign.com/Essays/Lisp_Curse.html
- justinmeiners 7y ago> Sounds like you took the long way to implementing a library. You are right that DSLs should be about as natural as libraries. Linq in C#, regex, and SQL are good examples of DSLs you probably rely on regularly. I suggest looking at the symbolic differentiator and circuit simulator in SICP. It is composed of just a few functions and is far cleaner than a library of functions on a custom data structure.
- tom_mellior 7y ago> It's not an accident that the talk/article is about Clojure On the other hand, I'm surprised that the optimizations in question don't happen automatically for Clojure programs. Surely the underlying JVM is capable of constant propagation and folding? Maybe it's the let-from-let optimization that actually gets the 10% speedup.
- phadej 7y agoI probably don't say it in the talk very explicitly (or do I?): the target architecture is web = JavaScript.
- tom_mellior 7y agoOh, you do say it in the article: "However, for the maximum reach, we want our game to run in the web browsers." I missed that, thanks. But even so, JavaScript compilers aren't stupid. No need to do this for me of course, but if you got around to measuring the performance impact of the two optimizations separately, I'd be interested in that. (And the ClojureScript developers might be interested too.)
- lidHanteyk 7y agoOleg has worked in other non-Lisp languages, notably members of the ML family. I know that you want to be all pro-Lisp and such, but it's just another family of languages, really; there's nothing magical or special about them. All compilers use an anamorphic parser, a hylomorphic reducer, a paramorphic optimizer, and a katamorphic instruction selector. It's not about what the language provides, but about what the language doesn't require. Lisps can be low-ceremony, but they're not the only ones. Trees might be universal data structures, but they're not the only ones.
- TeMPOraL 7y agoI'm not saying Lisps are unique in this regard, but I am saying that complaints that I see in today's HN discussion most likely stem from lack of exposure to that kind of programming. In the mainstream world, "language" and "compiler" are products. In Lisp world, they're no different than functions and classes. Out of language families providing this level of capability, Lisps are probably closest to mainstream, and that's because of Clojure.
- jimbokun 7y ago"In Lisp world, they're no different than functions and classes." Haskell may be similar in this regard, due to the popularity of parser combinators? (Or were years ago when I last looked, don't know if they are still popular or widely used tool in that community.)
- lidHanteyk 7y agoIn the mainstream world, we study a single category of types and logic at a time. There is an ∞-category Tomb whose objects are languages and arrows are compilers; we don't talk about it much. Lisp is certainly not somehow an arena where we transcend computing concepts; it's a lightweight syntax for untyped lambda calculus, at best. A single topos, not the entire multiverse of topoi. I could restate your entire argument for ANS FORTH and I'm not sure whether it would lose anything. That should clue you into the idea that your argument is really about the age of the language family, and the corresponding maturity of philosophy and reasoning around members of the family. The only thing that Lisp makes easy is the parser, by dint of reusing the host Lisp's s-expression reader. The most trivial and surface-level part of a compiler, really.
- perl4ever 7y agoWell, I don't typically use languages like that or trees per se, but usually when I'm trying to do something I don't know how to do, I define a data structure that expresses what I want to do as naturally and succinctly as possible, and then try to write code to transform it into the actions I want. And then I get bogged down in edge cases, and one day I realized, hey, I really should, as a matter of course, use tricky coding as feedback that tells me I should tweak my data structure(s) to make them simpler to parse, and eliminate bugs in advance by reducing my cognitive load.