From: ···········@gmail.com
Subject: define-modify-macro
Date: 
Message-ID: <4300545c-3db4-4716-a94b-2b4fd97d1acf@f63g2000hsf.googlegroups.com>
I was just reading the chapter on macros in Paul Graham's ANSI Common
Lisp, and I came across the part regarding define-modify-macro. It
explains why a custom definition of incf like the following is no
good:

(defmacro incf* (x feoptional (y 1))
  ' ( setf ,x (+ ,x , y ) ) )

because then things like (incf* (car (push 1 lst))), assuming lst were
initially NIL for instance, would not expand correctly.

Now, that's all fine and well, and I see the point perfectly. But I
was just a little curious about something that the book actually
doesn't explain:

Is define-modify-macro just a shortcut macro that we use as "syntactic
sugar" (and thus implementable somehow with DEFMACRO for example) or
is its presence inevitable because the rest of the macro system is not
powerful enough to encompass the previous situation concerning INCF
and others analogous to it?

From: ···········@gmail.com
Subject: Re: define-modify-macro
Date: 
Message-ID: <612157aa-8320-47fe-8dfa-5f2f14a69b7e@y38g2000hsy.googlegroups.com>
On May 10, 12:03 pm, ···········@gmail.com wrote:
> I was just reading the chapter on macros in Paul Graham's ANSI Common
> Lisp, and I came across the part regarding define-modify-macro. It
> explains why a custom definition of incf like the following is no
> good:
>
> (defmacro incf* (x feoptional (y 1))
>   ' ( setf ,x (+ ,x , y ) ) )
>
> because then things like (incf* (car (push 1 lst))), assuming lst were
> initially NIL for instance, would not expand correctly.
>
> Now, that's all fine and well, and I see the point perfectly. But I
> was just a little curious about something that the book actually
> doesn't explain:
>
> Is define-modify-macro just a shortcut macro that we use as "syntactic
> sugar" (and thus implementable somehow with DEFMACRO for example) or
> is its presence inevitable because the rest of the macro system is not
> powerful enough to encompass the previous situation concerning INCF
> and others analogous to it?

Correction! Sorry but I didnt't write the macro correctly (backquote!)
in the previous post:

(defmacro incf (x feoptional (y 1))
  `(setf ,x (+ ,x ,y)))
From: Kent M Pitman
Subject: Re: define-modify-macro
Date: 
Message-ID: <uiqxm2sg6.fsf@nhplace.com>
···········@gmail.com writes:

> Is define-modify-macro just a shortcut macro that we use as "syntactic
> sugar" (and thus implementable somehow with DEFMACRO for example) or
> is its presence inevitable because the rest of the macro system is not
> powerful enough to encompass the previous situation concerning INCF
> and others analogous to it?

No, it's not impossible to write define-modify-macro yourself as an 
ordinary macro.  It's just annoying to try to get the details right,
and a lot of people aren't skilled at it.  This macro offers the 
functionality as a service that's pretty accessible without a lot of
specialized knowledge, and also so that even if you know what those
specialized issues are and could write it, you don't injure yourself 
when you're in a hurry.
From: ···········@gmail.com
Subject: Re: define-modify-macro
Date: 
Message-ID: <c2b62c16-8f88-42db-90fa-42f2ffdbd53a@x41g2000hsb.googlegroups.com>
On May 10, 3:34 pm, Kent M Pitman <······@nhplace.com> wrote:
> ···········@gmail.com writes:
> > Is define-modify-macro just a shortcut macro that we use as "syntactic
> > sugar" (and thus implementable somehow with DEFMACRO for example) or
> > is its presence inevitable because the rest of the macro system is not
> > powerful enough to encompass the previous situation concerning INCF
> > and others analogous to it?
>
> No, it's not impossible to write define-modify-macro yourself as an
> ordinary macro.  It's just annoying to try to get the details right,
> and a lot of people aren't skilled at it.  This macro offers the
> functionality as a service that's pretty accessible without a lot of
> specialized knowledge, and also so that even if you know what those
> specialized issues are and could write it, you don't injure yourself
> when you're in a hurry.

OK. I see. Thank you. One last thing: what if I wanted to write
something like that INCF* but only locally within a MACROLET for
example, I suppose I could not take advantage of DEFINE-MODIFY-MACRO
in such a situation. Is there any way to achieve the smae
functionality, but only in a local scope?

Thanks again for your help.
From: Alan Crowe
Subject: Re: define-modify-macro
Date: 
Message-ID: <867ie2nk5r.fsf@cawtech.freeserve.co.uk>
···········@gmail.com writes:

> OK. I see. Thank you. One last thing: what if I wanted to write
> something like that INCF* but only locally within a MACROLET for
> example, I suppose I could not take advantage of DEFINE-MODIFY-MACRO
> in such a situation. Is there any way to achieve the smae
> functionality, but only in a local scope?
> 

One approach would be to use the package system.

(defpackage "BEHIND-THE-SCENES"
  (:use "CL")
  (:export "CHANGE-IT"))

(inpackage "BEHIND-THE-SCENES")

(define-modify-macro change-it () funk)

(defpackage "REAL-PROG"
  (:use "CL") ;remember NOT to use BEHIND-THE-SCENES
  ;; Don't import BEHIND-THE-SCENES:CHANGE-IT
  )

(in-package "REAL-PROG")

(defun change-it () ...) ;This gets shadowed

(macrolet ((change-it (&rest args) 
             (cons 'behind-the-scenes:change-it
                   args)))
  (let ((x 10))
    (change-it x) ;local modify macro
    x))
  
Alan Crowe
Edinburgh
Scotland
From: Alex Mizrahi
Subject: Re: define-modify-macro
Date: 
Message-ID: <4826ba38$0$90262$14726298@news.sunsite.dk>
 rr> OK. I see. Thank you. One last thing: what if I wanted to write
 rr> something like that INCF* but only locally within a MACROLET for
 rr> example,

locally you can avoid writting weirdness like (car (push 1 lst)), so setf 
will be just fine