Skip to content

Commit 39d73d0

Browse files
authored
More suggestions and fixes (#595)
* add `Remove`. Fix load definitions. Normalize assignment for MakeBoxes * support strings
1 parent d9b9edc commit 39d73d0

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
@@ -15,6 +15,7 @@ New Builtins
1515
#. ``Curl`` (2-D and 3-D vector forms only)
1616
#. ``Kurtosis``
1717
#. ``PauliMatrix``
18+
#. ``Remove``
1819
#. ``SixJSymbol``
1920
#. ``Skewness``
2021
#. ``ThreeJSymbol``

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
@@ -167,6 +167,36 @@ def do_clear(self, definition):
167167
definition.defaultvalues = []
168168

169169

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