3 ms·
I'm not quite sure that it's just the line between using and mentioning the syntax. Template Haskell uses a syntax element called Oxford brackets ([| and |]) to
by Locke1689 15y ago
I'm not quite sure that it's just the line between using and mentioning the syntax. Template Haskell uses a syntax element called Oxford brackets ([| and |]) to grab the AST for a piece of Haskell. This is very easy but Haskell still doesn't have the same compile-time macro programming as Lisp.
I think the reason that Lisp metaprogramming works so well is just that Lisp basically has no syntax and the simplest reasonable grammar. Lisp syntax is just the AST with nested brackets. This means that modifying the Lisp syntax structure is equivalent to modifying the AST.
Fundamentally, you can make accessing the AST as easy as possible, but at the end of the day you're still accessing the AST so you can change the AST. The more complicated the AST, the more complicated the change will need to be.
- _delirium 15y agoI think they have somewhat different use cases. Lisp's approach works best if you don't really want to analyze a fragment of code deeply, but just want to specify a new construct by looking one level deep in the AST, like the classic example of implementing "cond" or "if" as a macro. But if you actually want to walk recursively down an AST, the fact that it's hard to get a usable AST in Lisp makes metaprogramming difficult and error-prone. For many uses, I don't really want the source code to a Lisp fragment, but the AST, in the sense of a semantically marked-up fragment that's resolved variable bindings and done all the other things a parser does. Implementing a correct code-walker for Common Lisp is... really nontrivial. The best available one I've found is in Arnesi, http://common-lisp.net/project/bese/docs/arnesi/html/A_0020Code_0020Walker.html http://common-lisp.net/project/bese/docs/arnesi/html/A_0020C... Whereas walking a Haskell AST fragment is trivial.
- swannodette 15y agoHard to get a usable AST in Lisp? The ClojureScript analyzer is 500 lines of code. For all the meta-programming projects I've taken on - delimited continuations, embedded logic programming, and extensible pattern matching - I'll take Lisp data structures over the actual Lisp AST any day.
- Locke1689 15y agoAfter thinking about it, I agree. Sometimes that semantic information or metadata can be very useful in operation. Specifically, having typing information and pattern-matching can be a Godsend in many cases.