Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question
    
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? –  Myridium Jun 1 at 3:31

2 Answers 2

You have to use "===" like

 "string" === False

Or

SameQ["string",False]
share|improve this answer
1  
I think OP probably knows this. The question is, why doesn't Equal give the same result? SameQ doesn't express a mathematical relation. –  Oleksandr R. May 31 at 15:11
2  
@OleksandrR. Quite frankly: I don't know, all the more so since "string"==1 returns False. –  eldo May 31 at 15:36
1  
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. –  Oleksandr R. May 31 at 16:37
1  
@OleksandrR. FWIW: Assuming["string" == False, Refine["string" == False]] calls PolynomialReduce[-"string" + False, {-"string" + False}, {False}, MonomialOrder -> DegreeReverseLexicographic], –  Michael E2 Jun 1 at 0:19

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 *)
share|improve this answer
    
Interesting. What would be the definition for "determinate"? –  eldo May 31 at 20:55
    
I suppose String or one of the number types. I don't think they have a name. It's not, for example, "atomic." –  Michael E2 May 31 at 21:23
    
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. –  m_goldberg May 31 at 21:51
    
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. –  Oleksandr R. May 31 at 21:53
    
"foo" == 1 giving False while False == 1 remains unevaluated is a puzzling case, however. –  Oleksandr R. May 31 at 21:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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