Skip to content

Program elaboration

These functions elaborate a program declaration into a Quivers Indexed Effect Core (QIEC) computation, resolving its names and dependencies. A change to how draws, observations, or deterministic bindings run is made here.

Internal module

This module is an implementation detail of the compiler and its tooling. Its __all__ is empty, so none of the names below are part of the public API, and they may change without notice.

program_elaboration

Elaboration of program declarations into QIEC computations.

A program is domain-specific notation for a named computation: its data, observations, and index maps are the computation's typed parameters, its sample steps perform Random.sample on the module's canonical random instance, its observe and score steps perform Score.add on the canonical score instance, its let steps bind pure expressions, its marginalize blocks call a helper computation that enumerates a finite latent under the prelude's enumeration handler, and its return builds the result tuple. The elaboration is a mixin over the QIEC elaborator, since it shares the value lowering, the registry, and the diagnostics of every other declaration.

Plates are typed data. A step's over and iid_over axes, its : Axis annotation, and a morphism's Real N codomain fix a PlateShape on the step's distribution, whose sample type is the tensor over those axes; a via index map re-indexes a grouped latent's arguments to the observation rows and segments the observation's weights back to the groups.

ObjectInfo didactic-model

ObjectInfo(*, extent: int | IndexTerm | None = field(default=None, opaque=True, extras=OPAQUE_BY_VALUE), real_width: int | IndexVariable | None = field(default=None, opaque=True, extras=OPAQUE_BY_VALUE), finite: bool = False)

Bases: QiecModel

What the elaborator reads off one object declaration.

PARAMETER DESCRIPTION
extent

The object's size as an axis: its cardinality when finite, its coordinate count when continuous. An object parameter of a program template has an index variable for its size, bound by the template's telescope.

TYPE: int | IndexTerm | None DEFAULT: field(default=None, opaque=True, extras=OPAQUE_BY_VALUE)

real_width

The width of a Real N object, whose values are real vectors.

TYPE: int | IndexVariable | None DEFAULT: field(default=None, opaque=True, extras=OPAQUE_BY_VALUE)

finite

Whether the object is a finite set.

TYPE: bool DEFAULT: False

marginal_helper_name

marginal_helper_name(program: str, latent: str) -> str

The name of the helper a marginalize step generates.

PARAMETER DESCRIPTION
program

The program holding the step.

TYPE: str

latent

The marginalized latent's name.

TYPE: str

RETURNS DESCRIPTION
str

The member program.latent_marginal.

Source code in src/quivers/dsl/program_elaboration.py
def marginal_helper_name(program: str, latent: str) -> str:
    """The name of the helper a ``marginalize`` step generates.

    Parameters
    ----------
    program : str
        The program holding the step.
    latent : str
        The marginalized latent's name.

    Returns
    -------
    str
        The member ``program.latent_marginal``.
    """
    return member_name(program, f"{latent}_marginal")

reached_site_labels

reached_site_labels(name: str, declarations: Mapping[str, ProgramDecl]) -> frozenset[str]

The labels of every site a program reaches when it runs.

A program reaches its own sites, the sites of the programs it calls with let y <- p(...), which keep their labels, and the sites of the step programs it scans with, which it reaches once per position under the same labels.

PARAMETER DESCRIPTION
name

The program's name.

TYPE: str

declarations

The module's program declarations, step programs included.

TYPE: Mapping[str, ProgramDecl]

RETURNS DESCRIPTION
frozenset[str]

The labels; empty for a name that is not a program.

Source code in src/quivers/dsl/program_elaboration.py
def reached_site_labels(
    name: str, declarations: Mapping[str, ProgramDecl]
) -> frozenset[str]:
    """The labels of every site a program reaches when it runs.

    A program reaches its own sites, the sites of the programs it calls
    with ``let y <- p(...)``, which keep their labels, and the sites of
    the step programs it scans with, which it reaches once per position
    under the same labels.

    Parameters
    ----------
    name : str
        The program's name.
    declarations : Mapping[str, ProgramDecl]
        The module's program declarations, step programs included.

    Returns
    -------
    frozenset[str]
        The labels; empty for a name that is not a program.
    """
    found: set[str] = set()
    pending = [name]
    visited: set[str] = set()
    while pending:
        current = pending.pop()
        if current in visited:
            continue
        visited.add(current)
        declaration = declarations.get(current)
        if declaration is None:
            continue
        found |= _own_site_labels(declaration.draws)
        pending.extend(_callees(declaration.draws))
    return frozenset(found)