Skip to content

Commit

Permalink
More pattern and rule related tweaks (#1125)
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky authored Oct 12, 2024
1 parent bed36ea commit ba8c5ba
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
3 changes: 3 additions & 0 deletions mathics/builtin/patterns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@
The attributes 'Flat', 'Orderless', and 'OneIdentity' affect pattern matching.
"""

# This tells documentation how to sort this module
sort_order = "mathics.builtin.rules-and-patterns"
14 changes: 7 additions & 7 deletions mathics/builtin/patterns/restrictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""
from typing import Optional as OptionalType
from typing import Optional as OptionalType, Tuple

from mathics.core.atoms import Integer, Number, Rational, Real, String
from mathics.core.attributes import A_HOLD_REST, A_PROTECTED
Expand All @@ -27,11 +27,11 @@ class Condition(BinaryOperator, PatternObject):
<dl>
<dt>'Condition[$pattern$, $expr$]'
<dt>'$pattern$ /; $expr$'
<dd>places an additional constraint on $pattern$ that only
allows it to match if $expr$ evaluates to 'True'.
<dd>places an additional constraint on $pattern$ that only \
allows it to match if $expr$ evaluates to 'True'.
</dl>
The controlling expression of a 'Condition' can use variables from
The controlling expression of a 'Condition' can use variables from \
the pattern:
>> f[3] /. f[x_] /; x>0 -> t
= t
Expand Down Expand Up @@ -92,8 +92,8 @@ class PatternTest(BinaryOperator, PatternObject):
<dl>
<dt>'PatternTest[$pattern$, $test$]'
<dt>'$pattern$ ? $test$'
<dd>constrains $pattern$ to match $expr$ only if the
evaluation of '$test$[$expr$]' yields 'True'.
<dd>constrains $pattern$ to match $expr$ only if the \
evaluation of '$test$[$expr$]' yields 'True'.
</dl>
>> MatchQ[3, _Integer?(#>0&)]
Expand Down Expand Up @@ -354,5 +354,5 @@ def yield_match(vars_2, rest):
# except StopGenerator:
# pass

def get_match_count(self, vars_dict: OptionalType[dict] = None):
def get_match_count(self, vars_dict: OptionalType[dict] = None) -> Tuple[int, int]:
return self.pattern.get_match_count(vars_dict)
6 changes: 3 additions & 3 deletions mathics/builtin/patterns/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def eval_list(
result = eval_dispatch_atom(rules, evaluation)
return result

def eval_list(
def eval(
self, rules: Expression, evaluation: Evaluation
) -> OptionalType[BaseElement]:
"""Dispatch[rules_]"""
Expand Down Expand Up @@ -354,7 +354,7 @@ class ReplaceList(Builtin):
"reps": "`1` is not a valid replacement rule.",
"rmix": "Elements of `1` are a mixture of lists and nonlists.",
}
summary_text = "list of possible replacement results"
summary_text = "list possible replacement results"

def eval(
self,
Expand Down Expand Up @@ -462,7 +462,7 @@ def eval_list(
return rules

maxit = self.get_option(options, "MaxIterations", evaluation)
if maxit.is_numeric(evaluation):
if maxit is not None and maxit.is_numeric(evaluation):
maxit = maxit.get_int_value()
else:
maxit = -1
Expand Down
4 changes: 3 additions & 1 deletion mathics/core/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,9 @@ def get_name(self):

return ""

def get_option_values(self, evaluation, allow_symbols=False, stop_on_error=True):
def get_option_values(
self, evaluation: "Evaluation", allow_symbols=False, stop_on_error=True
) -> Optional[dict]:
pass

def get_precision(self) -> Optional[int]:
Expand Down
4 changes: 2 additions & 2 deletions mathics/core/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,11 +769,11 @@ def get_mutable_elements(self) -> list:
return list(self._elements)

def get_option_values(
self, evaluation, allow_symbols=False, stop_on_error=True
self, evaluation: Evaluation, allow_symbols=False, stop_on_error=True
) -> Optional[dict]:
"""
Build a dictionary of options from an expression.
For example Symbol("Integrate").get_option_values(evaluation, allow_symbols=True)
For example, Symbol("Integrate").get_option_values(evaluation, allow_symbols=True)
will return a list of options associated to the definition of the symbol "Integrate".
"""
options = self
Expand Down

0 comments on commit ba8c5ba

Please sign in to comment.