in reply to (Perl6) Groking Continuations

Well, I can't claim to great knowledge on this, but I'll give it a go. Schemers out there will probably laugh their heads off.

Essentially, a continuation is the abstract notion of "what do do next".

Languages like Scheme carry one of these around on each of their stack frames as something accessible from the language itself. call-with-current-continuation is simply a Higher Order Functionish way of doing this.

;; Guile scheme (call-with-current-continuation ;; Often abbr'd to call-cc. Takes a function as arg. (lambda (fred) (+ 3 4) (do-something 42 fred) (fred 42) ))

What's happening here is that when the outer call-cc is run, it takes the continuation of its current context -- where the call-cc would go next if it exited normally -- and calls (lambda (fred) ...) with the continuation in the formal parameter 'fred'.

When the function object in fred is called, the call-cc exits, and the interpreter goes merrily on its way. The call-cc call itself evaluates to whatever is passed to 'fred' when (or if) that gets called.

Note that the function in 'fred' can be passed around too, and called by other functions. The do-something function used above might decide to call its 'fred' argument with some answer other than 42.

You could fake Scheme's call-cc behaviour in Perl using an eval-BLOCK/if($@){} combo. The trick is distinguishing our fake death from a real thrown error:

sub call_cc (&) { my $clause = shift; my @fake_returns; my $fake_continuation; $fake_continuation = sub { @fake_returns = @_; die $fake_continuation; }; eval { return $clause->($fake_continuation); }; if ($@) { die $@ unless $@ == $fake_continuation; return @fake_returns; } #NOTREACHED } print STDERR call_cc { my $fred = shift; $fred->(42); return 64; # NOTREACHED }; print STDERR "\n";

But no, it's not a real continuation.

Replies are listed 'Best First'.
Re: Re: (Perl6) Groking Continuations
by John M. Dlugosz (Monsignor) on Apr 10, 2003 at 18:10 UTC
    Hmm, I think that's basically a try/catch mechanism. That is, call_cc gives you a block which includes fred. Calling fred will pop out of the whole call_cc block, even if fred was called by a subroutine deeply nested in the calling sequence. That is, it generates a "throw" keyword for you that's specific to this "try" scope. The argument to fred is used to signal exception data back to the point after call_cc, which can use that to distinguish success from failure.


      Is the right answer. It's not a real continuation because squirreling away a copy of the sub-reference $fred in, say, a package-global variable $Jane and calling $Jane later doesn't do what a real call-cc would allow you to do.

      use vars qw{$Jane}; print call_cc { my $fred = shift; $Jane = $fred; # Squirrel away a reference $fred->(42); 64; #NOTREACHED }; print "\n"; # And then call it later, outside the call_cc block... $Jane->(101); #bang.

      Results in... you guessed it, an uncaught exception. So it's just an ersatz call-cc.

      Funnily enough, call-cc is used in Scheme-like languages a lot for early exits from deep/non-deterministic recursions despite also having a try/catch-style mechanism.

        So the (real) continuation (saved in a global variable) could be called even after the body of call_cc returned?

        I see. If the function call activation frames are treated just like everything else, and there is no special action for "going out of scope", keeping a live reference to it is just like any other closure.

        In Perl 6 we'll have to watch it because of the mechanism for having block-exit code in a finally clause or added dynamically by a caller. What would that do, if one can exit more times than enter?! (I have some ideas of what it should mean, but that's for another thread)