The Standard Evaluation Procedure

Here the standard procedure used by the Wolfram System to evaluate expressions is described. This procedure is the one followed for most kinds of expression. There are, however, some kinds of expressions, such as those used to represent the Wolfram System programs and control structures, that are evaluated in a nonstandard way.

In the standard evaluation procedure, the Wolfram System first evaluates the head of an expression and then evaluates each element of the expression. These elements are in general themselves expressions, to which the same evaluation procedure is recursively applied.

The three Print functions are evaluated in turn, each printing its argument, then returning the value Null:
Click for copyable input
This assigns the symbol ps to be Plus:
Click for copyable input
The head ps is evaluated first, so this expression behaves just like a sum of terms:
Click for copyable input

As soon as the Wolfram System has evaluated the head of an expression, it sees whether the head is a symbol that has attributes. If the symbol has the attributes Orderless, Flat, or Listable, then immediately after evaluating the elements of the expression the Wolfram System performs the transformations associated with these attributes.

The next step in the standard evaluation procedure is to use definitions that the Wolfram System knows for the expression it is evaluating. The Wolfram System first tries to use definitions that you have made, and if there are none that apply, it tries builtin definitions.

If the Wolfram System finds a definition that applies, it performs the corresponding transformation on the expression. The result is another expression, which must then in turn be evaluated according to the standard evaluation procedure.

Evaluate the head of the expression.
Evaluate each element in turn.
Apply transformations associated with the attributes Orderless, Listable, and Flat.
Apply any definitions that you have given.
Apply any builtin definitions.
Evaluate the result.

The standard evaluation procedure.

As discussed in "Principles of Evaluation", the Wolfram System follows the principle that each expression is evaluated until no further definitions apply. This means that the Wolfram System must continue reevaluating results until it gets an expression that remains unchanged through the evaluation procedure.

Here is an example that shows how the standard evaluation procedure works on a simple expression. Assume that a=7.

2ax+a^2+1here is the original expression
Plus[Times[2,a,x],Power[a,2],1]this is the internal form
Times[2,a,x]this is evaluated first
Times[2,7,x]a is evaluated to give 7
Times[14,x]builtin definitions for Times give this result
Power[a,2]this is evaluated next
Power[7,2]here is the result after evaluating a
49builtin definitions for Power give this result
Plus[Times[14,x],49,1]here is the result after the arguments of Plus have been evaluated
Plus[50,Times[14,x]]builtin definitions for Plus give this result
50+14xthe result is printed like this

A simple example of evaluation in the Wolfram System.

The Wolfram System provides various ways to "trace" the evaluation process, as discussed in "Tracing Evaluation". The function Trace[expr] gives a nested list showing each subexpression generated during evaluation. (Note that the standard evaluation traverses the expression tree in a depthfirst way, so that the smallest subparts of the expression appear first in the results of Trace.)

First set a to 7:
Click for copyable input
This gives a nested list of all the subexpressions generated during the evaluation of the expression:
Click for copyable input

The order in which the Wolfram System applies different kinds of definitions is important. The fact that the Wolfram System applies definitions you have given before it applies builtin definitions means that you can give definitions that override the builtin ones, as discussed in "Modifying Built-in Functions".

This expression is evaluated using the builtin definition for ArcSin:
Click for copyable input
You can give your own definitions for ArcSin. You need to remove the protection attribute first:
Click for copyable input
Your definition is used before the one that is built in:
Click for copyable input

As discussed in "Associating Definitions with Different Symbols", you can associate definitions with symbols either as upvalues or downvalues. The Wolfram System always tries upvalue definitions before downvalue ones.

If you have an expression like f[g[x]], there are, in general, two sets of definitions that could apply: downvalues associated with f and upvalues associated with g. The Wolfram System tries the definitions associated with g before those associated with f.

This ordering follows the general strategy of trying specific definitions before more general ones. By applying upvalues associated with arguments before applying downvalues associated with a function, the Wolfram System allows you to make definitions for special arguments that override the general definitions for the function with any arguments.

This defines a rule for f[g[x_]], to be associated with f:
Click for copyable input
This defines a rule for f[g[x_]], to be associated with g:
Click for copyable input
The rule associated with g is tried before the rule associated with f:
Click for copyable input
If you remove rules associated with g, the rule associated with f is used:
Click for copyable input
Definitions associated with g are applied before definitions associated with f in the expression f[g[x]].

The order in which definitions are applied.

Most functions such as Plus that are built into the Wolfram System have downvalues. There are, however, some objects in the Wolfram System that have builtin upvalues. For example, SeriesData objects, which represent power series, have builtin upvalues with respect to various mathematical operations.

For an expression like f[g[x]], the complete sequence of definitions that are tried in the standard evaluation procedure is:

The fact that upvalues are used before downvalues is important in many situations. In a typical case, you might want to define an operation such as composition. If you give upvalues for various objects with respect to composition, these upvalues will be used whenever such objects appear. However, you can also give a general procedure for composition, to be used if no special objects are present. You can give this procedure as a downvalue for composition. Since downvalues are tried after upvalues, the general procedure will be used only if no objects with upvalues are present.

Here is a definition associated with q for composition of "q objects":
Click for copyable input
Here is a general rule for composition, associated with comp:
Click for copyable input
If you compose two q objects, the rule associated with q is used:
Click for copyable input
If you compose r objects, the general rule associated with comp is used:
Click for copyable input

In general, there can be several objects that have upvalues in a particular expression. The Wolfram System first looks at the head of the expression and tries any upvalues associated with it. Then it successively looks at each element of the expression, trying any upvalues that exist. The Wolfram System performs this procedure first for upvalues that you have explicitly defined, and then for upvalues that are built-in. The procedure means that in a sequence of elements, upvalues associated with earlier elements take precedence over those associated with later elements.

This defines an upvalue for p with respect to c:
Click for copyable input
This defines an upvalue for q:
Click for copyable input
Which upvalue is used depends on which occurs first in the sequence of arguments to c:
Click for copyable input