3 ms·
Compiler macros are kind of a fluff feature that can easily be subsumed into regular macros. You need two ingredients: (1) it must be possible to define a mac
by kazinator 5y ago
Compiler macros are kind of a fluff feature that can easily be subsumed into regular macros.
You need two ingredients: (1) it must be possible to define a macro and function simultaneously for the same symbol. (2) the regular macro expander must detect fixed points in macro expansion and stop expanding. You want (2) anyway; if a macro returns the original form, so that its output is eq to its input, it make no sense to call it again.
This is the TXR Lisp interactive listener of TXR 275.
Quit with :quit or Ctrl-D on an empty line. Ctrl-X ? for cheatsheet.
TXR Lisp has no protected class members; C++ refugees may face discrimination.
1> (sqrt 5)
2.23606797749979
2> (fboundp 'sqrt)
t
3> (mboundp 'sqrt)
nil
4> (defmacro sqrt (n :form f)
(if (constantp n) (sqrt (eval n)) f))
** expr-4:1: warning: defmacro: defining sqrt, which is also a built-in defun
sqrt
5> (sqrt 5)
2.23606797749979
6> (macroexpand '(sqrt 5))
2.23606797749979
7> (macroexpand '(sqrt x))
(sqrt x)
8> (mboundp 'sqrt)
t
9> (mmakunbound 'sqrt)
sqrt
10> (mboundp 'sqrt)
nil
11> (macroexpand '(sqrt 5))
(sqrt 5)
Unlike a compiler macro, sqrt will always expand, guaranteed.
User-defined optimizations want to be tree rewrite patterns. We can impose that onto macros (or compiler macros).
In the TXR world, this can be done with a parameter macro, and one is provided for that, namely :match.
12> (defmacro sqrt (:match :form f)
(((@(constantp @exp)) (eval exp)))
(((* @exp @exp)) exp)
(((expt @exp 2)) exp)
((@else) f))
** expr-23:1: warning: defmacro: defining sqrt, which is also a built-in defun
sqrt
13> (macroexpand '(sqrt (* (+ x y) (+ x y))))
(+ x y)
14> (macroexpand '(sqrt (* (+ x y) (+ x z))))
(sqrt (* (+ x y) (+ x z)))
15> (macroexpand '(sqrt (* 5.5 5.5)))
5.5
16> (macroexpand '(sqrt (expt (sin x) 2)))
(sin x)
17> (macroexpand '(sqrt (expt (sin x) 3)))
(sqrt (expt (sin x) 3))
Parameter macros like :match are bound in the keyword namespace. When these bound keywords occur at the head of a parameter list, their macro gets invoked, receiving the parameter list and the function body, rewriting both of them together.
- lisper 5y agoFirst I've heard of TXR. Very impressive!
- deleted 5y ago[deleted]
- kazinator 5y agoI made an interesting discovery yesterday. When you allow a symbol to have both a function and macro binding, it behooves you to implement the following expansion rule: If a function call form (op ...) is the result of the expansion of a macro form (op ...) then after its argument forms are recursively expanded, if those recursive expansions make any difference, the resulting form should again be tried as a macro form. This is because transformations on the arguments of the function call may turn the call into something that is interesting to the macro again. With this, I can do the following now: This is the TXR Lisp interactive listener of TXR 275. Quit with :quit or Ctrl-D on an empty line. Ctrl-X ? for cheatsheet. TXR contains many small parts, unsuitable for children under 12 months. 1> (defmacro sqrt (:match :form f) (((* @exp @exp)) exp) ;; (sqrt (* x x)) -> x (@else f)) ** expr-1:1: warning: defmacro: defining sqrt, which is also a built-in defun sqrt 2> (defmacro expt (:match :form f) ((@exp 2) ^(* ,exp ,exp)) ;; (expt x 2) -> (* x x) (@else f)) ** expr-2:1: warning: defmacro: defining expt, which is also a built-in defun expt 3> (expand '(sqrt (expt x 2))) x See? (sqrt (expt x 2)) reduced down to x, even though the sqrt macro has no such optimization case. First (sqrt (expt x 2)) is tried as a macro. That immediately hits a fixed point by returning the form. The expander makes a note that expansion took place (even if a no-op). Then the expander expands it as a function call, expanding the argument expression, turning the form into (sqrt (* x x)). That expansion having made a difference, together with the fact that the form came from a macro, now means that the expander tries the form again as a macro: (sqrt (* x x)) is expanded again, and now there is a "hit". (For simplicity, these examples don't care about multiple evaluation; and it is a given that algebraic changes are risky in the face of floating-point. It's a good way to illustrate the technique.) CL compiler macros could do something like this. I don't see the requirement in the HyperSpec. The requirement could be that when a compiler macro declines expansion, then after the resulting function form is recursively processed for more macros, the compiler macro should be tried again, and this process should repeat until both conditions are met: the macro declines, and the function form no longer contains any arguments that are macros. Of course, a compiler macro can capture an &environment and do its own expanding via macroexpand. In this example, that would work. What I implemented is subtly different: a complete expansion is performed on the function. You would need macroexpand-all to simulate the feature in the macro itself. Not having to do that at all, having things Just Work, is pretty nice.