Skip to content

Commit c8cbe50

Browse files
committed
a bunch of fixes
split mathics.core.attributes
1 parent 2ffdf02 commit c8cbe50

16 files changed

+1033
-752
lines changed

CHANGES.rst

+5-3
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,17 @@ Internals
4242
#. ``eval*`` methods in `Builtin` classes are considerer as synonyms of ``apply*`` methods.
4343
#. Modularize and improve the way in which `Builtin` classes are selected to have an associated `Definition`.
4444
#. `_SetOperator.assign_elementary` was renamed as `_SetOperator.assign`. All the special cases are not handled by the `_SetOperator.special_cases` dict.
45-
46-
45+
#. ``get_sort_key`` now has a uniform interface on all the subclasses of ``KeyComparable``, accepting the parameter `pattern_sort`.
46+
#. circular dependencies in ``mathics.core.definitions`` were reduced.
47+
#. `to_boxes` now returns always a `FullForm` of the input Expression, instead of raising an exception.
4748

4849
Bugs
4950
++++
5051

5152
# ``0`` with a given precision (like in ```0`3```) is now parsed as ``0``, an integer number.
5253
#. ``RandomSample`` with one list argument now returns a random ordering of the list items. Previously it would return just one item.
53-
54+
#. Rules of the form ``pat->Condition[expr, cond]`` are handled as in WL. The same works for nested `Condition` expressions. Also, comparisons among these rules was improved.
55+
5456

5557
Enhancements
5658
++++++++++++

mathics/builtin/assignments/assignment.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66

77
from mathics.builtin.base import BinaryOperator, Builtin
8-
from mathics.core.assignment import (
9-
ASSIGNMENT_FUNCTION_MAP,
8+
from mathics.core.eval.set import (
9+
SET_EVAL_FUNCTION_MAP,
1010
AssignmentException,
1111
assign_store_rules_by_tag,
1212
normalize_lhs,
@@ -47,7 +47,7 @@ def assign(self, lhs, rhs, evaluation, tags=None, upset=False):
4747
try:
4848
# Using a builtin name, find which assignment procedure to perform,
4949
# and then call that function.
50-
assignment_func = ASSIGNMENT_FUNCTION_MAP.get(lookup_name, None)
50+
assignment_func = SET_EVAL_FUNCTION_MAP.get(lookup_name, None)
5151
if assignment_func:
5252
return assignment_func(self, lhs, rhs, evaluation, tags, upset)
5353

@@ -170,11 +170,21 @@ class SetDelayed(Set):
170170
'Condition' ('/;') can be used with 'SetDelayed' to make an
171171
assignment that only holds if a condition is satisfied:
172172
>> f[x_] := p[x] /; x>0
173+
>> f[x_] := p[-x]/; x<-2
173174
>> f[3]
174175
= p[3]
175176
>> f[-3]
176-
= f[-3]
177-
It also works if the condition is set in the LHS:
177+
= p[3]
178+
>> f[-1]
179+
= f[-1]
180+
Notice that the LHS is the same in both definitions, but the second
181+
does not overwrite the first one.
182+
183+
To overwrite one of these definitions, we have to assign using the same condition:
184+
>> f[x_] := Sin[x] /; x>0
185+
>> f[3]
186+
= Sin[3]
187+
In a similar way, the condition can be set in the LHS:
178188
>> F[x_, y_] /; x < y /; x>0 := x / y;
179189
>> F[x_, y_] := y / x;
180190
>> F[2, 3]

mathics/builtin/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -818,15 +818,15 @@ def get_head_name(self):
818818
def get_lookup_name(self):
819819
return self.get_name()
820820

821-
def get_sort_key(self) -> tuple:
821+
def get_sort_key(self, pattern_sort=False) -> tuple:
822822
return self.to_expression().get_sort_key()
823823

824824
def get_string_value(self):
825825
return "-@" + self.get_head_name() + "@-"
826826

827827
def sameQ(self, expr) -> bool:
828828
"""Mathics SameQ"""
829-
return expr.sameQ(self)
829+
return self.to_expression().sameQ(expr)
830830

831831
def do_format(self, evaluation, format):
832832
return self

mathics/builtin/box/layout.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from mathics.core.symbols import Symbol, SymbolMakeBoxes
2424
from mathics.core.systemsymbols import (
2525
SymbolFractionBox,
26+
SymbolFullForm,
2627
SymbolRowBox,
2728
SymbolSqrtBox,
2829
SymbolStandardForm,
@@ -58,7 +59,7 @@ def to_boxes(x, evaluation: Evaluation, options={}) -> BoxElementMixin:
5859
return x_boxed
5960
if isinstance(x_boxed, Atom):
6061
return to_boxes(x_boxed, evaluation, options)
61-
raise Exception(x, "cannot be boxed.")
62+
return to_boxes(Expression(SymbolFullForm, x), evaluation, options)
6263

6364

6465
class BoxData(Builtin):
@@ -201,7 +202,10 @@ class RowBox(BoxExpression):
201202
summary_text = "horizontal arrange of boxes"
202203

203204
def __repr__(self):
204-
return "RowBox[List[" + self.items.__repr__() + "]]"
205+
try:
206+
return "RowBox[List[" + self.items.__repr__() + "]]"
207+
except:
208+
return "RowBox[List[{uninitialized}]]"
205209

206210
def apply_list(self, boxes, evaluation):
207211
"""RowBox[boxes_List]"""

mathics/builtin/patterns.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ def create_rules(rules_expr, expr, name, evaluation, extra_args=[]):
172172
else:
173173
result = []
174174
for rule in rules:
175-
if rule.get_head_name() not in ("System`Rule", "System`RuleDelayed"):
175+
head_name = rule.get_head_name()
176+
if head_name not in ("System`Rule", "System`RuleDelayed"):
176177
evaluation.message(name, "reps", rule)
177178
return None, True
178179
elif len(rule.elements) != 2:
@@ -186,7 +187,13 @@ def create_rules(rules_expr, expr, name, evaluation, extra_args=[]):
186187
)
187188
return None, True
188189
else:
189-
result.append(Rule(rule.elements[0], rule.elements[1]))
190+
result.append(
191+
Rule(
192+
rule.elements[0],
193+
rule.elements[1],
194+
delayed=(head_name == "System`RuleDelayed"),
195+
)
196+
)
190197
return result, False
191198

192199

@@ -1690,7 +1697,7 @@ def __init__(self, rulelist, evaluation):
16901697
self._elements = None
16911698
self._head = SymbolDispatch
16921699

1693-
def get_sort_key(self) -> tuple:
1700+
def get_sort_key(self, pattern_sort=False) -> tuple:
16941701
return self.src.get_sort_key()
16951702

16961703
def get_atom_name(self):

0 commit comments

Comments
 (0)