Skip to content

Tree

opulse.trace.tree

Hierarchical derivations: one derivation that can be read at any granularity.

A :class:Derivation is the reduction of one expression in which every defined operator is computed in one step (an eval step). Each such step carries a child: the derivation of that application on its own, op(inputs), which unfolds op once (an expand or undefined step) and computes the applications of the body, each defined one again in one step with a child of its own. Following children all the way down reaches base operations only.

The cut (which defined operators are known) only decides how much of the tree is shown. :func:flatten replaces every eval step of an operator that should be unfolded by the steps of its child, recursively, and gives exactly the :class:~opulse.trace.Trace that :class:~opulse.trace.Reducer produces with the same expand and max_depth (tests/test_trace_tree.py checks this). The reduction is applicative order, leftmost-innermost, so the body of an unfolded application is reduced completely before anything to its right, which is what makes the splice exact.

Every node is a reducer trace of its own and is checked with :func:opulse.verify.verify_trace (:func:verify_tree).

JSON (:func:to_json, schema opulse.derivation/1)::

{
  "schema": "opulse.derivation/1",
  "expression": "<text of the expression>",
  "value": <int> | null,            # null: undefined (NaN)
  "root": "<node id>",
  "operators": {"<func_id>": {"symbol", "n_ary", "position", "layer", "kind",
                              "priority", "associativity", "definition"}},
  "nodes": {"<node id>": {
      "operator": "<func_id>" | null,   # null for the root expression
      "inputs": [<int> | null, ...],     # null: NaN; [] for the root
      "source": <tree>, "text": "<text of source>", "value": <int> | null,
      "steps": [{"kind": "expand" | "eval" | "undefined",
                 "path": [<child index>, ...],   # from the node's source
                 "operator": "<func_id>", "inputs": [...], "depth": <int>,
                 "before": <tree>, "after": <tree>, "expression_after": <tree>,
                 "branch": <int> | null, "condition": "<text>" | null,
                 "reason": "nan_condition" | "no_branch" | null,
                 "child": "<node id>" | null}]}}
}

kind of an operator is base (no definition), simple, branch or recursive. A <tree> is {"num": <int>}, {"num": null} (NaN), {"var": "<name>"} or {"op": "<func_id>", "args": [<tree>, ...]}. Node ids are n0 (the root), n1, ... in the order the nodes were built; equal applications share one node. depth is local to the node: 0 for steps on the node's own source, 1 for the body of its unfolding.

TreeStep dataclass

A step of one node; child opens an eval step of a defined operator.

Derivation dataclass

The coarse reduction of source; operator/inputs are set for a sub-derivation.

nodes

nodes() -> list[Derivation]

This node and every node below it, each once, in build order of discovery.

ViewStep dataclass

A step of a flattened derivation and, for an eval of a defined operator, its node.

TreeBuilder

Builds derivation trees over registry; equal applications share one node.

build

build(expression: Node) -> Derivation

The tree of expression (a closed expression), opened down to base operations.

application

application(
    operator: Operator, inputs: Iterable
) -> Derivation

The tree of one application operator(inputs), unfolded once at the top.

build_tree

build_tree(
    expression: Node,
    registry,
    runtime,
    parser,
    max_nodes: int = 50000,
) -> Derivation

Shortcut for TreeBuilder(registry, runtime, parser).build(expression).

cut

cut(
    known: Iterable[str] = (), known_layers: int = 0
) -> Callable[[Operator], bool]

The expand predicate of a cut, as in opulse expressions --cot-known(-layers).

A defined operator is unfolded unless its layer is below known_layers or its symbol or func_id is in known.

flatten_view

flatten_view(
    root: Derivation,
    expand: Callable[[Operator], bool] | None = None,
    max_depth: int | None = None,
) -> list[ViewStep]

The steps of root at a cut, with the node behind every remaining defined eval.

align

align(
    root: Derivation,
    expands: list[Callable[[Operator], bool]],
) -> tuple[list[list[ViewStep]], list[tuple[int, ...]]]

The derivation at several cuts, ordered from coarse to fine, and how their steps line up.

Returns (views, owners): views[k] is :func:flatten_view at expands[k], and owners[j][k] is the index of the step at cut k that the j-th step at the finest cut belongs to (so owners[j][-1] == j). One step at a coarser cut stands for a contiguous block of finer steps. Every cut must unfold at least what the cut before it unfolds; otherwise a ValueError is raised.

flatten

flatten(
    root: Derivation,
    expand: Callable[[Operator], bool] | None = None,
    max_depth: int | None = None,
    max_steps: int | None = None,
) -> Trace

The reducer trace of root.source at a cut: equal to Reducer(..., expand=expand, max_depth=max_depth, max_steps=max_steps).reduce(root.source).

verify_tree

verify_tree(root: Derivation, oracle) -> int

Check every node with :func:opulse.verify.verify_trace; returns the number of nodes.

to_json

to_json(root: Derivation, renderer=None) -> dict

The tree as JSON-compatible data (schema at the top of this module).

from_json

from_json(data: dict, registry) -> Derivation

Rebuild a tree written by :func:to_json (operators are looked up in registry).

render_text

render_text(
    root: Derivation,
    renderer,
    expand: Callable[[Operator], bool] | None = None,
    max_depth: int | None = None,
    open_levels: int = 0,
    indent: str = "    ",
) -> str

Plain text: the derivation at a cut, one = expression line per step with its kind; the remaining one-step evaluations of defined operators are opened open_levels deep, indented under their step.