What does CPS (continuation-passing style) mean in functional programming?

Source: Internet
Author: User
There are no Lisp-based, C + + examples, preferably Python, but also lamdba expression.

Reply content:

The code that the CPS executes after the function is called is wrapped by a closure and invokes the function to be executed as a function parameter.

Continuation passing Style revisited part Five:cps and Asynchronythe CPS transform is essentially a call to a function that passes it to another function (so, The language must support higher-order functions and closures, and the called function does not return the result to the caller, but instead returns the result to the function passed in by the parameter.

I don't know if this concept is implemented in any other language, or it's called a different name.

Here's a scheme example: the CALL/CC Quest is answered from another angle.
Lambda calculus (called calculus) with CALLCC can be isomorphic to classical logic by Curry-howard, while ordinary calculus can only be isomorphic to intuitive logic. But there is a Gilvenko theorem in formal logic that claims:

for any proposition and premise, in classical logic if and only in the intuitive logic

After proving this theorem, Del (yes, the one that proves the existence of an uncertain proposition) and the root cen (the inventor of natural deduction and sequential calculus) invented a double negation transformation, also called the Del-root CEN transformation, whose rules are:


It is noted that the arbitrary proposition of the Del-root CEN transformation is equivalent to the original proposition, but not the intuition equivalence (the intuition logic itself denies), but according to the Gilvenko theorem, if the double negative proposition can be proved true in the intuitionistic logic system, it must be true in the classical logic system and vice versa.
Then according to Curry-howard isomorphism, the type assignment under the calculus can be assigned by a calculation type by the Del-root CEN transformation: The process of converting an expression (isomorphic to the proof process) into a CPS transformation. Directly copying the type of the Del-root CEN transformation, we have the following results:

    1. Atomic

    2. Call

    3. Abstract

    4. CALL/CC operator

It can be proved that: CPS transformation does not change semantics.
Of course, this version of the CPS is very lengthy, the market saw those are in the transformation is directly after the statute, delete the large heap of redex.

These once again illustrate how closely the logic and programming are connected. A concise addition.

This is a simple function to calculate the output:

  static int Times3 (int x)    {        return x * 3;    }   Console.WriteLine (Times3 (5));

(The answer is copied from http://www. ) cs.indiana.edu/cgi-pub/ LKUPER/C311/_MEDIA/CPS-NOTES.SCM )
Define the following four functions (in order to remain consistent with the original answer, two is enough)

def f (var0):    passdef g (var0, var1):    return passdef H (var0): Return    passdef J (var0):    return Pass

is actually to

Fuckee Findfuckee () {return kula;} void Fuck (Fuckee fuckee, int count) {for (int i=0;i<count;i++) Fuckee. Fuck ();} void Main () {Fuck (Findfuckee (), 100);}

You can fill in the logic ... @Belleve answer is too abstract, people who have no logical background to read,
I saw a very good answer on the Quora, and a section from a logical point of view might serve as a supplement to @Belleve answer:
What's continuation-passing style in functional programming?

What is the meaning of CPS (continuation-passing style) in functional programming? For more information, please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.