More Macro Mysteries 
Author Message
 More Macro Mysteries

OK, I admit that I've been working on this with far too little sleep,
but again, I just don't understand what's wrong here. I *think* that
this macro should write a function which calls one of a variety of
functions based upon the value of the symbol passed in. A wee 'object
method dispatch' as it were...

(define-syntax dispatcher
   (syntax-rules (selector args)
      ((dispatcher zel0 zel1 ...)
       (let-syntax (dispatch-case
                    (syntax-rules ()
                       ((dispatch-case ((sel arg ...) e1 e2 ...))
                        ((sel) (apply (lambda (arg ...) e1 e2 ...) args)))
                       ((dispatch-case ((sel arg ...) e1 e2 ...) sel1 sel2 ...)
                        ((sel) (apply (lambda (arg ...) e1 e2 ...) args))
                        (dispatch-case sel1 sel2 ...))))
          (lambda (selector . args)
             (case selector
                (dispatch-case zel0 zel1 ...)
                (else (dispatch-error (cons selector args)))))))))

Bigloo dumps core when expanding this version, or, if you change the
'zel's to 'sel's it complains at definition time that there aren't
enough ellipses following sel1 (but which? that's why I changed to
zel). Guile OTOH, doesn't like either version, complaining that there
is an extra ellipses in there somewhere at definition time. But then
again, Guile is Dybvig's syncase, not r5rs. AFAICT, the above should be
good r5rs, anyway?

Halp!

david rush
--

PS+++(--) PE(++) Y+ PGP !tv b+++ DI++ D+(--) e*(+++>+++) h---- r+++
z++++



Sat, 06 Jul 2002 03:00:00 GMT  
 More Macro Mysteries

Quote:

> OK, I admit that I've been working on this with far too little sleep,
> but again, I just don't understand what's wrong here. I *think* that
> this macro should write a function which calls one of a variety of
> functions based upon the value of the symbol passed in. A wee 'object
> method dispatch' as it were...

> (define-syntax dispatcher
>    (syntax-rules (selector args)
>       ((dispatcher zel0 zel1 ...)
>        (let-syntax (dispatch-case
>                (syntax-rules ()
>                   ((dispatch-case ((sel arg ...) e1 e2 ...))
>                    ((sel) (apply (lambda (arg ...) e1 e2 ...) args)))
>                   ((dispatch-case ((sel arg ...) e1 e2 ...) sel1 sel2 ...)
>                    ((sel) (apply (lambda (arg ...) e1 e2 ...) args))
>                    (dispatch-case sel1 sel2 ...))))
>      (lambda (selector . args)
>         (case selector
>            (dispatch-case zel0 zel1 ...)
>            (else (dispatch-error (cons selector args)))))))))

> Bigloo dumps core when expanding this version, or, if you change the
> 'zel's to 'sel's it complains at definition time that there aren't
> enough ellipses following sel1 (but which? that's why I changed to
> zel). Guile OTOH, doesn't like either version, complaining that there
> is an extra ellipses in there somewhere at definition time. But then
> again, Guile is Dybvig's syncase, not r5rs. AFAICT, the above should be
> good r5rs, anyway?

With R5RS macros, you cannot in general have `let-syntax' inside
`syntax-rules' because template expansion (matching those ellipses)
can be done in whole before the expanded template is scanned
further. Here the macro expander barfs because for example `arg' is
followed with ellipsis but `arg' does not appear as a pattern variable
in the pattern `(dispatcher zel0 zel1 ...)'.

Some macro implementations can have workarounds. Will Clinger's R4RS
macro implementation, for example, has special syntactic form `(:::
<whatever>)' that suppresses expandind ellipses inside `(:::
<whatever>)'. You need something like that to be able to define local
macros inside macro expansions.

--
Antti Huima



Sat, 06 Jul 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. R5RS Macro Mysteries

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

3. macro -vs- macro/codeblock

4. Help with macros writing macros in Bigloo

5. syntax-rules macros with sub-macros

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

7. Macros defining macros with define-syntax

8. symbol-macros and regular macros

9. Question about a macro-defining macro

10. Macro-Defining Macros

11. macro macros

12. Local macro within a macro?

 

 
Powered by phpBB® Forum Software