While working on a solution to this question I've come across a case where I simply don't understand Mathematica's behaviour.
I've got the following definitions:
PatternImplies[(x:(Verbatim[Blank]
|Verbatim[BlankSequence]
|Verbatim[BlankNullSequence]))[h_],x[]]:=True
(*CatchAll rule*)
PatternImplies[_,_]:=Maybe;
Now I try this:
PatternImplies[_Integer,_]
(*
==> Maybe
*)
I would have expected True
. My first thought was that I probably got the pattern wrong, so I tested:
MatchQ[PatternImplies[_Integer,_],
PatternImplies[(x:(Verbatim[Blank]
|Verbatim[BlankSequence]
|Verbatim[BlankNullSequence]))[h_],x[]]]
(*
==> True
*)
In other words, the pattern matches. Moreover, looking at Downvalues
I see that the special rule is indeed stored before the catch-all rule.
So why does Mathematica chose the second definition (and more importantly, what can I do about it?)
Internal`ComparePatterns
. For instanceInternal`ComparePatterns[_Integer, _]
gives"Specific"
, i.e._Integer
is a special case of_
. $\endgroup$ComparePatterns
. So far it seems that this amazing function handles all my example cases posted there. $\endgroup$Internal`ComparePatterns
can actually address. $\endgroup$