Repository navigation
Flat patterns: a blank can take several elements before a literal; no element is lost - #2011
Merged
Merged
Conversation
… element is lost
Two bugs in matching against an expression whose head is Flat:
* basic_match_expression pairs leading pattern elements of match count 1
with expression elements one by one. With Flat a blank x_ can take
several elements (x_ in f[x_, z] matches f[x, y] in f[x, y, z]), so
f[a_, 0] never matched f[x, y, 0]. Skip that shortcut for Flat heads
as it is already skipped for Orderless ones.
* After the first pattern element matched, the next one was still
matched with "first" set, so in a partial match (fully=False) it could
start anywhere, skipping elements; the skipped ones were then dropped
from the result: f[x, y, z] /. f[a__, z] :> {a} gave {x}. Only the
first element may leave elements before it.
This also fixes Mathics3#1381 and the examples in the Scope section of the
Flat reference page (f[a, b, c, d, e] /. f[b, c, d] -> x).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 16, 2026
mmatera
reviewed
Oct 16, 2026
Member
|
Wow - this is complicated.
@tombouctou would you please open an issue so we have this tracked? LGTM - thanks for fixing this long-standing bug. |
Collaborator
Author
|
Thanks! Opened #2026 for these two (both come from the same thing: with a |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1381.
Two bugs in matching a pattern against an expression whose head is
Flat.1. A blank before a literal never takes several elements.
basic_match_expressionpairs the leading pattern elements whose match count is 1 with the expression's elements one by one, as a fast pre-check. WithFlat, a blankx_can take several elements (x_inf[x_, z]matchesf[x, y]inf[x, y, z]), so the pre-check rejected valid matches. It is already skipped forOrderlessheads; this skips it forFlatheads too.2. Elements are dropped. After the first pattern element matched, the next one was matched with
firststill set. In a partial match (fully=False, asReplaceAlluses forFlatheads) it could then start anywhere, skipping elements, and the skipped elements were dropped from the result. Only the first element may leave elements before it.Together, the inputs from the Scope section of the
Flatreference page now work:Tests:
test_flat_patternsintest/builtin/test_patterns.py; 11 of its cases fail on master. Full pytest and doctests pass.Not changed here (separate differences from WMA):
f[a, b, c] /. f[x_] -> {x}givesf[{f[a, b]}, c]rather than{f[a, b, c]}, andMatchQ[f[1, 2], f[_]]givesFalse.🤖 Generated with Claude Code