Why does Equal["string", False] not evaluate to False? I would have thought Mathematica could prove that these two things are not equal.

  • $\begingroup$ I would think that Mathematica is unable to compare a string with a boolean; it's non-sensical. Does this usually work when comparing distinct types of variables? $\endgroup$
    – Myridium
    Jun 1 '14 at 3:31

I think it is because Equal treats symbols as having an indeterminate value, but as @M_goldberg points out this isn't the whole story.

Equal["foo", foo]
(* "foo" == foo *)

This is true when one side is numeric (but not a number per se) and the other is a string:

Equal["foo", GoldenRatio]
(* "foo" == GoldenRatio *)

Equal["foo", Sqrt[2]]  (* Sqrt can be overridden *)
(* "foo" == Sqrt[2] *)

Equal[Sqrt, Sqrt[2]]
(* Sqrt == Sqrt[2] *)

Equal also chokes on unevaluated, nonnumeric function expressions, including nonsense such as this:

Equal[2, 2[2]]
(* 2 == 2[2] *)

Equal will evaluate if both sides are numeric; also if both sides are determinate, such as comparing a string and a number. It also evaluates to True if the symbols are the same, or if one is True and the other is False. (There may be other exceptions to this rule. Thanks to @m_goldberg for point out these examples.)

Equal["foo", N@Sqrt[2]]
(* False *)

Equal[True, False]
(* False *)

Equal[x, x]
(* True *)

Override Sqrt:

Block[{Sqrt = "foo" &},
 Equal["foo", Sqrt[2]]
(* True *)
  • $\begingroup$ Interesting. What would be the definition for "determinate"? $\endgroup$
    – eldo
    May 31 '14 at 20:55
  • $\begingroup$ I suppose String or one of the number types. I don't think they have a name. It's not, for example, "atomic." $\endgroup$
    – Michael E2
    May 31 '14 at 21:23
  • $\begingroup$ It can't be as simple as "Equal treats symbols as having an indeterminate value". Consider, Equal[False, #] & /@ {True, False} which returns {False, True}. Further, Equal clearly calls on SameQ and returns True if SameQ does; i.e., Equal[x, x] is always True for any x, including symbols. $\endgroup$
    – m_goldberg
    May 31 '14 at 21:51
  • $\begingroup$ Equal also considers True and False to be comparable with each other. As far as I can tell, it always remains unevaluated when the comparands are not in the same category, but what it considers a category is unclear. However it does seem that strings are in the same category as (unassigned) symbols as far as Equal is concerned, even if other functions treat them as distinct. x == x and "x" == "x" both give True. It's perhaps not as simple as falling through to SameQ, as the example in my comment under the other answer shows. $\endgroup$ May 31 '14 at 21:53
  • $\begingroup$ "foo" == 1 giving False while False == 1 remains unevaluated is a puzzling case, however. $\endgroup$ May 31 '14 at 21:58

You have to use "===" like

 "string" === False


  • 2
    $\begingroup$ I think OP probably knows this. The question is, why doesn't Equal give the same result? SameQ doesn't express a mathematical relation. $\endgroup$ May 31 '14 at 15:11
  • 2
    $\begingroup$ @OleksandrR. Quite frankly: I don't know, all the more so since "string"==1 returns False. $\endgroup$
    – eldo
    May 31 '14 at 15:36
  • 1
    $\begingroup$ Indeed, it's a puzzling inconsistency and IMO a good question. The answer seems to be something to do with strings being treated as symbols in some contexts but not others. For example, Solve can't solve with respect to strings, but Assuming["string" == False, Refine["string" == False]] works the same with a string as it would with any other symbol. $\endgroup$ May 31 '14 at 16:37
  • 1
    $\begingroup$ @OleksandrR. FWIW: Assuming["string" == False, Refine["string" == False]] calls PolynomialReduce[-"string" + False, {-"string" + False}, {False}, MonomialOrder -> DegreeReverseLexicographic], $\endgroup$
    – Michael E2
    Jun 1 '14 at 0:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.