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

From: Jos Koot (jos.koot at telefonica.net)
Date: Thu Jul 17 18:15:44 EDT 2008

Placing impl within process does work:

#lang scheme
(define cond1 #f)
(define cond2 #f)
(define (foo-process arg1 arg2)
 (define (process)
  (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 arg2)
    (let ((result (+ arg1 arg2)))
     (let/cc resume-here (set! impl resume-here))
     (return result)))
     (call/cc impl))
 process)

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

Welcome to DrScheme, version 4.0.2.3-svn17jul2008 [3m].
Language: Module; memory limit: 600 megabytes.
Passed in args 13 and 29
"Condition 1 not met"
Passed in args 13 and 29
Condition 1 is met, going to condition 2
"Condition 2 not met"
Passed in args 13 and 29
Condition 1 is met, going to condition 2
Condition 2 is met, can finish the process
The process has finished with values 13 and 29
42
Passed in args 665 and 1
Condition 1 is met, going to condition 2
Condition 2 is met, can finish the process
The process has finished with values 665 and 1
666
> 
----- Original Message ----- 
From: "Grant Rettke" <grettke at acm.org>
To: "PLT-list" <plt-scheme at list.cs.brown.edu>
Sent: Thursday, July 17, 2008 10:53 PM
Subject: [plt-scheme] Confusing continuation behavior (a question about)


> Hi,
> 
> 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)
>    (begin
>      (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
> arg2)
>      (let ((result (+ arg1 arg2)))
>        (let/cc resume-here (set! impl resume-here))
>        (return result))))
> 
>  process)
> 
> (define a-process (foo-process 13 29))
> (define b-process (foo-process 665 1))
> (a-process)
> (set! cond1 #t)
> (a-process)
> (set! cond2 #t)
> (a-process)
> (b-process)
> 
> ; 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.
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme


Posted on the users mailing list.