I can work around this by using With, but it seems really odd to get the following:

PrimeQ[x] /. x -> 5
With[{x = 5}, PrimeQ[x]]

Obviously there is a logical explanation. But what is it?

  • 1
    $\begingroup$ Because PrimeQ[x] is evaluated first, yielding False, so eventually you're doing False /. x->5. $\endgroup$ – corey979 Feb 6 '19 at 12:34
  • 3
    $\begingroup$ PrimeQ has the rather undesireable property that it evaluates to False on symbolic input (which is bad because at this point, the question is actually undecidable.). ReleaseHold[Hold[PrimeQ[x]] /. x -> 5] works better. $\endgroup$ – Henrik Schumacher Feb 6 '19 at 12:35
  • $\begingroup$ Gotcha. Thanks. $\endgroup$ – Richard Burke-Ward Feb 6 '19 at 12:40
  • 2
    $\begingroup$ @HenrikSchumacher I disagree that PrimeQ's behaviour is undesirable: the general design of Q functions that they are meant to resolve to boolean values under all circumstances (with some annoying exceptions like FileExistsQ, which doesn't evaluate for non-strings). Programming in WL would be very tedious if the Q functions didn't follow this rule. You just need to keep that in mind when doing symbolic computations. $\endgroup$ – Sjoerd Smit Feb 6 '19 at 16:29
  • 1
    $\begingroup$ @SjoerdSmit You make a very good point. The Q-function are not seldomly used in argument patterns for functions (e.g. as _?NumberQ). This might also be the reason why it is Positive and not PositiveQ. $\endgroup$ – Henrik Schumacher Feb 6 '19 at 16:43

Browse other questions tagged or ask your own question.