is define-syntax inside define-syntax OK? 
Author Message
 is define-syntax inside define-syntax OK?

Hello group,

I wrote the following macro:

(define-syntax define-fixture
  (syntax-rules ()
     ((define-fixture name definitions ...)
         (define-syntax name
            (syntax-rules ()
               ((name exprs ...)
                (begin definitions ... (begin exprs ...))))))))

The idea is that this macro allows you to make a "fixture", i.e.
a local environment with things set up. I.e.

(define-fixture foo-fixture
  (define some-list '(1 2 3))

,and then do:

(foo-fixture
   (display some-list))

,which I would like to expand into:

(begin
   (define some-list '(1 2 3))
   (begin
      (display some-list)))

However, bigloo complains (yes I used -hygien):

1:=> (define-fixture foo-fixture (define some-list '(1 2 3)))
*** ERROR:bigloo:bigloo:
`segmentation violation' exception -- reached
#unspecified

Strangely enough, it doesn't do this when I define the maxro, but when
I try to use it.

Is my macro flawed or is this a bug in Bigloo?
BTW, which Scheme implementations have good R5RS macro support?
(I get the impression support is lacking in a lot of Schemes.)

Greetings,

Stephan

--
ir. Stephan H.M.J. Houben
tel. +31-40-2474358 / +31-40-2743497



Tue, 26 Aug 2003 19:17:27 GMT  
 is define-syntax inside define-syntax OK?
r5rs section 5:3 begins with:

Syntax definitions are only valid at the top level of a <program>.

--
(for-each (lambda (str) (display (string-append (make-string (- 40
(quotient (string-length str) 2)) #\space) str)) (newline)) '(""
"Bruce Lewis" "MIT 1990" " http://brl.sourceforge.net/



Wed, 27 Aug 2003 02:55:12 GMT  
 is define-syntax inside define-syntax OK?

Quote:

> Hello group,

> I wrote the following macro:

> (define-syntax define-fixture
>   (syntax-rules ()
>      ((define-fixture name definitions ...)
>          (define-syntax name
>        (syntax-rules ()
>           ((name exprs ...)
>            (begin definitions ... (begin exprs ...))))))))

I think you would like the ellipses `...' after `exprs' in the
generated DEFINE-SYNTAX, not in the define-fixture DEFINE-SYNTAX,
which requires some way of quoting an ellipsis.  R5RS does not specify
any means of quoting ellipses.  In my opinion this is a substantial
wart, and really hobbles LET-SYNTAX and LETREC-SYNTAX.

Quote:
> BTW, which Scheme implementations have good R5RS macro support?
> (I get the impression support is lacking in a lot of Schemes.)

For what it's worth, SCM reported an error when evaluating your
define-fixture definition:

ERROR: Bad ellipsis: (exprs ...) " in template " (define-syntax name (syntax-rules () ((name exprs ...) (begin definitions ... (begin exprs ...)))))

SCM allows ellipses to be quoted as `(... ...)', a feature copied from
SYNTAX-CASE.  Your example could be written:

(define-syntax define-fixture
  (syntax-rules ()
     ((define-fixture name definitions ...)
         (define-syntax name
            (syntax-rules ()
               ((name exprs (... ...))
                (begin definitions ... (begin exprs (... ...)))))))))

This works as expected in SCM.

I don't know how widespread such extensions to SYNTAX-RULES are among
Scheme implementations, but you might profit by searching Bigloo's
documentation.



Wed, 27 Aug 2003 03:06:03 GMT  
 is define-syntax inside define-syntax OK?

Quote:

> Hello group,

> I wrote the following macro:

> (define-syntax define-fixture
>   (syntax-rules ()
>      ((define-fixture name definitions ...)
>          (define-syntax name
>        (syntax-rules ()
>           ((name exprs ...)
>            (begin definitions ... (begin exprs ...))))))))

R5RS, section 5.3 ("Syntax definitions", page 17 left) states that:

  Syntax definitions are valid only at the top level of a <program>.

and, a bit further, that:

  There is no define-syntax analogue of internal definitions.

So, from the "legal" point of view, your code is malformed. Probably,
it doesn't get intercepted at definition-time because the inner
define-syntax is inside the template and thus is, basically, data, not
program. When this macro is used, your define-fixture is rewritten as
define-syntax, which causes the error. (How exactly does this happen
in Bigloo?)

You might use let-syntax (section 4.3.1, "Binding constructs for
syntactic keywords") instead of define-syntax for the inner
definition.

Quote:
> BTW, which Scheme implementations have good R5RS macro support?
> (I get the impression support is lacking in a lot of Schemes.)

Looks like they function pretty well in Scheme 48 and in MzScheme. I'm
not using them extensively, though, so take my opinion with a large
grain of salt.

Yours,
 -Boris.



Wed, 27 Aug 2003 03:05:49 GMT  
 is define-syntax inside define-syntax OK?

Quote:


> > Hello group,

> > I wrote the following macro:

> > (define-syntax define-fixture
> >   (syntax-rules ()
> >      ((define-fixture name definitions ...)
> >          (define-syntax name
> >           (syntax-rules ()
> >              ((name exprs ...)
> >               (begin definitions ... (begin exprs ...))))))))

> R5RS, section 5.3 ("Syntax definitions", page 17 left) states that:

>   Syntax definitions are valid only at the top level of a <program>.

> and, a bit further, that:

>   There is no define-syntax analogue of internal definitions.

This is only an issue if a define-fixture occurs somewhere other than
top level.  Certainly using define-fixture elsewhere is an error.

[ ... ]

Quote:
> You might use let-syntax (section 4.3.1, "Binding constructs for
> syntactic keywords") instead of define-syntax for the inner
> definition.

That would require the invocation of define-fixture to include all
uses of the defined fixture, at which point it might be better to
call it `let-fixture'.


Wed, 27 Aug 2003 03:24:35 GMT  
 is define-syntax inside define-syntax OK?

Quote:

> r5rs section 5:3 begins with:

> Syntax definitions are only valid at the top level of a <program>.

Later on it says "...macros may expand into definitions and syntax
definitions in any context that permits them..."


Wed, 27 Aug 2003 03:30:00 GMT  
 is define-syntax inside define-syntax OK?

Quote:

> Hello group,

> I wrote the following macro:

> (define-syntax define-fixture
>   (syntax-rules ()
>      ((define-fixture name definitions ...)
>          (define-syntax name
>             (syntax-rules ()
>                ((name exprs ...)
>                 (begin definitions ... (begin exprs ...))))))))

> The idea is that this macro allows you to make a "fixture", i.e.
> a local environment with things set up. I.e.

> (define-fixture foo-fixture
>   (define some-list '(1 2 3))

> ,and then do:

> (foo-fixture
>    (display some-list))

> ,which I would like to expand into:

> (begin
>    (define some-list '(1 2 3))
>    (begin
>       (display some-list)))

> However, bigloo complains (yes I used -hygien):

> 1:=> (define-fixture foo-fixture (define some-list '(1 2 3)))
> *** ERROR:bigloo:bigloo:
> `segmentation violation' exception -- reached
> #unspecified

> Strangely enough, it doesn't do this when I define the maxro, but when
> I try to use it.

> Is my macro flawed or is this a bug in Bigloo?

I'd say both.

Others have commented on the problem with quoting the ellipsis.  But the
more fundamental problem is that what you are trying to do is inherently
non-hygienic and should never work with a purely hygienic macro system.

To explain: There is no lexically apparent binding for some-list in
   (foo-fixture (display some-list))
Therefore, some-list must refer to the old binding of that variable.
Now, this is all garbled up in Scheme because the definition in question
does not, in fact, create a new variable binding but SET!s the (conceptually
existing) global variable.  So this might end up working in Scheme, but
the very fact that it works shows that the behavior of top-level bindings
is broken -- a fact that I have complained about many times in many years.

Matthias



Wed, 27 Aug 2003 04:05:58 GMT  
 is define-syntax inside define-syntax OK?

Quote:


> > BTW, which Scheme implementations have good R5RS macro support?
> > (I get the impression support is lacking in a lot of Schemes.)

> Looks like they function pretty well in Scheme 48 and in MzScheme.

My experience has been that Scheme48 is the best Scheme for *standard*
behavior. Larceny is also very good for hygienic macros (does it even
*have* a low-level system). I have experienced many oddities
in Bigloo hygienic macros, but nothing I could convince myself was a
bug - I usually realized that I was basically wrong-headed in the
first place.

david rush
--
The important thing is victory, not persistence.
        -- the Silicon Valley Tarot



Fri, 29 Aug 2003 17:15:09 GMT  
 is define-syntax inside define-syntax OK?

Quote:


> > Hello group,

> > I wrote the following macro:

> > (define-syntax define-fixture
> >   (syntax-rules ()
> >      ((define-fixture name definitions ...)
> >          (define-syntax name
> >           (syntax-rules ()
> >              ((name exprs ...)
> >               (begin definitions ... (begin exprs ...))))))))

> R5RS, section 5.3 ("Syntax definitions", page 17 left) states that:

>   Syntax definitions are valid only at the top level of a <program>.

> and, a bit further, that:

>   There is no define-syntax analogue of internal definitions.

> So, from the "legal" point of view, your code is malformed.

I disagree.  Shortly after the quote you posted, Section 5.3 goes on to
say:

"...macros may expand into definitions and *syntax definitions* in any
context that permits them..." [emphasis added]



Fri, 29 Aug 2003 17:44:18 GMT  
 is define-syntax inside define-syntax OK?
On Fri, 09 Mar 2001 15:05:58 -0500, Matthias Blume

Quote:

>Others have commented on the problem with quoting the ellipsis.  But the
>more fundamental problem is that what you are trying to do is inherently
>non-hygienic and should never work with a purely hygienic macro system.

>To explain: There is no lexically apparent binding for some-list in
>   (foo-fixture (display some-list))
>Therefore, some-list must refer to the old binding of that variable.

...and then the student was enlightened.

OK, so I see now that this {c|sh}ouldn't possibly work.
(WRT your remark on toplevel define's being set!: yes, but I was planning
 to use the fixtures in a non-toplevel context, in which case the define's
 would become a letrec. So even Scheme's sloppy semantics of define
 cannot save me here.)

I will summarise what I learnt:

1. define-syntax in define-syntax should be OK according to R5RS.
2. However, there is a problem that the ... in the inner define-syntax
   are interpreted by the outer define-syntax. There is no standard
   way to solve this, but some implementations allow the inner ...
   to be quoted.
3. But even if so corrected, the resulting fixture would not expand
   into a (let ...) containing the original names but into a (let ...)
   with renamed names, so that it would not be possible to access the
   bindings. If it didn't work that way, it wouldn't be hygienic.

Thanks everybody for his/her contributions.

Stephan

--
ir. Stephan H.M.J. Houben
tel. +31-40-2474358 / +31-40-2743497



Fri, 29 Aug 2003 17:59:10 GMT  
 is define-syntax inside define-syntax OK?

Quote:

> 2. However, there is a problem that the ... in the inner define-syntax
>    are interpreted by the outer define-syntax. There is no standard
>    way to solve this, but some implementations allow the inner ...
>    to be quoted.

You could probably work around using a (define-fixture name first-def .
rest-of-defs) type pattern (with some additional plumbing) rather than
using ellipsis, but that wouldn't solve your fundamental hygiene
problem.


Fri, 29 Aug 2003 20:57:44 GMT  
 is define-syntax inside define-syntax OK?

Quote:

> that wouldn't solve your fundamental hygiene problem.

Apropos of nothing, but wouldn't it be nice to see SRFI codification
of one or two other macro-transformers (I'm thinking particularly of
syntax-case)? The define-syntax grammar (Section 4.3 of R5RS - I
*love* the new HTML version) seems to be almost designed with this in
mind.

Admittedly, a truly low-level transformer might move pretty far in the
direction of requiring compilers to also have full eval capabilities,
but I keep wishing for the ability to derive some syntactic
information concerning macro-expansion. I am also *not* a strict
believer in hygienic macros, and it would be nice to have a standard
mechanism...

Just $.02 worth

david rush
--
Coffee should be black as hell, strong as death and sweet as love.
        -- Turkish proverb



Fri, 29 Aug 2003 23:22:19 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. shadows of define and define-syntax

2. define in define-syntax

3. define-macro -> define-syntax

4. define-macro vs define-syntax

5. Macros defining macros with define-syntax

6. define-syntax must be followed by syntax-rules ??

7. unusual define-syntax syntax

8. Syntax Error: Label in prototype not defined SECURITY_ATTRIBUTES.

9. define-syntax problem...

10. define-syntax for do

11. define-syntax

12. define-syntax

 

 
Powered by phpBB® Forum Software