1
$\begingroup$

Consider the following function:

notEvaluating[x_] := 
  Module[{y},
    If[x != $Failed,
      y = "not failed",
      y =  "failed"];
    y
  ];

notEvaluating[$Failed] returns, as expected "failed". notEvaluating[1] however returns y\$1234, because 1 != $Failed isn't evaluated further, you need to use FailureQ instead.

Is there a way to get a warning when, as in this example, some expressions aren't evaluated completely? I imagine it could be pretty helpful for debugging.

edit: I suspect it's not easy to find a concise description of the desired behavior in Mathematica terms. But I guess a first approach would be, that a warning occurs, when the Head of the result of an expression is equal to the Head of the expression itself. Ideally ignoring things like Integer, that under no circumstances can be evaluated further.

$\endgroup$
7
  • 1
    $\begingroup$ For your specific case you can give the If a third branch that is evaluated if the condition doesn't evaluate to True or False. Regarldess of that, it's an interesting question. However, could you clarify at what point you'd like to have the warning? When the inequality check doesn't evaluate, or when the If doesn't evalute? $\endgroup$ Commented Feb 9, 2017 at 13:56
  • 1
    $\begingroup$ How does a general expression that has not evaluated look? In the polynomial x[1]^2 - 3 x[2]^3, has x[1] been evaluated completely? Are Ifs the only case you need to process, or can you have other things? $\endgroup$ Commented Feb 9, 2017 at 13:56
  • 2
    $\begingroup$ Can you give a computable definition of what "expression is not evaluated completely" means? Until you can do that, this question is unanswerable. $\endgroup$
    – m_goldberg
    Commented Feb 9, 2017 at 14:26
  • 3
    $\begingroup$ How close ValueQ is? $\endgroup$
    – Kuba
    Commented Feb 9, 2017 at 14:59
  • 2
    $\begingroup$ you can use =!= to guarantee the logical test will be either true or false. $\endgroup$
    – george2079
    Commented Feb 9, 2017 at 15:31

0

Browse other questions tagged or ask your own question.