deeplog.circuit.fold

Re-emit a circuit through a formula algebra — the circuit’s catamorphism.

A circuit node is an atom, a unary or a binary: the fragment of the formula language a circuit can express. Leaves, named constants and arbitrary numeric constants are all atoms (get_symbol_name() gives each one symbol), and every operator node is unary or binary, because every one is built through get_operator() with one or two children.

So a circuit is a sub-algebra of the formula AST, and folding one needs no interface of its own: the same DeepLogFormulaFactory that deeplog.formula.ast.fold() drives over a tree drives this over a graph. An algebra that only ever meets circuit nodes raises on the four eliminators a circuit has no node for.

Functions

fold_circuit(circuit, roots, algebra, *[, frontier, memo]) → dict[int, T]

Re-emit the subgraph under roots through algebra.

Module Contents

fold_circuit(
circuit: Circuit,
roots: list[int],
algebra: DeepLogFormulaFactory[T],
*,
frontier: Mapping[int, Symbol] | None = None,
memo: dict[int, T] | None = None,
) → dict[int, T][source]

Re-emit the subgraph under roots through algebra.

Returns the carrier built for every node reached, keyed by source node id – so result[root] is the root’s, and the map itself is the record of what each node became.

Children are visited before parents, so an algebra may be stateful and see its create_* calls in dependency order. A node reached twice is built once: circuit ids are canonical, so unlike the AST fold (deeplog.formula.ast.fold(), which relies on hash-consing) memoising by id needs nothing established first.

frontier nodes are not descended into and become atoms under the symbols given, which is how a bounded lowering treats a cut (deeplog.circuit.split). memo seeds the result and is mutated in place, so successive folds sharing one algebra build a subgraph once between them – as deeplog.formula.ast.fold()’s does on the AST side.