# Rules for internal functions

I'm at my string rewrite systems again. I always implemented these as rules on function arguments. For example, assume a rule is you can drop all arguments 0:

F[x___,0,y___]:=F[x,y];

F[1,0]

No problem. But now assume my task is to insert a pair of formal brackets (call them A and Z) everywhere in F:

(*In*) F[1,2]

(*Out*) {F[A,1,Z,2],F[1,A,2,Z],F[A,1,2,Z]}

Doable - I replace F by List, run over all position pairs, replace List back to F, whoopsie, forgot that I can have nested F expressions...a nuisance. Then I had the idea: Why not define on List directly?

List[x___,a,y___]:=List[x,y];

(*Special Epic Fail*)

My question: Am I right that you can't make rules on internal functions of Mathematica at all, to protect the innocent?

Log[x_]:=Sin[x];

(*Generic Epic Fail*)

Or is there a limited possibility? Of course, since all Mathematica is based on List, it has to be protected at all costs, but, say, replacing all Exp[x]=Cos[x]+I*Sin[x] on the fly doesn't look that desastrous...

SetAttributes[Log, Flat]

Log[Log[a]]

(Silly, but this shows that Protected doesn't protect against all shenanigans.)

• You are right that you should avoid modifying the behavior of basic and fundamental built-in such as List in as sweeping a way as you did. It is a Very Bad Idea and it will Break Things all over the place. But you have found out the answer to that question already, by direct experimentation. So perhaps rewrite your question to focus on accomplishing the task you originally wanted instead. Jan 27, 2022 at 14:55

It is a very bad idea to redefine built in functions. Therefore, built in have the attribute "Protected".

However, you may store an assignment containing a built in under a different name by the operator "/:". E.g. your example with Exp[x];

x /: Exp[x] := Cos[x] + I*Sin[x]


This is now tied to the variable "x" and if you write:

Exp[x]


it is automatically changed to:

Cos[x] + I Sin[x]


Or your example where you delete "a" in any list:

a /: {x___, a, y___} = {x, y}

{1, a, 2, a, 3}

(* {1, 2, 3} *)