www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Continuations in D?

reply Ian Clarke <Ian_member pathlink.com> writes:
Does D support continuations?  I anticipate that continuations will become
increasingly important as 
people seek ways to retain the simplicity of a threaded architecture while
avoiding the overhead of actual 
threads.  If D could support this language feature, I think it would make D very
appealing for a whole 
variety of applications.

Ian.
Feb 19 2006
next sibling parent nick <nick.atamas gmail.com> writes:
Ian Clarke wrote:
 Does D support continuations?  I anticipate that continuations will become
 increasingly important as 
 people seek ways to retain the simplicity of a threaded architecture while
 avoiding the overhead of actual 
 threads.  If D could support this language feature, I think it would make D
very
 appealing for a whole 
 variety of applications.
 
 Ian.
 
 

this point the concentration is on creating a D1.0. Nick.
Feb 19 2006
prev sibling next sibling parent reply nick <nick.atamas gmail.com> writes:
Ian Clarke wrote:
 Does D support continuations?  I anticipate that continuations will become
 increasingly important as 
 people seek ways to retain the simplicity of a threaded architecture while
 avoiding the overhead of actual 
 threads.  If D could support this language feature, I think it would make D
very
 appealing for a whole 
 variety of applications.
 
 Ian.
 
 

D1.0 sometime in the foreseeable future, so most feature suggestions are being targeted at D2.0.
Feb 19 2006
parent ian locut.us writes:
In article <dtbs79$15en$1 digitaldaemon.com>, nick says...
Sorry, getting late. I should clarify that a bit. The aim is to release
D1.0 sometime in the foreseeable future, so most feature suggestions are
being targeted at D2.0.

I understand, can this suggestion be added to the to-do list for D2.0? I think it would be a valuable addition to the language. Ian.
Feb 21 2006
prev sibling parent reply "Unknown W. Brackets" <unknown simplemachines.org> writes:
Please forgive my ignorance; what is a continuation or where can I read 
about it?

Thanks,
-[Unknown]


 Does D support continuations?  I anticipate that continuations will become
 increasingly important as 
 people seek ways to retain the simplicity of a threaded architecture while
 avoiding the overhead of actual 
 threads.  If D could support this language feature, I think it would make D
very
 appealing for a whole 
 variety of applications.
 
 Ian.
 
 

Feb 20 2006
next sibling parent reply David Medlock <noone nowhere.com> writes:
Unknown W. Brackets wrote:

 Please forgive my ignorance; what is a continuation or where can I read 
 about it?
 
 Thanks,
 -[Unknown]
 
 
 Does D support continuations?  I anticipate that continuations will 
 become
 increasingly important as people seek ways to retain the simplicity of 
 a threaded architecture while
 avoiding the overhead of actual threads.  If D could support this 
 language feature, I think it would make D very
 appealing for a whole variety of applications.

 Ian.


To my knowledge of them, they are an alternate form of writing code. Consider a function which returns an integer: int foo() { return 1; } Now suppose you wanted to return multiple integers: int foo() { return 2; return 3; return 4; } // doesnt work to do this requires an inversion of control, the called function will call you back with the values: void foo( void delegate( int ) yield ) { yield( 2 ); yield( 3 ); yield( 4 ); } This is commonly known as continuation passing style or CPS. In this style the function passed to a function represents the 'rest of the program'. In this way the function can complete the program more than one, hence a continuation of the current program. Converting the code to this form allows you to use pieces of code in various ways. Consider try/catch. try { calc1(); calc2(); } catch( ... ) { ..various fail code... } Using this you can see the conversion of this to CPS form: void failure_code() { ..various fail code... } void try_function( void delegate() failure ) { if ( calc1 == FAIL ) failure(); else if ( calc2 == FAIL ) failure(); } try_function( &failure_code ) Inner functions in D make this style of code very workable. PS. Note that with an appropriate return value from failure() you can signify that the failed calculation be retried! This is a simplification but if you google there are several good papers on them. -DavidM
Feb 20 2006
next sibling parent David Medlock <noone nowhere.com> writes:
David Medlock wrote:

<snip>
 -DavidM

As a quick follow up, the D foreach syntax is a CPS construct. The code within the braces below the foreach *is* a continuation. That is why the opApply accepts a delegate. -DavidM
Feb 20 2006
prev sibling parent reply Remy Moueza <Remy_member pathlink.com> writes:
I thought it was possible to simulate continuations with a smart use of C's
setjump and longjump though I never tried to implement any.
Has anyone had a similar idea ?
Feb 20 2006
parent reply David Medlock <noone nowhere.com> writes:
Remy Moueza wrote:
 I thought it was possible to simulate continuations with a smart use of C's
 setjump and longjump though I never tried to implement any.
 Has anyone had a similar idea ?
 
 

This method seems much less flexible than true continuations, which depict differing branches of the program which can be executed multiple times. -DavidM
Feb 20 2006
parent David Medlock <noone nowhere.com> writes:
David Medlock wrote:

 Remy Moueza wrote:
 
 I thought it was possible to simulate continuations with a smart use 
 of C's
 setjump and longjump though I never tried to implement any.
 Has anyone had a similar idea ?

This method seems much less flexible than true continuations, which depict differing branches of the program which can be executed multiple times. -DavidM

Feb 20 2006
prev sibling parent reply ian locut.us writes:
In article <dtclr2$1vgq$1 digitaldaemon.com>, Unknown W. Brackets says...
Please forgive my ignorance; what is a continuation or where can I read 
about it?

Continuations can be quite confusing, depending on how they are explained. The "correct" explanation is quite difficult to understand. A simpler way is to think of a continuation as a frozen thread, although typically they use far fewer resources than an actual frozen thread. It allows you to program in a very "thread heavy" way, but without the overhead that would typically imply. Continuations are useful in situations where you might otherwise use a lot of threads, but where those threads spend most of their time waiting for things to happen. Many networked applications fall into this category, indeed, what initially got me interested in continuations was my work on P2P architectures. The alternative to continuations is to use an event-based, or callback-based architecture, but this can quickly degenerate into spaghetti code (been there, done that). Continuations are present in some of the more academic languages, including Scheme, and some dialects of ML. Ruby also has them, as does Stackless Python. They can also be simulated in C using some rather scary low-level hacking (someone else alluded to this in this thread), but that isn't for the faint of heart, nor is it cross-platform. There are also some kludges to achieve continuations in Java, but they aren't very pretty or efficient. Support for continuations in D would make it a prime candidate for anyone interested in implementing network-based stuff in an efficient language, especially P2P stuff. Ian.
Feb 21 2006
parent reply Brad Anderson <brad dsource.dot.org> writes:
ian locut.us wrote:
 Support for continuations in D would make it a prime candidate for anyone
 interested in implementing 
 network-based stuff in an efficient language, especially P2P stuff.

I would have to agree. I'd been hoping Walter would see the thread on true closures and see the tie-in with continuations. Good stuff here: http://en.wikipedia.org/wiki/Continuation I see gracefully handling the "back" button and maintaining appropriate state in web-based applications as a big use of continuations in the future, so I'm hoping Walter can help and/or Pragma can hack something for DSP. There's a ton of applications for appropriately representing data along a workflow with continuations. So, the closures / first-class functions discussions interest me very much, and continuations is a prime example of uses for those features in the language. BA
Feb 22 2006
parent reply Brad Anderson <brad dsource.dot.org> writes:
Brad Anderson wrote:
 Good stuff here: http://en.wikipedia.org/wiki/Continuation

and following one of the links from the above page, this was great for C programmers: http://www.intertwingly.net/blog/2005/04/13/Continuations-for-Curmudgeons BA
Feb 22 2006
parent pragma <pragma_member pathlink.com> writes:
In article <dthuue$297c$1 digitaldaemon.com>, Brad Anderson says...
Brad Anderson wrote:
 Good stuff here: http://en.wikipedia.org/wiki/Continuation

and following one of the links from the above page, this was great for C programmers: http://www.intertwingly.net/blog/2005/04/13/Continuations-for-Curmudgeons BA

I'd love to see something like that get off the ground, especially for web-programming. From what I understand, a true continuation needs to be context-switchable which is to say it can save/restore register and stack info. Win32 Fibers already provide nuts and bolts for this capability, and I think unix/linux has an analgoue of the same. So a wrapper using what the OS provides, for something like this, could be hashed up without too much fuss. It might also be possible to concoct something in D, that uses some ASM to handle saving and restoring the stack state. You'd almost want this solution as it would invariably involve less kernel time and could lend itself to tighter resource management (key for scalable web apps that could have more than one such continuation per user session). With all of the above approaches, you still won't have a syntax as elegant as what Python has. D would need a 'yield' statement and/or something that flags a function/delegate as having its own discrete stack space. As for hacking it into DSP, it's more a factor of "when we get there" than anything else. :) - Eric Anderton at yahoo
Feb 22 2006