Composite lets¶
These helpers normalize a program AST before lowering. They rewrite supported composite expressions into the forms the next stage expects, which makes their dependencies explicit (one checked core) without asking later stages to handle every source spelling.
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.
composite_lets
¶
Normalize program ASTs before IR lowering.
The pass expands sampled composite bindings into atomic sample and let steps. It preserves marginalize blocks for the lowering stage while expanding composite references inside their scopes.
LEARNED_SCAN_INIT_SUFFIX
module-attribute
¶
The suffix of the input a scan with init=learned starts from.
POSITIONS_BUNDLE
module-attribute
¶
The let function pairing sequences position by position.
$positions(a, b) over sequences of one length is the sequence whose
entry at each position lays the entries of a and b there end to
end. The expansion of a fan or @ whose branches each return
every state of a scan binds it; the $ keeps it apart from every
function a source can call.
ModelBindError
¶
ModelBindError(step: SampleStep, expected: str)
Bases: StepResolutionError
A model bind names a term outside its operation's namespace.
Source code in src/quivers/dsl/composite_lets.py
ScanBinding
didactic-model
¶
Bases: Model
An input label of a scan and the scanning program's name bound to it.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The label: a free data name the scan's cell reads.
TYPE:
|
name
|
The scanning program's name the apply step binds it to, resolved in the program's scope where the step is written.
TYPE:
|
ScanCall
didactic-model
¶
ScanCall(*, step: str, sequence: str, init: str | None = None, init_label: str | None = None, bindings: tuple[ScanBinding, ...] = (), outputs: Literal['last', 'all'] = 'last', reverse: bool = False, marginal: bool = False)
Bases: Model
A scan as the expansion binds it: let h = scan(step, xs, ...).
| PARAMETER | DESCRIPTION |
|---|---|
step
|
The step program's name.
TYPE:
|
sequence
|
The name of the sequence the scan runs over.
TYPE:
|
init
|
The initial state's name: the scanning program's name an apply
step binds the scan's
TYPE:
|
init_label
|
The label the scan's
TYPE:
|
bindings
|
Every binding of the apply step the scan comes from. The scan reads its cell's free data names through these, never through the spelling of the scanning program's own names.
TYPE:
|
outputs
|
Whether the scan's value is its final state or every state.
TYPE:
|
reverse
|
Whether the scan visits the positions from the last to the first.
TYPE:
|
marginal
|
Whether the scan sums out a finite state its cell marginalizes
and returns (a marginalized chain);
TYPE:
|
bound
¶
The scanning program's name bound to an input label.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str | None
|
The bound name, or |
Source code in src/quivers/dsl/composite_lets.py
validate_model_binds
¶
validate_model_binds(module: Module) -> None
Check each explicit model operation against its source namespace.
Source code in src/quivers/dsl/composite_lets.py
expand_composite_lets
¶
Rewrite program_decl bodies so composite-let sample steps
become equivalent chains of atomic sample steps.
A composite let is a DefineDecl whose .expr is an ExprCompose
(the prior >> likelihood form). Each SampleStep /
ObserveStep whose morphism slot names such a let is rewritten
into a sequence of fresh SampleSteps, one per element of the
composition chain, with the trailing step keeping the original
step's bound variable name.
The returned Module shares vertex identity with the input for
every non-rewritten statement; only the program_decls with
composite-let references are rebuilt.
The rewrite is target-independent: every backend consumes the same
expanded AST, and the choice of whether to enumerate or to sample a
marginalize latent is each renderer's, taken against the lowered
IRMarginalize. target is
accepted so callers can name the backend they are compiling for
without the pass branching on it.
Source code in src/quivers/dsl/composite_lets.py
expand_program_scans
¶
expand_program_scans(declaration: ProgramDecl, module: Module) -> tuple[tuple[ProgramStep, ...], dict[str, ProgramDecl]]
Expand the draws of one program that run a scan.
A static application or an observation whose morphism resolves to a chain
with a scan is rewritten exactly as
expand_composite_lets
rewrites it: a let h = scan(step, xs) over a step program, with
the chain's other elements as steps of the step program or of the
program. Every other step is kept as written. The expansion draws
its fresh names from the serial the module-wide expansion uses, so
an expanded step binds the names the checked elaboration binds.
| PARAMETER | DESCRIPTION |
|---|---|
declaration
|
The program.
TYPE:
|
module
|
The module declaring it, its morphisms, defines, and programs.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[tuple[ProgramStep, ...], dict[str, ProgramDecl]]
|
The program's draws, the input itself when no step runs a scan; and the step programs the expansion declared, by name, each after the step programs it scans with. |
Source code in src/quivers/dsl/composite_lets.py
refuse_marginal_chain_reads
¶
refuse_marginal_chain_reads(draws: tuple[ProgramStep, ...], returned: tuple[str, ...]) -> None
Refuse a read of the name a marginalized chain binds.
The chain sums its states out, so the name its sample step binds
holds no state: no later step reads it and the program does not
return it. A marginalized chain inside a marginalize block is
refused too, since the block's weight is a single number per atom.
| PARAMETER | DESCRIPTION |
|---|---|
draws
|
A program's expanded steps.
TYPE:
|
returned
|
The names it returns.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
StepResolutionError
|
If a later step or the return reads such a name, or such a
chain lies inside a |
Source code in src/quivers/dsl/composite_lets.py
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 | |
refuse_program_cell_calls
¶
refuse_program_cell_calls(draws: tuple[ProgramStep, ...], programs: dict[str, ProgramDecl], morphism_formals: frozenset[str] = frozenset()) -> None
Refuse every draw or call of a cell that returns a marginalized latent.
| PARAMETER | DESCRIPTION |
|---|---|
draws
|
A program body, searched through marginalization scopes.
TYPE:
|
programs
|
The module's programs by name.
TYPE:
|
morphism_formals
|
Lexical morphism formals that still await substitution.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
StepResolutionError
|
If a step draws from or calls such a cell other than as the cell of a scan. |
Source code in src/quivers/dsl/composite_lets.py
refuse_standalone_cell
¶
refuse_standalone_cell(cell: ProgramDecl, line: int) -> None
Refuse a concrete call of a program that only runs as a scan cell.
Source code in src/quivers/dsl/composite_lets.py
is_learned_scan_init
¶
Whether a scan's initial-state name is a learned initial state.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
The third argument of
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True for the |
Source code in src/quivers/dsl/composite_lets.py
scan_let_expression
¶
scan_let_expression(call: ScanCall) -> LetExprCall
The let expression binding a scan.
| PARAMETER | DESCRIPTION |
|---|---|
call
|
The scan.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
LetExprCall
|
|
Source code in src/quivers/dsl/composite_lets.py
scan_let_call
¶
scan_let_call(expr: LetExprNode) -> ScanCall | None
Read a scan off a let expression scan_let_expression built.
| PARAMETER | DESCRIPTION |
|---|---|
expr
|
The expression.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ScanCall | None
|
The scan; |
Source code in src/quivers/dsl/composite_lets.py
positions_let_call
¶
positions_let_call(expr: LetExprNode) -> tuple[str, ...] | None
Read $positions(a, b, ...) off a let expression.
| PARAMETER | DESCRIPTION |
|---|---|
expr
|
The expression.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[str, ...] | None
|
The paired sequences' names, in order; |