3
$\begingroup$

When I looked carefully at the $x_{j}=1$, I realized that it is essentially an assignment of a function. A function is as left-value, isn't it strange? What syntax supports this?

I tried assigning a value to a normal function, like "Plus[x,y]=1", but it's not allowed. Then, when I looked up some related information, I saw someone say that $x_{j}$ is a box-structure. But what is box-structure? Even Mathematica's documentation doesn't explain it. Because everything in Mathematica can be write into the nesting of Head[arguments], what are the features of box-structure? i have a guess: the function can be left-value is a box-structure, is it right?

$\endgroup$
0

2 Answers 2

10
$\begingroup$

Definitions

The tech note, Transformation Rules and Definitions, starting with the section "Making Definitions for Indexed Objects" and going on through "Modifying Built-in Functions" address the issues raised in the OP about definitions, including the fact that almost any (unprotected) expression may be an LHS. The key restriction is that there is a symbol that is not Protected and not Locked to which the definition rule can be attached. For Set/SetDelayed the symbol is either the head or the head of the head (of the head...) of the expression. For UpSet or TagSet, the symbol may be at level 1; see their doc pages. Another restriction is that the expression functions as a pattern in the replacement rule created by the definition. The first part of the tech note deals with this.

For the example in the OP, $x_j=1$ is executed in the kernel as Set[Subscript[x, j], 1], which creates a DownValue for Subscript, which is an unprotected symbol in the System` context.

Btw, Plus is special: Why does this pattern with Plus not work for numbers?.

Boxes

Finally, box-structure seems irrelevant to making definitions. Boxes are principally used to structure input and output so that the front end and kernel can communicate. Box structures are just expressions, with the same nested structure; valid box structures are ones that the front end accepts. Subscript[x, j] is not a box structure. MakeBoxes[Subscript[x, j], StandardForm] converts it to the box structure that is displayed in the front end with a subscript, namely SubscriptBox[x, j]. Showing the display form of boxes is called "typesetting" in Mathematica.

Some functions for dealing with boxes:

MakeBoxes[expr, form]
MakeExpression[boxes, form]
RawBoxes[boxes]
DisplayForm[boxes]
ToBoxes[expr]
ToExpression[boxes]

The tech note, Textual Input and Output, discusses representing expressions by boxes. See also the tag wiki for [boxes].

$\endgroup$
6
$\begingroup$

Assigning values to symbols or expressions follows conventinal rules of programming laguages.

Plus[x,y]=1

is simply not allowed, because it changes the definition of Plus. This is the case for all fixed operators by their Attribute Protected. Since the definition of a function is in effect wirting a list of replacement rules for patterns, the assigment of values to functions is the normal.

You may Unprotect Plus and make definitions. But these definitions change the system globally, so you will not have much fun by this approach.

To make it a definition tied to x or y you have to define Rules only for x

x:/ Plus[x,y]:= x*y 

To see what the system really is storing, use

UpValues[x]

{HoldPattern[x + y] :> x y}

This rule will apply to the fixed symbols x,y as two arguments of Plus only. By the Attributes Orderless and Flat of Plus is works for y+x and x + 1 + y too.

If you really want to mimic everyday times as operator eg., be careful

Unprotect[Times];
Times[d[x_], y_] ^:= D[y, x]
Protect[Times]

d[x]*f[x]

f'[x]
$\endgroup$
1
  • $\begingroup$ @Aerterliusi "f you really want to mimic everyday times as operator..." That is how to do it, but don't; you really don't want to change the behavior of basic operations like Plus and Times)! Instead, define the behavior of binary operators like NonCommutativeMultiply (**) that don't have any built-in functionality. $\endgroup$
    – march
    Commented Apr 17, 2023 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.