# Why does this pattern not match?

Why does the following match?

MatchQ[HoldPattern[f], HoldPattern[_]]

True

But the following does not:

MatchQ[HoldPattern[f], HoldPattern[f]]

False

Or:

MatchQ[HoldPattern[f[1]], HoldPattern[f[_]]]

False
• To match an expression itself is a pattern, you'll need Verbatim to "inactivate" pattern expression to make it match, something like MatchQ[HoldPattern[f], Verbatim[HoldPattern[f]]]. Commented Nov 4, 2023 at 17:24
• Thank you, Verbatim[HoldPattern[f]] is the pattern that works, my unsuccessful attempt was HoldPattern[HoldPattern]. Commented Nov 4, 2023 at 19:13

The first one matches because HoldPattern just prevents the pattern from evaluation, so it is equivalent to

MatchQ[HoldPattern[f], _]
(* True *)

The second one does not match because it is, again, equivalent to

MatchQ[HoldPattern[f], f]
(* False *)

What you are probably looking for is Verbatim.

MatchQ[HoldPattern[f], Verbatim[HoldPattern[f]]]
(* True *)
• I still don't understand. The job of HoldPattern is to extract the pattern of an expression without actually evaluating it, in that sense f == _ , f == f and f[1]==f[ _ ] .There is still something I am missing. Commented Nov 4, 2023 at 20:58
• @EhabShoubaki, yes, but in the code, it is not f compared to f, but instead HoldPattern[f] compared to f, which is obviously not the same. HoldPattern in the first argument does not have any special meaning, it is just this exact expression. Commented Nov 4, 2023 at 21:38
• This is good but I think the following variant might be preferrable. In[82]:= MatchQ[HoldPattern[f], Verbatim[HoldPattern][f]] Out[82]= True It makes no difference in this example, but there can be cases where it is only the HoldPattern that should be verbatimmed (to make up a term). Commented Nov 4, 2023 at 21:59
• @DanielLichtblau, thanks for your valuable comment! Can you perhaps make a simple example so that I can include it in the answer? Commented Nov 5, 2023 at 0:20
• @Domen , so the way to understand this is that MatchQ expects an "expression" in the first argument, and HoldPattern would still be the head of that expression, while for the second argument it expects a "pattern", which it extracts from inside the HoldPattern unevaluated and discards HoldPattern head. Verbatim forces MatchQ to keep that head. Commented Nov 5, 2023 at 0:26