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 7 commits into
base: master
Choose a base branch
from
Open

MakeBoxes OutputForm / FullForm #1163

wants to merge 7 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
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.

1 participant