[plt-scheme] Confusing continuation behavior (a question about)

From: Grant Rettke (grettke at acm.org)
Date: Thu Jul 17 16:53:00 EDT 2008


In v372 I wrote some code (pretty big language) to try to understand
how one might utilize continuations to write processes that could
pause and resume.

It is toy code, it stops at two points, each time checking for some
"condition" to be true, and then proceeds, finally adding two numbers.

Here is the code, along with some output:

(define cond1 #f)
(define cond2 #f)
(define (foo-process arg1 arg2)

  (define (process)
    (call/cc impl))

  (define (impl return)
      (printf "Passed in args ~a and ~a~n" arg1 arg2)
      (let/cc resume-here (set! impl resume-here))
      (if cond1
          (printf "Condition 1 is met, going to condition 2~n")
          (return "Condition 1 not met"))
      (let/cc resume-here (set! impl resume-here))
      (if cond2
          (printf "Condition 2 is met, can finish the process~n")
          (return "Condition 2 not met"))
      (printf "The process has finished with values ~a and ~a~n" arg1
      (let ((result (+ arg1 arg2)))
        (let/cc resume-here (set! impl resume-here))
        (return result))))


(define a-process (foo-process 13 29))
(define b-process (foo-process 665 1))
(set! cond1 #t)
(set! cond2 #t)

; Passed in args 13 and 29
; "Condition 1 not met~n"
; Condition 1 is met, going to condition 2
; "Condition 2 not met~n"
; Condition 2 is met, can finish the process
; The process has finished with values 13 and 29
; 42

I just cut it over to 4.0.2 with #lang scheme, and I get a behavior I
don't understand, it basically enters an infinite loop because the
execution of the program never reaches the point where cond2 gets set
to #t. Instead, it just loops forever like this (I debugged it):

1 -> 2 -> 3 -> 1 -> 2 -> 3 -> ...

1. (a-process)
2. (set! cond1 #t)
3. (a-process)
4. (set! cond2 #t)

I have *just* started digging into this, but let/cc doesn't seem to
have changed... more later.

Posted on the users mailing list.