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
8
votes
1 answer

Right way to compose continuation and state monad transformers

I have primitive interpreter written in haskell. This interpreter can correctly handle return statements (see my previous question). Now I want to add global state to my interpreter. This state could be changed from global code or from function's…
sergeyz
  • 1,308
  • 2
  • 14
  • 23
8
votes
2 answers

Can we describe Clojure's core.async as 'continuation passing style'?

In Clojure's core.async library we see a macro that creates a state machine that wraps around go blocks to create channels that handle blocking IO. This appears to be modelling on C#'s async and on Go-lang's goroutines. In The Seasoned Schemer -…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
8
votes
1 answer

How is callCC implemented in strict functional languages?

Consider the following example in Haskell of a function quux along with the definitions of the continuation monad and callCC. instance Monad (Cont r) where return x = cont ($ x) s >>= f = cont $ \c -> runCont s $ \x -> runCont (f x)…
user782220
  • 10,677
  • 21
  • 72
  • 135
8
votes
1 answer

difference between closures and continuations

Can someone please explain the difference between closures and continuations? The corresponding articles in wikipedia do not really compare the differences between the two.
JRR
  • 6,014
  • 6
  • 39
  • 59
8
votes
2 answers

Inferring result type in continuations

Is it possible to remove some types from the following code: import util.continuations._ object TrackingTest extends App { implicit def trackable(x: Int) = new { def tracked[R] = shift { cf: (Int => (R, Set[Int])) => cf(x) match { …
miah
  • 8,190
  • 2
  • 24
  • 23
7
votes
4 answers

Simplest example of backwards continuations in Scheme without explicit mutation

I've written a small Scheme interpreter in C#, and realised that the way I had implemented it, it was very easy to add support for proper continuations. So I added them... but want to "prove" that they way that I've added them is correct. My Scheme…
Paul Hollingsworth
  • 13,124
  • 12
  • 51
  • 68
7
votes
1 answer

callstack? retainstack? namestack?

This page of the Factor manual talks about these types of stacks stored in continuations: datastack retainstack callstack namestack catchstack What precisely do these stacks hold? The three most confusing for me are the callstack, retainstack and…
Pubby
  • 51,882
  • 13
  • 139
  • 180
7
votes
3 answers

Formal definition of Scala continuations

There are some questions about what Scala continuations are (here and here). But the answers only try to explain it. So in this question I'm asking for a formal definition of what (Scala's) delimited continuations are. I don't require an example…
ziggystar
  • 28,410
  • 9
  • 72
  • 124
7
votes
1 answer

Play's continuations holding http threads

We have implemented a feature in our web app that updates the GUI in response to new events in the server by using Play's continuations, just like a chat app. After running it for some time in production we started to experience server hangs, more…
7
votes
1 answer

Can one simplify the Codensity monad on Maybe?

The codensity monad on a type constructor f is defined by: newtype C f a = C { unC ∷ forall r. (a → f r) → f r } It is well known that C f is a monad for any type constructor f (not necessarily covariant). The codensity monad is useful in several…
winitzki
  • 3,179
  • 24
  • 32
7
votes
1 answer

How is Prolog `shift`/`reset` like other languages?

I found an example of shift-reset delimited continuations in Haskell here: resetT $ do alfa bravo x <- shiftT $ \esc -> do charlie lift $ esc 1 delta lift $ esc 2 return 0 zulu x This…
Lynn
  • 10,425
  • 43
  • 75
7
votes
3 answers

Understanding continuations in JavaScript

I'm trying to solve the last exercise of this JavaScript Closure Tutorial which takes about Continuation Passing. This is the exercise: Define a function named bothC similar to seqC that takes functions fC and gC and continuations success and…
helpermethod
  • 59,493
  • 71
  • 188
  • 276
7
votes
2 answers

How could I implement an early return from outside the body of a method in Scala?

Disclaimer: Before someone says it: yes, I know it's bad style and not encouraged. I'm just doing this to play with Scala and try to learn more about how the type inference system works and how to tweak control flow. I don't intend to use this code…
Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234
7
votes
1 answer

Complex Continuation in F#

All of the continuation tutorials I can find are on fixed length continuations(i.e. the datastructure has a known number of items as it is being traversed I am implementing DepthFirstSearch Negamax(http://en.wikipedia.org/wiki/Negamax) and while the…
Snark
  • 1,664
  • 14
  • 27
7
votes
1 answer

How to define exceptions in scheme using a dynamic variable and abortive control?

I am having trouble implementing exceptions (handle and raise) using a dynamically scoped variable and abort. This question came from reading the paper A syntactic theory of dynamic binding, section 6 figure 7. What I have attempted seems to work…
river
  • 1,028
  • 6
  • 16