Query on macro definitions within macro definitions within... 
Author Message
 Query on macro definitions within macro definitions within...

I am using Common Lisp. My problem is that of writing macro
definitions within macro definitions. I can't seem to
compose the appropriate combination of backquotes and
commas.  As well as seeking a solution to my particular
problem, it has also led me to wonder what, if any, are the
limitations of the backquote mechanism in the sense of,
which structures cannot be built using it?

What I am trying to do is this. I want to write a macro
which will define an accessor function for lists and also
define a setf method for that accessor. The behaviour I
would like is something like the following:

(setq alpha '(a b c d e f)) => (a b c d e f)
(defaccessor myfourth fourth) => myfourth
(myfourth alpha) => d
(setf (myfourth alpha) 'z) => z
alpha => (a b c z e f)

The definition for the defaccessor macro should be
something like:

(defmacro defaccessor (acc fn)
  (let ((var1 (gensym))
        (var2 (gensym))
        (var3 (gensym)))
   <backquote form>))

where <backquote form> should _expand_ into something like

(progn
  (defun myfourth (#:G1) (fourth #:G1))
  (defsetf myfourth (#:G2) (#:G3)
    `(setf (fourth ,#:G2) ,#:G3)))

when given the call (defaccessor myfourth fourth).

However trying to use backquote presents a problem. The form
inside the defaccessor macro requires nested backquotes, one
before the "(progn ..." and one before the "(setf ...". What
is required is that the outer backquote should be expanded
during the defaccessor call, followed by an expansion of the
inner one during the defsetf call, but _not_until_ defsetf
is called. However, according to Steele, with nested
backquotes the innermost backquoted form should be expanded
first (p.529 in 2nd ed, p.350 in 1st). I'm not clear as to
the rationale behind this but it suggests to me that what is
needed is a form inside the defsetf which expands into a
form which, when expanded due to the backquote outside the
progn, yields a backquoted form to pass to the defsetf (got
that?). Anyhow, having played with Steele's nifty backquote
implementation in Appendix C of Common Lisp 2 for long
enough to get frustrated, I give up. Questions: Am I going
about this the wrong way?, Can it be done at all, by this
means or otherwise?, Are there inherent limits on the kinds
of structures the backquote mechanism can produce and, if
so, what are they?, and What is the rationale behind the
expansion rules for nested backquotes?

       ,
Breanndan.

--
____________________________________________________________
       ,  ,      ,
Breanndan O Nuallain        Department of Computer Science
                            University College Dublin
                            Belfield, Dublin 4
Phone: 01-693244 ext 2487   Ireland
------------------------------------------------------------



Mon, 26 Oct 1992 23:59:34 GMT  
 Query on macro definitions within macro definitions within...

writes:

Quote:
>I am using Common Lisp. My problem is that of writing macro
>definitions within macro definitions. I can't seem to
>compose the appropriate combination of backquotes and
>commas.
...
>The definition for the defaccessor macro should be
>something like:

>(defmacro defaccessor (acc fn)
>  (let ((var1 (gensym))
>        (var2 (gensym))
>        (var3 (gensym)))
>   <backquote form>))

>where <backquote form> should _expand_ into something like

>(progn
>  (defun myfourth (#:G1) (fourth #:G1))
>  (defsetf myfourth (#:G2) (#:G3)
>    `(setf (fourth ,#:G2) ,#:G3)))

>when given the call (defaccessor myfourth fourth).

The following will have the behavior you want:

(defmacro defaccessor (acc fn)
  (let ((arg-var (gentemp "ARG"))
        (setf-arg-var (gentemp "SETF-ARG"))
        (setf-val-var (gentemp "SETF-VAL")))
    `(progn
      (defun ,acc (,arg-var) (,fn ,arg-var))
      (defsetf ,acc (,setf-arg-var) (,setf-val-var)
        `(setf (,',fn ,,setf-arg-var) ,,setf-val-var))
      )))

This macroexpands (defaccessor myfourth fourth) into:

(PROGN (DEFUN MYFOURTH (ARG7) (FOURTH ARG7))
       (DEFSETF MYFOURTH
                (SETF-ARG8)
                (SETF-VAL9)
                `(SETF (FOURTH ,SETF-ARG8) ,SETF-VAL9)))

I think that the following is a straightforward way to think about nested
backquotes: a comma in an inner backquote causes its form to be processed
as usual by the outer backquote.  

In the definition of DEFACCESSOR above, the form ,',FN will be turned into
,<FORM> where <FORM> is the result of the outer backquote's processing of
',FN.  Since the outer backquote processes ',FN to be 'FOURTH, the ,',FN
results in ,'FOURTH, which is transformed into the equivalent FOURTH.

Alternatively, the form ,,SETF-ARG-VAR is transformed into ,<FORM2> where
FORM2 is the result of the outer backquote's processing of ,SETF-ARG-VAR.
FORM2 becomes SETF-ARG8, so ,,SETF-ARG-VAR becomes ,SETF-ARG8.

I don't know if this makes things any clearer, but at least the example
above shows the two main cases you need in using nested backquotes.

Bruce Krulwich
Institute for the Learning Sciences



Tue, 27 Oct 1992 23:53:44 GMT  
 Query on macro definitions within macro definitions within...

Quote:
> I am using Common Lisp. My problem is that of writing macro
> definitions within macro definitions.
[stuff deleted]
> What I am trying to do is this. I want to write a macro
> which will define an accessor function for lists and also
> define a setf method for that accessor. The behaviour I
> would like is something like the following:

> (setq alpha '(a b c d e f)) => (a b c d e f)
> (defaccessor myfourth fourth) => myfourth
> (myfourth alpha) => d
> (setf (myfourth alpha) 'z) => z
> alpha => (a b c z e f)

Try:

(defmacro defaccessor (acc fn)
  (let ( (v1 (gensym))
         (v2 (gensym))
         (v3 (gensym)) )
    `(progn
       (defun ,acc (,v1) (,fn ,v1))
       (defsetf ,acc (,v2) (,v3)
         (list 'setf (list ',fn ,v2) ,v3)))))

or if you must use mested backquotes:

(defmacro defaccessor (acc fn)
  (let ( (v1 (gensym)) )
    `(progn
       (defun ,acc (,v1) (,fn ,v1))
       (defsetf ,acc (v2) (v3)
         `(setf (,',fn ,v2) ,v3)))))

You cannot use nested backquotes and also have gensyms as your
dummy arguments to defstef.  (But then again it doesn't really matter
because they are just dummy arguments.)  This is because backquote
expression are expanded at read time, i.e. when defaccessor is defined.
But the values of v2 and v3 are not known until eval time, i.e. when
defaccessor is invoked.

Another way to look at it is this: what you were trying to do was to
create a nested backquote expression which expanded into an expression
with commas in it.  But this is not possible because all commas vanish
at read time.

Erann Gat



Wed, 28 Oct 1992 00:54:32 GMT  
 Query on macro definitions within macro definitions within...

Quote:
>I am using Common Lisp. My problem is that of writing macro
>definitions within macro definitions. I can't seem to
>compose the appropriate combination of backquotes and
>commas.  As well as seeking a solution to my particular
>problem, it has also led me to wonder what, if any, are the
>limitations of the backquote mechanism in the sense of,
>which structures cannot be built using it?

Most of the limitations in using backquote come from confusion on how
to describe the form you are trying to build to the backquote
mechanism. It can be hard.

Quote:
>What I am trying to do is this. I want to write a macro
>which will define an accessor function for lists and also
>define a setf method for that accessor. The behaviour I
>would like is something like the following:
...
>The definition for the defaccessor macro should be
>something like:

[...It expands into:]
Quote:

>(progn
>  (defun myfourth (#:G1) (fourth #:G1))
>  (defsetf myfourth (#:G2) (#:G3)
>    `(setf (fourth ,#:G2) ,#:G3)))

This will do what you describe:

(defmacro defaccessor (acc fn)
  (let ((var1 (gensym))
        (var2 (gensym))
        (var3 (gensym)))
    `(progn
       (defun ,acc (,var1) (,fn ,var1))
       (defsetf ,acc (,var2) (,var3)
         `(setf (,',fn ,,var2) ,,var3)))))

Quote:
>when given the call (defaccessor myfourth fourth).

>However trying to use backquote presents a problem. The form
>inside the defaccessor macro requires nested backquotes, one
>before the "(progn ..." and one before the "(setf ...". What
>is required is that the outer backquote should be expanded
>during the defaccessor call, followed by an expansion of the
>inner one during the defsetf call, but _not_until_ defsetf
>is called. However, according to Steele, with nested
>backquotes the innermost backquoted form should be expanded
>first (p.529 in 2nd ed, p.350 in 1st). I'm not clear as to
>the rationale behind this...

I think your confused as to what expansion means here. Steele is
talking about expansion of the backquote macro itself, not the
creation at runtime of the structure for which the backquoted form it
the template. Expansion is described in{*filter*}detail on p528 of CLtL2.
Appendix C helps.

Quote:
>... but it suggests to me that what is
>needed is a form inside the defsetf which expands into a
>form which, when expanded due to the backquote outside the
>progn, yields a backquoted form to pass to the defsetf (got
>that?).

Your tying yourself in knots :-)  Take a look at the forms that are
produced by backquote, using either the code produced by Appendix C or
your implementation's own backquote.

Quote:
>____________________________________________________________
>       ,  ,      ,
>Breanndan O Nuallain        Department of Computer Science
>                            University College Dublin
>                            Belfield, Dublin 4
>Phone: 01-693244 ext 2487   Ireland
>------------------------------------------------------------


"Ah, youth. Ah, statute of limitations."
                -John Waters


Wed, 28 Oct 1992 01:11:45 GMT  
 Query on macro definitions within macro definitions within...
I often hear that Lisp is OK for research/prototyping/toy systems, but
no one does "real" applications in it.  I know better than that.  At
Texas Instruments, we have several "real" systems including the
Explorer itself and the Droid VLSI CAD system.  I know of a fair
number of others in the world including the ICAD mechanical CAD
system, the various expert systems shells...

I'm trying to collect a list of "real" applications written in Lisp.
For this purpose, a system gets more "real" as:

1) It gets larger.
2) It is in daily use for some purpose other than it's own
   development.
3) It is commerically marketed
4) It is supporting some "important" part of a business e.g.; running
   a factory, designing turbine blades, planning corporate
   strategy...

If you know of such things (that you either developed or use), I'd
like to hear about them.  Don't worry, it won't show up in some TI
adverti{*filter*}t, but don't tell me anything proprietary, either.  But
I'd appreciate it if you could send me a message telling:

1) What does it do?
2) Who did it?
3) How big is it?
4) How many users does it have?
5) Is it being actively supported or marketed?
6) Why did you choose Lisp and are you glad?

Many thanks
-- Doug



Tue, 03 Nov 1992 23:57:52 GMT  
 Query on macro definitions within macro definitions within...

Doug,
please post the results you find here in this group, I'd like
to hear of them.

One company, Interleaf, which makes a large Desk TOp Publishing
system for SUn workstations and others is written in a
high percentage of Lisp (their own dialect I think), as well
as C code.

Other examples - contact Lucid in Palo ALTO, CA - or FRANZ INC.
they can give you a whole boatload of ideas.

        'mark
--
=============== regards   'mark  =============================================
Mark Ahlenius             voice:(708)-632-5346  email: uunet!motcid!ahleniusm
Motorola Inc.             fax:  (708)-632-2413
Arlington, Hts. IL, USA  60004



Fri, 06 Nov 1992 23:13:09 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Local macro within a macro?

2. Workaround for the problem with groups within a file definition

3. type definitions within record scopes

4. Functions attributes accessed from within the function definition URGENT please

5. New definition for Simple Macro

6. Internal definitions and macros

7. Loops in macro definition

8. Preprocessor for parameterized macro definition

9. Multiline macro definition

10. How to specify a string value in a +define macro definition

11. macro that examines function-definition at run-time?

12. local macro definitions

 

 
Powered by phpBB® Forum Software