Make macros and functions more integrated

Ok, I understand well how to use functions and macros.

What interests me is why the compiler cannot be smarter if you integrate the two, for example. consider the Clojure code:

(defmacro wonky-add [ab] `(+ ~a (* 2 ~b))) (defn wonky-increment [a] (apply wonky-add a 1)) => Error: can't take value of a macro 

Yes, I know that I can do this work by extracting “apply” - but why can't the compiler figure out how to do it myself?

Is it possible in Clojure / other LISPs to create a version of an application or other higher-order functions that work equally well with functions and macros as parameters?

+6
compiler-construction macros functional-programming lisp clojure
source share
2 answers

This answer is not so much about Clojure, but Lisp and macros in general.

Remember:

APPLY is the ability to call functions with argument lists that are created at runtime.

Macros to generate new source code from some source code - and the generated source code will be launched.

Now:

If you allow the application to feed other source code to the macro at run time, you need to be able to generate the result code at run time, as well as execute the generated code.

Thus, in a compiled Lisp system, each APPLY call using a macro potentially creates new code that must be compiled at run time in order to be able to execute. It also means that you may get new compiler errors at runtime, when your code is executing, and the code has some problems. Therefore, to use macros at run time, you need a compiler and all the necessary information (for example, other macros) to be able to extend and compile the code.

It also means that in general, you cannot compile APPLY with a macro before execution, since it is not known which run-time arguments will be applied.

There can be more information in the Lisp interpreter, and macros can be applied all the time.

At the beginning of Lisp there were ordinary functions and the so-called FEXPR. FEXPRs allowed flexible call and runtime processing. Later in the history of Lisp, they were replaced by macros, because macros allow efficient compilation, and on compiler-based systems they allow processing of syntax errors before starting.

+11
source share

What you describe sounds like a first class macro (i.e. a macro that can be manipulated as a function). Some of them have such ( Arc , but apply , due to the fact that it is not the macro itself, probably work.

+2
source share

All Articles