Skip to content

Commit

Permalink
Cleanup/refactoring, fixed recursion anchor of repair function
Browse files Browse the repository at this point in the history
  • Loading branch information
rindPHI committed Jan 17, 2024
1 parent 402c914 commit 3f0400e
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 141 deletions.
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[pytest]

log_cli = 1
log_cli_level = DEBUG
log_cli_level = INFO
log_cli_format = %(asctime)s [%(levelname)8s] (%(name)s) %(message)s (%(filename)s:%(lineno)s)
log_cli_date_format=%Y-%m-%d %H:%M:%S

Expand Down
3 changes: 2 additions & 1 deletion src/isla/derivation_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
Callable,
Union,
Generator,
Mapping,
)

import graphviz
Expand Down Expand Up @@ -516,7 +517,7 @@ def __len__(self):
return self.__len

def substitute(
self, subst_map: Dict["DerivationTree", "DerivationTree"]
self, subst_map: Mapping["DerivationTree", "DerivationTree"]
) -> "DerivationTree":
# We perform an iterative reverse post-order depth-first traversal and use a
# stack to store intermediate results from lower levels.
Expand Down
46 changes: 44 additions & 2 deletions src/isla/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,50 @@ def singleton_iterator(elem: T) -> Iterator[T]:
return iter([elem])


def star(f: Callable[[[Any, ...]], T]) -> Callable[[Sequence[Any]], T]:
return lambda x: f(*x)
def star(f: Callable[..., T], do_flatten=False) -> Callable[..., T]:
"""
Transforms a function accepting n arguments into one accepting a sequence of n
elements. If :code:`do_flatten` is True, the returned function accepts multiple
arguments. Sequences in these arguments are flattened.
Example
-------
Transforming a function adding two numbers to a function adding the two elements
of a list of numbers:
>>> star(lambda a, b: a + b)([1, 2])
3
Transforming a function adding four numbers to a function adding all passed numbers
or their elements using flattening. There must be exactly four numbers in the
given arguments.
>>> star(lambda a, b, c, d: a + b + c + d, do_flatten=True)(1, [2, 3], 4)
10
It does not matter if the passed arguments are inside a list or not:
>>> star(lambda a, b, c, d: a + b + c + d, do_flatten=True)([1], 2, [3, 4])
10
But we must pass exactly five numbers:
>>> star(lambda a, b, c, d: a + b + c + d, do_flatten=True)(1, [2, 3, 4], 5)
Traceback (most recent call last):
...
TypeError: <lambda>() takes 4 positional arguments but 5 were given
:param f: The function to be "starred."
:param do_flatten: Set to True if the returned function should accept multiple
arguments that are flattened before forwarding them to the original function.
:return: The "starred" function.
"""

if do_flatten:
return lambda *x: f(*flatten(x))
else:
return lambda x: f(*x)


def is_path(maybe_path: Any) -> bool:
Expand Down
4 changes: 2 additions & 2 deletions src/isla/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -1502,7 +1502,7 @@ def substitute_variables(self, subst_map: Dict[Variable, Variable]) -> "SMTFormu

def substitute_expressions(
self,
subst_map: Dict[Union[Variable, DerivationTree], DerivationTree],
subst_map: Mapping[Union[Variable, DerivationTree], DerivationTree],
force: bool = False,
) -> "SMTFormula":
if not force and not self.auto_eval and not subst_map_relevant(self, subst_map):
Expand Down Expand Up @@ -1720,7 +1720,7 @@ def __hash__(self):

def subst_map_relevant(
formula: Formula,
subst_map: Dict[Union[Variable, DerivationTree], DerivationTree],
subst_map: Mapping[Union[Variable, DerivationTree], DerivationTree],
) -> bool:
return formula.free_variables().intersection(subst_map.keys()) or any(
tree_arg.find_node(subst_tree) is not None
Expand Down
Loading

0 comments on commit 3f0400e

Please sign in to comment.