Replies: 2 comments 1 reply
-
I have been going over the information here and thinking about this over the last couple of days. Even though these thoughts appear after other discussions such as in PRs, those discussions have been motivated by trying to wrap my head around what has been proposed here. I think what we should do try to narrow the initial work to just fixing up MakeBoxes. Let's defer revising any of the forms like Fullform or adding Character-based 2D rendering for later. I think there is enough to do in just getting MakeBoxes, in the areas it currently handles, to conform better to WMA. And I suspect making even small changes to MakeBoxes is going to drastically change output that a lot of our tests are going to fail. If this is the case, then to mitigate this we can create a new makeboxes routine NMakeBoxes, and have the two work side by side. There is an art to trying to simplify and narrow a big problem. In particular one needs to make sure that the simplification we goes with the natural grain or groove of the problem. The analogy I've given in the past is in solving a Rubik's cube by its underlying subgroups instead of the visual proximity of a corner piece and its neighboring 3 edge pieces. So the specific details given here should help ensure that we are not going against the natural grain in reimplementing MakeBoxes. To start with something simple, consider this. WMA: In[1]:= MakeBoxes[a b]
Out[1]= RowBox[{a, , b}] (* There is a space character, " ", between the two commas, but rendering drops double quotes *)
In[2]:= MakeBoxes[a + b]
Out[2]= RowBox[{a, +, b}] Mathics3: In[1]:= MakeBoxes[a b]
Out[1]= RowBox[{Times, [, RowBox[{a, ,, b}], ]}]
In[2]:= MakeBoxes[a + b]
Out[2]= RowBox[{Plus, [, RowBox[{a, ,, b}], ]}] Can we write an NMakeBoxes function that does not violate the operational details outlined here and that makes this work? |
Beta Was this translation helpful? Give feedback.
-
The difference between the WMA and Mathics behavior is that |
Beta Was this translation helpful? Give feedback.
-
Plan for refactor MakeBoxes and formatting
What we have learn about how formatting works in WMA
Differently to other parts of WMA, the way in which boxes works is particularly obscure, and is
hidden even when we look at "low-level" tracing tools, like
TracePrint
ortrace
debug. So toestablish how expressions must be formatted, or how
MakeBoxes[...]
expressions should beevaluated relies mostly on doing experiments and guessing. These are some of my findings studing it:
MakeBoxes[expr, fmt] only should allow a
fmt
listed in $BoxForms.It is possible to extend
$BoxForms
by usingAppendTo
,and automatically it is included in
$PrintForm
. However, we can not use it in aMakeBoxes
Expression until we assign aParentForm
But we can only
https://reference.wolfram.com/language/ref/message/ParentForm/deflt.html.en
If
fmt
is in $PrintForms but not in $BoxForms, then the rules should beof the form
MakeBoxes[Format[expr_,fmt], boxfmt_]
orMakeBoxes[fmt[expr_], boxfmt_]
:It it useful to define a format rule when
MyForm
is the head of an expression. Then, by wrapping theexpression with the form name, we get the correspoindig form, and the form appears in the output line:
If we now set a MakeBoxes rule for this symbol (as an upvalue), the rule only applies
when the expression is wrapped in StandardForm:
When an expression does not have a
MyForm
rule that match, OutputForm is used:InputForm, OutputForm and FullForm must be processed using FormatValues associated to them, but not
using MakeBoxes. MakeBoxes OutputForm / FullForm #1163 and Independent FullForm #1316 goes in that line.
MakeBoxes[] specific rules should be stored as format rules associated to the
corresponding symbols, and not as downvalues rules associated to MakeBoxes.
Notice for example that UpValues are not used in MakeBoxes:
This also allows to remove the special case in mathics.core.builtin.Builtin.contribute
that requires that a MakeBoxes definition be loaded before any other.
The only special case must be the "general" rule
MakeBoxes[expr_, fmt_]
that look for format rules and apply them to expr.
Makeboxes rules are applied with the following priority:
UpValues rules are applied before than downvalues:
User defined rules are applied over builtin rules:
Assignment to
Format
with a single argument defines both a generic MakeBoxes rule and a Format rule:The first rule handles a $BoxForm as it was a $PrintForm. The second rule applies to any $PrintForm. Notice that
by setting a MakeBoxes as downvalue we can not modify the result:
By assigning as UpValue, then we get the effect:
Notice now that if we overwrite the standard rule for MakeBoxes, all the outputs tagged as
StandardForm
are affected:but OutputForm is not affected:
InputForm
,FullForm
and any other$PrintForm
are neither affected:This is because $PrintForms do not make use of MakeBoxes rules, but just the Format[...] rules.
Assignment to MakeBoxes should store FormatValues to MakeBoxes (if a DownValue)
and to the Head of the first element (
MakeBoxes[f[x_],fmt_]^:=formatfunc[x,ftm]
must be stored as a
FormatValue
off
).How I guess we should proceed
To get better compliance with WMA, I think we should introduce changes in sequence:
FullForm
,InputForm
andOutputForm
formatting, without relying on MakeBoxes. I already started to do this.
MakeBoxes[expr, form]
rules for forms not in $BoxForms toMakeBoxes[form[expr], boxform_]
. This includes MathMLForm and TeXForm.to the corresponding symbols, and add an
eval
function that applies these rules.This would be the larger change.
assignment
code to modify where MakeBoxes rules are stored in assignments.Of course, there are more, but once we reach this point, the following changes will be smaller, and can also be implemented as autoload WL modules.
Beta Was this translation helpful? Give feedback.
All reactions