Marc Nieper-Wißkirchen
This SRFI defines a mechanism to define auxiliary syntax keywords
independently in different modules that still have the same binding so
that they can be used interchangedly as literal identifiers
in syntax-rules
and syntax-case
expressions and
can be both imported under the same name without conflicts.
Literal identifiers in syntax-rules
and syntax-case
expressions match another
identifier if and only if both identifiers have the same lexical
binding or if both identifiers are the same and both have no
lexical binding. This equivalence relation is the same as the
one defined by free-identifier=?
from R4RS and
R6RS.
Macro writers that need these literal identifiers as part of specific surrounding forms have therefore two choices. Either they match against a bound or against an unbound identifier.
Matching against an unbounded identifier is problematic because an unbounded identifier cannot be renamed.
Usually, as in the derived forms defined in R6RS and R7RS, the
literal identifiers are therefore bound. Their bindings are
called auxiliary syntax in R6RS and R7RS. They are bound to a
unique transformer for that syntax. In other words, two
identifiers naming auxiliary syntax match
(are free-identifier=?
) if and only if they are
bound to the same transformer.
This has a different kind of problem. Often, the names for
auxiliary syntax are either short common words
(like else) or simple symbolic identifiers
(like =>). Therefore, independent libraries may
each export an auxiliary keyword but all with the same name. This is
problematic when the bindings are not mutually the same (that
is, the identifiers are not free-identifier=?
).
This SRFI defines therefore a mechanism for independent libraries to independently export auxiliary syntax that have mutually the same bindings.
If syntax parameters are supported, this auxiliary syntax is a
syntax parameter so that it can be hygienically be rebound
through syntax-parameterize
.
This SRFI also defines a facility to define auxiliary syntax in the above sense lexically locally. This can be used to bring back auxiliary syntax back into scope and can be interesting for macro writers.
In the mailing list to SRFI 177, the idea of hygienic keywords (where keywords are understood in the sense of SRFI 177) has been discussed. Arguments in favor of hygienic keywords were the avoidance of any runtime overhead and that they bring in all the advantages of the usual Scheme macro hygiene. One argument against hygienic keywords was that independent libraries may want to independently export hygienic keywords under the same name without conflicts. That is now possible with this SRFI.
This SRFI defines one syntax binding
form, define-auxiliary-syntax
.
(define-auxiliary-syntax〈keyword〉〈symbol〉)
This form is a definition that can be used wherever syntax definitions are allowed.
The 〈keyword〉
is
bound to (auxiliary) syntax named by 〈symbol〉
.
A 〈keyword〉
bound
through define-auxiliary-syntax
has the same
binding as another identifier (that is, they
are free-identifier=?
) if and only if the other
identifier is also bound
through define-auxiliary-syntax
and with the same
name.
In a Scheme supporting syntax parameters (see SRFI 139), the auxiliary syntax are syntax parameters.
This SRFI defines two libraries, (srfi 206)
and (srfi 206 *)
.
The define-auxiliary-syntax
keyword is (the sole
identifier) exported by the library (srfi 206)
.
The library (srfi 206 *)
is a (magic) library that
can, in import sets, only be referenced in the form (only
(srfi 206 *) 〈identifier〉…)
. This import set imports
the 〈identifier〉
s, each bound at top-level as if
through define-auxiliary-syntax
to auxiliary syntax
with the respective name.
In a Scheme implementation supporting this SRFI, all auxiliary
syntax, in particular the
identifiers else
, =>
, unquote
,
unquote-splicing
, _
, ...
defined in (scheme base)
behave as if defined
through define-auxiliary-syntax
with their
respective names.
Syntax parameters that have no sensible initial binding
(like yield
of SRFI
190) shall be bound through define-auxiliary-syntax
.
This SRFI cannot be implemented portably in R6RS or R7RS alone. A sample implementation for Chibi-Scheme will be provided.
In
the original
syntax model of the syntax-case expander by R. Kent Dybvig et
al. two bound identifiers are free-identifier=?
if and only if their associated labels are the same. An
implementation following this model thus has to maintain a
(weak) hash-table mapping (symbolic) names to labels. Whenever
auxiliary syntax with some name is defined the associated label
is looked up or, if it does not exist yet, created on the fly.
In a given environment, these labels resolve to suitable
transformers.
Credit goes to Adam Nelson, Jim Rees, Alex Shinn, and everyone else who initially discussed the idea for this SRFI on the SRFI 197 mailing list.
Special thanks go to John Cowan for reviewing a pre-draft of this SRFI.
Copyright © Marc Nieper-Wißkirchen (2020).
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.