Skip to content
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

MakeBoxes OutputForm / FullForm #1163

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

MakeBoxes OutputForm / FullForm #1163

wants to merge 2 commits into from

Conversation

mmatera
Copy link
Contributor

@mmatera mmatera commented Nov 11, 2024

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 evaluating MakeBoxes[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:

  • Evaluate formatting rules (apply Format[....] rules) over expr.
  • Then, applies MakeBoxes rules.
    For example:

In[1]:= MakeBoxes[F[x_],fmt_]:=RowBox[{"F",MakeBoxes[x]}]                       

In[2]:= Format[F[x_],OutputForm]:="OutputForm F"                                

In[3]:= Format[F[x_],StandardForm]:="StandardForm F"                            

In[4]:= MakeBoxes[F[x],StandardForm]                                            

Out[4]= "StandardForm F"

In[5]:= MakeBoxes[F[x]//OutputForm,StandardForm]                                

Out[5]= InterpretationBox[PaneBox["OutputForm F"], OutputForm F, 
 
>    Editable -> False]

In[6]:= FormatValues[F]                                                         

Out[6]= {HoldPattern[OutputForm F] :> OutputForm F, 
 
>    HoldPattern[MakeBoxes[OutputForm F, StandardForm]] :> "StandardForm F"}

In[7]:= ??MakeBoxes                                                             

Out[7]= MakeBoxes[expr, form] is the low‐level function used in 
         
        >    Wolfram System sessions to convert expressions into boxes. 

         
        >     MakeBoxes[expr] is the function to convert expr
         
        >       to StandardForm boxes.
        MakeBoxes[F[x_], fmt_] := RowBox[{"F", MakeBoxes[x]}]
         
        Attributes[MakeBoxes]={HoldAllComplete}

Notice that evaluating MakeBoxes[F[x], StandardForm] does not returns the (down) value RowBox[...] (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]:

In[12]:= Format[F[x_],StandardForm]:={Fs[1]}                                    

In[13]:= MakeBoxes[Fs[x_],form_]^:="p"                                          

Now, when the REPL wants to format F[1], uses the OutputForm

In[14]:= F[1]                                                                   

Out[14]= OutputForm F

But if we ask for the StandardForm

In[16]:= F[1]//StandardForm                                                     

Out[16]//StandardForm= {p}

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.

In[17]:= MakeBoxes[Fs[u_],form_]:="q"                                           

In[18]:= F[1]//StandardForm                                                     

Out[18]//StandardForm= {p}

Here we see that UpSet[MakeBoxes[...], ....] stores the second argument as FormatValues instead of Upvalues:

In[19]:= ?? Fs                                                                  

Out[19]= Global`Fs
         MakeBoxes[Fs[x_], form_] ^:= "p"
          

In[20]:= FormatValues[Fs]                                                       

Out[20]= {HoldPattern[MakeBoxes[Fs[x_], form_]] :> p}

@mmatera
Copy link
Contributor Author

mmatera commented Nov 11, 2024

In this PR, I partially mimic this behavior for OutputForm and FullForm by modifying mathics.eval.makeboxes.format_element to follow this path (call a function that is equivalent to evaluate MakeBoxes[...] in WMA)

@mmatera mmatera marked this pull request as ready for review November 12, 2024 01:46
outputform and fullform following the WMA formatting steps
@@ -188,6 +188,33 @@ def eval_display(boxexpr, evaluation):
return boxexpr.elements[0]


class PaneBox(BoxExpression):
Copy link
Member

@rocky rocky Feb 11, 2025

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.

Copy link
Contributor Author

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]

Copy link
Member

@rocky rocky Feb 11, 2025

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.

Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants