jacopy.Expr

class jacopy.Expr

Bases: ABC

Abstract base class for all expressions.

Subclasses are immutable. Structural equality and hashing are derived from the node’s type together with a canonical _key() payload provided by each subclass.

__init__()

Methods

__init__()

clone()

Return self.

find(predicate)

Yield descendants (including self) satisfying predicate.

replace_at(path, new)

Return a new tree with new spliced at path.

substitute_atom(dummy, target)

Return self with every occurrence of dummy replaced by target.

walk()

Pre-order traversal yielding self followed by descendants through children only.

Attributes

children

Tuple of immediate children.

is_atom

abstract property children: Tuple[Expr, ...]

Tuple of immediate children. Atoms return ().

walk()

Pre-order traversal yielding self followed by descendants through children only. jacopy.core.traverse.iter_nodes() also descends into the rewritable_slots of operator atoms.

Return type:

Iterator[Expr]

find(predicate)

Yield descendants (including self) satisfying predicate.

Return type:

Iterator[Expr]

replace_at(path, new)

Return a new tree with new spliced at path.

path is a tuple of child indices, (1, 0) means “the first child of the second child”. An empty path replaces the root. Rebuilding uses the raw constructor (no smart-ctor flattening), so the surrounding shape is preserved exactly.

Parameters:
Return type:

Expr

clone()

Return self.

Expr instances are immutable, so structural sharing is always safe. clone() exists for API symmetry with mutable tree libraries; it does not copy.

Return type:

Expr

substitute_atom(dummy, target)

Return self with every occurrence of dummy replaced by target.

dummy must be an Atom. The default implementation does the obvious top-level match, then recurses through children(). Atoms that hide a sub-atom inside a private slot (e.g. a frame vector field carrying a FrameIndex) override this method to expose that hidden state for substitution. Binders that introduce their own dummy (notably IndexedSum) override to respect shadowing.

Parameters:
Return type:

Expr