-
-
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
improve OneIdentity
builtin
#602
Conversation
@@ -476,9 +476,14 @@ class OneIdentity(Predefined): | |||
|
|||
'OneIdentity' affects pattern matching: | |||
>> SetAttributes[f, OneIdentity] | |||
>> a /. f[args___] -> {args} | |||
>> a /. f[x_:0, u_] -> {u} |
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.
Reading the documentation, I realize that this test was wrong...
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.
Good catch! This bug goes back to the very first Mathics version.
@@ -262,9 +262,6 @@ def __init__( | |||
# status of last evaluate | |||
self.exc_result = self.SymbolNull | |||
self.last_eval = None | |||
# Necesary to handle OneIdentity on | |||
# lhs in assignment | |||
self.ignore_oneidentity = False |
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.
bye bye...
def test_context2(): | ||
nomessage = tuple([]) | ||
for expr, expected, lst_messages, msg in [ | ||
@pytest.mark.parametrize( |
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.
I reformulated this test as a parametrized test because it was easier to debug...
test/test_rules_patterns.py
Outdated
from .helper import check_evaluation | ||
|
||
|
||
DEBUGRULESPAT = int(os.environ.get("DEBUGRULESPAT", "0")) == 1 |
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.
Still, there is a part of the behavior that I did not cover. This allows you to choose to mark the corresponding tests as xfail or skip, as in other unit tests.
test/test_rules_patterns.py
Outdated
], | ||
) | ||
@skip_or_fail | ||
def test_one_identity_stil_failing(str_expr, str_expected, msg): |
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.
To pass these tests, we should fix the Rule.apply
method. I left it for another round.
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.
Looks great!
c5834d0
to
ef51773
Compare
mathics/builtin/attributes.py
Outdated
>> a /. f[u_] -> {u} | ||
= a | ||
|
||
Also, it does not affect evaluation: |
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.
I find these examples and the documentation to be extremely confusing.
Do you mind if I just rewrite the examples and doc here?
A small change is to replace "Also, it does not" with "'OneIdentity' does not"
Page 272 of the Mathematica 5 book motivates OneIdentity:
... in the case where
x_
matches a single argument in a flat function, the question comes up as to whether the object it matches is really just and argument a itself or f[a]. Mathematica choses the first of these cases if the function carries the attributeOneIdentity
and chooses the second case otherwise.
In/Out 16-20 of the book make this clear.
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.
Sure, go ahead.
test/test_rules_patterns.py
Outdated
), | ||
(None, None, None), | ||
], | ||
) |
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.
Thanks for adding these tests. The ones from Patterns.html I think should go in a doctests with suitable commentary (and then they could be removed here); they were written with the intention of describing how OneIdentity
works.
@mmatera In trying to come up with examples, I am not seeing this follow the examples shown in https://reference.wolfram.com/language/tutorial/Patterns.html
Thoughts? What is an example that shows the difference between Flat versus Flat and OneIdentity? Also, I notice that
|
This is something that I detected, but I didn't try to fix yet. I have put a test for this marked as xfail |
symbols.py: system_symbols() -> symbol_set(); "systems_symbols" name is too close Symbol( System` to module systemsymbols. Also, we now require symbols as parameters), not strings. systemsymbols.py: more system symbols
It appears this was originally Pattern.create. I suspect due to bad modularity and a lack of understandig Python that an import could be added inside the routine, this static method got moved outside of the class. Later on, the modularity was fixed, but the hack persisted. These kinds of code smells side effects of poor communication.
cc92b6c
to
318a2b8
Compare
wehn it gets fixed.
OneIdentity is an attribute that allows to implement of the property of Idempotency in the pattern matching mechanism.
https://reference.wolfram.com/language/ref/OneIdentity.html
https://mathematica.stackexchange.com/questions/124372/about-oneidentity
Documentation of this attribute in WR is not very complete, so the previous implementation was the result of a poor understanding of the expected behavior. This also forced us to put a lot of hacks in order to make basic things like assignments to work.
This PR simplifies a lot this behavior, removing unnecessary parameters in the
match
methods, and provides a behavior closer to the expected WL. Also, several new pytests were included.In the process, I started to add comments on the
mathics.core.pattern
module to make it easier to understand and follow (I still do not do it fully).