11
$\begingroup$

What is the reason for built-in symbols to be Protected?

What happens if I Unprotect built-ins and add my own SetDelayed, etc.? Is it degraded performance?

What are general precautions in doing that?


My particular scenario is that I want to add custom formatting to Hold[] expressions and I can not assign because tag Hold is protected. Will this affect performance if I unprotect and do it anyways?

$\endgroup$
  • $\begingroup$ You can also assign values to Format if you are just interested in appearance. $\endgroup$ – LLlAMnYP Jul 3 '17 at 16:25
  • 4
    $\begingroup$ No, it is not safe to Unprotect everything. The implication is more bugs in your code and wasting your time finding them. $\endgroup$ – Nasser Jul 3 '17 at 16:47
  • 3
    $\begingroup$ You should edit this question to focus it on real problem: changing the appearance of held expressions. $\endgroup$ – m_goldberg Jul 3 '17 at 19:04
  • $\begingroup$ I'm really sorry, I understand this is unproductive but unprotecting all built-ins AND modifying Hold? lol, that sounds like the stuff nightmares are made of, if you are not sure what you're doing $\endgroup$ – yosimitsu kodanuri Aug 18 at 17:13
16
$\begingroup$

The reason is to prevent users to mess with it. Unprotecting something is not necessarily dangerous, but unprotecting everything definitely is. Performance is probably not the issue. The real danger is that you replace a very important feature of a built-in symbol by chance. Since built-in symbols often have the ReadProtected attribute, you cannot check beforehand whether you are going to overwrite anything important.

Regarding the appearance of held expressions: One can do this

Unprotect[Hold];
Format[Hold[a___]] := Held[a];
Protect[Hold];

at the cost of the puzzling results

a = Hold[1 + 1]
a // FullForm

(* Held[2] *)
(* Hold[1 + 1] *)

Internally, the expression should be treated by Mathematica as usual; only the appearance was changed in a counterintuitive way. This particular issue can be fixed by giving the HoldAll attribute to Held:

SetAttributes[Held,HoldAll]
a
(* Held[1+1] *)

But still, messing with internals is a very, very bad idea.

Addendum: Mathematica also provides the attribute Locked which prevents changing the Attibutes of a symbol. If that symbol has also the attribute Protected then it cannot be messed with any more. That seems to be the way how developers are meant to protect their users from themselves. However, one cannot rely on the combination Protected and Locked being used consistently for all "important" symbols.

$\endgroup$
  • $\begingroup$ very nice answer + 1 ! $\endgroup$ – Ali Hashmi Jul 5 '17 at 12:11
15
$\begingroup$

There is a typesetting step after formatting, and MakeBoxes definitions do not require unprotecting Hold, you will just add more DonwValues for MakeBoxes.

MakeBoxes[Hold[a___], fmt_] := With[
    {foo = MakeBoxes[Panel[Column[{a}]], fmt]}
  , InterpretationBox[
        RowBox[{"Hold", "[", foo, "]"}]
      , Hold[a]
    ]
]

Hold[1 + 1, 2 + 2, 4 + 4]

enter image description here

It is always crucial to not let any evaluation leak during typesetting. Here, the content from Hold is passed directly to another MakeBoxes which holds its arguments.

$\endgroup$
  • 3
    $\begingroup$ It is worth noting that this method does come at the cost of some overhead compared to unprotecting Hold and attaching the formatting to it using TagSet; see: (39675). However in most cases it should not matter and of course +1. $\endgroup$ – Mr.Wizard Jul 4 '17 at 16:57
  • $\begingroup$ @Mr.Wizard somehow I missed that, thanks! $\endgroup$ – Kuba Jul 5 '17 at 6:45
0
$\begingroup$

The danger when you unprotect things is that there may be unintended side effects. For example, say we unprotect "Plus"...

Unprotect[Plus];
Plus = Times;

Now if I ask for

5+4
20

Integrate[x^2 + 3, x]
x^3

(which is not right at all).

It is very difficult to anticipate all the consequences of unprotecting the meaning of basic symbols.

$\endgroup$

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.