Skip to content

Commit cdd8e6a

Browse files
authored
Merge pull request #602 from Mathics3/fix_one_identity
improve `OneIdentity` builtin
2 parents ad6b87a + de5bbbe commit cdd8e6a

20 files changed

+567
-257
lines changed

mathics/builtin/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
PatternObject,
2121
)
2222

23+
from mathics.core.pattern import pattern_objects
24+
2325
from mathics.settings import ENABLE_FILES_MODULE
2426
from mathics.version import __version__ # noqa used in loading to check consistency.
2527

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

270-
pattern_objects = {}
271272
builtins_precedence = {}
272273

273274
new_builtins = _builtins

mathics/builtin/assignments/clear.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,20 @@
2222
Atom,
2323
Symbol,
2424
SymbolNull,
25-
system_symbols,
25+
symbol_set,
2626
)
2727

2828
from mathics.core.systemsymbols import (
2929
SymbolContext,
3030
SymbolContextPath,
31+
SymbolDownValues,
3132
SymbolFailed,
33+
SymbolMessages,
34+
SymbolNValues,
3235
SymbolOptions,
36+
SymbolOwnValues,
37+
SymbolSubValues,
38+
SymbolUpValues,
3339
)
3440

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

322328

323-
SYSTEM_SYMBOL_VALUES = system_symbols(
324-
"OwnValues",
325-
"DownValues",
326-
"SubValues",
327-
"UpValues",
328-
"NValues",
329-
"Options",
330-
"Messages",
329+
SYSTEM_SYMBOL_VALUES = symbol_set(
330+
SymbolDownValues,
331+
SymbolMessages,
332+
SymbolNValues,
333+
SymbolOptions,
334+
SymbolOwnValues,
335+
SymbolSubValues,
336+
SymbolUpValues,
331337
)

mathics/builtin/assignments/internals.py

-10
Original file line numberDiff line numberDiff line change
@@ -720,12 +720,7 @@ def process_rhs_conditions(lhs, rhs, condition, evaluation):
720720

721721

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

746741

747742
def process_tags_and_upset_allow_custom(tags, upset, self, lhs, evaluation):
748-
# TODO: the following provides a hacky fix for 1259. I know @rocky loves
749-
# this kind of things, but otherwise we need to work on rebuild the pattern
750-
# matching mechanism...
751743
name = lhs.get_head_name()
752744
focus = lhs
753-
flag_ioi, evaluation.ignore_oneidentity = evaluation.ignore_oneidentity, True
754745
focus = focus.evaluate_elements(evaluation)
755-
evaluation.ignore_oneidentity = flag_ioi
756746
if tags is None and not upset:
757747
name = focus.get_lookup_name()
758748
if not name:

mathics/builtin/attributes.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -460,15 +460,24 @@ class OneIdentity(Predefined):
460460
461461
<dl>
462462
<dt>'OneIdentity'
463-
<dd>is an attribute specifying that '$f$[$x$]' should be treated \
464-
as equivalent to $x$ in pattern matching.
463+
<dd>is an attribute assigned to a symbol, say $f$, indicating that '$f$[$x$]', $f$[$f$[$x$]], etc. are all \
464+
equivalent to $x$ in pattern matching.
465465
</dl>
466466
467-
'OneIdentity' affects pattern matching:
467+
>> a /. f[x_:0, u_] -> {u}
468+
= a
469+
470+
Here is how 'OneIdentity' changes the pattern matched above :
471+
468472
>> SetAttributes[f, OneIdentity]
469-
>> a /. f[args___] -> {args}
473+
>> a /. f[x_:0, u_] -> {u}
470474
= {a}
471-
It does not affect evaluation:
475+
476+
However, without a default argument, the pattern does not match:
477+
>> a /. f[u_] -> {u}
478+
= a
479+
480+
'OneIdentity' does not change evaluation:
472481
>> f[a]
473482
= f[a]
474483
"""

mathics/builtin/files_io/files.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ class FilePrint(Builtin):
296296
}
297297

298298
def apply(self, path, evaluation, options):
299-
"FilePrint[path_ OptionsPattern[FilePrint]]"
299+
"FilePrint[path_, OptionsPattern[FilePrint]]"
300300
pypath = path.to_python()
301301
if not (
302302
isinstance(pypath, str)

mathics/builtin/graphics.py

+27-27
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,23 @@
4747
from mathics.core.list import ListExpression
4848
from mathics.core.symbols import (
4949
Symbol,
50-
system_symbols,
50+
symbol_set,
5151
system_symbols_dict,
5252
SymbolList,
5353
SymbolNull,
5454
)
5555
from mathics.core.systemsymbols import (
56+
SymbolEdgeForm,
57+
SymbolFaceForm,
5658
SymbolMakeBoxes,
59+
SymbolRule,
5760
)
5861

5962
from mathics.core.formatter import lookup_method
6063

6164
from mathics.core.attributes import A_PROTECTED, A_READ_PROTECTED
6265

6366

64-
SymbolEdgeForm = Symbol("System`EdgeForm")
65-
SymbolFaceForm = Symbol("System`FaceForm")
66-
6767
GRAPHICS_OPTIONS = {
6868
"AspectRatio": "Automatic",
6969
"Axes": "False",
@@ -1427,26 +1427,26 @@ class Tiny(Builtin):
14271427

14281428

14291429
element_heads = frozenset(
1430-
system_symbols(
1431-
"Arrow",
1432-
"BezierCurve",
1433-
"Circle",
1434-
"Cone",
1435-
"Cuboid",
1436-
"Cylinder",
1437-
"Disk",
1438-
"FilledCurve",
1439-
"Inset",
1440-
"Line",
1441-
"Point",
1442-
"Polygon",
1443-
"Rectangle",
1444-
"RegularPolygon",
1445-
"Sphere",
1446-
"Style",
1447-
"Text",
1448-
"Tube",
1449-
"UniformPolyhedron",
1430+
symbol_set(
1431+
Symbol("System`Arrow"),
1432+
Symbol("System`BezierCurve"),
1433+
Symbol("System`Circle"),
1434+
Symbol("System`Cone"),
1435+
Symbol("System`Cuboid"),
1436+
Symbol("System`Cylinder"),
1437+
Symbol("System`Disk"),
1438+
Symbol("System`FilledCurve"),
1439+
Symbol("System`Inset"),
1440+
Symbol("System`Line"),
1441+
Symbol("System`Point"),
1442+
Symbol("System`Polygon"),
1443+
Symbol("System`Rectangle"),
1444+
Symbol("System`RegularPolygon"),
1445+
Symbol("System`Sphere"),
1446+
Symbol("System`Style"),
1447+
Symbol("System`Text"),
1448+
Symbol("System`Tube"),
1449+
Symbol("System`UniformPolyhedron"),
14501450
)
14511451
)
14521452

@@ -1477,7 +1477,7 @@ class Tiny(Builtin):
14771477
style_heads = frozenset(styles.keys())
14781478

14791479
style_and_form_heads = frozenset(
1480-
style_heads.union(system_symbols("System`EdgeForm", "System`FaceForm"))
1480+
style_heads.union(symbol_set(SymbolEdgeForm, SymbolFaceForm))
14811481
)
14821482

14831483
GLOBALS.update(
@@ -1497,8 +1497,8 @@ class Tiny(Builtin):
14971497
GLOBALS.update(styles)
14981498

14991499
GRAPHICS_SYMBOLS = {
1500-
Symbol("System`List"),
1501-
Symbol("System`Rule"),
1500+
SymbolList,
1501+
SymbolRule,
15021502
Symbol("System`VertexColors"),
15031503
*element_heads,
15041504
*[Symbol(element.name + "Box") for element in element_heads],

mathics/builtin/options.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ def eval(self, f, i, evaluation):
7979
i = [index.get_int_value() for index in i]
8080
for index in i:
8181
if index is None or index < 1:
82-
evaluation.message(SymbolDefault, "intp")
82+
evaluation.message(SymbolDefault.name, "intp")
8383
return
8484
name = f.get_name()
8585
if not name:
86-
evaluation.message(SymbolDefault, "sym", f, 1)
86+
evaluation.message(SymbolDefault.name, "sym", f, 1)
8787
return
8888
result = get_default_value(name, evaluation, *i)
8989
return result

mathics/core/atoms.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
Symbol,
2525
SymbolNull,
2626
SymbolTrue,
27-
system_symbols,
27+
symbol_set,
2828
)
29-
from mathics.core.systemsymbols import SymbolInfinity
29+
from mathics.core.systemsymbols import SymbolInfinity, SymbolInputForm, SymbolFullForm
3030

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

38-
SYSTEM_SYMBOLS_INPUT_OR_FULL_FORM = system_symbols("InputForm", "FullForm")
38+
SYSTEM_SYMBOLS_INPUT_OR_FULL_FORM = symbol_set(SymbolInputForm, SymbolFullForm)
3939

4040

4141
class Number(Atom, ImmutableValueMixin, NumericOperators):

mathics/core/evaluation.py

-3
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,6 @@ def __init__(
262262
# status of last evaluate
263263
self.exc_result = self.SymbolNull
264264
self.last_eval = None
265-
# Necesary to handle OneIdentity on
266-
# lhs in assignment
267-
self.ignore_oneidentity = False
268265
# Used in ``mathics.builtin.numbers.constants.get_constant`` and
269266
# ``mathics.builtin.numeric.N``.
270267
self._preferred_n_method = []

mathics/core/evaluators.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
)
2222
from mathics.core.convert.sympy import from_sympy
2323
from mathics.core.definitions import PyMathicsLoadException
24+
from mathics.core.element import BaseElement
2425
from mathics.core.evaluation import Evaluation
2526
from mathics.core.expression import Expression
2627
from mathics.core.number import PrecisionValueError, get_precision
27-
from mathics.core.symbols import Atom, BaseElement
28+
from mathics.core.symbols import Atom
2829
from mathics.core.systemsymbols import SymbolMachinePrecision, SymbolN
2930

3031

mathics/core/expression.py

+19-11
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@
3737
Monomial,
3838
NumericOperators,
3939
Symbol,
40+
SymbolAbs,
41+
SymbolDivide,
4042
SymbolList,
4143
SymbolN,
44+
SymbolPlus,
4245
SymbolTimes,
4346
SymbolTrue,
44-
system_symbols,
47+
symbol_set,
4548
)
4649
from mathics.core.systemsymbols import (
4750
SymbolAborted,
@@ -50,9 +53,14 @@
5053
SymbolCondition,
5154
SymbolDirectedInfinity,
5255
SymbolFunction,
56+
SymbolMinus,
5357
SymbolPattern,
58+
SymbolPower,
5459
SymbolSequence,
60+
SymbolSin,
5561
SymbolSlot,
62+
SymbolSqrt,
63+
SymbolSubtract,
5664
SymbolUnevaluated,
5765
)
5866

@@ -70,16 +78,16 @@
7078
SymbolVerbatim = Symbol("Verbatim")
7179

7280

73-
symbols_arithmetic_operations = system_symbols(
74-
"Sqrt",
75-
"Times",
76-
"Plus",
77-
"Subtract",
78-
"Minus",
79-
"Power",
80-
"Abs",
81-
"Divide",
82-
"Sin",
81+
symbols_arithmetic_operations = symbol_set(
82+
SymbolAbs,
83+
SymbolDivide,
84+
SymbolMinus,
85+
SymbolPlus,
86+
SymbolPower,
87+
SymbolSin,
88+
SymbolSqrt,
89+
SymbolSubtract,
90+
SymbolTimes,
8391
)
8492

8593

mathics/core/list.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def __init__(
4141
# call_frame = inspect.getouterframes(curframe, 2)
4242
# print("caller name:", call_frame[1][3])
4343

44-
# from mathics.core.symbols import BaseElement
44+
# from mathics.core.element import BaseElement
4545
# for element in elements:
4646
# if not isinstance(element, BaseElement):
4747
# from trepan.api import debug; debug()

0 commit comments

Comments
 (0)