# Wolfram Function Repository

Instant-use add-on functions for the Wolfram Language

Function Repository Resource:

Generate a tree of all expressions in an evaluation chain

Contributed by:
Ian Ford (Wolfram Research)

ResourceFunction["TraceTree"][ generates a tree of all expressions used in the evaluation of | |

ResourceFunction["TraceTree"][ includes only those expressions that match | |

ResourceFunction["TraceTree"][ includes all evaluations that use transformation rules associated with the symbol |

In general, *form* in ResourceFunction["TraceTree"][*expr*,*form*] is compared both with each complete expression that is evaluated and with the tag associated with an transformation rule used in the evaluation.

ResourceFunction["TraceTree"][*expr*,*lhs*→*rhs*] picks out expressions that match *lhs*, then replaces them with *rhs* in the tree returned.

All expressions in the tree returned by ResourceFunction["TraceTree"] are wrapped in HoldForm.

ResourceFunction["TraceTree"] returns a Tree object. Each non-leaf subtree corresponds to a single evaluation chain, which contains the sequence of forms found for a particular expression. Subtrees have branches that give histories of subsidiary evaluations.

A rounded rectangular node is used for the first form in an evaluation chain. An unlabeled circular node is used as a placeholder when the first form is not included.

Rectangular nodes are used for all forms in an evaluation chain after the first form. An unlabeled square node is used as a placeholder when the last form is not included.

Options for ResourceFunction["TraceTree"] include:

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 |

Trace each step in an evaluation:

In[1]:= |

Out[1]= |

Trace only the computations with head Times:

In[2]:= |

Out[2]= |

Trace an empty evaluation chain:

In[3]:= |

Out[3]= |

Trace a single step evaluation:

In[4]:= |

Out[4]= |

Trace each branch in an evaluation:

In[5]:= |

Out[5]= |

Trace evaluations given by definitions:

In[6]:= |

In[7]:= |

Out[7]= |

Trace each step in an evaluation:

In[8]:= |

Out[8]= |

Trace the operation of FoldList:

In[9]:= |

Out[9]= |

Trace the steps in a non-standard evaluation:

In[10]:= |

Out[10]= |

Trace each step in an evaluation:

In[11]:= |

Out[11]= |

Include only those expressions that match _Plus:

In[12]:= |

Out[12]= |

Trace the computations with head Plus:

In[13]:= |

Out[13]= |

Apply a transformation rule to expressions that match a pattern:

In[14]:= |

In[15]:= |

Out[15]= |

Modify the setting for the *form* argument during the execution of TraceTree[*expr*,*form*] by resetting the value of the global variable $TracePattern:

In[16]:= |

Out[16]= |

By default, symbols such as *x* match symbols with local names of the form *x*$*nnn*:

In[17]:= |

In[18]:= |

Out[18]= |

With MatchLocalNames→False, only an explicit match of *x* will show up:

In[19]:= |

Out[19]= |

A recursive definition for finding Fibonacci numbers:

In[20]:= |

Show only what sums of fib are encountered:

In[21]:= |

Out[21]= |

Show the beginning of the evaluation chain that leads to each sum of fib:

In[22]:= |

Out[22]= |

Show the entire evaluation chain that leads to each sum of fib:

In[23]:= |

Out[23]= |

A recursive definition for finding Fibonacci numbers:

In[24]:= |

Show only what additions of positive integers are required:

In[25]:= |

Out[25]= |

Show the beginning of the evaluation chain that leads to each addition:

In[26]:= |

Out[26]= |

Show all intermediate evaluations that led to each addition:

In[27]:= |

Out[27]= |

A recursive definition for finding Fibonacci numbers:

In[28]:= |

Trace only evaluations through depth 3:

In[29]:= |

Out[29]= |

Trace all evaluations:

In[30]:= |

Out[30]= |

A recursive definition for finding Fibonacci numbers:

In[31]:= |

Show only what evaluations of fib are encountered:

In[32]:= |

Out[32]= |

Show only the evaluations of fib and the results:

In[33]:= |

Out[33]= |

Show all intermediate evaluations between calls of fib and the result:

In[34]:= |

Out[34]= |

Trace evaluation of an expression that evaluates a function *g*:

In[35]:= |

In[36]:= |

Out[36]= |

Omit evaluations required to get the values of *g*:

In[37]:= |

Out[37]= |

Trace evaluation of an expression that evaluates a function *g*:

In[38]:= |

In[39]:= |

Out[39]= |

Trace only evaluation inside of *g*:

In[40]:= |

Out[40]= |

Trace evaluation of an expression showing evaluation chains for expressions that change:

In[41]:= |

Out[41]= |

Show evaluation chains even for expressions that do not change:

In[42]:= |

Out[42]= |

TraceTree[*expr*] traces each step in the evaluation of *expr*:

In[43]:= |

In[44]:= |

Out[44]= |

TraceTree[*expr*,*form*] includes only those expressions that match *form*:

In[45]:= |

Out[45]= |

This corresponds to deleting all expressions that do not match *form*, then deleting empty evaluation chains:

In[46]:= |

Out[46]= |

In[47]:= |

Out[47]= |

In[48]:= |

Out[48]= |

In[49]:= |

Out[49]= |

All expressions in the tree returned by TraceTree are wrapped in HoldForm to prevent evaluation:

In[50]:= |

Out[50]= |

- Live CEOing Ep 616: Language Design in Wolfram Language [Multicomputation]–Wolfram Research YouTube
- Research Working Session: Tuesday, Apr. 5, 2022 [Multicomputation]–Wolfram Research YouTube
- Live CEOing Ep 572: Language Design in Wolfram Language [Multicomputation]–Wolfram Research YouTube
- Live CEOing Ep 569: Language Design in Wolfram Language [Multicomputation]–Wolfram Research YouTube
- Live CEOing Ep 565: Language Design in Wolfram Language [Multicomputation]–Wolfram Research YouTube

- 1.0.0 – 15 July 2022

This work is licensed under a Creative Commons Attribution 4.0 International License