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
1
vote
1 answer

How Kotlin coroutine internals decide should function be suspended or result can be returned immediately?

As I understand suspend function transforms to function with additional Continuation parameter and when block from suspendCoroutineUninterceptedOrReturn returning COROUTINE_SUSPENDED value - suspend fun suspends. But how Kotlin coroutine internals…
mobiledev Alex
  • 2,228
  • 2
  • 28
  • 30
1
vote
1 answer

Why does converting the factorial function to iterative form by defunctionalizing the continuation give such a bad result?

I'm trying the "defunctionalize the continuation" technique on some recursive functions, to see if I can get a good iterative version to pop out. Following along with The Best Refactoring You've Never Heard Of (in Lua just for convenience; this…
1
vote
1 answer

Full CPS transformation viability when using truffle graalvm?

I'm working on an interpreter, and just recently discovered graal truffle, which promises fast performance if I use it to implement the interpreter. However, from what I can the mileage varies on the interpreter's code and how easily compiler can…
Coderino Javarino
  • 2,819
  • 4
  • 21
  • 43
1
vote
1 answer

How to make Koen Claessen's concurrency monad with pure variables?

It's known how to make a pure concurrency monad based on ContT, based on a functional pearl of Koen Claessen: data Action m where Atom :: m (Action m) -> Action m Fork :: [Action m] -> Action m Stop :: Action m fork :: Applicative m => [ContT…
Turion
  • 5,684
  • 4
  • 26
  • 42
1
vote
1 answer

Can we determine function "k", if we know k(l-1)=f(l) holds for all function f of type int->int and l of type int?

Suppose both k and f are functions of type int -> int. If we know k(l-1)=f(l) holds for all l of type int, can we be certain that k is the function v->f(v+1) ? I have this question while doing an exercise of functional programming: to transform a…
zell
  • 9,830
  • 10
  • 62
  • 115
1
vote
1 answer

In scala continuation, how to write a loop in CPS form?

I'm trying to implement an example at: https://portal.klewel.com/watch/webcast/scala-days-2019/talk/37/ using scala continuation: object ReverseGrad_CPSImproved { import scala.util.continuations._ case class Num( x: Double, var d:…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
1
vote
1 answer

How do you pass arrays between pages without a form in php

I have used multi-dimesional array from a database to bring back a row ($dataTotal) and another one to bring back a position in that row ($data['position1']) depending on if I want to display anything in the array on that first page When the user…
1
vote
1 answer

Rewrite the Ackermann function in the CPS style

Out of simple curiosity and desire to deepen my understanding of the CPS style (continuation passing style), I would like to know if there is a way to rewrite this function according to this scheme. The difficulty probably lies in the design of the…
Foxy
  • 980
  • 8
  • 17
1
vote
1 answer

Hints About Exercise 6.31 of EOPL3

I am reading EOPL 3rd edition. Now I am stuck on exercise 6.31. Exercise 6.31 Write a translator that takes the output of cps-of-program and produces an equivalent program in which all the continuations are represented by data structures, as in…
1
vote
1 answer

Is a function in CPS required to make a tail call?

I saw an example from The Scheme Programming Language, which converts (letrec ([f (lambda (x) (cons 'a x))] [g (lambda (x) (cons 'b (f x)))] [h (lambda (x) (g (cons 'c x)))]) (cons 'd (h '()))) (d b a c) into CPS (letrec ([f…
user10082400
1
vote
2 answers

How can I write `(if (null? x) (quote ()) (cdr x))` in CPS?

The Scheme Programming Languages says Thus, at any point during the evaluation of any expression, there is a continuation ready to complete, or at least continue, the computation from that point. Let's assume that x has the value (a b c). We…
user10082400
1
vote
2 answers

Can the function argument to `call/cc` equivalently either invoke the continuation or return without invoking the continuation?

The Scheme Programming Language says Scheme allows the continuation of any expression to be captured with the procedure call/cc. call/cc must be passed a procedure p of one argument. call/cc constructs a concrete representation of the current …
user10082400
1
vote
1 answer

How to express delimited continuations with pure functions only?

I worked through Oleg's tutorial of delimited continuations: newtype Cont r a = Cont{runCont :: (a -> r) -> r} instance Monad (Cont r) where return x = Cont (\k -> k x) Cont m >>= f = Cont (\k -> m (\v -> runCont (f v) k)) runC :: Cont r r ->…
user10675354
1
vote
1 answer

JS Callbacks: continuation-passing or candy factory style?

At programming styles course we are asked to implement some code in both, "continuation-passing style" and "candy factory style". The book we are reading is "Exercises in programming styles" by Cristina Videira Lopes (chapter 5 and 8). We are asked…
BioShock
  • 763
  • 2
  • 13
  • 33
1
vote
1 answer

[a,b].reduce(f,x) code to [a,b].reduce(f) using transducer /CPS based functional references?

In my previous Quesion: Extracting data from a function chain without arrays @Aadit M Shah gave me astonishing solution as follows: https://stackoverflow.com/a/51420884/6440264 Given an expression like A(a)(b)(f) where f is a function, it's…
user6440264