3
$\begingroup$

Here is a typical issue: Simplify[x + y, x = y] produces 2 True.

Is there a way to detect operator Set in assumptions and issue a warning?

Update: As Mr.Wizard suggested it is possible to write a wrapper for Simplify that will check the second argument. But since there are many built-in functions which use assumptions it will be cumbersome to overload them all. Maybe there is a more simple way?

$\endgroup$
2
  • 1
    $\begingroup$ Look up $Pre and $PreRead $\endgroup$
    – rm -rf
    Commented Jan 24, 2013 at 23:32
  • $\begingroup$ rm-rf gave you the best advice I think. Consider putting some function in $Pre which checks for whatever list of patterns you create of stuff you want to be warned about. $\endgroup$
    – Rojo
    Commented Jan 25, 2013 at 2:17

2 Answers 2

4
$\begingroup$

I'm not sure I understand the spirit of your question. My apologies of this answer is obvious to you and you intended something else.

Simplify::badSet = "Function Set used in assumptions where is likely in error.";

SetAttributes[mySimplify, HoldAll];

mySimplify[expr_, assum_] :=
  Message[Simplify::badSet] /; ! FreeQ[Unevaluated@assum, Set]

mySimplify[else__] := Simplify[else]

Now:

mySimplify[x + y, x = a]

Simplify::badSet: Set used in assumptions where is likely in error. >>

You could overload Simplify itself if you are comfortable with such things. You'll want to be familiar with the Villegas-Gayley method of you choose that path.

$\endgroup$
7
  • 1
    $\begingroup$ This made me think about SyntaxInformation's "ColorEqualSigns" option, only to find that it isn't working (on my system, v8 or v9) other than for the first argument $\endgroup$
    – Rojo
    Commented Jan 25, 2013 at 0:02
  • $\begingroup$ Try "ColorEqualSigns" /. SyntaxInformation /@ {And, For, Implies, Nand, Nor, Not, Or, Which, Xor}. +1 btw. In >v8 that gives {All, {2, 2}, All, All, All, All, All, Odd, All}. Try it with a custom function, it doesn't work either $\endgroup$
    – Rojo
    Commented Jan 25, 2013 at 0:03
  • $\begingroup$ @Rojo same result in v7: {All, {2, 2}, All, All, All, All, All, Odd, All} $\endgroup$
    – Mr.Wizard
    Commented Jan 25, 2013 at 0:09
  • 1
    $\begingroup$ And it also doesn't work? And[x=4, y=6] only colours red the first =? $\endgroup$
    – Rojo
    Commented Jan 25, 2013 at 0:23
  • 1
    $\begingroup$ @Rojo sorry; that's the same too. $\endgroup$
    – Mr.Wizard
    Commented Jan 25, 2013 at 0:25
2
$\begingroup$

@rm-rf suggested somtething along these lines I believe

$cancelOnSyntaxError = False;

syntaxCheckingRules = {i : HoldPattern[Simplify[_, _ = _]] :> 
    "Equal in " <> ToString@HoldForm@i};

fixRules[lhs_ :> rhs_] := 
  lhs :> Block[{}, (Print@rhs; dontEval = True) /; True];

$Pre = Function[code,
       Block[{dontEval = False},
        Unevaluated[code] /. fixRules /@ syntaxCheckingRules;
        If[! dontEval || ! $cancelOnSyntaxError, code, $Canceled]
    ], HoldAllComplete];

Too add more checks, append to syntaxCheckingRules the pattern of the syntax error and the string you want to be displayed as output.

With that set

Simplify[2, x = 4]

prints a warning message

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.