1
0
Fork 0
cl-sites/guile.html_node/An-Introduction-to-CPS.html
2024-12-17 12:49:28 +01:00

148 lines
6.2 KiB
HTML

<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- This manual documents Guile version 3.0.10.
Copyright (C) 1996-1997, 2000-2005, 2009-2023 Free Software Foundation,
Inc.
Copyright (C) 2021 Maxime Devos
Copyright (C) 2024 Tomas Volf
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
copy of the license is included in the section entitled "GNU Free
Documentation License." -->
<title>An Introduction to CPS (Guile Reference Manual)</title>
<meta name="description" content="An Introduction to CPS (Guile Reference Manual)">
<meta name="keywords" content="An Introduction to CPS (Guile Reference Manual)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content=".texi2any-real">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Continuation_002dPassing-Style.html" rel="up" title="Continuation-Passing Style">
<link href="CPS-in-Guile.html" rel="next" title="CPS in Guile">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
-->
</style>
<link rel="stylesheet" type="text/css" href="https://www.gnu.org/software/gnulib/manual.css">
</head>
<body lang="en">
<div class="subsubsection-level-extent" id="An-Introduction-to-CPS">
<div class="nav-panel">
<p>
Next: <a href="CPS-in-Guile.html" accesskey="n" rel="next">CPS in Guile</a>, Up: <a href="Continuation_002dPassing-Style.html" accesskey="u" rel="up">Continuation-Passing Style</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsubsection" id="An-Introduction-to-CPS-1"><span>9.4.4.1 An Introduction to CPS<a class="copiable-link" href="#An-Introduction-to-CPS-1"> &para;</a></span></h4>
<p>Consider the following Scheme expression:
</p>
<div class="example lisp">
<pre class="lisp-preformatted">(begin
(display &quot;The sum of 32 and 10 is: &quot;)
(display 42)
(newline))
</pre></div>
<p>Let us identify all of the sub-expressions in this expression,
annotating them with unique labels:
</p>
<div class="example lisp">
<pre class="lisp-preformatted">(begin
(display &quot;The sum of 32 and 10 is: &quot;)
|k1 k2
k0
(display 42)
|k4 k5
k3
(newline))
|k7
k6
</pre></div>
<p>Each of these labels identifies a point in a program. One label may be
the continuation of another label. For example, the continuation of
<code class="code">k7</code> is <code class="code">k6</code>. This is because after evaluating the value of
<code class="code">newline</code>, performed by the expression labelled <code class="code">k7</code>, we
continue to apply it in <code class="code">k6</code>.
</p>
<p>Which expression has <code class="code">k0</code> as its continuation? It is either the
expression labelled <code class="code">k1</code> or the expression labelled <code class="code">k2</code>.
Scheme does not have a fixed order of evaluation of arguments, though it
does guarantee that they are evaluated in some order. Unlike general
Scheme, continuation-passing style makes evaluation order explicit. In
Guile, this choice is made by the higher-level language compilers.
</p>
<p>Let us assume a left-to-right evaluation order. In that case the
continuation of <code class="code">k1</code> is <code class="code">k2</code>, and the continuation of
<code class="code">k2</code> is <code class="code">k0</code>.
</p>
<p>With this example established, we are ready to give an example of CPS in
Scheme:
</p>
<div class="example smalllisp lisp">
<pre class="lisp-preformatted">(lambda (ktail)
(let ((k1 (lambda ()
(let ((k2 (lambda (proc)
(let ((k0 (lambda (arg0)
(proc k4 arg0))))
(k0 &quot;The sum of 32 and 10 is: &quot;)))))
(k2 display))))
(k4 (lambda _
(let ((k5 (lambda (proc)
(let ((k3 (lambda (arg0)
(proc k7 arg0))))
(k3 42)))))
(k5 display))))
(k7 (lambda _
(let ((k6 (lambda (proc)
(proc ktail))))
(k6 newline)))))
(k1))
</pre></div>
<p>Holy code explosion, Batman! What&rsquo;s with all the lambdas? Indeed, CPS
is by nature much more verbose than &ldquo;direct-style&rdquo; intermediate
languages like Tree-IL. At the same time, CPS is simpler than full
Scheme, because it makes things more explicit.
</p>
<p>In the original program, the expression labelled <code class="code">k0</code> is in effect
context. Any values it returns are ignored. In Scheme, this fact is
implicit. In CPS, we can see it explicitly by noting that its
continuation, <code class="code">k4</code>, takes any number of values and ignores them.
Compare this to <code class="code">k2</code>, which takes a single value; in this way we
can say that <code class="code">k1</code> is in a &ldquo;value&rdquo; context. Likewise <code class="code">k6</code> is
in tail context with respect to the expression as a whole, because its
continuation is the tail continuation, <code class="code">ktail</code>. CPS makes these
details manifest, and gives them names.
</p>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="CPS-in-Guile.html">CPS in Guile</a>, Up: <a href="Continuation_002dPassing-Style.html">Continuation-Passing Style</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>