3

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?

Nick Stranniy
  • 1,233
  • 9
  • 15
  • 1
    Look up $Pre and $PreRead – rm -rf Jan 24 '13 at 23:32
  • 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. – Rojo Jan 25 '13 at 02:17

2 Answers2

4

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.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 1
    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 – Rojo Jan 25 '13 at 00:02
  • 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 – Rojo Jan 25 '13 at 00:03
  • @Rojo same result in v7: {All, {2, 2}, All, All, All, All, All, Odd, All} – Mr.Wizard Jan 25 '13 at 00:09
  • 1
    And it also doesn't work? And[x=4, y=6] only colours red the first =? – Rojo Jan 25 '13 at 00:23
  • 1
    @Rojo sorry; that's the same too. – Mr.Wizard Jan 25 '13 at 00:25
  • This is a possible workaround. But it will require to overload a lot of functions. – Nick Stranniy Jan 25 '13 at 02:06
  • @Nick How else would you target this warning or interception other than to overload functions or list all functions explicitly in a pattern (for e.g. $Pre)? – Mr.Wizard Jan 25 '13 at 17:12
2

@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

Rojo
  • 42,601
  • 7
  • 96
  • 188