Function Repository Resource:

TracePrintEvaluations

Source Notebook

Print all expressions used in the evaluation of an expression, along with the results of evaluating those expressions

Contributed by: Carl Woll

ResourceFunction["TracePrintEvaluations"][expr, form]

prints an in/out toggler for expressions that match form.

ResourceFunction["TracePrintEvaluations"][expr,s]

includes all evaluations that use transformation rules associated with the symbol s.

Details and Options

The output of ResourceFunction["TracePrintEvaluations"] looks very similar to the output of TracePrint. However, clicking one of the outputs toggles to the evaluated expression. The output is the result of evaluating expr:
ResourceFunction["TracePrintEvaluations"] indents its output in correspondence with the nesting levels for lists generated by Trace.
ResourceFunction["TracePrintEvaluations"] prints the forms of expressions before any of their elements are evaluated.
The output toggles between the unevaluated expressions and the actual result of evaluating those expressions during the evaluation of expr.
Right-clicking the trace output copies an unevaluated version of the displayed expression.
When the output has no visible form (e.g. an empty string or a Sequence[] object), the output is automatically modified to show the InputForm.
ResourceFunction["TracePrintEvaluations"][expr] returns the result of evaluating expr.
Loading ResourceFunction["TracePrintEvaluations"] automatically adds cell styles to the evaluation notebook. If the cell styles are absent from the notebook, the trace output includes the necessary styling information in each output cell, increasing the size of the notebook.
With the default option SaveDefinitionsFalse, trace cells do not include the evaluation information once the kernel has been quit.
Using the option SaveDefinitionsTrue stores the evaluated expressions of each trace cell in the result. The evaluated expressions persist even if the kernel has been quit, or the notebook has been closed.
The following options can be used to display the specified information using additional clicks:
"Timing"set to True to include timing of the traced evaluation
"TraceIn"function to apply to the traced input
"TraceOut"function to apply to the traced input/output pair
The option "TraceIndent" can be used to specify how to indent the input trace cells.

Examples

Basic Examples (2) 

Recursive Fibonacci definition:

In[1]:=
f[0] = 0;
f[1] = 1;
f[n_] := f[n - 1] + f[n - 2]

Use TracePrintEvaluations to track calls to f. Click the printed values to toggle between inputs and outputs:

In[2]:=
ResourceFunction["TracePrintEvaluations"][f[4], _f]
Out[2]=

Scope (2) 

See what DateList evaluated to during the following function:

In[3]:=
f[] := OddQ @ DateValue["Second"]

The TracePrintEvaluations trace cell includes the evaluation of the subexpression:

In[4]:=
ResourceFunction["TracePrintEvaluations"][f[], _DateValue]
Out[4]=

Options (8) 

SaveDefinitions (3) 

Using SaveDefinitionsTrue stores all of the evaluation expression in the result cell. Consider the recursive Fibonacci definition again:

In[5]:=
f[0] = 0;
f[1] = 1;
f[n_] := f[n - 1] + f[n - 2]

Using TracePrintEvaluations:

In[6]:=
ResourceFunction["TracePrintEvaluations"][f[4], _f, SaveDefinitions -> True]
Out[6]=

Notice the CellDingbat of the previous result includes a • character. This indicates that the trace cell evaluation information is stored in the result cell:

In[7]:=
Cases[NotebookRead @ PreviousCell[CellStyle -> "PersistentOutput"], RuleDelayed[DynamicModuleValues, rhs_] :> First @ rhs, Infinity] // InputForm
Out[9]=

Timing (1) 

Using the "Timing"True option includes evaluation timing information in each trace cell:

In[10]:=
ResourceFunction["TracePrintEvaluations"][Pause[.1]; Pause[.2]; 3, _Pause, "Timing" -> True]
Out[10]=

Click on any trace cell twice to see the evaluation timing.

TraceIn (1) 

Using the "TraceIn"func option evaluates func[subexpr], where subexpr is the subexpression matching the TracePrintEvaluations pattern, and include the result in the trace cell:

In[11]:=
ResourceFunction["TracePrintEvaluations"][
 1 + (2 + 3) + 4, _Plus, "TraceIn" -> Replace[HoldForm[s_] :> Length @ Unevaluated @ s]
 ]
Out[11]=

Click any trace cell twice to see the length of the Plus expression.

TraceIndent (1) 

The option "TraceIndent"string specifies the indentation to use to reflect the trace level:

In[12]:=
ResourceFunction[
 "TracePrintEvaluations"][(1 + (2 + (3 + (4 + 5)))), _Plus, "TraceIndent" -> "\[Bullet]"]
Out[12]=

TraceOut (2) 

Using the "TraceOut"func option evaluates func[subexpr,eval], where subexpr is the subexpression matching the TracePrintEvaluations pattern and eval is what the subexpression evaluated to, and include the result in the trace cell:

In[13]:=
ResourceFunction[
 "TracePrintEvaluations"][(1 + (2 + (3 + (4 + 5)))), _Plus, "TraceOut" -> Rule]
Out[13]=

Click any trace cell twice to see a rule indicating what each subexpression evaluated to.

Publisher

Carl Woll

Version History

  • 1.0.0 – 24 October 2019

Related Resources

License Information