3
$\begingroup$

I am finding it a bit challenging to deduce the standard evaluation sequence from its documentation, and I suspect I am failing to understand how some terms are being used. Take the documentation at https://reference.wolfram.com/language/tutorial/Evaluation.html What part of this can be interpreted as saying that own values will always be applied before down values? (I'm assuming both that this is true and that it's in there somewhere ...)

$\endgroup$
5
$\begingroup$

The documentation section cited describes the steps to evaluate an expression like h[e1, e2...].

The second step is:

Evaluate the head h of the expression.

This is a recursive step where the head itself is evaluated. Most of the time, the head of a function call is a symbol. When a symbol is evaluated, it is replaced by its own-value (if any). The cited documentation does not mention how symbols are evaluated.

It is not until we get to the eleventh step that we find:

Use any applicable transformation rules that you have defined for h[f[e1,e2,…],…] or for h[…][…].

This step refers to the use of user-defined down-values (h[...]) or sub-values (h[...][...]) of h.

Incidentally, I think that the somewhat confusing appearance of f[e1,e2...] in this step is a typo, a copy-and-paste error from the preceding two steps which define the action of up-values (user-defined and built-in). The mistake is not repeated in the next step which refers to built-in down-values simply by h[e1,e2,...].

The evaluation sequence is described in more detail in Chapter 7 of Power Programming with Mathematica by David B. Wagner. It is freely available to StackExchangers via (16485).

When Do Own-Values and Down-Values Conflict?

Most of the time, there is no conflict between own-values and down-values. For example, consider this simple function definition and its invocation:

ClearAll[f]

f[x_] := x + 1

f[1]
(* 2 *)

When the head of f[1] is evaluated in step two, it means that f is evaluated. Most of the time, symbols that are used as functions do not have any own-values, so they self-evaluate. Such symbols are said to be "inert". In the present case, f is inert. As a result, when evaluation gets down to the eleventh step the down-value of f is used to produce the final result.

Now let's consider are less conventional case where f has both a down-value and an own-value:

ClearAll[f, g]

f[x_] := x + 1

f = g;

f[1]
(* g[1] *)

Now, when the head f is evaluated its own-value comes into play. f evaluates to g which means that evaluation effectively restarts with the expression g[1]. This is noted where the documentation states:

Every time the expression changes, the Wolfram Language effectively starts the evaluation sequence over again.

g is inert, so the expression remains unchanged in the restarted second step. Once we get down to the eleventh step we find that the down-value of f does not come into play. Why? Because the head of the expression is now g. g has no down-value so the expression as a whole is inert.

In this way we see that in some sense the own-value of f "shadows" the down-value of f.

The head of an expression need not be a symbol. It could be a function call in its own right. This introduces a similar sequence of events where a down-value of f can shadow a sub-value:

ClearAll[f, g]

(* establish a sub-value on f *)
f[x_][y_] := {x, y}

f[1][2]
(* {1, 2} *)

(* establish a down-value on f *)    
f[x_] := g[x]

f[1][2]
(* g[1][2] *)

Again I emphasize that non-inert heads are not the norm. But they can be a very powerful tool and are used extensively (consider, for example, f = Interpolation[{1, 2, 3, 2, 3, 1, 3}]). On the other hand, it would be a very specialized context indeed where one purposely establishes own-values, down-values and/or sub-values simultaneously on the same symbol. Such a circumstance is more likely to occur by error rather than by design (but I won't rule it out completely -- such is the power of symbolic computation).

$\endgroup$
  • $\begingroup$ I had guessed this would be the spot, but I'm still not sure exactly what "Evaluate the head h of the expression" means and in particular why this means "apply the own-values rules". Are these synonymous? In general, I think using the word "evaluate" to define the word "evaluate" is not particularly helpful. And if "the somewhat confusing appearance of f[e1,e2...] is indeed a typo, that also helps me understand this page. $\endgroup$ – Alan Oct 11 '16 at 15:53
  • $\begingroup$ The word "evaluation" is repeated in step two on purpose: it is a recursive step. The cited documentation is really just an outline and glosses over many details. In particular, it is silent about symbol evaluation. A symbol's own-value comes into play when that symbol itself is evaluated. I have added a new section that tries to spell out the interaction between own-values and down-values in more detail: When Do Own-Values and Down-Values Conflict? The Wagner book contains much more detail than the Mathematica documentation itself. $\endgroup$ – WReach Oct 11 '16 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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