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[1]:= With[{g = f}, {HoldPattern@g@x_ :> 2 + x}]
Out[1]= {HoldPattern[f[x_]] :> 2 + x}
and
In[2]:= With[{g = f}, {HoldPattern@g@x_ :> 2 + g@x}]
Out[2]= {HoldPattern[f[x$_]] :> 2 + f[x$]}
Here is my version of quoting a definition:
In[3]:= 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[4]:= ClearAll /@ HoldComplete[oldF, newF] // ReleaseHold;
In[5]:= oldF@x_ := 2 - oldF[2 + x]
In[6]:= newF = "Will be overwritten";
In[7]:= InheritDefinition[newF, oldF]
Out[7]= 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[1] and In[2]? Is it really because of recursion?