Lisp - Scope and Bindings in Macro



Macros as compared to functions operate on code itself instead of evaluation, so understanding scope and bindings of variables within a macro is very important. In this chapter, we're discussing how variables and symbols are handled in a macro.

Macro as Code Generator/Transformer

A macro is a function which can transform code. A macro takes LISP form as input and generates a new LISP form as output. A macro is evaluated at compile time. The resulted transformed code is evaluated by LISP compiler.

Hygienic Macro

While working with Macro, we should take case of unintentional variable capturing. A variable capture occurs when a macro introduces a variable of same name as passed as an argument to macro. It may lead to unintentional behavior.

Common Lisp, provides ways to handle variable capturing to create a Hygienic Macro. Consider the following case:

Variable Capturing

; define a macro to swap two numbers 
(defmacro swap (x y)
   `(let ((temp ,x))  ; variable capturing occurs when x is passed as temp
      (setf ,x ,y)
      (setf ,y temp)))
     
; define two variables temp and my-var
(let ((temp 1) (my-var 2))
      (swap temp my-var) ; swap macro unintentionally uses local temp over passed argument temp
      (print temp)       ; temp is not modified
      (print my-var))    ; my-var is not modified

Output

When you execute the code, it returns the following result −

1
2

Now in order to fix the variable capturing, we can use gensym function to create a generic symbol in order to create a unique temporary variable.

Hygienic Macro

; define a macro to swap two numbers 
(defmacro swap (x y)
   (let ((temp (gensym))) ; create a unique variable
      `(let ((,temp ,x))  ; insert the unique variable and value of x
         (setf ,x ,y)
         (setf ,y ,temp))))
     
(let ((temp 1) (my-var 2)) 
   (swap temp my-var) ; swap macro correctly uses passed temp argument
   (print temp) 
   (print my-var))

Output

When you execute the code, it returns the following result −

2
1

Here due to variable capturing, variable swap is not working.

Scope in Macro Expansion

Scope of a variable in a macro is determined by the lexical scope of the code produced by macro. When a macro is expanded, the resultant code is inserted into the context where macro is called.

Variables introduced by a macro are bound to same scoping rules as for any other variable in same context.

Use of symbol-macrolet

symbol-macrolet is used to create local symbolic macros and works at level of variable access. See the example below:

(let ((x 10))
   (symbol-macrolet ((y x)) ; y is now alias of x
   (print y)  ; prints 10
   (setf y 20) ; replace value of y as 20
   (print x))) ; print updated value of x as 20

Output

When you execute the code, it returns the following result −

10
20

Use of macrolet

macrolet is used to create local function macros and macro is available only within function and has limited lexical scope of that of function. See the example below:

(defun my-function (x)
  (macrolet ((square (n) `(* ,n ,n))) ; define a macro to get square of a number
    (square x)))  ; call the macro
    
(print(my-function 10))

Output

When you execute the code, it returns the following result −

100
Advertisements