Strange behavior with Inequality and pattern matching
The issue is pre-evaluation of the pattern. For the ones that evaluate to False
:
Inequality[___]
(* True *)
and
Inequality[1, _, x, LessEqual, 2]
(* Inequality[1, _, x] && x <= 2 *)
Neither of those evaluated forms will match
Inequality[1, Less, x, LessEqual, 2]
To fix this, merely add HoldPattern
. For instance,
MatchQ[Inequality[1, Less, x, LessEqual, 2], HoldPattern[Inequality[___]]]
(* True *)
Inactivate
is helpful in analyzing this problem.
Inactivate[
Column[
{MatchQ[Inequality[1, Less, x, LessEqual, 2], _Inequality],
MatchQ[Inequality[1, Less, x, LessEqual, 2], Inequality[___]],
MatchQ[
Inequality[1, Less, x, LessEqual, 2],
Inequality[1, Less, x, LessEqual, 2]],
MatchQ[
Inequality[1, Less, x, LessEqual, 2],
Inequality[_, Less, _, LessEqual, _]],
MatchQ[
Inequality[1, Less, x, LessEqual, 2],
Inequality[1, _, x, LessEqual, 2]]}],
MatchQ]
The above results show clearly why the second and last forms evaluate to False
. They also suggests a work-around.
Inactivate[
MatchQ[
Inequality[1, Less, x, LessEqual, 2],
Inequality[1, Less, x, LessEqual, 2]],
Inequality]
True
Inactivate[
MatchQ[
Inequality[1, Less, x, LessEqual, 2],
Inequality[1, Less, _, LessEqual, 2]],
Inequality]
True