From the documentation of Function, I take this example (slightly simplified). Renaming the dummy or bound variable (y->y$
in the inner Function
) is necessary in:
In[1206]:= Function[x, Function[y, f[x, y]]][y]
Out[1206]= Function[y$, f[y, y$]]
so that, for example,
In[1215]:= Function[x, Function[y, f[x, y]]][y][1]
Out[1215]= f[y, 1]
because
In[1211]:= Function[y$, f[y, y$]][1]
Out[1211]= f[y, 1]
Without renaming, we would obtain a different and generally wrong value
In[1212]:= Function[y, f[y, y]][1]
Out[1212]= f[1, 1]
The documentation shows that renaming occurs even if it is not necessary:
In[1213]:= Function[x, Function[y, f[x, y]]][1]
Out[1213]= Function[y$, f[1, y$]]
Indeed, the choice of parameter name y
should have no consequence on the function value.
Now let me try:
In[1214]:= Function[x, Function[y, f[x, y]]][y$]
Out[1214]= Function[y$, f[y$, y$]]
which is the same as Function[y, f[y, y]]
that is wrong, as explained above.
So it looks to me like a plain fat bug, is not it?
Edited after comments
The bug is equivalent to:
In[1335]:= With[{x = y$}, Function[y, f[x, y]]]
Out[1335]= Function[y$, f[y$, y$]]
In this related case on the contrary no renaming occurs:
In[1338]:= Function[y, f[x, y]] /. x -> y$
Out[1338]= Function[y, f[y$, y]]
That is because the job of ReplaceAll
is just to replace, not to detect name conflict.
Unique
or some such thing that couldn't be hacked. But the simple rule is, "Don't ever end your variables with a $." (A similar renaming occurs inDynamicModule
, but with$$
.) $\endgroup$Block
was using internally single \$-ending name but I can't prove it so I was probably mistaken. $\endgroup$Block[]
does not rename variables.Module[]
andDynamicModule[]
rename variables, but they use a different mechanism than the one you're discussing. $\endgroup$Function[t, Function[u$, t + u$]]@u$
where name conflict should be resolved byu$$
but is not. Amazingly,Function[t, Function[u$$, t + u$$]]@u$$
resolves name conflict withu$$$
. This is weird. $\endgroup$