# Tracing Evaluation

The standard way in which *Mathematica* works is to take any expression you give as input, evaluate the expression completely, and then return the result. When you are trying to understand what *Mathematica* is doing, however, it is often worthwhile to look not just at the final result of evaluation, but also at intermediate steps in the evaluation process.

Trace[expr] | generate a list of all expressions used in the evaluation of expr |

Trace[expr,form] | include only expressions that match the pattern form |

Tracing the evaluation of expressions.

In[1]:= |

Out[1]= |

In[2]:= |

Out[2]= |

In[3]:= |

Out[3]= |

Trace[expr] gives a list that includes *all* the intermediate expressions involved in the evaluation of expr. Except in rather simple cases, however, the number of intermediate expressions generated in this way is typically very large, and the list returned by Trace is difficult to understand.

Trace[expr, form] allows you to "filter" the expressions that Trace records, keeping only those that match the pattern form.

In[4]:= |

Out[4]= |

*all*the intermediate expressions generated in the evaluation of . The result is quite complicated.

In[5]:= |

Out[5]= |

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

Trace[expr, form] effectively works by intercepting every expression that is about to be evaluated during the evaluation of expr, and picking out those that match the pattern form.

If you want to trace "calls" to a function like , you can do so simply by telling Trace to pick out expressions of the form . You can also use patterns like to pick out calls with particular argument structure.

A typical *Mathematica* program, however, consists not only of "function calls" like , but also of other elements, such as assignments to variables, control structures, and so on. All of these elements are represented as expressions. As a result, you can use patterns in Trace to pick out any kind of *Mathematica* program element. Thus, for example, you can use a pattern like to pick out all assignments to the symbol .

In[8]:= |

Out[8]= |

Trace[expr, form] can pick out expressions that occur at any time in the evaluation of expr. The expressions need not, for example, appear directly in the form of expr that you give. They may instead occur, say, during the evaluation of functions that are called as part of the evaluation of expr.

In[9]:= |

In[10]:= |

Out[10]= |

Trace allows you to monitor intermediate steps in the evaluation not only of functions that you define, but also of some functions that are built into *Mathematica*. You should realize, however, that the specific sequence of intermediate steps followed by built-in *Mathematica* functions depends in detail on their implementation and optimization in a particular version of *Mathematica*.

Trace[expr,f[___]] | show all calls to the function f |

Trace[expr,i=_] | show assignments to i |

Trace[expr,_=_] | show all assignments |

Trace[expr,Message[___]] | show messages generated |

Some ways to use Trace.

The function Trace returns a list that represents the "history" of a *Mathematica* computation. The expressions in the list are given in the order that they were generated during the computation. In most cases, the list returned by Trace has a nested structure, which represents the "structure" of the computation.

The basic idea is that each sublist in the list returned by Trace represents the "evaluation chain" for a particular *Mathematica* expression. The elements of this chain correspond to different forms of the same expression. Usually, however, the evaluation of one expression requires the evaluation of a number of other expressions, often subexpressions. Each subsidiary evaluation is represented by a sublist in the structure returned by Trace.

In[11]:= |

Out[11]= |

In[12]:= |

Out[12]= |

In[13]:= |

Out[13]= |

In[14]:= |

Out[14]= |

In[15]:= |

Out[15]= |

In[16]:= |

Out[16]= |

There are two basic ways that subsidiary evaluations can be required during the evaluation of a *Mathematica* expression. The first way is that the expression may contain subexpressions, each of which has to be evaluated. The second way is that there may be rules for the evaluation of the expression that involve other expressions that themselves must be evaluated. Both kinds of subsidiary evaluations are represented by sublists in the structure returned by Trace.

In[17]:= |

Out[17]= |

In[18]:= |

In[19]:= |

Out[19]= |

You often get nested lists when you trace the evaluation of functions that are defined "recursively" in terms of other instances of themselves. The reason is typically that each new instance of the function appears as a subexpression in the expressions obtained by evaluating previous instances of the function.

Thus, for example, with the definition , the evaluation of yields the expression , which contains as a subexpression.

In[20]:= |

Out[20]= |

In[21]:= |

In[22]:= |

Out[22]= |

In[23]:= |

In[24]:= |

Out[24]= |

In[25]:= |

Out[25]= |

Each step in the evaluation of any *Mathematica* expression can be thought of as the result of applying a particular transformation rule. As discussed in "Associating Definitions with Different Symbols", all the rules that *Mathematica* knows are associated with specific symbols or "tags". You can use Trace[expr, f] to see all the steps in the evaluation of expr that are performed using transformation rules associated with the symbol f. In this case, Trace gives not only the expressions to which each rule is applied, but also the results of applying the rules.

In general, Trace[expr, form] picks out all the steps in the evaluation of expr where form matches *either* the expression about to be evaluated, *or* the tag associated with the rule used.

Trace[expr,f] | show all evaluations that use transformation rules associated with the symbol f |

Trace[expr,f|g] | show all evaluations associated with either f or g |

Tracing evaluations associated with particular tags.

In[26]:= |

Out[26]= |

In[27]:= |

Out[27]= |

In[28]:= |

In[29]:= |

Out[29]= |

Trace[expr,form,TraceOn->oform] | switch on tracing only within forms matching oform |

Trace[expr,form,TraceOff->oform] | switch off tracing within any form matching oform |

Switching off tracing inside certain forms.

Trace[expr, form] allows you to trace expressions matching form generated at any point in the evaluation of expr. Sometimes, you may want to trace only expressions generated during certain parts of the evaluation of expr.

By setting the option TraceOn->oform, you can specify that tracing should be done only during the evaluation of forms that match oform. Similarly, by setting TraceOff->oform, you can specify that tracing should be switched off during the evaluation of forms that match oform.

In[30]:= |

Out[30]= |

In[31]:= |

Out[31]= |

In[32]:= |

Out[32]= |

Trace[expr,lhs->rhs] | find all expressions matching lhs that arise during the evaluation of expr, and replace them with rhs |

Applying rules to expressions encountered during evaluation.

In[33]:= |

Out[33]= |

A powerful aspect of the *Mathematica* Trace function is that the object it returns is basically a standard *Mathematica* expression that you can manipulate using other *Mathematica* functions. One important point to realize, however, is that Trace wraps all expressions that appear in the list it produces with HoldForm to prevent them from being evaluated. The HoldForm is not displayed in standard *Mathematica* output format, but it is still present in the internal structure of the expression.

In[34]:= |

Out[34]= |

In[35]:= |

Out[35]//InputForm= | |

*Mathematica*output format, it is sometimes difficult to tell which lists are associated with the structure returned by Trace, and which are expressions being evaluated.

In[36]:= |

Out[36]= |

In[37]:= |

Out[37]//InputForm= | |

In[38]:= |

Out[38]= |

For sophisticated computations, the list structures returned by Trace can be quite complicated. When you use Trace[expr, form], Trace will include as elements in the lists only those expressions that match the pattern form. But whatever pattern you give, the nesting structure of the lists remains the same.

In[39]:= |

Out[39]= |

In[40]:= |

Out[40]= |

You can set the option TraceDepth->n to tell Trace to include only lists nested at most n levels deep. In this way, you can often pick out the "big steps" in a computation, without seeing the details. Note that by setting TraceDepth or TraceOff you can avoid looking at many of the steps in a computation, and thereby significantly speed up the operation of Trace for that computation.

In[41]:= |

Out[41]= |

Trace[expr,form,TraceDepth->n] | trace the evaluation of expr, ignoring steps that lead to lists nested more than n levels deep |

Restricting the depth of tracing.

When you use Trace[expr, form], you get a list of all the expressions that match form produced during the evaluation of expr. Sometimes it is useful to see not only these expressions, but also the results that were obtained by evaluating them. You can do this by setting the option TraceForward->True in Trace.

In[42]:= |

Out[42]= |

Expressions picked out using Trace[expr, form] typically lie in the middle of an evaluation chain. By setting TraceForward->True, you tell Trace to include also the expression obtained at the end of the evaluation chain. If you set TraceForward->All, Trace will include *all* the expressions that occur after the expression matching form on the evaluation chain.

In[43]:= |

Out[43]= |

By setting the option TraceForward, you can effectively see what happens to a particular form of expression during an evaluation. Sometimes, however, you want to find out not what happens to a particular expression, but instead how that expression was generated. You can do this by setting the option TraceBackward. What TraceBackward does is to show you what *preceded* a particular form of expression on an evaluation chain.

In[44]:= |

Out[44]= |

In[45]:= |

Out[45]= |

TraceForward and TraceBackward allow you to look forward and backward in a particular evaluation chain. Sometimes, you may also want to look at the evaluation chains within which the particular evaluation chain occurs. You can do this using TraceAbove. If you set the option TraceAbove->True, then Trace will include the initial and final expressions in all the relevant evaluation chains. With TraceAbove->All, Trace includes all the expressions in all these evaluation chains.

In[46]:= |

Out[46]= |

In[47]:= |

Out[47]= |

Trace[expr,form,opts] | trace the evaluation of expr using the specified options |

TraceForward->True | include the final expression in the evaluation chain containing form |

TraceForward->All | include all expressions following form in the evaluation chain |

TraceBackward->True | include the first expression in the evaluation chain containing form |

TraceBackward->All | include all expressions preceding form in the evaluation chain |

TraceAbove->True | include the first and last expressions in all evaluation chains that contain the chain containing form |

TraceAbove->All | include all expressions in all evaluation chains that contain the chain containing form |

Option settings for including extra steps in trace lists.

The basic way that Trace[expr, ...] works is to intercept each expression encountered during the evaluation of expr, and then to use various criteria to determine whether this expression should be recorded. Normally, however, Trace intercepts expressions only *after* function arguments have been evaluated. By setting TraceOriginal->True, you can get Trace also to look at expressions *before* function arguments have been evaluated.

In[48]:= |

Out[48]= |

The list structure produced by Trace normally includes only expressions that constitute steps in non-trivial evaluation chains. Thus, for example, individual symbols that evaluate to themselves are not normally included. Nevertheless, if you set TraceOriginal->True, then Trace looks at absolutely every expression involved in the evaluation process, including those that have trivial evaluation chains.

In[49]:= |

Out[49]= |

option name | default value | |

TraceForward | False | whether to show expressions following form in the evaluation chain |

TraceBackward | False | whether to show expressions preceding form in the evaluation chain |

TraceAbove | False | whether to show evaluation chains leading to the evaluation chain containing form |

TraceOriginal | False | whether to look at expressions before their heads and arguments are evaluated |

Additional options for Trace.

When you use Trace to study the execution of a program, there is an issue about how local variables in the program should be treated. As discussed in "How Modules Work", *Mathematica* scoping constructs such as Module create symbols with new names to represent local variables. Thus, even if you called a variable in the original code for your program, the variable may effectively be renamed when the program is executed.

Trace[expr, form] is set up so that by default a symbol x that appears in form will match all symbols with names of the form that arise in the execution of expr. As a result, you can for example use Trace[expr, x=_] to trace assignment to all variables, local and global, that were named x in your original program.

Trace[expr,form,MatchLocalNames->False] | |

include all steps in the execution of expr that match form, with no replacements for local variable names allowed |

Preventing the matching of local variables.

In some cases, you may want to trace only the global variable x, and not any local variables that were originally named x. You can do this by setting the option MatchLocalNames->False.

In[50]:= |

Out[50]= |

In[51]:= |

Out[51]= |

The function Trace performs a complete computation, then returns a structure that represents the history of the computation. Particularly in very long computations, it is however sometimes useful to see traces of the computation as it proceeds. The function TracePrint works essentially like Trace, except that it prints expressions when it encounters them, rather than saving up all of the expressions to create a list structure.

The sequence of expressions printed by TracePrint corresponds to the sequence of expressions given in the list structure returned by Trace. Indentation in the output from TracePrint corresponds to nesting in the list structure from Trace. You can use the Trace options TraceOn, TraceOff and TraceForward in TracePrint. However, since TracePrint produces output as it goes, it cannot support the option TraceBackward. In addition, TracePrint is set up so that TraceOriginal is effectively always set to True.

Trace[expr,...] | trace the evaluation of expr, returning a list structure containing the expressions encountered |

TracePrint[expr,...] | trace the evaluation of expr, printing the expressions encountered |

TraceDialog[expr,...] | trace the evaluation of expr, initiating a dialog when each specified expression is encountered |

TraceScan[f,expr,...] | trace the evaluation of expr, applying f to HoldForm of each expression encountered |

Functions for tracing evaluation.

In[53]:= |

Out[53]= |

In[54]:= |

Out[54]= |

In[55]:= |

Out[55]= |

The function TraceDialog effectively allows you to stop in the middle of a computation and interact with the *Mathematica* environment that exists at that time. You can, for example, find values of intermediate variables in the computation, and even reset those values. There are, however, a number of subtleties, mostly associated with pattern and module variables.

What TraceDialog does is to call the function Dialog on a sequence of expressions. The Dialog function is discussed in detail in "Dialogs". When you call Dialog, you are effectively starting a subsidiary *Mathematica* session with its own sequence of input and output lines.

In general, you may need to apply arbitrary functions to the expressions you get while tracing an evaluation. TraceScan[f, expr, ...] applies f to each expression that arises. The expression is wrapped with HoldForm to prevent it from evaluating.

In TraceScan[f, expr, ...], the function f is applied to expressions before they are evaluated. TraceScan[f, expr, patt, fp] applies f before evaluation, and fp after evaluation.