{"id":476419,"date":"2023-08-09T07:29:55","date_gmt":"2023-08-09T07:29:55","guid":{"rendered":""},"modified":"2023-09-05T11:12:43","modified_gmt":"2023-09-05T11:12:43","slug":"continuation-passing-style-cps","status":"publish","type":"wiki","link":"https:\/\/oneproxy.pro\/de\/wiki\/continuation-passing-style-cps\/","title":{"rendered":"Continuation-Passing-Stil (CPS)"},"content":{"rendered":"<p>Der Continuation-Passing-Stil (CPS) ist eine Methode zur Handhabung des Kontrollflusses in der Computerprogrammierung, bei der die Steuerung explizit \u00fcber einen Funktionsparameter \u00fcbergeben wird.<\/p>\n<h2>Die Entwicklung des Continuation-Passing-Stils (CPS)<\/h2>\n<p>Die Urspr\u00fcnge des Continuation-Passing-Stils lassen sich auf die Entwicklung der theoretischen Informatik zur\u00fcckf\u00fchren, und das Konzept der Fortsetzungen selbst hat seine Wurzeln in der Lambda-Rechnung. Die erste explizite Erw\u00e4hnung des \u201eContinuation-Passing-Stils\u201c als Ausdruck und seine Verwendung in der Praxis wurde in den 1960er Jahren vom Informatiker Christopher Strachey eingef\u00fchrt. In dieser Zeit erforschten er und seine Kollegen die denotationale Semantik, ein Rahmenwerk zur Definition der Bedeutungen von Programmiersprachen.<\/p>\n<h2>Entfaltung des Continuation-Passing-Stils (CPS)<\/h2>\n<p>Continuation-Passing Style (CPS) ist eine Form der Programmorganisation, die die explizite Verwendung von Fortsetzungen beinhaltet. Eine Fortsetzung ist eine Darstellung des Zustands eines Computerprogramms zu einem bestimmten Zeitpunkt, einschlie\u00dflich des Aufrufstapels und der Werte von Variablen.<\/p>\n<p>In CPS erh\u00e4lt jede Funktion ein zus\u00e4tzliches Argument, das normalerweise \u201econt\u201c oder \u201ek\u201c hei\u00dft und die Fortsetzung des Programms darstellt \u2013 was passieren soll, nachdem die Funktion ihre Berechnung abgeschlossen hat. Wenn die Funktion ihr Ergebnis berechnet hat, \u201egibt\u201c sie dieses Ergebnis zur\u00fcck, indem sie es an die Fortsetzung \u00fcbergibt, anstatt es auf die \u00fcbliche Weise zur\u00fcckzugeben.<\/p>\n<p>Das Konzept kann als eine M\u00f6glichkeit betrachtet werden, den Kontrollfluss explizit zu machen: Anstatt die Kontrolle implizit an den Aufrufer zu \u00fcbergeben, wenn sie abgeschlossen ist, \u00fcbergibt eine CPS-Funktion die Kontrolle durch den Aufruf der Fortsetzung.<\/p>\n<h2>Die Struktur des Continuation-Passing-Stils (CPS)<\/h2>\n<p>In der traditionellen Funktionsaufrufkonvention wird eine Funktion beim Aufruf ausgef\u00fchrt und gibt die Steuerung mit einem R\u00fcckgabewert an den Aufrufer zur\u00fcck. Beim Continuation-Passing-Stil wird die Steuerung jedoch explizit \u00fcber einen Funktionsparameter \u00fcbergeben, der oft als \u201eFortsetzung\u201c bezeichnet wird.<\/p>\n<p>Die Fortsetzung stellt den Rest der Berechnung dar. Das hei\u00dft, wenn eine Funktion eine Fortsetzung empf\u00e4ngt, f\u00fchrt sie einige Operationen aus und \u00fcbergibt dann das Ergebnis an die empfangene Fortsetzung. Daher wird im Fortsetzungs\u00fcbergabestil die R\u00fcckgabe nie implizit ausgef\u00fchrt.<\/p>\n<p>Eine typische CPS-Funktion in einer Pseudosprache k\u00f6nnte wie folgt aussehen:<\/p>\n<pre><div class=\"bg-black rounded-md mb-4\"><div class=\"flex items-center relative text-gray-200 bg-gray-800 px-4 py-2 text-xs font-sans justify-between rounded-t-md\"><span>CSS<\/span><button class=\"flex ml-auto gap-2\"><svg stroke=\"currentColor\" fill=\"none\" stroke-width=\"2\" viewbox=\"0 0 24 24\" stroke-linecap=\"round\" stroke-linejoin=\"round\" class=\"h-4 w-4\" height=\"1em\" width=\"1em\" ><path d=\"M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2\"><\/path><rect x=\"8\" y=\"2\" width=\"8\" height=\"4\" rx=\"1\" ry=\"1\"><\/rect><\/svg>Code kopieren<\/button><\/div><div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-css\" data-no-translation=\"\">function add(<span class=\"hljs-selector-tag\">a<\/span>, <span class=\"hljs-selector-tag\">b<\/span>, continuation) {\n    result = <span class=\"hljs-selector-tag\">a<\/span> + <span class=\"hljs-selector-tag\">b<\/span>;\n    continuation(result);\n}\n<\/code><\/div><\/div><\/pre>\n<p>Diese \u201eAdd\u201c-Funktion f\u00fchrt eine Additionsoperation aus und \u00fcbergibt das Ergebnis dann an die Fortsetzung.<\/p>\n<h2>Hauptmerkmale des Continuation-Passing-Stils (CPS)<\/h2>\n<ol>\n<li>\n<p><strong>Expliziter Kontrollfluss<\/strong>: In CPS ist der Kontrollfluss explizit. Es gibt keinen versteckten Stacktrace und Sie k\u00f6nnen die Ausf\u00fchrungsreihenfolge im Code klar erkennen.<\/p>\n<\/li>\n<li>\n<p><strong>Flexibilit\u00e4t<\/strong>: Da CPS die Berechnung vom Kontrollfluss entkoppelt, bietet es mehr Flexibilit\u00e4t bei der Manipulation des Kontrollflusses.<\/p>\n<\/li>\n<li>\n<p><strong>Nicht blockierende Operationen<\/strong>: CPS ist sehr n\u00fctzlich bei der Verwaltung nicht blockierender oder asynchroner Vorg\u00e4nge. Es kann verwendet werden, um Callback-H\u00f6llen zu vermeiden und komplexe Kontrollflussszenarien in nicht blockierendem Code zu verwalten.<\/p>\n<\/li>\n<li>\n<p><strong>Tail Call-Optimierung<\/strong>: Sprachen, die Tail-Call-Optimierung unterst\u00fctzen, k\u00f6nnen von CPS profitieren, da es alle Aufrufe in Tail-Calls umwandelt, was hinsichtlich der Speichernutzung effizienter sein kann.<\/p>\n<\/li>\n<\/ol>\n<h2>Arten des Continuation-Passing-Stils (CPS)<\/h2>\n<p>Es gibt haupts\u00e4chlich zwei Arten von Fortsetzungen, <strong>direkter Stil<\/strong> Und <strong>Fortsetzungspassing-Stil<\/strong>. Nachfolgend finden Sie einen Vergleich zwischen den beiden:<\/p>\n<table>\n<thead>\n<tr>\n<th>Stil<\/th>\n<th>Beschreibung<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Direkter Stil<\/td>\n<td>Beim direkten Stil schlie\u00dft eine Funktion ihre Ausf\u00fchrung ab und gibt die Kontrolle an die aufrufende Funktion zur\u00fcck. Der R\u00fcckgabewert ist h\u00e4ufig ein Berechnungsergebnis.<\/td>\n<\/tr>\n<tr>\n<td>Fortsetzungs\u00fcbergabestil<\/td>\n<td>In CPS erh\u00e4lt die Funktion ein zus\u00e4tzliches Argument, die Fortsetzung, und \u00fcbergibt das Ergebnis an diese Fortsetzung. Der Kontrollfluss ist explizit.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Nutzung, Probleme und L\u00f6sungen<\/h2>\n<p>CPS wird haupts\u00e4chlich in funktionalen Programmiersprachen und zur Verwaltung asynchroner Vorg\u00e4nge verwendet.<\/p>\n<ol>\n<li>\n<p><strong>Asynchrones JavaScript<\/strong>: JavaScript, insbesondere in Node.js, verwendet CPS, um asynchrone, nicht blockierende Vorg\u00e4nge zu verwalten. Callbacks in JavaScript sind Beispiele f\u00fcr CPS.<\/p>\n<\/li>\n<li>\n<p><strong>Funktionale Programmierung<\/strong>: Sprachen wie Scheme und Haskell verwenden CPS, um Kontrollstrukturen wie Schleifen und Ausnahmebehandlung zu handhaben.<\/p>\n<\/li>\n<\/ol>\n<p>CPS kann jedoch zu einigen Problemen f\u00fchren:<\/p>\n<ul>\n<li><strong>Lesbarkeit<\/strong>: CPS kann aufgrund der Callback-H\u00f6lle manchmal zu Code f\u00fchren, der schwer zu lesen und zu verstehen ist, insbesondere wenn viele verschachtelte Callbacks vorhanden sind.<\/li>\n<li><strong>Effizienz<\/strong>: Die CPS-Transformation kann aufgrund zus\u00e4tzlicher Parameter und Funktionsaufrufe m\u00f6glicherweise die Codegr\u00f6\u00dfe erh\u00f6hen.<\/li>\n<\/ul>\n<p>Die L\u00f6sungen f\u00fcr diese Probleme sind:<\/p>\n<ul>\n<li>Verwenden <strong>Versprechen<\/strong> oder <strong>asynchron\/warten<\/strong> in JavaScript, um die Callback-H\u00f6lle zu vermeiden und die Lesbarkeit zu verbessern.<\/li>\n<li>Die Verwendung von Programmiersprachen, die die Tail-Call-Optimierung unterst\u00fctzen, kann Effizienzbedenken ausr\u00e4umen.<\/li>\n<\/ul>\n<h2>Vergleiche<\/h2>\n<p>Hier ist ein Vergleich von CPS mit anderen Programmierparadigmen:<\/p>\n<table>\n<thead>\n<tr>\n<th>Programmierparadigma<\/th>\n<th>Kontrollfluss<\/th>\n<th>Anwendungsfall<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Fortsetzungspassing-Stil (CPS)<\/td>\n<td>Explizit, mit Fortsetzungen.<\/td>\n<td>Nicht blockierende\/asynchrone Vorg\u00e4nge, Tail-Call-Optimierung.<\/td>\n<\/tr>\n<tr>\n<td>Direkter Stil<\/td>\n<td>Implizit, die Funktion kehrt zum Anrufer zur\u00fcck.<\/td>\n<td>Synchrone\/blockierende Operationen.<\/td>\n<\/tr>\n<tr>\n<td>Coroutinen<\/td>\n<td>Kooperatives Multitasking, indem die Ausf\u00fchrung von Funktionen angehalten und fortgesetzt wird.<\/td>\n<td>Komplexer Kontrollfluss, kooperatives Multitasking.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Zukunftsperspektiven<\/h2>\n<p>CPS spielt weiterhin eine wichtige Rolle bei der Strukturierung von asynchronem Code, insbesondere in JavaScript. Die Einf\u00fchrung von async\/await, das eine syntaktische Vereinfachung gegen\u00fcber Promises darstellt, kann als Weiterentwicklung des traditionellen CPS angesehen werden, da es eine bessere Syntax bietet und die Callback-H\u00f6lle vermeidet.<\/p>\n<p>Da Web- und Serveranwendungen immer komplexer werden und Parallelit\u00e4t immer wichtiger wird, werden CPS und andere asynchrone Programmierparadigmen wahrscheinlich noch wichtiger werden. Es wird laufend an der Verbesserung von Programmiersprachen und Laufzeitsystemen geforscht, um diese Paradigmen besser zu unterst\u00fctzen.<\/p>\n<h2>Proxyserver und CPS<\/h2>\n<p>Proxyserver fungieren als Vermittler f\u00fcr Anfragen von Clients, die Ressourcen von anderen Servern anfordern. Bei der Verarbeitung gleichzeitiger Clientanfragen kann ein Proxyserver CPS oder \u00e4hnliche asynchrone Programmierparadigmen verwenden, um diese Anfragen ohne Blockierung zu verwalten und so Durchsatz und Leistung zu verbessern.<\/p>\n<h2>verwandte Links<\/h2>\n<ol>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Continuation-passing_style\" target=\"_new\" rel=\"noopener nofollow\">Continuation-Passing-Stil auf Wikipedia<\/a><\/li>\n<li><a href=\"https:\/\/www.cs.utah.edu\/plt\/publications\/icfp07-fyff.pdf\" target=\"_new\" rel=\"noopener nofollow\">Die Kunst des Fortsetzers<\/a><\/li>\n<li><a href=\"https:\/\/www.microsoft.com\/en-us\/research\/publication\/a-history-of-haskell-being-lazy-with-class\/\" target=\"_new\" rel=\"noopener nofollow\">Eine Geschichte von Haskell: Faulheit im Unterricht<\/a><\/li>\n<\/ol>","protected":false},"featured_media":468006,"menu_order":0,"template":"","meta":{"_acf_changed":false,"content-type":"","inline_featured_image":false,"footnotes":""},"class_list":["post-476419","wiki","type-wiki","status-publish","has-post-thumbnail","hentry"],"acf":{"faq_title":"Frequently Asked Questions about <mark>A Deep Dive into Continuation-passing Style (CPS)<\/mark>","faq_items":[{"question":"What is Continuation-passing Style (CPS)?","answer":"<p>Continuation-passing Style (CPS) is a method of managing control flow in computer programming. Instead of returning a value to the caller in the usual way, functions in CPS receive an extra argument (often termed as \"continuation\") representing what should happen after the function finishes its computation.<\/p>"},{"question":"When was Continuation-passing Style (CPS) first mentioned?","answer":"<p>The concept of Continuation-passing Style (CPS) was first introduced by computer scientist Christopher Strachey in the 1960s when exploring denotational semantics, a framework for defining the meanings of programming languages.<\/p>"},{"question":"How does a Continuation-passing Style (CPS) function work?","answer":"<p>In CPS, every function receives an extra argument, representing the continuation of the program. When the function has computed its result, it \"returns\" this result by passing it to the continuation, making control flow explicit.<\/p>"},{"question":"What are the key features of Continuation-passing Style (CPS)?","answer":"<p>The key features of CPS include explicit control flow, increased flexibility, improved handling of non-blocking or asynchronous operations, and enhanced tail call optimization.<\/p>"},{"question":"What types of Continuation-passing Style (CPS) exist?","answer":"<p>There are mainly two types of continuations: direct style and continuation-passing style. In direct style, a function completes its execution and returns control to the calling function. In continuation-passing style, the function passes the result to a received continuation, making the control flow explicit.<\/p>"},{"question":"What are the main uses and problems related to CPS?","answer":"<p>CPS is mostly used in functional programming languages and for managing asynchronous operations. It's useful in JavaScript, particularly in Node.js, and languages like Scheme and Haskell. However, it can lead to problems like reduced code readability (due to callback hell) and increased code size. These can be mitigated by using Promises or async\/await in JavaScript and tail-call optimization in other languages.<\/p>"},{"question":"What is the future perspective of Continuation-passing Style (CPS)?","answer":"<p>CPS continues to be essential in structuring asynchronous code, with developments like async\/await in JavaScript improving upon traditional CPS. As web and server applications become more complex and concurrent, CPS and other asynchronous programming paradigms are likely to become more important.<\/p>"},{"question":"How are proxy servers related to Continuation-passing Style (CPS)?","answer":"<p>Proxy servers, acting as intermediaries for requests from clients seeking resources from other servers, might use CPS or similar asynchronous programming paradigms to manage concurrent client requests without blocking, thereby improving throughput and performance.<\/p>"}]},"_links":{"self":[{"href":"https:\/\/oneproxy.pro\/de\/wp-json\/wp\/v2\/wiki\/476419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oneproxy.pro\/de\/wp-json\/wp\/v2\/wiki"}],"about":[{"href":"https:\/\/oneproxy.pro\/de\/wp-json\/wp\/v2\/types\/wiki"}],"version-history":[{"count":0,"href":"https:\/\/oneproxy.pro\/de\/wp-json\/wp\/v2\/wiki\/476419\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/oneproxy.pro\/de\/wp-json\/wp\/v2\/media\/468006"}],"wp:attachment":[{"href":"https:\/\/oneproxy.pro\/de\/wp-json\/wp\/v2\/media?parent=476419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}