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

How does this continuation-passing style Clojure function generator work?

This is from the Joy of Clojure, 2nd Edition. http://www.manning.com/fogus2/ (defn mk-cps [accept? kend kont] (fn [n] ((fn [n k] (let [cont (fn [v] (k ((partial kont v) n)))] (if (accept? n) (k 1) …
mparaz
  • 2,049
  • 3
  • 28
  • 47
10
votes
1 answer

Continuations and for comprehensions -- what's the incompatibility?

I am new to Scala and trying to wrap my head around continuations I'm trying to reproduce the yield return C# statement. Following this post, I have written the following code : package com.company.scalatest import…
GuiSim
  • 7,361
  • 6
  • 40
  • 50
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
4 answers

How to use C# async/await as a stand-alone CPS transform

Note 1 : Here CPS stands for "continuation passing style" I would be very interested in understanding how to hook into C# async machinery. Basically as I understand C# async/await feature, the compiler is performing a CPS transform and then passes…
9
votes
3 answers

CPS in curried languages

How does CPS in curried languages like lambda calculus or Ocaml even make sense? Technically, all function have one argument. So say we have a CPS version of addition in one such language: cps-add k n m = k ((+) n m) And we call it like (cps-add…
Zorf
  • 6,334
  • 2
  • 29
  • 24
9
votes
4 answers

Define fix-point combinator in Continuation Passing Style

The fix-point combinators are very useful tools to introduce recursion. The Continuation-Passing style is a style of lambda calculus where functions never return. Instead you pass the rest of your program as a lambda argument into your function and…
9
votes
2 answers

What exactly are administrative redexes after CPS conversion?

In the context of Scheme and CPS conversion, I'm having a little trouble deciding what administrative redexes (lambdas) exactly are: all the lambda expressions that are introduced by the CPS conversion only the lambda expressions that are…
eljenso
  • 16,789
  • 6
  • 57
  • 63
8
votes
2 answers

Convert to CPS (Continuation Passing Style)

How do I convert these procedures in Scheme to CPS form? (lambda (x y) ((x x) y)) (lambda (x) (lambda (f) (f (lambda (y) (((x x) f) y)))) ((lambda (x) (x x) (lambda (x) (x x)) *This is not any homework!
Eran Egozi
  • 775
  • 1
  • 7
  • 18
8
votes
2 answers

How do collector functions work in Scheme?

I am having trouble understanding the use of collector functions in Scheme. I am using the book "The Little Schemer" (by Daniel P. Friedman and Matthias Felleisen). A comprehensive example with some explanation would help me massively. An example of…
CPUFry
  • 566
  • 4
  • 18
8
votes
2 answers

What good is the monad instance of Cont?

I'm playing around with CPS and Control.Monad.Cont and wonder what we gain by noticing the monadic structure. For code like this: sumOfSquares'cps :: Cont r Int -> Cont r Int -> Cont r Int sumOfSquares'cps x y = x >>= \x' -> y…
Sebastian Graf
  • 3,602
  • 3
  • 27
  • 38
7
votes
1 answer

How do I lazily transform a list into this type?

Consider this Vect type: {-# LANGUAGE GADTs, DataKinds, KindSignatures, RankNTypes #-} import Data.Kind (Type) data Nat = Zero | Succ Nat data Vect :: Nat -> Type -> Type where Nil :: Vect 'Zero a Cons :: a -> Vect n a -> Vect ('Succ n)…
7
votes
4 answers

Ocaml continuation passing style

I'm new to ocaml and tryin to write a continuation passing style function but quite confused what value i need to pass into additional argument on k for example, I can write a recursive function that returns true if all elements of the list is even,…
REALFREE
  • 4,378
  • 7
  • 40
  • 73
6
votes
1 answer

Scala continuations: many shifts in sequence

I have been trying to wrap my head around the complex typing issues with scala continuations. I've been reading all the material I can find on it, including the reference docs on the continuations package. I think I have it figured out to some…
Jeremy
  • 533
  • 3
  • 10
6
votes
3 answers

How to implement the equivalent of Promise.all for my Task implementation?

Here is my Task implementation (i.e. a sort of Promise but complying with the monad laws and cancelable). It works rock solid: const Task = k => ({runTask: (res, rej) => k(res, rej)}); const tAp = tf => tk => Task((res, rej) =>…
6
votes
1 answer

Continuation-Passing Style in Scheme?

I ran into this code on Wikipedia: (define (pyth x y k) (* x x (lambda (x2) (* y y (lambda (y2) (+ x2 y2 (lambda (x2py2) (sqrt x2py2 k)))))))) The article says that that code is the Continuation-Passing…
user541686
  • 205,094
  • 128
  • 528
  • 886
1
2
3
10 11