What do “continuations” mean in functional programming?(Specfically SML)

梦想的初衷 提交于 2019-12-05 14:08:32

Continuations simply treat "what happens next" as first class objects that can be used once unconditionally, ignored in favour of something else, or used multiple times.

To address what Continuation Passing Style is, here is some expression written normally:

let h x = f (g x)

g is applied to x and f is applied to the result. Notice that g does not have any control. Its result will be passed to f no matter what.

in CPS this is written

let h x next = (g x (fun result -> f result next))

g not only has x as an argument, but a continuation that takes the output of g and returns the final value. This function calls f in the same manner, and gives next as the continuation.

What happened? What changed that made this so much more useful than f (g x)? The difference is that now g is in control. It can decide whether to use what happens next or not. That is the essence of continuations.

An example of where continuations arise are imperative programming languages where you have control structures. Whiles, blocks, ordinary statements, breaks and continues are all generalized through continuations, because these control structures take what happens next and decide what to do with it, for example we can have

...
while(condition1) {
    statement1;
    if(condition2) break;
    statement2;
    if(condition3) continue;
    statement3;
}
return statement3;
...

The while, the block, the statement, the break and the continue can all be described in a functional model through continuations. Each construct can be considered to be a function that accepts the

  • current environment containing
    • the enclosing scopes
    • optional functions accepting the current environment and returning a continuation to
      • break from the inner most loop
      • continue from the inner most loop
      • return from the current function.
  • all the blocks associated with it (if-blocks, while-block, etc)
  • a continuation to the next statement

and returns the new environment.

In the while loop, the condition is evaluated according to the current environment. If it is evaluated to true, then the block is evaluated and returns the new environment. The result of evaluating the while loop again with the new environment is returned. If it is evaluated to false, the result of evaluating the next statement is returned.

With the break statement, we lookup the break function in the environment. If there is no function found then we are not inside a loop and we give an error. Otherwise we give the current environment to the function and return the evaluated continuation, which would be the statement after the the while loop.

With the continue statement the same would happen, except the continuation would be the while loop.

With the return statement the continuation would be the statement following the call to the current function, but it would remove the current enclosing scope from the environment.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!