This article is rated C-class on Wikipedia's
content assessment scale. It is of interest to the following WikiProjects: | ||||||||||||||||||
|
To-do list for Continuation:
|
A continuation is a function (that takes an argument) used to explicitly model control flow. You can also observe that in various other articles such as those on continuation-passing style. Because continuations often (call-cc/delimited) can capture a program state the author now seems to inadvertently believe that that is the defining feature of a continuation, it isn't, it's about explicitly modelling control flow not capturing program state. — Preceding unsigned comment added by 2A02:A467:CCD6:1:940D:DBF8:92F5:3D86 ( talk) 19:44, 23 February 2022 (UTC)
I really would have to question whether C# supports continuations in any meaningful form. A C# delegate is little more than a function pointer. (See [1]) MAllerton 19:14, 18 July 2005 (UTC)
IEnumerable CF(int max) { for(int i=0; i < max; ++i ) { yield return i; } }
The article continuation states that there is more than one kind of continuation as follows:
Support for continuations varies widely. A programming language supports re-invocable continuations if a continuation may be invoked repeatedly (even after it has already returned1). Re-invocable continuations were introduced by Peter Landin using his J (for Jump) operator that could perform a nonlocal goto into the middle of a procedure invocation. Re-invocable continutations have also been called "re-entrant" in the MzScheme programming language. However this use of the term "re-entrant" is too easily confused with its use in discussions of multitasking.
At one time Sussman and McDermott thouught that using re-invocable continutations (which they called "Hairy Control Structure") was the solution to the AI control structure problems that had originated in Planner. Carl Hewitt et. al. developed message passing as an alternative solution in the Actor model. Guy Steele and Gerry Sussman then developed in the continuations in Scheme in their attempt to understand the Actor model.
A more limited kind is the escape continuation that may be used to escape the current context to a surrounding one. Many languages which do not explicitly support continuations support
exception handling, which is equivalent to escape continuations and can be used for the same purposes. C's
setjmp
and
longjmp
are also equivalent: they can only be used to unwind the stack. Escape continuations can also be used to implement
tail-call optimization.
-- Carl Hewitt 04:30, 25 August 2005 (UTC)
Python doesn't have an explicit "continuation object" which you can store and reclaim state from/to. However, in the current version of Python (2.4) you have "generators"; they are resumable functions which return a value using the "yield" statement. Does that qualify as a continuation? See [ [2]] for examples of use.-- Larry Hastings 16:46, 15 March 2006 (UTC)
I've added the scheme example from Tim Peters's linked posting. This article (an continuations in general) didn't make any sense until I read a concrete example. —The preceding unsigned comment was added by 14:38, 10 July 2006 ( talk • contribs) Gene Thomas.
The article currently contains the following paragraph:
I do not see why the closures would have to be anonymous as it is obviously possible (albeit tedious) to name all continuations. Also, while you can program using CPS in every language with closures, I am not sure whether this really is practical without guaranteed tail-call elimination, so I think this should at least be mentioned. — Tobias Bergemann 08:32, 15 August 2006 (UTC)
I don't think the initial examples should be in Scheme. I don't think this is the best for understanding for the vast majority of programmers. I myself program for a living, and I totally balked at the Scheme intro, not grasping one single thing of what it tries to explain (The same goes for Closure (computer science), using Lisp). If one look at Coroutines, this starts off with some nice pseudocode, immediately understandable for anyone having the slightest interest in computer languages, adaptable to one's preferred universe. —Preceding unsigned comment added by 84.208.115.107 ( talk • contribs)
Although I am not a Scheme user (I studied it years ago, but for work, I write mostly in Python) I think that ideas like this are better illustrated with Scheme or Lisp than with C-ish languages. A Lisp-oid language shows explicitly where the lexical contours are. In a subject such as continuations, this is essential. -- FOo 03:03, 18 September 2006 (UTC)
Piet Delport removed perl with a bogus explanation (the entry refered to Coro, not Coro::Cont, and both Coro and Coro::Cont provide continuations and not just generators). —Preceding unsigned comment added by 85.180.123.88 ( talk • contribs)
getcontext
/swapcontext
do).call/cc
, and this is exactly why it does not belong on this page: if it did, everything in the
Coroutine and
Generator articles would too.call/cc
, or Scheme?setcontext
/getcontext
. In fact, there are language implementations that use setcontext
/getcontext
not only for that, but for all their first-class continuation support. (
GNU Guile, for example
[3])Coro::State
or (as far as i can tell from the source code and documentation) anything else in the Coro module. (It allows you to capture/resume the current coroutine, but not any of the (outer, exception-handling) continuations entailed by it.)call/cc
need not make any guarantee about the kind or extent ("repeatability") of the continuations obtained by it (unless you're talking about specific definitions, like the one in R5RS
[4] (which has no bearing on anything outside of Scheme, of course)). Many non-Scheme languages with call/cc
(and some Scheme implementations) place necessary restrictions on the extent of the continuations obtained in certain contexts: for example, when mixtures of C and high-level stack frames are involved.Coro::State
comes with a disclaimer saying "This module has not yet been extensively tested, but works on most platforms. Expect segfaults and memleaks", and that it does not seem to have much, if any, actual users. --
Piet Delport
08:41, 1 October 2006 (UTC)There is an article on Fiber (computer science). Fibers appear to be very similar to continuations. If they are the same, perhaps the articles should be merged. If they are not the same, then perhaps a reference and some distinctions should be added to the article. Kevin99 18:04, 21 February 2007 (UTC)
I've redirected to here. If anyone who understands them could add a section, that'd be good. -- Gwern (contribs) 13:14 4 July 2007 (GMT)
Does anyone have a reference for the statement in the article that Steve Russell implemented continuations in his second Lisp interpreter? Kleg 20:01, 31 July 2007 (UTC)
The section states that there's no consensus yet, however, after looking at the linked argument against, it's pretty clear that the opposition to continuations is a result of a fairly gross misunderstanding of the nature and working of continuations, the argument being, roughly, "let's not bother with continuations, instead let's devise a mechanism which would let us store enough of the state to resume the computation at any point", which is exactly what continuations are. The first linked blog post even goes as far as citing the fact that continuations can supposedly break if the user submits a form multiple times, showing utter confusion and completely missing the fact that preventing that is the very reason continuations are used for web programming.
Given that both the pros and cons arguments linked are fairly insubstantial (just random blogs), I'd propose either either expanding that section into a proper explanation of exact nature of continuations as applied to the web programming, or entirely removing the mention of supposed lack of consensus, as it lacks any serious backup in well-researched publications. mathrick 22:48, 2 October 2007 (UTC)
Though Dan Friedman is certainly a fan of call/cc, and talks about continuations often, and several chapters of the Essentials of Programming Languages are dedicated to the Continuation Passing Style, he didn't come up with it. Sussman and Steele's original Scheme, as detailed in the lambda papers, had an operator comparable to call/cc. Even so, they didn't come up with it either, exactly, though their Rabbit compiler was the first to convert programs to the continuation passing style as part of the compiliation. This site suggests that Landin may have come up with it, though the site's author is not sure.
The concept of a contination, as opposed to operators such as call/cc that allow one to grab a continuation, has an obscure origin in the early days of computing. I don't really know many details here. 66.170.46.244 ( talk) 12:55, 29 June 2008 (UTC)
Wow, that's a heavy article. I'm a computer scientist and I had a hard time grasping the summary. 81.15.57.130 ( talk) 09:48, 11 September 2008 (UTC)
During email discussions, Eric Cooper at CMU offered up the below very simple, very elegant example of using call/cc to implement coroutines/threads. I post it here mostly because I think its a very nice programming Design pattern (computer science), combining a queue with call/cc to make it all work. By comparison, googling "coroutine scheme" returns some truly horrid, mind-bendingly bad examples.
This example provides a very nice illustration of "what the heck are continuations good for?" -- because, without examples of their utility, they seem somewhat abstract and pointless. I'd like to see this example in some article somewhere, but not sure which article: this example, though very brief, is still longer than most.
;;; A naive queue for thread scheduling. ;;; It holds a list of continuations "waiting to run". (define *queue* '()) (define (empty-queue?) (null? *queue*)) (define (enqueue x) (set! *queue* (append *queue* (list x)))) (define (dequeue) (let ((x (car *queue*))) (set! *queue* (cdr *queue*)) x)) ;;; This starts a new thread running (proc). (define (fork proc) (call/cc (lambda (k) (enqueue k) (proc)))) ;;; This yields the processor to another thread, if there is one. (define (yield) (call/cc (lambda (k) (enqueue k) ((dequeue))))) ;;; This terminates the current thread, or the entire program ;;; if there are no other threads left. (define (thread-exit) (if (empty-queue?) (exit) ((dequeue))))
;;; The body of some typical Scheme thread that does stuff: (define (do-stuff-n-print str) (lambda () (let loop ((n 0)) (format #t "~A ~A\n" str n) (yield) (loop (1+ n))))) ;;; Create two threads, and start them running. (fork (do-stuff-n-print "This is AAA")) (fork (do-stuff-n-print "Hello from BBB")) (thread-exit)
Again, thanks to Eric Cooper for above nice example. linas ( talk) 15:50, 22 August 2009 (UTC)
Hey, linus, that's a really great example! It showed me exactly how continuations might be used. Just a small point though, shouldn't ((dequeue)) be ((dequeue) nil) so that the continuation get's its argument? On my implementation, at least, the enqueued continuation requires an argument to use as its return-value. -- Kristianlm ( talk) 06:49, 10 March 2012 (UTC)
Another classic example of the usefulness of continuations is the implementation of exception handlers (throw, catch). I think this would be an excellent addition to this article: so, for example, both Java and C++ implement exceptions, but they do it in a hacky, non-general kind of way. By contrast, it should become very simple if instead one had general-purpose continuations. linas ( talk) 17:11, 23 August 2009 (UTC)
This article says "In any language which supports closures, it is possible to write programs in continuation passing style and manually implement call/cc."
Well, Lua supports closures, so can anyone give an example on how to implement call/cc in Lua? I'm not talking about Lua's coroutine table. I already know that exists, plus that's for coroutines, which if I understand correctly, can also be implemented using call/cc but they themselves are not call/cc. Nothingist ( talk) 12:52, 12 May 2010 (UTC)
A continuation reifies the program control state, i.e. the continuation is a data structure that represents the computational process at a given point in the process's execution; the created data structure can be accessed by the programming language, instead of being hidden in the runtime environment.
I wouldn't exactly call this a welcome matt. — MaxEnt 15:00, 3 August 2017 (UTC)
Hello fellow Wikipedians,
I have just modified 2 external links on Continuation. Please take a moment to review my edit. If you have any questions, or need the bot to ignore the links, or the page altogether, please visit this simple FaQ for additional information. I made the following changes:
When you have finished reviewing my changes, you may follow the instructions on the template below to fix any issues with the URLs.
This message was posted before February 2018.
After February 2018, "External links modified" talk page sections are no longer generated or monitored by InternetArchiveBot. No special action is required regarding these talk page notices, other than
regular verification using the archive tool instructions below. Editors
have permission to delete these "External links modified" talk page sections if they want to de-clutter talk pages, but see the
RfC before doing mass systematic removals. This message is updated dynamically through the template {{
source check}}
(last update: 5 June 2024).
Cheers.— InternetArchiveBot ( Report bug) 16:13, 12 August 2017 (UTC)
I found that MoarVM (Perl 6) has Single-shot continuations ( https://www.moarvm.org/features.html). And later I found paper "Representing Control in the Presence ofOne-Shot Continuations" ( https://www.cs.indiana.edu/~dyb/pubs/call1cc.pdf). It is too heavy for me so I would appreciate some extension of the article if it make sense. Or at least could be MoarVM (Perl 6) added to the list? Thank you. -- mj41 ( talk) 09:40, 2 August 2019 (UTC)
This article is rated C-class on Wikipedia's
content assessment scale. It is of interest to the following WikiProjects: | ||||||||||||||||||
|
To-do list for Continuation:
|
A continuation is a function (that takes an argument) used to explicitly model control flow. You can also observe that in various other articles such as those on continuation-passing style. Because continuations often (call-cc/delimited) can capture a program state the author now seems to inadvertently believe that that is the defining feature of a continuation, it isn't, it's about explicitly modelling control flow not capturing program state. — Preceding unsigned comment added by 2A02:A467:CCD6:1:940D:DBF8:92F5:3D86 ( talk) 19:44, 23 February 2022 (UTC)
I really would have to question whether C# supports continuations in any meaningful form. A C# delegate is little more than a function pointer. (See [1]) MAllerton 19:14, 18 July 2005 (UTC)
IEnumerable CF(int max) { for(int i=0; i < max; ++i ) { yield return i; } }
The article continuation states that there is more than one kind of continuation as follows:
Support for continuations varies widely. A programming language supports re-invocable continuations if a continuation may be invoked repeatedly (even after it has already returned1). Re-invocable continuations were introduced by Peter Landin using his J (for Jump) operator that could perform a nonlocal goto into the middle of a procedure invocation. Re-invocable continutations have also been called "re-entrant" in the MzScheme programming language. However this use of the term "re-entrant" is too easily confused with its use in discussions of multitasking.
At one time Sussman and McDermott thouught that using re-invocable continutations (which they called "Hairy Control Structure") was the solution to the AI control structure problems that had originated in Planner. Carl Hewitt et. al. developed message passing as an alternative solution in the Actor model. Guy Steele and Gerry Sussman then developed in the continuations in Scheme in their attempt to understand the Actor model.
A more limited kind is the escape continuation that may be used to escape the current context to a surrounding one. Many languages which do not explicitly support continuations support
exception handling, which is equivalent to escape continuations and can be used for the same purposes. C's
setjmp
and
longjmp
are also equivalent: they can only be used to unwind the stack. Escape continuations can also be used to implement
tail-call optimization.
-- Carl Hewitt 04:30, 25 August 2005 (UTC)
Python doesn't have an explicit "continuation object" which you can store and reclaim state from/to. However, in the current version of Python (2.4) you have "generators"; they are resumable functions which return a value using the "yield" statement. Does that qualify as a continuation? See [ [2]] for examples of use.-- Larry Hastings 16:46, 15 March 2006 (UTC)
I've added the scheme example from Tim Peters's linked posting. This article (an continuations in general) didn't make any sense until I read a concrete example. —The preceding unsigned comment was added by 14:38, 10 July 2006 ( talk • contribs) Gene Thomas.
The article currently contains the following paragraph:
I do not see why the closures would have to be anonymous as it is obviously possible (albeit tedious) to name all continuations. Also, while you can program using CPS in every language with closures, I am not sure whether this really is practical without guaranteed tail-call elimination, so I think this should at least be mentioned. — Tobias Bergemann 08:32, 15 August 2006 (UTC)
I don't think the initial examples should be in Scheme. I don't think this is the best for understanding for the vast majority of programmers. I myself program for a living, and I totally balked at the Scheme intro, not grasping one single thing of what it tries to explain (The same goes for Closure (computer science), using Lisp). If one look at Coroutines, this starts off with some nice pseudocode, immediately understandable for anyone having the slightest interest in computer languages, adaptable to one's preferred universe. —Preceding unsigned comment added by 84.208.115.107 ( talk • contribs)
Although I am not a Scheme user (I studied it years ago, but for work, I write mostly in Python) I think that ideas like this are better illustrated with Scheme or Lisp than with C-ish languages. A Lisp-oid language shows explicitly where the lexical contours are. In a subject such as continuations, this is essential. -- FOo 03:03, 18 September 2006 (UTC)
Piet Delport removed perl with a bogus explanation (the entry refered to Coro, not Coro::Cont, and both Coro and Coro::Cont provide continuations and not just generators). —Preceding unsigned comment added by 85.180.123.88 ( talk • contribs)
getcontext
/swapcontext
do).call/cc
, and this is exactly why it does not belong on this page: if it did, everything in the
Coroutine and
Generator articles would too.call/cc
, or Scheme?setcontext
/getcontext
. In fact, there are language implementations that use setcontext
/getcontext
not only for that, but for all their first-class continuation support. (
GNU Guile, for example
[3])Coro::State
or (as far as i can tell from the source code and documentation) anything else in the Coro module. (It allows you to capture/resume the current coroutine, but not any of the (outer, exception-handling) continuations entailed by it.)call/cc
need not make any guarantee about the kind or extent ("repeatability") of the continuations obtained by it (unless you're talking about specific definitions, like the one in R5RS
[4] (which has no bearing on anything outside of Scheme, of course)). Many non-Scheme languages with call/cc
(and some Scheme implementations) place necessary restrictions on the extent of the continuations obtained in certain contexts: for example, when mixtures of C and high-level stack frames are involved.Coro::State
comes with a disclaimer saying "This module has not yet been extensively tested, but works on most platforms. Expect segfaults and memleaks", and that it does not seem to have much, if any, actual users. --
Piet Delport
08:41, 1 October 2006 (UTC)There is an article on Fiber (computer science). Fibers appear to be very similar to continuations. If they are the same, perhaps the articles should be merged. If they are not the same, then perhaps a reference and some distinctions should be added to the article. Kevin99 18:04, 21 February 2007 (UTC)
I've redirected to here. If anyone who understands them could add a section, that'd be good. -- Gwern (contribs) 13:14 4 July 2007 (GMT)
Does anyone have a reference for the statement in the article that Steve Russell implemented continuations in his second Lisp interpreter? Kleg 20:01, 31 July 2007 (UTC)
The section states that there's no consensus yet, however, after looking at the linked argument against, it's pretty clear that the opposition to continuations is a result of a fairly gross misunderstanding of the nature and working of continuations, the argument being, roughly, "let's not bother with continuations, instead let's devise a mechanism which would let us store enough of the state to resume the computation at any point", which is exactly what continuations are. The first linked blog post even goes as far as citing the fact that continuations can supposedly break if the user submits a form multiple times, showing utter confusion and completely missing the fact that preventing that is the very reason continuations are used for web programming.
Given that both the pros and cons arguments linked are fairly insubstantial (just random blogs), I'd propose either either expanding that section into a proper explanation of exact nature of continuations as applied to the web programming, or entirely removing the mention of supposed lack of consensus, as it lacks any serious backup in well-researched publications. mathrick 22:48, 2 October 2007 (UTC)
Though Dan Friedman is certainly a fan of call/cc, and talks about continuations often, and several chapters of the Essentials of Programming Languages are dedicated to the Continuation Passing Style, he didn't come up with it. Sussman and Steele's original Scheme, as detailed in the lambda papers, had an operator comparable to call/cc. Even so, they didn't come up with it either, exactly, though their Rabbit compiler was the first to convert programs to the continuation passing style as part of the compiliation. This site suggests that Landin may have come up with it, though the site's author is not sure.
The concept of a contination, as opposed to operators such as call/cc that allow one to grab a continuation, has an obscure origin in the early days of computing. I don't really know many details here. 66.170.46.244 ( talk) 12:55, 29 June 2008 (UTC)
Wow, that's a heavy article. I'm a computer scientist and I had a hard time grasping the summary. 81.15.57.130 ( talk) 09:48, 11 September 2008 (UTC)
During email discussions, Eric Cooper at CMU offered up the below very simple, very elegant example of using call/cc to implement coroutines/threads. I post it here mostly because I think its a very nice programming Design pattern (computer science), combining a queue with call/cc to make it all work. By comparison, googling "coroutine scheme" returns some truly horrid, mind-bendingly bad examples.
This example provides a very nice illustration of "what the heck are continuations good for?" -- because, without examples of their utility, they seem somewhat abstract and pointless. I'd like to see this example in some article somewhere, but not sure which article: this example, though very brief, is still longer than most.
;;; A naive queue for thread scheduling. ;;; It holds a list of continuations "waiting to run". (define *queue* '()) (define (empty-queue?) (null? *queue*)) (define (enqueue x) (set! *queue* (append *queue* (list x)))) (define (dequeue) (let ((x (car *queue*))) (set! *queue* (cdr *queue*)) x)) ;;; This starts a new thread running (proc). (define (fork proc) (call/cc (lambda (k) (enqueue k) (proc)))) ;;; This yields the processor to another thread, if there is one. (define (yield) (call/cc (lambda (k) (enqueue k) ((dequeue))))) ;;; This terminates the current thread, or the entire program ;;; if there are no other threads left. (define (thread-exit) (if (empty-queue?) (exit) ((dequeue))))
;;; The body of some typical Scheme thread that does stuff: (define (do-stuff-n-print str) (lambda () (let loop ((n 0)) (format #t "~A ~A\n" str n) (yield) (loop (1+ n))))) ;;; Create two threads, and start them running. (fork (do-stuff-n-print "This is AAA")) (fork (do-stuff-n-print "Hello from BBB")) (thread-exit)
Again, thanks to Eric Cooper for above nice example. linas ( talk) 15:50, 22 August 2009 (UTC)
Hey, linus, that's a really great example! It showed me exactly how continuations might be used. Just a small point though, shouldn't ((dequeue)) be ((dequeue) nil) so that the continuation get's its argument? On my implementation, at least, the enqueued continuation requires an argument to use as its return-value. -- Kristianlm ( talk) 06:49, 10 March 2012 (UTC)
Another classic example of the usefulness of continuations is the implementation of exception handlers (throw, catch). I think this would be an excellent addition to this article: so, for example, both Java and C++ implement exceptions, but they do it in a hacky, non-general kind of way. By contrast, it should become very simple if instead one had general-purpose continuations. linas ( talk) 17:11, 23 August 2009 (UTC)
This article says "In any language which supports closures, it is possible to write programs in continuation passing style and manually implement call/cc."
Well, Lua supports closures, so can anyone give an example on how to implement call/cc in Lua? I'm not talking about Lua's coroutine table. I already know that exists, plus that's for coroutines, which if I understand correctly, can also be implemented using call/cc but they themselves are not call/cc. Nothingist ( talk) 12:52, 12 May 2010 (UTC)
A continuation reifies the program control state, i.e. the continuation is a data structure that represents the computational process at a given point in the process's execution; the created data structure can be accessed by the programming language, instead of being hidden in the runtime environment.
I wouldn't exactly call this a welcome matt. — MaxEnt 15:00, 3 August 2017 (UTC)
Hello fellow Wikipedians,
I have just modified 2 external links on Continuation. Please take a moment to review my edit. If you have any questions, or need the bot to ignore the links, or the page altogether, please visit this simple FaQ for additional information. I made the following changes:
When you have finished reviewing my changes, you may follow the instructions on the template below to fix any issues with the URLs.
This message was posted before February 2018.
After February 2018, "External links modified" talk page sections are no longer generated or monitored by InternetArchiveBot. No special action is required regarding these talk page notices, other than
regular verification using the archive tool instructions below. Editors
have permission to delete these "External links modified" talk page sections if they want to de-clutter talk pages, but see the
RfC before doing mass systematic removals. This message is updated dynamically through the template {{
source check}}
(last update: 5 June 2024).
Cheers.— InternetArchiveBot ( Report bug) 16:13, 12 August 2017 (UTC)
I found that MoarVM (Perl 6) has Single-shot continuations ( https://www.moarvm.org/features.html). And later I found paper "Representing Control in the Presence ofOne-Shot Continuations" ( https://www.cs.indiana.edu/~dyb/pubs/call1cc.pdf). It is too heavy for me so I would appreciate some extension of the article if it make sense. Or at least could be MoarVM (Perl 6) added to the list? Thank you. -- mj41 ( talk) 09:40, 2 August 2019 (UTC)