by Lassi Kortela
This SRFI is currently in draft status. Here is an explanation of each status that a SRFI can hold. To provide input on this SRFI, please send email to srfi-243@nospamsrfi.schemers.org
. To subscribe to the list, follow these instructions. You can access previous messages via the mailing list archive.
This SRFI standardizes a widely used lexical syntax for writing unreadable objects.
None at present.
Lisp code is represented as data. A Lisp system can be asked to write any live object as an S-expression. However, it’s inevitable that some of those objects have complex environmental dependencies which are difficult or impossible to write down.
The prototypical example of such an object is a port. Other common examples are procedures, continuations, promises, parameters, environments, and libraries. Objects managed by a foreign function interface tend to be unreadable. Objects that stand in for end-of-file and unspecified values are commonly written as unreadable objects since it makes little sense to read them.
Common Lisp reserves the lexical syntax
#<...>
for unreadable objects. The language
specification says:
#<
is not valid reader syntax. The Lisp reader will signal an error of typereader-error
on encountering#<
. This syntax is typically used in the printed representation of objects that cannot be read back in.
Apart from being de jure standard in Common Lisp, this syntax is de facto standard in Scheme. It features in so many implementations that using any other syntax for unreadable objects would be going against the grain of both Scheme and Lisp as they are practiced.
MIT Scheme uses square brackets #[...]
instead of
angle brackets.
Chicken and Gambit support multi-line strings via the following syntax. (The syntax imitates the Bourne shell, which calls a multi-line string a “here-document”.)
#<<end
.
.
.
end
An arbitrary identifier can be used in place of
end
.
SRFI 107 (XML
reader syntax) uses #<
for XML tags mixed in
with Scheme code. It is implemented by Kawa. An example of this
syntax is #<da></da>
.
In both cases there is a syntactic ambiguity. Chicken, Gambit,
and Kawa all use #<
to write unreadable objects.
Since valid Scheme source code cannot contain unreadable objects,
there is no read-time ambiguity for valid code. The ambiguity
does make it harder to deal with invalid input where an
unreadable object may be silently misinterpreted as some type of
readable object or rejected with a confusing error message.
The point of unreadable objects is that the Scheme reader cannot read them. Nevertheless it can be useful for a special-purpose reader to skip them.
That’s why this SRFI stipulates a stricter syntax than is
required by Common Lisp. Specifically, each #<
must be balanced by a matching >
. The text in
between the angle brackets is expected to be zero or more datums.
Hence a #<...>
can be read just like a list
except that the delimiters are angle brackets instead of
parentheses. (Common Lisp provides a utility function
read-delimited-list
in anticipation of jobs like
this.)
The grammar in R3RS, R4RS, R5RS, or R7RS section 7.1.2. (External representations), or R6RS section 4.3. (Datum syntax), is extended as follows.
A new class <unreadable>
is added. It
matches:
#<
,<datum>
class,>
.The class <compound datum>
is extended such
that <unreadable>
becomes an instance of
it.
The standard grammar shows that a <compound
datum>
is a kind of <datum>
. This
implies that unreadable objects can be nested.
Any attempt to read <unreadable>
syntax
signals an error.
However, special-purpose readers which can skip past datums
instead of turning every datum into an object may skip
<unreadable>
datums.
The Scheme procedures write
and
display
should write unreadable
objects using #<...>
syntax.
When writing objects between the angle brackets,
write
(and not display
)
should always be used. Using
display
on some objects will yield unskippable
syntax.
There should be one or more datums between the angle brackets. (The zero-datum case is supported for the sake of simplicity and uniformity but looks perplexing.)
The first datum should be an identifier. Known identifiers are tracked in the Scheme Registry.
#<void> #<procedure> #<procedure +> #<procedure fold-left> #<environment *top*> #<input/output port stdin/out>
#<undef> #<eof> #<procedure #f 0> #<procedure append 0+> #<opcode "write-char"> #<Output-Port 140501393508544> #<Environment 140501393819456>
#<unspecified> #<procedure C_plus> #<procedure (?)> #<procedure (scheme#write x . rest)> #<promise> #<output port "(stdout)"> #<environment interaction-environment>
#<procedure (tween#quadratic-ease direction123 percent124)> #<cjson> #<sql-null-type> #<sdl2:surface> #<sdl2:rect (10 20 300 400)> #<mat 2x2 4.0 0.0 0.0 4.0 >
#<procedure #2> #<procedure #3 fold> #<promise #6> #<output-port #2 (stdout)>
#<undef> #<eof> #<closure (fold kons knil lis . more)> #<subr (+ :rest args)> #<subr "continuation"> #<promise 0x102097fa0> #<subr #<<parameter> 0x102ae8800>> #<oport (standard output) 0x100d3cc00> #<module user>
#<unspecified> #<eof> #<procedure + (#:optional _ _ . _)> #<continuation 10716dae0> #<promise #<procedure 7f9b6980e6a0 at <unknown port>:11:0 ()>> #<output: file /dev/ttys001>
#<procedure +> #<macro define> #<environment main> #<output-port /dev/stdout> #<location-proc gnu.mapping.ThreadLocation[param#1]>
#<unspecified> #<subr +> #<closure 62157904> #<eof-object>
#<unspecified> #<eof> #<subr + 0:1> #<closure #f 0:0> #<<promise> 0x10d5200c0> #<<parameter> 0x1061970a0> #<transcoded-port utf8-codec #<buffered-port #<binary-output-port stdout>>>
A sample implementation that runs on Gauche is in the Git repo.
Thanks to Common Lisp for blazing the trail, and to the Scheme implementers who followed suit.
The Common Lisp HyperSpec, section 2.4.8.20 (Sharpsign Less-Than-Sign)
© 2022 Lassi Kortela.
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:
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
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.