Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cons of first class continuations

What are some of the criticisms leveled against exposing continuations as first class objects?

I feel that it is good to have first class continuations. It allow complete control over the execution flow of instructions. Advanced programmers can develop intuitive solutions to certain kind of problems. For instance, continuations are used to manage state on web servers. A language implementation can provide useful abstractions on top of continuations. For example, green threads.

Despite all these, are there strong arguments against first class continuations?

like image 974
Vijay Mathew Avatar asked Dec 03 '22 07:12

Vijay Mathew


1 Answers

The reality is that many of the useful situations where you could use continuations are already covered by specialized language constructs: throw/catch, return, C#/Python yield. Thus, language implementers don't really have all that much incentive to provide them in a generalized form usable for roll-your-own solutions.

In some languages, generalized continuations are quite hard to implement efficiently. Stack-based languages (i.e. most languages) basically have to copy the whole stack every time you create a continuation.

Those languages can implement certain continuation-like features, those that don't break the basic stack-based model, a lot more efficiently than the general case, but implementing generalized continuations is quite a bit harder and not worth it.

Functional languages are more likely to implement continuations for a couple of reasons:

  1. They are frequently implemented in continuation passing style, which means the "call stack" is probably a linked list allocated on the heap. This makes it trivial to pass a pointer to the stack as a continuation, since you don't need to overwrite the stack context when you pop the current frame and push a new one. (I've never implemented CPS but that's my understanding of it.)
  2. They favor immutable data bindings, which make your old continuation a lot more useful because you will not have altered the contents of variables that the stack pointed to when you created it.

For these reasons, continuations are likely to remain mostly just in the domain of functional languages.

like image 57
Nate C-K Avatar answered Jan 04 '23 22:01

Nate C-K