Is there a way to create a Mathematica pattern which matches expressions whose heads may be arbitrarily deep, i.e. something like f[___][___][___]...
?
There seems to be no built-in construct to pattern-test nested heads automatically. We can achieve the goal by writing a function which would, for any given (sub)expression of the form f[___]...[___]
, efficiently determine f
(which, with a slight abuse of terminology, we may call a symbolic head for the expression). Here is the code:
ClearAll[shead];
SetAttributes[shead, HoldAllComplete];
shead[expr_] := Scan[Return, Unevaluated[expr], {-1}, Heads -> True];
Here is how it can be used (I will use the same set of tests as @Sasha):
In[105]:= Cases[{f[1], g[f[1]], f[1, 2, 3][1], f[1][2][3][4]}, x_ /; shead[x] === f]
Out[105]= {f[1], f[1, 2, 3][1], f[1][2][3][4]}
If you prefer to use the syntax suggested by @Sasha, that version would look like
Clear[headPattern];
headPattern[head_] := _?(Function[Null, shead[#] === head, HoldFirst]);
In[108]:= Cases[{f[1], g[f[1]], f[1, 2, 3][1], f[1][2][3][4]}, headPattern[f]]
Out[108]= {f[1], f[1, 2, 3][1], f[1][2][3][4]}
Here are some hints for the logic that lead to this solution, and how it works. The solution will be most concise and efficient if we manage to leverage some of the built-in expression-traversal functions. Some that come to mind are Map
, Scan
,Cases
,MapIndexed
,Position
. Given that we need the heads, we'd need to pass the Heads->True
option. I used Scan
, since this one is easy to stop at any point (unlike other mentioned constructs, for which you'd typically need to throw an exception to stop them "in the middle", which is rather inelegant and induces some overhead as well) as soon as we find what we want. Our result will be the very first thing Scan
finds on its depth-first expression traversal, so it is expected to be very efficient (it does not traverse the entire expression).
Another comment is on evaluation. You can see that HoldAllComplete
attribute is used in shead
, and Unevaluated
is used in its body. These are very important - they serve to prevent possible evaluation of expressions passed to the function. It may matter in cases like this:
In[110]:= m = n = 0;
g[x_] := n++;
h[x_] := m++;
{Cases[Hold[f[g[1]][h[2]]], x_ /; shead[x] === f :> Hold[x], Infinity], {m, n}}
Out[113]= {{Hold[f[g[1]][h[2]]]}, {0, 0}}
Here, we see what we'd expect - even though Cases
has been traversing the entire expression and feeding its (sub)parts to shead
, no evaluation of sub-parts was triggered by shead
. Now we define a naive version of shead
which "leaks evaluation":
sheadEval[expr_] := Scan[Return, expr, {-1}, Heads -> True]
And now,
In[114]:= {Cases[Hold[f[g[1]][h[2]]], x_ /; sheadEval[x] === f :> Hold[x], Infinity], {m, n}}
Out[114]= {{Hold[f[g[1]][h[2]]]}, {2, 1}}
The latter behavior is unsatisfactory generally. The whole code-is-data paradigm, so useful in meta - programming, is very powerful in Mathematica because you can use rules to destructure code. Possible (unwanted) evaluation during the pattern- matching would greatly impair it. The whole problem is in the sub-parts. Wrapping Hold
only prevents the whole expression from evaluation. Functions like Cases
and other similar functions for code destructuring are so great because they don't evaluate sub-parts when doing the structural (syntactic) matching.
The last comment here (mostly about definitions) is that the shead
function returns not exactly what is normally called symbolic head in Mathematica. The difference is for atomic expressions. For example, shead[f]
returns f
, while for atomic expressions, the true symbolic head should coincide with the head of an expression (Symbol
in this case). I have developed the symbolicHead
function with this behavior here, and that one can also be successfully used in place of shead
in the above, although shead
is more efficient.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With