more fun with macros 
Author Message
 more fun with macros

Yes it's been a macro kind of week.  Amazing how much more complicated
ac concept becomes when you actually try to do something useful with
it.

Can somebody provide a real-world example of a problem which can be
solved with define-macro, aka lisp style macros, but not with
define-syntax/syntax-rules?

Thanks

Brian



Fri, 21 Jun 2002 03:00:00 GMT  
 more fun with macros
Brian Denheyer:

Quote:
> Can somebody provide a real-world example of a problem which can be
> solved with define-macro, aka lisp style macros, but not with
> define-syntax/syntax-rules?

Suppose yoo want a macro like Common Lisp's DEFSTRUCT: the macro call
should expand into a sequence of definitions of variables whose names
were created by the macro.  SYNTAX-RULES won't do this for you.

rthappe



Sat, 22 Jun 2002 03:00:00 GMT  
 more fun with macros

Quote:

> Yes it's been a macro kind of week.  Amazing how much more complicated
> ac concept becomes when you actually try to do something useful with
> it.

> Can somebody provide a real-world example of a problem which can be
> solved with define-macro, aka lisp style macros, but not with
> define-syntax/syntax-rules?

Two of the things that SYNTAX-RULES can't yet do is splicing names and
distinguishing between different kinds of atomic data.

The first problem was also mentioned by another poster.  Say I want to
define a macro DEFINE-RECORD that expands

        (define-record point (x))  ; 1D point

into

        (define point (make-record-type 'point '(x)))
        (define point? (record-predicate point))
        (define make-point (record-constructor point))
        (define point-x (record-accessor point 'x))
        (define point-x-set! (record-updater point 'x))

I can't do this with SYNTAX-RULES because, even though the macro has
both "point" and "x" in hand, it cannot splice them to "point-x".  It
seems to me that the macro system ought to be able to do this.  For
example, let the syntax (+++ token ...) where each token must be a
symbol be taken to mean the literal splicing of the tokens into a single
symbol.  Then:

        (define-syntax point
          (syntax-rules ()
            ((define-record ?name ?fields)
             (begin (define ?name
                      (make-record-type '?name ?fields))
                    (define (+++ ?name ?)
                      (record-predicate ?name))
                    (define (+++ make- ?name)
                      (record-constructor ?name))
                    (define-record-accessors ?name ?fields)
                    (define-record-updaters ?name ?fields)))))

        (define-syntax define-record-accessors
          (syntax-rules ()
            ((define-record-accessors ?name ())
             #t)
            ((define-record-accessors ?name (?x ?y ...))
             (begin (define (+++ ?name - ?f)
                      (record-accessor ?name '?x))
                    (define-record-accessors ?name (?y ...))))))

        ; ditto for define-record-updaters

I haven't tried implementing this, however.

The other problem came up when I was trying to rewrite the MATCH macro
using SYNTAX-RULES.  Everything went along swimmingly until I discovered
that while SYNTAX-RULES can distinguish the string "foo" from the symbol
bar it cannot distinguish the class of strings from the class of
symbols, which I needed to do.  Thus I ended up writing most of MATCH
using SYNTAX-RULES, but I had to define a small auxiliary nonhygienic
macro that distinguishes among atomic data.  No doubt we can find a
working solution for this too, if we want.

(I should mention that I didn't try to implement all of the facilities
of MATCH, so there may be other problems lurking in a full translation!)

--lars



Sun, 30 Jun 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. count bits, fun fun fun.

2. Macro fun

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

4. macro -vs- macro/codeblock

5. Help with macros writing macros in Bigloo

6. syntax-rules macros with sub-macros

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

8. Macros defining macros with define-syntax

9. symbol-macros and regular macros

10. Question about a macro-defining macro

11. Macro-Defining Macros

12. macro macros

 

 
Powered by phpBB® Forum Software