From: David Bakhash
Subject: Scheme-like macro syntax for CL
Date: 
Message-ID: <m3g0kp3rqs.fsf@cadet.dsl.speakeasy.net>
Hi,

I recently saw the Scheme macro syntax using DEFINE-SYNTAX and
SYNTAX-RULES.  I can't say I prefer this to CL's DEFMACRO right away,
and it seems like there are a bunch of things I can't figure out how
to do with DEFINE-SYNTAX.  But I'd like to hear from people who have
used Scheme's DEFINE-SYNTAX extensively.  How does it compare?  Does
it handle enough cases in the real world to make it useful (I have to
ask, since I've found Scheme to be a hindrance for real-world use).

I fired up umb-scheme and evaluated this definition for WHEN:

(define-syntax when
  (syntax-rules ()
   ((when <pred> <form> ...)
    ;; =>
    ((if <pred> (begin <form> ...))))))

Then I got this stuff:

--------------

==> (when #t (write "hello") (write "bye"))
GCing... 379872 bytes collected, 420104 bytes used, heapsize 800000 bytes.

Error: Bad function object:()

Reset (Use Control-d to quit UMB Scheme)"hello""bye"
==> (when #f (write "hello") (write "bye"))

Error: Bad function object:()

Reset (Use Control-d to quit UMB Scheme)
==>

--------------

So it kinda worked.

I'm really interested to know if such a pattern matching syntax exists 
for CL.  I am aware of Henry Baker's paper on Pragmatic Parsing in CL,
but that might be overkill.

Has DEFINE-SYNTAX been implemented for Common Lisp?  Does someone out
there have a DEFMACRO-like implementation that does a nice job with
pattern matching, making it easier to write macros?

dave

From: Eric Marsden
Subject: Re: Scheme-like macro syntax for CL
Date: 
Message-ID: <wzi7l60t1v2.fsf@mail.dotcom.fr>
>>>>> "db" == David Bakhash <·····@alum.mit.edu> writes:

  db> Has DEFINE-SYNTAX been implemented for Common Lisp? Does someone
  db> out there have a DEFMACRO-like implementation that does a nice
  db> job with pattern matching, making it easier to write macros?

see <URL:http://www.cs.rice.edu/~dorai/mbe/mbe-lsp.html> for a CL
implementation of R5RS macros (define-syntax, let-syntax and
letrec-syntax) by Dorai Sitaram.

-- 
Eric Marsden                          <URL:http://www.laas.fr/~emarsden/>
From: Lieven Marchand
Subject: Re: Scheme-like macro syntax for CL
Date: 
Message-ID: <m34s14i041.fsf@localhost.localdomain>
David Bakhash <·····@alum.mit.edu> writes:

> I recently saw the Scheme macro syntax using DEFINE-SYNTAX and
> SYNTAX-RULES.  I can't say I prefer this to CL's DEFMACRO right away,
> and it seems like there are a bunch of things I can't figure out how
> to do with DEFINE-SYNTAX.  But I'd like to hear from people who have
> used Scheme's DEFINE-SYNTAX extensively.  How does it compare?  Does
> it handle enough cases in the real world to make it useful (I have to
> ask, since I've found Scheme to be a hindrance for real-world use).
> 

I haven't used it extensively, but it seems fairly cumbersome to
me. Hygienic macros are one of these ideas that work far better in
theory than in practice. I don't think they buy much beyond DEFMACRO +
a WITH-GENSYMS macro.

If you want to see the idea even more worked out, check out the online
Dylan Reference Manual, where they have a fairly powerful syntax-rules
like macro facility for infix Dylan.

> I'm really interested to know if such a pattern matching syntax exists 
> for CL.  I am aware of Henry Baker's paper on Pragmatic Parsing in CL,
> but that might be overkill.
> 

The CMU archive has a whole directory with Miranda like matchers.

> Has DEFINE-SYNTAX been implemented for Common Lisp?  Does someone out
> there have a DEFMACRO-like implementation that does a nice job with
> pattern matching, making it easier to write macros?

Internally, most Scheme implementations have a DEFMACRO like facility
and SYNTAX-RULES is implemented with that. I believe the reference
implementation by Dybvig could be made to work trivially in CL.

-- 
Lieven Marchand <···@bewoner.dma.be>
Lambda calculus - Call us a mad club
From: Michael Livshin
Subject: Re: Scheme-like macro syntax for CL
Date: 
Message-ID: <s37l5zhpdy.fsf@bigfoot.com>
Lieven Marchand <···@bewoner.dma.be> writes:

> David Bakhash <·····@alum.mit.edu> writes:
> 
> > I recently saw the Scheme macro syntax using DEFINE-SYNTAX and
> > SYNTAX-RULES.  I can't say I prefer this to CL's DEFMACRO right away,
> > and it seems like there are a bunch of things I can't figure out how
> > to do with DEFINE-SYNTAX.  But I'd like to hear from people who have
> > used Scheme's DEFINE-SYNTAX extensively.  How does it compare?  Does
> > it handle enough cases in the real world to make it useful (I have to
> > ask, since I've found Scheme to be a hindrance for real-world use).
> > 
> 
> I haven't used it extensively, but it seems fairly cumbersome to
> me. Hygienic macros are one of these ideas that work far better in
> theory than in practice. I don't think they buy much beyond DEFMACRO +
> a WITH-GENSYMS macro.

they are not supposed to buy you things, they are supposed to shield
you from unintended name capturing (hence "hygienic").  in CL, the
reader does that for you, for the most part.

the `syntax-rules' thingie specified by the Scheme standard is indeed
kind of cumbersome to use.  Dybvig's `syntax-rules' facility is much
better (it's quite clever indeed, and lets you compose lists
programmatically almost like `defmacro' while maintaining the
hygiene), but it's not standard.

-- 
(only legal replies to this address are accepted)

... it's just that in C++ and the like, you don't trust _anybody_,
and in CLOS you basically trust everybody.  the practical result
is that thieves and bums use C++ and nice people use CLOS.
                -- Erik Naggum
From: Bruce Hoult
Subject: Re: Scheme-like macro syntax for CL
Date: 
Message-ID: <bruce-B2781B.11240720112000@news.nzl.ihugultra.co.nz>
In article <··············@localhost.localdomain>, Lieven Marchand 
<···@bewoner.dma.be> wrote:

> If you want to see the idea even more worked out, check out the online
> Dylan Reference Manual, where they have a fairly powerful syntax-rules
> like macro facility for infix Dylan.

Which can be found at:

   <http://www.gwydiondylan.org/drm/drm_76.htm>


There are some example macros in the Dylan Reference Manual (which 
doesn't refer to any particular implementation of the language) at...

   <http://www.gwydiondylan.org/drm/drm_86.htm>

... and much the same things (plus more) in the runtime library of an 
actual compiler at...

   
<http://www.ccc.de/cgi-bin/cvsweb/gd/src/d2c/runtime/dylan/macros.dylan?r
ev=1.9>


You can also find macros used to help define a state machine for a lexer 
at ...

   <http://www.ccc.de/cgi-bin/cvsweb/ICFP2000/src/lexer.dylan?rev=1.11>

-- Bruce
From: Rainer Joswig
Subject: Re: Scheme-like macro syntax for CL
Date: 
Message-ID: <joswig-00E870.03230820112000@news.is-europe.net>
In article <··············@cadet.dsl.speakeasy.net>, David Bakhash 
<·····@alum.mit.edu> wrote:

> Has DEFINE-SYNTAX been implemented for Common Lisp?  Does someone out
> there have a DEFMACRO-like implementation that does a nice job with
> pattern matching, making it easier to write macros?

DEFINE-SYNTAX is very limited. Its translation capabilities
are not powerful enough for what I usually use macros for.
It may be enough to write WHEN, UNLESS, etc.. But those
are trivial to write with DEFMACRO, too.

-- 
Rainer Joswig, Hamburg, Germany
Email: ·············@corporate-world.lisp.de
Web: http://corporate-world.lisp.de/
From: David Bakhash
Subject: Re: Scheme-like macro syntax for CL
Date: 
Message-ID: <m3r9473hpk.fsf@cadet.dsl.speakeasy.net>
Rainer Joswig <······@corporate-world.lisp.de> writes:

> In article <··············@cadet.dsl.speakeasy.net>, David Bakhash 
> <·····@alum.mit.edu> wrote:
> 
> > Has DEFINE-SYNTAX been implemented for Common Lisp?  Does someone out
> > there have a DEFMACRO-like implementation that does a nice job with
> > pattern matching, making it easier to write macros?
> 
> DEFINE-SYNTAX is very limited. Its translation capabilities
> are not powerful enough for what I usually use macros for.
> It may be enough to write WHEN, UNLESS, etc.. But those
> are trivial to write with DEFMACRO, too.

Yes.  As I'm now starting to see, DEFINE-SYNTAX is not nearly general
enough for what I use macros for.  I couldn't even write my
define-safe-package (similar to Tim's) with DEFINE-SYNTAX.  But
then again, maybe that's just because I'm not too clear on the full
usage of DEFINE-SYNTAX.  But without a doubt, if the "grammar" of the
SYNTAX-CASE clauses could be augmented with simple regular language
extensions, then maybe it could work.

I guess the bottom line is that we end up getting closer and closer to 
META, and that's not the direction I wanted to go, mostly because I
despise the input syntax, and how to extract information.  Basically,
what I want is just an extension of the normal destructuring that you
see in macro lambda lists (which is great for simple macros, but
lacking heavily for parsing issues).  For example, half of the
implementation of LOOP is parsing.  At the company I'm at now, I was
working with some guys on some neat macros (one in particular), and
though I've had to write much worse ones, and they're fun to write,
they're not "productive" fun; they're fun because they're a
challenge.  I'd much rather work on other stuff.

I think that if I could deal with a Scheme programmer directly, they
could tell me how to do some of the simpler things with DEFINE-SYNTAX,
in which case I might let go of DEFMACRO, just for readability when
others come on the scene.  for example, I still don't know how to do
side-effects, especially based on the input data.  I'm guessing that
there could be a way to give DEFINE-SYNTAX a lambda list in addition
to it having its syntax transformation clauses, so that one could get
the best of both worlds.

I really like Dorai's implementation of DEFINE-SYNTAX in Common Lisp,
and recommend that people check it out if they havn't seen this syntax 
before.

dave
From: David Bakhash
Subject: Re: Scheme-like macro syntax for CL
Date: 
Message-ID: <m3vgtj3i1z.fsf@cadet.dsl.speakeasy.net>
Rainer Joswig <······@corporate-world.lisp.de> writes:

> In article <··············@cadet.dsl.speakeasy.net>, David Bakhash 
> <·····@alum.mit.edu> wrote:
> 
> > Has DEFINE-SYNTAX been implemented for Common Lisp?  Does someone out
> > there have a DEFMACRO-like implementation that does a nice job with
> > pattern matching, making it easier to write macros?
> 
> DEFINE-SYNTAX is very limited. Its translation capabilities
> are not powerful enough for what I usually use macros for.
> It may be enough to write WHEN, UNLESS, etc.. But those
> are trivial to write with DEFMACRO, too.

Yes.  As I'm now starting to see, DEFINE-SYNTAX is not nearly general
enough for what I use macros for.  I couldn't even write my
define-safe-package (similar to yours, Tim) with DEFINE-SYNTAX.  But
then again, maybe that's just because I'm not too clear on the full
usage of DEFINE-SYNTAX.  But without a doubt, if the "grammar" of the
SYNTAX-CASE clauses could be augmented with simple regular language
extensions, then maybe it could work.

I guess the bottom line is that we end up getting closer and closer to 
META, and that's not the direction I wanted to go, mostly because I
despise the input syntax, and how to extract information.  Basically,
what I want is just an extension of the normal destructuring that you
see in macro lambda lists (which is great for simple macros, but
lacking heavily for parsing issues).  For example, half of the
implementation of LOOP is parsing.  At the company I'm at now, I was
working with some guys on some neat macros (one in particular), and
though I've had to write much worse ones, and they're fun to write,
they're not "productive" fun; they're fun because they're a
challenge.  I'd much rather work on other stuff.

I think that if I could deal with a Scheme programmer directly, they
could tell me how to do some of the simpler things with DEFINE-SYNTAX,
in which case I might let go of DEFMACRO, just for readability when
others come on the scene.  for example, I still don't know how to do
side-effects, especially based on the input data.  I'm guessing that
there could be a way to give DEFINE-SYNTAX a lambda list in addition
to it having its syntax transformation clauses, so that one could get
the best of both worlds.

I really like Dorai's implementation of DEFINE-SYNTAX in Common Lisp,
and recommend that people check it out if they havn't seen this syntax 
before.

dave