Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes that makes CellsToTeX finally loads #624

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
9 changes: 6 additions & 3 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.. contents::

CHANGES
=======
*******

5.0.3dev0
---------
Expand Down Expand Up @@ -42,14 +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.
#. ``mathics.core.Pattern.create`` now catches the case when the input parameter is not an `Atom` or an `Expression`.


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
9 changes: 7 additions & 2 deletions mathics/builtin/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,15 +818,20 @@ 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)
if expr is self:
return True
# Otherwise, we need to convert
# self into an to expression
# to avoid an infinite loop...
return self.to_expression().sameQ(expr)

def do_format(self, evaluation, format):
return self
Expand Down
13 changes: 11 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,12 @@ 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 RowBox(
String("BoxError: <<"),
to_boxes(Expression(SymbolFullForm, x), evaluation, options),
String(">>"),
)


class BoxData(Builtin):
Expand Down Expand Up @@ -201,7 +207,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