chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] macro systems and chicken (long)


From: Alaric Snell-Pym
Subject: Re: [Chicken-users] macro systems and chicken (long)
Date: Fri, 18 Apr 2008 11:21:15 +0100


On 18 Apr 2008, at 3:32 am, Alex Shinn wrote:

    Alaric> When *will* it happen in practice?

You could force it whenever you want with
make-syntactic-closure.  I think in this case it's only
likely to come about as part of a higher order macro that
closes individual parts all the time for safety.

Ok, but the form you're passed as input, before you make any
syntactic closures of it - or do you mean if the input form is itself
the result of a previous macro expansion?

/me does some experiments

#;4> (define-syntax test (er-macro-transformer (lambda (form rename
compare) (pp form) (cons 'begin (cdr form)))))
#;5> (test
123)                                                                 (
#<syntactic-closure test () (chicken)> 123)
123
#;6> (test + - * / make-hash-table (make-hash-table) 'make-hash-
table 123)
(#<syntactic-closure test () (chicken)>
 +
 -
 *
 /
 make-hash-table
 (make-hash-table)
 'make-hash-table
 123)



Ok, it looks like with er, the first symbol of the form - the macro
name - is wrapped in a closure while the rest isn't, regardless of if
it's a bare symbol, in application position, or quoted (not that I'd
expected any change in quoted symbols, but I tried it for completeness)

#;11> (define-syntax test2 (sc-macro-transformer (lambda (form env)
(pp form) (cons 'begin (cdr form)))))
#;13> (test2 + - * / make-hash-table (make-hash-table) 'make-hash-
table 123)    (#<syntactic-closure test2 () (chicken)>
 +
 -
 *
 /
 make-hash-table
 (make-hash-table)
 'make-hash-table
 123)


Looks like synclos are the same.

Now, let's try seeing what happens if we nest macro applications.
Macros are expanded from the outside in, so the following test
shouldn't be too interesting:

#;14> (test2 (test + - * / make-hash-table (make-hash-table) 'make-
hash-table 123))
(#<syntactic-closure test2 () (chicken)>
 (test + - * / make-hash-table (make-hash-table) 'make-hash-table
123))
(#<syntactic-closure test () (transformer test2)>
 +
 -
 *
 /
 make-hash-table
 (make-hash-table)
 'make-hash-table
 123)


Sure enough, test2 happens first, and the body is the call to test
which, after two macro-expansions, is still all plain symbols; but
what about when the result of expanding a macro then contains a macro
call, nesting in the other way?

#;15> (define-syntax test3 (sc-macro-transformer (lambda (form env)
(pp form) (cons 'test2 (cdr form)))))
#;16> (test3 + - * / make-hash-table (make-hash-table) 'make-hash-
table 123)    (#<syntactic-closure test3 () (chicken)>
 +
 -
 *
 /
 make-hash-table
 (make-hash-table)
 'make-hash-table
 123)
(#<syntactic-closure test2 () (transformer test3)>
 +
 -
 *
 /
 make-hash-table
 (make-hash-table)
 'make-hash-table
 123)

Still looks like it's just symbols in the inner macro expansion (the
second one shown).

Ok, but I wasn't actually creating a syntactic closure. So how about
this:


#;18> (define-syntax test4 (sc-macro-transformer (lambda (form env)
(pp form) (cons 'test2 (map (cut make-syntactic-closure env '() <>)
(cdr form))))))


*now* we hit pay dirt:

#;19> (test4 + - * / make-hash-table (make-hash-table) 'make-hash-
table 123)
(#<syntactic-closure test4 () (chicken)>
 +
 -
 *
 /
 make-hash-table
 (make-hash-table)
 'make-hash-table
 123)
(#<syntactic-closure test2 () (transformer test4)>
 #<syntactic-closure + () (chicken)>
 #<syntactic-closure - () (chicken)>
 #<syntactic-closure * () (chicken)>
 #<syntactic-closure / () (chicken)>
 #<syntactic-closure make-hash-table () (chicken)>
 #<syntactic-closure (make-hash-table) () (chicken)>
 #<syntactic-closure (quote make-hash-table) () (chicken)>
 #<syntactic-closure 123 () (chicken)>)



So it looks like that whenever we call make-syntactic-closure, the
synclos thus created persist until all macro expansion is done and
are THEN converted down to appropriately renamed symbols, so macros
that work upon the results of previous macro expansions *will* see them.

Hmmkay.

What we need, then, rather than a special syntax matching macro, is
an extension to the existing match macro to accept a custom
comparison predicate for symbols! Then an ER macro can pass in the
compare procedure and use match willy-nilly as if nothing was amiss,
while a syntactic-closure macro can do the same with identifier= and
all that :-)

ABS

--
Alaric Snell-Pym
Work: http://www.snell-systems.co.uk/
Play: http://www.snell-pym.org.uk/alaric/
Blog: http://www.snell-pym.org.uk/?author=4






reply via email to

[Prev in Thread] Current Thread [Next in Thread]