The reason is that you have to escape the HoldPattern
, perhaps with Verbatim:
In[11]:= Cases[dvs,
Verbatim[RuleDelayed][
Verbatim[HoldPattern][HoldPattern[f[_Integer]]], _]]
Out[11]= {HoldPattern[f[1]] :> 1, HoldPattern[f[2]] :> 2}
There are just a few heads for which this is necessary, and HoldPattern
is one of them, precisely because it is normally "invisible" to the pattern-matcher. For your temporary
, or other heads, this wouldn't be necessary. Note by the way that the pattern f[_Integer]
is wrapped in HoldPattern
- this time HoldPattern
is used for its direct purpose - to protect the pattern from evaluation. Note that RuleDelayed
is also wrapped in Verbatim
- this is in fact another common case for Verbatim
- this is needed because Cases
has a syntax involving a rule, and we do not want Cases
to use this interpretation here. So, this is IMO an overall very good example to illustrate both HoldPattern
and Verbatim
.
Note also that it is possible to achieve the goal entirely with HoldPattern
, like so:
In[14]:= Cases[dvs,HoldPattern[HoldPattern[HoldPattern][f[_Integer]]:>_]]
Out[14]= {HoldPattern[f[1]]:>1,HoldPattern[f[2]]:>2}
However, using HoldPattern
for escaping purposes (in place of Verbatim
) is IMO conceptually wrong.
EDIT
To calrify a little the situation with Cases
, here is a simple example where we use the syntax of Cases
involving transformation rules. This extended syntax instructs Cases
to not only find and collect matching pieces, but also transform them according to the rules, right after they were found, so the resulting list contains the transformed pieces.
In[29]:= ClearAll[a, b, c, d, e, f];
Cases[{a, b, c, d, e, f}, s_Symbol :> s^2]
Out[30]= {a^2, b^2, c^2, d^2, e^2, f^2}
But what if we need to find elements that are themselves rules? If we just try this:
In[33]:= Cases[{a:>b,c:>d,e:>f},s_Symbol:>_]
Out[33]= {}
It doesn't work since Cases
interprets the rule in the second argument as an instruction to use extended syntax, find a symbol and replace it with _
. Since it searches on level 1 by default, and symbols are on level 2 here, it finds nothing. Observe:
In[34]:= Cases[{a:>b,c:>d,e:>f},s_Symbol:>_,{2}]
Out[34]= {_,_,_,_,_,_}
In any case, this is not what we wanted. Therefore, we have to force Cases
to consider the second argument as a plain pattern (simple, rather than extended, syntax). There are several ways to do that, but all of them "escape" RuleDelayed
(or Rule
) in some way:
In[37]:= Cases[{a:>b,c:>d,e:>f},(s_Symbol:>_):>s]
Out[37]= {a,c,e}
In[38]:= Cases[{a:>b,c:>d,e:>f},Verbatim[RuleDelayed][s_Symbol,_]:>s]
Out[38]= {a,c,e}
In[39]:= Cases[{a:>b,c:>d,e:>f},(Rule|RuleDelayed)[s_Symbol,_]:>s]
Out[39]= {a,c,e}
In all cases, we either avoid the extended syntax for Cases
(last two examples), or manage to use it to our advantage (first case).