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

ANF conversion in continuation passing style

An algorithm for converting code from S-expressions to A-normal form is given at http://matt.might.net/articles/a-normalization/ The author has made the unusual choice of writing the algorithm in continuation passing style. (That is, it's not that…
rwallace
  • 31,405
  • 40
  • 123
  • 242
2
votes
1 answer

heap usage for CPS vs non-CPS parsers in Haskell's parsec

I'm trying to write the following parser using parsec: manyLength :: forall s u m a. Monad m => ParsecT s u m a -> ParsecT s u m Int manyLength p = go 0 where go :: Int -> ParsecT s u m Int go !i = (p *> go (i + 1)) <|> pure…
illabout
  • 3,517
  • 1
  • 18
  • 39
2
votes
1 answer

In F#, how can I turn a list of continuations into a continuation that takes a list?

First some context: I'm playing around with KFoldTree from this excellent blog post. I have a tree structure that is n-ary instead of binary. With a binary tree, applying the CPS transform to the function you pass to KFoldTree is not too hard. You…
2
votes
2 answers

Intermediate and return values in continuation-passing style

I am coming from a OOP, non-functional background, so I am having trouble fully visualizing several online examples regarding continuation passing. Also, functional languages like Scheme don't have to specify types of arguments or return values, so…
Lou
  • 4,244
  • 3
  • 33
  • 72
2
votes
0 answers

Transpiling language constructs to z3

I am looking for a discussion of how to convert various programming language constructs to Z3, without limiting the efficiency of the solver. In particular, is there an algorithm and a set of best-practice rules for converting a function/program…
2
votes
2 answers

Continuation-passing style in Scala

I have superficially read a couple of blog articles/Wikipedia about continuation-passing style. My high-level goal is to find a systematic technique to make any recursive function (or, if there are restrictions, being aware of them) tail-recursive.…
Dici
  • 25,226
  • 7
  • 41
  • 82
2
votes
0 answers

Haskell Continuations for inversion of control in the wild?

Continuations are interesting. Especially interesting is how they encompass all other monads. Also, I hear they are used a lot for optimizing. One thing I hear they could be used for is inversion of control in things. Like, gui's and stuff. I am…
PyRulez
  • 10,513
  • 10
  • 42
  • 87
2
votes
2 answers

Persistent expression templates with unique_ptr and matrices

I want to use expression templates to create a tree of objects that persists across statement. Building the tree initially involves some computations with the Eigen linear algebra library. The persistent expression template will have additional…
2
votes
1 answer

How do I convert the following functions in JavaScript to continuation passing style (CPS)?

Suppose I have the following functions: function f(x) { return x + 1; } function g(x) { return x * 2; } function h() { return 5; } How could I convert the expression f(g(h())) into continuation passing style? I know that h is transformed…
cybertextron
  • 10,547
  • 28
  • 104
  • 208
2
votes
0 answers

Order of function application

I am currently trying to figure out in which order Haskell tries to applicate certain functions. If I've got a function call like: exec:: Stm -> State -> State exec (Seq s1 s2) = \s -> exec s2 (exec s1 s) This is part of a bigger program executing…
Sushiman
  • 55
  • 6
2
votes
1 answer

Continuation passing style code in Objective C

Can we write Continuation passing style code in Objective C? If yes, could you please give some examples?
2
votes
2 answers

Exception handling around shift in Scala

Scala 2.10.2. Running import util.continuations._ import concurrent.ops._ object Main { def main(args: Array[String]) { reset { try { shift { cont: (Unit => Unit) => { spawn { …
Yuri Geinish
  • 16,744
  • 6
  • 38
  • 40
2
votes
1 answer

Inconsistent behavior in Scala CPS framework

I'm trying to build a coroutine framework to enable batch data fetching by stepping through each data-dependent function in parallel. Here is what I have so far: http://pastie.org/7147798 This doesn't work def get(id: Long) = reset { // Is it not…
2
votes
2 answers

JavaScript: Direct code vs CPS styled generated code performance comparison

In my application i am generating JavaScript code which is following CPS style. I am 'not' using any 'continuations' as such. No async behavior, No pause and resume, and No callbacks. Just that the code is following a continuation passing style of…
weima
  • 4,653
  • 6
  • 34
  • 55
2
votes
1 answer

my CPS is right?

in "The Scheme Programming Language 4th Edition", there is a example as below: (define product (lambda (ls) (call/cc (lambda (break) (let f ([ls ls]) (cond [(null? ls) 1] [(= (car ls) 0) (break…
abelard2008
  • 1,984
  • 1
  • 20
  • 35