Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it necessary to recompile the definitions that use a Common Lisp macro when we modify it?

This is one doubt that always made me curious.

Why is it necessary to also recompile the definitions that use a Common Lisp macro when we modify it (besides, obviously, recompile the modified Common Lisp macro)?

Thanks in advance.

like image 383
Paulo Tomé Avatar asked Jun 07 '13 10:06

Paulo Tomé


2 Answers

Common Lisp macros are code substitutions, they happen at "macro expansion time". For interpreted code, this may be "run time". For compiled code, it's almost always during "compile time".

Once the code has been substituted, there is no reference to the macro in the resulting compiled code (the original source on disk still has this, if the macro usage has been saved to a file).

While there would be some convenience in a lisp system that saved all "pre-macro expansion" function bodies and kept track of what macros had been used where and automatically re-compile anything that uses a given macro (possibly recursively), that was left out of the standard.

Typically, I write my utility macros quite early in development, so there's not much need, for me, to have this functionality and I am on the whole happier without it than I would be with it (it cuts down on the running image size quite a bit, not needing to track all of that).

like image 69
Vatine Avatar answered Oct 26 '22 17:10

Vatine


Note that it may be necessary.

The reason is that macros in compiled implementations are expanded only once at compile time and the macro itself is not present in the generated code.

To be more clear consider

(defmacro badsquare (x)
  `(* ,x ,x))

(defun f (x)
  (badsquare (+ x 3))

when the compiler analyzes f code will expand it to

(defun f (x)
  (* (+ x 3) (+ x 3)))

and the references to the badsquare macro are not necessarily present any more so if you redefine the badsquare macro to something else this will have no effect on f unless you also recompile it.

like image 21
6502 Avatar answered Oct 26 '22 16:10

6502