What does it mean for Scheme library to be *loaded*? When are Scheme libraries *loaded*?

306 views Asked by At

I am studying the Revised7 Report on the Algorithmic Language Scheme. My question is on section 5.6 Libraries.

In this section, it says:

When a library is loaded, its expressions are executed in textual order. If a library's definitions are referenced in the expanded form of a program or library body, then that library must be loaded before the expanded program or library body is evaluated. This rule applies transitively. If a library is imported by more than one program or library, it may possibly be loaded additional times.

What is this supposed to mean? Does it mean that a library is loaded only if an imported identifier is actually being referenced or already when the library is part of an import set of an expanded program or library? If the same library is being referenced by two other library imported by the same program, is the library loaded twice or just once?

As the loading of a library may have side-effects due to the execution of its expressions, the answers to these questions seem important to me. Also, do share the two libraries that import a third library its internal global variables?

I have done some experiments with chibi-scheme: Per program, chibi-scheme loads every library only once and even if none of its exported identifiers are actually referenced. In fact, this looks like a sensible and easily implementable thing to me.

P.S.: There is another point where I think the specification is a bit vague: What happens if, in a program, an import set imports an identifier named import? Does it mean that an immediately following line (import ...) is interpretated as a command or definition (depending on what the imported identifier import stands for) or still as an import set?

P.P.S.: What is even the reason for allowing more than one import declaration in a top-level program?

1

There are 1 answers

1
Justin Ethier On BEST ANSWER

Let me attempt to answer each of your questions one-at-a-time. Also, if it would help, here is a link to the meta-language used to implement libraries in chibi scheme.


A library is loaded when it is imported via an import statement.


If a library's definitions are referenced in the expanded form of a program or library body, then that library must be loaded before the expanded program or library body is evaluated.

This just means that the library must be loaded before its definitions are referenced (or it would be an error, since the definitions would not be found).


If a library is imported by more than one program or library, it may possibly be loaded additional times.

This is implementation-dependent, so your library code should not make assumptions that it will only be loaded once.


What happens if, in a program, an import set imports an identifier named import?

Most likely, the new import identifier would shadow or replace the import, so that any import statements in the same scope would no longer work as expected. This might be implementation-dependent - if import is implemented as a special form then it would not be overridden by a newly introduced identifier.