-
-
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
1 / Fix issues with pattern matching for Rubi #1176
Merged
rocky
merged 10 commits into
Mathics3:master
from
aravindh-krishnamoorthy:pattern-fix-rubi
Nov 23, 2024
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
85ad0f5
Fix pattern matching: pop optionals.
aravindh-krishnamoorthy c1999ce
Fix optional patterns without default values for the given positions.
aravindh-krishnamoorthy e1dee54
Fix optional patterns without default values: Newpattern only if not …
aravindh-krishnamoorthy 0a89945
Do not allow optional patterns for more than one argument.
aravindh-krishnamoorthy eff7ebc
Add tests for the fixes.
aravindh-krishnamoorthy bac8e03
Run isort and black.
aravindh-krishnamoorthy e1d342f
Merge branch 'master' into pattern-fix-rubi
aravindh-krishnamoorthy 0f9786a
Add extended tests for function patterns.
aravindh-krishnamoorthy ae7ea50
Minor housekeeping.
aravindh-krishnamoorthy 8a7a564
Move Mathics tests to toplevel; rewrite mathics.eval.patterns tests i…
aravindh-krishnamoorthy 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Unit tests for mathics pattern matching | ||
""" | ||
|
||
import sys | ||
import time | ||
from test.helper import check_evaluation, evaluate | ||
|
||
import pytest | ||
|
||
@pytest.mark.parametrize( | ||
("str_expr", "msgs", "str_expected", "fail_msg"), | ||
[ | ||
# Two default arguments (linear) | ||
("MatchQ[1, a_.+b_.*x_]",None,"True",None), | ||
("MatchQ[x, a_.+b_.*x_]",None,"True",None), | ||
("MatchQ[2*x, a_.+b_.*x_]",None,"True",None), | ||
("MatchQ[1+x, a_.+b_.*x_]",None,"True",None), | ||
("MatchQ[1+2*x, a_.+b_.*x_]",None,"True",None), | ||
# Default argument (power) | ||
("MatchQ[1, x_^m_.]",None,"True",None), | ||
("MatchQ[x, x_^m_.]",None,"True",None), | ||
("MatchQ[x^1, x_^m_.]",None,"True",None), | ||
("MatchQ[x^2, x_^m_.]",None,"True",None), | ||
# Two default arguments (power) | ||
("MatchQ[1, x_.^m_.]",None,"True",None), | ||
("MatchQ[x, x_.^m_.]",None,"True",None), | ||
("MatchQ[x^1, x_.^m_.]",None,"True",None), | ||
("MatchQ[x^2, x_.^m_.]",None,"True",None), | ||
# Two default arguments (no non-head) | ||
("MatchQ[1, a_.+b_.]",None,"True",None), | ||
("MatchQ[x, a_.+b_.]",None,"True",None), | ||
("MatchQ[1+x, a_.+b_.]",None,"True",None), | ||
("MatchQ[1+2*x, a_.+b_.]",None,"True",None), | ||
("MatchQ[1, a_.+b_.]",None,"True",None), | ||
("MatchQ[x, a_.*b_.]",None,"True",None), | ||
("MatchQ[2*x, a_.*b_.]",None,"True",None), | ||
], | ||
) | ||
def test_patterns(str_expr, msgs, str_expected, fail_msg): | ||
"""patterns""" | ||
check_evaluation( | ||
str_expr, | ||
str_expected, | ||
to_string_expr=True, | ||
to_string_expected=True, | ||
hold_expected=True, | ||
failure_message=fail_msg, | ||
expected_messages=msgs, | ||
) |
Oops, something went wrong.
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.
Something that would be interesting is to check MatchQ over expressions of the form
A[x_.,B[y_.*z_]]
and check the match with
A[1,B[2,y]]
,A[1,B[y,2]]
,A[B[y,2],1]
for different settings of the attributes ofA
andB
.Another group of tests should check if variables are properly assigned. To do that, we should test rules using these patterns. For example,
rule=A[x_.,B[y_.*z_]]->{x,y,z}
and then apply it to the previous cases.
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.
Agree! This makes sense.
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 will add them as soon as I have all your and rocky's comments.
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'd like to see more/some unit tests for
mathics.core.pattern
as its own unit tests, e.g. (not via MatchQ). Other than that, I don't have any further comments.When everything is in place, I'd like to look over.
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.
Done!
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 guess it's always gonna be via
MatchQ
or rules, as mmatera suggested. Alternate ideas are welcome!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.
Also, please feel free to add commits to this PR. I think I've enabled the option.
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.
At some point, we should try to add tests at a lower level (using
mathics.eval.patterns.match
or directly testing the subclasses ofmathics.core.pattern.Pattern
) that avoids passing through the evaluation loop. But for now I think this is a good starting point.