To start with, Condition
has semantic meaning in Cases/MatchQ:
With[
{b = True},
Cases[{Condition[a, b], a}, Condition[a, b]]]
(* {a} *)
That explains why MatchQ[Condition[a, b], Condition[a, b]]
returns False
. Just for grins, let's try this:
MatchQ[3, 3 /; False]
(* False *)
Now let's work through your first example. A couple of variables to hold our expressions:
list1 = {a -> b /; c, a -> b, a};
alts = Alternatives @@ list1;
Start with something simpler:
Cases[list1, alts[[1]]]
(* {b /; c} *)
What happened? Well, in Cases
you can give a rule to actually replace the value that matched. alts[[1]]
is a -> b /; c
, so we matched on a
and replaced it with b/;c
. Similarly:
Cases[list1, alts[[2]]]
(* {b} *)
Next, something a bit more complicated:
Cases[list1, alts[[2 ;; 2]]]
(* {a -> b} *)
This time we matched against an Alternatives
, not a Rule
, so we don't do replacement. However, we did find an exact match.
Okay, moving on again:
Cases[list1, alts[[1 ;; 1]]]
(* {} *)
No matches! We're matching against Alternatives
, not Rule
, so we should be looking at the whole a -> b /; c
expression. The problem is that the Condition
still comes into play. Rather than try to explain directly, it's easier to find something that does match:
Cases[{a -> b}, Alternatives[a -> b /; True]]
(* {a -> b} *)
MatchQ[a -> b, a -> b /; True]
(* True *)
We're matching the b
part as long as the Condition[b, True]
is satisfied, which it is by definition. The condition of Condition[b, c]
is not satisfied, so we're not matching the b
.
Verbatim
is used in patterns where you want to match exactly--no transformation or interpretation. So, you could try this:
Cases[list1, Alternatives @@ Verbatim /@ list1]
(* {a -> b /; c, a -> b, a} *)
MatchQ[f[x, y], f[x, y]]
returnsTrue
whether or notf
has the attributeHoldAll
. $\endgroup$