When reading through the documentation, you often encounter the phrases DownValues, UpValues, SubValues, and OwnValues. I am trying to understand the distinction between the four of them, and how, for a given Symbol, I can use them. Also, are they processed differently internally?

Truth is, I just looked up what an OwnValue is. But, I'll put together an answer shortly, if Leonid doesn't get to it first.
–
rcollyerJan 18 '12 at 2:54

It's 7.30 in the morning now at my place, and even though I am on vacation, I am inclined to stop now. So, its your call then :)
–
Leonid ShifrinJan 18 '12 at 3:27

1

Just for completeness, there's also FormatValues and NValues, (and less importantly: DefaultValues, DynamicModuleValues and SingularValues)
–
SimonJan 18 '12 at 4:39

4

@Simon SingularValues is a function that calculates the singular values for a matrix. DynamicModuleValues is an option name. They are not in this category.
–
SzabolcsJan 18 '12 at 16:20

2

@Simon You can see everything that's associated with symbols by evaluating something like Language`FullDefinition[x]. The rest are NValues, which can be set as N[x] = 1.0, DefaultValues storing default argument values, FormatValues (please see Format which is also assignable), Messages (self explanatory) and Attributes (self explanatory).
–
SzabolcsJan 18 '12 at 16:21

2 Answers
2

In Mathematica, all functions are really just patterns, and there are different kinds of those.

Let's start with OwnValues, which is the pattern type of a variable as you know it from other programming languages. The symbol having the OwnValue has, as the name suggests, intrinsic, "own", value.

In[1] := a = 2; OwnValues[a]
Out[1] := {HoldPattern[a] :> 2}

A DownValue is defined when the variable itself does not have a meaning, but can get one when combined with the proper arguments. This is the case for the most function definitions

f[x_] := x^2

This defines a pattern for f specifying that each time f[...] is encountered, it is to be replaced by ...^2. This pattern is meaningless if there is a lonely f,

In[2] := f
Out[2] := f

However, when encountered with an argument downwards (i.e. down the internal structure of the command you entered), the pattern applies,

In[3] := f[b]
Out[3] := b^2

You can see the generated rule using

In[4] := DownValues[f]
Out[4] := {HoldPattern[f[x_]] :> x^2}

The next type of pattern concerns UpValues. Sometimes, it's convenient not to associate the rule to the outermost symbol. For example, you may want to have a symbol whose value is 2 when it has a subscript of 1, for example to define a special case in a sequence. This would be entered as follows:

c /: Subscript[c, 1] := 2

If the symbol c is encountered, neither of the discussed patterns apply. c on its own has no own hence no OwnValue, and looking down the command tree of c when seeing Subscript[c,1] yields nothing, since c is already on an outermost branch. An UpValue solves this problem: a symbol having an UpValue defines a pattern where not only the children, but also the parents are to be investigated, i.e. Mathematica has to look up the command tree to see whether the pattern is to be applied.

In[5] := UpValues[c]
Out[5] := {HoldPattern[Subscript[c, 1]] :> 2}

The last command is SubValues, which is used for definitions of the type

d[e][f] = x;

This defines neither an OwnValue nor a DownValue for d, since it does not really define the value for the atomic object d itself, but for d[e], which is a composite. Read the definition above as (d[e])[f]=x.

In[6] := SubValues[d]
Out[6] := {HoldPattern[d[e][f]] :> x}

(Intuitively, an OwnValue for d[e] is created, however calling for this results in an error, i.e. OwnValues[d[e]] creates Argument d[e] at position 1 is expected to be a symbol.)

It's interesting that you mention the function, SubValues, as it doesn't show up in the docs.
–
rcollyerJan 18 '12 at 3:43

5

This concept of upvalues and downvalues seems very powerful, was this a truly Wolfram innovation? or is something that predated in earlier symbolic manipulation?, there is a concept like this in other languages? (looks like in other languages everything is a downvalue at best). Sorry if I am off topic.
–
alfCDec 16 '13 at 19:47

Preamble

In addition to the differences between these global rules reflected in the patterns for which assignment operators put the global rule into one or another ...Value, there is another, and IMO,no less important difference, and that is in how these rules are used in the evaluation sequence.

Evaluation: OwnValues

The OwnValues represent symbols themselves, and therefore they are applied when symbols are evaluated. If these symbols are atomic elements inside some expression, and evaluation comes to them, they are (normally) replaced with the r.h.s. of their OwnValues. The more interesting situation is when the symbols are heads of some normal expressions. In this case, since heads are evaluated very early in the evaluation sequence, these symbols are also replaced with what their OwnValues instruct, but now as heads.

As you can see, none of the Attributes assigned to f actually had a chance to play, since f was replaced by h before the attributes were even considered by the evaluator. The same will happen when we add some definitions (as DownValues, UpValues or SubValues):

In[106]:=
g[x_]:=x^2;
g=h;
g[5]
Out[108]= h[5]

Once again, these definitions had no chance to execute.

These cases may seem contrived, and indeed they do not happen very often as results of a deliberate coding, but they can quite often happen due to a mistake, and then are hard to debug. One very relevant discussion is here. To complete this part, let me mention another very insidious case. Let us just take the last example and reverse the order in which we were creating definitions:

This is another case of evaluation during an assigment: the first definition for f evaluated inside SetDelayed when we attempted to create the second one.

Another important difference: functions defined via DownValues can hold all their arguments if needed, through the HoldAll or HoldAllComplete attributes. Functions defined with SubValues, however, can only normally hold the "innermost" group of arguments:

Similar situation holds for some other attributes, e.g. Listable. This topic is discussed in more detail e.g. here

DownValues vs UpValues: System-wide changes vs. locality

There are several important ways in which UpValues are different from DownValues. One very important aspect is that they allow you to "softly" overload some system functions only on certain symbols. The importance of this can not be emphasized enough - this makes the effect of your code local, and drastically reduces the chances that your code can globally interact and affect some other part of the system or other piece of user-defined code, unlike when you Unprotect system symbols and add some DownValues to them.

In addition to being safe and local, such redefinitions may also be quite general, if one uses constructs like yourSymbol/:f_[_yourSymbol,rest___]:=.... These should be used with care, but can sometimes give very concise and simple solutions. Here is one example where one code can be used to "overload" several system functions at once, giving them additional non-trivial functionality.

DownValues vs UpValues: Order of evaluation

The next point is evaluation. The common statement you can encounter is that "UpValues are applied before DownValues". This must be clarified: for f[g[args]] it means that UpValues for g are applied before DownValues for f, provided that the evaluation process already went all they way "down" to innermost parts, and then went back "up". In particular, it does not mean that UpValues for g will be applied before DownValues for g - if g[args] can evaluate inside f because g has appropriate DownValues, it will (unless f has one of the Hold-attributes), and the presence of UpValues won't prevent that, because (for standard evaluation), evaluation of g[args] happens before the evaluation of f[result-of-evaluation-of g[args]]. For example, here:

The UpValues for g had no chance to apply, since g[y] is transformed into Cos[y] at the previous evaluation step. The situation would be different for non-standard evaluation - either if we give f attributes HoldAll or HoldFirst, or if we wrap g[y] in Unevaluated - in both cases we give the evaluator the instruction to skip the evaluation of g[y]:

In[138]:= f[Unevaluated[g[y]]]
Out[138]= Sin[Cos[y]]

DownValues vs UpValues: Hold-attributes

This one is related to the previous point: one should be aware that search for UpValues is performed even inside heads with Hold- attributes, and therefore, UpValue-based definitions may evaluate even when similarly-looking DownValue - based ones won't. Example:

Summary

I tried to outline and illustrate several technical points which pop up quite frequently in practice and can be frustrating at first. My main message is that a huge difference between different kinds of rules is induced by the way they are used in the main evaluation sequence. This difference may not be immediately apparent, but it affects all aspects of how these rules are used in practice.

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.