Repository navigation
Fix TypeError sorting Orderless patterns that mix atoms and literal expressions - #1991
tombouctou wants to merge 1 commit into
Conversation
…xpressions Since Mathics3#1987 a literal ExpressionPattern sorts by (MAGIC_ATOM_SORT_KEY, element_order), while atoms use (MAGIC_ATOM_SORT_KEY, 0, 0). Both share the magic number, so sorting the elements of an Orderless pattern such as 2 x[3] compares 0 with a tuple: MatchQ[2 x[3], 2 x[3]] raises TypeError. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The only reason I hesitate is that this is the first batch of Claude Code-generated PRs. Or at least of this number and magnitude; and we've not said anything publicly about the policy. My own take is that using Claude Code is okay, provided that, as before, people (and reviewers) take responsibility for the results produced. But I was holding off until hearing from @mmatera @mmatera: your thoughts? I'll be going over each PR, which may take a little time. (This one was immediately obvious, though.) And thanks for all of the fixes. |
|
Thanks for looking at it so quickly, and for being open about the policy question. That's fair. To be clear on our side: I take responsibility for these PRs as the person submitting them. Claude Code does much of the work and every PR says so, but what goes out is something we have checked and stand behind. We put particular effort into validating each change:
That last check comes from what these PRs are for. We are trying to run Neil Strickland's Mathematica package for Toda's tables of unstable homotopy groups of spheres (strickland1.org/toda) unmodified in Mathics3. With the fixes from the open PRs:
The lambda algebra homology, computed with the new There's no rush on our side; please review at whatever pace suits you. If you'd prefer them in a different form, say fewer at a time or grouped differently, just tell us. And thanks for Mathics3; the package was a good fit for it. |
I join in the thanks for trying the code and for looking for ways to improve it. Regarding the AI code, in particular, I am fine with using Claude, Deepseek, or any other AI-generated code, as long as humans (us) review it. The main risk would be flooding our reviewing capacity (which is something that is happening in many other areas), but in this case, most of the proposed changes are relatively small and self-contained, so I think we can handle it. |
| ("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 |
There was a problem hiding this comment.
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.
| # here too, or sorting an Orderless pattern mixing both fails. | ||
| return ( | ||
| BASIC_LITERAL_EXPRESSION_PATTERN_SORT_KEY, | ||
| 1, |
There was a problem hiding this comment.
Instead of magic number 1, we should define a constant like BASIC_LITERAL_EXPRESSION_SECOND_SLOT = 1 in mathics.core.keycomparable
| 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, ...): |
|
Probably the implementation of the fix would be changed when I finish with the rule ordering refactor. But the new tests are good, so I am happy to merge this. |
Since #1987, a literal
ExpressionPatternsorts by(MAGIC_ATOM_SORT_KEY, element_order), while an atom pattern usesBASIC_ATOM_PATTERN_SORT_KEY = (MAGIC_ATOM_SORT_KEY, 0, 0). The two keys share the same first element, so sorting the elements of an Orderless pattern that contains both compares0with a tuple:The same crash happens for any assignment or replacement whose left-hand side contains such a pattern, e.g.
f[2 x[3]] = 1orf[x[3] + 2] = 1. On 10.0.1 these work.This PR keeps the literal-expression key in the shape
(magic, int, ...), with1in the second slot. Atoms (0) still sort before literal expressions, and two literal expressions are still ordered byelement_order, as #1987 intended.Tests: new cases in
test/builtin/test_testing_expressions.py(test_matchq) andtest/builtin/test_patterns.py(test_pattern). They fail on current master and pass with this change.🤖 Generated with Claude Code