Skip to content

Reference checks

references

Prose references to declarations in a literate module.

The prose of a literate document names declarations with the role {qvr:ref}`Name`. A reference is resolved like a name written in the module's code, in the scope of the module the document tangles to: a bare name is one of the module's declarations (or a member of one, such as a deduction's generated Grammar.Item), and M.name is a declaration of an imported module M. A reference that resolves to nothing is refused with its document line and column, so renaming a declaration breaks the prose that names it rather than leaving it silently stale.

prose_references finds the references of a document; check_references resolves them.

ReferenceProblem didactic-model

ReferenceProblem(*, reference: ProseReference, message: str)

Bases: Model

A prose reference that names no declaration.

PARAMETER DESCRIPTION
reference

The reference.

TYPE: ProseReference

message

Why it does not resolve.

TYPE: str

declared_names

declared_names(flat: FlatProgram, module: QiecModule | None) -> frozenset[str]

Every flat-program name a reference may denote.

PARAMETER DESCRIPTION
flat

The interpreted module graph.

TYPE: FlatProgram

module

Its checked core, whose generated declarations (deduction items, constructors, derived instances) are named there.

TYPE: QiecModule | None

RETURNS DESCRIPTION
frozenset[str]

The names of the flat program's statements and of the core's families, constructors, index sorts, computations, entries, effects, instances, handlers, and structure declarations.

Source code in src/quivers/literate/references.py
def declared_names(flat: FlatProgram, module: QiecModule | None) -> frozenset[str]:
    """Every flat-program name a reference may denote.

    Parameters
    ----------
    flat : FlatProgram
        The interpreted module graph.
    module : QiecModule | None
        Its checked core, whose generated declarations (deduction items,
        constructors, derived instances) are named there.

    Returns
    -------
    frozenset[str]
        The names of the flat program's statements and of the core's
        families, constructors, index sorts, computations, entries,
        effects, instances, handlers, and structure declarations.
    """
    names: set[str] = set()
    for statement in flat.module.statements:
        name = getattr(statement, "name", None)
        if isinstance(name, str):
            names.add(name)
    if module is not None:
        names.update(family.name for family in module.families)
        names.update(constructor.name for constructor in module.constructors)
        names.update(sort.name for sort in module.index_sorts)
        names.update(computation.name for computation in module.computations)
        names.update(entry.name for entry in module.entries)
        names.update(effect.ref.name for effect in module.effects)
        names.update(instance.name for instance in module.instances)
        names.update(handler.name for handler in module.handlers)
        names.update(structure.name for structure in module.structures)
    return frozenset(names)

check_references

check_references(references: Iterable[ProseReference], graph: ModuleGraph, known: frozenset[str]) -> tuple[ReferenceProblem, ...]

Resolve prose references in the scope of a document's module.

PARAMETER DESCRIPTION
references

The references.

TYPE: Iterable[ProseReference]

graph

The loaded graph rooted at the document's module.

TYPE: ModuleGraph

known

The names the program declares (declared_names).

TYPE: frozenset[str]

RETURNS DESCRIPTION
tuple[ReferenceProblem, ...]

One problem per reference that names no declaration, empty when all resolve.

Source code in src/quivers/literate/references.py
def check_references(
    references: Iterable[ProseReference],
    graph: ModuleGraph,
    known: frozenset[str],
) -> tuple[ReferenceProblem, ...]:
    """Resolve prose references in the scope of a document's module.

    Parameters
    ----------
    references : Iterable[ProseReference]
        The references.
    graph : ModuleGraph
        The loaded graph rooted at the document's module.
    known : frozenset[str]
        The names the program declares (`declared_names`).

    Returns
    -------
    tuple[ReferenceProblem, ...]
        One problem per reference that names no declaration, empty when all
        resolve.
    """
    root = graph.root
    loaded = graph.loaded[root]
    checked = graph.checked[root]
    interfaces = _interfaces(graph)
    private: dict[ModuleIdentity, frozenset[str]] = {
        identity: frozenset(
            declared.name for declared in graph.loaded[identity].names.declarations
        )
        - frozenset(interface.names())
        for identity, interface in interfaces.items()
        if not isinstance(identity, HoleModule)
    }
    problems: list[ReferenceProblem] = []
    for reference in references:
        if _NAME.fullmatch(reference.target) is None:
            problems.append(
                ReferenceProblem(
                    reference=reference,
                    message=f"{reference.target!r} is not a name",
                )
            )
            continue
        scope = file_scope(
            loaded.source, checked.names, checked.imports, interfaces, private
        )
        occurrence = Occurrence(
            start=0,
            end=len(reference.target.encode("utf-8")),
            text=reference.target,
            role="reference",
            parent_kind="prose_reference",
            field="target",
            statement=0,
            position="value",
            line=reference.line,
            column=reference.column,
        )
        try:
            resolution = resolve_occurrence(scope, occurrence)
        except ModuleError as error:
            problems.append(
                ReferenceProblem(reference=reference, message=error.diagnostic.message)
            )
            continue
        if resolution is None or resolution.symbol is None:
            flat_name = reference.target
        else:
            flat_name = symbol_flat_name(resolution.symbol, root)
            segments = reference.target.split(".")
            members = (
                segments[len(segments) - resolution.members :]
                if resolution.members
                else []
            )
            path = (*resolution.path, *members)
            if path:
                flat_name = member_name(flat_name, *path)
        if flat_name not in known:
            problems.append(
                ReferenceProblem(
                    reference=reference,
                    message=f"{reference.target!r} names no declaration of the "
                    "module or of a module it imports",
                )
            )
    return tuple(problems)