Skip to content

Commit

Permalink
Optimize pyk.kast.inner.collect (#4708)
Browse files Browse the repository at this point in the history
The previous implementation called `bottom_up` and as a side effect
created a deep copy of the input term.
  • Loading branch information
tothtamas28 authored Dec 11, 2024
1 parent e2a9c6f commit c4d9777
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions pyk/src/pyk/kast/inner.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,20 +877,18 @@ def _var_occurence(_term: KInner) -> None:
return _var_occurrences


# TODO replace by method that does not reconstruct the AST
def collect(callback: Callable[[KInner], None], kinner: KInner) -> None:
"""Collect information about a given term traversing it bottom-up using a function with side effects.
"""Collect information about a given term traversing it top-down using a function with side effects.
Args:
callback: Function with the side effect of collecting desired information at each AST node.
kinner: The term to traverse.
"""

def f(kinner: KInner) -> KInner:
callback(kinner)
return kinner

bottom_up(f, kinner)
subterms = [kinner]
while subterms:
term = subterms.pop()
subterms.extend(reversed(term.terms))
callback(term)


def build_assoc(unit: KInner, label: str | KLabel, terms: Iterable[KInner]) -> KInner:
Expand Down

0 comments on commit c4d9777

Please sign in to comment.