Skip to content
Closed
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
12 changes: 6 additions & 6 deletions mathics/builtin/atomic/symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Symbol Handling

Symbolic data. Every symbol has a unique name, exists in a certain context \
or namespace, and can have a variety of type of values and attributes.
or namespace, and can have a variety of types of values and attributes.
"""
import re
from typing import Callable, Optional
Expand All @@ -28,7 +28,7 @@
from mathics.core.evaluation import Evaluation
from mathics.core.expression import Expression
from mathics.core.list import ListExpression
from mathics.core.rules import Rule
from mathics.core.rules import RewriteRule
from mathics.core.symbols import (
Symbol,
SymbolFalse,
Expand Down Expand Up @@ -69,7 +69,7 @@ def rhs_format(expr):
return expr

def format_rule(
rule: Rule,
rule: RewriteRule,
up: bool = False,
lhs: Callable = lambda k: k,
rhs: Callable = lambda r: r,
Expand All @@ -78,7 +78,7 @@ def format_rule(
Add a line showing `rule`
"""
evaluation.check_stopped()
if isinstance(rule, Rule):
if isinstance(rule, RewriteRule):
lhs_pat = Expression(SymbolInputForm, lhs(rule.pattern.expr))
repl_expr = rhs(
rule.replace.replace_vars(
Expand Down Expand Up @@ -363,7 +363,7 @@ class DownValues(Builtin):
= {HoldPattern[f[x_]] ⧴ x ^ 2}

Mathics3 will sort the rules you assign to a symbol according to \
their specificity. If it cannot decide which rule is more special, \
their specificity. If it cannot decide which rule is more specific, \
the newer one will get higher precedence.
>> f[x_Integer] := 2
>> f[x_Real] := 3
Expand Down Expand Up @@ -420,7 +420,7 @@ class FormatValues(Builtin):
>> FormatValues[F]
= {HoldPattern[Subscript[x_, F]] ⧴ Subscript[x, F]}

The replacment pattern on the right in the delayed rule is formatted according to the top-level form. To see the rule input, we can use 'InputForm':
The replacement pattern on the right in the delayed rule is formatted according to the top-level form. To see the rule input, we can use 'InputForm':
>> FormatValues[F] //InputForm
= {HoldPattern[Format[F[x_], OutputForm]] ⧴ Subscript[x, F]}
"""
Expand Down
11 changes: 6 additions & 5 deletions mathics/builtin/box/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""
from abc import ABC
from math import atan2, cos, degrees, pi, sin
from typing import Any, Dict, Final, List, Optional, Tuple
from typing import Any, Final, Optional

from mathics.builtin.box.expression import BoxExpression
from mathics.builtin.colors.color_directives import (
Expand All @@ -31,6 +31,7 @@
from mathics.core.expression import Expression
from mathics.core.formatter import lookup_method
from mathics.core.list import ListExpression
from mathics.core.rules import is_rule
from mathics.core.symbols import Symbol
from mathics.core.systemsymbols import SymbolInsetBox, SymbolTraditionalForm
from mathics.format.box import format_element
Expand All @@ -56,7 +57,7 @@ def init(self, graphics, item=None, style={}, opacity=1.0):
# GraphicsElementBox Builtin class that should not get added as a definition,
# and therefore not added to to external documentation.

DOES_NOT_ADD_BUILTIN_DEFINITION: Final[List[BoxExpression]] = [GraphicsElementBox]
DOES_NOT_ADD_BUILTIN_DEFINITION: Final[list[BoxExpression]] = [GraphicsElementBox]


class _Polyline(GraphicsElementBox):
Expand Down Expand Up @@ -471,9 +472,9 @@ class GraphicsBox(BoxExpression):
summary_text = "symbol used in boxing 'Graphics'"

def init(self, *items, **kwargs):
self._elements: Optional[Tuple[BaseElement], ...] = None
self._elements: Optional[tuple[BaseElement], ...] = None
self.content = items[0]
self.box_options: Dict[str, Any] = kwargs
self.box_options: dict[str, Any] = kwargs
self.background_color = None
self.tooltip_text: Optional[str] = None
self.evaluation = kwargs.pop("_evaluation", None)
Expand Down Expand Up @@ -763,7 +764,7 @@ def init(self, graphics, style, item=None):
self.do_init(graphics, points)
self.vertex_colors = None
for element in item.elements[1:]:
if not element.has_form("Rule", 2):
if not is_rule(element):
raise BoxExpressionError
name = element.elements[0].get_name()
self.process_option(name, element.elements[1])
Expand Down
3 changes: 2 additions & 1 deletion mathics/builtin/functional/apply_fns_to_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from mathics.core.exceptions import InvalidLevelspecError, MessageException
from mathics.core.expression import Expression
from mathics.core.list import ListExpression
from mathics.core.rules import is_rule
from mathics.core.symbols import Atom, Symbol, SymbolNull, SymbolTrue
from mathics.core.systemsymbols import SymbolMapThread
from mathics.eval.functional.apply_fns_to_lists import eval_MapAt
Expand Down Expand Up @@ -191,7 +192,7 @@ def callback(level):
#
# Fixing this would require a different implementation of this eval_ method.
#
if is_association and level.has_form(("Rule", "RuleDelayed"), 2):
if is_association and is_rule(level):
return Expression(
level.get_head(),
level.elements[0],
Expand Down
17 changes: 9 additions & 8 deletions mathics/builtin/list/associations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from mathics.core.convert.expression import to_mathics_list
from mathics.core.evaluation import Evaluation
from mathics.core.expression import Expression
from mathics.core.rules import is_rule
from mathics.core.symbols import Symbol, SymbolTrue
from mathics.core.systemsymbols import SymbolAssociation, SymbolMakeBoxes, SymbolMissing
from mathics.eval.list.associations import (
Expand Down Expand Up @@ -68,7 +69,7 @@ def eval_makeboxes(self, rules, f, evaluation: Evaluation):

def validate(exprs):
for expr in exprs:
if expr.has_form(("Rule", "RuleDelayed"), 2):
if is_rule(expr):
pass
elif expr.has_form(("List", "Association"), None):
if not validate(expr.elements):
Expand Down Expand Up @@ -96,7 +97,7 @@ def eval(self, rules, evaluation: Evaluation):

def make_flatten(exprs, rules_dictionary: dict = {}):
for expr in exprs:
if expr.has_form(("Rule", "RuleDelayed"), 2):
if is_rule(expr):
elements = expr.elements
key = elements[0].evaluate(evaluation)
value = elements[1].evaluate(evaluation)
Expand All @@ -117,7 +118,7 @@ def eval_key(self, rules, key, evaluation: Evaluation):

def find_key(exprs, rules_dictionary: dict = {}):
for expr in exprs:
if expr.has_form(("Rule", "RuleDelayed"), 2):
if is_rule(expr):
if expr.elements[0] == key:
rules_dictionary[key] = expr.elements[1]
elif expr.has_form(("List", "Association"), None):
Expand Down Expand Up @@ -158,7 +159,7 @@ class AssociationQ(Test):
def test(self, expr) -> bool:
def validate(elements):
for element in elements:
if element.has_form(("Rule", "RuleDelayed"), 2):
if is_rule(element):
pass
elif element.has_form(("List", "Association"), None):
if not validate(element.elements):
Expand Down Expand Up @@ -233,7 +234,7 @@ def eval(self, rules, evaluation: Evaluation):
"Keys[rules_]"

def get_keys(expr):
if expr.has_form(("Rule", "RuleDelayed"), 2):
if is_rule(expr):
return expr.elements[0]
elif expr.has_form("List", None) or (
expr.has_form("Association", None)
Expand All @@ -258,7 +259,7 @@ def eval_with_head(self, rules, head, evaluation: Evaluation):
"Keys[rules_, head_]"

def get_keys_with_head(expr, h):
if expr.has_form(("Rule", "RuleDelayed"), 2):
if is_rule(expr):
key = expr.elements[0]
return Expression(h, key)
elif expr.has_form("List", None) or (
Expand Down Expand Up @@ -419,7 +420,7 @@ def eval(self, rules, evaluation: Evaluation):
"Values[rules_]"

def get_values(expr):
if expr.has_form(("Rule", "RuleDelayed"), 2):
if is_rule(expr):
return expr.elements[1]
elif expr.has_form("List", None) or (
expr.has_form("Association", None)
Expand All @@ -445,7 +446,7 @@ def eval_with_head(self, rules, head, evaluation: Evaluation):
"Values[rules_, head_]"

def get_values_with_head(expr, h):
if expr.has_form(("Rule", "RuleDelayed"), 2):
if is_rule(expr):
value = expr.elements[1]
return Expression(h, value)
elif expr.has_form("List", None) or (
Expand Down
12 changes: 5 additions & 7 deletions mathics/builtin/list/eol.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from mathics.core.expression import Expression
from mathics.core.expression_predefined import MATHICS3_INFINITY
from mathics.core.list import ListExpression
from mathics.core.rules import Rule
from mathics.core.rules import RewriteRule, is_rule
from mathics.core.symbols import Atom, Symbol, SymbolNull, SymbolTrue
from mathics.core.systemsymbols import (
SymbolAppend,
Expand Down Expand Up @@ -216,7 +216,7 @@ def eval(self, items, pattern, levelspec, evaluation, options):
if isinstance(items, Atom):
return ListExpression()

if levelspec.has_form("Rule", 2):
if is_rule(levelspec):
if levelspec.elements[0].get_name() == "System`Heads":
heads = levelspec.elements[1] is SymbolTrue
levelspec = ListExpression(Integer1)
Expand All @@ -234,9 +234,9 @@ def eval(self, items, pattern, levelspec, evaluation, options):

results = []

if pattern.has_form("Rule", 2) or pattern.has_form("RuleDelayed", 2):
if is_rule(pattern):
match = Matcher(pattern.elements[0], evaluation).match
rule = Rule(pattern.elements[0], pattern.elements[1])
rule = RewriteRule(pattern.elements[0], pattern.elements[1])

def callback(level):
if match(level, evaluation):
Expand Down Expand Up @@ -1497,9 +1497,7 @@ def eval(self, expr, replacements, evaluation):
new_expr = expr.copy()
replacements = replacements.get_sequence()
for replacement in replacements:
if not replacement.has_form("Rule", 2) and not replacement.has_form( # noqa
"RuleDelayed", 2
):
if not is_rule(replacement):
evaluation.message("ReplacePart", "reps", ListExpression(*replacements))
return
position = replacement.elements[0]
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/patterns/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ def eval_list(
class Rule_(InfixOperator):
"""

<url>:WMA link:https://reference.wolfram.com/language/ref/Rule_.html</url>
<url>:WMA link:https://reference.wolfram.com/language/ref/Rule.html</url>

<dl>
<dt>'Rule'[$x$, $y$]
Expand Down
10 changes: 5 additions & 5 deletions mathics/core/assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from mathics.core.evaluation import Evaluation
from mathics.core.expression import Expression
from mathics.core.list import ListExpression
from mathics.core.rules import Rule
from mathics.core.rules import RewriteRule
from mathics.core.symbols import Atom, Symbol, SymbolList
from mathics.core.systemsymbols import (
SymbolAnd,
Expand All @@ -29,9 +29,9 @@
)


def build_rulopc(optval: BaseElement) -> Rule:
def build_rulopc(optval: BaseElement) -> RewriteRule:
"""Build an option value rule for optval"""
return Rule(
return RewriteRule(
Expression(
SymbolOptionValue,
Expression(SymbolPattern, Symbol("$cond$"), SymbolBlank),
Expand Down Expand Up @@ -157,7 +157,7 @@ def get_symbol_values(
return ListExpression(*elements)

for rule in definition.get_values_list(position):
if isinstance(rule, Rule):
if isinstance(rule, RewriteRule):
pattern = rule.pattern
if pattern.has_form("HoldPattern", 1):
expr_pattern = pattern.expr
Expand Down Expand Up @@ -350,7 +350,7 @@ def unroll_patterns(
# like
# rhs = Expression(Symbol("System`Replace"), Rule(*rulerepl))
# TODO: check if this is the correct behavior.
rhs, _ = rhs.do_apply_rules([Rule(*rulerepl)], evaluation)
rhs, _ = rhs.do_apply_rules([RewriteRule(*rulerepl)], evaluation)
name = lhs.get_head_name()
elif name == "System`HoldPattern":
lhs = lhs_elements[0]
Expand Down
20 changes: 8 additions & 12 deletions mathics/core/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
from mathics.core.parser.operators import OPERATOR_DATA
from mathics.core.parser.util import PyMathicsDefinitions, SystemDefinitions
from mathics.core.pattern import BasePattern, build_pattern_sort_key
from mathics.core.rules import BaseRule, FunctionApplyRule, Rule
from mathics.core.rules import BaseRule, FunctionApplyRule, RewriteRule
from mathics.core.symbols import (
Atom,
BaseElement,
Expand Down Expand Up @@ -300,7 +300,6 @@ def contribute(self, definitions: Definitions, is_pymodule=False):
function,
check_options,
attributes=pat_attr,
system=True,
)
)
for pattern_str, replace_str in self.rules.items():
Expand All @@ -309,11 +308,10 @@ def contribute(self, definitions: Definitions, is_pymodule=False):
replace_str = replace_str % {"name": name}
pat_attr = attributes if pattern.get_head_name() == name else None
rules.append(
Rule(
RewriteRule(
pattern,
parse_builtin_rule(replace_str),
attributes=pat_attr,
system=not is_pymodule,
)
)

Expand Down Expand Up @@ -361,7 +359,7 @@ def contextify_form_name(f):
formatvalues[form] = []
formatvalues[form].append(
FunctionApplyRule(
name, pattern, function, None, attributes=pat_attr, system=True
name, pattern, function, None, attributes=pat_attr
)
)
for pattern, replace in self.formats.items():
Expand All @@ -374,7 +372,7 @@ def contextify_form_name(f):
pattern = parse_builtin_rule(pattern)
replace = replace % {"name": name}
formatvalues[form].append(
Rule(pattern, parse_builtin_rule(replace), system=True)
RewriteRule(pattern, parse_builtin_rule(replace))
)

formatvalues.setdefault("_MakeBoxes", []).extend(box_rules)
Expand All @@ -385,19 +383,17 @@ def contextify_form_name(f):
if hasattr(self, "summary_text"):
self.messages["usage"] = self.summary_text
messages = [
Rule(
RewriteRule(
Expression(SymbolMessageName, Symbol(name), String(msg)),
String(value),
system=True,
)
for msg, value in self.messages.items()
]

messages.append(
Rule(
RewriteRule(
Expression(SymbolMessageName, Symbol(name), String("optx")),
String("`1` is not a supported option for `2`[]."),
system=True,
)
)

Expand All @@ -410,7 +406,7 @@ def contextify_form_name(f):
elif isinstance(spec, int):
pattern = Expression(SymbolDefault, Symbol(name), Integer(spec))
if pattern is not None:
defaults.append(Rule(pattern, value, system=True))
defaults.append(RewriteRule(pattern, value))

definition = Definition(
name=name,
Expand Down Expand Up @@ -1389,7 +1385,7 @@ def __init__(self, *args, **kwargs):
operator = ascii_operator_to_symbol.get(self.operator, self.__class__.__name__)

if self.default_formats:
if name not in ("Rule", "RuleDelayed"):
if name not in ("RewriteRule", "Rule", "RuleDelayed"):
formats = {
op_pattern: "HoldForm[Infix[{%s}, %s, %d, %s]]"
% (replace_items, operator, self.precedence, self.grouping)
Expand Down
4 changes: 2 additions & 2 deletions mathics/core/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from mathics.core.attributes import A_NO_ATTRIBUTES
from mathics.core.convert.expression import to_mathics_list
from mathics.core.element import BaseElement, fully_qualified_symbol_name
from mathics.core.rules import BaseRule, Rule
from mathics.core.rules import BaseRule, RewriteRule
from mathics.core.symbols import Atom, Symbol, strip_context
from mathics.core.util import canonic_filename
from mathics.settings import ROOT_DIR
Expand Down Expand Up @@ -771,7 +771,7 @@ def get_ownvalue(self, name: str) -> BaseElement:
def set_ownvalue(self, name: str, value) -> None:
"""Set an ownvalue for name"""
name = self.lookup_name(name)
self.add_rule(name, Rule(Symbol(name), value))
self.add_rule(name, RewriteRule(Symbol(name), value))
self.clear_cache(name)

def set_options(self, name: str, options) -> None:
Expand Down
Loading
Loading