Evaluation

The Standard Evaluation Sequence

The following is the sequence of steps that the Wolfram Language follows in evaluating an expression like h[(Subscript[e, 1]),(Subscript[e, 2])…]. Every time the expression changes, the Wolfram Language effectively starts the evaluation sequence over again.

If the expression is a raw object (e.g., (RefLink[Integer, paclet:ref/Integer]), (RefLink[String, paclet:ref/String]), etc.), leave it unchanged.

Evaluate the head h of the expression.

Evaluate each element (Subscript[e, i]) of the expression in turn. If h is a symbol with attributes (RefLink[HoldFirst, paclet:ref/HoldFirst]), (RefLink[HoldRest, paclet:ref/HoldRest]), (RefLink[HoldAll, paclet:ref/HoldAll]), or (RefLink[HoldAllComplete, paclet:ref/HoldAllComplete]), then skip evaluation of certain elements.

Unless h has attribute (RefLink[HoldAllComplete, paclet:ref/HoldAllComplete]), strip the outermost of any (RefLink[Unevaluated, paclet:ref/Unevaluated]) wrappers that appear in the (Subscript[e, i]).

Unless h has attribute (RefLink[SequenceHold, paclet:ref/SequenceHold]), flatten out all (RefLink[Sequence, paclet:ref/Sequence]) objects that appear among the (Subscript[e, i]).

If h has attribute (RefLink[Flat, paclet:ref/Flat]), then flatten out all nested expressions with head h.

If h has attribute (RefLink[Listable, paclet:ref/Listable]), then thread through any (Subscript[e, i]) that are lists.

If h has attribute (RefLink[Orderless, paclet:ref/Orderless]), then sort the (Subscript[e, i]) into order.

Unless h has attribute (RefLink[HoldAllComplete, paclet:ref/HoldAllComplete]), use any applicable transformation rules associated with f that you have defined for objects of the form h[f[(Subscript[e, 1]),…],…].

Use any built-in transformation rules associated with f for objects of the form h[f[(Subscript[e, 1]),…],…].

Use any applicable transformation rules that you have defined for h[f[(Subscript[e, 1]),(Subscript[e, 2]),…],…] or for h[…][…].

Use any built-in transformation rules for h[(Subscript[e, 1]),(Subscript[e, 2]),…] or for h[…][…].

Nonstandard Argument Evaluation

There are a number of built-in Wolfram Language functions that evaluate their arguments in special ways. The control structure (RefLink[While, paclet:ref/While]) is an example. The symbol (RefLink[While, paclet:ref/While]) has the attribute (RefLink[HoldAll, paclet:ref/HoldAll]). As a result, the arguments of (RefLink[While, paclet:ref/While]) are not evaluated as part of the standard evaluation process. Instead, the internal code for (RefLink[While, paclet:ref/While]) evaluates the arguments in a special way. In the case of (RefLink[While, paclet:ref/While]), the code evaluates the arguments repeatedly, so as to implement a loop.

Built-in functions that evaluate their arguments in special ways.

Logical Operations

In an expression of the form (Subscript[e, 1])&&(Subscript[e, 2])&&(Subscript[e, 3]) the (Subscript[e, i]) are evaluated in order. As soon as any (Subscript[e, i]) is found to be (RefLink[False, paclet:ref/False]), evaluation is stopped, and the result (RefLink[False, paclet:ref/False]) is returned. This means that you can use the (Subscript[e, i]) to represent different “branches” in a program, with a particular branch being evaluated only if certain conditions are met.

The (RefLink[Or, paclet:ref/Or]) function works much like (RefLink[And, paclet:ref/And]); it returns (RefLink[True, paclet:ref/True]) as soon as it finds any argument that is (RefLink[True, paclet:ref/True]). (RefLink[Xor, paclet:ref/Xor]), on the other hand, always evaluates all its arguments.

Iteration Functions

An iteration function such as (RefLink[Do, paclet:ref/Do])[f,{i,(Subscript[i, min]),(Subscript[i, max])}] is evaluated as follows:

The limits (Subscript[i, min]), (Subscript[i, max]) are evaluated.

The value of the iteration variable i is made local, effectively using (RefLink[Block, paclet:ref/Block]).

(Subscript[i, min]) and (Subscript[i, max]) are used to determine the sequence of values to be assigned to the iteration variable i.

The iteration variable is successively set to each value, and f is evaluated in each case.

The local values assigned to i are cleared.

If there are several iteration variables, the same procedure is followed for each variable in turn, for every value of all the preceding variables.

Unless otherwise specified, f is not evaluated until a specific value has been assigned to i, and is then evaluated for each value of i chosen. You can use (RefLink[Evaluate, paclet:ref/Evaluate])[f] to make f be evaluated immediately, rather than only after a specific value has been assigned to i.

Assignments

The left-hand sides of assignments are only partially evaluated.

If the left-hand side is a symbol, no evaluation is performed.

If the left-hand side is a function without hold attributes, the arguments of the function are evaluated, but the function itself is not evaluated.

The right-hand side is evaluated for immediate (=), but not for delayed (:=), assignments.

Any subexpression of the form (RefLink[HoldPattern, paclet:ref/HoldPattern])[expr] that appears on the left-hand side of an assignment is not evaluated. When the subexpression is used for pattern matching, it matches as though it were expr without the (RefLink[HoldPattern, paclet:ref/HoldPattern]).

Overriding Nonstandard Argument Evaluation

Overriding holding of arguments.

By using (RefLink[Evaluate, paclet:ref/Evaluate]), you can get any argument of a function evaluated immediately, even if the argument would usually be evaluated later under the control of the function. An exception to this is when the function has the (RefLink[HoldAllComplete, paclet:ref/HoldAllComplete]) attribute; in this case, the contents of the function are not modified by the evaluator.

Preventing Evaluation

The Wolfram Language provides various functions which act as "wrappers" to prevent the expressions they contain from being evaluated.

Wrappers that prevent expressions from being evaluated.

Global Control of Evaluation

In the evaluation procedure described so far, two basic kinds of steps are involved:

Iteration: evaluate a particular expression until it no longer changes.

Recursion: evaluate subsidiary expressions needed to find the value of a particular expression.

Iteration leads to evaluation chains in which successive expressions are obtained by the application of various transformation rules.

(RefLink[Trace, paclet:ref/Trace]) shows evaluation chains as lists, and shows subsidiary evaluations corresponding to recursion in sublists.

The expressions associated with the sequence of subsidiary evaluations which lead to an expression currently being evaluated are given in the list returned by (RefLink[Stack, paclet:ref/Stack])[].

Global variables controlling the evaluation of expressions.

Aborts

You can ask the Wolfram Language to abort at any point in a computation, either by calling the function (RefLink[Abort, paclet:ref/Abort])[], or by typing appropriate interrupt keys.

When asked to abort, the Wolfram Language will terminate the computation as quickly as possible. If the answer obtained would be incorrect or incomplete, then the Wolfram Language returns (RefLink[$Aborted, paclet:ref/$Aborted]) instead of giving that answer.

Aborts can be caught using (RefLink[CheckAbort, paclet:ref/CheckAbort]), and can be postponed using (RefLink[AbortProtect, paclet:ref/AbortProtect]).