# Quoting definition explicitly

I'm trying to quote a definition using DownValues, OwnValues, etc. Everything seems to be fine, except for temporary symbols' annoying appearance in recursive definitions: compare

In:= With[{g = f}, {HoldPattern@g@x_ :> 2 + x}]
Out= {HoldPattern[f[x_]] :> 2 + x}


and

In:= With[{g = f}, {HoldPattern@g@x_ :> 2 + g@x}]
Out= {HoldPattern[f[x$_]] :> 2 + f[x$]}


Here is my version of quoting a definition:

In:= SetAttributes[InheritDefinition, HoldAll];

InheritDefinition[f_Symbol, g_Symbol, rules_:{}] :=
{ ClearAll@f
(*, InheritOptions[f, g]*)
, (#@f = With[{g = f}, Evaluate[#@g /. rules]])& /@
{DownValues, OwnValues, SubValues, UpValues}
, FullDefinition@f } // Last


I don't see any need for temporary symbols here at all, since RuleDelayed is HoldRest, and HoldPattern is HoldAll. So, x$ is a dependency that seems totally unnecessary. In:= ClearAll /@ HoldComplete[oldF, newF] // ReleaseHold; In:= oldF@x_ := 2 - oldF[2 + x] In:= newF = "Will be overwritten"; In:= InheritDefinition[newF, oldF] Out= newF[x$_] := 2 - newF[2 + x$] Attributes[x$] = {Temporary}


1) How to quote definitions without temporary symbols?

2) Why exactly is there a difference between In and In? Is it really because of recursion?

### Why the renamings

It is probably easier to first answer your second question. The reason for what you see is that variable renaming (as a part of Mathematica's mechanism to emulate lexical scoping) is often excessive, in the sense that more symbols get renamed than strictly necessary.

Here is what is happening: lexical scoping constructs (Module, With, Function, SetDelayed) analyze the code inside, and if they sense variable collisions in inner scoping constructs (as a result of variable bindings they have to perform), they rename variables in inner scoping constructs. This is described in much more detail here. The excessive renaming is probably intended as a substitute for full code analysis, which makes sense for Mathematica, given that such full analysis would be very hard if not impossible, and also much more computationally intensive.

### Cloning symbols

As to the second question, the most direct and effective answer was given here, and this is what I'd use if you are only interested in the end result.

If you want to understand the mechanics, then one example of doing something similar to what you want can be found here. In particular, the key ingredient is to use replacement rules:

setProp = (#[clone] = (#[s] /. HoldPattern[s] :> clone)) &


where clone is the symbol to inherit the definitions, and setProp is a function to be applied to DownValues, etc.

Since rule application does not care about possible naming conflicts, and just does what it is asked to do, you don't get the renaming this way. It has its downsides too, however: the other side of this is that, for example, pure functions with named arguments are a leaky abstraction in Mathematica. More on that can be found here and here (at the end of the post).