[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Chicken-janitors] #394: implicit renaming macros
From: |
Chicken Trac |
Subject: |
[Chicken-janitors] #394: implicit renaming macros |
Date: |
Tue, 21 Sep 2010 07:09:19 -0000 |
#394: implicit renaming macros
-------------------------------------------+--------------------------------
Reporter: sjamaan | Owner: felix
Type: enhancement | Status: new
Priority: not urgent at all | Milestone: 4.7.0
Component: expander | Version: 4.6.x
Keywords: ir-macros, reverse, er-macros |
-------------------------------------------+--------------------------------
From my mail, saved here for later because Felix is too busy right now:
I've decided to pick up an old idea of mine which I got from Alex's
post about macros which I think would rock to have in Chicken core.
The part where he explains how sc-macro-transformer and
rsc-macro-transformer relate to eachother made me think there ought
to be a similar "inverse" of er-macro-transformer. Something like
"rer-macro-transformer".
The idea is to introduce a new kind of macros called "implicit
renaming macros" (or "explicit injection macros", I haven't
decided on what's better) where instead of explicitly indicating what
you want hygienically renamed (like in er macros), you explicitly
indicate where you want to break hygiene. This is a lot like what
syntax-case does, except without the cruft ;) -- and without the
pattern matching :(
A macro would be much simplified. Here's an side-by-side example taken
from the testsuite:
{{{
(define-syntax loop | (define-syntax loop
(er-macro-transformer | (ir-macro-transformer
(lambda (x r c) | (lambda (x i c)
(let ((body (cdr x))) | (let ((body (cdr x)))
`(,(r 'call/cc) | `(call/cc
(,(r 'lambda) (exit) | (lambda (,(i 'exit))
(,(r 'let) ,(r 'f) () | (let f ()
,@body (,(r 'f)))))))) | ,@body (f))))))))
}}}
As you can see, it's a lot cleaner: only "exit" needs to be "injected"
because it requires it to be inserted literally. All the rest is
implicitly renamed, just like in syntax-rules. It makes a lot more
sense to make the default state of identifiers be hygienic, and
breaking the hygiene the exception. It also is easier on the eyes;
less comma-splicing/quoting punctuation crap. You can get rid of
whole LET-blocks in the typical "rename everything once" idiom.
To make this work, I hacked the er-macro-transformer procedure.
The idea is extremely simple:
* You first *fully* rename the input expression by calling RENAME on it
* You then run the ER macro transformer as usual. The user renames those
expressions he wants to insert _un_hygienically.
* You then run the renamer in reverse. This causes all previously
renamed identifiers (those in the input expression and the ones that were
"renamed" by the user) to be reverted to their original state.
The result is that all non-renamed expressions are the ones that actually
''are'' renamed in the final output, while the ones that are renamed are
''not'' renamed (a simple inversion of the "renamed" state, so to speak).
It adds a little overhead: one extra up front traversal over the input
expression and one extra traversal over the output expression. However,
the user needs to do less renaming in the macro itself (only on
injection), so not the full two traversals count as "extra overhead",
since the lookups would be done in either case (if the macro is properly
hygienic).
This is a tradeoff the user is free to decide on for himself. The
overhead is not there in case of the original er-macros and my patch
doesn't add any overhead to it, either.
--
Ticket URL: <http://www.intute.us/ticket/394>
Chicken Scheme <http://www.call-with-current-continuation.org/>
Chicken Scheme is a compiler for the Scheme programming language.
- [Chicken-janitors] #394: implicit renaming macros,
Chicken Trac <=
- [Chicken-janitors] Re: #394: implicit renaming macros, Chicken Trac, 2010/09/21
- [Chicken-janitors] Re: #394: implicit renaming macros, Chicken Trac, 2010/09/26
- [Chicken-janitors] Re: #394: implicit renaming macros, Chicken Trac, 2010/09/26
- [Chicken-janitors] Re: #394: implicit renaming macros, Chicken Trac, 2010/09/26
- [Chicken-janitors] Re: #394: implicit renaming macros, Chicken Trac, 2010/09/26
- [Chicken-janitors] Re: #394: implicit renaming macros, Chicken Trac, 2010/09/26