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

dymamic-wind



In article <JAFFER.92Sep10234847@camelot.ai.mit.edu> jaffer@zurich.ai.mit.edu (Aubrey Jaffer) writes:

   About (dynamic-wind <thunk1> <thunk2> <thunk3>):

   If a continuation is called from within <thunk1> which escapes into
   <thunk2>, should <thunk3> and then <thunk1> be called before <thunk2>?

   If a continuation is called from within <thunk1> which escapes into
   <thunk2>, should just <thunk1> be called before <thunk2>?

   If a continuation is called from within <thunk3> which escapes into
   <thunk2>, should <thunk3> and then <thunk1> be called before <thunk2>?

   If a continuation is called from within <thunk3> which escapes into
   <thunk2>, should just <thunk1> be called before <thunk2>?

   If a continuation is called from within <thunk1> which escapes into
   <thunk3>, should any additional thunks be called?

   If a continuation is called from within <thunk3> which escapes into
   <thunk1>, should any additional thunks be called?

Graphically, these questions are asking where the dynamic environment
stack should be considered switched.

        possible incoming switch context
<thunk1>
	alternate incoming switch context
<thunk2>
        possible outgoing switch context
<thunk3>
	alternate outgoing switch context

This was getting so confusing that I decided to enter the exciting
world of experimental computer science.  Here is my program:

(define cont #f)
(define (test1)
  (dynamic-wind (lambda () (display "thunk1") (newline)
			(call-with-current-continuation
			 (lambda (c) (set! cont c)))
			(display "continuing thunk1")(newline))
		(lambda () (display "thunk2") (newline)
			(if cont (cont #f))
			(display "continuing thunk2") (newline))
		(lambda () (display "thunk3") (newline))))
(define (thunk2)
  (display "starting thunk2")
  (newline)
  (if cont (set! cont #f)
      (call-with-current-continuation (lambda (c) (set! cont c))))
  (display "continuing thunk2")
  (newline))
(define (test3)
  (dynamic-wind (lambda () (display "thunk1") (newline))
		thunk2
		(lambda () (display "thunk3") (newline)
			(if cont (cont #f)))))

Using MITScheme here are the results I get.

;Loading "dymess.scm" -- done
;Value 1: "dymess"

1 ]=> (test1)
thunk1
continuing thunk1
thunk2
continuing thunk1
thunk2
continuing thunk1
thunk2
continuing thunk1
thunk2
...

So if <thunk2> escapes to <thunk1>, <thunk1> will not be called again.
Loading again and running test3 gives:

1 ]=> (test3)
thunk1
starting thunk2
continuing thunk2
thunk3
thunk1
continuing thunk2
thunk3
thunk1
continuing thunk2
thunk3
thunk1
...

So if <thunk3> escapes to <thunk2>, <thunk1> will be called again.  In
fact, I could not interrupt it (I had to kill the process).

I think the following chart reflects how MITScheme does it:

        incoming switch context
<thunk1>

<thunk2>
        outgoing switch context
<thunk3>

Have I correctly interpreted the data?  This seems asymetrical.  My
guess for the correct operation would be to have both switches
adjacent to <thunk2>.  That is how the current implementation of SLIB
operates.

Could people with other implementations of dynamic-wind please try
this out and post the results?