-9

So having watched 3 hours of youtube videos, and spent equally long reading about Lisp, I've yet to see these "magic macros" that allow one to write DSLs, or even do simple things like 4 + 5 without nesting this inside some braces.

There is some discussion here: Common lisp: is there a less painful way to input math expressions? but the syntax doesn't look any nicer, and still requires some enclosing fluff to define where the macro starts and ends.

So here is the challenge, define an infix macro, then use it without having to enclose it with some kind of additional syntax. I.e.

Some macro definition here

1 + 2

NOT

Some macro definition here

(my-macro 1 + 2)

NOT

Some macro definition here

ugly-syntax 1 + 2 end-ugly-syntax

If this is not possible in Lisp, then what is all the fuss about? It's kinda like saying "you have awesome powerful macros that allow for cool DSLs, provided those DSLs are wrapped in Lisp like syntax".

Community
  • 1
  • 1
samthebest
  • 30,803
  • 25
  • 102
  • 142
  • 3
    I don't think it's possible. The basic syntax of code is not really customizable. You need some way to tell it to start processing the expression using your alternate syntax, and when to stop, and that requires a wrapper of some kind. – Barmar Jul 14 '15 at 20:32
  • 2
    Stackoverflow is not for posting programming challenges. It's for real programming problems, best with some code, example use and error description. Stackoverflow says: 'Focus on questions about an actual problem you have faced. Include details about what you have tried and exactly what you are trying to do.' – Rainer Joswig Jul 14 '15 at 20:34
  • You have to replace the whole Lisp syntax with this macro then (which is perfectly fine and a very common thing to do). – SK-logic Jul 14 '15 at 22:31
  • You wrote: 'So here is the challenge, define an infix macro...'. What have you tried so far? Syntax? Code? Examples? What were the problems you have encountered parsing expressions? As you have posted the question, it is opinionated, overly broad, isn't useful... I think Stackoverflow is best, when actual problems with code get answers with more code. Stuff which actually helps people solving actual programming problems. – Rainer Joswig Jul 15 '15 at 10:28
  • @RainerJoswig It's not opinionated it's very well defined, give me a a way to define a macro that I don't have to wrap in some containing scope. I have not tried anything so far, because there is nothing on the internet explaining how to do it, the first comment appears to be correct - it's simply impossible - this would be an accepted answer. Anyway, my question was not "what do you want questions to look like", it was "if they don't fit your ideal, what is the worst that can happen _to you_". SO has lot's of pedants with nothing better to do than try to prevent other people learning. ... – samthebest Jul 15 '15 at 15:16
  • 1
    @RainerJoswig If this question get's closed what do you gain? What damage has been done if left open? If someone actually provided the code to answer my question, surely this would make it easier for future Googlers to learn Lisp? There is 1.0 probability that it will help me, and 0.0 probability that it will hurt anyone - except a few KB in the SO DB. Why be so obstructive? You think I'm lazy, and stupid and you want punish me - but why? Even if I am lazy and stupid, what hurt is it causing you to allow my post to stay open? – samthebest Jul 15 '15 at 15:19
  • 1
    @samthebest The damage is well defined, very real, and has been discussed ad nauseam. http://meta.stackoverflow.com/questions/254358/why-the-backlash-against-poor-questions – user229044 Jul 15 '15 at 16:06
  • How about moving it to [programmers.se]? I think it's the proper site for this kind of directed and specific discussions, not directly focused on code snippets and solutions. – acelent Jul 15 '15 at 16:45
  • 3
    @PauloMadeira No. If it's too broad for Stack Overflow, it's likely too broad for Programmers. We don't do discussions. – Thomas Owens Jul 15 '15 at 16:46
  • 1
    @ThomasOwens, I beg to differ. As seen clearly on [SO's help site](http://stackoverflow.com/help/on-topic) and [Programmers help site](http://programmers.stackexchange.com/help/on-topic), it's the other way around. This question seems to fall right into Programmer's topic: **software architecture and design**. – acelent Jul 15 '15 at 16:51
  • 3
    @PauloMadeira I didn't say it was off-topic. I said it was too broad. To me, it reads like a discussion starter, rather than a question about understanding a concept or solving a problem. Since I'm a moderator on Programmers, I think I have a pretty good idea about what would be closed if posted there. – Thomas Owens Jul 15 '15 at 16:52
  • 1
    @ThomasOwens, ok, but this doesn't have to be a discussion, a straight answer like *you're right, DSLs in Lisp are straightforward as long as the syntax is based on s-expressions*. From my point of view, someone with experience can answer without discussion. Someone else should refrain from answering. That is, the question is not really begging for hand-waving discussion, but rather for refutation, it's different. And as I said, there might be no refutation, the op may be right. – acelent Jul 15 '15 at 16:55
  • 2
    @samthebest, on the other hand, the post tone could (and should) be more polite. For instance, note the all upper-case *NOT*s, quoting *"magic macros"* when you could just mention macros, words like *fluff* and *fuss*... – acelent Jul 15 '15 at 16:55
  • 1
    @PauloMadeira consider that Thomas is a moderator at Programmers, you better pay attention when he says that it's not okay over there. See [On discussions and why they don't make good questions](http://meta.programmers.stackexchange.com/q/6742/31260). Recommended reading: **[What goes on Programmers.SE? A guide for Stack Overflow](http://meta.programmers.stackexchange.com/q/7182/31260)** – gnat Jul 15 '15 at 17:54
  • 1
    @gnat, ok, I totally agree with the reasoning behind those FAQs. I'd rather be pointed to them (or similar content) from the start. [Appeals to authority](https://en.wikipedia.org/wiki/Argument_from_authority) just leave me in the dark. Stating things like *I (or he/she) know(s) better than you* is void to me, unless it's also backed up with experience reports. And it sounds rather condescending, too. But anyway, thank you both for clearing this out for me, as I've done this same mistake (suggesting a move to Programmers under similar conditions) once or twice in the past. – acelent Jul 15 '15 at 18:36
  • OMG, I really don't care that much, just close it and I won't use Lisp. – samthebest Jul 16 '15 at 11:17
  • 1
    @samthebest, you can define a macro that expands to a top-level `eval-when` form that includes `:compile-toplevel` and/or `:load-toplevel` which body changes the current `*readtable*` to one that can read infix notation. You'd still need an end delimiter, or just carry the syntax until EOF as both `load` and `compile-file` bind `*readtable*` around file processing. And yes, DSLs in Lisp are easy given an s-expr syntax, (or rather, a Lisp readable syntax), but otherwise require a parser like in every other language. You must weight the (dis)advantages and see if this fits your needs. – acelent Jul 16 '15 at 14:13
  • @PauloMadeira Great thanks! Could you please provide the example? – samthebest Jul 16 '15 at 17:48
  • I really can't, I don't have one and it would take some time to fabricate one. Such a readtable would be truly convoluted, up to the point where I'd prefer to use a proper parser for the DSL, instead of subverting Common Lisp's reader. There's still an easier part in Lisp, which is to turn the [AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree) into Lisp code and feed it to `compile` or `compile-file`. I remember doing this for a custom language's AST (C lex&yacc style) and it not only ran, it ran so well that I could compare results with the compiled machine language. – acelent Jul 17 '15 at 11:59

1 Answers1

15

Of course you can implement what you're talking about in Lisp.

One big difference between Lisp and other languages is that nothing is fixed and you've control on what the reader does on every single character of the input source code. Literally.

Consider for example the project CL-Python and its mixed-syntax Lisp/Python mode.

Basically in that mode if what you type starts with an open parenthesis then it's considered Lisp, otherwise it's considered Python (including multi-line constructs).

...HOWEVER...

this kind of macro/reader-macro library is implemented rarely, because one of the main advantages of the s-expression approach is that it's well composable and you can build other macros on top of existing macros, raising the level of the language.

Once abandoned the regularity of s-expression approach, writing macros becomes annoying because to write code that manipulates code you need to consider the several different constructs of the language, precedence rules, special rules, special syntax forms.

Languages that are not based on s-expression sometimes provide real macro processing capability but working on the AST level, i.e. after some parsing processing has already been done in a fixed way. Moreover the macro code in these languages looks really weird because the code the macro is manipulating, inspecting or building doesn't look like real code.

Sometimes in other languages instead you only find text-based macros that are basically search-replace. To see an example of how ugly things can get consider the Python standard library implementation of collections.namedtuple (around line 284) and its set of absurd limitations induced just because of that implementation.

Another example of how things can get horribly complex once you force yourself to a template-only approach to avoid the complexity of manipulating an irregular and special cased language is C++ template metaprogramming.

A simple s-expression based language and quasi-quotation instead makes macro code much easier to write and read and understand, and that's why Lisp code doesn't move away from it. Not because it cannot, but because it doesn't make sense to go to a worse syntax for no reason.

In Lisp you "bend" the language a little by adding the abstractions that are really needed without however breaking everything else and, most important, without dropping the ability to do more bending in the future if needed. Writing a macro/reader-macro that makes the expression parsing the nightmare of say C++ and at the same time removes the ability to write further macros and add more constructs (or makes it impossibly hard) would be a nonsensical suicide.

Even a macro like (infix x + y * z) is just an exercise... I doubt that any lisper would use that to write real code... why on earth would someone reintroduce the absurd function/operator duality and the nightmare of precedence/associativity rules? If you don't like Lisp then just don't use Lisp.

For a lisper it's not the (infix and ) part that is ugly... it's what is in the middle.

Also why do you think that 2+3*6 is "naturally" 20? Because the teacher hit you on the palms of your hands with a stick when you were a kid until you got it right?

6502
  • 112,025
  • 15
  • 165
  • 265
  • 1
    // Also why do you think that 2+3*6 is "naturally" 20? // Mathematics has a huge number of different types of notation ... it doesn't just stop at infix & postfix, consider summations or limits, or set builder notation, or matricies, category theoretic diagrams. Mathematicians even put functions and variables above one another - in different fonts. It's not a question of what is natural, it is a question of how to communicate efficiently. When someone puts the operator in the middle they are communicating that this is an algebraic operation, .... – samthebest Jul 15 '15 at 10:21
  • 1
    .... and it's likely that algebraic properties can be assumed. When functions are put above it's arguments it's implied the argument represents a sequence. The more power a language has the more efficiently it can communicate - if a language cannot EASILY support several notations, then it cannot efficiently communicate and will not ever reach mainstream adoption. Anyway this post wasn't supposed to open a debate, I genuinely wanted to know how to do this because someone told me I should Lisp because I'm writing my own macro compiler for LaTeX. – samthebest Jul 15 '15 at 10:25
  • @samthebest: In math when you see `a×b` you can guess very little as it depends on the context. It may be an internal operation or not (e.g. the vector product in ℝ²), it may be commutative or not (vector product, matrix multiplication). Something that's quite often true is that the operation is associative (but it's still not true in general). Really you don't know much more than it's something that given `a` and `b` gives back something else in some space. It's just a two-variables function. – 6502 Jul 18 '15 at 05:26
  • That is not what I was taught when I got my pure maths undergrad, nor postgrad degrees. Notation means a HUGE amount in mathematics, I don't know how you could deny that, yet in your own comment you use R^2, knowing full well that means something completely different to R_2. Yes, group theory starts with associativity ... – samthebest Jul 18 '15 at 14:05
  • Infix indeed implies algebraic structure, postfix implies an arbitrary function. Asymmetrical infix relation symbols imply transitivity, etc. Yes one would need a mathematical proof before one would be really sure of any of these properties, but that is not notation is for, notation, and language in general, is for communicating, not proving statements. Most mathematicsl proofs in textbooks are not even formal, they communicate the proof, the reader must perform a large amount of steps in their head. – samthebest Jul 18 '15 at 14:18
  • ... the greater the ability to use many constructs, notations, sounds, symbols, characters, etc the greater the expressibility of the langauge. This is trivially true - more symbols means more information can be packed into fewer bytes. Hence languages that allow for more constructs, communicate more, more efficiently, the result is they become more popular because less effort is required to read and write in those languages. A compiler programmer on the other hand wants the least number of constructs/features, because less features means less work and faster compilers. – samthebest Jul 18 '15 at 14:23
  • (Sorry for extended comments, last one I promise). Finally precedence rules are a good thing too, they too can communicate information. Precedence in mathematics is usually defined in terms of the complexity of the function. Symbols that are defined in terms of other symbols are applied before those symbols, like * is applies before + because it's defined in terms of +, similarly / is defined in terms of *, so is applied before, similarly with ^, and so on. The reason why this sucks at school, is because schools suck, when you learn this at degree level, it become powerful and cool! – samthebest Jul 18 '15 at 14:29