Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions mathics/builtin/box/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
symbolic "boxes".

The routines here assist in boxing at the bottom of the hierarchy, typically found when using in a notebook.

`Expression` objects having symbols in this module as head, are evaluated to
`BoxElementMixin` objects. These objects are literal objects, so do not have the method `evaluate`. Text render functions (in `mathics.format`) process `BoxElementMixin` to produce their output.

"""
from typing import Tuple

Expand All @@ -14,7 +18,7 @@
from mathics.core.atoms import String
from mathics.core.attributes import A_HOLD_ALL_COMPLETE, A_PROTECTED, A_READ_PROTECTED
from mathics.core.builtin import Builtin
from mathics.core.element import BaseElement, BoxElementMixin
from mathics.core.element import BaseElement, BoxElementMixin, EvalMixin
from mathics.core.evaluation import Evaluation
from mathics.core.exceptions import BoxConstructError
from mathics.core.expression import Expression
Expand Down Expand Up @@ -208,6 +212,10 @@ class InterpretationBox(BoxExpression):
"""

attributes = A_HOLD_ALL_COMPLETE | A_PROTECTED | A_READ_PROTECTED
options = {
"Editable": "Automatic",
"AutoDelete": "Automatic",
}
summary_text = "box associated to an input expression"

def __repr__(self):
Expand All @@ -233,17 +241,26 @@ def elements(self):
)
return self._elements

def eval_create(self, reprs, expr, evaluation):
"""InterpretationBox[reprs_, expr_]"""
return InterpretationBox(reprs, expr)
def eval_create(self, reprs, expr, evaluation, options):
"""InterpretationBox[reprs_, expr_, OptionsPattern[]]"""
# If the first element is not a literal, this
# function evaluates it (because the symbol has
# the attribute HoldAllComplete, this does not happen
# in the evaluation loop). Then, if the result is a
# BoxElementMixin, creates and return instance of `InterpretationBox`.
if isinstance(reprs, EvalMixin):
reprs = reprs.evaluate(evaluation)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are examples of this code getting used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the exampe is in test.format.format_test.m

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's generic test code. Where in the YAML though?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, the YAML file contains the examples

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I figured that, but anything specific.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the evaluation is successful, the final expression shows the options in lexicographical order.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the information. This is sufficiently complicated and subtle that a comment should be added.

I've been trying to think of one, but I am not sure I fully understand.

Is the idea that reprs is either some sort of evaluatable expression (or is it just some of StyleBox)?

If it is in this former category, then we need to evaluate it to turn it into a box which will process and remove the options?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rule converts an Expression object into a InterpretationBox object. Expression does not have boxes_to_* methods, and InterpretationBox does not have an evaluate method. In order for the conversion happens, the rule must match with the expression. But the expression we have is of the form InterpretationBox[boxed, expr, opt1_, opt2_,...].
Also, InterpretationBox has a HoldAll attribute, so if the first element is not a BoxElementMixin, we need to evaluate it first.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Let's add this as a comment. I can do that tomorrow, along with the other small doc changes.

There is a lot of code in this PR that feels mysterious and bottom-up, as opposed to there beings some higher-level principle that can be used to derive the details coded.

Maybe later we will have a simpler model for expressing InputForm and how that relates IntrpretationBox. IF that happens, it might be that it fits so well that we don't need to make a test like this, or the test feels more obvious.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mechanism of conversions has been around for a while, but probably requires better documentation. The main idea is that MakeBoxes[] does not produce Expression objects, but BoxElementMixin objects. *Box expressions should then be evaluated to these kind of objects. The problem that this PR tries to fix is that this conversion fails for InterpretationBox expressions having optional parameters.

if not isinstance(reprs, BoxElementMixin):
return
return InterpretationBox(reprs, expr, **options)

def eval_to_expression1(self, boxexpr, evaluation):
"""ToExpression[boxexpr_InterpretationBox]"""
return boxexpr.elements[1]
return boxexpr.expr

def eval_to_expression2(self, boxexpr, form, evaluation):
"""ToExpression[boxexpr_InterpretationBox, form_]"""
return boxexpr.elements[1]
return boxexpr.expr

def eval_display(self, boxexpr, evaluation):
"""DisplayForm[boxexpr_InterpretationBox]"""
Expand Down
Loading