Discussion:
[Larceny-users] define-macro library
Eduardo Cavazos
2009-02-13 23:37:54 UTC
Permalink
Hello,

So, I've seen a few "define-macro using syntax-case" implementations
floating around. This one loads and works for simple cases. But it has
trouble when you reference non-rnrs-base functions in the body.

For example, this works:

----------------------------------------------------------------------
(define-macro (test-macro a) `(list ,a))
(test-macro 10)
(10)
----------------------------------------------------------------------

but this doesn't:

----------------------------------------------------------------------
(define (sq n) (* n n))
(define-macro (test-macro-sq a) `(list (sq ,a)))
(test-macro-sq 10)
Syntax violation: invalid reference

No binding available for sq in library (define-macro)
----------------------------------------------------------------------

Any suggestions?

Ed

----------------------------------------------------------------------
(library

(define-macro)

(export define-macro)

(import

(for (rnrs base) run expand)

(for (rnrs syntax-case) run expand)

)

(define-syntax define-macro
(lambda (x)
(syntax-case x ()
((_ (name . args) . body)
(syntax (define-macro name (lambda args . body))))
((_ name transformer)
(syntax
(define-syntax name
(lambda (y)
(syntax-case y ()
((_ . args)
(datum->syntax
(syntax _)
(apply transformer
(syntax->datum (syntax args)))))))))))))

)

----------------------------------------------------------------------
Andre van Tonder
2009-02-14 01:05:06 UTC
Permalink
Try changing the underscores to some identifier (for example k) in the
definition of define-macro. Underscores are not pattern variables any longer
in r6rs, so the datum->syntax instance will not do the correct thing any longer.
Haven't tested it but hopefully it will help.
Post by Eduardo Cavazos
Hello,
So, I've seen a few "define-macro using syntax-case" implementations
floating around. This one loads and works for simple cases. But it has
trouble when you reference non-rnrs-base functions in the body.
----------------------------------------------------------------------
(define-macro (test-macro a) `(list ,a))
(test-macro 10)
(10)
----------------------------------------------------------------------
----------------------------------------------------------------------
(define (sq n) (* n n))
(define-macro (test-macro-sq a) `(list (sq ,a)))
(test-macro-sq 10)
Syntax violation: invalid reference
No binding available for sq in library (define-macro)
----------------------------------------------------------------------
Any suggestions?
Ed
----------------------------------------------------------------------
(library
(define-macro)
(export define-macro)
(import
(for (rnrs base) run expand)
(for (rnrs syntax-case) run expand)
)
(define-syntax define-macro
(lambda (x)
(syntax-case x ()
((_ (name . args) . body)
(syntax (define-macro name (lambda args . body))))
((_ name transformer)
(syntax
(define-syntax name
(lambda (y)
(syntax-case y ()
((_ . args)
(datum->syntax
(syntax _)
(apply transformer
(syntax->datum (syntax args)))))))))))))
)
----------------------------------------------------------------------
_______________________________________________
Larceny-users mailing list
https://lists.ccs.neu.edu/bin/listinfo/larceny-users
Eduardo Cavazos
2009-02-14 01:42:39 UTC
Permalink
Post by Andre van Tonder
Try changing the underscores to some identifier (for example k) in the
definition of define-macro. Underscores are not pattern variables any
longer in r6rs, so the datum->syntax instance will not do the correct
thing any longer. Haven't tested it but hopefully it will help.
Thanks for the tip Andre!

I sent out a separate note explaining how I dealt with the problem.

Interestingly, I didn't need to replace the underscores; it worked as is.

Ed
Andre van Tonder
2009-02-14 13:05:33 UTC
Permalink
Doing (datum->syntax (syntax _) .....) still does not make sense. If you use a
pattern variable instead, it should work if you put it in its own library.
Post by Eduardo Cavazos
Post by Andre van Tonder
Try changing the underscores to some identifier (for example k) in the
definition of define-macro. Underscores are not pattern variables any
longer in r6rs, so the datum->syntax instance will not do the correct
thing any longer. Haven't tested it but hopefully it will help.
Thanks for the tip Andre!
I sent out a separate note explaining how I dealt with the problem.
Interestingly, I didn't need to replace the underscores; it worked as is.
Ed
_______________________________________________
Larceny-users mailing list
https://lists.ccs.neu.edu/bin/listinfo/larceny-users
Eduardo Cavazos
2009-02-14 01:40:09 UTC
Permalink
Post by Eduardo Cavazos
----------------------------------------------------------------------
(define (sq n) (* n n))
(define-macro (test-macro-sq a) `(list (sq ,a)))
(test-macro-sq 10)
Syntax violation: invalid reference
No binding available for sq in library (define-macro)
----------------------------------------------------------------------
So... the way I got around this problem is to *not* put 'define-macro'
in it's own Larceny library (i.e. *.sls) file.

What I'm doing right now is evaluating this at the toplevel in ERR5RS mode.

----------------------------------------------------------------------
(import

(for (rnrs base) run expand)

(for (rnrs syntax-case) run expand)

(for (rnrs bytevectors) run expand)

(for (rnrs control) run expand)

(rnrs io simple)
(err5rs records syntactic)
(err5rs load)
(primitives current-directory file-exists?)

)

(define-syntax define-macro
(lambda (x)
(syntax-case x ()
((_ (name . args) . body)
(syntax (define-macro name (lambda args . body))))
((_ name transformer)
(syntax
(define-syntax name
(lambda (y)
(syntax-case y ()
((_ . args)
(datum->syntax
(syntax _)
(apply transformer
(syntax->datum (syntax args)))))))))))))
----------------------------------------------------------------------

Of course, I don't copy and paste that into the listener each time... I
have that in a file called 'src/boot/larceny/larceny.scm' and I start
larceny like this:

cat src/boot/larceny/larceny.scm - | larceny -err5rs

Kludges FTW! :-)

Anywho, the above works for my purposes for now. I hope that eventually
there'll be an implementation of 'define-macro' available in the Larceny
distribution which is blessed by the core team.

Ed
David Rush
2009-02-14 05:37:55 UTC
Permalink
Just off the top of my head this sounds like a classic phasing error.
The macro is actually expanded in an entirely different context from
its apparent definition. I believe that R6RS has language regarding
whether definitions are to be available at expansion-time or at
run-time in the spirit of the _You Want It When_ paper.

Caveat Coder: I've not looked at R6RS in detail since I cast my ballot
against it. I have a large R5RS code base and haven't really felt the
need...
Post by Eduardo Cavazos
Hello,
So, I've seen a few "define-macro using syntax-case" implementations
floating around. This one loads and works for simple cases. But it has
trouble when you reference non-rnrs-base functions in the body.
(define-macro (test-macro a) `(list ,a))
(test-macro 10)
(10)
(define (sq n) (* n n))
(define-macro (test-macro-sq a) `(list (sq ,a)))
(test-macro-sq 10)
Syntax violation: invalid reference
No binding available for sq in library (define-macro)
--
GPG Public key at http://cyber-rush.org/drr/gpg-public-key.txt
Loading...