shack_a_nerd ([info]shack_a_nerd) wrote,

Continuations

Everyone who understands continuations writes their own explanation of what they are and how they work. In fact, what follows is a list of those I found helpful:

Anyhow, now I have to write something explaining continuations too, so here goes.

What happens to the return value of a function call?

Well, it depends on where the function call is. In other words, it depends on the context. If there were a single thing you could point to and say "that is what happens to the return value", that would be a continuation.

To expand, let's say there's a function f. If you call f like so:
print f()+1

Then everything in this statement except the call to f() is the continuation. So you could draw it like this:
print ###+1

where "###" might mean something like "return value goes here". If you could get this continuation (I think the word is "reify"), there would be one relevant operation to perform -- you could invoke the continuation with one value. That value would get passed to the "+1" and eventually the "print". In other words, when you invoked the continuation, it would have the effect of "making" f return with the value you gave.

But how would you get a continuation? From God? Note that if you have a magic function M which returns a continuation, you get some unexpected complications:
c = M() #(1)
#some stuff
c.invoke(4) #returns you to (1), M() returns 4

But now, when you get down to the line where c.invoke is called, you end up trying to invoke an int! Game over, man. So instead we make a different magic function, call-with-current-continuation (sometimes abbreviated as call/cc), which takes a function f, and passes the continuation to f.
def f(c):
  c.invoke(4)
  print 'Hi!'

print call_cc(f) #prints 4

This is because the continuation to call_cc is "print". So the continuation, when invoked, returns a value to print. As previously, invoking the continuation makes call_cc return immediately, and 'Hi!' is never printed.

That's all I really wanted to say tonight. I have some thoughts from my brush with the FibraNet package, but I don't know if I'll post them or not.

Ethan
Tags: hacking, teaching

  • Post a new comment

    Error

  • 0 comments
Create an Account
Forgot your login or password?
Facebook Twitter More login options
English • Español • Deutsch • Русский…