Subject: Re: Multiple values and BEGIN (again)
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 24 Sep 2000 04:33:56 GMT
Newsgroups: comp.lang.scheme
Message-ID: <8qk07k$116kab$1@fido.engr.sgi.com>
Galen Hancock <galen@soda.csua.Berkeley.edu> wrote:
+---------------
| R5RS gives the following as an expansion for begin
|   (define-syntax begin
|     (syntax-rules ()
|       ((begin exp)
|        exp)
|       ((begin exp1 exp2 ...)
|        (let ((x exp1))
|          (begin exp2 ...)))))
| and it would certainly be incorrect to try to bind multiple values to x.
| So yes, you are allowed to signal an error.
+---------------

But as R5RS says, what you're quoting here is an "alternative expansion
of begin". The one presented first in the spec, immediately above yours,
is slightly different:

	(define-syntax begin
	  (syntax-rules ()
	    ((begin exp ...) ((lambda () exp ...)))))

And all section 4.1.4 "Procedures" says is that "the expressions in the 
body of the lambda expression will be evaluated sequentially...", nothing
about how many values each produces [except the last]. So right there,
one would claim that "(begin (values) 1)" should be legal, if only because
it's not forbidden [at least, in a Scheme implmentation that uses the
*first* expansion of "begin" given in R5RS].

In any case, from the presence of such "alternative expansions", and from 
everal other "tricks" that are presented in various places [yes, they use
the word "trick" in the spec!], I have always assumed that the derived-
expression expansions in R5RS are intended as implementation *suggestions*,
not as mandatory requirements. If so, then the following (albeit ugly
and/or slow) "alternative expansion of begin" should also be permitted,
at least as still one more acceptable "alternative":

	(define-syntax begin
	  (syntax-rules ()
	    ((begin exp)
	     exp)
	    ((begin exp1 exp2 ...)
	     (call-with-values (lambda () exp1)
				(lambda ignored (begin exp2 ...))))))

Note that, as with the second version in R5RS, this version does not
depend on more than one expression in the body of a lambda expression,
either.


-Rob

-----
Rob Warnock, 31-2-510		rpw3@sgi.com
Network Engineering		http://reality.sgi.com/rpw3/
Silicon Graphics, Inc.		Phone: 650-933-1673
1600 Amphitheatre Pkwy.		PP-ASEL-IA
Mountain View, CA  94043