r/ProgrammingLanguages • u/Mr-Tau • Aug 03 '26
Discussion Between more constrained, local metaprogramming approaches and full-blown DSL interpreters, where's the practical use case for LISP-style metaprogramming?
Of the various contemporary metaprogramming approaches, I'm mostly very happy with systems that let you operate on static data and types. In this category I would count, for example, C++'s template metaprogramming--a declarative sub-language that somewhat wonkily and arduously allows you to operate on types and constants--as well as the C++26 metaprogramming features, which are turning out somewhat Zig-like--you get to run regular code in a slightly constrained environment at compile time, where it can read constants as well as special data structures describing types, and generate new constants or types to be injected into a well-defined place in the code.
Now many LISPs (AFAICT, similarly Jai and, with a stricter separation of stages, Rust's procedural macros) tout as a feature the ability to inspect and rewrite the entirety of the AST, notably including function bodies. This is obviously strictly more powerful than the the first category, but where would you practically use that extra power? Specifically, it seems to me like you would either
a) Try to preserve the semantics of the input code--which, for procedural languages at least, is actually pretty difficult. The only transformations you could make confidently are so localized that you don't really gain anything over the more constrained metaprogramming approaches; anything more advanced would require the full analysis passes of the actual compiler to have any chance at soundness, and at that point you're really trying to write a compiler plugin instead. Nothing wrong with that, I'd love more easily extensible compilers, but I wouldn't call that a language feature. Or am I missing a point between those two extremes?
b) Attribute different semantics to the code. I think the history of LISP has already somewhat shown how proliferation of DSLs harms maintainability and shareability of code, but even in the cases where a DSL is genuinely useful, what benefits do you really gain from implementing it through metaprogramming? You can't expect any IDE features, LSPs, smart syntax highlighters, debuggers, or other tooling for the base language to automatically work for your DSL. So you just get to use the parser? Come on, an S-expr parser is less than a hundered LOC.
View all comments
Show parent comment
2
u/EggplantExtra4946 Aug 05 '26 edited Aug 05 '26
Like lispm said, yes they can rewrite their arguments and I guess that if you wanted to "rewrite anything" you could just surround the entire file with a macro call and inside the macro definition you could iterate over the program and rewrite it as you please. Not that I wouldn't prefer to have a hook to do just that.
They are hugely different, there is not much of a comparison to be made.
When anyone mentions Lisp && metaprogramming, of course it's about macros. Reader macros are for syntactic sugar, not metaprogramming.
They aren't, all they allow you to do is things like transforming
#(1 2 3)into(list 1 2 3). Even if you could actually define a new syntax, as in C-like language or whatever, this wouldn't make them more powerful than regular macros.I thought it was going to be a fun read but I stopped reading after seeing the description of the "generalized macro" and that the rationale for it was to implement
defer. I really don't understand why LISP people restrict themselves to the AST and to macros, when it comes to metaprogramming features. The AST is not the only data structure in a compiler and macros isn't the only conceivable way to do metaprogramming or to rewrite a program. Addingdefercould be done in a much cleaner way with a rewrite hook on the AST root after macro expansion is done (let the user walk over the root of the AST and return want he wants), or better yet, a similar hook but on the CFG. If the language had adeferbuiltin, the desugaring would also happen after macro expansion anyway.