-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsrfi-248.html
331 lines (279 loc) · 14.6 KB
/
srfi-248.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<!DOCTYPE html>
<html lang="en">
<!--
SPDX-FileCopyrightText: 2025 Marc Nieper-Wißkirchen
SPDX-License-Identifier: MIT
-->
<head>
<meta charset="utf-8">
<title>SRFI 248: Minimal delimited continuations</title>
<link href="/favicon.png" rel="icon" sizes="192x192" type="image/png">
<link rel="stylesheet" href="https://srfi.schemers.org/srfi.css" type="text/css">
<meta name="viewport" content="width=device-width, initial-scale=1"></head>
<body>
<h1><a href="https://srfi.schemers.org/"><img class="srfi-logo" src="https://srfi.schemers.org/srfi-logo.svg" alt="SRFI surfboard logo" /></a>248: Minimal delimited continuations</h1>
<p>by Marc Nieper-Wißkirchen</p>
<h2 id="status">Status</h2>
<p>This SRFI is currently in <em>final</em> status. Here is <a href="https://srfi.schemers.org/srfi-process.html">an explanation</a> of each status that a SRFI can hold. To provide input on this SRFI, please send email to <code><a href="mailto:srfi+minus+248+at+srfi+dotschemers+dot+org">srfi-248@<span class="antispam">nospam</span>srfi.schemers.org</a></code>. To subscribe to the list, follow <a href="https://srfi.schemers.org/srfi-list-subscribe.html">these instructions</a>. You can access previous messages via the mailing list <a href="https://srfi-email.schemers.org/srfi-248/">archive</a>.</p>
<ul>
<li>Received: 2023-10-27</li>
<li>Draft #1 published: 2023-10-29</li>
<li>Draft #2 published: 2023-10-31</li>
<li>Draft #3 published: 2024-09-07</li>
<li>Draft #4 published: 2024-09-13</li>
<li>Finalized: 2025-01-24</li>
</ul>
<h2 id="abstract">Abstract</h2>
<p>
Scheme's exception system is extended so that exception handlers
gain access to the delimited continuation representing the rest
of the computation of the call of the thunk guarded by the
handler. Algebraic effect handlers can be directly expressed in
this extended exception system. The system directly implements
the <code>shift0</code>/<code>reset0</code> delimited control
operators. It is well known that other delimited control
operators like <code>prompt0</code>/<code>control0</code> or
<code>reset</code>/<code>shift</code> are expressible
in <code>shift0</code>/<code>reset0</code> (and vice versa).
</p>
<h2 id="rationale">Rationale</h2>
<p>Scheme was the first programming language with first-class
continuations. Since their invention, it has become
increasingly clear that they do not provide the right
abstraction in many use cases, but should be supplemented by
delimited continuations, which only represent a portion of the
rest of the computation. While delimited continuations are
becoming popular in other programming languages, they have yet
to find their way into a Scheme standard.
</p>
<p>
<a href="https://srfi.schemers.org/srfi-226/srfi-226.html">SRFI
226</a> is an extensive proposal to add many control structures,
including delimited continuations, to Scheme in a coherent way.
SRFI 226 also contains a rationale for delimited continuations
and provides many links to existing literature. Due to its size
and far-reaching consequences, it does, however, place a burden
not be underestimated on implementers of Scheme. For a future
standard comparable in size with R<sup>6</sup>RS or
R<sup>7</sup>RS Small, a minimal API supporting delimited
continuations might be more appropriate. This SRFI proposes
such an API. It integrates smoothly into the existing
facilities by hooking into Scheme's exception system, and only
comes with one essential primitive procedure (and extends the
existing <code>guard</code> convenience syntax).
<p>
This SRFI dispenses with the idea of prompt tags as in other
systems for delimited continuations (but it is possible to
implement a delimited continuation system with prompt tags with
it). If it were not for <code>call/cc</code>, the system could
dispense with <code>dynamic-wind</code>.
</p>
<h3>Examples</h3>
<p>In the following examples, we use the <code>guard</code>
syntax, which is a convenient syntactic abstraction over the
primitive <code>with-unwind-handler</code> procedure. Both
are defined in the <a href="#specification">specification
section</a>.</p>
<p>The following is a version
of <a href="https://srfi.schemers.org/srfi-158/srfi-158.html">SRFI
158</a>'s <code>make-coroutine-generator</code> that
prevents the space leaks inherent to the sample implementation
of SRFI 158, which uses <code>call/cc</code>. This version, which uses delimited
continuations, is also easier to understand.
</p>
<pre>(define make-coroutine-generator
(lambda (proc)
(define-condition-type &yield &condition
make-yield-condition yield-condition?
(value condition-value))
(define yield
(lambda (val)
(raise-continuable (make-yield-condition val))))
(define thunk
(lambda ()
(guard (c k
[(yield-condition? c)
(set! thunk k)
(condition-value c)])
(proc yield)
(eof-object))))
(lambda ()
(thunk))))</pre>
<p>The next example shows how delimited continuations and the API
of this SRFI in particular can be used to transform a procedure
with <code>for-each</code>-like semantics describing an abstract
list into a procedure with <code>fold</code>-like semantics
describing the same abstract list. Notably, the implementation
does not use state, which would be incompatible with the existence
of <code>call/cc</code>.
<pre>(define for-each->fold
(lambda (for-each)
(define-condition-type &yield &condition
make-yield-condition yield-condition?
(value condition-value))
(lambda (proc nil)
((guard (c k
[(yield-condition? c)
(lambda (s)
((k) (proc s (condition-value c))))])
(for-each
(lambda (x)
(raise-continuable (make-yield-condition x))))
values)
nil))))
(define string-fold
(lambda (proc seed s)
(define for-each-s (lambda (proc) (string-for-each proc s)))
(define fold-s (for-each->fold for-each-s))
(fold-s proc seed)))</pre>
<p>More examples can be found in the <a href="tests.scm">bundled
test cases</a>.</p>
<h2 id="specification">Specification</h2>
<p>The following syntax and procedures are provided by the <code>(srfi
:248 exceptions)</code> and <code>(srfi :248)</code> libraries
(R<sup>6</sup>RS) and by the <code>(srfi 248)</code> library
(R<sup>7</sup>RS Small).</p>
<h3>Procedures</h3>
<p><code>(with-unwind-handler</code> <var>handler</var> <var>thunk</var><code>)</code></p>
<p><var>Handler</var> must be a procedure and should accept two
arguments. <var>Thunk</var> must be a procedure that accepts
zero arguments. The <code>with-unwind-handler</code> procedure
returns the results of invoking <var>thunk</var>. An anonymous
handler is installed as the current exception handler for the
dynamic extent (as defined in R6RS) of the invocation
of <var>thunk</var>.</p>
<p>When the anonymous handler is invoked on an
object <var>obj</var> as a result of a call
to <code>raise</code> or <code>raise-continuable</code>, it
packages the portion of the current continuation up to and
including the call to <code>with-unwind-handler</code> that
installed the anonymous handler in a “delimited
continuation procedure” <var>k</var>. It then
applies <var>handler</var> on <var>obj</var> and <var>k</var> in
the continuation and dynamic environment of the call
to <code>with-unwind-handler</code>.</p>
<p>Implementation responsibilities (R<sup>6</sup>RS): The
implementation must check the restrictions on <var>handler</var>
to the extent performed by applying it as described when it is
called as a result of a call to <code>raise</code>
or <code>raise-continuable</code>. An implementation may check
whether <code>handler</code> is an appropriate argument before
applying it.</p>
<p><code>(empty-continuation?</code> <var>k</var><code>)</code></p>
<p>The argument <var>k</var> should be a delimited continuation
procedure. The <code>empty-continuation?</code> predicate
returns <code>#t</code> if <var>k</var> represents the empty
delimited continuation, that is, if <var>k</var> is the
delimited continuation argument of a handler invoked as a result
of a call to <code>raise-continuable</code> that occurs in tail
context of the thunk of the <code>with-unwind-handler</code>
expression that installed the handler. If <var>k</var> does not
represent the empty delimited continuation, <code>#f</code> is
returned.</p>
<p>Example:</p>
<pre>(with-unwind-handler
(lambda (obj k)
(empty-continuation? k))
(lambda ()
(raise-continuable 42))) ; ⇒ #t
(with-unwind-handler
(lambda (obj k)
(empty-continuation? k))
(lambda ()
(not (raise-continuable 42)))) ; ⇒ #t</pre>
<p><code>(with-exception-handler</code> <var>handler</var> <var>thunk</var><code>)</code></p>
<p>This procedure is the same as in R<sup>6</sup>RS and R<sup>7</sup>RS Small.</p>
<p><code>(raise</code> <var>obj</var><code>)</code></p>
<p>This procedure is the same as in R<sup>6</sup>RS and R<sup>7</sup>RS Small.</p>
<p><code>(raise-continuable</code> <var>obj</var><code>)</code></p>
<p>This procedure is the same as in R<sup>6</sup>RS and R<sup>7</sup>RS Small.</p>
<h3>Syntax</h3>
<p style="white-space: pre"><code>(guard</code> (⟨variable⟩ ⟨continuation variable⟩
⟨cond clause<sub>1</sub>⟩
⟨cond clause<sub>1</sub>⟩
…<code>)
</code>⟨body⟩<code>)</code>
<code>=></code>
<code>else</code></p>
<p><i>Syntax:</i> ⟨Cond clause⟩ is as in the
specification of <code>cond</code>. <code>=></code>
and <code>else</code> are the same as in R<sup>6</sup>RS and
R<sup>7</sup>RS Small.
</p>
<p><i>Semantics:</i> Evaluating a guard form evaluates
⟨body⟩ with an anonymous exception handler that
packages the portion of the current continuation up to and
including the evaluation of the <code>guard</code> form in a
delimited continuation procedure. It then binds the raised
object to ⟨variable⟩ and the captured delimited
continuation procedure to ⟨continuation variable⟩,
and within the scope of those bindings evaluates the clauses as
if they were the clauses of a <code>cond</code> expression. The
implicit <code>cond</code> expression is evaluated within the
continuation and dynamic environment of the <code>guard</code>
expression. If every ⟨cond clause⟩'s
⟨test⟩ evaluates to <code>#f</code> and there is
no <code>else</code> clause, then <code>raise-continuable</code>
is invoked on the raised object, and the captured delimited
continuation procedure is applied to the values returned by the
call to <code>raise-continuable</code>.
</p>
<p>
The final expression in a ⟨cond clause⟩
is in a tail context if the <code>guard</code> expression itself is.
</p>
<p>If a call to <code>raise-continuable</code> occurs in tail
context of the <code>body</code>, the delimited continuation
procedure bound to ⟨continuation variable⟩
represents the empty continuation.
</p>
<p style="white-space: pre"><code>(guard</code> (⟨variable⟩
⟨cond clause<sub>1</sub>⟩
⟨cond clause<sub>1</sub>⟩
…<code>)
</code>⟨body⟩<code>)</code></p>
<p>This syntactic form is the same as in R<sup>6</sup>RS (with errata
applied) and R<sup>7</sup>RS Small (with the tail context specification
as in R<sup>6</sup>RS).</p>
<h2 id="implementation">Implementation</h2>
<p>A portable implementation of delimited continuations (which
uses <code>call/cc</code> and global state) is only possible by
at least replacing the user-visible <code>call/cc</code>.
This is analogous to the fact that <code>dynamic-wind</code> can be
implemented by using <code>call/cc</code> and global state, but
only by replacing the user-visible <code>call/cc</code>.
</p>
<p>This SRFI is implementable in any Scheme that supports any form
of delimited continuations compatible with <code>call/cc</code>
and <code>dynamic-wind</code> and where one can test for
equality of continuation procedures. We provide a
<a href="lib/srfi/srfi-248.scm">a sample implementation for
Guile</a>, which is only missing
the <code>empty-continuation?</code> predicate.</p>
<h2 id="acknowledgements">Acknowledgements</h2>
<p>Thanks to the members of the SRFI discussion group.</p>
<h2 id="copyright">Copyright</h2>
<p>© 2025 Marc Nieper-Wißkirchen.</p>
<p>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:</p>
<p>
The above copyright notice and this permission notice (including the
next paragraph) shall be included in all copies or substantial
portions of the Software.</p>
<p>
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.</p>
<hr>
<address>Editor: <a href="mailto:srfi-editors+at+srfi+dot+schemers+dot+org">Arthur A. Gleckler</a></address></body></html>