Skip to content

Commit d348304

Browse files
committed
An example of defining MakeBoxes in Mathics code
We hack assigment for MakeBoxes, but ultimately this is wrong. There seems to be confusion in MakeBoxes internals with respect to Rules and delayed assignment.
1 parent acf3c0d commit d348304

4 files changed

Lines changed: 41 additions & 3 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
(* This implements StandardForm boxing rules in Mathics *)
2+
3+
4+
Begin["System`"]
5+
6+
(******************************************************************************************)
7+
(* Common Boxing routines that are used by many forms. FIXME: place this in another file. *)
8+
(******************************************************************************************)
9+
10+
(* Change RadBox to RadicalBox. We use RadBox to make it clear that
11+
the below code was a read-in from a file and not some pre-existing
12+
code. *)
13+
RadicalBox[expr_, form_] = RadBox[MakeBoxes[expr, form], 3];
14+
15+
(******************************************************************************************)
16+
(* StandardForm Boxing Rules *)
17+
(******************************************************************************************)
18+
19+
MakeBoxes[CubeRoot[expr_], StandardForm] := RadicalBox[expr_, form_];
20+
(*All the other StandardForm boxing routines... *)
21+
End[]

mathics/builtin/arithfns/basic.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,6 @@ class CubeRoot(Builtin):
112112
rules = {
113113
"CubeRoot[n_?NumberQ]": "If[n > 0, Power[n, Divide[1, 3]], Times[-1, Power[Times[-1, n], Divide[1, 3]]]]",
114114
"CubeRoot[n_]": "Power[n, Divide[1, 3]]",
115-
"MakeBoxes[CubeRoot[x_], f:StandardForm|TraditionalForm]": (
116-
"RadicalBox[MakeBoxes[x, f], 3]"
117-
),
118115
}
119116

120117
summary_text = "cubed root"

mathics/builtin/assignments/internals.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,5 +755,21 @@ def assign(self, lhs, rhs, evaluation):
755755
return False
756756
indices = lhs.elements[1:]
757757
return walk_parts([rule.replace], indices, evaluation, rhs)
758+
759+
# FIXME: the below is a big hack.
760+
# Currently MakeBoxes boxing is implemented as a bunch of rules.
761+
# See mathics.builtin.base contribute().
762+
# I think we want to change this so it works like normal SetDelayed
763+
# That is:
764+
# MakeBoxes[CubeRoot, StandardForm] := RadicalBox[3, StandardForm]
765+
# rather than:
766+
# MakeBoxes[CubeRoot, StandardForm] -> RadicalBox[3, StandardForm]
767+
elif lhs.get_head_name() == "System`MakeBoxes":
768+
makeboxes_rule = Rule(lhs, rhs, system=True)
769+
makeboxes_defs = defs.builtin["System`MakeBoxes"]
770+
makeboxes_defs.add_rule(makeboxes_rule)
771+
# FIXME: what should be the result?
772+
return makeboxes_rule
773+
758774
else:
759775
return self.assign_elementary(lhs, rhs, evaluation)

mathics/builtin/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,18 @@ def check_options(options_to_check, evaluation):
284284
BuiltinRule(name, pattern, function, check_options, system=True)
285285
)
286286
for pattern, replace in self.rules.items():
287+
# FIXME: sometimes pattern is a string and sometimes a BaseElement?
288+
# This seems wrong.
287289
if not isinstance(pattern, BaseElement):
290+
assert pattern
288291
pattern = pattern % {"name": name}
289292
pattern = parse_builtin_rule(pattern, definition_class)
290293
replace = replace % {"name": name}
291294
# FIXME: Should system=True be system=not is_pymodule ?
292295
rules.append(Rule(pattern, parse_builtin_rule(replace), system=True))
293296

294297
box_rules = []
298+
# FIXME: Why a special case for System`MakeBoxes? Remove this
295299
if name != "System`MakeBoxes":
296300
new_rules = []
297301
for rule in rules:

0 commit comments

Comments
 (0)