Program encoding¶
This module encodes a MonadicProgram as a Quivers Indexed Effect Core (QIEC) computation. Each step's Python part is a request
on the prelude's Compute interface over the values it reads, and each draw is a Random.sample
request on the sampleable that part returns. Running the encoding on the reference machine,
the library's interpreter for QIEC, collects the program's results and scores
(runtime providers).
program_module
¶
The kernel encoding of a MonadicProgram.
A MonadicProgram is a sequence of host steps: a draw from a torch
morphism at an input built from earlier bindings, a deterministic binding
computed by a host closure, or a score contributed by one. The kernel has
no term for a host function, and does not need one: each step's host part
is an instance of the prelude's Compute interface, performed on the
values it reads (the arguments of a draw, the names a closure declares,
or every value bound so far when it declares none), and each draw is the
canonical Random.sample request on the sampleable the host part
returns.
The computation this module builds therefore makes the program's data flow
and effect structure explicit while every tensor operation stays in the
host, behind a handler a runtime provider installs. The user's effect
handlers are lexical handlers of the same random and score
instances, so conditioning, intervention, replay, reweighting, and tracing
compose by the kernel's own rules.
HostStep
didactic-model
¶
HostStep(*, instance: EffectInstanceId = field(opaque=True, extras=OPAQUE_BY_VALUE), effect: EffectRef = field(opaque=True, extras=OPAQUE_BY_VALUE), kind: str, spec: Step | None = field(opaque=True, extras=OPAQUE_BY_IDENTITY), environment: tuple[str, ...], answer_type: TypeExpr = field(opaque=True, extras=OPAQUE_BY_VALUE), site: str | None, program: MonadicProgram = field(opaque=True, extras=OPAQUE_BY_IDENTITY), scanned: bool, position: int)
Bases: QiecModel
One host part of the program, as the runtime provider serves it.
Kernel types and effect identities compare by value. The executable program and its step specification retain their runtime identities.
| PARAMETER | DESCRIPTION |
|---|---|
instance
|
The
TYPE:
|
effect
|
The applied
TYPE:
|
kind
|
TYPE:
|
spec
|
The step record the host part serves, or
TYPE:
|
environment
|
The names of the locals the step reads, in the order the environment tuple carries them.
TYPE:
|
answer_type
|
The type the host part returns.
TYPE:
|
site
|
The site label of a draw step or a site address, else
TYPE:
|
program
|
The program
TYPE:
|
scanned
|
Whether the step runs once per position of a scan.
TYPE:
|
position
|
The step's position among the kernel's host steps.
TYPE:
|
ProgramKernel
didactic-model
¶
ProgramKernel(*, module: QiecModule = field(opaque=True, extras=OPAQUE_BY_VALUE), program: MonadicProgram = field(opaque=True, extras=OPAQUE_BY_IDENTITY), steps: tuple[HostStep, ...] = field(opaque=True, extras=OPAQUE_BY_VALUE), sites: tuple[str, ...], site_types: dict[str, TypeExpr] = field(opaque=True, extras=OPAQUE_BY_IDENTITY), data: tuple[str, ...], random: NamedEffectInstance = field(opaque=True, extras=OPAQUE_BY_VALUE), score: NamedEffectInstance = field(opaque=True, extras=OPAQUE_BY_VALUE), param: NamedEffectInstance = field(opaque=True, extras=OPAQUE_BY_VALUE), result_type: TypeExpr = field(opaque=True, extras=OPAQUE_BY_VALUE), locals: dict[str, Local] = field(default_factory=dict, opaque=True, extras=OPAQUE_BY_IDENTITY), scanned: frozenset[str] = field(default=frozenset(), opaque=True, extras=OPAQUE_BY_VALUE), site_morphisms: dict[str, ContinuousMorphism] = field(default_factory=dict, opaque=True, extras=OPAQUE_BY_IDENTITY), site_events: dict[str, tuple[int, ...]] = field(default_factory=dict, opaque=True, extras=OPAQUE_BY_IDENTITY))
Bases: QiecModel
A program's computation with the record the runtime needs to serve it.
The checked module, host-step records, and effect instances compare by value. Live programs, morphisms, and lookup tables retain identity.
| PARAMETER | DESCRIPTION |
|---|---|
module
|
The kernel module: the interfaces, the instances, the entry computation first, and the helper computation of each scan, without handlers; a run adds its own.
TYPE:
|
program
|
The program encoded.
TYPE:
|
steps
|
The host steps, in body order.
TYPE:
|
sites
|
The site label of every draw, in order; a site reached once per scan position is listed once, under its label.
TYPE:
|
site_types
|
The value type of each site, by label.
TYPE:
|
data
|
The names of the host data inputs after the program's input, in parameter order.
TYPE:
|
random
|
The canonical
TYPE:
|
score
|
The canonical
TYPE:
|
param
|
The canonical
TYPE:
|
result_type
|
The entry computation's result type.
TYPE:
|
locals
|
The entry computation's local for each name the program binds.
TYPE:
|
scanned
|
The labels of the sites reached once per scan position, whose
occurrences are addressed
TYPE:
|
site_morphisms
|
The morphism each site draws from, by label.
TYPE:
|
site_events
|
The event shape of one value of each site, by label:
TYPE:
|
space_type
¶
space_type(space: SetObject | ContinuousSpace) -> TypeExpr
The kernel type of a value of a space.
| PARAMETER | DESCRIPTION |
|---|---|
space
|
A program's domain, codomain, or a morphism's codomain.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
TypeExpr
|
|
Source code in src/quivers/effects/program_module.py
scanned_sites
¶
scanned_sites(program: MonadicProgram) -> frozenset[str]
The labels of the sites a program reaches once per scan position.
| PARAMETER | DESCRIPTION |
|---|---|
program
|
The program.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
frozenset[str]
|
The labels of the sites its scans' step programs reach, through the programs those call, at any depth. |
Source code in src/quivers/effects/program_module.py
reversed_scan_sites
¶
reversed_scan_sites(program: MonadicProgram) -> frozenset[str]
The labels of the sites a reversed scan reaches once per position.
| PARAMETER | DESCRIPTION |
|---|---|
program
|
The program.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
frozenset[str]
|
The labels of the sites the step programs of its reversed scans reach, through the programs its steps call, at any depth. A reversed scan reaches them first at its last position. |
Source code in src/quivers/effects/program_module.py
once_sites
¶
once_sites(program: MonadicProgram) -> frozenset[str]
The labels of the sites a program reaches at most once per run.
| PARAMETER | DESCRIPTION |
|---|---|
program
|
The program.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
frozenset[str]
|
Its own draw and observe labels and those of the programs its calls run, but none a scan reaches. |
Source code in src/quivers/effects/program_module.py
derived_bindings
¶
derived_bindings(program: MonadicProgram) -> tuple[frozenset[str], frozenset[str]]
The names a run computes and a caller may not supply a value for.
| PARAMETER | DESCRIPTION |
|---|---|
program
|
The program.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[frozenset[str], frozenset[str]]
|
The names the program's own call and scan steps bind, which the run computes from the sites those reach; and the names bound inside its scans' step programs, which the run computes once per position. |
Source code in src/quivers/effects/program_module.py
site_label
¶
The address of one occurrence of a site reached once per position.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The site's label.
TYPE:
|
occurrence
|
How many times the run reached the site before, counted from zero.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
|
Source code in src/quivers/effects/program_module.py
program_kernel
¶
program_kernel(program: MonadicProgram, data: Sequence[str] = ()) -> ProgramKernel
Encode a program as a kernel module.
| PARAMETER | DESCRIPTION |
|---|---|
program
|
The program.
TYPE:
|
data
|
The names of the host data inputs a run supplies beside the program's input, in parameter order.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ProgramKernel
|
The module and the record the runtime serves it by. |