syntax-rules macros with sub-macros 
Author Message
 syntax-rules macros with sub-macros

Hello guys,

I've written a macro that utilizes several sub-macros to do its
transformations.  The sub-macros are only interesting to the
toplevel macro and shouldn't need to be defined globally.  The
only way I've been able to have local macros is to specify them
with let-syntax *inside* the syntax-rule templates, forcing me
to repeat them for every (<pattern> <template>) rule.  It
becomes a mess when there are many macros.  For instance, the
following

  (define-syntax argtype
    (syntax-rules ()
      ((argtype (name type)) type)
      ((argtype name) <top>)))

  (define-syntax argtypes
    (syntax-rules ()
      ((argtypes arg) (cons (argtype arg) '()))
      ((argtypes arg arg1 ...) (cons (argtype arg) (argtypes arg1 ...)))))

becomes

  (define-syntax argtypes
    (syntax-rules ()
      ((argtypes arg)
        (let-syntax ((argtype (syntax-rules ()
                                ((argtype (name type)) type)
                                ((argtype name) <top>))))
          (cons (argtype arg) '())))
      ((argtypes arg arg1 ...)
        (let-syntax ((argtype (syntax-rules ()
                                ((argtype (name type)) type)
                                ((argtype name) <top>))))
          (cons (argtype arg) (argtypes arg1 ...))))))

and that is just one macro depending on one other.  The macro I am working on
has more complex dependencies than this...

Does anyone know of a better way to structure macros with sub-macros so
the internal sub-macros are hidden from the toplevel environment and shared
between the (<pattern> <template>) rules?

BTW, I'm using Guile with Goops, and the code above depends on the
(ice-9 syncase) and (oop goops) modules.

Cheers,

  Lars J



Fri, 25 Apr 2003 03:00:00 GMT  
 syntax-rules macros with sub-macros

Quote:
> Does anyone know of a better way to structure macros with sub-macros so
> the internal sub-macros are hidden from the toplevel environment and shared
> between the (<pattern> <template>) rules?

First I will explain how to do it using a non-R5RS feature, and then
I will mention how you can do it in R5RS Scheme.

Since the R4RS high-level macro system was published, we have learned
a few things that should be added, but we haven't had a standards
mechanism in place that could make meaningful changes to the language.
(The R5RS reflects the few changes for which unanimous consent could
be obtained at a workshop in Palo Alto.)  Anyway, one of the non-R5RS
features that is present in every R5RS-conforming implementation that
I know about is the use of "..." as a quoting device to prevent other
ellipses from being evaluated at the wrong level.

With this non-R5RS feature, you could write

  ; untested, for illustration only

  (define-syntax argtypes
    (syntax-rules ()
      ((argtypes arg1 ...)
       (letrec-syntax ((argtype (syntax-rules ()
                                  ((argtype (name type)) type)
                                  ((argtype name) <top>))))
                       (argtypes (syntax-rules ()
                                   ((argtypes arg)
                                    (cons (argtype arg) '()))
                                   ((argtypes arg arg1 (... ...))
                                    (cons (argtype arg)
                                          (argtypes arg1 (... ...)))))))
         (argtypes arg1 ...)))))

If you want to avoid the non-R5RS feature, you can rewrite this to
use the single-dot pattern notation instead of ellipses.  This is
left as an exercise for the reader.

Will



Fri, 25 Apr 2003 03:00:00 GMT  
 syntax-rules macros with sub-macros

Quote:
> Anyway, one of the non-R5RS
> features that is present in every R5RS-conforming implementation that
> I know about is the use of "..." as a quoting device to prevent other
> ellipses from being evaluated at the wrong level.

It doesn't seem like Guile supports it, but I believe Guile is R5RS
compliant - so that's the exception to the rule, I guess...

Quote:
> With this non-R5RS feature, you could write

>  ; untested, for illustration only

>  (define-syntax argtypes
>    (syntax-rules ()
>      ((argtypes arg1 ...)
>       (letrec-syntax ((argtype (syntax-rules ()
>                                  ((argtype (name type)) type)
>                                  ((argtype name) <top>))))

                                                           ^ one too many...

Quote:
>                       (argtypes (syntax-rules ()
>                                   ((argtypes arg)
>                                    (cons (argtype arg) '()))
>                                   ((argtypes arg arg1 (... ...))
>                                    (cons (argtype arg)
>                                          (argtypes arg1 (... ...)))))))
>         (argtypes arg1 ...)))))

> If you want to avoid the non-R5RS feature, you can rewrite this to
> use the single-dot pattern notation instead of ellipses.  This is
> left as an exercise for the reader.

I'll have to do that then...

Thanks for your help,

  Lars J



Sat, 26 Apr 2003 03:00:00 GMT  
 syntax-rules macros with sub-macros

Quote:
> Does anyone know of a better way to structure macros with sub-macros
> so the internal sub-macros are hidden from the toplevel environment
> and shared between the (<pattern> <template>) rules?

In Common Lisp, there's something called MACROLET, and it does a fine
job of this exact thing, if I understand what you want correctly.  You
may consider finding an implementation of this in Scheme.

However, MACROLET works very similarly to DEFMACRO, which you know is
completely different from the SYNTAX-RULES style of defining macros.

I'm responding because I'm recently interesting in finding some way to
improve on the way Common Lisp macros are written, but without
compromising the flexibility of them (especially with respect to the
dynamic environment, which I think means that I like the ability that
my macros can be non-hygenic, especially since I've seen this features
used quite a bit).

Oh, and the non-hygenic stuff...that reminds me...

It also sounds like what you want to do might be in the category of
non-hygenic, if you implement it a certain way.  But I don't know.
I'm stuck in my current Common Lisp macro world, and I'm trying to
break out of it.

dave



Fri, 09 May 2003 03:00:00 GMT  
 syntax-rules macros with sub-macros

Quote:

> > Does anyone know of a better way to structure macros with sub-macros
> > so the internal sub-macros are hidden from the toplevel environment
> > and shared between the (<pattern> <template>) rules?

> In Common Lisp, there's something called MACROLET, and it does a fine
> job of this exact thing, if I understand what you want correctly.  You
> may consider finding an implementation of this in Scheme.

I ended up abandoning the syntax-rules macro system and used Guile's
procedure->syntax instead.

  Lars J



Fri, 09 May 2003 03:00:00 GMT  
 syntax-rules macros with sub-macros

+---------------

| > Does anyone know of a better way to structure macros with sub-macros
| > so the internal sub-macros are hidden from the toplevel environment
| > and shared between the (<pattern> <template>) rules?
|
| In Common Lisp, there's something called MACROLET, and it does a fine
| job of this exact thing, if I understand what you want correctly.  You
| may consider finding an implementation of this in Scheme.
+---------------

Well, for what it's worth, "let-syntax" and "letrec-syntax" are part
of the R5RS standard, so any Scheme implementing R5RS *should* provide
them (along with "define-syntax"). See section "4.3.1 Binding constructs
for syntactic keywords"...

-Rob

-----

Network Engineering             http://reality.sgi.com/rpw3/
Silicon Graphics, Inc.          Phone: 650-933-1673
1600 Amphitheatre Pkwy.         PP-ASEL-IA
Mountain View, CA  94043



Sat, 10 May 2003 09:40:10 GMT  
 
 [ 6 post ] 

 Relevant Pages 

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

2. Macro-expand time arithmetics with syntax-rules

3. Scheme->syntax-rules [Was: hygienic macro primality tester]

4. How to write seemingly unhygienic macros using syntax-rules

5. Macros defining macros with define-syntax

6. macro -vs- macro/codeblock

7. Help with macros writing macros in Bigloo

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

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