## Why does Mathematica claim there is no even prime?

21

3

I wonder if this is a bug, or if I'm misunderstanding something:

Exists[n, EvenQ[n] && PrimeQ[n]] // Resolve
(*
==> False
*)


So if I interpret this result correctly, according to Mathematica there does not exist an even prime. However, if given the number directly, it gives the correct result:

EvenQ[2] && PrimeQ[2]
(*
==> True
*)


So did I find a bug in Mathematica (and if so, is it fixed in the latest version)? Or did I misunderstand Resolve?

24

Note: I am not particularly knowledgable in the field of this question, so what I write below may well be wrong.

I don't know whether or not this should be considered a bug, but to my mind this is an instance of a clash of programming and mathematical functionality. To put it differently, predicates (functions ending with Q) seem to be a wrong match for things like FindInstance or Resolve, because of their evaluation semantics. Functions suitable for mathematical transformations tend to return unevaluated when they don't know what to do, which gives the outer functions a chance to further transform them as expressions. OTOH, predicates will always return False immediately when they can not establish the the condition they check is True.

By using Trace[Exists[n,EvenQ[n]&&PrimeQ[n]]//Resolve, TraceInternal->True], one can see that at some point, both EvenQ and PrimeQ evaluate to False, and this is the reason for the result. Moreover, even a simpler request

Exists[n,EvenQ[n]]//Resolve
(*
-->  False
*)


However, this will work:

FindInstance[IntegerPart[n/2]*2==n && n>1 &&n<4 ,n,Integers]

(*
--> {{n->2}}
*)


I wasn't able to make the original request work (I tried using Divisors, but no luck). But my point is that recasting the condition as a set of equations and/or inequalities may increase the chances of success here, because their evaluation semantics is that of the mathematical rather than programming functionality. The borderline seems to be quite blurred, but I think it is there.

Note that e.g. FindInstance[EvenQ[n] && n > 10, n, Integers] yields {}. One can carefully say as you did "an instance of a clash of programming and mathematical functionality", but basically that is an incorrect result, so a proper term for this kind of issue is a bug. – Artes – 2012-01-24T14:01:59.527

Or maybe just that Exists[n, n^2 == 9 && OddQ[n]] // Resolve returns False if we cannot expect for too much from FindInstance – Artes – 2012-01-24T14:08:18.050

4@Artes I disagree. I think this is not an incorrect result, it is an incorrect usage. One should not use predicates for algebraic manipulations and the like. I think I formulated my view on this pretty clearly. I'd agree that the documentation could be more clear about this though. – Leonid Shifrin – 2012-01-24T15:26:24.973

@Mr.Wizard lol :) – Leonid Shifrin – 2012-01-24T16:00:43.160

4@Artes I think considering this a bug is similar to considering this Replace[z^4, z^2 -> x] not returning x^2 a bug (although the two situations are not equivalent). The documentation says that EvenQ "gives True if expr is an even integer, and False otherwise." (and this is elaborated in "More information": it must have head Integer and be even). So it's actually doing exactly what it's claimed to do. – acl – 2012-01-24T16:57:56.730

Thanks. I wasn't aware that functions like EvenQ and PrimeQ have this always-evaluate semantics (and frankly, I cannot think of an instance where I'd need the semantics of PrimeQ). – celtschk – 2012-01-24T17:24:08.953

@acl Ok, I corrected the statement. But these situations are far far not equivalent. When Exists[n, EvenQ[n] && PrimeQ[n]] yields False this is clearly misleading and the system does return neither warning nor error. – Artes – 2012-01-24T17:36:38.870

The way Mathematica overloads the same function with both mathematical and programming meaning has always made me a bit uncomfortable. It is very common to see this, I'd say the majority of common functions are like this (e.g. Sum, Max, Abs, ...). But admittedly this approach also makes simple usage more convenient. – Szabolcs – 2012-01-24T17:44:57.017

2FindInstance requires a system of equations and inequalites. The only reason it doesn't issue a message with predicates is that they evaluate to False (a valid result from an inequality) before being passed to FindInstance. The documentation clearly states that "expr can contain equations, inequalities, domain specifications and quantifiers, in the same form as in Reduce." – Rojo – 2012-01-24T18:02:47.367

11

It has been explained in good detail why your inputs did not work the way you wanted them; however, there is still a way to get what you want:

Resolve[Exists[n, Element[n, Primes] && Mod[n, 2] == 0]]
True

FindInstance[Element[n, Primes] && Mod[n, 2] == 0, n, Integers]
{{n -> 2}}


In general, use Element[n, Primes] whenever you need to make a restriction to prime numbers.

7

Neither Resolve or FindInstance hold their arguments, so they evaluate immediately, and we have:

In[68]:= Exists[n, EvenQ[n] && PrimeQ[n]]

Out[68]= False

In[69]:= EvenQ[n] && PrimeQ[n]

Out[69]= False


So the code isn't really doing what you're expecting it to.

1Exists does hold its arguments, and is inside Resolve, so I don't buy this (as a direct argument) for Resolve, but agree for FindInstance. If one traces Resolve, as outlined in my answer, one sees that Exists does evaluate the arguments inside it, and that's where it happens. – Leonid Shifrin – 2012-01-24T15:29:07.497

@LeonidShifrin: I hadn't looked at the attributes on Exists... So then the question is why Exists[n, EvenQ[n] && PrimeQ[n]] evaluates to False instead of returning unevaluated. – Brett Champion – 2012-01-24T15:34:54.303

Well, if you look at the Trace, you'll see why. But I still blame the use of predicates (in other words, the input code itself). I think they are for programming, because by their nature they will not return unevaluated. At the end, Exists must evaluate its content at some point (at least try)! It does some dummy variables replacement first, but that doesn't help here. – Leonid Shifrin – 2012-01-24T15:38:14.127

@LeonidShifrin: Ah right. Basically whatever you do to try to symbolically inspect the Q functions will lead to False. – Brett Champion – 2012-01-24T15:41:46.167

Yes, that's exactly my point! – Leonid Shifrin – 2012-01-24T15:44:08.407

3

There are more misleading issues :

FindInstance[EvenQ[n] && PrimeQ[n], n, Integers]
(*
==>  {}
*)


These both results are in v. 7.0.1 as well as in v. 8.0.4

Edit

Even much worse :

FindInstance[OddQ[n] && PrimeQ[n], n, Integers]
(*
==>  {}
*)


In general, Exists and FindInstance work unexpectedly with OddQ, EvenQ, PrimeQ, e.g. :

Exists[n, n^2 == 9 && PrimeQ[n]] // Resolve
(*
==> False
*)

Exists[n, n^2 == 9 && OddQ[n]] // Resolve
(*
==> False
*)

Exists[n, PrimeQ[n] && n < 20] // Resolve
(*
==> False
*)

FindInstance[EvenQ[n] && n > 10, n, Integers]
(*
==>{}
*)


However

FindInstance[n > 10 && n < 20, n, Integers]
(*
==> {{n -> 11}}
*)

Exists[n, n^2 == 9 && n < 0] // Resolve
(*
==> True
*)

Exists[n, 10 < n && n < 20] // Resolve
(*
==> True
*)


Edit 2

Since those issues rather souldn't be called bugs while they still can be misleading, one may blame them on misusing of predicates inside FindInstance and Exists and we could expect more detailed documentation.