Modules and static interpretation¶
This page states what a module is, how names resolve across modules, and how a module graph becomes the single program that the rest of the semantics is about. The surface is described in the modules reference.
Modules¶
A module is a triple \((\iota, I, B)\) (Rabe and Kohlhase, 2013):
- its identity \(\iota\), which for a source module is its path, a sequence of identifier segments;
- its interface \(I\), a finite map from public names to symbols \((\iota', x)\), where \(\iota'\) is the identity of the module that declares the name and \(x\) the name there;
- its body \(B\), its declarations in source order.
A module graph has an edge \(\iota \to \iota'\) for each import in \(\iota\) of \(\iota'\). Loading refuses a graph with a cycle; the refusal names the cycle's modules and imports, so every graph that is interpreted is acyclic and has a topological order.
Scopes¶
Write \(\mathrm{decl}(B)\) for the names a body declares. For a module \(\iota\) with imports \(m_1, \ldots, m_k\), each import \(m_j\) reaches the subset \(V_j\) of its interface that its selection list or exclusion leaves, through a qualifier \(q_j\) (its alias, or its path when it has none). The unqualified scope is
where \(S\) holds the imports with a selection or an exclusion and the imports some open names
(an open with its own list restricts \(V_j\) again). \(U(\iota)\) is a relation from names to symbols,
not a function: two imports may reach one name.
A token \(t = s_1.s_2 \cdots s_n\) at a position whose innermost binder does not bind \(s_1\) resolves as follows.
- If \(s_1 \in \mathrm{decl}(B)\), it names \((\iota, s_1)\).
- Otherwise, if a prefix \(s_1 \cdots s_\ell\) is a qualifier \(q_j\), then \(s_{\ell+1}\) must be in \(V_j\), and the token names \(V_j(s_{\ell+1})\). A name public in \(m_j\) but excluded is refused as not imported; a name \(m_j\) declares but does not export is refused as private; any other name is refused as unknown, with the names of \(V_j\) closest to it.
- Otherwise, if \(U(\iota)\) relates \(s_1\) to exactly one symbol, the token names it; if it relates \(s_1\) to two distinct symbols, the token is refused as ambiguous.
- Otherwise the token is left as written: it is a builtin or an unbound name, which the checker reports.
The remaining segments are a member path of the named declaration (a generated declaration, \(G.\mathit{Item}\); a family's constructor, \(F.c\)), except in a morphism expression after a morphism-valued declaration, and anywhere after a local value, where they select members of the value. Ambiguity in step 3 compares symbols, not spellings, so a declaration reached along two import paths is one symbol and never ambiguous with itself.
The interface of \(\iota\) maps each exported name of \(\mathrm{decl}(B)\) to \((\iota, x)\), each name
exported by export x to the symbol \(x\) resolves to, and each name of an export import to the
symbol the import reaches. Two distinct symbols under one exported name are refused. The
interface is computed from \(B\) and the interfaces of the imports alone, never their bodies.
Static interpretation¶
Fix a root \(\rho\). The flat name of a symbol is
and a member path extends it, \(\phi_\rho(\iota, G).\mathit{Item}\). Since : is not a character of
source names and \(\iota\) is part of every non-root flat name, \(\phi_\rho\) is injective on symbols.
The interpretation of the graph is the program obtained by visiting the modules in topological order and emitting each module's declarations with every declaring and resolved token replaced by its flat name; imports and opens are removed, and so are the exports of every module but the root. Each module is visited once, so a diamond contributes its declarations once.
Two properties follow from the construction.
- Renaming. Within one module, every token that resolves to a symbol is replaced by a name that depends on the symbol alone, and binders and the tokens they bind are untouched. The interpretation of a module is therefore an \(\alpha\)-renaming of the module, with the module-level names drawn from a namespace that no other module shares. Its meaning, under the typing and the programs as kernels semantics, is the meaning of the module with those names.
- Splitting. A single-file program and a split of it into modules (each declaration moved to one module, each reference qualified or imported) interpret to programs that differ only in the names of the moved declarations. The test suite pins this for corpus programs: their results on the reference machine are identical before and after the split.
Generated declarations are named by their parent's flat name and a member path, so a generated declaration of an imported deduction is \(\iota\!:\!G.\mathit{Item}\) and no generated name can equal an authored one.
Modules declared inside modules, signatures, functors, sealing, and module parameters of computations extend this construction with identities for applications and sealings; they are described in signatures, functors, and their static interpretation.
Cited sources¶
Florian Rabe and Michael Kohlhase. A Scalable Module System. Information and Computation, 230:1–54, 2013. doi:10.1016/j.ic.2013.06.001.