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
3
votes
0 answers

Why don't we use continuation passing style always?

I've been studying programming language course and I found a thing that I can't get the answer. I understood that pros about the continuation-passing-style is that we can get rid of the stack overflow by not using the stack frames, just by passing…
rokrokss
  • 137
  • 2
  • 11
3
votes
1 answer

Why doesn't my transducer work anymore when abstracting from reducing/composing?

I am stuck with my short circuiting and stack safe transducer implementation: const loop = f => { let acc = f(); while (acc && acc.type === tailRec) acc = f(...acc.args); return acc; }; const tailRec = (...args) => …
user10675354
3
votes
1 answer

isolate in SMLofNJ.Cont

I was reading about continuations in Standard ML (SMLofNJ.Cont). I understood what callcc and throw does, but could not understand isolate. The documentation says Discard all live data from the calling context (except what is reachable from f or…
him
  • 487
  • 3
  • 12
3
votes
2 answers

How to implement a stack-safe chainRec operator for the continuation monad?

I am currently experimenting with the continuation monad. Cont is actually useful in Javascript, because it abstracts from the callback pattern. When we deal with monadic recursion, there is always the risk of a stack overflow, because the recursive…
3
votes
2 answers

Can a convolution function written in tail recursive form?

I have a function that I want to write in tail recursive form. The function calculates the number of ways to get the sum of k by rolling an s sided die n times. I have seen the mathematical solution for this function on this answer. It is as…
3
votes
1 answer

Proving the Continuation Passing Style Monad in Coq

I'm trying to prove the Monad laws (left and right unit + associativity) for the Continuation Passing Style (CPS) Monad. I'm using a Type Class based Monad defintion from https://coq.inria.fr/cocorico/AUGER_Monad: Class Monad (m: Type -> Type): Type…
larsr
  • 5,447
  • 19
  • 38
3
votes
3 answers

Tail Recursion in F# : Stack Overflow

I'm trying to implement Kosaraju's algorithm on a large graph as part of an assignment [MOOC Algo I Stanford on Coursera] https://en.wikipedia.org/wiki/Kosaraju%27s_algorithm The current code works on a small graph, but I'm hitting Stack Overflow…
3
votes
0 answers

Using bind to clear the call stack during a CPS computation

EDIT: This question of mine has shown out to be quite ill-posed. I still believe that, especially going through the comments, it may provide some food for thoughts, but if enough votes will be reached to have it closed, I'll understand. During some…
Marco Faustinelli
  • 3,734
  • 5
  • 30
  • 49
3
votes
2 answers

How do I wrap a chained stateful computation in State monad?

I have computations in this format: s -> a -> s, where s is the type of some state. The result of such a function is also the state of the next evaluation. For example, appendInt :: String -> Int -> String appendInt s i = s ++ (show i) Then,…
3
votes
1 answer

Erlang implementing an amb operator.

On wikipedia it says that using call/cc you can implement the amb operator for nondeterministic choice, and my question is how would you implement the amb operator in a language in which the only support for continuations is to write in continuation…
user12596
  • 71
  • 2
3
votes
3 answers

Is this definition of a tail recursive fibonacci function tail-recursive?

I've seen around the following F# definition of a continuation-passing-style fibonacci function, that I always assumed to be tail recursive: let fib k = let rec fib' k cont = match k with | 0 | 1 -> cont 1 | k -> fib' (k-1) (fun a ->…
3
votes
0 answers

Haskell Optimization Failure

I have the following haskell function written in continuation-passing style: import Data.Bits ((.|.), shiftR) nextPowerOf2 :: Int -> Int nextPowerOf2 0 = 1 nextPowerOf2 x = (go $ go $ go $ go $ go $ \y m -> y + 1) (x - 1) 1 where go k y m = k…
user1502040
  • 451
  • 3
  • 13
3
votes
2 answers

Changing a function into CPS style

We were asked to write a procedure that when given a list it will replace the first occurrence of a given element and only the first, but the catch is to write in CPS style. We are unable to turn it to CPS style written procedure that is given a…
Rachel Bernouli
  • 225
  • 2
  • 9
2
votes
1 answer

How to write analyzer/evaluator functions like `eval-if` in CPS form?

I'm trying to write a toy python Scheme interpreter based on the meta-circular evaluator in SICP. Since python only supports a call stack of limited depth, I have to eliminate the tail calls. I read about trampolines and implemented the parser with…
pjhades
  • 1,948
  • 2
  • 19
  • 34
2
votes
3 answers

How To Write This In CPS?

I'm trying to master continuation passing style (CPS) and am therefore reworking an example shown to me by Gary Short quite a while ago. I don't have his sample source code so I'm trying to rework his example from memory. Consider the following…
Onorio Catenacci
  • 14,928
  • 14
  • 81
  • 132