unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* [PATCH] Preserve keyword in 'syntax-rules' and 'define-syntax-rule'
@ 2012-10-08 18:20 Mark H Weaver
  2012-10-09 21:34 ` Ludovic Courtès
  2012-10-10 17:41 ` Mark H Weaver
  0 siblings, 2 replies; 7+ messages in thread
From: Mark H Weaver @ 2012-10-08 18:20 UTC (permalink / raw)
  To: guile-devel

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

Hello all,

I'd like to be able to do things like this:

--8<---------------cut here---------------start------------->8---
(define-syntax alt-environment
  (syntax-rules ()
    ((alt-environment)
     (the-environment alt-environment))))
--8<---------------cut here---------------end--------------->8---

and have it actually capture the lexical environment of the macro use.
Unfortunately, this doesn't work because the definitions of
'syntax-rules' and 'define-syntax-rule' discard the identifier of the
keyword.

Another possible use of the keyword identifier would be to pass it to
'datum->syntax' or 'syntax-locally-bound-identifiers'.

This patch fixes these problems.  Comments and suggestions solicited.

    Thanks,
      Mark


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: [PATCH] Preserve keyword identifier in 'syntax-rules' and 'define-syntax-rule' --]
[-- Type: text/x-diff, Size: 5589 bytes --]

From 3e3d32dd9b2d71ffb0703dedc4d47387e981c9b5 Mon Sep 17 00:00:00 2001
From: Mark H Weaver <mhw@netris.org>
Date: Mon, 8 Oct 2012 14:08:43 -0400
Subject: [PATCH] Preserve keyword identifier in 'syntax-rules' and
 'define-syntax-rule'

* module/ice-9/psyntax-pp.scm (syntax-rule, define-syntax-rule):
  Preserve the keyword identifier.

* module/ice-9/psyntax-pp.scm: Regenerate.
---
 module/ice-9/psyntax-pp.scm |   24 ++++++++++++------------
 module/ice-9/psyntax.scm    |    8 ++++----
 2 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/module/ice-9/psyntax-pp.scm b/module/ice-9/psyntax-pp.scm
index 68d1bf6..b148c9a 100644
--- a/module/ice-9/psyntax-pp.scm
+++ b/module/ice-9/psyntax-pp.scm
@@ -2551,12 +2551,13 @@
                            (cons '#(syntax-object syntax-case ((top)) (hygiene guile))
                                  (cons '#(syntax-object x ((top)) (hygiene guile))
                                        (cons k
-                                             (map (lambda (tmp-1 tmp)
-                                                    (list (cons '#(syntax-object dummy ((top)) (hygiene guile)) tmp)
+                                             (map (lambda (tmp-2 tmp-1 tmp)
+                                                    (list (cons tmp tmp-1)
                                                           (list '#(syntax-object syntax ((top)) (hygiene guile))
-                                                                tmp-1)))
+                                                                tmp-2)))
                                                   template
-                                                  pattern))))))
+                                                  pattern
+                                                  keyword))))))
                    tmp)
             (let ((tmp ($sc-dispatch tmp-1 '(_ each-any any . #(each ((any . any) any))))))
               (if (if tmp
@@ -2576,12 +2577,13 @@
                                (cons '#(syntax-object syntax-case ((top)) (hygiene guile))
                                      (cons '#(syntax-object x ((top)) (hygiene guile))
                                            (cons k
-                                                 (map (lambda (tmp-1 tmp)
-                                                        (list (cons '#(syntax-object dummy ((top)) (hygiene guile)) tmp)
+                                                 (map (lambda (tmp-2 tmp-1 tmp)
+                                                        (list (cons tmp tmp-1)
                                                               (list '#(syntax-object syntax ((top)) (hygiene guile))
-                                                                    tmp-1)))
+                                                                    tmp-2)))
                                                       template
-                                                      pattern))))))
+                                                      pattern
+                                                      keyword))))))
                        tmp)
                 (syntax-violation
                   #f
@@ -2601,8 +2603,7 @@
                            name
                            (list '#(syntax-object syntax-rules ((top)) (hygiene guile))
                                  '()
-                                 (list (cons '#(syntax-object _ ((top)) (hygiene guile)) pattern)
-                                       template))))
+                                 (list (cons name pattern) template))))
                    tmp)
             (let ((tmp ($sc-dispatch tmp-1 '(_ (any . any) any any))))
               (if (if tmp
@@ -2616,8 +2617,7 @@
                                (list '#(syntax-object syntax-rules ((top)) (hygiene guile))
                                      '()
                                      docstring
-                                     (list (cons '#(syntax-object _ ((top)) (hygiene guile)) pattern)
-                                           template))))
+                                     (list (cons name pattern) template))))
                        tmp)
                 (syntax-violation
                   #f
diff --git a/module/ice-9/psyntax.scm b/module/ice-9/psyntax.scm
index 6c264a6..dc32f5a 100644
--- a/module/ice-9/psyntax.scm
+++ b/module/ice-9/psyntax.scm
@@ -2789,7 +2789,7 @@
            #((macro-type . syntax-rules)
              (patterns pattern ...))
            (syntax-case x (k ...)
-             ((dummy . pattern) #'template)
+             ((keyword . pattern) #'template)
              ...)))
       ((_ (k ...) docstring ((keyword . pattern) template) ...)
        (string? (syntax->datum #'docstring))
@@ -2799,7 +2799,7 @@
            #((macro-type . syntax-rules)
              (patterns pattern ...))
            (syntax-case x (k ...)
-             ((dummy . pattern) #'template)
+             ((keyword . pattern) #'template)
              ...))))))
 
 (define-syntax define-syntax-rule
@@ -2808,13 +2808,13 @@
       ((_ (name . pattern) template)
        #'(define-syntax name
            (syntax-rules ()
-             ((_ . pattern) template))))
+             ((name . pattern) template))))
       ((_ (name . pattern) docstring template)
        (string? (syntax->datum #'docstring))
        #'(define-syntax name
            (syntax-rules ()
              docstring
-             ((_ . pattern) template)))))))
+             ((name . pattern) template)))))))
 
 (define-syntax let*
   (lambda (x)
-- 
1.7.10.4


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

* Re: [PATCH] Preserve keyword in 'syntax-rules' and 'define-syntax-rule'
  2012-10-08 18:20 [PATCH] Preserve keyword in 'syntax-rules' and 'define-syntax-rule' Mark H Weaver
@ 2012-10-09 21:34 ` Ludovic Courtès
  2012-10-10 17:41 ` Mark H Weaver
  1 sibling, 0 replies; 7+ messages in thread
From: Ludovic Courtès @ 2012-10-09 21:34 UTC (permalink / raw)
  To: guile-devel

Hi Mark!

Makes sense to me, you can apply it.

Thanks,
Ludo’.




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

* Re: [PATCH] Preserve keyword in 'syntax-rules' and 'define-syntax-rule'
  2012-10-08 18:20 [PATCH] Preserve keyword in 'syntax-rules' and 'define-syntax-rule' Mark H Weaver
  2012-10-09 21:34 ` Ludovic Courtès
@ 2012-10-10 17:41 ` Mark H Weaver
  2012-10-10 20:41   ` Ludovic Courtès
  1 sibling, 1 reply; 7+ messages in thread
From: Mark H Weaver @ 2012-10-10 17:41 UTC (permalink / raw)
  To: guile-devel

Unfortunately, preserving the macro keyword breaks one of Oleg
Kiselyov's macros, namely 'ppat' in system/base/pmatch.scm:

--8<---------------cut here---------------start------------->8---
(define-syntax ppat
  (syntax-rules (_ quote unquote)
    ((_ v _ kt kf) kt)
    ((_ v () kt kf) (if (null? v) kt kf))
    ((_ v (quote lit) kt kf)
     (if (equal? v (quote lit)) kt kf))
    ((_ v (unquote var) kt kf) (let ((var v)) kt))
    ((_ v (x . y) kt kf)
     (if (pair? v)
         (let ((vx (car v)) (vy (cdr v)))
           (ppat vx x (ppat vy y kt kf) kf))
         kf))
    ((_ v lit kt kf) (if (eq? v (quote lit)) kt kf))))
--8<---------------cut here---------------end--------------->8---

Oleg's macro uses '_' in the keyword position of the pattern, even
though '_' is in the literals list.  Therefore, it fails to match
because 'ppat' does not match that literal.

Among other things, this broke our build, which is why Hydra has been
failing to build Guile recently.

I reverted the change.

     Mark



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

* Re: [PATCH] Preserve keyword in 'syntax-rules' and 'define-syntax-rule'
  2012-10-10 17:41 ` Mark H Weaver
@ 2012-10-10 20:41   ` Ludovic Courtès
  2012-10-10 23:34     ` Mark H Weaver
  2012-10-11  2:02     ` Alex Shinn
  0 siblings, 2 replies; 7+ messages in thread
From: Ludovic Courtès @ 2012-10-10 20:41 UTC (permalink / raw)
  To: guile-devel

Hi,

Mark H Weaver <mhw@netris.org> skribis:

> Unfortunately, preserving the macro keyword breaks one of Oleg
> Kiselyov's macros, namely 'ppat' in system/base/pmatch.scm:

[...]

> Oleg's macro uses '_' in the keyword position of the pattern, even
> though '_' is in the literals list.  Therefore, it fails to match
> because 'ppat' does not match that literal.

I would call it a bug in ‘ppat’.  However, the real question is how
frequent that “bug” is.  If people have come to rely on the current
behavior, then it may be more reasonable to stick to it.

> Among other things,

What were the other things?  :-)

Ludo’.




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

* Re: [PATCH] Preserve keyword in 'syntax-rules' and 'define-syntax-rule'
  2012-10-10 20:41   ` Ludovic Courtès
@ 2012-10-10 23:34     ` Mark H Weaver
  2012-10-11  2:02     ` Alex Shinn
  1 sibling, 0 replies; 7+ messages in thread
From: Mark H Weaver @ 2012-10-10 23:34 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: guile-devel

ludo@gnu.org (Ludovic Courtès) writes:

> Mark H Weaver <mhw@netris.org> skribis:
>
>> Unfortunately, preserving the macro keyword breaks one of Oleg
>> Kiselyov's macros, namely 'ppat' in system/base/pmatch.scm:
>
> [...]
>
>> Oleg's macro uses '_' in the keyword position of the pattern, even
>> though '_' is in the literals list.  Therefore, it fails to match
>> because 'ppat' does not match that literal.
>
> I would call it a bug in ‘ppat’.  However, the real question is how
> frequent that “bug” is.  If people have come to rely on the current
> behavior, then it may be more reasonable to stick to it.

I tend to agree that it's arguably a bug in 'ppat'.  I could go either
way on this, so I'll leave it up to you and Andy.

>> Among other things,
>
> What were the other things?  :-)

Well, anything that uses pmatch will fail.  I'm not aware of any other
problems.

     Mark



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

* Re: [PATCH] Preserve keyword in 'syntax-rules' and 'define-syntax-rule'
  2012-10-10 20:41   ` Ludovic Courtès
  2012-10-10 23:34     ` Mark H Weaver
@ 2012-10-11  2:02     ` Alex Shinn
  2012-10-11  7:26       ` Ludovic Courtès
  1 sibling, 1 reply; 7+ messages in thread
From: Alex Shinn @ 2012-10-11  2:02 UTC (permalink / raw)
  To: Ludovic Courtès; +Cc: guile-devel

On Thu, Oct 11, 2012 at 5:41 AM, Ludovic Courtès <ludo@gnu.org> wrote:
> Hi,
>
> Mark H Weaver <mhw@netris.org> skribis:
>
>> Unfortunately, preserving the macro keyword breaks one of Oleg
>> Kiselyov's macros, namely 'ppat' in system/base/pmatch.scm:
>
> [...]
>
>> Oleg's macro uses '_' in the keyword position of the pattern, even
>> though '_' is in the literals list.  Therefore, it fails to match
>> because 'ppat' does not match that literal.
>
> I would call it a bug in ‘ppat’.  However, the real question is how
> frequent that “bug” is.  If people have come to rely on the current
> behavior, then it may be more reasonable to stick to it.

This is not a bug.  R5RS states:

     The keyword at the beginning of the pattern in a <syntax rule> is
     not involved in the matching and is not considered a pattern
     variable or literal identifier.

R6RS forbids _ as a literal.  R7RS retains the R5RS ignoring
of the initial keyword, adds _ as a wildcard, but allows it to be
used as a literal.  So this code would only break in R6RS.

-- 
Alex



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

* Re: [PATCH] Preserve keyword in 'syntax-rules' and 'define-syntax-rule'
  2012-10-11  2:02     ` Alex Shinn
@ 2012-10-11  7:26       ` Ludovic Courtès
  0 siblings, 0 replies; 7+ messages in thread
From: Ludovic Courtès @ 2012-10-11  7:26 UTC (permalink / raw)
  To: Alex Shinn; +Cc: guile-devel

Hi Alex,

Alex Shinn <alexshinn@gmail.com> skribis:

> This is not a bug.  R5RS states:
>
>      The keyword at the beginning of the pattern in a <syntax rule> is
>      not involved in the matching and is not considered a pattern
>      variable or literal identifier.
>
> R6RS forbids _ as a literal.  R7RS retains the R5RS ignoring
> of the initial keyword, adds _ as a wildcard, but allows it to be
> used as a literal.  So this code would only break in R6RS.

Interesting, thanks for the clarification.

So I guess the safest decision for Guile is to keep the current behavior.

Thanks,
Ludo’.



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

end of thread, other threads:[~2012-10-11  7:26 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-10-08 18:20 [PATCH] Preserve keyword in 'syntax-rules' and 'define-syntax-rule' Mark H Weaver
2012-10-09 21:34 ` Ludovic Courtès
2012-10-10 17:41 ` Mark H Weaver
2012-10-10 20:41   ` Ludovic Courtès
2012-10-10 23:34     ` Mark H Weaver
2012-10-11  2:02     ` Alex Shinn
2012-10-11  7:26       ` Ludovic Courtès

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).