Questions tagged [continuations]

In computer science and programming, a continuation is an abstract representation of the control state. A continuation reifies an instance of a computational process at a given point in the process's execution. It contains information such as the process's current stack (including all data whose lifetime is within the process e.g. "local variables"), as well the process's point in the computation.

Using continuation, when a function calls another function, that is the last thing it does. An extra argument is part of every function, which will be used to pass each function's continuation. Instead of waiting for the called function to return, it puts any work it wants to do afterwards into a continuation, which it passes to the function. Essentially, this involves breaking up the code into a collection of callback functions.

535 questions
34
votes
4 answers

scheme continuations for dummies

For the life of me, I can't understand continuations. I think the problem stems from the fact that I don't understand is what they are for. All the examples that I've found in books or online are very trivial. They make me wonder, why anyone would…
user2379016
  • 351
  • 3
  • 5
32
votes
4 answers

Constructing efficient monad instances on `Set` (and other containers with constraints) using the continuation monad

Set, similarly to [] has a perfectly defined monadic operations. The problem is that they require that the values satisfy Ord constraint, and so it's impossible to define return and >>= without any constraints. The same problem applies to many other…
Petr
  • 62,528
  • 13
  • 153
  • 317
31
votes
5 answers

C# first class continuation via C++ interop or some other way?

We have a very high performance multitasking, near real-time C# application. This performance was achieved primarily by implementing cooperative multitasking in-house with a home grown scheduler. This is often called micro-threads. In this system…
Wayne
  • 2,959
  • 3
  • 30
  • 48
31
votes
5 answers

How can two continuations cancel each other out?

I'm reading through Some Tricks for List Manipulation, and it contains the following: zipRev xs ys = foldr f id xs snd (ys,[]) where f x k c = k (\((y:ys),r) -> c (ys,(x,y):r)) What we can see here is that we have two continuations stacked…
30
votes
11 answers

Continuations in Java

Is there a good implementation of continuations in Java? If so, what is the overhead like? The JVM wasn't designed with these sort of things in mind, right? So is this kind of going against the grain?
Mike
  • 58,961
  • 76
  • 175
  • 221
29
votes
5 answers

call/cc implementation?

I'm trying to find how call/cc is implemented. The best I've found is this Haskell snippet: callCC f = Cont $ \k -> runCont (f (\a -> Cont $ \_ -> k a)) k Although this is not as simple as I want due to the Cont and runCont. I've also found…
Pubby
  • 51,882
  • 13
  • 139
  • 180
29
votes
7 answers

Continuations in Clojure

I read somewhere where rich hickey said: "I think continuations might be neat in theory, but not in practice" I am not familiar with clojure. 1. Does clojure have continuations? 2. If no, don't you need continuations? I have seen a lot of good…
unj2
  • 52,135
  • 87
  • 247
  • 375
28
votes
3 answers

Does the yin yang continuations puzzle make sense in a typed language?

This question is related to "How the yin-yang puzzle works?". The yin yang example of continuations in scheme looks like this, according to Wikipedia article: (let* ((yin ((lambda (cc) (display #\@) cc) (call-with-current-continuation (lambda…
Andrej Bauer
  • 2,458
  • 17
  • 26
27
votes
4 answers

How can the continuation monad be expressed using the free monad?

Supposedly, all monads can be expressed using Free (if this isn't true, what is a counter-example and why)? How can the continuation monad or its corresponding transformer be expressed using Free or FreeT - what would be the corresponding functor?…
Petr
  • 62,528
  • 13
  • 153
  • 317
27
votes
2 answers

Implementing yield (yield return) using Scala continuations

How might one implement C# yield return using Scala continuations? I'd like to be able to write Scala Iterators in the same style. A stab is in the comments on this Scala news post, but it doesn't work (tried using the Scala 2.8.0 beta). Answers in…
Yang
  • 16,037
  • 15
  • 100
  • 142
26
votes
2 answers

Why continuation passing style

In The Scheme Programming Language by Kent Dybvig (4th edition) section 3.4, he describes very clearly what continuation passing style is. For the why he gives two reasons: pass more than one result to its continuation, because the procedure that…
Harry Spier
  • 1,395
  • 16
  • 30
25
votes
2 answers

How do I enable continuations in Scala?

Question says it all. (Yet, the details of how to get access to the shift and reset operations has changed over the years. Old blog entries and Stack Overflow answers may have out of date information.) See also What are Scala continuations and why…
Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
23
votes
7 answers

Is it possible to programmatically construct a Python stack frame and start execution at an arbitrary point in the code?

Is it possible to programmatically construct a stack (one or more stack frames) in CPython and start execution at an arbitrary code point? Imagine the following scenario: You have a workflow engine where workflows can be scripted in Python with…
23
votes
2 answers

Don't understand the typing of Scala's delimited continuations (A @cpsParam[B,C])

I'm struggling to understand what precisely does it mean when a value has type A @cpsParam[B,C] and what types of this form should I assign to my values when using the delimited continuations facility. I've looked at some…
jkff
  • 17,623
  • 5
  • 53
  • 85
22
votes
2 answers

How to make callCC more dynamic?

I thought the right type for ContT should be newtype ContT m a = ContT {runContT :: forall r. (a -> m r) -> m r} and other control operators shift :: Monad m => (forall r. (a -> ContT m r) -> ContT m r) -> ContT m a reset :: Monad m => ContT m a ->…
bobzhang
  • 1,771
  • 3
  • 17
  • 19
1
2
3
35 36