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

Why does my Macro get expanded 8 times?

    Date: Mon, 11 Jul 88 22:33:39 PDT
    From: gyro@kestrel.ARPA (Scott B. Layson)

    To prevent multiple macro expansion, you could make your macro a
    so-called "displacing" macro, using ZL:DEFMACRO-DISPLACE.  This will
    cause it to modify the the cons which is the head of the form that
    invokes the macro (got that?), effectively caching the expansion
    there.  (Try it to see the details; it's really pretty

    SCRC folks: surely one of the ways you considered fixing the problem
    was to make all macros displace in Phase I.  Why doesn't this work?

Because displacing macros are the wrong thing in a language with lexical
scoping.  This is because the expansion of a macro may depend on the
lexical environment at the time the expansion is done.  The failing case
is where the expansion of one macro contains a macro invocation of a
second macro.  See below:

(defmacro m1 ()

(defmacro m2 (&environment env)
  (macroexpand '(m3) env))

(defun f1 ()
  (macrolet ((m3 () '1))

(defun f2 ()
  (macrolet ((m3 () '2))

Consider what would happen if M2 were a displacing macro.  When F1 was
compiled, it would splice its expansion ('1) in place of the macro call
(the quoted constant in M1).  When F2 was compiled, the wrong expansion
for M2 would be used.