develooper Front page | perl.perl6.internals | Postings from November 2004

Re: Continuations, basic blocks, loops and register allocation

Thread Previous | Thread Next
From:
Jeff Clites
Date:
November 16, 2004 10:32
Subject:
Re: Continuations, basic blocks, loops and register allocation
Message ID:
E2543776-37FD-11D9-AFE8-000393A6B9DA@mac.com
On Nov 16, 2004, at 10:03 AM, Matt Fowles wrote:

> Since both you and Leo are arguing against me here, it seems like that
> I am wrong, but I would like to figure out exactly why I am wrong so
> that I can correct my train of thought in the future.

Here's a real example you can play with, if you have Ruby installed:

% cat continuation6.ruby
def strange
     callcc {|continuation| $saved = continuation}
end

def outer
     a = 0
     strange()
     a = a + 1
     print "a = ", a, "\n"
end

# these two lines are "main"
outer()
$saved.call

% ruby continuation6.ruby
a = 1
a = 2
a = 3
a = 4
a = 5
a = 6
a = 7
a = 8
a = 9
a = 10
...infinite loop, by design

What happens when the program runs is that outer() is called (only 
once) which creates a continuation (inside of strange()), increments a, 
prints and returns. The next thing that happens is that the 
continuation is invoked. Control jumps to the location in strange() 
right after the callcc line, then that return and we are at the line in 
outer() where 'a' is incremented. So 'a' increments from the last value 
it had in that frame (since we are magically back again inside of the 
"same" single invocation of outer()), then 'a' is printed and outer() 
returns again (note: outer only called once, returned twice so far), 
and then we call the continuation again, and start the loop over.

We only ever create one continuation in this example, since we only 
ever call strange() once. The continuation preserves the frame (the 
mapping from logical variables to their values), but not the values of 
those variables at the time the continuation was created. In effect, I 
think the continuation is arranging to preserve the state of the 
variables as they were when code in the frame was last executed, rather 
than at the time the continuation was created.

The behavior you were describing is what I had thought would happen, 
but then I realized I wasn't sure, so I confirmed that it wasn't. The 
above is the behavior of Ruby, and I believe Scheme works the same way. 
What you described would be useful for backtracking (jumping back not 
only to a previous location in a computation, but also its previous 
state), but it's not what these languages seem to do.

JEff


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About