Skip to content

Hole goals

The goal of each typed hole of a checked module, read off the kernel checker, and the check a filling must pass. See Typed holes.

goals

The goals of a module's typed holes: what each must be and what it can see.

A hole is well typed at its type, so checking proceeds around it. What the author needs back is the hole's goal: the type a filling must have, and the bindings in scope where the hole stands, which are what a filling may mention. hole_goals reads both off the kernel checker itself (through HOLE_OBSERVER), so a goal reports exactly the context the checker checks the hole in. check_filling checks a proposed filling against a goal by the same rules.

HoleGoal didactic-model

HoleGoal(*, name: str, type: TypeExpr = field(opaque=True, extras=OPAQUE_BY_VALUE), bindings: tuple[Local, ...] = field(opaque=True, extras=OPAQUE_BY_VALUE), declaration: str, origin: SourceOrigin = field(opaque=True, extras=OPAQUE_BY_VALUE), context: CheckContext = field(opaque=True, extras=OPAQUE_BY_VALUE))

Bases: ValueModel

What a typed hole must be, and what is in scope where it stands.

PARAMETER DESCRIPTION
name

The hole's name, without the sigil.

TYPE: str

type

The goal: the type every filling must have.

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

bindings

The value bindings in scope at the hole, outermost first, each with its type; a filling may mention any of them.

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

declaration

The computation or handler whose body holds the hole.

TYPE: str

origin

Where the hole was written.

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

context

The checker's whole context at the hole, including the equations enclosing case branches establish, against which a filling is checked.

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

render

render() -> str

Render the goal as a check report shows it.

RETURNS DESCRIPTION
str

?name : T and, when anything is in scope, the bindings as x : A, y : B.

Source code in src/quivers/qiec/goals.py
def render(self) -> str:
    """Render the goal as a check report shows it.

    Returns
    -------
    str
        ``?name : T`` and, when anything is in scope, the bindings as
        ``x : A, y : B``.
    """
    text = f"{HOLE_SIGIL}{self.name} : {render_static(self.type)}"
    if self.bindings:
        scope = ", ".join(
            f"{local.name} : {render_static(local.type)}" for local in self.bindings
        )
        text += f"  [{scope}]"
    return text

hole_goals

hole_goals(module: QiecModule) -> tuple[HoleGoal, ...]

Every value hole of a module with its goal type and bindings.

The module's declarations are resolved and every handler clause and computation body is checked once with an observer installed; each hole the checker meets is reported with the context it was checked in.

PARAMETER DESCRIPTION
module

The checked module.

TYPE: QiecModule

RETURNS DESCRIPTION
tuple[HoleGoal, ...]

One goal per hole occurrence, in the order the checker meets them (handlers first, then computations in declaration order).

RAISES DESCRIPTION
KernelError

If the module does not check.

Source code in src/quivers/qiec/goals.py
def hole_goals(module: QiecModule) -> tuple[HoleGoal, ...]:
    """Every value hole of a module with its goal type and bindings.

    The module's declarations are resolved and every handler clause and
    computation body is checked once with an observer installed; each hole
    the checker meets is reported with the context it was checked in.

    Parameters
    ----------
    module : QiecModule
        The checked module.

    Returns
    -------
    tuple[HoleGoal, ...]
        One goal per hole occurrence, in the order the checker meets them
        (handlers first, then computations in declaration order).

    Raises
    ------
    KernelError
        If the module does not check.
    """
    seen: dict[tuple[str, SourceOrigin], HoleGoal] = {}
    owner = [""]

    def observe(hole: HoleValue, context: CheckContext) -> None:
        """Record one hole the checker met.

        Parameters
        ----------
        hole : HoleValue
            The hole.
        context : CheckContext
            The context it was checked in.
        """
        key = (hole.name, hole.origin)
        if key not in seen:
            seen[key] = HoleGoal(
                name=hole.name,
                type=hole.type,
                bindings=_binding_names(context),
                declaration=owner[0],
                origin=hole.origin,
                context=context,
            )

    token = HOLE_OBSERVER.set(observe)
    try:
        owner[0] = "handlers"
        registry = declaration_registry(module)
        for computation in module.computations:
            owner[0] = computation.name
            inferred_computation_type(module, registry, computation.name)
    finally:
        HOLE_OBSERVER.reset(token)
    return tuple(seen.values())

type_hole_names

type_hole_names(module: QiecModule) -> tuple[str, ...]

The names of a module's type holes, without the sigil.

PARAMETER DESCRIPTION
module

The checked module.

TYPE: QiecModule

RETURNS DESCRIPTION
tuple[str, ...]

One name per type hole, in declaration order.

Source code in src/quivers/qiec/goals.py
def type_hole_names(module: QiecModule) -> tuple[str, ...]:
    """The names of a module's type holes, without the sigil.

    Parameters
    ----------
    module : QiecModule
        The checked module.

    Returns
    -------
    tuple[str, ...]
        One name per type hole, in declaration order.
    """
    families: tuple[FamilyDecl, ...] = type_holes_in(module.families)
    return tuple(
        split_module(family.name)[1].removeprefix(HOLE_SIGIL) for family in families
    )

check_filling

check_filling(goal: HoleGoal, filling: Value, registry: KernelRegistry) -> None

Check a proposed filling against a hole's goal.

PARAMETER DESCRIPTION
goal

The hole's goal.

TYPE: HoleGoal

filling

The value proposed for it; it may mention the goal's bindings.

TYPE: Value

registry

The module's resolved declarations.

TYPE: KernelRegistry

RAISES DESCRIPTION
KernelError

With code qiec-hole-fill if the filling does not check in the goal's context or has another type than the goal.

Source code in src/quivers/qiec/goals.py
def check_filling(goal: HoleGoal, filling: Value, registry: KernelRegistry) -> None:
    """Check a proposed filling against a hole's goal.

    Parameters
    ----------
    goal : HoleGoal
        The hole's goal.
    filling : Value
        The value proposed for it; it may mention the goal's bindings.
    registry : KernelRegistry
        The module's resolved declarations.

    Raises
    ------
    KernelError
        With code ``qiec-hole-fill`` if the filling does not check in the
        goal's context or has another type than the goal.
    """
    try:
        actual = infer_value(filling, registry, goal.context)
    except KernelError as error:
        raise KernelError(
            f"filling of {HOLE_SIGIL}{goal.name} does not check: {error}",
            "qiec-hole-fill",
        ) from error
    if actual != goal.type:
        raise KernelError(
            f"filling of {HOLE_SIGIL}{goal.name} has type {render_static(actual)}, "
            f"but the hole's goal is {render_static(goal.type)}",
            "qiec-hole-fill",
        )