Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion mathics/core/pattern/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,14 @@ def __init__(

def _build_pattern_sort_key(self) -> tuple:
if self.isliteral:
return (BASIC_LITERAL_EXPRESSION_PATTERN_SORT_KEY, self.expr.element_order)
# Keep the shape of BASIC_ATOM_PATTERN_SORT_KEY (magic, int, ...):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

# atoms share the magic number, so the second slot must be an int
# here too, or sorting an Orderless pattern mixing both fails.
return (
BASIC_LITERAL_EXPRESSION_PATTERN_SORT_KEY,
1,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of magic number 1, we should define a constant like BASIC_LITERAL_EXPRESSION_SECOND_SLOT = 1 in mathics.core.keycomparable

self.expr.element_order,
)
return (
BASIC_EXPRESSION_PATTERN_SORT_KEY,
self.head.pattern_precedence,
Expand Down
3 changes: 3 additions & 0 deletions test/builtin/test_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ def test_dispatch(str_expr, msgs, str_expected, fail_msg):
("f[10]", None, "100", None),
("f[10, Power -> 3]", None, "1000", None),
("Clear[f]", None, None, None),
# Orderless literal pattern mixing an atom and an expression

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and on 179, it would be nice to put this comment inside the assert fail message (fail_msg or last argument). That way, when something fails, the message indicates the kind of thing that failed.

("f[2 x[3]] = 1; f[2 x[3]]", None, "1", None),
("Clear[f]", None, None, None),
],
)
def test_pattern(str_expr, msgs, str_expected, fail_msg):
Expand Down
4 changes: 4 additions & 0 deletions test/builtin/test_testing_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ def test_numerical_properties(str_expr, msgs, str_expected, fail_msg):
("MatchQ[1, a_.*b_.]", None, "True", None),
("MatchQ[x, a_.*b_.]", None, "True", None),
("MatchQ[2*x, a_.*b_.]", None, "True", None),
# Orderless literal patterns mixing an atom and an expression
("MatchQ[2 x[3], 2 x[3]]", None, "True", None),
("MatchQ[x[3] + 2, 2 + x[3]]", None, "True", None),
("MatchQ[2 x[3], 3 x[3]]", None, "False", None),
],
)
def test_matchq(str_expr, msgs, str_expected, fail_msg):
Expand Down
Loading