# Wolfram Function Repository

Instant-use add-on functions for the Wolfram Language

Function Repository Resource:

Generate a graph of all expressions in an evaluation chain

Contributed by:
Ian Ford (Wolfram Research)

ResourceFunction["TraceGraph"][ generates a graph of all expressions used in the evaluation of | |

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

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

In general, *form* in ResourceFunction["TraceGraph"][*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["TraceGraph"][*expr*,*lhs*→*rhs*] picks out expressions that match *lhs*, then replaces them with *rhs* in the graph returned.

All expressions in the graph returned by ResourceFunction["TraceGraph"] are wrapped in HoldForm.

ResourceFunction["TraceGraph"] returns a rooted directed acyclic graph. Each branch corresponds to a single evaluation chain, which contains the sequence of forms found for a particular expression. Vertices have branches that give histories of subsidiary evaluations.

A round 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. Solid edges connect such a node to the first node in each of its subsidiary evaluation chains.

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. A dashed edge connects the last node in a subsidiary evaluation chain to the following node in its parent evaluation chain.

Highlighted dashed edges are used for transformation rules.

ResourceFunction["TraceGraph"] takes the same options as Graph, with the following changes:

Additional options for ResourceFunction["TraceGraph"] 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 TraceGraph[*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]= |

Trace the evaluation of control structures such as CompoundExpression:

In[43]:= |

Out[43]= |

Trace the evaluation of conditionals such as If:

In[44]:= |

Out[44]= |

Trace the evaluation of logical operations such as And:

In[45]:= |

Out[45]= |

Trace the evaluation of iteration functions such as Do:

In[46]:= |

Out[46]= |

Trace the evaluation of assignments such as SetDelayed:

In[47]:= |

Out[47]= |

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

In[48]:= |

In[49]:= |

Out[49]= |

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

In[50]:= |

Out[50]= |

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

In[51]:= |

Out[51]= |

In[52]:= |

Out[52]= |

In[53]:= |

Out[53]= |

In[54]:= |

Out[54]= |

TraceGraph gives a graph with vertices of the form Trees`TraceNode[HoldForm[*expr*],*pos*]:

In[55]:= |

Out[55]= |

In[56]:= |

Out[56]= |

TraceGraph uses highlighted dashed edges for a sequence of evaluations:

In[57]:= |

In[58]:= |

Out[58]= |

In contrast, TraceTree shows successive evaluations as siblings:

In[59]:= |

Out[59]= |

TraceGraph uses solid and dashed edges for increasing and decreasing evaluation levels, respectively:

In[60]:= |

Out[60]= |

The levels given by TraceTree correspond to the evaluation levels of subexpressions:

In[61]:= |

Out[61]= |

Each step in the evaluation of an expression at level *n* is represented as Trees`TraceNode[*expr*,{*i*_{1},…,*i*_{n}}]:

In[62]:= |

In[63]:= |

Out[63]= |

In[64]:= |

Out[64]= |

In[65]:= |

Out[65]= |

The result of evaluation at level *n* is represented as Trees`TraceNode[*expr*,{*i*_{1},…,*i*_{n-1}}]:

In[66]:= |

Out[66]= |

In[67]:= |

Out[67]= |

In[68]:= |

Out[68]= |

Trees`TraceNode[Null,{…,1}] represents the first node in an evaluation chain when the first expression is not included:

In[69]:= |

Out[69]= |

In[70]:= |

Out[70]= |

In[71]:= |

Out[71]= |

Trees`TraceNode[Null,{…,-1}] represents the last node in evaluation chain when the last expression is not included:

In[72]:= |

Out[72]= |

In[73]:= |

Out[73]= |

In[74]:= |

Out[74]= |

- 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

- 2.0.0 – 06 September 2023
- 1.0.0 – 15 July 2022

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