Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why continuation passing style

Tags:

In The Scheme Programming Language by Kent Dybvig (4th edition) section 3.4, he describes very clearly what continuation passing style is. For the why he gives two reasons:

  1. pass more than one result to its continuation, because the procedure that implements the continuation can take any number of arguments.
  2. CPS also allows a procedure to take separate continuations ..., which may accept different numbers of arguments.

Since the first reason can also be done using the values procedure and the second using case-lambda, I'm not clear the advantages of using continuation passing style. Could someone show me some examples of where continuation passing style is appropriate, where it makes the code better, clearer, etc.?

like image 295
Harry Spier Avatar asked Dec 17 '11 10:12

Harry Spier


People also ask

What is the benefit of continuation passing style?

Continuation passing style makes the control flow of programs more explicit as every procedure has the power to change the execution of the remainder of the program, contrast this to the traditional model in which procedures have no control over the behavior of the program once they return to their caller.

What does continuation passing style give you that tail recursion does not?

Continuation-Passing-Style, Tail Recursion, and Efficiency is not tail recursive, because the recursive call fact(n-1) is not the last thing the function does before returning. Instead, the function waits for the result of the recursive call, then multiples that by the value of n.

What is the function of continuation?

A continuation is a callback function k that represents the current state of the program's execution. More precisely, the continuation k is a function of one argument, namely the value that has been computed so far, that returns the final value of the computation after the rest of the program has run to completion.

What is continuation programming?

A continuation implements (reifies) the program control state, i.e. the continuation is a data structure that represents the computational process at a given point in the process's execution; the created data structure can be accessed by the programming language, instead of being hidden in the runtime environment.


1 Answers

Dybvig uses the explicit continuations in this section to motivate having call/cc as part of the language. The main point is made near the end of the section when he mentions that writing code without it requires a global tranformation of all code that is used, including functions that you call. So in Scheme you usually build your own construct using macros, and continuations are one of these useful constructs -- but you cannot implement them via macros since they implement only local transformations.

But using a CPS style directly can still be useful: for example, as he mentions, you could write a function that has more than one continuation, possibly with different arrities -- like a parsing function that receives a single-input function to send a parses value to and a nullary failure function to call when parsing fail (and this function might abort with an error or backtrack and try using other parsing rules). Another possible use is when you want to control exactly what goes into the continuation rather than letting call/cc grab the full context.

There also the obvious case of writing code in a language that has no first-class continuation, making CPSed code your only choice. An example of that would be lots of node.js programs that use IO and pretty much force you to write code in explicit CPS.

like image 128
Eli Barzilay Avatar answered Oct 12 '22 12:10

Eli Barzilay