Skip to content
Merged
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
22 changes: 15 additions & 7 deletions mathics/core/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -1058,13 +1058,11 @@ def expression_pattern_match_element_orderless(
needed = existing.elements
else:
needed = (existing,)

available = list(candidates)

for needed_element in needed:
if (
needed_element in available
and needed_element in element_candidates # nopep8
):
if needed_element in available and needed_element in element_candidates:
available.remove(needed_element)
else:
return set()
Expand Down Expand Up @@ -1304,8 +1302,18 @@ def per_name(yield_name: Callable, groups: Tuple, vars_dict: dict):

def sequence_matches(pattern, seq, vars_dict, evaluation):
"""Helper: check if pattern matches the whole sequence."""
# Create a dummy expression with head Sequence.
seq_expr = Expression(SymbolSequence, *seq)
# For a single element, match the bare element directly.
# Wrapping it in Sequence[...] would make the head of the
# matched expression "Sequence" instead of the element's own
# head, which breaks typed patterns like a_Integer (Blank[Integer]
# checks the head of what it's given -- Sequence[1] has head
# Sequence, not Integer, even though the untyped Blank[] doesn't
# care and matches either way).
if len(seq) == 1:
match_target = seq[0]
else:
match_target = Expression(SymbolSequence, *seq)

# Use a temporary context to see if the match consumes all.
consumed = False

Expand All @@ -1321,7 +1329,7 @@ def capture(vars, rest):
"fully": True,
}
try:
pattern.match(seq_expr, ctx)
pattern.match(match_target, ctx)
except StopGenerator:
pass
return consumed
Expand Down
3 changes: 0 additions & 3 deletions test/core/test_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,6 @@ def test_exact_supply_no_double_counting():
)


@pytest.mark.xfail(
reason="Problem with restricted patterns, like g[a_Integer,a_Integer, rest___]"
)
def test_repeated_name_respects_type_constraint():
"""a_Integer, a_Integer should not bind to a non-Integer duplicate."""
check_evaluation(None, None, None)
Expand Down
Loading
Loading