Search code examples
macroscommon-lispsbcllisp-macros

When and how often do macro expansions happen in SBCL Common Lisp implementation?


Some Lisp implementations (i) expand macros once and save the result for reuse; (ii) others reexpand the macro at each macro call. Some implementations (iii) even attempt to expand macro calls in function bodies at the time the function is DEFUNed.

Which one is the case for SBCL?

Thanks.


Solution

  • in the REPL:

    * (defparameter *expansions* 0)    
    *EXPANSIONS*
    * (defmacro foo ()
        (incf *expansions*)
        (print (list :expansions *expansions*))
        nil)
    * (foo)
    
    (:EXPANSIONS 1) 
    NIL
    * (defun bar () (foo))
    
    (:EXPANSIONS 2) 
    BAR
    * (bar)
    NIL
    * 
    

    So there is one expansion for a function definition and none at runtime of that function.

    Then one might try the file compiler, an interpreter, different debug options, etc. to check the number of macro expansions done by an implementation.