Skip to content

Commit 0eaec5a

Browse files
mmaterarocky
authored andcommitted
More suggestions and fixes (#595)
* add `Remove`. Fix load definitions. Normalize assignment for MakeBoxes * support strings
1 parent 8713078 commit 0eaec5a

6 files changed

Lines changed: 47 additions & 5 deletions

File tree

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ New Builtins
1717
#. ``Curl`` (2-D and 3-D vector forms only)
1818
#. ``Kurtosis``
1919
#. ``PauliMatrix``
20+
#. ``Remove``
2021
#. ``SetOptions``
2122
#. ``SixJSymbol``
2223
#. ``Skewness``

SYMBOLS_MANIFEST.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,7 @@ System`RegularExpression
831831
System`RegularPolygon
832832
System`RegularPolygonBox
833833
System`ReleaseHold
834+
System`Remove
834835
System`RemoveDiacritics
835836
System`RenameDirectory
836837
System`RenameFile

mathics/autoload/forms/StandardForm.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
code. *)
1313
Attributes[CommonRadicalBox] = HoldAll;
1414
Attributes[RadBox] = HoldAll;
15-
CommonRadicalBox[expr_, form_] = RadBox[MakeBoxes[expr, form], 3];
15+
CommonRadicalBox[expr_, form_]:= RadBox[MakeBoxes[expr, form], 3];
1616

1717
(******************************************************************************************)
1818
(* StandardForm Boxing Rules *)

mathics/builtin/assignments/clear.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,36 @@ def do_clear(self, definition):
166166
definition.defaultvalues = []
167167

168168

169+
class Remove(Builtin):
170+
"""
171+
<dl>
172+
<dt>'Remove[$x$]'
173+
<dd>removes the definition associated to $x$.
174+
</dl>
175+
>> a := 2
176+
>> Names["Global`a"]
177+
= {a}
178+
>> Remove[a]
179+
>> Names["Global`a"]
180+
= {}
181+
"""
182+
183+
attributes = A_HOLD_ALL | A_LOCKED | A_PROTECTED
184+
messages = {"ssym": "`1` is not a symbol."}
185+
precedence = 670
186+
summary_text = "remove the definition of a symbol"
187+
188+
def eval(self, symb, evaluation):
189+
"""Remove[symb_]"""
190+
if isinstance(symb, Symbol):
191+
evaluation.definitions.reset_user_definition(symb.name)
192+
elif isinstance(symb, String):
193+
evaluation.definitions.reset_user_definition(symb.value)
194+
else:
195+
evaluation.message(self.get_name(), "ssym", symb)
196+
return SymbolNull
197+
198+
169199
class Unset(PostfixOperator):
170200
"""
171201
<dl>

mathics/builtin/assignments/internals.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,9 +567,12 @@ def process_assign_makeboxes(self, lhs, rhs, evaluation, tags, upset):
567567
# MakeBoxes[CubeRoot, StandardForm] := RadicalBox[3, StandardForm]
568568
# rather than:
569569
# MakeBoxes[CubeRoot, StandardForm] -> RadicalBox[3, StandardForm]
570-
makeboxes_rule = Rule(lhs, rhs, system=True)
571-
makeboxes_defs = evaluation.definitions.builtin["System`MakeBoxes"]
572-
makeboxes_defs.add_rule(makeboxes_rule)
570+
571+
makeboxes_rule = Rule(lhs, rhs, system=False)
572+
definitions = evaluation.definitions
573+
definitions.add_rule("System`MakeBoxes", makeboxes_rule, "down")
574+
# makeboxes_defs = evaluation.definitions.builtin["System`MakeBoxes"]
575+
# makeboxes_defs.add_rule(makeboxes_rule)
573576
return True
574577

575578

mathics/core/definitions.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,14 @@ def __init__(
169169
if name.startswith("Global`"):
170170
raise ValueError("autoload defined %s." % name)
171171

172-
self.builtin.update(self.user)
172+
# The idea here is that all the symbols loaded in
173+
# autoload become converted in builtin.
174+
# For some reason, if we do not do this here,
175+
# `Export` and `Import` fails.
176+
# TODO: investigate why.
177+
for name in self.user:
178+
self.builtin[name] = self.get_definition(name)
179+
173180
self.user = {}
174181
self.clear_cache()
175182

0 commit comments

Comments
 (0)