help sought for macro defining macro 
Author Message
 help sought for macro defining macro

I'm probably missing something simple here, but I'd like to define a macro that
generates a new macro when invoked.  The problem that I have is defining
substitution in the new macro---building nested (and deferred evaluation)
backquote constructs.

For example, I'd like to define a macro

  (defmacro build (macro-name macro-parameter-list)
     `(defmacro ,macro-name ,macro-parameter-list
          ...))

That would be result in the definition of

  (defmacro eval-of-macro-name (... macro-parameter-n ...)
     `(... ,macro-parameter-n ...))

Thanks,
 -- Kevin



Tue, 19 Dec 1995 01:41:44 GMT  
 help sought for macro defining macro

|>
|> I'm probably missing something simple here, but I'd like to define a macro that
|> generates a new macro when invoked.  The problem that I have is defining
|> substitution in the new macro---building nested (and deferred evaluation)
|> backquote constructs.
|>
|> For example, I'd like to define a macro
|>
|>   (defmacro build (macro-name macro-parameter-list)
|>      `(defmacro ,macro-name ,macro-parameter-list
|>           ...))
|>
|> That would be result in the definition of
|>
|>   (defmacro eval-of-macro-name (... macro-parameter-n ...)
|>      `(... ,macro-parameter-n ...))
|>
|> Thanks,
|>  -- Kevin

I'm not sure what you want here, what about something like this?

(defmacro rename (macro-name parms other-name)


For example:

USER(3): (rename my-cons (x l) cons)
MY-CONS
USER(4): (my-cons 1 2)
(1 . 2)

This avoids the doubly nested backquote.  If you want one, you can use
the following to substitute for (list ...)


Note that (list X Y) == `(,X ,Y)

See CLtL2 for a long discussion on the doubly nested backquote.




Tue, 19 Dec 1995 02:23:31 GMT  
 help sought for macro defining macro

Quote:
>I'm probably missing something simple here, but I'd like to define a macro that
>generates a new macro when invoked.  The problem that I have is defining
>substitution in the new macro---building nested (and deferred evaluation)
>backquote constructs.
>For example, I'd like to define a macro
>  (defmacro build (macro-name macro-parameter-list)
>     `(defmacro ,macro-name ,macro-parameter-list
>          ...))
>That would be result in the definition of
>  (defmacro eval-of-macro-name (... macro-parameter-n ...)
>     `(... ,macro-parameter-n ...))

I wonder if a definition like:

(defmacro build (macro-name macro-paramaters-and-body)
   `(defmacro ,(eval macro-name) ,(car macro-parameters-and-body)

;; (build (car '(name1 name2)) ((par1 par2) `(+ ,par1 ,par2)))
;;    -->
;;    (defmacro name1 (par1 par2)
;;       `(+ ,par1 ,par2))

is a typical case of abuse of EVAL. If so, is the
solution to make sure macro-name is evaluated *before* calling the
build macro? I once had a simmilar problem, but it was less
general, so I could solve it with a 'compute-macro-name'-function
where in the above code eval is used.

Geert-Jan



Tue, 19 Dec 1995 17:15:30 GMT  
 help sought for macro defining macro

Quote:

> This avoids the doubly nested backquote.  If you want one, you can use


> Note that (list X Y) == `(,X ,Y)

I strongly advocate against this use of doubly nested backquote.
Simply because it is not covered by CLtL chapter 22.1.3.



  "Where a comma occurs, the form following the comma is to be evaluated
   to produce _an_object_ to be inserted at that point."


   following the at-sign is evaluated to produce a list of objects. These
   objects are then spliced ..."

This implies that no splicing is done for a comma not followed by an at-sign.


it produces a series of objects.

Another way to look at it is your equation

             (list X Y) == `(,X ,Y)

which holds for any _single_ objects X and Y. Most of these equations you
have in mind, e.g.

             (cons X Y) == `(,X . ,Y)
             (cons X (list Y)) == `(,X ,Y)

are only valid for single objects, not for series of objects.

This lack of rules is also what makes this kind of doubly nested backquote
unreadable.

For example,


has no predictable value.

                    Bruno Haible



Wed, 20 Dec 1995 01:38:09 GMT  
 help sought for macro defining macro

Quote:

>I strongly advocate against this use of doubly nested backquote.
>Simply because it is not covered by CLtL chapter 22.1.3.




Sorry for entering this conversation in the middle, but I've been
frustrated by these double nested backquotes for a while. Page 528 may
imply that these constructs are meaningless, however in Appendix C, pages
968-970 indicate it does work and even offers an explanation of what they
mean. I must admit that when I tried those examples on several
CL implementations, no implentation would handle every example. In fact the
example implementation code in Appendix C wouldn't do it either without
a couple of modifications. It appears to be a lot of hand waving on
Guy Steele's part.

--
Tom Almy

Standard Disclaimers Apply



Wed, 20 Dec 1995 03:30:02 GMT  
 help sought for macro defining macro

Quote:


> > This avoids the doubly nested backquote.  If you want one, you can use


> > Note that (list X Y) == `(,X ,Y)

> I strongly advocate against this use of doubly nested backquote.
> Simply because it is not covered by CLtL chapter 22.1.3.

Sure it is.  On page 529: "If the backquote syntax is nested, the
innermost backquoted form should be expanded first.  This means that
if several commas occur in a row, the leftmost one belongs to the
innermost backquote." This is well-defined.

Quote:




No, they are not.  Appendix C of CLtL2 discusses nested backquoting in
general and these examples in particular:


This follows from following the transformation rules on pg 528, and
doing some simplification:




(append (list 'list) (list ''foo) q)
(list* 'list ''foo q)


of values of the elements of the value of q.'"






(list 'cons ''foo (append (list 'append) q))
(list 'cons ''foo (cons 'append q))


the values of the elements of the value of q, so that many splicings
occur.'"

Quote:
>   "Where a comma occurs, the form following the comma is to be evaluated
>    to produce _an_object_ to be inserted at that point."


>    following the at-sign is evaluated to produce a list of objects. These
>    objects are then spliced ..."

> This implies that no splicing is done for a comma not followed by an
> at-sign.

Maybe not at at level of expansion.

Quote:


> it produces a series of objects.

You're not thinking about this correctly.  In an expression like


preformed on that expression as the whole inner backquote is
processed.

Quote:
> Another way to look at it is your equation

>              (list X Y) == `(,X ,Y)

> which holds for any _single_ objects X and Y. Most of these equations you
> have in mind, e.g.

>              (cons X Y) == `(,X . ,Y)
>              (cons X (list Y)) == `(,X ,Y)

> are only valid for single objects, not for series of objects.

What do you mean?  Of course you can't substitute random splicing
operators for X and Y without changing the meaning of the expression.

Quote:

> This lack of rules is also what makes this kind of doubly nested backquote
> unreadable.

> For example,


> has no predictable value.

That's not relevant.  Nested backquote forms are well-defined after
being evaluated the appropriate number of times.  The intermediate
stages are implementation-dependent.


equivalent to to (APPEND (LIST) 2 3).  Of course, if you evaluate
this again you will get an error.

--

"Wind in my hair - Shifting and drifting - Mechanical music - Adrenaline surge"
        - Rush



Tue, 19 Dec 1995 21:19:44 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Macros defining macros with define-syntax

2. Question about a macro-defining macro

3. Macro-Defining Macros

4. Load-time problem with a macro that defines a macro

5. A macro involving two sub-macros - where the 2nd macro needs results from the first

6. Help with macros writing macros in Bigloo

7. define-macro -> define-syntax

8. define-macro vs define-syntax

9. Scheme macro source: define/keyed, for defining keyword-triggered arguments

10. macro -vs- macro/codeblock

11. syntax-rules macros with sub-macros

12. Scheme macro source: rewriter-widget, a widely applicable macro interface

 

 
Powered by phpBB® Forum Software