DSL (Example Construction)

The spflow.dsl module provides a small, example-oriented expression layer for constructing simple circuits with algebraic syntax. It is intended for documentation and showcase snippets; production model construction is typically done directly via spflow.modules.

Construction-only DSL for building SPFlow circuits.

This module provides a small, non-invasive expression layer for writing examples with algebraic syntax while keeping the core spflow.modules API unchanged.

The DSL is intentionally minimal:

  • Products: term(A) * term(B)

  • Weighted sums (mixtures): 0.4 * term(A) + 0.6 * term(B)

To obtain an actual Module, call .build() on the resulting expression.

Notes

  • Weights must be provided for sums; term(A) + term(B) is intentionally disallowed.

  • Weighted sums are restricted to terms with out_shape.channels == 1 for simplicity.

For convenience in docs/examples, dsl() can temporarily enable operator overloads on spflow.modules.module.Module within a context manager, so that expressions like 0.4 * Normal(0) * Normal(1) + 0.6 * Normal(0) * Normal(1) work without wrapping leaves.

class spflow.dsl.Buildable(*args, **kwargs)[source]

Bases: Protocol

Protocol for objects that can build a Module.

build()[source]
Return type:

Module

class spflow.dsl.ProductExpr(factors)[source]

Bases: Buildable

Product of one or more sub-expressions.

build()[source]
Return type:

Module

factors: list[Buildable]
class spflow.dsl.SumExpr(terms)[source]

Bases: Buildable

A weighted mixture of expressions.

Terms are stored as (weight, expr) pairs and normalized on build.

build()[source]
Return type:

Module

terms: list[tuple[float, Buildable]]
class spflow.dsl.Term(module)[source]

Bases: Buildable

Leaf expression node that wraps a concrete Module.

build()[source]
Return type:

Module

module: Module
class spflow.dsl.WeightedExpr(weight, expr)[source]

Bases: object

A weighted expression term used as an input to mixtures.

build()[source]
Return type:

Module

expr: Buildable
weight: float
spflow.dsl.as_expr(value)[source]

Convert a Module or DSL expression to a DSL expression.

Return type:

Buildable

spflow.dsl.build(value)[source]

Build a concrete Module from a DSL expression (or pass through Module).

Return type:

Module

spflow.dsl.dsl()[source]

Temporarily enable DSL operator overloads on Module.

This is intended for documentation/examples. It monkeypatches operator methods on spflow.modules.module.Module for the duration of the context manager and restores the original methods afterward.

Within the context: - Module * Module builds a ProductExpr - float * Module and Module * float create a WeightedExpr - WeightedExpr + WeightedExpr (+ …) creates a SumExpr - Module + Module remains disallowed (weights must be explicit)

spflow.dsl.term(module)[source]

Wrap a Module as a DSL term.

Return type:

Term

spflow.dsl.w(weight, value)[source]

Convenience helper to create a weighted term.

Return type:

WeightedExpr