Modules¶
Every .qvr file is a module. A module declares names, keeps them private unless it exports
them, and reaches other modules' public names through import. A module can also declare modules,
signatures, and functors inside it, and computations can take modules as parameters; those are
described in signatures, functors, and module parameters. The module graph is interpreted
statically into one flat program before anything is compiled, so the
reference machine and every transpile target see a
single program in which each declaration has one fully qualified name
(Cardelli, 1997; Elsman et al., 2018).
Module paths¶
A module's path follows the directory layout below a search root: the file
syntax/lambda.qvr below a root is the module syntax.lambda. A file given to qvr check,
qvr run, or quivers.dsl.load is found relative to its own directory unless
search roots are given (--root DIR, or roots= in Python). A dotted module path given on the
command line is resolved below the roots, the working directory by default:
qvr check pkg.experiments.boolean --root .
qvr run pkg.experiments.boolean:fit '[0.5]'
qvr run pkg/experiments/boolean.qvr fit --root .
A module provided by two roots is refused (module-ambiguous-path), and a path that names no
module is refused with the module paths it is close to (module-unknown).
Imports¶
| Form | Reaches |
|---|---|
import syntax.lambda |
each public name as syntax.lambda.name |
import syntax.lambda as L |
each public name as L.name |
import syntax.lambda (Term, normalize) |
the listed names, unqualified and through the path |
import syntax.lambda hiding (shift) |
every public name except shift, unqualified and through the path |
open L |
every name the import of L reaches, unqualified |
open L (Term), open L hiding (shift) |
the listed names, or all but the listed names, unqualified |
meta import tools.arith |
the public names, for the meta phase only |
open names an alias or the path of an import. Imports and opens come before the module's
declarations. An unqualified name resolves to the innermost of: a local binder, a declaration of
the module itself, and a name brought in by a selection list, an exclusion, or an open. Two
such imports that bring in different declarations under one name make that name ambiguous: a
use of it is refused (name-ambiguous) with both sources, and hiding or a qualified path
removes the ambiguity. Two imports that bring in the same declaration never clash.
A module may not import itself, directly or through other modules. A cycle is refused at the
import that closes it, with the whole path (module-cycle):
cyc/c.qvr:2:7: error[module-cycle]: import cycle: cyc.a -> cyc.b -> cyc.c -> cyc.a; the module graph must be acyclic
Visibility and exports¶
A declaration is private to its module unless export precedes it. export applies to every
declaration kind (families, index sorts, effects, instances, handlers, computations, programs,
morphisms, objects, deductions, signatures, encoders, relations, and the rest), and
export NAME exports a declared or imported name. export import M re-exports every name the
import brings in.
import std.data as D (Option)
export import std.fn
#! The doubled value.
export define double(x : Real) : Real !{} =
return x + x
define helper(x : Real) : Real !{} =
return x
export D.Option
An importer that names a private declaration is refused (name-private), and one that names a
name the module does not declare at all is refused with its near misses (name-unknown):
main.qvr:4:15: error[name-private]: 'helper' is private to module arith.basic; export it there to use it here
main.qvr:4:15: error[name-unknown]: module arith.basic exports no 'dobule'; did you mean 'double'?
A name spelled like a word that starts a declaration (encoder, loss, program) is exported
by name in parentheses, export (encoder), since export encoder begins an exported encoder.
export takes declarations and names only: an expression is bound with define and that name
exported.
Privacy governs the module graph. The module given to qvr run or load is the root, and it
sees its whole body: any computation or program it declares is an entry point, exported or not.
Among the root's exports, the first that is a morphism or a program is the compiled
Program's primary morphism, and later ones are attached by name.
Qualified and generated names¶
A name is a dotted path in every position. Its first segments name a module (an alias or an import path), then a declaration of that module, then, for a declaration that generates others, the member path of a generated declaration:
| Written | Names |
|---|---|
L.normalize |
normalize of the module imported as L |
G.Item |
the item family that deduction G generates |
G.read_goal.Outcomes |
the outcome family of G's selector read_goal |
G.eq, G.run |
G's structural equality and run computations |
L.G.Item |
the item family of the deduction G of the module L |
Vec.Cons |
the constructor Cons of the family Vec |
Compiler-generated members use their parent and member path. The
generated computation graphs list what each declaration generates.
An explicit splice can introduce a top-level name, such as Term.
Trailing segments after a value select its members: chart.goal_weight()
calls a method of the local chart, and f.dagger or f.change_base(phi) is a postfix form of
the morphism f.
Identity and the flat program¶
A module is a triple of its identity, its interface, and its body. The identity of a source module is its path; the interface lists its public names, each with the identity of the module that declares it; the body is its declarations. An import includes the imported module's interface and keeps those identities, in the style of theory graphs (Rabe and Kohlhase, 2013), so a declaration reached along two paths (a diamond) is one declaration.
Static interpretation walks the modules in dependency order and concatenates their declarations
into one flat program. The root's declarations keep their names. A declaration of any other module
is named by the module path and its name, separated by :, which source never spells:
syntax.lambda:normalize, grammars.ccg:CCG.Item. Entry points, traces, and diagnostics use these
names; transpile targets spell . as __ and : as ___, by a spelling that is injective (see
holes, linking, and sites). An error inside an imported module
names that module's file and line.
Each module is checked against the interfaces of the modules it imports, never their bodies.
ModuleInterface.to_artifact gives the
interface as a canonical JSON artifact, which incremental builds key on, and qvr doc renders it:
The Markdown gives one section per public name with its declaring line and doc comment. The names
a declaration declares with it (a family's constructors, a syntax's constructors) are listed under
that declaration, and each example of a doc comment becomes a ```qvr example fence, so
a literate module that imports and opens the module runs the examples it
shows. The standard library reference is generated this way.
In Python¶
quivers.dsl.loads takes the imported modules' sources by path, and
quivers.dsl.load reads them from the search roots.
quivers.dsl.modules.interpret_file and its siblings return
the FlatProgram with its interfaces and source map.
from quivers.dsl import loads
from quivers.qiec import invoke_entry
library = """
export define double(x : Real) : Real !{} =
return x + x
"""
program = loads(
"""
import arith.basic as A
define quadruple(x : Real) : Real !{} =
let y <- A.double(x)
let z <- A.double(y)
return z
""",
modules={"arith.basic": library},
)
assert float(invoke_entry(program.qiec, "quadruple", (1.5,)).value) == 6.0
The language server resolves names through the same graph, with open buffers laid over the files below the workspace folders: go-to-definition follows a qualified or imported name into its module, and references and rename cover every module that spells one declaration.
Cited sources¶
Luca Cardelli. Program Fragments, Linking, and Modularization. In Proceedings of the 24th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, 266–277. 1997. doi:10.1145/263699.263735.
Martin Elsman, Troels Henriksen, Danil Annenkov, and Cosmin E. Oancea. Static Interpretation of Higher-Order Modules in Futhark: Functional GPU Programming in the Large. Proceedings of the ACM on Programming Languages, 2(ICFP):97:1–97:30, 2018. doi:10.1145/3236792.
Florian Rabe and Michael Kohlhase. A Scalable Module System. Information and Computation, 230:1–54, 2013. doi:10.1016/j.ic.2013.06.001.