Skip to content
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

Simplfy checking for patterns #1088

Merged
merged 1 commit into from
Sep 19, 2024
Merged
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
8 changes: 5 additions & 3 deletions mathics/core/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ def match_expression_with_one_identity(

# This tries to reduce the pattern to a non empty
# set of default values, and a single pattern.
from mathics.builtin.patterns import Pattern

self: ExpressionPattern = parms["self"]
vars_dict: dict = parms["vars_dict"]
Expand All @@ -721,7 +722,7 @@ def match_expression_with_one_identity(
elif pat_elem.get_head_name() == "System`Optional":
if len(pat_elem.elements) == 2:
pat, value = pat_elem.elements
if pat.get_head_name() == "System`Pattern":
if isinstance(pat, Pattern):
key = pat.elements[0].atom.name
else:
# if the first element of the Optional
Expand All @@ -731,7 +732,7 @@ def match_expression_with_one_identity(
optionals[key] = value
elif len(pat_elem.elements) == 1:
pat = pat_elem.elements[0]
if pat.get_head_name() == "System`Pattern":
if isinstance(pat, Pattern):
key = pat.elements[0].atom.name
else:
key = ""
Expand Down Expand Up @@ -893,12 +894,13 @@ def expression_pattern_match_element_orderless(
# otherwise, constructing a set() is very slow for large lists.
# performance test case:
# x = Range[100000]; Timing[Combinatorica`BinarySearch[x, 100]]
from mathics.builtin.patterns import Pattern

element: BaseElement = parms["element"]
element_candidates = set(element_candidates) # for fast lookup

sets = None
if element.get_head_name() == "System`Pattern":
if isinstance(element, Pattern):
varname = element.elements[0].get_name()
existing = parms["vars_dict"].get(varname, None)
if existing is not None:
Expand Down