unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* Patch to add (define-syntax (foo bar) ...) support
@ 2011-07-03 20:19 Chris K. Jester-Young
  2011-07-03 20:44 ` Noah Lavine
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Chris K. Jester-Young @ 2011-07-03 20:19 UTC (permalink / raw)
  To: guile-devel

[-- Attachment #1: Type: text/plain, Size: 655 bytes --]

Hi there,

When writing syntax-case macros, often one would write:

    (define-syntax foo
      (lambda (bar)
        (syntax-case bar ...)))

This seems overly long-winded; it would be preferable to be able to
write, instead:

    (define-syntax (foo bar)
      (syntax-case bar ...))

Attached is a patch that implements that. Note that there is nothing
original in this patch---it's just a straight copy-and-paste of the
"define" version immediately above, except changing define-form to
define-syntax-form---so there should be nothing controversial from a
correctness and/or copyright point of view.

Let me know what you think.

Many thanks,
Chris.

[-- Attachment #2: psyntax.scm.diff --]
[-- Type: text/x-diff, Size: 1012 bytes --]

diff --git a/module/ice-9/psyntax.scm b/module/ice-9/psyntax.scm
index 957a526..e83b3ff 100644
--- a/module/ice-9/psyntax.scm
+++ b/module/ice-9/psyntax.scm
@@ -1168,7 +1168,16 @@
                      ((_ name val)
                       (id? #'name)
                       (values 'define-syntax-form #'name
-                              #'val w s mod))))
+                              #'val w s mod))
+                     ((_ (name . args) e1 e2 ...)
+                      (and (id? #'name)
+                           (valid-bound-ids? (lambda-var-list #'args)))
+                      ;; need lambda here...
+                      (values 'define-syntax-form (wrap #'name w mod)
+                              (decorate-source
+                               (cons #'lambda (wrap #'(args e1 e2 ...) w mod))
+                               s)
+                              empty-wrap s mod))))
                   (else
                    (values 'call #f e w s mod)))))))
          ((syntax-object? e)

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: Patch to add (define-syntax (foo bar) ...) support
  2011-07-03 20:19 Patch to add (define-syntax (foo bar) ...) support Chris K. Jester-Young
@ 2011-07-03 20:44 ` Noah Lavine
  2011-07-03 21:03   ` Chris K. Jester-Young
  2011-07-03 22:34 ` Ian Price
  2011-09-02  8:32 ` Andy Wingo
  2 siblings, 1 reply; 9+ messages in thread
From: Noah Lavine @ 2011-07-03 20:44 UTC (permalink / raw)
  To: guile-devel

Hello,

I agree that this is much shorter, but I'm worried about defining the
short syntax in a way that forces you to choose between syntax-rules
and syntax-case. What I mean is that you could just as easily have

(define-syntax (foo bar)
  ...)

expand to

(define-syntax foo
  (syntax-rules ()
    ((_ bar) ...)))

It seems to me that this makes a somewhat arbitrary choice, which
isn't great. I'd rather see some way to unify the two possibilities,
but I don't know what that would be. There's also the possibility of
making it expand to

(define-syntax foo
  (syntax-case tmp ...
    ((bar) ...)))

because it is more analogous to how regular procedures work.

I don't know what the right choice is, but it's a good point that
there probably should be something.

Noah

On Sun, Jul 3, 2011 at 4:19 PM, Chris K. Jester-Young <cky944@gmail.com> wrote:
> Hi there,
>
> When writing syntax-case macros, often one would write:
>
>    (define-syntax foo
>      (lambda (bar)
>        (syntax-case bar ...)))
>
> This seems overly long-winded; it would be preferable to be able to
> write, instead:
>
>    (define-syntax (foo bar)
>      (syntax-case bar ...))
>
> Attached is a patch that implements that. Note that there is nothing
> original in this patch---it's just a straight copy-and-paste of the
> "define" version immediately above, except changing define-form to
> define-syntax-form---so there should be nothing controversial from a
> correctness and/or copyright point of view.
>
> Let me know what you think.
>
> Many thanks,
> Chris.
>



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Patch to add (define-syntax (foo bar) ...) support
  2011-07-03 20:44 ` Noah Lavine
@ 2011-07-03 21:03   ` Chris K. Jester-Young
  2011-07-03 21:08     ` Noah Lavine
  0 siblings, 1 reply; 9+ messages in thread
From: Chris K. Jester-Young @ 2011-07-03 21:03 UTC (permalink / raw)
  To: guile-devel

On Sun, Jul 03, 2011 at 04:44:46PM -0400, Noah Lavine wrote:
> I agree that this is much shorter, but I'm worried about defining the
> short syntax in a way that forces you to choose between syntax-rules
> and syntax-case.

Except, it doesn't. My version doesn't insert either syntax-case or
syntax-rules; it just inserts the lambda and lets you do whatever.

Granted, in practice that makes the shortcut useful for syntax-case
only, but at least it's highly consistent with how define's shortcut
works, and should therefore be less confusing.

>                  What I mean is that you could just as easily have
> 
> (define-syntax (foo bar)
>   ...)
> 
> expand to
> 
> (define-syntax foo
>   (syntax-rules ()
>     ((_ bar) ...)))

Racket resolves this by having a macro called define-syntax-rule, which
allows you to define a one-branch syntax-rules macro. Thus these two
macros are identical:

    (define-syntax-rule (foo bar baz) (...))

    (define-syntax foo
      (syntax-rules ()
        ((_ bar baz) (...))))

> It seems to me that this makes a somewhat arbitrary choice, which
> isn't great. I'd rather see some way to unify the two possibilities,
> but I don't know what that would be. There's also the possibility of
> making it expand to
> 
> (define-syntax foo
>   (syntax-case tmp ...
>     ((bar) ...)))
> 
> because it is more analogous to how regular procedures work.

The problem with this is that this then favours the one-branch variant,
which is not a common case for syntax-case macros.

Cheers,
Chris.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Patch to add (define-syntax (foo bar) ...) support
  2011-07-03 21:03   ` Chris K. Jester-Young
@ 2011-07-03 21:08     ` Noah Lavine
  0 siblings, 0 replies; 9+ messages in thread
From: Noah Lavine @ 2011-07-03 21:08 UTC (permalink / raw)
  To: guile-devel

> Except, it doesn't. My version doesn't insert either syntax-case or
> syntax-rules; it just inserts the lambda and lets you do whatever.

Oh, I must have been temporarily insane. My apologies. :-)

Your idea makes a lot of sense.

Noah



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Patch to add (define-syntax (foo bar) ...) support
  2011-07-03 20:19 Patch to add (define-syntax (foo bar) ...) support Chris K. Jester-Young
  2011-07-03 20:44 ` Noah Lavine
@ 2011-07-03 22:34 ` Ian Price
  2011-09-02  8:32 ` Andy Wingo
  2 siblings, 0 replies; 9+ messages in thread
From: Ian Price @ 2011-07-03 22:34 UTC (permalink / raw)
  To: guile-devel


Looks fine to me. When I started writing portable R6RS code instead of
PLT Scheme specific code, this was one thing I missed quite often. Not
because it couldn't be done, but because I wanted to avoid having to do
(import (except (rnrs) define-syntax) 
        (utils))
every single time I wanted to write a macro :-)

-- 
Ian Price

"There are only two hard problems in Computer Science: cache invalidation
and naming things." - Phil Karlton



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Patch to add (define-syntax (foo bar) ...) support
  2011-07-03 20:19 Patch to add (define-syntax (foo bar) ...) support Chris K. Jester-Young
  2011-07-03 20:44 ` Noah Lavine
  2011-07-03 22:34 ` Ian Price
@ 2011-09-02  8:32 ` Andy Wingo
  2011-09-02 13:33   ` Ian Price
  2 siblings, 1 reply; 9+ messages in thread
From: Andy Wingo @ 2011-09-02  8:32 UTC (permalink / raw)
  To: guile-devel

Hi!

On Sun 03 Jul 2011 22:19, "Chris K. Jester-Young" <cky944@gmail.com> writes:

> When writing syntax-case macros, often one would write:
>
>     (define-syntax foo
>       (lambda (bar)
>         (syntax-case bar ...)))
>
> This seems overly long-winded; it would be preferable to be able to
> write, instead:
>
>     (define-syntax (foo bar)
>       (syntax-case bar ...))

I am not sure what I think about this.  On the one hand it elides things
that you type all the time.  On the other hand sometimes I think it
obscures the fact that there is a lambda there; I'm used to having

  (define (foo bar) ...)

mentally indicate "procedure", but

  (define-syntax (foo bar) ...)

doesn't mentally indicate that for me, in part because it's explicitly
in the domain of macros, where we have to think about what we're
parsing.  For example, we are trained now to consider syntax-rules to be
a language that looks like scheme but it not scheme.  Without the
lambda, would people have trouble identifying a procedural macro as
being Scheme?

On the whole I am skeptical.  But, Chez Scheme and Racket both made this
change.  So I could go either way, though I would like thoughts from
other people before proceeding.

On the other hand, one thing that is clearly useful is
`define-syntax-rule'.  I'll add that now.

Andy

Andy
-- 
http://wingolog.org/



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Patch to add (define-syntax (foo bar) ...) support
  2011-09-02  8:32 ` Andy Wingo
@ 2011-09-02 13:33   ` Ian Price
  2011-09-02 16:53     ` Andy Wingo
  2011-09-05  7:58     ` Marijn
  0 siblings, 2 replies; 9+ messages in thread
From: Ian Price @ 2011-09-02 13:33 UTC (permalink / raw)
  To: Andy Wingo; +Cc: guile-devel

Andy Wingo <wingo@pobox.com> writes:

> On the whole I am skeptical.  But, Chez Scheme and Racket both made this
> change.  So I could go either way, though I would like thoughts from
> other people before proceeding.

While I like the change, another solution is to try to make a
'syntax-case' form that returns a lambda like syntax-rules.

Either,

(define-syntax foo
  (syntax-case* ()
    ((foo ...) ...)
    ...))

or if we need access to the syntax object for e.g. unhygienic macros

(define-syntax foo
  (syntax-case* stx ()
    ((foo ...) ...)
    ...))

I haven't used this in practice, but it would cut down on some
consistent verbosity. A rough implementation could be

(define-syntax syntax-case*
  (lambda (stx)  ; goodbye forever old friend ;-)
    (syntax-case stx ()
      ((syntax-case* syntax-object literals rule rules ...)
       (identifier? #'syntax-object)
       #'(lambda (syntax-object)
           (syntax-case syntax-object literals
             rule
             rules ...)))
      ((syntax-case* literals rule rules ...)
       #'(lambda (syntax-object)
           (syntax-case syntax-object literals
             rule
             rules ...))))))

It wouldn't match up with the meaning of syntax-case* in racket, but if
that's important to anyone, feel free to come up with another name :-)

-- 
Ian Price

"Programming is like pinball. The reward for doing it well is
the opportunity to do it again" - from "The Wizardy Compiled"



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Patch to add (define-syntax (foo bar) ...) support
  2011-09-02 13:33   ` Ian Price
@ 2011-09-02 16:53     ` Andy Wingo
  2011-09-05  7:58     ` Marijn
  1 sibling, 0 replies; 9+ messages in thread
From: Andy Wingo @ 2011-09-02 16:53 UTC (permalink / raw)
  To: Ian Price; +Cc: guile-devel

On Fri 02 Sep 2011 15:33, Ian Price <ianprice90@googlemail.com> writes:

> Andy Wingo <wingo@pobox.com> writes:
>
>> On the whole I am skeptical.  But, Chez Scheme and Racket both made this
>> change.  So I could go either way, though I would like thoughts from
>> other people before proceeding.
>
> While I like the change, another solution is to try to make a
> 'syntax-case' form that returns a lambda like syntax-rules.

I do not like this, though feel free to write it for yourself ;-)

Andy
-- 
http://wingolog.org/



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Patch to add (define-syntax (foo bar) ...) support
  2011-09-02 13:33   ` Ian Price
  2011-09-02 16:53     ` Andy Wingo
@ 2011-09-05  7:58     ` Marijn
  1 sibling, 0 replies; 9+ messages in thread
From: Marijn @ 2011-09-05  7:58 UTC (permalink / raw)
  To: Ian Price; +Cc: Andy Wingo, guile-devel

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 09/02/11 15:33, Ian Price wrote:
> Andy Wingo <wingo@pobox.com> writes:
> 
>> On the whole I am skeptical.  But, Chez Scheme and Racket both
>> made this change.  So I could go either way, though I would like
>> thoughts from other people before proceeding.
> 
> While I like the change, another solution is to try to make a 
> 'syntax-case' form that returns a lambda like syntax-rules.
> 
> Either,
> 
> (define-syntax foo (syntax-case* () ((foo ...) ...) ...))
> 
> or if we need access to the syntax object for e.g. unhygienic
> macros
> 
> (define-syntax foo (syntax-case* stx () ((foo ...) ...) ...))

If you reduce the requirement of being able to elide `stx' when it's
not used, then it becomes the really simple:

(define-syntax-rule (syntax-case/fn stx . args)
  (lambda (stx)
    (syntax-case stx . args)))

Personally I think there should also be a macro that let's one write:

(define-syntax-rules syntax-case/fn
  ((_ stx . args)
   (lambda (stx)
     (syntax-case stx . args)) ))

but which would also be able to handle the case of multiple rules.

> I haven't used this in practice, but it would cut down on some 
> consistent verbosity. A rough implementation could be
> 
> (define-syntax syntax-case* (lambda (stx)  ; goodbye forever old
> friend ;-) (syntax-case stx () ((syntax-case* syntax-object
> literals rule rules ...) (identifier? #'syntax-object) #'(lambda
> (syntax-object) (syntax-case syntax-object literals rule rules
> ...))) ((syntax-case* literals rule rules ...) #'(lambda
> (syntax-object) (syntax-case syntax-object literals rule rules
> ...))))))

The following macro seems equivalent:

(define-syntax syntax-case/fn
  (syntax-rules ()
    ((_ (lits ...) . rules)
     (lambda (stx)
       (syntax-case stx (lits ...) . rules)))
    ((_ stx (lits ...) . rules)
     (lambda (stx)
       (syntax-case stx (lits ...) . rules)))))

or using the above ``defined'' define-syntax-rules:

(define-syntax-rules syntax-case/fn
  ((_ (lits ...) . rules)
   (lambda (stx)
     (syntax-case stx (lits ...) . rules)))
  ((_ stx (lits ...) . rules)
   (lambda (stx)
     (syntax-case stx (lits ...) . rules)))))

> It wouldn't match up with the meaning of syntax-case* in racket,
> but if that's important to anyone, feel free to come up with
> another name :-)

Some alternative names might be syntax-case/fn, syntax-case/l,
syntax-case/lambda (but kinda defeats the purpose of not having to
type lambda).

Anyway, less typing for macro boilerplate is something I appreciate.

Marijn

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.18 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk5kgT8ACgkQp/VmCx0OL2zyugCguOpctFfLTV+x45cZ0IXrkv6u
ejMAn1ECvJyPbQHwSe1TT2Kd0h2sp8oX
=wv9z
-----END PGP SIGNATURE-----



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2011-09-05  7:58 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-07-03 20:19 Patch to add (define-syntax (foo bar) ...) support Chris K. Jester-Young
2011-07-03 20:44 ` Noah Lavine
2011-07-03 21:03   ` Chris K. Jester-Young
2011-07-03 21:08     ` Noah Lavine
2011-07-03 22:34 ` Ian Price
2011-09-02  8:32 ` Andy Wingo
2011-09-02 13:33   ` Ian Price
2011-09-02 16:53     ` Andy Wingo
2011-09-05  7:58     ` Marijn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).