Skip to content

Commit

Permalink
Merge pull request #602 from Mathics3/fix_one_identity
Browse files Browse the repository at this point in the history
improve `OneIdentity` builtin
  • Loading branch information
rocky authored Nov 13, 2022
2 parents ad6b87a + de5bbbe commit cdd8e6a
Show file tree
Hide file tree
Showing 20 changed files with 567 additions and 257 deletions.
3 changes: 2 additions & 1 deletion mathics/builtin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
PatternObject,
)

from mathics.core.pattern import pattern_objects

from mathics.settings import ENABLE_FILES_MODULE
from mathics.version import __version__ # noqa used in loading to check consistency.

Expand Down Expand Up @@ -267,7 +269,6 @@ def sanity_check(cls, module):
mathics_to_python = {} # here we have: name -> string
sympy_to_mathics = {}

pattern_objects = {}
builtins_precedence = {}

new_builtins = _builtins
Expand Down
24 changes: 15 additions & 9 deletions mathics/builtin/assignments/clear.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@
Atom,
Symbol,
SymbolNull,
system_symbols,
symbol_set,
)

from mathics.core.systemsymbols import (
SymbolContext,
SymbolContextPath,
SymbolDownValues,
SymbolFailed,
SymbolMessages,
SymbolNValues,
SymbolOptions,
SymbolOwnValues,
SymbolSubValues,
SymbolUpValues,
)

from mathics.core.atoms import String
Expand Down Expand Up @@ -320,12 +326,12 @@ def apply(self, expr, evaluation):
return SymbolNull


SYSTEM_SYMBOL_VALUES = system_symbols(
"OwnValues",
"DownValues",
"SubValues",
"UpValues",
"NValues",
"Options",
"Messages",
SYSTEM_SYMBOL_VALUES = symbol_set(
SymbolDownValues,
SymbolMessages,
SymbolNValues,
SymbolOptions,
SymbolOwnValues,
SymbolSubValues,
SymbolUpValues,
)
10 changes: 0 additions & 10 deletions mathics/builtin/assignments/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,12 +720,7 @@ def process_rhs_conditions(lhs, rhs, condition, evaluation):


def process_tags_and_upset_dont_allow_custom(tags, upset, self, lhs, focus, evaluation):
# TODO: the following provides a hacky fix for 1259. I know @rocky loves
# this kind of things, but otherwise we need to work on rebuild the pattern
# matching mechanism...
flag_ioi, evaluation.ignore_oneidentity = evaluation.ignore_oneidentity, True
focus = focus.evaluate_elements(evaluation)
evaluation.ignore_oneidentity = flag_ioi
name = lhs.get_head_name()
if tags is None and not upset:
name = focus.get_lookup_name()
Expand All @@ -745,14 +740,9 @@ def process_tags_and_upset_dont_allow_custom(tags, upset, self, lhs, focus, eval


def process_tags_and_upset_allow_custom(tags, upset, self, lhs, evaluation):
# TODO: the following provides a hacky fix for 1259. I know @rocky loves
# this kind of things, but otherwise we need to work on rebuild the pattern
# matching mechanism...
name = lhs.get_head_name()
focus = lhs
flag_ioi, evaluation.ignore_oneidentity = evaluation.ignore_oneidentity, True
focus = focus.evaluate_elements(evaluation)
evaluation.ignore_oneidentity = flag_ioi
if tags is None and not upset:
name = focus.get_lookup_name()
if not name:
Expand Down
19 changes: 14 additions & 5 deletions mathics/builtin/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,15 +460,24 @@ class OneIdentity(Predefined):
<dl>
<dt>'OneIdentity'
<dd>is an attribute specifying that '$f$[$x$]' should be treated \
as equivalent to $x$ in pattern matching.
<dd>is an attribute assigned to a symbol, say $f$, indicating that '$f$[$x$]', $f$[$f$[$x$]], etc. are all \
equivalent to $x$ in pattern matching.
</dl>
'OneIdentity' affects pattern matching:
>> a /. f[x_:0, u_] -> {u}
= a
Here is how 'OneIdentity' changes the pattern matched above :
>> SetAttributes[f, OneIdentity]
>> a /. f[args___] -> {args}
>> a /. f[x_:0, u_] -> {u}
= {a}
It does not affect evaluation:
However, without a default argument, the pattern does not match:
>> a /. f[u_] -> {u}
= a
'OneIdentity' does not change evaluation:
>> f[a]
= f[a]
"""
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/files_io/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ class FilePrint(Builtin):
}

def apply(self, path, evaluation, options):
"FilePrint[path_ OptionsPattern[FilePrint]]"
"FilePrint[path_, OptionsPattern[FilePrint]]"
pypath = path.to_python()
if not (
isinstance(pypath, str)
Expand Down
54 changes: 27 additions & 27 deletions mathics/builtin/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,23 @@
from mathics.core.list import ListExpression
from mathics.core.symbols import (
Symbol,
system_symbols,
symbol_set,
system_symbols_dict,
SymbolList,
SymbolNull,
)
from mathics.core.systemsymbols import (
SymbolEdgeForm,
SymbolFaceForm,
SymbolMakeBoxes,
SymbolRule,
)

from mathics.core.formatter import lookup_method

from mathics.core.attributes import A_PROTECTED, A_READ_PROTECTED


SymbolEdgeForm = Symbol("System`EdgeForm")
SymbolFaceForm = Symbol("System`FaceForm")

GRAPHICS_OPTIONS = {
"AspectRatio": "Automatic",
"Axes": "False",
Expand Down Expand Up @@ -1427,26 +1427,26 @@ class Tiny(Builtin):


element_heads = frozenset(
system_symbols(
"Arrow",
"BezierCurve",
"Circle",
"Cone",
"Cuboid",
"Cylinder",
"Disk",
"FilledCurve",
"Inset",
"Line",
"Point",
"Polygon",
"Rectangle",
"RegularPolygon",
"Sphere",
"Style",
"Text",
"Tube",
"UniformPolyhedron",
symbol_set(
Symbol("System`Arrow"),
Symbol("System`BezierCurve"),
Symbol("System`Circle"),
Symbol("System`Cone"),
Symbol("System`Cuboid"),
Symbol("System`Cylinder"),
Symbol("System`Disk"),
Symbol("System`FilledCurve"),
Symbol("System`Inset"),
Symbol("System`Line"),
Symbol("System`Point"),
Symbol("System`Polygon"),
Symbol("System`Rectangle"),
Symbol("System`RegularPolygon"),
Symbol("System`Sphere"),
Symbol("System`Style"),
Symbol("System`Text"),
Symbol("System`Tube"),
Symbol("System`UniformPolyhedron"),
)
)

Expand Down Expand Up @@ -1477,7 +1477,7 @@ class Tiny(Builtin):
style_heads = frozenset(styles.keys())

style_and_form_heads = frozenset(
style_heads.union(system_symbols("System`EdgeForm", "System`FaceForm"))
style_heads.union(symbol_set(SymbolEdgeForm, SymbolFaceForm))
)

GLOBALS.update(
Expand All @@ -1497,8 +1497,8 @@ class Tiny(Builtin):
GLOBALS.update(styles)

GRAPHICS_SYMBOLS = {
Symbol("System`List"),
Symbol("System`Rule"),
SymbolList,
SymbolRule,
Symbol("System`VertexColors"),
*element_heads,
*[Symbol(element.name + "Box") for element in element_heads],
Expand Down
4 changes: 2 additions & 2 deletions mathics/builtin/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ def eval(self, f, i, evaluation):
i = [index.get_int_value() for index in i]
for index in i:
if index is None or index < 1:
evaluation.message(SymbolDefault, "intp")
evaluation.message(SymbolDefault.name, "intp")
return
name = f.get_name()
if not name:
evaluation.message(SymbolDefault, "sym", f, 1)
evaluation.message(SymbolDefault.name, "sym", f, 1)
return
result = get_default_value(name, evaluation, *i)
return result
Expand Down
6 changes: 3 additions & 3 deletions mathics/core/atoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
Symbol,
SymbolNull,
SymbolTrue,
system_symbols,
symbol_set,
)
from mathics.core.systemsymbols import SymbolInfinity
from mathics.core.systemsymbols import SymbolInfinity, SymbolInputForm, SymbolFullForm

# Imperical number that seems to work.
# We have to be able to match mpmath values with sympy values
Expand All @@ -35,7 +35,7 @@
SymbolI = Symbol("I")
SymbolString = Symbol("String")

SYSTEM_SYMBOLS_INPUT_OR_FULL_FORM = system_symbols("InputForm", "FullForm")
SYSTEM_SYMBOLS_INPUT_OR_FULL_FORM = symbol_set(SymbolInputForm, SymbolFullForm)


class Number(Atom, ImmutableValueMixin, NumericOperators):
Expand Down
3 changes: 0 additions & 3 deletions mathics/core/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,6 @@ def __init__(
# status of last evaluate
self.exc_result = self.SymbolNull
self.last_eval = None
# Necesary to handle OneIdentity on
# lhs in assignment
self.ignore_oneidentity = False
# Used in ``mathics.builtin.numbers.constants.get_constant`` and
# ``mathics.builtin.numeric.N``.
self._preferred_n_method = []
Expand Down
3 changes: 2 additions & 1 deletion mathics/core/evaluators.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
)
from mathics.core.convert.sympy import from_sympy
from mathics.core.definitions import PyMathicsLoadException
from mathics.core.element import BaseElement
from mathics.core.evaluation import Evaluation
from mathics.core.expression import Expression
from mathics.core.number import PrecisionValueError, get_precision
from mathics.core.symbols import Atom, BaseElement
from mathics.core.symbols import Atom
from mathics.core.systemsymbols import SymbolMachinePrecision, SymbolN


Expand Down
30 changes: 19 additions & 11 deletions mathics/core/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@
Monomial,
NumericOperators,
Symbol,
SymbolAbs,
SymbolDivide,
SymbolList,
SymbolN,
SymbolPlus,
SymbolTimes,
SymbolTrue,
system_symbols,
symbol_set,
)
from mathics.core.systemsymbols import (
SymbolAborted,
Expand All @@ -50,9 +53,14 @@
SymbolCondition,
SymbolDirectedInfinity,
SymbolFunction,
SymbolMinus,
SymbolPattern,
SymbolPower,
SymbolSequence,
SymbolSin,
SymbolSlot,
SymbolSqrt,
SymbolSubtract,
SymbolUnevaluated,
)

Expand All @@ -70,16 +78,16 @@
SymbolVerbatim = Symbol("Verbatim")


symbols_arithmetic_operations = system_symbols(
"Sqrt",
"Times",
"Plus",
"Subtract",
"Minus",
"Power",
"Abs",
"Divide",
"Sin",
symbols_arithmetic_operations = symbol_set(
SymbolAbs,
SymbolDivide,
SymbolMinus,
SymbolPlus,
SymbolPower,
SymbolSin,
SymbolSqrt,
SymbolSubtract,
SymbolTimes,
)


Expand Down
2 changes: 1 addition & 1 deletion mathics/core/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(
# call_frame = inspect.getouterframes(curframe, 2)
# print("caller name:", call_frame[1][3])

# from mathics.core.symbols import BaseElement
# from mathics.core.element import BaseElement
# for element in elements:
# if not isinstance(element, BaseElement):
# from trepan.api import debug; debug()
Expand Down
Loading

0 comments on commit cdd8e6a

Please sign in to comment.