-
-
Notifications
You must be signed in to change notification settings - Fork 59
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
using a private symbol to store the state inside DisplayForm. #617
base: DisplayForm
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
from mathics.builtin.box.layout import GridBox, RowBox, to_boxes | ||
from mathics.builtin.comparison import expr_min | ||
from mathics.builtin.forms.base import FormBaseClass | ||
from mathics.builtin.makeboxes import MakeBoxes, number_form | ||
from mathics.builtin.makeboxes import MakeBoxes, SYMBOL_FORMATBOXES, number_form | ||
from mathics.builtin.tensors import get_dimensions | ||
|
||
from mathics.core.atoms import ( | ||
|
@@ -30,6 +30,7 @@ | |
StringFromPython, | ||
) | ||
|
||
from mathics.core.attributes import A_LOCKED, A_PROTECTED | ||
from mathics.core.expression import Expression, BoxError | ||
from mathics.core.formatter import format_element | ||
from mathics.core.list import ListExpression | ||
|
@@ -188,12 +189,16 @@ def eval_makeboxes(self, expr, f, evaluation): | |
# MakeBoxes is formatting an expression inside a DisplayForm. | ||
# Hopefully, this is temporal and it is not going to be | ||
# needed after the Format/MakeBoxes refactor. | ||
previous_df, evaluation.in_display_form = evaluation.in_display_form, True | ||
|
||
old_value_in_display_form = SYMBOL_FORMATBOXES.evaluate(evaluation) | ||
evaluation.definitions.set_ownvalue(SYMBOL_FORMATBOXES.name, SymbolTrue) | ||
try: | ||
result = MakeBoxes(expr, f).evaluate(evaluation) | ||
except: | ||
pass | ||
evaluation.in_display_form = previous_df | ||
except Exception: | ||
result = None | ||
evaluation.definitions.set_ownvalue( | ||
SYMBOL_FORMATBOXES.name, old_value_in_display_form | ||
) | ||
return result | ||
|
||
|
||
|
@@ -1124,3 +1129,23 @@ def eval_makeboxes_matrix(self, table, f, evaluation, options): | |
return RowBox(String("("), result, String(")")) | ||
|
||
return result | ||
|
||
|
||
# Custom private symbol that helps DisplayForm to control if | ||
# MakeBoxes must process BoxElements as expressions, or keep as they are. | ||
class PrivateSymbolThatControlsTheStateOfDisplayForm(Builtin): | ||
""" | ||
<dl> | ||
<dt>'PrivateSymbolThatControlsTheStateOfDisplayForm' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small thing - please indent nested There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rocky, the question is, do you think that this is a better approach? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mmatera good question. I have to think about it. What I'd be looking for is clues in the WMA code. Think of this like the way a physicist would. We have two hypotheses for how this might have been coded in WMA. Is there a test that distinguishes these? Suppose we hypothesize that WMA follows the WMA way to do this, what test could we come up with that would distinguish the implementations? (Likewise for the other way to code) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok - I trust your conclusion, but I don't follow it. The output above doesn't indicate anything to me one way or another. Please break it down for a novice. If it were implemented as a private hidden symbol what then would you expect to see? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably implement TracePrint so that we can check correspondence easier. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would expect an evaluation for the corresponding hidden symbol. This is an example in which internal variables are used: (I just cut the first lines) Notice the use of symbols in hidden contexts (BoxForm |
||
<dd>If True, MakeBoxes keep Box elements as they are. Otherwise,\ | ||
tries to format them as if it were expressions. | ||
</dl> | ||
""" | ||
|
||
attributes = A_LOCKED | A_PROTECTED | ||
context = "System`Private`MakeBoxes`" | ||
name = "PrivateSymbolThatControlsTheStateOfDisplayForm" | ||
rules = { | ||
"System`Private`MakeBoxes`PrivateSymbolThatControlsTheStateOfDisplayForm": "False", | ||
} | ||
summary_text = "control if boxes are reevaluated." |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,7 @@ | |
from mathics.core.symbols import ( | ||
Atom, | ||
Symbol, | ||
SymbolTrue, | ||
) | ||
|
||
from mathics.core.systemsymbols import ( | ||
|
@@ -46,6 +47,12 @@ | |
) | ||
|
||
|
||
# Afterwards, we can find a better name | ||
SYMBOL_FORMATBOXES = Symbol( | ||
"System`Private`MakeBoxes`PrivateSymbolThatControlsTheStateOfDisplayForm" | ||
) | ||
|
||
|
||
def int_to_s_exp(expr, n): | ||
n = expr.get_int_value() | ||
if n < 0: | ||
|
@@ -396,7 +403,7 @@ def apply_general(self, expr, f, evaluation): | |
if isinstance(expr, BoxElementMixin): | ||
# If we are inside a DisplayForm block, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this really just DisplayForm that could do this? There are no other forms that are like this. Or this isn't something Form creator might want to do? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By now, this is the only case. This could also be a way to implement There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
What makes a print form "non-standard"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MakeBoxes accepts
Makeboxes first applies the format rules associated with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
https://reference.wolfram.com/language/ref/TraditionalForm.html says:
Does this provide a mechanism for how to do this? If so, possibly the same is could done for StandardForm (and is done but just not mentioned). Sometimes it happens that only in implementing the second thing of a kind the general mechanism, and the first is done in a more hacky way is not retrofitted. |
||
# BoxElementMixin are not processed. | ||
if evaluation.in_display_form: | ||
if SYMBOL_FORMATBOXES.evaluate(evaluation) is SymbolTrue: | ||
return expr | ||
expr = expr.to_expression() | ||
if isinstance(expr, Atom): | ||
|
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.
Notice that here we can not use the higher level way
because the attributes.
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.
The drawbacks of this approach are that the access to the attribute is slower, and the code to set the context is larger and weirder than just setting an attribute.