16

I cannot figure out why I have to evaluate this twice (in Mathematica 7) for the assignment to take.

First evaluation:

Unprotect[Rule];
Attributes[Rule]
pp = Plot | LogLinearPlot | ListPlot | ParametricPlot3D;
(h : pp)[True -> False] ^:= Print["Irrelevant data"]

(*

Out[2]= {SequenceHold}

During evaluation of In[1]:= UpSetDelayed::write: Tag Rule in (h:Plot|LogLinearPlot|ListPlot|ParametricPlot3D)[True->False] is Protected. >>

Out[4]= $Failed

*)

As can be seen from Out[2]= {SequenceHold}, Unprotect[Rule] worked, yet the error message indicates otherwise. If I evaluate the cell a second time, the assignment takes and no error is generated.

Why does this happen?

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125

1 Answers1

16

As you might well know, Mathematica loads binary MX files that implement some of its functionality. These MX files store implementations as well as definitions and attributes.

This is insidious, but your Unprotect[Rule] is undone by Mathematica's newly loaded MX file, and this explains why it worked the second time. Because Mathematica had already loaded all MX files it needed.

If you first evaluate all the symbols in your expression, then it stops complaining:

{Unprotect, Rule, Attributes, Plot, LogLinearPlot, ListPlot, 
  ParametricPlot3D, True, False, Print};
Unprotect[Rule];
Attributes[Rule];
pp = Plot | LogLinearPlot | ListPlot | ParametricPlot3D;
(h : pp)[True -> False] ^:= Print["Irrelevant data"]

EDIT The first evaluation is needed to trigger all the auto-loading before you unprotect Rule.

Sasha
  • 5,935
  • 1
  • 25
  • 33
  • Let me see if I understand this. Mathematica loads one of the Plot functions only when it appears (via `DeclarePackage` or something similar) and that package contains an `Unprotect[Rule] ... Protect[Rule]` pair (or something similar). Correct? – Mr.Wizard Apr 13 '11 at 13:01
  • 2
    Not quite, it contains the attribute of the symbol. Kind of like `SetAttributes[a, Protected]; DumpSave[FileNameJoin[{$HomeDirectory, "Desktop", "tmp.mx"}], a];` and then `Unprotect[a]; Get[FileNameJoin[{$HomeDirectory, "Desktop", "tmp.mx"}]]; a = 1`. – Sasha Apr 13 '11 at 14:12
  • 1
    In this case, the culprit is `LogPlot.mx` which is loaded when the symbol `LogLinearPlot` is evaluated. Why that should restore the protected status of `Rule` is a mystery to me. Perhaps there is a `Unprotect[Rule];...;Protect[Rule]` sequence in that file. Interestingly, `LogLinearPlot` does not have the usual `Stub` attribute embued by `DeclarePackage`. There must be some other auto-loading mechanism at work here. – WReach Apr 13 '11 at 14:40
  • 6
    @Sasha Thanks for sharing this information. From the user's standpoint, I'd still classify this as a bug - this contradicts the very general language semantics regarding symbol protection, as well as the documentation. As soon as the amount of third-party mma code used in production becomes significant (assuming this ever happens), cases like this will become a real problem. At the very least, IMO the official coding guidelines are needed with a list of built-in functions that should *never* be modified by the user, i.e. for which such modifications will result in undefined behavior. – Leonid Shifrin Apr 13 '11 at 14:46
  • 3
    @WReach .mx file loading doesn't work via `Stub`. A definition is added so that evaluating the symbol will load the .mx file (and then the special definition gets cleared.) – Brett Champion Apr 13 '11 at 15:42
  • @WReach As I understand from Sasha's explanation in his second comment here, the file `LogPlot.mx` does not contain the `Unprotect[Rule];...;Protect[Rule]` sequence. It just contains something like `Attributes[Rule] = {Protected}` and is created by `DumpSave` that seemingly works as analog of `Save`. – Alexey Popkov Aug 05 '11 at 10:27