Quote:
> I'm trying to get a handle on call/cc, and I was wondering how you can
> trace out by hand what a program using call/cc does. For example,
> without set!, the "substitution model" can be applied to evaluate
> expressions, but after that you need to add in the "environment model,"
> with rules like these:
> 1. To create a fuction, create a pair with one element set to the list
> of arguments and the body of the function, and the other the environment
> in which it was defined.
> 2. To apply a function to arguments, create a new frame, with a parent
> the environment from the function, and the arguments bound to their
> values. Evaluate the body of the function in this new environment.
> 3. To evaluate an expression which is a list, evaluate all of it's
> elements and apply the first item to the rest.
> 4 to n. Lots of special cases for special forms like set!, define,
> lambda, let, etc.
> How can these be modified to include continuations?
The `environment model' doesn't include any model of the control flow,
so it would be hard to modify it to include continuations.
The easiest way (at least it was easiest for me) to understand
continuations is to use the `machine' model. This model is developed
in Chapter 5 of S&ICP, and it uses a `machine' with `registers' and a
`stack' to evaluate Scheme expressions.
In this model, the `continuation' is the *entire* state of the machine
(registers and stack) that is available at the point where the machine
is about to do a return.
The primitive function CALL-WITH-CURRENT-CONTINUATION will be taking a
function as an argument. What it needs to do is `reify' its
continuation (make a copy of the entire state of the machine and wrap
it up in an object), and then apply the receiver to that reified
continuation by putting the receiver in the function register, the
reified continuation into the argument list, and jumping to apply.
When you invoke (apply) one of these reified continuations, you simply
unwrap the whole thing and replace the current machine state with the
old copy, put the argument you are invoking it on wherever return
values go, and jump to return.
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----