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 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
|
\chapter{Libraries}
\label{librarychapter}
\mainindex{library}
Libraries are parts of a program that can be distributed
independently.
The library system supports macro definitions within libraries,
macro exports, and distinguishes the phases in which definitions
and imports are needed. This chapter defines the notation for
libraries and a semantics for library expansion and execution.
\section{Library form}
\label{librarysyntaxsection}
A library definition must have the following form:\mainschindex{library}\mainschindex{import}\mainschindex{export}
\begin{scheme}
(library \hyper{library~name}
(export \hyper{export~spec} \ldots)
(import \hyper{import~spec} \ldots)
\hyper{library~body})%
\end{scheme}
A library declaration contains the following elements:
\begin{itemize}
\item The \hyper{library~name} specifies the name of the library
(possibly with version).
\item The {\cf export} subform specifies a list of exports, which name
a subset of the bindings defined within or imported into the
library.
\item The {\cf import} subform specifies the imported bindings as a
list of import dependencies, where each dependency specifies:
\begin{itemize}
\item the imported library's name, and, optionally, constraints on its
version,
\item the relevant levels, e.g., expand or run time (see
section~\ref{phasessection}, and
\item the subset of the library's exports to make available within the
importing library, and the local names to use within the importing
library for each of the library's exports.
\end{itemize}
\item The \hyper{library body} is the library body, consisting of a
sequence of definitions followed by a sequence of expressions. The
definitions may be both for local (unexported) and exported
bindings, and the expressions are initialization expressions to be evaluated
for their effects.
\end{itemize}
An identifier can be imported with the same local name from two or
more libraries or for two levels from the same library only if the
binding exported by each library is the same (i.e., the binding is
defined in one library, and it arrives through the imports only by
exporting and re-exporting). Otherwise, no identifier can be imported
multiple times, defined multiple times, or both defined and imported.
No identifiers are visible within a library except for those
explicitly imported into the library or defined within the library.
A \hyper{library name} uniquely identifies a library within an
implementation, and is globally visible in the {\cf import} clauses
(see below) of all other libraries within an implementation.
A \hyper{library name} has the following form:
\begin{scheme}
(\hyperi{identifier} \hyperii{identifier} \ldots \hyper{version})%
\end{scheme}
where \hyper{version} is empty or has the following form:
%
\begin{scheme}
(\hyper{sub-version} \ldots)%
\end{scheme}
Each \hyper{sub-version} must represent an exact nonnegative integer object.
An empty \hyper{version} is equivalent to {\cf ()}.
An \hyper{export~spec} names a set of imported and locally defined bindings to
be exported, possibly with different
external names. An \hyper{export~spec} must have one of the
following forms:
\begin{scheme}
\hyper{identifier}
(rename (\hyperi{identifier} \hyperii{identifier}) \ldots)%
\end{scheme}
In an \hyper{export~spec}, an \hyper{identifier} names a single binding defined
within or imported into the library, where the external name for the export is
the same as the name of the binding within the library.
A {\cf rename} spec exports the binding named by
\hyperi{identifier} in each {\cf (\hyperi{identifier}
\hyperii{identifier})} pairing, using \hyperii{identifier} as the
external name.
Each \hyper{import~spec} specifies a set of bindings to be imported into
the library, the levels at which they are to be available, and the local
names by which they are to be known. An \hyper{import spec} must
be one of the following:
%
\begin{scheme}
\hyper{import set}
(for \hyper{import~set} \hyper{import~level} \ldots)%
\end{scheme}
An \hyper{import level} is one of the following:
\begin{scheme}
run
expand
(meta \hyper{level})%
\end{scheme}
where \hyper{level} represents an exact integer object.
As an \hyper{import level}, {\cf run} is an abbreviation for {\cf
(meta 0)}, and {\cf expand} is an abbreviation for {\cf (meta 1)}.
Levels and phases are discussed in section~\ref{phasessection}.
An \hyper{import~set} names a set of bindings from another library and
possibly specifies local names for the imported bindings. It must be
one of the following:
\begin{scheme}
\hyper{library~reference}
(library \hyper{library~reference})
(only \hyper{import~set} \hyper{identifier} \ldots)
(except \hyper{import~set} \hyper{identifier} \ldots)
(prefix \hyper{import~set} \hyper{identifier})
(rename \hyper{import~set} (\hyperi{identifier} \hyperii{identifier}) \ldots)%
\end{scheme}
A \hyper{library~reference} identifies a library by its
name and optionally by its version. It has one of the following forms:
\begin{scheme}
(\hyperi{identifier} \hyperii{identifier} \ldots)
(\hyperi{identifier} \hyperii{identifier} \ldots \hyper{version~reference})%
\end{scheme}
A \hyper{library~reference} whose first \hyper{identifier} is
{\cf for}, {\cf library}, {\cf only}, {\cf except}, {\cf prefix}, or {\cf rename} is
permitted only within a {\cf library} \hyper{import~set}.
The \hyper{import~set} {\cf (library \hyper{library~reference})} is
otherwise equivalent to \hyper{library~reference}.
A \hyper{library~reference} with no \hyper{version~reference}
(first form above) is equivalent to a \hyper{library~reference} with a
\hyper{version~reference} of {\cf ()}.
A \hyper{version~reference} specifies a set of \hyper{version}s that
it matches. The \hyper{library~reference} identifies all libraries of
the same name and whose version is matched by the
\hyper{version~reference}. A \hyper{version~reference} has
the following form:
%
\begin{scheme}
(\hyperi{sub-version reference} \ldots \hypern{sub-version reference})
(and \hyper{version reference} \ldots)
(or \hyper{version reference} \ldots)
(not \hyper{version reference})%
\end{scheme}
%
A \hyper{version reference} of the first form matches a \hyper{version}
with at least $n$ elements, whose \hyper{sub-version reference}s match
the corresponding \hyper{sub-version}s. An {\cf and} \hyper{version
reference} matches a version if all \hyper{version references}
following the {\cf and} match it. Correspondingly, an {\cf
or} \hyper{version reference} matches a version if one of
\hyper{version references} following the {\cf or} matches it,
and a {\cf not} \hyper{version reference} matches a version if the
\hyper{version reference} following it does not match it.
A \hyper{sub-version reference} has one of the following forms:
\begin{scheme}
\hyper{sub-version}
(>= \hyper{sub-version})
(<= \hyper{sub-version})
(and \hyper{sub-version~reference} \ldots)
(or \hyper{sub-version~reference} \ldots)
(not \hyper{sub-version~reference})%
\end{scheme}
A \hyper{sub-version reference} of the first form matches a
\hyper{sub-version} if it is equal to it. A {\cf >=}
\hyper{sub-version reference} of the first form matches a sub-version
if it is greater or equal to the \hyper{sub-version} following it;
analogously for {\cf <=}. An {\cf and} \hyper{sub-version reference}
matches a sub-version if all of the subsequent \hyper{sub-version
reference}s match it. Correspondingly, an {\cf or}
\hyper{sub-version reference} matches a sub-version if one of the
subsequent \hyper{sub-version reference}s matches it, and a {\cf not}
\hyper{sub-version reference} matches a sub-version if the subsequent
\hyper{sub-version reference} does not match it.
Examples:
\texonly\begin{center}\endtexonly
\begin{tabular}{lll}
version reference & version & match?
\\
{\cf ()} & {\cf (1)} & yes\\
{\cf (1)} & {\cf (1)} & yes\\
{\cf (1)} & {\cf (2)} & no\\
{\cf (2 3)} & {\cf (2)} & no\\
{\cf (2 3)} & {\cf (2 3)} & yes\\
{\cf (2 3)} & {\cf (2 3 5)} & yes\\
{\cf (or (1 (>= 1)) (2))} & {\cf (2)} & yes\\
{\cf (or (1 (>= 1)) (2))} & {\cf (1 1)} & yes\\
{\cf (or (1 (>= 1)) (2))} & {\cf (1 0)} & no\\
{\cf ((or 1 2 3))} & {\cf (1)} & yes\\
{\cf ((or 1 2 3))} & {\cf (2)} & yes\\
{\cf ((or 1 2 3))} & {\cf (3)} & yes\\
{\cf ((or 1 2 3))} & {\cf (4)} & no
\end{tabular}
\texonly\end{center}\endtexonly
When more than one library is identified by a library reference, the
choice of libraries is determined in some implementation-dependent manner.
To avoid problems such as incompatible types and replicated state,
implementations should prohibit the two libraries whose library names
consist of the same sequence of identifiers but whose versions do not
match to co-exist in the same program.
By default, all of an imported library's exported bindings are made
visible within an importing library using the names given to the bindings
by the imported library.
The precise set of bindings to be imported and the names of those
bindings can be adjusted with the {\cf only}, {\cf except},
{\cf prefix}, and {\cf rename} forms as described below.
\begin{itemize}
\item An {\cf only} form produces a subset of the bindings from another
\hyper{import~set}, including only the listed
\hyper{identifier}s.
The included \hyper{identifier}s must be in
the original \hyper{import~set}.
\item An {\cf except} form produces a subset of the bindings from another
\hyper{import~set}, including all but the listed
\hyper{identifier}s.
All of the excluded \hyper{identifier}s must be in
the original \hyper{import~set}.
\item A {\cf prefix} form adds the \hyper{identifier} prefix to each
name from another \hyper{import~set}.
\item A {\cf rename} form, {\cf (rename (\hyperi{identifier} \hyperii{identifier}) \ldots)},
removes the bindings for {\cf \hyperi{identifier} \ldots} to form an
intermediate \hyper{import~set}, then adds the bindings back for the
corresponding {\cf \hyperii{identifier} \ldots} to form the final
\hyper{import~set}.
Each \hyperi{identifier} must be in the original \hyper{import~set},
each \hyperii{identifier} must not be in the intermediate \hyper{import~set},
and the \hyperii{identifier}s must be distinct.
\end{itemize}
It is a syntax violation if a constraint given above is not met.
\label{librarybodysection}
The \hyper{library~body} of a {\cf library} form consists of forms
that are classified as
\textit{definitions}\mainindex{definition} or
\textit{expressions}\mainindex{expression}. Which forms belong to
which class depends on the imported libraries and the result of
expansion---see chapter~\ref{expansionchapter}. Generally, forms that
are not
definitions (see section~\ref{defines} for definitions available
through the base library) are expressions.
A \hyper{library~body} is like a \hyper{body} (see section~\ref{bodiessection}) except that
a \hyper{library~body}s need not include any expressions. It must
have the following form:
\begin{scheme}
\hyper{definition} \ldots \hyper{expression} \ldots%
\end{scheme}
When {\cf begin}, {\cf let-syntax}, or {\cf letrec-syntax} forms
occur in a top-level body prior to the first
expression, they are spliced into the body; see section~\ref{begin}.
Some or all of the body, including portions wrapped in {\cf begin},
{\cf let-syntax}, or {\cf letrec-syntax}
forms, may be specified by a syntactic abstraction
(see section~\ref{macrosection}).
The transformer expressions and bindings are evaluated and created
from left to right, as described in chapter~\ref{expansionchapter}.
The expressions of variable definitions are evaluated
from left to right, as if in an implicit {\cf letrec*},
and the body expressions are also evaluated from left to right
after the expressions of the variable definitions.
A fresh location is created for each exported variable and initialized
to the value of its local counterpart.
The effect of returning twice to the continuation of the last body
expression is unspecified.
\begin{note}
The names {\cf library}, {\cf export}, {\cf import},
{\cf for}, {\cf run}, {\cf expand}, {\cf meta},
{\cf import}, {\cf export}, {\cf only}, {\cf except}, {\cf
prefix}, {\cf rename}, {\cf and}, {\cf or}, {\cf not}, {\cf >=}, and {\cf <=}
appearing in the library syntax are part of the
syntax and are not reserved, i.e., the same names can be used for other
purposes within the library or even exported from or imported
into a library with different meanings, without affecting their
use in the {\cf library} form.
\end{note}
Bindings defined with a library are not visible in code
outside of the library, unless the bindings are explicitly exported from the
library.
An exported macro may, however, \emph{implicitly export} an otherwise
unexported identifier defined within or imported into the library.
That is, it may insert a reference to that identifier into the output code
it produces.
\label{importsareimmutablesection}
All explicitly exported variables are immutable in both the
exporting and importing libraries.
It is thus a syntax violation if an
explicitly exported variable appears on the left-hand side of a {\cf set!}
expression, either in the exporting or importing libraries.
All implicitly exported variables are also immutable in both the
exporting and importing libraries.
It is thus a syntax violation if a
variable appears on the left-hand side of a {\cf set!}
expression in any code produced by an exported macro outside of the
library in which the variable is defined.
It is also a syntax violation if a
reference to an assigned variable appears in any code produced by
an exported macro outside of the library in which the variable is defined,
where an assigned variable is one that appears on the left-hand
side of a {\cf set!} expression in the exporting library.
All other variables defined within a library are mutable.
\section{Import and export levels}
\label{phasessection}
Expanding a library may require run-time information from another
library. For example, if a macro transformer calls a
procedure from library $A$, then the library $A$ must be
instantiated before expanding any use of the macro in library $B$. Library $A$ may
not be
needed when library $B$ is eventually run as part of a program, or it
may be needed for run time of library $B$, too. The library
mechanism distinguishes these times by phases, which are explained in
this section.
Every library can be characterized by expand-time information (minimally,
its imported libraries, a list of the exported keywords, a list of the
exported variables, and code to evaluate the transformer expressions) and
run-time information (minimally, code to evaluate the variable definition
right-hand-side expressions, and code to evaluate the body expressions).
The expand-time information must be available to expand references to
any exported binding, and the run-time information must be available to
evaluate references to any exported variable binding.
\mainindex{phase}
%
A \emph{phase} is a time at which the expressions within a library are
evaluated.
Within a library body, top-level expressions and
the right-hand sides of {\cf define} forms are evaluated at run time,
i.e., phase $0$, and the right-hand
sides of {\cf define-syntax} forms are evaluated at expand time, i.e.,
phase $1$.
When {\cf define-syntax},
{\cf let-syntax}, or {\cf letrec-syntax}
forms appear within code evaluated at phase $n$, the right-hand sides
are evaluated at phase $n+1$.
These phases are relative to the phase in which the library itself is
used.
An \defining{instance} of a library corresponds to an evaluation of its
variable definitions and expressions in a particular phase relative to another
library---a process called \defining{instantiation}.
For example, if a top-level expression in a library $B$ refers to
a variable export from another library $A$, then it refers to the export from an
instance of $A$ at phase $0$ (relative to the phase of $B$).
But if a phase $1$ expression within $B$ refers to the same binding from
$A$, then it refers to the export from an instance of $A$ at phase $1$
(relative to the phase of $B$).
A \defining{visit} of a library corresponds to the evaluation of its syntax
definitions in a particular phase relative to another
library---a process called \defining{visiting}.
For example, if a top-level expression in a library $B$ refers to
a macro export from another library $A$, then it refers to the export from a
visit of $A$ at phase $0$ (relative to the phase of $B$), which corresponds
to the evaluation of the macro's transformer expression at phase $1$.
\mainindex{level}\mainindex{import level}
%
A \emph{level} is a lexical property of an identifier that determines
in which phases it can be referenced. The level for each identifier
bound by a definition within a library is $0$; that is, the identifier
can be referenced only at phase $0$ within the library.
The level for each imported binding is determined by the enclosing {\cf
for} form of the {\cf import} in the importing library, in
addition to the levels of the identifier in the exporting
library. Import and export levels are combined by pairwise addition of
all level combinations. For example, references to an imported
identifier exported for levels $p_a$ and $p_b$ and imported for levels
$q_a$, $q_b$, and $q_c$ are valid at levels $p_a+q_a$, $p_a+q_b$,
$p_a+q_c$, $p_b+q_a$, $p_b+q_b$, and $p_b+q_c$. An \hyper{import~set}
without an enclosing {\cf for} is equivalent to {\cf (for
\hyper{import~set} run)}, which is the same as {\cf (for
\hyper{import~set} (meta 0))}.
The export level of an exported binding is $0$ for all bindings
that are defined within the exporting library. The export levels of a
reexported binding, i.e., an export imported from another library, are the
same as the effective import levels of that binding within the reexporting
library.
For the libraries defined in the library report, the export level is
$0$ for nearly all bindings. The exceptions are {\cf syntax-rules},
{\cf identifier-syntax}, {\cf ...}, and {\cf \_} from the
\rsixlibrary{base} library, which are exported with level $1$, {\cf
set!} from the \rsixlibrary{base} library, which is exported with
levels $0$ and $1$, and all bindings from the composite
\thersixlibrary{} library (see library
chapter~\extref{lib:complibchapter}{Composite library}), which are
exported with levels $0$ and $1$.
Macro expansion within a library can introduce a reference to an
identifier that is not explicitly imported into the library. In that
case, the phase of the reference must match the identifier's level as
shifted by the difference between the phase of the source library
(i.e., the library that supplied the identifier's lexical context) and
the library that encloses the reference. For example, suppose that
expanding a library invokes a macro transformer, and the evaluation of
the macro transformer refers to an identifier that is exported from
another library (so the phase-$1$ instance of the library is used);
suppose further that the value of the binding is a syntax object
representing an identifier with only a level-$n$ binding; then, the
identifier must be used only at phase $n+1$ in the
library being expanded. This combination of levels and phases is why
negative levels on identifiers can be useful, even though libraries
exist only at non-negative phases.
If any of a library's definitions are referenced at phase $0$ in the
expanded form of a program, then an instance of the referenced library
is created for phase $0$ before the program's definitions and
expressions are evaluated. This rule applies transitively: if the
expanded form of one library references at phase $0$ an identifier
from another library, then before the referencing library is
instantiated at phase $n$, the referenced library must be instantiated
at phase $n$. When an identifier is referenced at any phase $n$
greater than $0$, in contrast, then the defining library is
instantiated at phase $n$ at some unspecified time before the
reference is evaluated. Similarly, when a macro keyword is referenced at
phase $n$ during the expansion of a library, then the
defining library is visited at phase $n$ at some unspecified time
before the reference is evaluated.
An implementation may distinguish instances/visits of a library for
different phases or to use an instance/visit at any phase as an instance/visit at
any other phase. An implementation may further
expand each {\cf library} form with distinct
visits of libraries in any phase and/or instances of
libraries in phases above $0$. An implementation may
create instances/visits of more libraries at more phases than required to
satisfy references. When an identifier appears as an expression in a
phase that is inconsistent with the identifier's level, then an
implementation may raise an exception either at expand time or run
time, or it may allow the reference. Thus, a library whose meaning depends on whether the
instances of a library are distinguished or shared across phases or
{\cf library} expansions may be unportable.
\section{Examples}
Examples for various \hyper{import~spec}s and \hyper{export~spec}s:
\begin{scheme}
(library (stack)
(export make push! pop! empty!)
(import (rnrs))
(define (make) (list '()))
(define (push! s v) (set-car! s (cons v (car s))))
(define (pop! s) (let ([v (caar s)])
(set-car! s (cdar s))
v))
(define (empty! s) (set-car! s '())))
(library (balloons)
(export make push pop)
(import (rnrs))
(define (make w h) (cons w h))
(define (push b amt)
(cons (- (car b) amt) (+ (cdr b) amt)))
(define (pop b) (display "Boom! ")
(display (* (car b) (cdr b)))
(newline)))
(library (party)
;; Total exports:
;; make, push, push!, make-party, pop!
(export (rename (balloon:make make)
(balloon:push push))
push!
make-party
(rename (party-pop! pop!)))
(import (rnrs)
(only (stack) make push! pop!) ; not empty!
(prefix (balloons) balloon:))
;; Creates a party as a stack of balloons,
;; starting with two balloons
(define (make-party)
(let ([s (make)]) ; from stack
(push! s (balloon:make 10 10))
(push! s (balloon:make 12 9))
s))
(define (party-pop! p)
(balloon:pop (pop! p))))
(library (main)
(export)
(import (rnrs) (party))
(define p (make-party))
(pop! p) ; displays "Boom! 108"
(push! p (push (make 5 5) 1))
(pop! p)) ; displays "Boom! 24"%
\end{scheme}
Examples for macros and phases:
\begin{schemenoindent}
(library (my-helpers id-stuff)
(export find-dup)
(import (rnrs))
(define (find-dup l)
(and (pair? l)
(let loop ((rest (cdr l)))
(cond
[(null? rest) (find-dup (cdr l))]
[(bound-identifier=? (car l) (car rest))
(car rest)]
[else (loop (cdr rest))])))))
(library (my-helpers values-stuff)
(export mvlet)
(import (rnrs) (for (my-helpers id-stuff) expand))
(define-syntax mvlet
(lambda (stx)
(syntax-case stx ()
[(\_ [(id ...) expr] body0 body ...)
(not (find-dup (syntax (id ...))))
(syntax
(call-with-values
(lambda () expr)
(lambda (id ...) body0 body ...)))]))))
(library (let-div)
(export let-div)
(import (rnrs)
(my-helpers values-stuff)
(rnrs r5rs))
(define (quotient+remainder n d)
(let ([q (quotient n d)])
(values q (- n (* q d)))))
(define-syntax let-div
(syntax-rules ()
[(\_ n d (q r) body0 body ...)
(mvlet [(q r) (quotient+remainder n d)]
body0 body ...)])))%
\end{schemenoindent}
%%% Local Variables:
%%% mode: latex
%%% TeX-master: "r6rs"
%%% End:
|