Continuations and threads (was Re: Iterators & generators)
Gordon McMillan
gmcm at hypernet.com
Fri Feb 18 16:23:49 EST 2000
Thomas Wouters wrote:
[continuations]
> I may be too confused to tell for sure, but i have a gut feeling that i'm
> getting closer and closer to some complete and utterly consistent
> cluelessness of this issue.
That's a good sign.
I can't claim any deep understanding, but I'm a friend of
Christian's, and on good terms with the timbot (I feed him
recycled jpegs), so if I screw up, they'll probably be nice about
it ;-).
"Continuation" comes from "the continuation of the current
program as of this moment in time". There's not much point in
saying it "is" a something-else. It's an abstraction that hides
something completely different.
Go back to assembler, and things like self-modifying code,
gotos and jumps. Forget the processor's "call" instruction.
Now trace the control-flow through a program using tricks like
jump tables (that get dynamically modified) or passed-in target
addresses. It's not a nice linear program that can be captured
on a single stack. It's probably got little stacklettes and
register-save areas all over the place.
The horror of this is what led to "goto considered harmful". But
now take the nasty tricks and take a large part of the venom
out of them by (partially) sanitizing them and giving them an
official place in your language. Call them, hmmm...,
continuations.
See, functional programmers are an insular lot. You rarely see
them in public, except at parades when they all have antler-
hats and silly shoes on. So they completely missed the
infamous "goto considered harmful" thread and didn't even
realize they were doing anything wrong.
Now, let's pretend you're writing a 'bot that can pass as a
functional programmer. There's a complex protocol here.
When two functional programmers see each other on the
street, they recognize each other by the antler hats. But in
certain parts of the Midwest, regular people wear antler hats,
too. So there's a protocol. First a <wink wink>. Then the
secret handshake. Then you sniff each other's armpits and
stamp your foot 3 times.
OK, so you've written a bot, and it works fine on the street.
Now you send it to a cocktail party. It sees a potential
functional programmer and gives the <wink wink>. Now it tries
to move into position to do the secret handshake, but
discovers that it's antler-hat is entangled with someone else's.
Oops. <wink wink> at the new guy. Handshake. But before it
can sniff, the first one has moved up for his handshake. Ay yi
yi. Your bot crashes and is exposed.
So now you rewrite your bot to use a finite state machine so it
can handle multiple sessions. That means throwing out all the
code that worked on the street. But if you'd used
continuations, it would be a relatively minor adjustment of that
code. 'Course you wouldn't have had to write the bot to begin
with.
Now clearly I'm not a functional programmer (I don't know the
handshake and I don't smell right). But ages ago I did enough
hairy assembler stuff to recognize that "structured
programming" throws out a lot of power in the name of safety.
I'd probably still play with that stuff if Intel assembler weren't
such a horrid abomination.
but-I've-never-mounted-the-safety-guard-on-my-table-saw-ly y'rs
- Gordon
More information about the Python-list
mailing list