Subject: Re: dynamic-wind, Was: Re: Strange thing about define-syntax II
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 17 May 2001 02:01:11 GMT
Newsgroups: comp.lang.scheme
Message-ID: <9dvbd7$8tvr0$1@fido.engr.sgi.com>
Antti Huima  <huima@localhost.localdomain> wrote:
+---------------
| Now R*RS does not specify how the dynamic environment should actually
| respond to non-local exits. Namely, `with-input-from-file' and
| `with-output-to-file' which change the dynamic environment have
| unspecified behaviour w.r.t. such exits. I think it is common sense
| that usually dynamic environments should be restored at jumps to
| continuations.  In the case of exception handlers this is especially clear.
+---------------

In the case of file I/O it's not at *all* "clear"!!  For instance,
what happens (or "should" happen) if a continuation captured inside
a "with-output-to-file" is (re)invoked after the "with-output-to-file"
terminates normally... thus *closing* the file?!?!?

The Common Lisp standard on the other hand is quite explicit:

	[The] "with-open-file" [macro] evaluates the forms as an
	implicit progn with stream bound to the value returned by open.

	When control leaves the body, either normally or abnormally
	(such as by use of "throw"), the file is automatically closed.
	If a new output file is being written, and control leaves
	abnormally, the file is aborted and the file system is left,
	so far as possible, as if the file had never been opened. 

This may not be what you *want*, but at least it's defined (and the
sane thing to do in most situations).

To tie this to the "call/cc vs call/wc" issue, if a multi-threading
system tries to use "call/wc" (or a call/wc-in-disguise *named* "call/cc),
disaster is likely if a thread switch occurs while the currently-active
thread is inside a "with-output-to-file"...

Even more perverse examples are easy to imagine.


-Rob

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