Talk:Continuation-passing style

From Wikipedia, the free encyclopedia

This article is within the scope of WikiProject Computer science, which aims to create a comprehensive computer science reference for Wikipedia. Visit the project page for more information and to join in on related discussions.
Start rated as Start-Class on the assessment scale
Mid rated as Mid-importance on the assessment scale

Contents

[edit] Organization and Scope

The material beginning "programming with continuations" to the end of that section seems outside the scope of this article: it is about using continuations as a program control flow mechanism, within a program written largely in direct style, rather than about CPS per-se. I would suggest that this material be moved to its own page, possibly fleshed out, and that a pointer be made from this page saying something like "continuations are useful in less formal contexts, for instance as ways of passing and manipulating control in a GUI system. This is called Programming with Continuations. Any thoughts or objections? Barak (talk) 12:49, 25 January 2008 (UTC)

[edit] Need an example of how to call

For people not as familiar with continuations, like me, it might be good to include an example of how to call at least one of the scheme CPS examples.

For instance, I pasted the factorial example into my scheme interpreter, but I have no idea how to call it so that it prints out 120 as the answer. ( factorial 5 ??? ) what do I type for the ??? ?

I added an example about the identity function. But I think the CPS style examples (pyth, factorial) are wrong, because they dont't work. --SteloKim (talk) 06:05, 3 January 2008 (UTC)
I don't find this a very good example. Instead I will add an example of calling the CPS version of factorial, showing how to interface between code in direct style and code in CPS. This will also remove the call/cc, which is premature at that point in the article. However it would be nice to add a section on call/cc in Scheme, or perhaps better a separate article on call/cc and a pointer to it. Barak (talk) 23:50, 14 January 2008 (UTC)

The pyth CPS example isn't valid scheme, in that is will error when run. A continuation must be applied at some point for it to be run. I will update the pyth with a contrived example, say where pyth gives a non-whole number and you pass in a continuation for dealing with that case. Ozten (talk) 00:41, 24 January 2008 (UTC)

Added a verbose re-write of pyth example. Examples of CPS tend to be more verbos... pyth isn't a problem where CPS technique would shine, so I invented a requirement where you could pass in a continuation for dealing with weird error situation such as the pythagorean number being a real number instead of a whole number. Using the examples from "Scheme the Programming Language" would be better, but I guess copyright issues would prevent that. Added this book to references... Ozten (talk) 01:08, 24 January 2008 (UTC)

I backed out this change to the pyth example because it was wrong: it was no longer in CPS. There is a specific technical definition of CPS, and this did not fulfill it. Eg, it called + without a continuation, and called it in a nested fashion. (feel free to email me if you want to discuss.) Barak (talk) 09:04, 25 January 2008 (UTC)

[edit] Examples of compilers that use CPS internally

As continuation passing style renders return values virtually useless, it can also be used to eliminate the need for a runtime stack. Several compilers and interpreters for functional programming languages use this ability internally in novel ways.

Can anyone fill this in a bit with examples of specific compilers and/or interpreters that make use of CPS, and which novel ways they do so in? Ari 08:20, 26 December 2005 (UTC)

  • I believe SML/NJ uses CPS internally. It did, at least, in the mid nineties. As I recall, this also makes the implementation of CML thread much more efficient. I'll see if I can dig out some references... Brighterorange 18:25, 26 December 2005 (UTC)
  • yeah

[edit] CPS is a monad?

The article says "CPS is a monad." Can someone please make this clearer with "CPS is a monad because..." or something like that. Toby 21:48, 20 March 2006 (UTC)

Ok, I found a paper explaing how it is a monad (or rather how continuations are monads). I'll add it on the "monads in functional programming" page and clarify / correct this sentance when I get back from work later on.--NotQuiteEXPComplete 00:05, 3 August 2006 (UTC)
On second though I'll remove this comment since the statement that "CPS is a monad" is wrong. Continuations are monads, but saying this in an article on CPS doesn't make much sense. --NotQuiteEXPComplete 12:52, 3 August 2006 (UTC)

[edit] CPS as GOTO

Think about that for a moment. This certainally smells like a goto chain.

  • Yes, it is much like GOTO, except that a call to a continuation can transfer control non-locally. — brighterorange (talk) 18:53, 25 September 2006 (UTC)
  • Continuations are the functional equivalent of the GOTO statement in imperative languages. Same risk - functional spaghetti-code. Phraine 07:58, 26 December 2006 (UTC)