Continuation - where does it continue

M

michelemendel

Why does the following code print the line "doing other stuff" twice?


def cc
puts "do A"
puts "----> 1. #{caller}"
callcc { |cont| return cont}
puts "----> 2. #{caller}"
puts "do B"
end

puts "calling cc"
cont = cc()
puts "doing other stuff"
cont.call() if cont
puts "end of work"
puts


OUTPUT:
calling cc
do A
----> 1. remove2.rb:59
doing other stuff
----> 2. remove2.rb:59
do B
doing other stuff
end of work
 
P

Patrick Roemer

Responding to (e-mail address removed):
Why does the following code print the line "doing other stuff" twice?

def cc
puts "do A"
puts "----> 1. #{caller}"
callcc { |cont| return cont}
puts "----> 2. #{caller}"
puts "do B"
end

puts "calling cc"
cont = cc()
puts "doing other stuff"
cont.call() if cont
puts "end of work"
puts

The continuation 'continues' from exactly the state where it left off:
It starts after the callcc call, after returning from the cc method it
continues one 'stack frame'[1] deeper after the cc() call, just as in
normal execution - it won't 'jump back' to the code position after the
continuation call.

Best regards,
Patrick

[1] Java lingo, don't know what it's called in Ruby
 
P

Pit Capitain

Why does the following code print the line "doing other stuff" twice?
=20
def cc
puts "do A"
puts "----> 1. #{caller}"
callcc { |cont| return cont}

The continuation continues here, after the call to callcc.
puts "----> 2. #{caller}" =20
puts "do B"
end
=20
puts "calling cc"
cont =3D cc() # <------ (X)
puts "doing other stuff"
cont.call() if cont
puts "end of work"
puts
=20
=20
OUTPUT:
calling cc
do A
----> 1. remove2.rb:59
doing other stuff
----> 2. remove2.rb:59
do B
doing other stuff
end of work

Above I've shown the place where the continuation continues. So when you=20
call the continuation, you're back in the cc method after the callcc.=20
The program prints the remaining messages of the cc method and returns=20
nil (the result of puts). After the cc method returns, you're back at=20
the place where the method has been called, which the line marked with=20
(X). cont is set to nil, and the program prints "doing other stuff" for=20
the second time.

If you dont' want this, you have to change the code to something like

puts "calling cc"
cont =3D cc()
if cont
puts "doing other stuff"
cont.call()
end
puts "end of work"
puts

Florian Gro=DF has written something to make Continuation handling easier=
=20
You should be able to find it in the mailing list archives.

Regards,
Pit
 
J

Jim Weirich

(e-mail address removed) said:
Why does the following code print the line "doing other stuff" twice?
01 def cc
02 puts "do A"
03 puts "----> 1. #{caller}"
04 callcc { |cont| return cont}
05 puts "----> 2. #{caller}"
06 puts "do B"
07 end08 puts "calling cc"
09 cont =3D cc()
10 puts "doing other stuff"
11 cont.call() if cont
12 puts "end of work"
13 puts

This is the seqence of events (by line number). Comments on what's
happening are in parenthesis.

08: calling cc
09 (calling cc)
02: do A
03: ----> 1. remove2.rb:59
04: (returning the continuation)
10: doing other stuff
11: (calling the continuation, so picking up in the middle of cc)
05: ----> 2. remove2.rb:59
06: do B
07: (returning from cc to the original location cc call site.
Now we return a nil value)
10: doing other stuff
11: (since cc returned a nil the second time, we don't call the
continuation this time)
12: end of work

--=20
-- Jim Weirich (e-mail address removed) http://onestepback.org
 
M

michele

AHA - EUREKA!!!

Thanks for your very quick answers! After "studying" continuations for
a couple of days, reading everthing I could find about, I think I
finally understand it. Your answers made it all clear.


Michele
 
S

Sean O'Halpin

Hi,

this finally cleared up Ruby's continuations for me too.

Would this be the effect you're after?
# code
def cc
puts "do A"
puts "----> 1. #{caller}"
cont =3D callcc { |cont| return cont}
puts "----> 2. #{caller}"
puts "do B"
end

puts "calling cc"
if cont =3D cc()
puts "doing other stuff"
cont.call() if cont
puts "end of work"
puts
end

#output
__END__
calling cc
do A
----> 1. continuations.rb:10
doing other stuff
----> 2. continuations.rb:10
do B
#end

I'm curious - what problem are you trying to solve?

Regards,
Sean
 
R

rpardee

What I don't understand is why the line:

04 callcc { |cont| return cont}

Doesn't give an 'undefined local method or variable "callcc"' error.
To my noob eyes that looks like a method call w/an accompanying block.
Have you got a clue for me?

Thanks!

-Roy
 
J

Jim Weirich

What I don't understand is why the line:

04 callcc { |cont| return cont}

Doesn't give an 'undefined local method or variable "callcc"' error.
To my noob eyes that looks like a method call w/an accompanying block.

You've got good eyes. That's exactly what it is ... a call to the "callcc"
method passing a block.
 
M

michele

I justed wanted to understand how continuations work and where it
happens, so I made the simplest example possible I could think of. I
wanted to start work one place, leave it in the middle, do some other
work, and then continue from where I left.

Your answer as well as Pit Capitain's above solved the problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top