Compiler

Compilation from DSL AST to executable quivers.

compiler

Compiler: transform a quivers DSL AST into a trainable Program.

The compiler walks the AST in declaration order, building up an environment of objects, spaces, and morphisms, then compiles the output expression into a quivers.Program (nn.Module).

Supports both discrete (FinSet-based) and continuous (ContinuousSpace- based) morphisms, including stochastic (Markov kernels), boundary (Discretize/Embed), and parameterized family distributions.

CompileError

CompileError(message: str, line: int = 0, col: int = 0)

Bases: Exception

Raised when the compiler encounters a semantic error.

PARAMETER DESCRIPTION
message

Error description.

TYPE: str

line

Source line number (0 if unknown).

TYPE: int DEFAULT: 0

col

Source column number (0 if unknown).

TYPE: int DEFAULT: 0

Source code in src/quivers/dsl/compiler.py
211
212
213
214
215
def __init__(self, message: str, line: int = 0, col: int = 0) -> None:
    self.line = line
    self.col = col
    loc = f"line {line}, col {col}: " if line else ""
    super().__init__(f"{loc}{message}")

Compiler

Compiler(module: Module)

Compile a quivers DSL AST into a Program.

The compiler maintains three environments:

  • objects: name -> SetObject (discrete finite sets)
  • spaces: name -> ContinuousSpace
  • morphisms: name -> Morphism or ContinuousMorphism (any morphism-like)

It processes statements in order and compiles the output expression into a Program wrapping the morphism DAG.

PARAMETER DESCRIPTION
module

The parsed AST.

TYPE: Module

Source code in src/quivers/dsl/compiler.py
236
237
238
239
240
241
242
243
244
245
def __init__(self, module: Module) -> None:
    self._module = module
    self._quantale: Quantale = PRODUCT_FUZZY
    self._categories: list[str] = []
    self._rules: dict = {}
    self._objects: dict[str, SetObject] = {}
    self._spaces: dict = {}
    self._morphisms: dict = {}
    self._groups: dict[str, list[str]] = {}
    self._output_expr: Expr | None = None

categories property

categories: list[str]

The declared category atoms.

rules property

rules: dict

The compiled rule schemas from rule declarations.

objects property

objects: dict[str, SetObject]

The compiled object environment.

spaces property

spaces: dict

The compiled space environment.

morphisms property

morphisms: dict

The compiled morphism environment.

quantale property

quantale: Quantale

The active quantale.

compile

compile() -> Program

Compile the module into a trainable Program.

RETURNS DESCRIPTION
Program

The compiled nn.Module wrapping the morphism DAG.

RAISES DESCRIPTION
CompileError

On semantic errors (undefined names, type mismatches, etc.).

Source code in src/quivers/dsl/compiler.py
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
def compile(self) -> Program:
    """Compile the module into a trainable Program.

    Returns
    -------
    Program
        The compiled nn.Module wrapping the morphism DAG.

    Raises
    ------
    CompileError
        On semantic errors (undefined names, type mismatches, etc.).
    """
    _register_extra_quantales()
    for stmt in self._module.statements:
        self._compile_statement(stmt)
    if self._output_expr is None:
        raise CompileError("no output declaration found")
    root_morphism = self._compile_expr(self._output_expr)
    return Program(root_morphism)

compile_env

compile_env() -> dict

Compile all statements and return the full environment.

Useful for inspection without requiring an output declaration.

RETURNS DESCRIPTION
dict

Combined environment of objects, spaces, morphisms, and the quantale.

Source code in src/quivers/dsl/compiler.py
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
def compile_env(self) -> dict:
    """Compile all statements and return the full environment.

    Useful for inspection without requiring an output declaration.

    Returns
    -------
    dict
        Combined environment of objects, spaces, morphisms, and the quantale.
    """
    _register_extra_quantales()
    for stmt in self._module.statements:
        self._compile_statement(stmt)
    env: dict = {}
    env["__quantale__"] = self._quantale
    for name, obj in self._objects.items():
        env[name] = obj
    for name, space in self._spaces.items():
        env[name] = space
    for name, morph in self._morphisms.items():
        env[name] = morph
    for name, rule in self._rules.items():
        env[name] = rule
    return env