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,
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,