-
-
Notifications
You must be signed in to change notification settings - Fork 60
MakeBoxes OutputForm / FullForm #1163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
In this PR, I partially mimic this behavior for OutputForm and FullForm by modifying |
outputform and fullform following the WMA formatting steps
c4a373d
to
a35983e
Compare
@@ -188,6 +188,33 @@ def eval_display(boxexpr, evaluation): | |||
return boxexpr.elements[0] | |||
|
|||
|
|||
class PaneBox(BoxExpression): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the class called PaneBox when the WMA link goes to InterpretationBox?
I can't find PaneBox mentioned in WMA docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In[1]:= MakeBoxes[OutputForm[a^2/b],StandardForm]
2
a
Out[1]= InterpretationBox[PaneBox[" 2\na\n--\nb"], --, Editable -> False]
b
A PaneBox is a block of multiline text, which by default is "centered".
It can be produced by applying MakeBoxes to a Pane
expression:
In[2]:= MakeBoxes[Pane["hola\nHello",10],StandardForm]
Out[2]= PaneBox["hola\nHello", ImageSize -> 10]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok - thanks for the information. Given this, it seems this is more like a Pane than it is an InterpretationBox. I see that Information[PaneBox]
gives information.
Are there any other Boxes in this category? (Like ParentBox, it exists but is not documented). I can't find any, but I feel we've encountered this kind of situation where there is something that exists but is not documented. If so, how did we handle things there?
But as for changing the link say from InterpretationBox to Pane (if that turns out to be the better thing to do), that is not urgent right now.
This PR is large and I think this PR should come after we work out some simple basics of MakeBoxes.
Also, the PR title is misleading because a lot of this is about character-based formatting on top of stuff related to MakeBoxes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably this can be reformulated in small pieces. One of them is #1316
from mathics.eval.makeboxes import _boxed_string | ||
|
||
inner = str(self.value) | ||
if f in SYSTEM_SYMBOLS_INPUT_OR_FULL_FORM: | ||
if f in ("System`InputForm", "System`FullForm"): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can instance (f, SymblInputForm, SymbolFullForm)
be used instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason, this method receives strings instead symbols as input. We can change this, too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I would not be surprised if there is a conversion or access function to go from an Expression symbol into a string. If so, this was one of the many ways that the original code was hideously slow.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW I noticed recently that Expression.replace_vars is another place where strings are used instead of Symbol.
@@ -126,8 +126,21 @@ def int_to_string_shorter_repr(value: int, form: Symbol, max_digits=640): | |||
return String(value_str) | |||
|
|||
|
|||
def eval_makeboxes_outputform(expr, evaluation, form): | |||
""" | |||
Build a 2D text representation of the expression. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If 2D is synonymous with str(...)
on line 136, then something feels very wrong about putting this in mathics.eval.makeboxes
. 2D is a formatting kind of thing, not a Boxing kind of thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about this more. For high-level form handling and boxing, which largely involve Mathics3 (simple and compound) expressions, these should be done in separate directories, e.g., mathics.form
and mathics.box
.
This is the idea I am trying to elaborate and test in the "Character2D" branches.
In WMA, "formatting" is the result of "render" a
Box
expression, and box expressions are generated by evaluatingMakeBoxes[expr]
in a way that is different from the standard evaluation.In WMA, the "normal" evaluation of the expression
MakeBoxes[expr, form]
discards any user definition and always proceed as follows:expr
.For example:
Notice that evaluating
MakeBoxes[F[x], StandardForm]
does not returns the (down) valueRowBox[...]
(which is what happens in Mathics) but applies first the format rule. Then, MakeBoxes rules are applied over the result of formatting.Notice also that MakeBoxes rules are looked first in the FormValues of the head of the resulting expression, and then in the downvaloes of MakeBoxes.
Here we reset the StandardForm of F in terms of
Fs
, and set a "UpValue" for MakeBoxes[Fs]:Now, when the REPL wants to format
F[1]
, uses theOutputForm
But if we ask for the StandardForm
MakeBoxes looks in FormatValues[Fs] for a rule. Setting a DownValue for MakeBoxes does not have any effect, because the rules are first looked in the FormatValues of
Fs
.Here we see that UpSet[MakeBoxes[...], ....] stores the second argument as FormatValues instead of Upvalues: