Skip to content

Commit

Permalink
a bunch of fixes
Browse files Browse the repository at this point in the history
split mathics.core.attributes
  • Loading branch information
mmatera committed Nov 16, 2022
1 parent 2ffdf02 commit c8cbe50
Show file tree
Hide file tree
Showing 16 changed files with 1,033 additions and 752 deletions.
8 changes: 5 additions & 3 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ Internals
#. ``eval*`` methods in `Builtin` classes are considerer as synonyms of ``apply*`` methods.
#. Modularize and improve the way in which `Builtin` classes are selected to have an associated `Definition`.
#. `_SetOperator.assign_elementary` was renamed as `_SetOperator.assign`. All the special cases are not handled by the `_SetOperator.special_cases` dict.


#. ``get_sort_key`` now has a uniform interface on all the subclasses of ``KeyComparable``, accepting the parameter `pattern_sort`.
#. circular dependencies in ``mathics.core.definitions`` were reduced.
#. `to_boxes` now returns always a `FullForm` of the input Expression, instead of raising an exception.

Bugs
++++

# ``0`` with a given precision (like in ```0`3```) is now parsed as ``0``, an integer number.
#. ``RandomSample`` with one list argument now returns a random ordering of the list items. Previously it would return just one item.

#. 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.


Enhancements
++++++++++++
Expand Down
20 changes: 15 additions & 5 deletions mathics/builtin/assignments/assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@


from mathics.builtin.base import BinaryOperator, Builtin
from mathics.core.assignment import (
ASSIGNMENT_FUNCTION_MAP,
from mathics.core.eval.set import (
SET_EVAL_FUNCTION_MAP,
AssignmentException,
assign_store_rules_by_tag,
normalize_lhs,
Expand Down Expand Up @@ -47,7 +47,7 @@ def assign(self, lhs, rhs, evaluation, tags=None, upset=False):
try:
# Using a builtin name, find which assignment procedure to perform,
# and then call that function.
assignment_func = ASSIGNMENT_FUNCTION_MAP.get(lookup_name, None)
assignment_func = SET_EVAL_FUNCTION_MAP.get(lookup_name, None)
if assignment_func:
return assignment_func(self, lhs, rhs, evaluation, tags, upset)

Expand Down Expand Up @@ -170,11 +170,21 @@ class SetDelayed(Set):
'Condition' ('/;') can be used with 'SetDelayed' to make an
assignment that only holds if a condition is satisfied:
>> f[x_] := p[x] /; x>0
>> f[x_] := p[-x]/; x<-2
>> f[3]
= p[3]
>> f[-3]
= f[-3]
It also works if the condition is set in the LHS:
= p[3]
>> f[-1]
= f[-1]
Notice that the LHS is the same in both definitions, but the second
does not overwrite the first one.
To overwrite one of these definitions, we have to assign using the same condition:
>> f[x_] := Sin[x] /; x>0
>> f[3]
= Sin[3]
In a similar way, the condition can be set in the LHS:
>> F[x_, y_] /; x < y /; x>0 := x / y;
>> F[x_, y_] := y / x;
>> F[2, 3]
Expand Down
4 changes: 2 additions & 2 deletions mathics/builtin/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,15 +818,15 @@ def get_head_name(self):
def get_lookup_name(self):
return self.get_name()

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

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

def sameQ(self, expr) -> bool:
"""Mathics SameQ"""
return expr.sameQ(self)
return self.to_expression().sameQ(expr)

def do_format(self, evaluation, format):
return self
Expand Down
8 changes: 6 additions & 2 deletions mathics/builtin/box/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from mathics.core.symbols import Symbol, SymbolMakeBoxes
from mathics.core.systemsymbols import (
SymbolFractionBox,
SymbolFullForm,
SymbolRowBox,
SymbolSqrtBox,
SymbolStandardForm,
Expand Down Expand Up @@ -58,7 +59,7 @@ def to_boxes(x, evaluation: Evaluation, options={}) -> BoxElementMixin:
return x_boxed
if isinstance(x_boxed, Atom):
return to_boxes(x_boxed, evaluation, options)
raise Exception(x, "cannot be boxed.")
return to_boxes(Expression(SymbolFullForm, x), evaluation, options)


class BoxData(Builtin):
Expand Down Expand Up @@ -201,7 +202,10 @@ class RowBox(BoxExpression):
summary_text = "horizontal arrange of boxes"

def __repr__(self):
return "RowBox[List[" + self.items.__repr__() + "]]"
try:
return "RowBox[List[" + self.items.__repr__() + "]]"
except:
return "RowBox[List[{uninitialized}]]"

def apply_list(self, boxes, evaluation):
"""RowBox[boxes_List]"""
Expand Down
13 changes: 10 additions & 3 deletions mathics/builtin/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ def create_rules(rules_expr, expr, name, evaluation, extra_args=[]):
else:
result = []
for rule in rules:
if rule.get_head_name() not in ("System`Rule", "System`RuleDelayed"):
head_name = rule.get_head_name()
if head_name not in ("System`Rule", "System`RuleDelayed"):
evaluation.message(name, "reps", rule)
return None, True
elif len(rule.elements) != 2:
Expand All @@ -186,7 +187,13 @@ def create_rules(rules_expr, expr, name, evaluation, extra_args=[]):
)
return None, True
else:
result.append(Rule(rule.elements[0], rule.elements[1]))
result.append(
Rule(
rule.elements[0],
rule.elements[1],
delayed=(head_name == "System`RuleDelayed"),
)
)
return result, False


Expand Down Expand Up @@ -1690,7 +1697,7 @@ def __init__(self, rulelist, evaluation):
self._elements = None
self._head = SymbolDispatch

def get_sort_key(self) -> tuple:
def get_sort_key(self, pattern_sort=False) -> tuple:
return self.src.get_sort_key()

def get_atom_name(self):
Expand Down
Loading

0 comments on commit c8cbe50

Please sign in to comment.