I found the following text in this doc

[Continuation Passing Style (CPS)] is a programming style where no function is ever allowed to return. A function "A" must emulate returning by passing its would-be-return value to a continuation function that was passed into "A" as an explicit parameter. Thus, all function calls are tail calls, which means, all function calls are instances of "goto with parameters."

What follows is my interpretation. Given the example program:

sub input { ...; return $value; } sub process1 { my ($input ) = @_; ...; return $result; } sub process2 { my ($input ) = @_; ...; return $result; } sub output { my ($result) = @_; ...; } output( process1( process2( input() ) ) );

Continuation style, it would look like:

sub input { my ( $c) = @_; ...; &$c($value ); } sub process1 { my ($input, $c) = @_; ...; &$c($result); } sub process2 { my ($input, $c) = @_; ...; &$c($result); } sub output { my ($result ) = @_; ...; } input( sub { process1(@_, sub { process2(@_, \&output ) } ) } );

Why oh why? A language and/or compiler designed to optimize function calls at the end of a sub into a goto would compile the above snippet into:

sub input { my ( $c) = @_; ...; @_ = ($value ); goto(&$c); } sub process1 { my ($input, $c) = @_; ...; @_ = ($result); goto(&$c); } sub process2 { my ($input, $c) = @_; ...; @_ = ($result); goto(&$c); } sub output { my ($result ) = @_; ...; } input( sub { process1(@_, sub { process2(@_, \&output ) } ) } );

Update: It might even optimize it to the following. I'm deep into speculative waters, now.

goto line1; input: { my ( $c) = @_; ...; @_ = ($value ); goto($c); } process1: { my ($input, $c) = @_; ...; @_ = ($result); goto($c); } process2: { my ($input, $c) = @_; ...; @_ = ($result); goto($c); } output: { my ($result ) = @_; ...; } line1: { push(@_, 'line2'); goto input; } line2: { push(@_, 'line3'); goto process1; } line3: { push(@_, 'line4'); goto process2; } line4: { goto output; }

Any why is that good? And what about loops and everything? I'm not exactly sure. And all that being said, I don't know how it relates to coroutines. But it was interesting to me, and I hope it helps you.


In reply to Re: (Semi-OT) Coroutine vs Continuation? by ikegami
in thread (Semi-OT) Coroutine vs Continuation? by BUU

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.