此为 Mathematica 7 文档,内容基于更早版本的 Wolfram 语言
查看最新文档(版本11.1)

Precedence of Operators in Notations

How Precedence is determined

The precedence of any new notation or operator is determined by examining the components from which it is constructed. For instance, +ScriptCapitalR is grouped according to the precedence of +, the operator CirclePlusn is grouped according to the precedence of CirclePlus, and the mapping is grouped according to the precedence of LongRightArrow. Generally the grouping behavior of positioning boxes is determined by the "base element". For instance, the expression SubscriptBox[symb, sub] is grouped according to symb. But for some other structural boxes the grouping behavior of surrounding elements is not affected by the behavior of the contents of the box. The precedence of compound objects is determined according to the following table:
type of boxbehavior
AdjustmentBox, ErrorBox, StyleBox, SubscriptBox, SuperscriptBox, SubsuperscriptBox, UnderscriptBox, OverscriptBox, UnderoverscriptBox, TagBoxThese box structures do not isolate their contents from the outside; the contents of these boxes can affect the grouping of surrounding elements
ButtonBox, FormBox, FractionBox, FrameBox, GridBox, InterpretationBox, RowBox, RadicalBox, SqrtBoxThese box structures isolate their contents from the outside; the contents of these boxes do not affect the grouping of surrounding elements

The standard boxes and their relationship to precedence.

The design decision of making the precedence of new compound operators correspond to their constituents makes intuitive sense and generally leads to notations that are consistent. For instance, consider a possible notation for addition and multiplication over a ring LeftAngleBracketScriptCapitalR,+ScriptCapitalR,*ScriptCapitalRRightAngleBracket.
This defines a notation for ring addition and ring multiplication.
In[28]:=
Click for copyable input
Mathematica now can parse and format expressions containing ring additions and multiplications.
In[30]:=
Click for copyable input
Out[30]=
In[31]:=
Click for copyable input
Out[31]//FullForm=
The ring multiplication operator *ScriptCapitalR has a higher precedence than the ring addition operator +ScriptCapitalR because * has a higher precedence than + . Moreover, the notation is automatically set up to parenthesize the expression appropriately to maintain the correct structure.
The output has the correct formatting, styling, spacing, and parenthesization.
In[32]:=
Click for copyable input
Out[32]=
However the above notation for ring addition and ring multiplication is still somewhat limited.
In[33]:=
Click for copyable input
Out[33]=
The above Notation statement can be removed and an InfixNotation can be used instead.
In[34]:=
Click for copyable input
Now +ScriptCapitalR and *ScriptCapitalR act as true infix operators for RingPlus and RingTimes.
In[38]:=
Click for copyable input
Out[38]=

Parentheses in Notations

It is important to point out that bracketing expressions by using {}, (), LeftAngleBracketRightAngleBracket, [], | or other braces in a notation not only changes the grouping of the notation but requires the brackets to be literally present in the input expression.
The following defines a notation that uses parentheses.
In[39]:=
Click for copyable input
In[40]:=
Click for copyable input
Out[40]//FullForm=
Without parentheses input is not recognized as a Semantic wrapper.
In[41]:=
Click for copyable input
Out[41]//FullForm=

Changing Precedences in Notations

You can isolate the elements around an operator or expression by surrounding the operator or expression with a box structure that isolates its contents (cf. the table above). The box structure typically used to do this encapsulation / isolation is the TagBox. Furthermore it is possible to change the precedence of an operator through the use of the TagBox option SyntaxForm. More will be said on this later in "Complex Patterns and Advanced Features".
The underlying box structure of DomainPartialD has an embedded TagBox.
In[21]:=
Click for copyable input
Out[21]=
The embedded TagBox in DomainPartialD is necessary for several reasons: to contain the StyleBox which specifies ZeroWidthTimes as well the styling, to suppress the natural prefix operator grouping of PartialD, and to allow the parsing and formatting rules to act on an encapsulated unit.