Sunday, February 9, 2020

Four ways to use macros

The way I see it, there are about four five basic ways to use macros in Common Lisp.

First are macros that circumvent the regular call-by-value semantics. These might evaluate a subform at macro expansion time, treat a subform as a place (an l-value) rather than a value, or otherwise treat a subform as something other than a runtime function call. For example, if incf fully evaluated its argument, it could perform the increment on the value, but it couldn't put the value back where it got it. Another example is the check-type macro. You use it like this:
(defun foo (x)
  (check-type foo (integer 1 *) "a positive integer")
  (bar (- x 1)))
The check-type macro has to be a macro because it treats foo as a place (it will allow you to proceed by modifying foo), and it treats its second argument as a type specifier.

Second are macros that introduce new syntax to the language. Examples are cond, case, do, dotimes, defun, defvar, etc. These treat their arguments specially or have special clauses that don't act like ordinary function calls.
CL-USER> (macroexpand-all '(do ((i 0 (+ i 1))
                                (j 1 (* j 2)))
                               ((> j 65536) nil)
                             (format t "~%~2d ~5d" i j)))

  (COMMON-LISP:LET ((I 0) (J 1))
      (GO #:G748)
      (TAGBODY (FORMAT T "~%~2d ~5d" I J))
      (COMMON-LISP:LET* ((#:NEW1 (+ I 1)) (#:NEW1 (* J 2)))
        (SETQ I #:NEW1)
        (SETQ J #:NEW1)
      (IF (> J 65536)
          (GO #:G747))

Third are macros that implement tiny languages within Common Lisp. The loop macro is a good example. It looks like this
(loop for i from 1 to (compute-top-value)
      while (not (unacceptable i))
      collect (square i)
      do (format t "Working on ~D now" i)
      when (evenp i)
        do (format t "~D is a non-odd number" i) 
      finally (format t "About to exit!"))
It works like a little compiler. It parses the loop clauses and generates a Lisp form that carries them out
             (TYPE (AND REAL NUMBER) I))
        (WHEN (> I #:LOOP-LIMIT-744) (GO SB-LOOP::END-LOOP))
         (#:LOOP-LIST-HEAD-745 #:LOOP-LIST-TAIL-746) (LIST (SQUARE I)))
        (FORMAT T "Working on ~D now" I)
        (IF (EVENP I)
            (FORMAT T "~D is a non-odd number" I))
        (SB-LOOP::LOOP-DESETQ I (1+ I))
        (FORMAT T "About to exit!")

Fourth are macros that run code in a special context. The with-… macros and when and unless fall in this category. These macros take ordinary Lisp code and wrap it with other code that establishes a context or tests a conditional.
CL-USER> (macroexpand '(when (condition) (foo) (bar)))

    (PROGN (FOO) (BAR)))

CL-USER> (macroexpand '(with-open-file (foo "~/.bashrc" :if-does-not-exist :create)
                         (print (read-line foo))

(LET ((FOO (OPEN "~/.bashrc" :IF-DOES-NOT-EXIST :CREATE)) (#:G751 T))
    (WHEN FOO (CLOSE FOO :ABORT #:G751))))

These aren't hard and fast categories, many macros can be thought of as in more than one category. All macros work by syntactic transformation and most treat at least one of their subforms as something other than a call-by-value form, for instance. There are also the occasional macros that have the look and feel of a standard function calls. The series package appears to allow you to manipulate series through standard function calls, but works by clever macroexpansion into iterative code.

I find it useful to think of macros in these four different ways, but I'm sure that others have their own categorizations that they find useful.


An anonymous reader asked, “What about code walking/analysis/optimization?”. I really overlooked that. I think Richard Waters's series package would be a good example. It takes ordinary functional programs that can operate on series of data (coinductively defined data or codata), and turns it into the equivalent iterative construct that operates on one element at a time. It does this by clever macros that walk the code, analyse it, and rewrite it to a more optimal form
CL-USER> (sb-cltl2:macroexpand-all '(let ((x (scan-range :from 0 :below 10)))
                               (collect (choose-if #'evenp x))))

                   (#:LASTCONS-751 (LIST NIL))
                   (#:LST-752 #:LASTCONS-751))
           (TYPE CONS #:LASTCONS-751)
           (TYPE LIST #:LST-752))
    (SETQ X (+ X (COERCE 1 'NUMBER)))
    (IF (NOT (< X 10))
        (GO SERIES::END))
    (IF (NOT (EVENP X))
        (GO #:LL-756))
    (GO #:LL-756)
  (CDR #:LST-752)))
As you can see, the series code does a major rewrite of the original lisp code. An astute reader will notice that the let form has to have been redefined to do dataflow analysis of it's bindings and body. Thanks to anonymous for the suggestion.

Addendum 2: Symbol macros

A comment by Paul F. Dietz got me thinking about symbols and it occurred to me that symbol macros deserve their own category as well. Symbol macros appear to be an ordinary symbolic references to variables, but they expand to some code that computes a value. For instance, if foo were a symbol macro that expanded to (car bar), then using it in a form such as (+ foo 7) would expand to (+ (car bar) 7). A symbol macro is a two-edged sword. It is a very useful abstraction for providing a name to a computed value, but they also can fool the user of such a macro into thinking that a simple variable reference is happening when some more complex computation could be happening.

I think that makes seven ways and counting.


Anonymous said...

Hey, great article, but what about code walking/analysis/optimization?

Joe Marshall said...

Good point. I was sort of thinking mini languages, but I think it deserves a category all its own. I've added an addendum.

Paul F. Dietz said...

A very special use I've sometimes made of macros is as things to query the macro environment, by expanding the macro using MACROEXPAND and the &environment parameter to some other macro. The results from these "environment query macros" are never themselves used as code, but are used by the code for that other macro to generate code. One could, for example, implement symbol tables for a DSL using this mechanism.

Joe Marshall said...

...six ways — among the ways of using macros.

That's an interesting use case. It hadn't occurred to me that you could do anything portable with the &environment but pass it along to other macros.

John Cowan said...

I think your first and second types really are the same thing. INCF is just as much a syntax extension as COND, it's just not as complicated.

As for the fourth type, it's a mere syntax variant of procedures that take a functional argument: there is no real difference between (with-foo (this) (that)) and (with-foo #'(lambda () (this) (that))).

Joe Marshall said...

They are all syntax extensions. I put COND and INCF in different categories because COND (and CASE and TYPECASE, etc.) has these funny clauses that are in parenthesis, but don't resemble function calls, whereas INCF appears to have a normal argument but which acts as a place specifier.

I put WITH-… macros in their own category because they have an &BODY and because they are easily desugared into a call that takes a lambda expression as an argument.

The categories aren't exclusive by any means. All macros work by syntactic transformation and almost all modify the call-by-value semantics.

Unknown said...

Thanks Joe, very nice article...where do Paul Graham's anaphoric macros fit in? Jans

M. Flack said...

Also compiler macros are very useful, to optimize a function call in-situ - possibly to alter the call to a different function (perhaps a standard library one); or even to obviate the call away with a fixed answer.

Joe Marshall said...

Hi Jans, good question! It's a little hard to say where an anamorphic macro fits in. They don't usually change the call-by-value semantics, but they do run their bodies in a special context where the anamorphic name is visible. This makes them a lot like WITH-… macros.

You could write (defmacro with-it (form &body body) `(let ((it ,form)) ,@body)) and then write (defmacro aif (test consequent alternative) `(with-it ,test (if it ,consequent ,alternative))) or (defmacro awhen (form &body body) `(with-it ,form (when it ,@body)))

David Bakin said...

I thought it appropriate to add to this post a shout-out to the excellent (and highly opinionated!) book Let Over Lambda by Doug Hoyle. More than everything you wanted to know about how to use standard (i.e., non-hygienic macros ...