18
$\begingroup$

In version 9, with a newly started Mathematica session, try the following code:

plotAttributes = Attributes[Plot]

Plot[Sin[x], {x, 0, 1}];

Attributes[Plot]

% == plotAttributes

{Protected, ReadProtected}

{HoldAll, Protected, ReadProtected}

False

Is there any explanation why Plot doesn't have HoldAll as one of its attributes when a fresh Mathematica session is started, and acquires this attribute only after I call Plot once?

$\endgroup$
0

1 Answer 1

20
$\begingroup$

It is because, in version 9, the implementation of Plot is loaded from a dump file on its first usage, rather than loading when the kernel starts. One can see this by clearing the ReadProtected attribute:

ClearAttributes[Plot, ReadProtected]
Information[Plot]

(* -> Plot := System`Dump`AutoLoad[
        Hold[Plot], Hold[syms], Visualization`Proto`
       ] /; System`Dump`TestLoad *)

where syms is a large sequence of symbols (including Plot itself) whose usage will cause the dump file containing the Plot implementation to be loaded, if it has not been already. The file in question is (as Visualization`Proto` suggests):

FileNameJoin[{
 $InstallationDirectory, "SystemFiles", "Kernel", "SystemResources", $SystemID,
 "Visualization", "Proto.mx"
}]

Since the definition of Plot needed for the autoloading functionality constitutes a single ownvalue, it does not matter whether Attributes[Plot] contains HoldAll or not, because there are no downvalues defined at this stage. After the full implementation is loaded, the attribute becomes necessary and therefore is set, but because heads are always evaluated before bodies, HoldAll is effectively present from the start. We can observe the same behavior in this example:

f := Function[Null, Null, HoldAll];
f@Print["evaluation leak!"] (* does not print anything *)
$\endgroup$
5
  • 1
    $\begingroup$ @Brett if not to make the kernel start up more quickly, could you perhaps add an answer outlining the reason for this change, assuming it's not too complicated to admit a brief explanation? $\endgroup$ Jan 4, 2013 at 15:19
  • $\begingroup$ @Brett and Oleksandr thanks for the explanation. This comes up whenever we try to explain why tables of functions have to be plotted with Plot[Evaluate[...]...], and now the explanation has just become a little more complicated. At least there is one. $\endgroup$
    – Jens
    Jan 4, 2013 at 17:00
  • $\begingroup$ @OleksandrR. It's just an implementation detail. $\endgroup$ Jan 4, 2013 at 17:31
  • 4
    $\begingroup$ @BrettChampion Except that I can't tell people: "look at the attributes of Plot before using it, to understand that it will hold its arguments and therefore won't recognize a Table of functions as separate functions". I now have to say: "Use Evaluate, trust me. You can find the explanation after you make the plot." $\endgroup$
    – Jens
    Jan 4, 2013 at 18:23
  • $\begingroup$ @BrettChampion - Then, shouln't the documentation for Plot include a "Possible Issues" section that points this out? $\endgroup$
    – Bob Hanlon
    Jul 26, 2019 at 19:41

Your Answer

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

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