Skip to content

Commit

Permalink
More suggestions and fixes (#595)
Browse files Browse the repository at this point in the history
* add `Remove`. Fix load definitions. Normalize assignment for MakeBoxes

* support strings
  • Loading branch information
mmatera authored and rocky committed Nov 6, 2022
1 parent 8713078 commit 0eaec5a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ New Builtins
#. ``Curl`` (2-D and 3-D vector forms only)
#. ``Kurtosis``
#. ``PauliMatrix``
#. ``Remove``
#. ``SetOptions``
#. ``SixJSymbol``
#. ``Skewness``
Expand Down
1 change: 1 addition & 0 deletions SYMBOLS_MANIFEST.txt
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,7 @@ System`RegularExpression
System`RegularPolygon
System`RegularPolygonBox
System`ReleaseHold
System`Remove
System`RemoveDiacritics
System`RenameDirectory
System`RenameFile
Expand Down
2 changes: 1 addition & 1 deletion mathics/autoload/forms/StandardForm.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
code. *)
Attributes[CommonRadicalBox] = HoldAll;
Attributes[RadBox] = HoldAll;
CommonRadicalBox[expr_, form_] = RadBox[MakeBoxes[expr, form], 3];
CommonRadicalBox[expr_, form_]:= RadBox[MakeBoxes[expr, form], 3];

(******************************************************************************************)
(* StandardForm Boxing Rules *)
Expand Down
30 changes: 30 additions & 0 deletions mathics/builtin/assignments/clear.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,36 @@ def do_clear(self, definition):
definition.defaultvalues = []


class Remove(Builtin):
"""
<dl>
<dt>'Remove[$x$]'
<dd>removes the definition associated to $x$.
</dl>
>> a := 2
>> Names["Global`a"]
= {a}
>> Remove[a]
>> Names["Global`a"]
= {}
"""

attributes = A_HOLD_ALL | A_LOCKED | A_PROTECTED
messages = {"ssym": "`1` is not a symbol."}
precedence = 670
summary_text = "remove the definition of a symbol"

def eval(self, symb, evaluation):
"""Remove[symb_]"""
if isinstance(symb, Symbol):
evaluation.definitions.reset_user_definition(symb.name)
elif isinstance(symb, String):
evaluation.definitions.reset_user_definition(symb.value)
else:
evaluation.message(self.get_name(), "ssym", symb)
return SymbolNull


class Unset(PostfixOperator):
"""
<dl>
Expand Down
9 changes: 6 additions & 3 deletions mathics/builtin/assignments/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,12 @@ def process_assign_makeboxes(self, lhs, rhs, evaluation, tags, upset):
# MakeBoxes[CubeRoot, StandardForm] := RadicalBox[3, StandardForm]
# rather than:
# MakeBoxes[CubeRoot, StandardForm] -> RadicalBox[3, StandardForm]
makeboxes_rule = Rule(lhs, rhs, system=True)
makeboxes_defs = evaluation.definitions.builtin["System`MakeBoxes"]
makeboxes_defs.add_rule(makeboxes_rule)

makeboxes_rule = Rule(lhs, rhs, system=False)
definitions = evaluation.definitions
definitions.add_rule("System`MakeBoxes", makeboxes_rule, "down")
# makeboxes_defs = evaluation.definitions.builtin["System`MakeBoxes"]
# makeboxes_defs.add_rule(makeboxes_rule)
return True


Expand Down
9 changes: 8 additions & 1 deletion mathics/core/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,14 @@ def __init__(
if name.startswith("Global`"):
raise ValueError("autoload defined %s." % name)

self.builtin.update(self.user)
# The idea here is that all the symbols loaded in
# autoload become converted in builtin.
# For some reason, if we do not do this here,
# `Export` and `Import` fails.
# TODO: investigate why.
for name in self.user:
self.builtin[name] = self.get_definition(name)

self.user = {}
self.clear_cache()

Expand Down

0 comments on commit 0eaec5a

Please sign in to comment.