[r6rs-discuss] [Formal] Trivial Enhancement of macros in v5.91: capture-syntax
On Nov 26, 2006, at 8:57 PM, Lynn Winebarger wrote:
> ---
> This message is a formal comment which was submitted to formal-
> comment_at_r6rs.org, following the requirements described at: http://
> www.r6rs.org/process.html
> ---
> The macro system needs a way of redefining core forms without
> losing their primitive definitions.
>
> Scheme is a language without reserved words. However, while it
> is possible to redefine
> core forms such as lambda, doing so will render the system useless,
> because the original
> primitive transformer will be lost. The v 5.91 draft has admitted
> that syntactic transformation
> is accomplished by closures. These closures should be accessible.
> The following code
> illustrates the idea.
>
> (define-syntax primitive-lambda (capture-syntax lambda))
> (define-syntax lambda (capture-syntax trace-lambda))
> <code to be traced>
> (define-syntax lambda (capture-syntax primitive-lambda))
>
> A more serious example might extend the definition of lambda to
> take keyword arguments or
> more patterns in the argument than a simple list. The capture-
> syntax form should be trivial
> to implement.
Very nice idea. Here is how it is done (tested in my implementation
that I did not post yet).
This is a library that defines the stuff you want to do.
(library tracers
(export trace-region untrace-region)
(import r6rs r6rs::syntax-case)
(define print-args
(lambda (fml* act*)
(display "Lambda ")
(display fml*)
(display " : ")
(display act*)
(newline)))
(define-syntax trace-region
(lambda (x)
(syntax-case x ()
[(kwd b b* ...)
(with-syntax ([L (datum->syntax #'kwd 'lambda)])
#'(let-syntax ([L
(lambda (stx)
(syntax-case stx ()
[(_ fml* body body* (... ...))
#'(lambda act*
(print-args 'fml* act*)
(apply (lambda fml* body body*
(... ...))
act*))]))])
b b* ...))])))
(define-syntax untrace-region
(lambda (x)
(syntax-case x ()
[(kwd b b* ...)
(with-syntax ([L (datum->syntax #'kwd 'lambda)])
#'(let-syntax ([L
(lambda (stx)
(syntax-case stx ()
[(_ fml* body body* (... ...))
#'(lambda fml* body body*
(... ...))]))])
b b* ...))]))))
And here is a test library that uses it:
(library FOO
(export)
(import r6rs tracers)
(define a (lambda (q) (display "A not traced\n")))
(trace-region
(define b (lambda (r) (display "did it work in B?\n")))
(untrace-region
(define c (lambda (s) (display "C not traced\n"))))
(define d (lambda (t) (display "did it work in D?\n"))))
(a 'a)
(b 'b)
(c 'c)
(d 'd))
Executing FOO produces:
A not traced
Lambda (r) : (b)
did it work in B?
C not traced
Lambda (t) : (d)
did it work in D?
Cool, no? It even worked the first time. Of course the library would
have been not as simple had explicit phases been in place, but that's
another story.
Aziz,,,
Received on Tue Nov 28 2006 - 04:43:34 UTC
This archive was generated by hypermail 2.3.0
: Wed Oct 23 2024 - 09:15:00 UTC