Skip to content

Module build

The compiler's stages as queries over a module graph: each module is checked against the interfaces of its imports, so an edit to a private body re-checks that module alone.

pipeline

The compiler as incremental queries over a module graph.

ModuleBuild runs the stages of compiling a QVR module graph as queries of a QueryEngine, keyed by module path:

query key reads result
parse module its source the parse tree
declared module parse its name analysis
imports module declared the modules it imports
names module declared, imports' interface its names, constructors in scope included
check module names, imports, imports' interface and private its resolutions and interface
interface module check its public interface
private module declared, interface the names it declares without exporting
order root imports the graph's modules, dependencies first
program root every parse, names, check the flat program
elaborate root program the checked QIEC module (persisted)
hashes root elaborate the core hashes
specialize root, computation, static arguments elaborate the static arguments and the specialized type

A module is checked against the interfaces of the modules it imports, never their bodies, and an interface's fingerprint is its digest, so an edit to a module's private body re-runs that module's check and stops at its interface (early cutoff): no dependent is checked again. Elaboration needs every body and runs on the flat program; its result is persisted in the store, so a later process reuses the checked module (and the deductions compiled into it) for an unchanged program without elaborating again. The checked meta-phase library persists in the same store (persisting_meta_library), so a later process elaborating a changed program does not check it again.

ImportEdges didactic-model

ImportEdges(*, imports: tuple[ResolvedImport, ...] = ())

Bases: Model

The resolved imports of one module.

PARAMETER DESCRIPTION
imports

Each import with the module it names.

TYPE: tuple[ResolvedImport, ...] DEFAULT: ()

Specialization didactic-model

Specialization(*, computation: str, arguments: tuple[StaticArgument, ...] = field(opaque=True, extras=OPAQUE_BY_VALUE), type: ComputationType = field(opaque=True, extras=OPAQUE_BY_VALUE))

Bases: ValueModel

A computation instantiated at closed static arguments.

PARAMETER DESCRIPTION
computation

The computation's name.

TYPE: str

arguments

The checked static arguments, in telescope order.

TYPE: tuple[StaticArgument, ...] DEFAULT: field(opaque=True, extras=OPAQUE_BY_VALUE)

type

The computation's type with its telescope instantiated.

TYPE: ComputationType DEFAULT: field(opaque=True, extras=OPAQUE_BY_VALUE)

ModuleBuild

ModuleBuild(loader: ModuleLoader, *, store: ArtifactStore | None = None)

Incremental compilation of the module graphs a loader provides.

PARAMETER DESCRIPTION
loader

Finds a module's source by its path.

TYPE: ModuleLoader

store

Where elaborations persist across processes, or None to keep them in memory.

TYPE: ArtifactStore or None DEFAULT: None

Source code in src/quivers/incremental/pipeline.py
def __init__(
    self, loader: ModuleLoader, *, store: ArtifactStore | None = None
) -> None:
    self.loader = loader
    self.engine = QueryEngine(store=store, namespace="qvr-modules")
    engine = self.engine
    self.sources: Input[ModuleSource] = engine.input("source", _json_fingerprint)
    self.parse: Query[_Tree] = engine.query(
        "parse",
        self._parse,
        lambda tree: content_digest(tree.source),
    )
    self.declared: Query[FileNames] = engine.query(
        "declared", self._declared, _json_fingerprint
    )
    self.imports: Query[ImportEdges] = engine.query(
        "imports", self._imports, _json_fingerprint
    )
    self.names: Query[FileNames] = engine.query(
        "names", self._names, _json_fingerprint
    )
    self.check: Query[CheckedModule] = engine.query(
        "check", self._check, _json_fingerprint
    )
    self.interface: Query[ModuleInterface] = engine.query(
        "interface",
        lambda key: self.check.get(key).interface,
        lambda interface: content_digest(interface.to_artifact()),
    )
    self.private: Query[tuple[str, ...]] = engine.query(
        "private", self._private, _tuple_fingerprint
    )
    self.order: Query[tuple[str, ...]] = engine.query(
        "order", self._order, _tuple_fingerprint
    )
    self.program: Query[FlatProgram] = engine.query(
        "program",
        self._program,
        lambda flat: content_digest(flat.module.model_dump_json().encode("utf-8")),
    )
    self.elaborate: Query[QiecModule] = engine.query(
        "elaborate",
        self._elaborate,
        lambda module: content_digest(dumps(module).encode("utf-8")),
        codec=QueryCodec(
            lambda module: dumps(module).encode("utf-8"),
            _decode_module,
            "qiec-json",
        ),
    )
    self.hashes: Query[ModuleHashes] = engine.query(
        "hashes",
        lambda key: module_hashes(self.elaborate.get(key)),
        _json_fingerprint,
    )
    self.specialize: Query[Specialization] = engine.query(
        "specialize",
        self._specialize,
        lambda item: content_digest(
            dumps((item.computation, item.arguments, item.type)).encode("utf-8")
        ),
    )

load

load(module: str) -> None

Set the source of a module and of everything it imports.

PARAMETER DESCRIPTION
module

The module's dotted path.

TYPE: str

RAISES DESCRIPTION
ModuleError

If a module is not found or is provided by two roots.

Source code in src/quivers/incremental/pipeline.py
def load(self, module: str) -> None:
    """Set the source of a module and of everything it imports.

    Parameters
    ----------
    module : str
        The module's dotted path.

    Raises
    ------
    ModuleError
        If a module is not found or is provided by two roots.
    """
    pending = [module]
    while pending:
        key = pending.pop()
        if key not in self.sources.keys():
            self.sources.set(key, self._find(key, None))
        for edge in self.imports.get(key).imports:
            target = module_display(edge.module)
            if target not in self.sources.keys():
                pending.append(target)

reload

reload(module: str) -> bool

Read a module's source again, as after an edit.

PARAMETER DESCRIPTION
module

The module's dotted path.

TYPE: str

RETURNS DESCRIPTION
bool

Whether its source changed.

RAISES DESCRIPTION
ModuleError

If the module is no longer found.

Source code in src/quivers/incremental/pipeline.py
def reload(self, module: str) -> bool:
    """Read a module's source again, as after an edit.

    Parameters
    ----------
    module : str
        The module's dotted path.

    Returns
    -------
    bool
        Whether its source changed.

    Raises
    ------
    ModuleError
        If the module is no longer found.
    """
    changed = self.sources.set(module, self._find(module, None))
    self.load(module)
    return changed

executed

executed(query: str, *, since: int = 0) -> tuple[str, ...]

The keys a query ran for, in order.

PARAMETER DESCRIPTION
query

The query name.

TYPE: str

since

Skip the first since log entries.

TYPE: int DEFAULT: 0

RETURNS DESCRIPTION
tuple[str, ...]

The keys of its execute and cutoff steps: every time its function actually ran.

Source code in src/quivers/incremental/pipeline.py
def executed(self, query: str, *, since: int = 0) -> tuple[str, ...]:
    """The keys a query ran for, in order.

    Parameters
    ----------
    query : str
        The query name.
    since : int
        Skip the first ``since`` log entries.

    Returns
    -------
    tuple[str, ...]
        The keys of its ``execute`` and ``cutoff`` steps: every time its
        function actually ran.
    """
    return tuple(
        event.key
        for event in self.engine.events(since=since)
        if event.query == query and event.action in {"execute", "cutoff"}
    )

specialization_key

specialization_key(root: str, computation: str, statics: tuple[str, ...]) -> str

The specialize key of a computation at static arguments.

PARAMETER DESCRIPTION
root

The root module's path.

TYPE: str

computation

The computation's name.

TYPE: str

statics

NAME=TERM assignments, in any order.

TYPE: tuple[str, ...]

RETURNS DESCRIPTION
str

A key that names the same specialization however the assignments are ordered.

Source code in src/quivers/incremental/pipeline.py
def specialization_key(root: str, computation: str, statics: tuple[str, ...]) -> str:
    """The ``specialize`` key of a computation at static arguments.

    Parameters
    ----------
    root : str
        The root module's path.
    computation : str
        The computation's name.
    statics : tuple[str, ...]
        ``NAME=TERM`` assignments, in any order.

    Returns
    -------
    str
        A key that names the same specialization however the assignments
        are ordered.
    """
    return json.dumps([root, computation, sorted(statics)])