Questions tagged [continuation-passing]

In functional programming, continuation-passing style (CPS) is a style of programming in which control is passed explicitly in the form of continuation function(s).

In CPS, in addition to receiving arguments as usual, each function also receives an additional function to be used as a continuation, so that instead of being returned as usual, a value is rather passed to this continuation function, as the function's final act.

Continuation functions are not expected to return in a normal sense, but rather return their values in the same manner, by further calling other continuation functions.

A computation either simply calls its continuation function, or constructs a new, more complex one, to express more complex patterns of computation, like recursion, or iteration.

Sometimes more than one continuation are used for each call, as e.g. with two continuations, one for a "successful" computation, another for a "failed" one.

Example:

nlog(n, x, onSuccess, onFailure) =            (* iterated logarithm *)
     onFailure(x)                      , if x <= 0 or n < 0
     onSuccess(x)                      , if n == 0
     onSucess(log(x))                  , if n == 1
     nlog(n-1, x, 
           x => onSuccess(log(x)),            (* new success continuation constructed *)
           onFailure                          (* same failure continuation *)
           )                           , otherwise
164 questions
4
votes
3 answers

Continuation Passing Style (CPS) during graph construction

I am working on a library for subdivision surfaces. In order to represent the mesh topology I'm using a kind of split-vertex lath data structure (see the diagram on the left side). During the construction of a mesh, that also can be seen as a…
LambdaStaal
  • 537
  • 2
  • 9
4
votes
1 answer

How to write Functor instance of Continuation Monad?

newtype Cont k a = Cont { runCont :: (a -> k) -> k } instance Functor (Cont k) where -- fmap :: (a -> b) -> (Cont k a) -> (Cont k b) fmap f (Cont akTok) = Cont $ ??? My doubts: We can only write Functor instance to any data type that can…
Pawan Kumar
  • 1,443
  • 2
  • 16
  • 30
4
votes
1 answer

Continuation passing style - function composition

I'm learning about CPS with Racket, and I've managed to write up these functions: ;lift a regular single-arg function into CPS (define (lift/k f) (lambda (x k) (k (f x)))) ;compose two CPS functions (define (compose/k f g) (lambda (x k) …
Dan Burton
  • 53,238
  • 27
  • 117
  • 198
4
votes
1 answer

When to use foldr with a continuation as an accumulation function?

There is a technique I've seen a few times with foldr. It involves using a function in place of the accumulator in a foldr. I'm wondering when it is necessary to do this, as opposed to using an accumulator that is just a regular value. Most people…
illabout
  • 3,517
  • 1
  • 18
  • 39
4
votes
4 answers

Is there a way to make this continuation passing with codata example work in F#?

type Interpreter<'a> = | RegularInterpreter of (int -> 'a) | StringInterpreter of (string -> 'a) let add<'a> (x: 'a) (y: 'a) (in_: Interpreter<'a>): 'a = match in_ with | RegularInterpreter r -> x+y |> r | StringInterpreter r…
Marko Grdinić
  • 3,798
  • 3
  • 18
  • 21
4
votes
1 answer

Is call/cc a copy of a stack frame or an actual jump in execution?

I don't understand how the following does not set up an infinite loop: (define call/cc call-with-current-continuation) ; ccc alias (define return #f) ; declare a global variable 'return' (+ 1 (call/cc (lambda (cont) ; setup continuation with 'cont'…
4
votes
1 answer

nested CPS "reset"

using the CPS compiler-plugin of Scala 2.8, there are the two magic controls reset and shift. Reset delimits the continuation and shift captures the continuation. There is an example of using CPS with NIO, using nested resets as a type of…
4
votes
2 answers

Continuation passing JavaScript

Can someone please explain to me the concept of continuation passing in javascript? I'm trying to understand it by reading this http://nathansjslessons.appspot.com/lesson?id=1090 and trying to solve the exercise given, but I can't seem to solve it.…
Radu
  • 524
  • 1
  • 6
  • 19
4
votes
1 answer

Why does null.asInstanceOf[] fail?

Is there any logical reason why null.asInstanceOf[] fails to compile? For context, see this github issue thread.
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
4
votes
2 answers

Is there a way to capture the continuations in a do notation?

Since the following do block: do x <- foo y <- bar return x + y is desugared to the following form: foo >>= (\x -> bar >>= (\y -> return x + y)) aren't \x -> ... and y -> ... actually continuations here? I was wondering if there is a way to…
3
votes
1 answer

Tail recursion elimination on conditional types doesn't work

In TS 4.5 tail call optimization was added for recursive generics. The following snippet computes Fibonacci numbers (in unary) up to F12, but for F13 it fails with the usual "Type instantiation is excessively deep and possibly infinite" exception.…
3
votes
2 answers

Simplify multiway tree traversal with continuation passing style

I am fascinated by the approach used in this blog post to traverse a rose tree a.k.a multiway tree a.k.a n-ary tree using CPS. Here is my code, with type annotations removed and names changed, which I did while trying to understand the…
3
votes
2 answers

Is it possible to convert a HOAS function to continuation passing style?

Mind the following Haskell program: -- A HOAS term, non-polymorphic for simplicity data Term = Lam (Term -> Term) | App Term Term | Num Int -- Doubles every constant in a term fun0 :: Term -> Term fun0 (Lam b) = Lam (\ x -> fun0 (b x)) fun0…
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
3
votes
1 answer

Evaluation with Continuation Passing Style without the Cont Monad

I have a task: to write a function evalCPS which evaluates expressions formalized by the ADT below, using Continuation Passing Style but without Cont Monad or similar stuff. data Expr a = Expr a :+: Expr a | Expr a :/: Expr a …
3
votes
1 answer

Can the CPS-styled `call/cc` be written in terms of a hypothetical non-CPS-styled `cc'`?

In a language which supports continuation, e.g. Scheme, Ruby, and Haskell, suppose there is a function cc' which takes no argument and return the current continuation, so that the caller that obtains a continuation by calling cc' can then invoke the…
user10082400