0

I am trying to do the exercises on this tutorial about CLOS using SBCL and Slime (Emacs).

I have this class, instance, and function to set values for the slots:

(defclass point ()
  (x y z))

(defvar my-point
  (make-instance 'point))

(defun with-slots-set-point-values (point a b c)
  (with-slots (x y z) point (setf x a y b z c)))

Using the REPL, it works fine:

CL-USER> (with-slots-set-point-values my-point 111 222 333)

333

CL-USER> (describe my-point)
#<POINT {1003747793}>
  [standard-object]

Slots with :INSTANCE allocation:
  X                              = 111
  Y                              = 222
  Z                              = 333
; No value

Now, the exercises indicates that using the symbol-macrolet I need to implement my version of with-slots.

I have a partial implementation of my with-slots (I still need to insert add the operation):

(defun partial-my-with-slots (slot-list object)
   (mapcar #'(lambda (alpha beta) (list alpha beta))
           slot-list
           (mapcar #'(lambda (var) (slot-value object var)) slot-list)))

It works when calling it:

CL-USER> (partial-my-with-slots '(x y z) my-point)
((X 111) (Y 222) (Z 333))

Since this use of symbol-macrolet works:

CL-USER> (symbol-macrolet ((x 111) (y 222) (z 333))
   (+ x y z))
666

I tried doing:

CL-USER> (symbol-macrolet (partial-my-with-slots '(x y z) my-point)
   (+ x y z))

But, for some reason that I do not know, Slime throws the error:

malformed symbol/expansion pair: PARTIAL-MY-WITH-SLOTS
   [Condition of type SB-INT:SIMPLE-PROGRAM-ERROR]

Why does this happen? How can I fix this?

Pedro Delfino
  • 2,421
  • 1
  • 15
  • 30

2 Answers2

1

You need to return expressions that will call slot-value when substituted into the macro expansion, rather than calling the function immediately. Backquote is useful for this.

(defun partial-my-with-slots (slot-list object)
   (mapcar #'(lambda (alpha beta) (list alpha beta))
           slot-list
           (mapcar #'(lambda (var) `(slot-value ,object ',var)) slot-list)))
> (partial-my-with-slots '(x y z) 'my-point)
((x (slot-value my-point 'x)) (y (slot-value my-point 'y)) (z (slot-value my-point 'z)))

You use this in your with-slots macro like this:

(defmacro my-with-slots ((&rest slot-names) instance-form &body body)
  `(symbol-macrolet ,(partial-my-with-slots slot-names instance-form)
     ,@body))

> (macroexpand '(my-with-slots (x y z) point (setf x a y b z c))) 
(SYMBOL-MACROLET ((X (SLOT-VALUE POINT 'X))
                  (Y (SLOT-VALUE POINT 'Y))
                  (Z (SLOT-VALUE POINT 'Z)))
  (SETF X A
        Y B
        Z C))
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

You can't write with-slots as a function which is called at run time. Instead it needs to be a function which takes source code as an argument and returns other source code. In particular if given this argument

(my-with-slots (x ...) <something> <form> ...)

It should return this result:

(let ((<invisible-variable> <something))
  (symbol-macrolet ((x (slot-value <invisible-variable>)) ...)
    <form> ...))

You need <invisible-variable> so you evaluate <object-form> only once.

Well, here is a function which does most of that:

(defun mws-expander (form)
  (destructuring-bind (mws (&rest slot-names) object-form &rest forms) form
    (declare (ignore mws))
    `(let ((<invisible-variable> ,object-form))
       (symbol-macrolet ,(mapcar (lambda (slot-name)
                                    `(,slot-name (slot-value <invisible-variable> 
                                                             ',slot-name)))
                                  slot-names)
         ,@forms))))

And you can check this:

> (mws-expander '(my-with-slots (x y) a (list x y)))
(let ((<invisible-variable> a))
  (symbol-macrolet ((x (slot-value <invisible-variable> 'x))
                    (y (slot-value <invisible-variable> 'y)))
    (list x y)))

So that's almost right, except the invisible variable really needs to be invisible:

(defun mws-expander (form)
  (destructuring-bind (mws (&rest slot-names) object-form &rest forms) form
    (declare (ignore mws))
    (let ((<invisible-variable> (gensym)))
      `(let ((,<invisible-variable> ,object-form))
         (symbol-macrolet ,(mapcar (lambda (slot-name)
                                     `(,slot-name (slot-value ,<invisible-variable>
                                                              ',slot-name)))
                                   slot-names)
         ,@forms)))))

And now:

> (mws-expander '(my-with-slots (x y) a (list x y)))
(let ((#:g1509 a))
  (symbol-macrolet ((x (slot-value #:g1509 'x))
                    (y (slot-value #:g1509 'y)))
    (list x y)))

Well, a function which takes source code as an argument and returns other source code is a macro. So, finally, we need to install this function as a macroexpander, arranging to ignore the second argument that macro functions get:

(setf (macro-function 'mws)
      (lambda (form environment)
        (declare (ignore environment))
        (mws-expander form)))

And now:

> (macroexpand '(mws (x y) a (list x y)))
(let ((#:g1434 a))
  (symbol-macrolet ((x (slot-value #:g1434 'x)) (y (slot-value #:g1434 'y)))
    (list x y)))

This would be more conventionally written using defmacro, of course:

(defmacro mws ((&rest slot-names) object-form &rest forms)
  (let ((<invisible-variable> (gensym)))
    `(let ((,<invisible-variable> ,object-form))
       (symbol-macrolet ,(mapcar (lambda (slot-name)
                                   `(,slot-name (slot-value ,<invisible-variable> ',slot-name)))
                                 slot-names)
         ,@forms))))

However the two definitions are equivalent (modulo needing some eval-whenery to make the first work properly with the compiler).

ignis volens
  • 7,040
  • 2
  • 12
  • Thanks. But, I am afraid I can't solve the `Exercise: Use symbol-macrolet to implement with-slots`. I can do the following with: `CL-USER> (with-slots (x y z) my-point (sqrt (+ (* x x) (* y y) (* z z))))`. But, your solution fails with: `(mws (x y z) my-point (sqrt (+ (* x x) (* y y) (* z z))))`. Using the `defmacro` definition. It returns a compile-time error: `malformed symbol/expansion pair: (SLOT-VALUE #:G634 'X)` – Pedro Delfino Mar 07 '22 at 18:28
  • 1
    @PedroDelfino: there was a very obvious error in the definitions which is fixed now. – ignis volens Mar 07 '22 at 18:44