[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Does defmacro need a lexicality barrier?



It is not clear to me how defmacro is supposed to create the expansion
function to be stored as the macro-function of the given symbol.  The
issues related to understanding the argument list are reasonably
obvious, but those relating to the lexical scope of the function are
not.

Consider the following oversimplified outline for the definition of
defmacro:

(defmacro defmacro (name arg-list &body body)
        ... analyze the argument list and construct the pieces out of
which
        ... an expansion funcion could be put together.  Store the
actual
        ... S-expression on the variable expansion-function.
        
        `(setf (macro-function ,name) ,expansion-function) )

This definition ignores such features as documentation strings, but what
I'm really concerned about is what to put in the variable
``expansion-function''.  If we set it to something like

        #'(lambda (form env) ...)

then the macro body will have access to any variables in the lexical
environment of the defmacro.  It appears that we could fix that by
changing #' into simply ':

        '(lambda (form env) ...)

Unfortunately, this implies reasonably strongly that the macro body will
not be compiled, since it is only a random list.  I suppose that it is
possible that the compiler would notice that this random list is being
stuck somewhere that a funcallable object is supposed to go, but it
doesn't sound either likely or correct.  If I am worried about getting
the expansion function compiled, but in the correct (null) lexical
environment, I guess I can set ``expansion-function'' to

        (compile nil '(lambda (form env) ...))

This has the somewhat strange effect of preventing the existence of
interpreted macros.  Surely this is not the intent of the design of
Common Lisp.

What I would really like is a method for the construction of
``closures'' over the null lexical environment.  As far as I can tell,
Common Lisp does not supply such a mechanism.  Am I just totally
confused?  Is this whole point moot?  After all, what is the meaning of
a defmacro that makes reference to lexical variables?  If it ``is an
error'', then I can use the #' solution above.

What does everyone else do?  Do we need a new special form
``with-null-lexical-environment''?

        Pavel