Conversation
| 'OneIdentity' affects pattern matching: | ||
| >> SetAttributes[f, OneIdentity] | ||
| >> a /. f[args___] -> {args} | ||
| >> a /. f[x_:0, u_] -> {u} |
There was a problem hiding this comment.
Reading the documentation, I realize that this test was wrong...
There was a problem hiding this comment.
Good catch! This bug goes back to the very first Mathics version.
| self.last_eval = None | ||
| # Necesary to handle OneIdentity on | ||
| # lhs in assignment | ||
| self.ignore_oneidentity = False |
| def test_context2(): | ||
| nomessage = tuple([]) | ||
| for expr, expected, lst_messages, msg in [ | ||
| @pytest.mark.parametrize( |
There was a problem hiding this comment.
I reformulated this test as a parametrized test because it was easier to debug...
| from .helper import check_evaluation | ||
|
|
||
|
|
||
| DEBUGRULESPAT = int(os.environ.get("DEBUGRULESPAT", "0")) == 1 |
There was a problem hiding this comment.
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.
| ], | ||
| ) | ||
| @skip_or_fail | ||
| def test_one_identity_stil_failing(str_expr, str_expected, msg): |
There was a problem hiding this comment.
To pass these tests, we should fix the Rule.apply method. I left it for another round.
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.
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 attributeOneIdentityand chooses the second case otherwise.
In/Out 16-20 of the book make this clear.
| ), | ||
| (None, None, None), | ||
| ], | ||
| ) |
There was a problem hiding this comment.
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
matchmethods, 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.patternmodule to make it easier to understand and follow (I still do not do it fully).