[plt-scheme] Lazy Scheme and continuations

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sun Dec 17 20:57:32 EST 2006

Continuations work in lazy languages just like they work in strict  
languages.
Of course, call/cc (as a primitive ho function) is strict in its  
first argument.
But that's minor.

The programmer must think harder to figure out what's going on but  
fortunately
the reduction semantics works out and is perfectly predictive:

w(define (f a)
   (call/cc
     (lambda (k)
       (if (>= a 0) a
	(k (- 0 a))))))

(f -55)
|-->
(call/cc
   (lambda (k)
     (if (>= -55 0) -55
       (k (- 0 -55)))))
|-->
[(lambda (k)
    (if (>= -55 0) -55
      (k (- 0 -55))))
(lambda (x)
    (abort x))]
|-->
(if (>= -55 0)
     -55
     ((lambda (x) (abort x)) (- 0 -55)))
|-->
((lambda (x) (abort x)) (- 0 -55))
|-->
((lambda (x) (abort x)) 55)
|-->
(abort 55)
|-->
55


-- Matthias





On Dec 17, 2006, at 1:46 PM, Eli Barzilay wrote:

> On Dec 16, Gregory Woodhouse wrote:
>>
>> Okay, this is a bit whimsical. I tried entering the following in
>> Lazy Scheme (expecting something about an unbound identifier call/cc
>> or some such)
>>
>> (define (f a)
>>      (call/cc
>>       (lambda (k)
>>         (if (>= a 0) a
>>             (k (- 0 a))))))
>>
>> It does correctly compute the absolute value in normal (er....better
>> make that standard) Scheme, but (f -4) seems to loop indefinitely in
>> Lazy Scheme. Now, maybe that's just the point, but I thought it
>> might also be possible that it's due to an unrelated bug. Is this
>> behavior "expected"?
>
> My guess is that continuations do their usual thing of exposing
> implementation details in subtle ways.  The current version uses
> MzScheme promises -- I have a new implementation (which will be
> comitted soon) that uses its own promise implementation (related to
> recent discussions on the srfi-45 list), and your code works with it.
>
> (BTW, I just included everything by default, with some sane treatment
> of some features like `begin'.  I didn't even think of the effects of
> using continuations in the lazy language...)
>
> -- 
>           ((lambda (x) (x x)) (lambda (x) (x x)))          Eli  
> Barzilay:
>                   http://www.barzilay.org/                 Maze is  
> Life!
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.