Trace
Details and Options
- In general, form in Trace[expr,form] is compared both with each complete expression that is evaluated and with the tag associated with any transformation rule used in the evaluation.
- Trace[expr,lhs->rhs] picks out expressions that match lhs, then replaces them with rhs in the list returned. »
- All expressions in the list returned by Trace are wrapped in HoldCompleteForm. »
- Trace returns a set of nested lists. Each individual list corresponds to a single evaluation chain, which contains the sequence of forms found for a particular expression. The list has sublists that give the histories of subsidiary evaluations.
- The following options can be given:
-
MatchLocalNames True whether to allow x to stand for x$nnn TraceAbove False whether to show evaluation chains that contain the chain containing form TraceBackward False whether to show expressions preceding form in the evaluation chain TraceDepth Infinity how many levels of nested evaluations to include TraceForward False whether to show expressions following form in the evaluation chain TraceOff None forms within which to switch off tracing TraceOn _ forms within which to switch on tracing TraceOriginal False whether to look at expressions before their heads and arguments are evaluated - During the execution of Trace, the settings for the form argument, and for the options TraceOn and TraceOff, can be modified by resetting the values of the global variables $TracePattern, $TraceOn and $TraceOff, respectively. »
Examples
open all close allBasic Examples (3)
Trace a single-step evaluation:
Trace[1 + 2]Trace each branch in an evaluation:
Trace[2 3 + 4 5 + 6 7]Trace only the computations associated with the symbol Times:
Trace[2 3 + 4 5 + 6 7, Times]Show only expressions with head Times:
Trace[2 3 + 4 5 + 6 7, _Times]Scope (11)
Standard Evaluation (5)
Trace an empty evaluation chain:
Trace[1]Trace evaluations resulting from local definitions:
a = 1;b = 2;f[x_, y_] := x + y;Trace[f[a, b]]Trace the operation of Map:
Trace[Map[Cos, {0, π, 2π}]]Trace all expressions used in an evaluation:
Trace[(2 + 3) ^ (2 + 3) + (4 + 5) ^ (4 + 5)]Include only those expressions that match _Plus:
Trace[(2 + 3) ^ (2 + 3) + (4 + 5) ^ (4 + 5), _Plus]Include all evaluations that use transformation rules associated with the symbol Plus:
Trace[(2 + 3) ^ (2 + 3) + (4 + 5) ^ (4 + 5), Plus]Obtain evaluations in a trace that match a pattern:
fib[1] = fib[2] = 1;fib[n_] := fib[n - 1] + fib[n - 2];Trace[fib[5], fib[n_]]Apply a transformation rule to each match in order to remove the head fib:
Trace[fib[5], fib[n_] :> n]Nonstandard Evaluation (6)
Trace the evaluation of conditionals such as If; the 2+2 in the False branch is never evaluated:
Trace[If[EvenQ[2], 1 + 1, 2 + 2], TraceOriginal -> Automatic]Trace the evaluation of logical operations such as And; notice that OddQ[5] is not evaluated:
Trace[OddQ[3] && OddQ[4] && OddQ[5], TraceOriginal -> Automatic]Trace the evaluation of iteration functions such as Do:
Trace[Do[n ^ 2, {n, 1 + 1, 2 + 2}], TraceOriginal -> Automatic]Trace each step in CompoundExpression (;), which has the attribute HoldAll. It evaluates each argument in turn inside the body:
Trace[1 + 1;2 + 2, TraceOriginal -> Automatic]Define a function with no attributes that, like CompoundExpression, returns its last argument:
f[most___, last_] := Identity[last]f[…] evaluates all the arguments prior to entering the body, and then returns the last argument:
Trace[f[1 + 1, 2 + 2], TraceOriginal -> Automatic]Trace the steps in an evaluation where a head with holding attributes is replaced with a head without them:
Trace[List@@Join[Hold[1 + 1], Hold[2 + 2]], TraceOriginal -> Automatic]While the result is the same, the order of evaluation is quite different if there are no holding attributes to begin with:
Trace[List@@Join[gold[1 + 1], gold[2 + 2]], TraceOriginal -> Automatic]The function FoldList has no holding attributes:
Attributes[FoldList]Internally, it uses nonstandard evaluation to compute the sequence of steps f[x0,x1], f[f[x0,x1],x2], …:
Trace[FoldList[Plus, 0, {3, 2, 1}]]Generalizations & Extensions (3)
Modify the setting for the form argument during the execution of Trace[expr,form] by resetting the value of the global variable $TracePattern:
Trace[(1 + 1) ^ (2 + 2);$TracePattern = Power;(1 + 1) ^ (2 + 2), _]Modify the setting for the TraceOn option during the execution of Trace by resetting the value of the global variable $TraceOn:
fac[1] = 1;fac[n_] := n fac[n - 1];Trace[fac[4];$TraceOn = _;fac[4], TraceOn -> _Plus]Modify the setting for the TraceOff option during the execution of Trace by resetting the value of the global variable $TraceOff:
fac[1] = 1;fac[n_] := n fac[n - 1];Trace[fac[4];$TraceOff = _fac;fac[4]]Options (8)
MatchLocalNames (1)
By default, symbols such as x match symbols with local names of the form x$nnn:
f[x_] := 1 / (1 + x ^ 2)Trace[f[x] + Module[{x}, f[x]], f[x]]With MatchLocalNamesFalse, only an explicit match of x will show up:
Trace[f[x] + Module[{x}, f[x]], f[x], MatchLocalNames -> False]TraceAbove (1)
A recursive definition for finding Fibonacci numbers:
fib[1] = fib[2] = 1;fib[n_] := fib[n - 1] + fib[n - 2];Show only the sums of fib that are encountered:
Trace[fib[4], fib[x_] + fib[y_]]Show the beginning and the end of the evaluation chain that leads to each sum of fib:
Trace[fib[4], fib[x_] + fib[y_], TraceAbove -> True]Show the entire evaluation chain that leads to each sum of fib:
Trace[fib[4], fib[x_] + fib[y_], TraceAbove -> All]Show the evaluation chain leading to the sum, but not what comes after the sum:
Trace[fib[4], fib[x_] + fib[y_], TraceAbove -> {All, False}]TraceBackward (1)
A recursive definition for finding Fibonacci numbers:
fib[1] = fib[2] = 1;fib[n_] := fib[n - 1] + fib[n - 2];Show only what additions of positive integers are required:
Trace[fib[3], Plus[__Integer ? Positive]]Show the beginning of the evaluation chain that leads to each addition:
Trace[fib[3], Plus[__Integer ? Positive], TraceBackward -> True]Show all intermediate evaluations that led to each addition:
Trace[fib[3], Plus[__Integer ? Positive], TraceBackward -> All]TraceDepth (1)
A recursive definition for finding Fibonacci numbers:
fib[1] = fib[2] = 1;fib[n_] := fib[n - 1] + fib[n - 2];Trace only evaluations through level 3:
Trace[fib[10], TraceDepth -> 3]fib[6] and fib[7], the starts of the deepest chains, are at level 3 of the result:
Position[%, HoldCompleteForm[fib[6 | 7]]]TraceForward (1)
A recursive definition for finding Fibonacci numbers:
fib[1] = fib[2] = 1;fib[n_] := fib[n - 1] + fib[n - 2];Show only what evaluations of fib are encountered:
Trace[fib[4], _fib]Show only the evaluations of fib and the results:
Trace[fib[4], _fib, TraceForward -> True]Show all intermediate evaluations between calls of fib and the result:
Trace[fib[4], _fib, TraceForward -> All]TraceOff (1)
TraceOn (1)
TraceOriginal (1)
Trace evaluation of an expression showing evaluation chains for expressions that change:
Trace[f[1, 2 + 3]]Show evaluation chains even for expressions that do not change:
Trace[f[1, 2 + 3], TraceOriginal -> True]Show the starting point of each evaluation chain, but drop chains that do not change:
Trace[f[1, 2 + 3], TraceOriginal -> Automatic]Applications (2)
As explained in Evaluation of Expressions, heads are evaluated first, then arguments are evaluated in some order depending whether the head has one of the attributes HoldFirst, HoldRest, HoldAll or HoldAllComplete. Trace all steps in the HoldAll function CompoundExpression, including the evaluation of heads:
Trace[1 + 1;2 + 2, TraceOriginal -> True]Combine a function with attribute HoldAll (CompoundExpression), one with attribute HoldFirst (Set) and one with no holding attributes (Plus):
Trace[a = 2;b = 3;a + b, TraceOriginal -> True]Trace the evaluation of assignments that use SetDelayed, which has attribute HoldAll:
Trace[f[x_] := x ^ 2, TraceOriginal -> True]Compare with making the assignment using Set, which has attribute HoldFirst; the right-hand side is evaluated prior to the creation of the pattern:
Trace[g[x_] = x ^ 2, TraceOriginal -> True]Properties & Relations (4)
Trace gives a nested list of expressions wrapped in HoldCompleteForm:
InputForm[Trace[2 ^ 3 + 4]]When form is not a symbol, Trace[expr,form] includes only those evaluations that match form:
fib[1] = fib[2] = 1;fib[n_] := fib[n - 1] + fib[n - 2];Trace[fib[4], _fib]This corresponds to deleting all evaluations that do not match form, then deleting empty evaluation chains:
Trace[fib[4]]Delete all non-matching evaluations:
DeleteCases[%, HoldCompleteForm[Except[_fib]], Infinity]Remove empty evaluation chains, keeping the matching evaluations unchanged:
ReplaceRepeated[{h_HoldCompleteForm :> h, {} -> Nothing}] /@ %% === Trace[fib[4], _fib]When form is a symbol, Trace[expr,form] includes not only those evaluations that match form, but also those evaluations that match the left-hand side of a rule for any sort of evaluation definition associated with form as well as the result of applying those rules:
fib[1] = fib[2] = 1;fib[n_] := fib[n - 1] + fib[n - 2];Trace[Identity[fib][4], fib]Attributes[Trace]This means the pattern will not be evaluated prior to tracing:
s = Plus;
Trace[1 + 1, s]Use Evaluate or similar techniques to force evaluation prior to entering Trace:
Trace[1 + 1, Evaluate[s]]See Also
TraceDialog TracePrint TraceScan Monitor EvaluationMonitor Stack Tr
Function Repository: TraceInteractive
Tech Notes
Related Guides
Related Workflows
- Get a Stack Trace
History
Introduced in 1991 (2.0) | Updated in 2026 (15.0)
Text
Wolfram Research (1991), Trace, Wolfram Language function, https://reference.wolfram.com/language/ref/Trace.html (updated 2026).
CMS
Wolfram Language. 1991. "Trace." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2026. https://reference.wolfram.com/language/ref/Trace.html.
APA
Wolfram Language. (1991). Trace. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Trace.html
BibTeX
@misc{reference.wolfram_2026_trace, author="Wolfram Research", title="{Trace}", year="2026", howpublished="\url{https://reference.wolfram.com/language/ref/Trace.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_trace, organization={Wolfram Research}, title={Trace}, year={2026}, url={https://reference.wolfram.com/language/ref/Trace.html}, note=[Accessed: 13-June-2026]}