On 2011-02-04, at 8:38 AM, Oleg Parashchenko wrote:
Hello,
answering to myself:
How to restore the stack correctly when jumping to a continuation?
After some observation, I decided that GVM code has the property: values in the stack are read-only. Therefore, call/cc makes a copy of the stack, and a jump to a continuation restores this copy.
Maybe this solution is not optimal, but it works at least for the yin-yang puzzle. (http://en.wikipedia.org/wiki/Call-with-current-continuation )
It is not quite true that stack slots are read-only, because a stack slot can be reused for a different variable when the variable it was holding is no longer live. Mutable Scheme variables are allocated in the heap, so in that sense a "set!" will not change the stack.
So... continuations can indeed be implemented by copying the stack. Copying the entire stack however is wasteful and can lead to very poor performance. For this reason Gambit copies each frame independently. Pages 72-80 of the "Gambit inside out" presentation explain how continuations are implemented in Gambit (see the Gambit wiki main page for the link to the talk).
Marc