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

How to convert CPS-style gcd computation to use the Continuation Monad

Let us consider the following implementation of the Continuation monad, for CPS-style computations yielding and integer: module Cont : sig type 'a t = ('a -> int) -> int val return : 'a -> 'a t val bind : 'a t -> ('a -> 'b t) -> 'b t val…
Adèle Blanc-Sec
  • 453
  • 2
  • 11
10
votes
1 answer

Continuation monad "interface"

The state monad "interface" class MonadState s m where get :: m s put :: s -> m () (+ return and bind) allows to construct any possible computation with State monad without using State constructor. For example, State $ \s -> (s+1, s-1) can…
sdcvvc
  • 25,343
  • 4
  • 66
  • 102
10
votes
1 answer

How come that we can implement call/cc, but the classical logic (intuitionistic + call/cc) is not constructive?

Intuitionistic logic, being constructive, is the basis for type systems in functional programming. The classical logic is not constructive, in particular the law of excluded middle A ∨ ¬A (or its equivalents, such as double negation elimination or…
Petr
  • 62,528
  • 13
  • 153
  • 317
10
votes
1 answer

How to translate shift/reset into delimcc?

I'm studying Oleg's and Asai's delimited continuations "for dummies" paper(http://pllab.is.ocha.ac.jp/~asai/cw2011tutorial/main-e.pdf) but this paper uses the shift/reset formalism instead of the prompt stuff available in Oleg's delimcc. So I have a…
10
votes
8 answers

How to do structured programming using blocks in Objective-C

When using methods which return blocks they can be very convenient. However, when you have to string a few of them together it gets messy really quickly for instance, you have to call 4 URLs in succession: [remoteAPIWithURL:url1 success:^(int…
10
votes
2 answers

How to handle Jetty exception - a long running HTTP request times out, but the process it calls never terminates and Jetty is unhappy

I have a Jetty server handling long running HTTP requests- the responses are generated by an a different process X and end up in a collector hash which Jetty requests periodically check. There are 3 cases: Process X finishes before the timeout…
nflacco
  • 4,972
  • 8
  • 45
  • 78
9
votes
1 answer

Using scala continuations with netty/NIO listeners

I'm using the Netty library (version 4 from GitHub). It works great in Scala, but I am hoping for my library to be able to use continuation passing style for the asynchronous waiting. Traditionally with Netty you would do something like this (an…
Jeremy
  • 533
  • 3
  • 10
9
votes
1 answer

How can C++ use continuation-passing style?

Suppose in C++ you're doing too many recursive calls on a recursive function and getting a stack overflow error. How would you rewrite this in a continuation-passing style to avoid the stack overflow? I'm having a slight difficulty picturing this in…
achow
  • 1,013
  • 2
  • 12
  • 20
9
votes
1 answer

What are modern continuation operators all about?

Back in the day, I thought I understood call/cc. These days I'm seeing a lot more references to "delimited" continuation operators, which seem to come in pairs like shift/reset, prompt/control, and sometimes more exotic ones. But I haven't seen a…
9
votes
3 answers

Escaping from the IO monad inside the Continuation monad

A confusing title for a confusing question! I understand a) monads, b) the IO monad, c) the Cont monad (Control.Monad.Cont), and d) the ContT continuation transformer monad. (And I vaguely understand monad transformers in general -- though not…
mgiuca
  • 20,958
  • 7
  • 54
  • 70
9
votes
2 answers

How are tail-position contexts GHC join points paper formed?

Compiling without Continuations describes a way to extend ANF System F with join points. GHC itself has join points in Core (an intermediate representation) rather than exposing join points directly in the surface language (Haskell). Out of…
9
votes
2 answers

Example of nested resets in Scala

This is a question about Scala continuations. Can resets be nested? If they can: what are nested resets useful for ? Is there any example of nested resets?
Michael
  • 10,185
  • 12
  • 59
  • 110
9
votes
3 answers

How to adapt trampolines to Continuation Passing Style?

Here is a naive implementation of a right fold: const foldr = f => acc => ([x, ...xs]) => x === undefined ? acc : f(x) (foldkr(f) (acc) (xs)); This is non-tail recursion and hence we cannot apply a trampoline. One approach would be to…
9
votes
2 answers

How to share intermediate results of continuations?

Please note that even though the example in this question is encoded in Javascript, the underlying concepts are common in Haskell and I while I prefer to express myself in Javascript I also appreciate answers in Haskell. In Javascript I use CPS to…
user5536315
9
votes
3 answers

Is continuation-style-programming prone to stack overflow

In response to this question about jQuery effects, I thought about using the callback argument to .fadeIn( 500, my_function ). While in principle, this is a viable idea, I have no clue (and neither has the jQuery documentation :( ) if the callback…
xtofl
  • 40,723
  • 12
  • 105
  • 192