I found a nice bug in Mathematica Could anyone check to reproduce it?

Create a file temp.txt with one line:

Attributes[s$] = {Temporary}

Load it into Mathematica with <<.

Now calculate:

Sum[x^(-n)*y^n, {n, 0, Infinity}]

Mathematica stops responding and the only option is to quit the kernel. You can't interrupt or abort.

Now change s to a or b in the file and repeat the above. It works!

Next put the line

Attributes[s$] = {Temporary}

directly into the notebook and calculate the sum. It works!

What is so special about temporary variable s$ loaded from the file?

I used a temporary variable s$ in one of my functions and then saved the function to the file. Mathematica added the above line to the file. Everything stopped working.

  • 2
    $\begingroup$ Mathematica uses renaming to localize variables. In certain situations s will be renamed to s$ to avoid name collisions (though not in Module). You should never use such names in your code or you're risking to break things. $\endgroup$ – Szabolcs Feb 7 '14 at 0:31
  • 1
    $\begingroup$ @Vlad This is not a Q/A site, not a place to rant. Please make sure that you ask a clear and answerable question. If your question was, "is this a bug and how do I work around it?", then you got the answer: if you used s$ then it's not a bug, it's user error. If you didn't then maybe. Yet you fail to tell us if you did or didn;t. $\endgroup$ – Szabolcs Feb 7 '14 at 0:59
  • 1
    $\begingroup$ @Mr.Wizard I think that, if your use stopped working or happened to eventually break something, it wouldn't be a MMA bug, but a risk you were willing to take $\endgroup$ – Rojo Feb 7 '14 at 1:41
  • 1
    $\begingroup$ @Vlad, as to the difference, I think at least in the notebook the line where you set the attributes shouldn't do anything. Because since it is Temporary the symbol should get removed instantly. Perhaps that's not the case if it was defined in the file? I haven't tried $\endgroup$ – Rojo Feb 7 '14 at 1:46
  • 2
    $\begingroup$ I filed a bug report for this. Best advice remains: Don't use variables that end with $. Too much chance of confusing the scoping mechanism. $\endgroup$ – Daniel Lichtblau Feb 7 '14 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.