4 ms·
Desugaring as a Language Feature
- aapl_bad_stream 12y agoIf you took away the artificial sweetners and sugar from Ruby, it would just leave the bitter taste of Perl!
- 6cxs2hd6 12y agoThere are quite a few introductions to macros. Maybe like monads, it's a topic where people need to read a few until one "clicks" for them. Having said that, I think this is among the best. The writing style is friendly, the examples are meaningful, and it builds up in digestible steps.
- PeterisP 12y agoHow about sugaring as a language feature? What I mean is that if the language has multiple ways to express statements that are semantically absolutely identical but differ in just the 'sugar', then it would be useful to have a way to automatically 'normalize' the code to always use a single idiomatic way of doing it (which in many languages is the 'sugar' version). It's just like with indentation and brace styles - the original authors may each have their own preferences, but when reading code, it's more efficient to view the code in a single style according to my preferences, and that can be done automatically by showing me the code as reindented, with my preferred brace positioning, my habitual syntax highlighting color scheme ... and also with a standartized level of sugaring.
- shriramkmurthi 12y agoIf you have a way of automatically detecting that two terms in a program are semantically identical, don't keep it secret! [Hint: Halting Problem.]
- alloyed 12y agoThat's not how I read gp, exactly. What he is saying sounds more like taking the AST of a program, desugaring at much as possible, and then re-sugaring it in such a way as to produce a consistently styled codebase, sort of like gofmt but more opinionated and linter-like.
- shriramkmurthi 12y agoWhat he wrote is "statements that are semantically absolutely identical but differ in just the 'sugar'". I was just quoting him back. Of course, one can always construct various hacky approximations to this, and maybe that's what he intended, even if it isn't what he wrote; I couldn't possibly tell.
- Dylan16807 12y agoThe 'absolutely' should have hinted that 'semantically' wasn't the full extent of what he meant. Focus your quote onto "differ in just the 'sugar'" and you see exactly what he meant. He didn't misspeak. Sugar is a very specific term.
- shriramkmurthi 12y agoI'm not actually unclear on the meaning of these words. There happen to be multiple possible interpretations of what the OP wrote, some demanding problematic things and others impossible ones. As I said in my reply to @JadeNB, we have unambiguous terminology for all this. Would have helped to have used it. As for the meaning of "sugar", I _think_ I have some inkling about what it means. We're in a discussion thread about a document on desugaring. I happen to be its author.
- Dylan16807 12y agoGlad you have a very solid understanding of sugar. But that means you're being intentionally obtuse when you invoke the halting problem and discuss the entire set of programs that produce the same results. I think from https://news.ycombinator.com/item?id=8294806 https://news.ycombinator.com/item?id=8294806 that the core of your argument is about it being difficult to tease out sufficiently complex sugar, but to be honest I've almost never seen complex sugar. Most of this stuff, especially when PeterisP talks about being idiomatic, is simple transformations that you can do easily in either direction. You don't need a magical sugaring oracle, you need a language-specific sugaring oracle. And if you have a language where the sugar isn't decidable, I'd argue something has gone wrong.
- andrewflnr 12y agoYou don't need a perfect Desugaring Oracle, just a Good Enough, 80% solution. You could also get a lot of mileage out of non-turing-complete subsets of the language.
- shriramkmurthi 12y agoIt doesn't matter what you _need_: my point is you can't _have_ one in the general case. As for your 80% and non-TC solutions, see my other response (https://news.ycombinator.com/item?id=8294806 https://news.ycombinator.com/item?id=8294806). Maybe you've built such engines, in which case I'd love to hear more (because we're actively working on related problems). It's pretty damn hard because of the nature of reasoning you need for non-trivial examples.
- PeterisP 12y agoCome on, you're intentionally and misleadingly nitpicking if you're claiming that the halting problem is a restriction for this. You can trivially detect a large set of terms that are semantically identical; and all the rest - possibly identical and possibly not identical - can be left alone. "i = i+1;" <=> "i++;" and such. In particular, "syntactic sugar" explicitly refers to substitutions that can be (and are) made by considering only the local scope, and the rest of the program can't change the validity of this substitution; in the cases for syntactic [de]sugaring all the changes can be performed safely by looking at a statement or two (or their AST representations) in complete isolation. If you have two functions that happen to be semantically equal but differ in other things than pure sugar - well, then they won't be affected by desugaring or sugaring, obiously, and we don't need to detect if they're actually equal or not. Translating from source code to AST is clear and unambiguous and doesn't require anything related to the halting problem. Translating from AST to some canonical representation of source code can also be done - it's not trivial, but if you have the original code, then you can get a better result than the current decompilers do. This will result in cases where transforming back and forth will cause A => A' => A and B => A' => A which is the intended result.
- shriramkmurthi 12y agoFor more detail, I suggest reading chapters 35, 36, and 37 of the first edition of this book (PLAI), which provide even more detail. See http://www.plai.org/ http://www.plai.org/.