9
$\begingroup$

The command

SameQ[1, Null]

returns

False

which is what I would expect, but the command

Equal[1, Null]

returns

1==Null

Why is Mathematica agnostic as to whether 1 is equal to Null ? Surely it is not ?

$\endgroup$
  • 2
    $\begingroup$ It does not evaluate if non numeric symbols are involved, unless both sides are exactly (full form) the same. "lhs==rhs returns False if lhs and rhs are determined to be unequal by comparisons between numbers or other raw data, such as strings. " $\endgroup$ – Kuba Nov 10 '17 at 9:30
  • 2
    $\begingroup$ It's not agnostic: Equal only evaluates arguments if they have numeric or string values. 1==Automatic and Null == Automatic are all left unevaluated. 1 == 1 + Null is left unevaluated just as 1 == 1 + x is, which perfectly makes sense, as you don't have a value for x (or for Null) yet. Think of it (as the doc states) that Equal tries to be as symbolic as possible, and does not evaluate symbols without values. $\endgroup$ – István Zachar Nov 10 '17 at 9:32
  • 4
    $\begingroup$ I think the deeper reason is that Mathematica does not treat Null as special in that case and thus behaves like it would for any unassigned symbol and returns unevaluated. $\endgroup$ – Albert Retey Nov 10 '17 at 9:32
  • $\begingroup$ Food for thought: if things like 3 == x + 2 evaluated at once (or with any symbol in general like Null), we wouldn't be able to use them in functions like Solve[] and Reduce[]. $\endgroup$ – J. M. will be back soon Nov 10 '17 at 11:51
8
$\begingroup$

=== (SameQ) tests structural equality and is meant mainly for programming uses. Like nearly all functions named as ...Q, it evaluates immediately to either True or False.

== both represents and tests mathematical equality, and is meant for symbolic algebra use.

Thus when == is used with a symbol (or expression containing symbols) on either side, it will not evaluate. It stays unevaluated and it can be used to represent a mathematical statement. This is how we write equations in Mathematica, e.g. x^2 - 2 == 0.

Null has no meaning in symbolic algebra uses so here it is just treated as a symbol, which it is. Thus 1 == Null stays unevaluated the same way 1 == x does.

Mathematica is loosely typed: everything is an expression. You may put just about anything on either side of ==. The nature of the language does not make it practical or worthwhile for functions to try to give a special treatment for each kind of object. Since Null has no special meaning or use in symbolic algebra, it receives no special treatment from Equal.

Also consider that Mathematica will take things like Solve[Null^2 == 2, Null] with no complaint.

$\endgroup$
  • 2
    $\begingroup$ From the docs: lhs == rhs returns True if lhs and rhs are ordinary identical expressions. Thus, == yields True in some symbolic cases, like x==x, where === also yields True. $\endgroup$ – John Doty Nov 10 '17 at 12:40
  • $\begingroup$ @JohnDoty Sure, and there are cases when it doesn't evaluate even though both sides are NumericQ. But the whole story doesn't fit in a single post. $\endgroup$ – Szabolcs Nov 10 '17 at 12:43

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.