On Sep 13, 2009, at 4:20 PM, Abdulaziz Ghuloum wrote:

>
> On Sep 14, 2009, at 12:06 AM, Andre van Tonder wrote:
>
>> Ah, the way it is done in my expander is by keeping, for every body,
>> a list of identifiers whose denotations have affected expansion of
>> th body so far.  If a definition is encountered, the LHS identifier
>> is compared against this list, and if its denotation has already
>> been used to affect the body expansion so far, an error is raised.
>
> Sorry.  I guess my question isn't clear.  Let me try something else.
>
> In this one-pass expansion, do you think it's possible to expand
>
> (let ([f (lambda () 1)])
>   (let ()
>     (define (g) (f))
>     (define (f) 2)
>     (g)))
>
> to something like
>
> (let ([f0 (lambda () 1)])
>   (letrec* ([g1 (lambda () (f1))]
>             [f1 (lambda () 2)])
>     (g1))
>
> directly (i.e., without uncovering all internally-defined identifiers
> first, and without doing a second renaming pass)?
>
> If so, how?


Chicken does this. The algorithm seems to be:

* Start with an empty set of collected `define' forms, and an empty  
set of collected `define-syntax' forms.
* Examine each definition in turn.
* If the definition is a `define', and there is a nonempty set of  
`define-syntax' collected forms, convert this set into a `letrec- 
syntax' form wrapping the following definitions and body forms.
* If the definition is a `define-syntax' form, and there is a nonempty  
set of `define' collected forms, convert this set into a `letrec' form  
wrapping the following definitions and body forms.
* Otherwise, collect the form.
* After the last `define' or `define-syntax' form, clear any remaining  
collected forms as above.

However, it does not allow body forms to expand into `define' or  
`define-syntax' forms as far as I can tell.

Thus:

(let-syntax ((f (syntax-rules ()
                   ((_) 1))))
   (let ()
     (define (g) (f))
     (define-syntax h (syntax-rules ()
                        ((_) (i))))
     (define-syntax i (syntax-rules ()
                        ((_) 2)))
     (define (f) (h))
     (define (j) (f))
     (display (list (g) (j)))))

-> (1 2) in Chicken, and (2 2) in a R6RS implementation.

I believe that Chicken is a faithful implementation of the one-pass  
semantics in this case.
--
Brian Mastenbrook
[email protected]
http://brian.mastenbrook.net/

_______________________________________________
r6rs-discuss mailing list
[email protected]
http://lists.r6rs.org/cgi-bin/mailman/listinfo/r6rs-discuss

Reply via email to