-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsrfi-258.html
508 lines (434 loc) · 19.7 KB
/
srfi-258.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
<!DOCTYPE html>
<html lang="en">
<!--
SPDX-FileCopyrightText: 2025 Wolfgang Corcoran-Mathe
SPDX-License-Identifier: MIT
-->
<head>
<meta charset="utf-8">
<title>SRFI 258: Uninterned symbols</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">
<style>
<!--
/* Names of Scheme types: boolean, pair, etc. */
span.type-meta { font-style: italic }
/* Channel name on IRC or similar services. */
span.channel { font-family: monospace }
/* Variables in syntax forms. */
var.syn { font-style: normal; }
var.syn:before { content: "\27E8"; }
var.syn:after { content: "\27E9"; }
pre.example { margin-left: 2em; }
pre.grammar { font-family: serif; }
td.yes { background-color: LightGreen; }
td.no { background-color: LightSalmon; }
-->
</style>
<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>258: Uninterned Symbols</h1>
<p>by Wolfgang Corcoran-Mathe</p>
<h2 id="status">Status</h2>
<p>This SRFI is currently in <em>draft</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+258+at+srfi+dotschemers+dot+org">srfi-258@<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-258/">archive</a>.</p>
<ul>
<li>Received: 2025-01-22</li>
<li>60-day deadline: 2025-03-24</li>
<li>Wolfgang's <a href="https://github.com/Zipheir/srfi-258">personal
Git repo for this SRFI</a> for reference while the SRFI is in
<em>draft</em> status (<a href="https://htmlpreview.github.io/?https://github.com/Zipheir/srfi-258/blob/main/srfi-258.html">preview</a>)</li>
<li>Draft #1 published: 2025-01-23</li>
<li>Draft #2 published: 2025-01-27</li>
<li>Draft #3 published: 2025-02-24</li>
</ul>
<h2 id="abstract">Abstract</h2>
<p>An uninterned symbol is not the same as any other symbol, even one
with the same name. These symbols are useful in macro programming and
in other situations where guaranteed-unique names are needed.
A survey of uninterned and uniquely-named symbols
in Scheme is also provided.</p>
<h2 id="table-of-contents">Table of Contents</h2>
<ul>
<li><a href="#issues">Issues</a></li>
<li><a href="#rationale">Rationale</a></li>
<li><a href="#caveats">Caveats</a></li>
<li><a href="#specification">Specification</a></li>
<li>
<a href="#procedures">Procedures</a>
<ul>
<li>
<a href="#string-to-uninterned-symbol">
<code>string->uninterned-symbol</code>
</a>
</li>
<li>
<a href="#symbol-interned"><code>symbol-interned?</code></a>
</li>
<li>
<a href="#generate-uninterned-symbol">
<code>generate-uninterned-symbol</code>
</a>
</li>
</ul>
</li>
<li>
<a href="#prior-art">Prior art</a>
<ul>
<li><a href="#chezscheme">ChezScheme</a></li>
<li><a href="#common-lisp">Common Lisp</a></li>
</ul>
</li>
<li><a href="#implementation">Implementation</a></li>
<li><a href="#acknowledgments">Acknowledgments</a></li>
<li><a href="#references">References</a></li>
<li><a href="#copyright">Copyright</a></li>
</ul>
<h2 id="issues">Issues</h2>
<p>None at present.</p>
<h2 id="rationale">Rationale</h2>
<p>A characteristic property of symbols in Scheme is that “two symbols
are identical (in the sense of <code>eqv?</code>) if and only if their
names are spelled the same way” (<cite>R7RS</cite> section 6.5). Such
symbols are traditionally known as <dfn>interned</dfn> symbols. It is
sometimes useful, however, to create <dfn>uninterned</dfn> symbols
that are different from all other symbols, even those with the same
textual name.</p>
<p>Many Scheme implementations provide uninterned symbols, and they
are also part of the ANSI Common Lisp standard. Their use is almost
mandatory with unhygienic macro systems (like that of Common Lisp),
where they are used as hidden names that cannot be accidentally
captured. This issue is largely solved by Scheme’s hygienic macro
systems, but uninterned symbols still have their uses in Scheme macro
programming. They can be used as unique keys shared between
communicating macros or procedures, for example,
since there is no possibility of
collision between uninterned and user-created symbols.</p>
<p>The traditional way to create a unique symbol is through a
<code>gensym</code> procedure. The name of this procedure is
ubiquitous: it dates back at least as far as LISP 1.5, and a variant of
<code>gensym</code> is provided by almost every Scheme implementation.
It should come as no surprise that the behavior of these
<code>gensym</code> procedures varies
widely; in particular, many <code>gensym</code>s return interned
symbols (see the <a href="#prior-art">survey</a>
below for details). To increase
compatibility with existing implementations, this document specifies
<code>generate-uninterned-symbol</code>, a <code>gensym</code>-like
procedure, and
another constructor, <code>string->uninterned-symbol</code>, which
is consistent across the Scheme implementations that provide it.</p>
<h2 id="caveats">Caveats</h2>
<p>Adding uninterned symbols to a Scheme implementation changes the
semantics of symbols materially. They break the defining property of
symbols, namely, that two symbols are the same if they have the same
name. In the presence of uninterned symbols, symbol
equality (and thus general object equality in the senses of
<code>eqv?</code> and <code>equal?</code>) becomes harder to specify.
(An attempt is made below.)</p>
<p>Uninterned symbols have a long history in Lisp and Scheme, and
their behavior is mostly consistent across the Scheme implementations
that provide them. Nevertheless, the author of this SRFI is not sure
whether their benefits outweigh their drawbacks.</p>
<h2 id="specification">Specification</h2>
<p>The words “must”, “may”, etc., though not capitalized in this
SRFI, are to be interpreted as described in
<a href="https://datatracker.ietf.org/doc/html/rfc2119">RFC
2119</a>.</p>
<p>In this document, a symbol’s <dfn>textual name</dfn> refers to
the string returned when <code>symbol->string</code> is invoked
on the symbol.</p>
<p>An uninterned symbol is a symbol (in the sense of
<code>symbol?</code>) which is not identical (in the sense of
<code>symbol=?</code>) with any other symbol, even a symbol with
the same textual name. An uninterned symbol is “identical to
itself”. That is, if <var>s</var> and <var>t</var> are uninterned
symbols, <code>(symbol=?</code> <var>s</var>
<var>t</var><code>)</code> returns true if and only if <var>s</var>
and <var>t</var> were created by the same invocation of
<code>string->uninterned-symbol</code> or
<code>generate-uninterned-symbol</code>.</p>
<p>The exact external representation of an uninterned symbol is
unspecified, but the <code>read</code> procedure (<cite>R7RS</cite>
section 6.13.2) must signal an error (satisfying
<code>read-error?</code>) if it encounters such an external
representation. Thus uninterned symbols do not support the
write/read invariance described in <cite>R7RS</cite> section
6.5.</p>
<h2 id="procedures">Procedures</h2>
<p>The procedures in this section are exported by the
<code>(srfi :258 uninterned-symbols)</code> library.</p>
<p id="string-to-uninterned-symbol">
<code>(string->uninterned-symbol</code> <var>string</var><code>)</code>
→ <span class="type-meta">uninterned-symbol</span></p>
<p>Returns an uninterned symbol with a textual name given by <var>string</var>.</p>
<p id="symbol-interned"><code>(symbol-interned?</code> <var>symbol</var><code>)</code>
→ <span class="type-meta">boolean</span></p>
<p>Returns <code>#t</code> if <var>symbol</var> is an interned (ordinary)
symbol, and <code>#f</code> if it is uninterned.</p>
<p id="generate-uninterned-symbol"><code>(generate-uninterned-symbol</code>
[<var>prefix</var>]<code>)</code> →
<span class="type-meta">uninterned-symbol</span></p>
<p>Returns an uninterned symbol with a textual name that is likely to be
unique. If the optional <var>prefix</var> argument is provided and is a
string / symbol, then <var>prefix</var> / the name of <var>prefix</var> is
prepended to the resulting symbol’s name.</p>
<h4>Rationale:</h4>
<p><code>generate-uninterned-symbol</code> duplicates the behavior of
<code>gensym</code> in Scheme implementations that provide uninterned
symbols (and in which <code>gensym</code> returns an uninterned
symbol).
<h4>Implementation (informational discussion):</h4>
<p>Following <a href="#common-lisp">Common Lisp</a>, many
<code>gensym</code> implementations use a non-negative integer
counter, a representation of the value of which is appended to
the name of the returned symbol. Each call to <code>gensym</code>
increments this counter. Other implementations use Universally
Unique Identifiers (UUIDs) (ITU-T Rec. X.667, ISO/IEC
9834-8:2014) as names.</p>
<h2 id="prior-art">Prior art</h2>
<table>
<caption>Summary of existing support for uninterned symbols,
<code>gensym</code>, and related forms in Scheme.</caption>
<thead>
<tr>
<th>Implementation</th>
<th>Uninterned symbols</th>
<th><code>gensym</code></th>
<th><code>gensym</code> prefix argument</th>
<th><code>gensym</code> interned</th>
<th><code>string->uninterned-symbol</code></th>
</tr>
</thead>
<tbody>
<tr>
<td>Bigloo</td>
<td class="no">No</td>
<td class="yes">Yes</td>
<td>String or symbol</td>
<td>Yes</td>
<td class="no">No</td>
</tr>
<tr>
<td>ChezScheme</td>
<td class="yes">Yes</td>
<td class="yes">Yes</td>
<td>String</td>
<td>Yes</td>
<td class="yes">Yes</td>
</tr>
<tr>
<td>CHICKEN</td>
<td class="yes">Yes</td>
<td class="yes">Yes</td>
<td>String or symbol</td>
<td>No</td>
<td class="yes">Yes</td>
</tr>
<tr>
<td>Cyclone</td>
<td class="no">No</td>
<td class="yes">Yes</td>
<td>String or symbol</td>
<td>Yes</td>
<td class="no">No</td>
</tr>
<tr>
<td>Gambit</td>
<td class="yes">Yes</td>
<td class="yes">Yes</td>
<td>Symbol</td>
<td>No</td>
<td class="yes">Yes</td>
</tr>
<tr>
<td>Gauche</td>
<td class="yes">Yes</td>
<td class="yes">Yes</td>
<td>String</td>
<td>No</td>
<td class="yes">Yes</td>
</tr>
<tr>
<td>Guile</td>
<td class="yes">Yes</td>
<td class="yes">Yes</td>
<td>String</td>
<td>Yes</td>
<td class="no">No*</td>
<tr>
<td>Ikarus</td>
<td class="no">No</td>
<td class="yes">Yes</td>
<td>String</td>
<td>Yes</td>
<td class="no">No</td>
</tr>
<tr>
<td>Kawa</td>
<td class="no">No</td>
<td class="no">No†</td>
<td>n/a</td>
<td>n/a</td>
<td class="no">No</td>
</tr>
<tr>
<td>Larceny</td>
<td class="yes">Yes</td>
<td class="yes">Yes</td>
<td>String</td>
<td>No</td>
<td class="no">No</td>
</tr>
<tr>
<td>Loko</td>
<td class="yes">Yes</td>
<td class="yes">Yes</td>
<td>None</td>
<td>No</td>
<td class="no">No</td>
</tr>
<tr>
<td>MIT/GNU Scheme</td>
<td class="yes">Yes</td>
<td class="no">No‡</td>
<td>n/a</td>
<td>n/a</td>
<td class="yes">Yes</td>
<tr>
<td>Racket</td>
<td class="yes">Yes</td>
<td class="yes">Yes</td>
<td>String or symbol</td>
<td>No</td>
<td class="yes">Yes</td>
</tr>
<tr>
<td>STklos</td>
<td class="yes">Yes</td>
<td class="yes">Yes</td>
<td>String or symbol</td>
<td>No</td>
<td class="yes">Yes</td>
</tr>
<tr>
<td>TinySCHEME</td>
<td class="no">No</td>
<td class="yes">Yes</td>
<td>None</td>
<td>Yes</td>
<td class="no">No</td>
</tr>
</tbody>
</table>
<p>*: Guile’s <code>make-symbol</code> procedure is identical to
<code>string->uninterned-symbol</code>.</p>
<p>†: Kawa includes a
<a href="https://www.gnu.org/software/kawa/Macros.html#idm45230724012608">
zero-argument <code>gentemp</code> procedure</a> that returns an interned
symbol.</p>
<p>‡: MIT/GNU Scheme provides
<a href="https://web.mit.edu/scheme/scheme_v9.2/doc/mit-scheme-ref/Symbols.html">generate-uninterned-symbol</a>,
an extended version of
<a href="#generate-uninterned-symbol">the procedure described
above.</a></p>
<h3 id="chezscheme">ChezScheme</h3>
<p>Chez provides interned “gensyms” which are distinguished by the
<code>gensym?</code> predicate. Chez’s gensyms have “pretty” and
“unique” names. The former are created immediately and the latter are
generated lazily using an internal prefix and counter, which are
accessible through parameter objects.
<code>gensym->unique-string</code> returns the unique name of a
gensym.</p>
<p>Chez’s <code>gensym</code> procedure takes optional
<var>pretty-name</var> and <var>unique-name</var> string arguments.
The latter argument allows the unique name of one gensym to be given
to another; thus, distinct gensyms that are equal in the sense of
<code>symbol=?</code> can be created in Chez.</p>
<p>The lexical syntax <code>#:</code><var class="syn">name</var> is
used to write gensyms; <var class="syn">name</var> is the pretty
name of the resulting gensym.</p>
<p>See section 7.9 of the <cite>ChezScheme User’s Guide</cite> for
further details.</p>
<h3 id="common-lisp">Common Lisp</h3>
<p>Uninterned symbols are used extensively in Common Lisp macro
programming. The Common Lisp
HyperSpec specifies
<a href="http://clhs.lisp.se/Body/f_mk_sym.htm#make-symbol"><code>MAKE-SYMBOL</code></a>
(analogous to <code>string->uninterned-symbol</code>) and
<a href="http://clhs.lisp.se/Body/f_gensym.htm#gensym"><code>GENSYM</code></a>
procedures that construct uninterned symbols. The names of the
symbols returned by <code>GENSYM</code> usually include the value
of <code>*GENSYM-COUNTER*</code>, a non-negative integer variable,
which is incremented by each call to <code>GENSYM</code>. (This
behavior can be circumvented by providing an explicit suffix to
<code>GENSYM</code>. See the CLHS for details.)</p>
<p>Since Common Lisp does not have Scheme’s distinction between
symbols and identifiers, the implications of internedness are
much broader in Common Lisp. For example, objects named by
uninterned symbols are, in effect, private to the package in
which they were defined. A full discussion of uninterned symbols
in Common Lisp is beyond the scope of this SRFI.</p>
<h2 id="implementation">Implementation</h2>
<p>A portable implementation of uninterned symbols is
impossible. In Scheme implementations that provide them (see the
table above), uninterned symbols
and <code>string->uninterned-symbol</code> are generally
compatible with this SRFI. A <a href="srfi-258.sls">sample
implementation for ChezScheme</a> and
a <a href="tests/test-portable.scm">portable test suite</a> are
included in this SRFI’s repository.</p>
<p>Many Scheme implementations maintain a table of symbols, indexed
by name. When a symbol is created, its name is looked up in this
table; if a symbol with that name exists, it is returned;
otherwise, a fresh symbol is added to the table. In implementations
that use this or a similar strategy, uninterned symbols may be
provided by simply skipping the table-lookup and insertion
steps.</p>
<h2 id="acknowledgments">Acknowledgments</h2>
<p>Thanks to Daphne Preston-Kendal for pushing for this SRFI and to
Marc Nieper-Wißkirchen for challenging almost everything in it.</p>
<p>Thanks to those who provided reviews and commentary via the SRFI
mailing list or the <span class="channel">#scheme</span> IRC
channel.</p>
<h2 id="references">References</h2>
<p>Alex Shinn, John Cowan, & Arthur A. Gleckler, eds.,
<cite>Revised<sup>7</sup> Report on the Algorithmic Language Scheme</cite>
(R7RS Small) (2013). Available <a href="https://small.r7rs.org/">on the
Web</a>.</p>
<p>Kent M. Pitman, et al, eds., <cite>Common Lisp HyperSpec</cite>™
(CLHS) (1994). Available <a href="http://clhs.lisp.se/">on the
Web</a>.</p>
<p>Cisco Systems, Inc., <cite>Chez Scheme User’s Guide</cite> (2015).
Available <a href="https://cisco.github.io/ChezScheme/csug9.5/">on the
Web</a>.</p>
<p>S. Bradner, <cite>Key words for use in RFCs to Indicate Requirement
Levels</cite>. (RFC 2119) (1997).
https://datatracker.ietf.org/doc/html/rfc2119</p>
<h2 id="copyright">Copyright</h2>
<p>© 2025 Wolfgang Corcoran-Mathe</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>