Question about TraceScan

The main topic of this question is the logic of the first argument of TraceScan (as well as the fourth argument associated with it, but which is not needed for the problem under consideration): it sometimes excludes some evaluation steps (which Trace with the option TraceOriginal->True gives), but sometimes it includes them, as shown in the following examples. I am interested in understanding the logic of this behavior and how to get TraceScan to give a complete set of evaluation steps. This question originally arose in this thread (see My comments on the answer). A general comparison of the behavior of TraceScan compared to Trace given by WReach here , but it does not answer the following questions:

1.) Why does TraceScan not give the final expression f[a,1] in this if Trace gives:

 In[1]:= SetAttributes[traceScan,HoldAll]; traceScan[expr_]:=(list={};TraceScan[AppendTo[list,#]&,expr];list) In[3]:= ClearAll[f,a]; Trace[f[a,1],TraceOriginal->True] Out[4]= {f[a,1],{f},{a},{1},f[a,1]} In[5]:= ClearAll[f,a]; traceScan[f[a,1]] Out[6]= {f[a,1],f,a,1} 

2.) And in the following case, why both Trace and TraceScan give the final expression f[1,a] , where only Trace gives the intermediate expression f[a,1] , which corresponds to the evaluation step before applying the Orderless f attribute:

 In[7]:= ClearAll[f,a]; SetAttributes[f,Orderless] Trace[f[a,1],TraceOriginal->True] Out[9]= {f[a,1],{f},{a},{1},f[a,1],f[1,a]} In[12]:= ClearAll[f,a]; SetAttributes[f,Orderless] traceScan[f[a,1]] Out[14]= {f[a,1],f,a,1,f[1,a]} 

3.) And why, in this last case, both Trace and TraceScan give the final expression ff[1,b] and the intermediate expression ff[b,1] , which corresponds to the evaluation step before applying the Orderless ff attribute:

 In[21]:= ClearAll[f,ff,a]; SetAttributes[ff,Orderless];f=ff;a=b; Trace[f[a,1],TraceOriginal->True] Out[23]= {f[a,1],{f,ff},{a,b},{1},ff[b,1],ff[1,b]} In[24]:= ClearAll[f,ff,a]; SetAttributes[ff,Orderless];f=ff;a=b; traceScan[f[a,1]] Out[26]= {f[a,1],f,ff,a,b,1,ff[b,1],ff[1,b]} 

4.) Is there a way to get TraceScan always give comprehensive assessment information like Trace does?


Besides

Here is another, more informative version of TraceScan that uses the fourth argument:

 SetAttributes[traceScan, HoldAll]; traceScan[expr_] := (list1 = list2 = {}; TraceScan[AppendTo[list1, #] &, expr, _, AppendTo[list2, {##}]&]; Column[{list1, list2}]) 
+4
source share
1 answer

Cases 1 and 2. Citation Documentation :

Usually, ... Trace intercepts expressions only after function arguments have been evaluated. By setting TraceOriginal->True , you can get Trace also see the expressions before the function arguments were evaluated.

It seems that setting TraceOriginal->True just leads to adding extra information to the default output of Trace . As a result, we see an unnecessary additional “intermediate expression” f[a,1] , which does not really appear in the value chains in both cases, as shown in the figure of TraceScan and TracePrint :

 In[1]:= ClearAll[f, a]; SetAttributes[f, Orderless] TracePrint[f[a, 1]] During evaluation of In[1]:= f[a,1] During evaluation of In[1]:= f During evaluation of In[1]:= a During evaluation of In[1]:= 1 During evaluation of In[1]:= f[1,a] Out[3]= f[1, a] 

So it looks like a bug in Trace .

Case 3. In this case, everything works as expected, because the intermediate expression ff[b,1] really appears in the evaluation chain as a result of applying the definition associated with f . Nothing unexpected.

Conclusion Both Trace and TraceScan provide comprehensive information about the evaluation chain, but the output generated by Trace may additionally contain a misleading "intermediate expression", which is actually just the initial expression of the chain.

+1
source

All Articles