-
-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SameQ[] and Expression module tweaks #1079
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5868b8b
Merge github.com:Mathics3/mathics-core into sameQ_nonrecursive
rocky c81bd71
Tweaks to iterative SameQ..
rocky 6465195
Rename expression_sameQ to eval_SameQ ..
rocky 9b796ef
Fix CI syntax problem
rocky aa6cbf0
Merge branch 'sameQ_nonrecursive' into sameq-tweaks
mmatera File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
import time | ||
from bisect import bisect_left | ||
from itertools import chain | ||
from typing import Any, Callable, Iterable, List, Optional, Tuple, Type, Union | ||
from typing import Any, Callable, Iterable, Optional, Tuple, Type, Union | ||
|
||
import sympy | ||
|
||
|
@@ -88,21 +88,17 @@ | |
) | ||
|
||
|
||
def expression_sameQ(self, other): | ||
def eval_SameQ(self, other): | ||
""" | ||
Iterative implementation of SameQ. | ||
Iterative implementation of SameQ[]. | ||
|
||
Run a tree transversal comparison between `self` and `other`. | ||
Tree traversal comparison between `self` and `other`. | ||
Return `True` if both tree structures are equal. | ||
|
||
This implementation avoids the issue with | ||
the recursion limit in Python 3.12 | ||
This non-recursive implementation reduces the Python stack needed | ||
in evaluation. Staring in Python 3.12 there is a limit on the | ||
recursion level. | ||
""" | ||
# TODO: Consider a faster implementation. | ||
# Would be better to use iterators and yield | ||
# instead of this light stack implementation? | ||
# Or maybe using some tail recursive implementation? | ||
# Other ideas in https://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion/ | ||
|
||
len_elements = len(self.elements) | ||
if len(other._elements) != len_elements: | ||
|
@@ -234,14 +230,6 @@ def union(expressions, evaluation) -> Optional["ExpressionCache"]: | |
): | ||
return None | ||
|
||
# FIXME: this is workaround the current situtation that some | ||
# Atoms, like String, have a cache even though they don't need | ||
# it, by virtue of this getting set up in | ||
# BaseElement.__init__. Removing the self._cache in there the | ||
# causes Boxing to mess up. Untangle this mess. | ||
if expr._cache is None: | ||
return None | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My linter is telling me correctly that |
||
symbols = set.union(*[expr._cache.symbols for expr in expressions]) | ||
|
||
return ExpressionCache( | ||
|
@@ -250,12 +238,12 @@ def union(expressions, evaluation) -> Optional["ExpressionCache"]: | |
|
||
|
||
class Expression(BaseElement, NumericOperators, EvalMixin): | ||
""" | ||
A Mathics3 M-Expression. | ||
"""A Mathics3 (compound) M-Expression. | ||
|
||
A Mathics3 M-Expression is a list where the head is a function designator. | ||
(In the more common S-Expression the head is an a Symbol. In Mathics this can be | ||
an expression that acts as a function. | ||
A Mathics3 M-Expression is a list where the head is a function | ||
designator. (In the more common S-Expression the head is an a | ||
Symbol. In Mathics3, this can be an expression that acts as a | ||
function. | ||
|
||
positional Arguments: | ||
- head -- The head of the M-Expression | ||
|
@@ -266,10 +254,11 @@ class Expression(BaseElement, NumericOperators, EvalMixin): | |
|
||
Keyword Arguments: | ||
- elements_properties -- properties of the collection of elements | ||
|
||
""" | ||
|
||
_head: BaseElement | ||
_elements: List[BaseElement] | ||
_elements: Tuple[BaseElement] | ||
_sequences: Any | ||
_cache: Optional[ExpressionCache] | ||
elements_properties: Optional[ElementsProperties] | ||
|
@@ -492,7 +481,7 @@ def elements(self, values: Iterable): | |
self.elements_properties = None | ||
|
||
def equal2(self, rhs: Any) -> Optional[bool]: | ||
"""Mathics two-argument Equal (==) | ||
"""Mathics3 two-argument Equal (==) | ||
returns True if self and rhs are identical. | ||
""" | ||
if self.sameQ(rhs): | ||
|
@@ -762,6 +751,9 @@ def get_head_name(self): | |
return self._head.name if isinstance(self._head, Symbol) else "" | ||
|
||
def get_lookup_name(self) -> str: | ||
""" | ||
Returns symbol name of leftmost head. | ||
""" | ||
lookup_symbol = self._head | ||
while True: | ||
if isinstance(lookup_symbol, Symbol): | ||
|
@@ -1140,7 +1132,7 @@ def rewrite_apply_eval_step(self, evaluation) -> Tuple["Expression", bool]: | |
# used later, include: HoldFirst / HoldAll / HoldRest / HoldAllComplete. | ||
|
||
# Note: self._head can be not just a symbol, but some arbitrary expression. | ||
# This is what makes expressions in Mathics be M-expressions rather than | ||
# This is what makes expressions in Mathics3 be M-expressions rather than | ||
# S-expressions. | ||
head = self._head.evaluate(evaluation) | ||
|
||
|
@@ -1447,14 +1439,14 @@ def round_to_float( | |
return None | ||
|
||
def sameQ(self, other: BaseElement) -> bool: | ||
"""Mathics SameQ""" | ||
"""Mathics3 SameQ""" | ||
if not isinstance(other, Expression): | ||
return False | ||
if self is other: | ||
return True | ||
|
||
# All this stuff maybe should be in mathics.eval.expression | ||
return expression_sameQ(self, other) | ||
return eval_SameQ(self, other) | ||
|
||
def sequences(self): | ||
cache = self._cache | ||
|
@@ -1558,7 +1550,7 @@ def to_python(self, *args, **kwargs): | |
# ) | ||
return py_obj | ||
|
||
# Notice that in this case, `to_python` returns a Mathics Expression object, | ||
# Notice that in this case, `to_python` returns a Mathics3 Expression object, | ||
# instead of a builtin native object. | ||
return self | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not clear to me this is not fast enough. What is done here is equivalent to tail recursion elimination.
Actually, on my home machine I've never ran into the recursion limit problem. I guess I have more memory. The reason Python 3.12 put compiler/OS limitation to recursion was no doubt some sort of security thing. People aren't going to know (or care about) what is being referred to with "the issue".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, comparing the time the tests takes in GithubActions, it seems that this is faster than the recursive implementation.