define-syntax must be followed by syntax-rules ?? 
Author Message
 define-syntax must be followed by syntax-rules ??

Lately I've run across some code which looks like :

(define-syntax rx/cs (lambda (exp r c) (expand-rx exp #t r c)))

Is this legal ?  I have found some other examples in which
define-syntax is not used with syntax-rules.  A reading through 5r5s
leaves me confused as it indicates that define-syntax is used in
conjunction with syntax-rules.

BTW, the following line comes from Olin's Regex code and was commented
out, i.e. not used so I don't know if it is legal.  I have found some
other examples though, like:

   (define-syntax defmacro
    (lambda (e r c)
      `(define-syntax ,(cadr e)
         (lambda (em rm cm)
           (set-car!
             gentemp-cell
             (let ((counter -1))
               (lambda ()
                 (set! counter (+ counter 1))
                 (rm (string->symbol
                       (string-append "g" (number->string counter)))))))
           (let ((,(caddr e) (cdr em))) ,(cadddr e))))))

Which comes from Andrew Wright's pattern matching code and is specific
to scheme-48.

Would somebody kindly give me a clue ?

Just when I thought I was getting the hang of r5rs macros...

Brian



Fri, 21 Jun 2002 03:00:00 GMT  
 define-syntax must be followed by syntax-rules ??

Brian> Lately I've run across some code which looks like :

Brian> (define-syntax rx/cs (lambda (exp r c) (expand-rx exp #t r c)))

Brian> Is this legal ?  I have found some other examples in which
Brian> define-syntax is not used with syntax-rules.  A reading through 5r5s
Brian> leaves me confused as it indicates that define-syntax is used in
Brian> conjunction with syntax-rules.

You're right.  rx/cs as above uses Scheme 48's non-hygienic,
non-standard "internal" macro system.

You'll find a decent description of that system in

/ftp.cs.indiana.edu:/pub/scheme-repository/doc/prop/exrename.ps.gz

--
Cheers =8-} Mike
Friede, V?lkerverst?ndigung und berhaupt blabla



Sat, 22 Jun 2002 03:00:00 GMT  
 define-syntax must be followed by syntax-rules ??
Brian-

This isn't R5RS macros. It's an alternate, low-level macro system, called
Clinger/Rees explicit-renaming macros. It allows you to violate or preserve
hygiene, do arbitrary computation on the syntax tree in full Scheme, etc.

R5RS high-level macros are more limiting, though I use them for many
things. Clinger/Rees is strictly more powerful -- you can do anything
with them that you can do with R5RS macros, including write hygenic
macros.

A lot of the macros I write implement "little languages" -- they are actually
little compilers that translate syntax trees representing expressions in the
little language (regexps, or Awk, or process forms) into Scheme code. Some of
these compilers are pretty involved -- the regexp system is a couple of
thousand lines of code, all totalled up, and it looks a lot like a compiler:
parsing, analysis, optimisation, and code generation phases. I would not want
to (nor could I) write such a thing in the limited pattern notation of R5RS
macros; I had to do it using a lower-level system that gave me a general
language to implement my little compiler: Scheme.

Simpler macros I do in R5RS notation, because it's more portable and higher
level, hence easier.
    -Olin


    Newsgroups: comp.lang.scheme
    Subject: define-syntax must be followed by syntax-rules ??
    Date: 03 Jan 2000 21:01:48 -0800

    Lately I've run across some code which looks like :

    (define-syntax rx/cs (lambda (exp r c) (expand-rx exp #t r c)))

    Is this legal ?  I have found some other examples in which
    define-syntax is not used with syntax-rules.  A reading through 5r5s
    leaves me confused as it indicates that define-syntax is used in
    conjunction with syntax-rules.

    Just when I thought I was getting the hang of r5rs macros...



Sat, 22 Jun 2002 03:00:00 GMT  
 define-syntax must be followed by syntax-rules ??

Quote:

> R5RS high-level macros are more limiting, though I use them for many
> things. Clinger/Rees is strictly more powerful -- you can do anything
> with them that you can do with R5RS macros, including write hygenic
> macros.

Do you have pointers to papers/implementation specs on them?

Quote:
> A lot of the macros I write implement "little languages" -- they are actually
> little compilers that translate syntax trees representing expressions in the
> little language (regexps, or Awk, or process forms) into Scheme
> code. I would not want to (nor could I) write such a thing in the
> limited pattern notation of R5RS macros; I had to do it using a
> lower-level system that gave me a general language to implement my
> little compiler: Scheme.

Ok, then why did the R5RS editors choose what is (apparently) a broken
and (IMNSHO) obscure macro specification over either a more powerful
or a less obscure system? I thought that writing little compilers for
new features was the point of macros in the Lisp family of languages?

And I say R5RS macros are obscure because compared to define-macro, I
still haven't been able to figure out how to properly specify my
macro-forms.

BTW, This is actually about the only thing I don't like in the Scheme
world.

Quote:
> Simpler macros I do in R5RS notation, because it's more portable and higher
> level, hence easier.

]]]Choke[[[ Easier? I must really be missing soemthing here. Is there
a gentle introduction to R5RS macros somewhere on the net that does
something more interesting than show you how to define let*?

david rush
--

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



Mon, 24 Jun 2002 03:00:00 GMT  
 define-syntax must be followed by syntax-rules ??

Quote:

> Ok, then why did the R5RS editors choose what is (apparently) a broken
> and (IMNSHO) obscure macro specification over either a more powerful
> or a less obscure system?

Compromise.  Anyway, if you're looking for a macro system that offers
the best of both worlds, check out syntax-case from Indiana.  Since
someone will ask: yes, there are Web pointers.  Go to Kent Dybvig's
home page and track down the papers on the syntax-case specification
and writing macros in it (IU CS TR's 355 and 356, as I recall).

Quote:
> ]]]Choke[[[ Easier? I must really be missing soemthing here. Is there
> a gentle introduction to R5RS macros somewhere on the net that does
> something more interesting than show you how to define let*?

Look at Dybvig et al's papers, particularly the TR on writing macros
in syntax-case.  If you have access to a decent library (I assume not,
given that you repeatedly ask for Web pointers), you could also track
down the original macro-by-example paper by Kohlbecker and Wand.

'shriram

PS: Dybvig et al = Dybvig, Hieb and Bruggeman.



Mon, 24 Jun 2002 03:00:00 GMT  
 define-syntax must be followed by syntax-rules ??


Quote:
>Ok, then why did the R5RS editors choose what is (apparently) a broken
>and (IMNSHO) obscure macro specification over either a more powerful
>or a less obscure system? I thought that writing little compilers for
>new features was the point of macros in the Lisp family of languages?

A Common Lisp-like macro system is considered *too* powerful by many Scheme
proponents.  It would allow you to violate lexical scoping, which they
consider sacred.

The constraints of syntax-rules reduce its flexibility, but also make
everything lexically apparent.  It's good enough for most macros that
people need to write, although not the ones you're trying to create.

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.



Mon, 24 Jun 2002 03:00:00 GMT  
 define-syntax must be followed by syntax-rules ??

  David> And I say R5RS macros are obscure because compared to define-macro, I
  David> still haven't been able to figure out how to properly specify my
  David> macro-forms.

Really ?  I find R5RS macros easier to understand and use than
define-macro and its ilk. Go figure.

Methinks that in addition to the other advice you receive, you should
spend time understanding the pattern matching facility used in the
macro system whic is really the key.  Then I would peruse r5rs which
has very nice examples by way of the definition of cond, case, etc...

Brian



Tue, 25 Jun 2002 03:00:00 GMT  
 define-syntax must be followed by syntax-rules ??

Quote:

> Ok, then why did the R5RS editors choose what is (apparently) a broken
> and (IMNSHO) obscure macro specification over either a more powerful
> or a less obscure system? I thought that writing little compilers for
> new features was the point of macros in the Lisp family of languages?

The more powerful macro systems typically require the compiler to
evaluate user-supplied Scheme code during compilation, thus
intermixing compilation and evaluation. Moreover, the evaluated code
modifies syntax trees, which are objects internal to the compiler. If
the compiler wants to represent the syntax tree in a non-canonical way
(i.e. not as the datums returned by the standard read), either the
macro expansion becomes harder to understand to the user (examine
compiler's syntax tree representation instead of the familar scheme
data) or harder to implement for the compiler (map syntax trees back
to scheme data, probably losing information, and then back).

Thus, macro systems that permit evaluation have two drawbacks:

1. The compiler must provide also a run-time system

2. Internal compiler objects must be made visible to the user

I approve full-heartedly the sightful decision to define a
macro-system that does not require any of these as the standard. I
really like R5RS macros. (I assume them to be Turing-complete, BTW).

--

              * The {*filter*} of Christ cleanses from all sin. *



Tue, 25 Jun 2002 03:00:00 GMT  
 define-syntax must be followed by syntax-rules ??

Quote:


>   David> And I say R5RS macros are obscure because compared to define-macro, I
>   David> still haven't been able to figure out how to properly specify my
>   David> macro-forms.

> Really ?  I find R5RS macros easier to understand and use than
> define-macro and its ilk. Go figure.

Chalk it up to my years of C-encrusted thinking, but I find that
explicitly manipulating linked structures is almost second-nature
now. I'm still working to figure when it is best to use explicit
loops vs map/fold/unfold strategies, especially when I don't think
that there are too many Schemes that actually *do* build/cata fusion
optimizations.

Quote:
> Methinks that in addition to the other advice you receive, you should
> spend time understanding the pattern matching facility used in the
> macro system whic is really the key.  

I understand it when I read the R5RS description of it, but when it
comes to writing them I lose my mind. Reminds me of Fluid Mechanics
and Advanced Calculus, lo these many years ago...

Quote:
> Then I would peruse r5rs which
> has very nice examples by way of the definition of cond, case, etc...

Oops. I have always pretty much blipped over the Formal syntax and
semantics section. Looky there - definitions of all the
library-syntax! And I was going nuts trying to find a tutorial. Never
mind.

david rush
--
My name is Roseanne Rosannadanna...



Tue, 25 Jun 2002 03:00:00 GMT  
 define-syntax must be followed by syntax-rules ??
+---------------
| A Common Lisp-like macro system is considered *too* powerful
| by many Scheme proponents.
+---------------

Though certainly not all. Myself, the very first thing I do when I
come across a new Scheme implementation is find out what sort of
low-level macro facility there is, then define CL-style "defmacro"
in it (if it's not already there), and then forget the idiosyncratic
system and just do everything with "defmacro"... but YMMV.

[By the way, one can have it both ways: There's an implementation
of Scheme's hygenic macros in terms of "defmacro" in SLIB...]

+---------------
| It would allow you to violate lexical scoping, which they
| consider sacred.
+---------------

Unless you *like* anaphoric macros...  ;-}

-Rob

-----

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



Fri, 28 Jun 2002 03:00:00 GMT  
 define-syntax must be followed by syntax-rules ??

Quote:

> (I assume them to be Turing-complete, BTW).

Can anyone prove/disprove/show plausible this assertion? They don't
look that way to me, but I am not the most clueful of people wrt R5RS
macros.

david rush
--
From the start...the flute has been associated with pure (some might
say impure) energy. Its sound releases something naturally untamed, as
if a squirrel were let loose in a church." --Seamus Heaney



Fri, 28 Jun 2002 03:00:00 GMT  
 define-syntax must be followed by syntax-rules ??

Quote:


> > (I assume them to be Turing-complete, BTW).

> Can anyone prove/disprove/show plausible this assertion? They don't
> look that way to me, but I am not the most clueful of people wrt R5RS
> macros.

Well, Turing-complete at least so that deciding whether macro
expansion terminates in undecidable. This is how you emulate Turing
machine using the macros: (I haven't tested this, though, so don't
blame me if this doesn't run)

Let's name the states of the turing machines by numbers from 1 to N.
Let similarly the alphabet of the machine be denoted by integers from
0 to N. 0 denotes `space', i.e. the initial contents of the tape.

We represent the particular state of the Turing machine by macro instance
that looks like

  (Turing <current state>
          <symbol at head>
          <tape before the head in reverse order (list)>
          <tape after the head (list)>)

For example

  (Turing 3 5 (3 2 1) (9 8))

denotes the following configuration

1 2 3 5 9 8
      ^
     Head, state = 3

We then write

(define-syntax Turing
  (syntax-rules ()
    ...))

where at ... for every combination of <current state> and <symbol at head>
(finite number of these) we write one of the following bundles.
Let S be the state number and A the symbol.

Change symbol to B and change to state T:

((Turing S A ?before ?after) (Turing T B ?before ?after))

Go forward and change to state T:

;; expand tape if necessary
((Turing S A ?before ()) (Turing T 0 (A . ?before) ()))

;; otherwise just go forward
((Turing S A ?before (?first . ?rest))
 (Turing T ?first (A . ?before) ?rest))

Go backward and change to state T:

;; halt if at start
((Turing S A () ?rest) #f)

;; otherwise really move
((Turing S A (?first . ?rest) ?after)
 (Turing T ?first ?rest (A . ?after)))

Defined this way, the macro expansion ends to #f only when the Turing
machine passes the left end of the semi-infinite tape. Of course, any
state can be made a `halting' state by defining

((Turing S ?symbol ?before ?after) #f)

Thus, the macros are Turing complete.

--

              * The {*filter*} of Christ cleanses from all sin. *



Sat, 29 Jun 2002 03:00:00 GMT  
 define-syntax must be followed by syntax-rules ??


Quote:

> Brian> Lately I've run across some code which looks like :

> Brian> (define-syntax rx/cs (lambda (exp r c) (expand-rx exp #t r c)))

> Brian> Is this legal ?  I have found some other examples in which
> Brian> define-syntax is not used with syntax-rules.  A reading through 5r5s
> Brian> leaves me confused as it indicates that define-syntax is used in
> Brian> conjunction with syntax-rules.

> You're right.  rx/cs as above uses Scheme 48's non-hygienic,
> non-standard "internal" macro system.

> You'll find a decent description of that system in

> /ftp.cs.indiana.edu:/pub/scheme-repository/doc/prop/exrename.ps.gz

Twobit uses this same kind of low-level macro system, although Twobit
requires a keyword TRANSFORMER to introduce the procedure that performs
the macro expansion.  I didn't implement Twobit's macro system, but I
guess that the extra keyword is there to allow future expansion to
_other_ types of expanders.

For example, here's my macro for DEFMACRO (also created to allow me to
use Wright's MATCH in Larceny, as it happens):

(define-syntax defmacro
  (transformer
    (lambda (exp rename compare)
      (define (arglist? x)
        (or (symbol? x)
            (null? x)
            (and (pair? x)
                 (symbol? (car x))
                 (arglist? (cdr x)))))
      (if (not (and (list? exp)
                    (>= (length exp) 4)
                    (symbol? (cadr exp))
                    (arglist? (caddr exp))))
          (error "Bad macro definition: " exp))
      (let ((name (cadr exp))
            (args (caddr exp))
            (body (cdddr exp)))
        `(begin
           (define-syntax
             ,name
             (transformer
              (lambda (_defmacro_exp
                       _defmacro_rename
                       _defmacro_compare)

           ; This bit allows us to implement macro? and macroexpand-1.
           (set! *macros*
                 (cons (cons ',name
                             (lambda (_exp)

                       *macros*))
           )))))

--lars



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

 Relevant Pages 

1. Implementing letrec-syntax using only let-syntax and syntax-rules

2. is define-syntax inside define-syntax OK?

3. Syntax-case vs. syntax-rules

4. unusual define-syntax syntax

5. shadows of define and define-syntax

6. define in define-syntax

7. define-macro -> define-syntax

8. define-macro vs define-syntax

9. Macros defining macros with define-syntax

10. i am new to this how do you define a rule in a inference engine

11. C++ templates vs. syntax-rules: a median challenge

12. syntax rules

 

 
Powered by phpBB® Forum Software