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 ?
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 ?
=== (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.
== yields True in some symbolic cases, like x==x, where === also yields True.
– John Doty
Nov 10 '17 at 12:40
NumericQ. But the whole story doesn't fit in a single post.
– Szabolcs
Nov 10 '17 at 12:43
Equalonly evaluates arguments if they have numeric or string values.1==AutomaticandNull == Automaticare all left unevaluated.1 == 1 + Nullis left unevaluated just as1 == 1 + xis, which perfectly makes sense, as you don't have a value forx(or forNull) yet. Think of it (as the doc states) thatEqualtries to be as symbolic as possible, and does not evaluate symbols without values. – István Zachar Nov 10 '17 at 09:32Nullas special in that case and thus behaves like it would for any unassigned symbol and returns unevaluated. – Albert Retey Nov 10 '17 at 09:323 == x + 2evaluated at once (or with any symbol in general likeNull), we wouldn't be able to use them in functions likeSolve[]andReduce[]. – J. M.'s missing motivation Nov 10 '17 at 11:51