[gambit-list] CPS and state machines

Marc Feeley feeley at IRO.UMontreal.CA
Tue Dec 7 18:58:12 EST 2004


> Would anyone have an example of implementing a state machine in
> continuation passing style (CPS)? I'm looking for a simple example
> although it does not have to be.

Why do you want to do this?  In Scheme you can code in direct
style, and use call/cc when you want to access the continuation.
The overall structure of your code should be much clearer than
CPS'ed code.

> Howdy!
> 
> Is it possible to return continuations to C in Gambit? This is a quote
> from the documentation:

Yes you can.  The Scheme continuation (call it a stack if you want)
and the C stack are managed independently.  Scheme continuations are
managed in a heap and you can jump from one continuation to another
one at will.  In C, once you return from a function call (i.e. you pop
a stack frame), you can't return to the stack frame that was popped.
The problem is when you mix both worlds, i.e.  you call C from Scheme
or Scheme from C.  In this case, you can still jump from one Scheme
continuation to another at will.  However, if you return from a C to
Scheme function call X, this may remove from the C stack some frames
related to other Scheme continuations (the frames for the C to Scheme
calls that have been created since the call X and that have not
returned yet).  The constraint is that you can't return from these
(otherwise unrelated) C to Scheme calls.

Marc


More information about the Gambit-list mailing list