Modifying Builtin Functions

The Wolfram Language allows you to define transformation rules for any expression. You can define such rules not only for functions that you add to the Wolfram Language, but also for intrinsic functions that are already built into the Wolfram Language. As a result, you can enhance, or modify, the features of builtin Wolfram Language functions.

This capability is powerful, but potentially dangerous. The Wolfram Language will always follow the rules you give it. This means that if the rules you give are incorrect, then the Wolfram Language will give you incorrect answers.

To avoid the possibility of changing builtin functions by mistake, the Wolfram Language "protects" all builtin functions from redefinition. If you want to give a definition for a builtin function, you have to remove the protection first. After you give the definition, you should usually restore the protection, to prevent future mistakes.

Unprotect[f]remove protection
Protect[f]add protection

Protection for functions.

Builtin functions are usually "protected", so you cannot redefine them.
In[1]:=
Click for copyable input
Out[1]=
This removes protection for Log.
In[2]:=
Click for copyable input
Out[2]=
Now you can give your own definitions for Log. This particular definition is not mathematically correct, but the Wolfram Language will still allow you to give it.
In[3]:=
Click for copyable input
Out[3]=
The Wolfram Language will use your definitions whenever it can, whether they are mathematically correct or not.
In[4]:=
Click for copyable input
Out[4]=
This removes the incorrect definition for Log.
In[5]:=
Click for copyable input
This restores the protection for Log.
In[6]:=
Click for copyable input
Out[6]=

Definitions you give can override builtin features of the Wolfram Language. In general, the Wolfram Language tries to use your definitions before it uses builtin definitions.

The rules that are built into the Wolfram Language are intended to be appropriate for the broadest range of calculations. In specific cases, however, you may not like what the builtin rules do. In such cases, you can give your own rules to override the ones that are built in.

There is a builtin rule for simplifying Exp[Log[expr]].
In[7]:=
Click for copyable input
Out[7]=
You can give your own rule for Exp[Log[expr]], overriding the builtin rule.
In[8]:=
Click for copyable input
Now your rule is used, rather than the builtin one.
In[9]:=
Click for copyable input
Out[9]=