Download Firefox: WindowsMac OS X
logo       
Google Custom Search
    AddThis Social Bookmark Button

[Fwd: Re: define-values implementation wanted for SISC / Chez Scheme / sy: msg#00001

Subject: [Fwd: Re: define-values implementation wanted for SISC / Chez Scheme / syntax-case]
The attached c.l.s posts illustrates a neat trick of using psyntax modules to splice a mixture of definitions and expressions into any context.

I think we may be able to use this technique to simplify some of the macros in the records implementation, oo system and s2j, and make them better behaved when used in local contexts.


Matthias.

--- Begin Message ---
Subject: Re: define-values implementation wanted for SISC / Chez Scheme / syntax-case
Abdulaziz Ghuloum wrote:
You're in the right track in using the portable-syntax-case modules.

Here is how to do it (tested with chez).


(define-syntax define-values
  (lambda (ctx)
    (syntax-case ctx ()
      [(_ (x* ...) e)
       (with-syntax ([(y* ...) (generate-temporaries #'(x* ...))])
         #'(module (x* ...)
             (define x* (void)) ...
             (call-with-values (lambda () e)
                 (lambda (y* ...)
                   (set! x* y*) ...))))])))

Thank you very much! What I couldn't figure out was, if all the definitions were done first, how to get the multiple values to the set!'s without evaluating the multiple value expression more than once. Your solution is to push the expression down to the set!'s, which makes perfect sense.

Here's an implementation in syntax-rules:

(define-syntax define-values
  (syntax-rules ()

    ((define-values "generate-lambda" () (value ...) (set ...))
     (lambda (value ...)
       set ...))

    ((define-values "generate-lambda"
                    (variable1 variable ...) (value ...) (set ...))
     (define-values "generate-lambda"
                    (variable ...)
                    (value ... temporary)
                    (set ... (set! variable1 temporary))))

    ((define-values (variable ...) expression)
     (module (variable ...)
       (define variable (void)) ...
       (call-with-values (lambda () expression)
         (define-values "generate-lambda" (variable ...) () ()))))))

Here each recursive expansion of "generate-lambda" generates a temporary for us, as "temporary" is free.

However I think your implementation is clearer.

I dedicate my code here to the public domain, in case it is useful to anyone else.

Thank you!

Andrew Wilcox
email: scheme[at]andrewwilcox.name

--- End Message ---
<Prev in Thread] Current Thread [Next in Thread>