2.7.4 Advanced Topic: Variables in Pure Functions and Rules
Module and With allow you to give a specific list of symbols whose names you want to treat as local. In some situations, however, you want to automatically treat certain symbol names as local.
For example, if you use a pure function such as Function[{x}, x + a], you want x to be treated as a "formal parameter", whose specific name is local. The same is true of the x that appears in a rule like f[x_] > x^2, or a definition like f[x_] := x^2.
Mathematica uses a uniform scheme to make sure that the names of formal parameters which appear in constructs like pure functions and rules are kept local, and are never confused with global names. The basic idea is to replace formal parameters when necessary by symbols with names of the form x$. By convention, x$ is never used as a global name.
Here is a nested pure function.
In[1]:= Function[{x}, Function[{y}, x + y]]
Out[1]=
Mathematica renames the formal parameter y in the inner function to avoid conflict with the global object y.
In[2]:= %[2y]
Out[2]=
The resulting pure function behaves as it should.
In[3]:= %[a]
Out[3]=
In general, Mathematica renames the formal parameters in an object like Function[vars, body] whenever body is modified in any way by the action of another pure function.
The formal parameter y is renamed because the body of the inner pure function was changed.
In[4]:= Function[{x}, Function[{y}, x + y]] [a]
Out[4]=
Since the body of the inner function does not change, the formal parameter is not renamed.
In[5]:= Function[{x}, x + Function[{y}, y^2]] [a]
Out[5]=
Mathematica renames formal parameters in pure functions more liberally than is strictly necessary. In principle, renaming could be avoided if the names of the formal parameters in a particular function do not actually conflict with parts of expressions substituted into the body of the pure function. For uniformity, however, Mathematica still renames formal parameters even in such cases.
In this case, the formal parameter x in the inner function shields the body of the function, so no renaming is needed.
In[6]:= Function[{x}, Function[{x}, x + y]] [a]
Out[6]=
Here are three nested functions.
In[7]:= Function[{x}, Function[{y}, Function[{z}, x + y + z]]]
Out[7]=
Both inner functions are renamed in this case.
In[8]:= %[a]
Out[8]=
As mentioned in Section 2.2.5, pure functions in Mathematica are like expressions in formal logic. The renaming of formal parameters allows Mathematica pure functions to reproduce all the semantics of standard expressions faithfully.
Scoping constructs in Mathematica.
Mathematica has several "scoping constructs" in which certain names are treated as local. When you mix these constructs in any way, Mathematica does appropriate renamings to avoid conflicts.
Mathematica renames the formal parameter of the pure function to avoid a conflict.
In[9]:= With[{x = a}, Function[{a}, a + x]]
Out[9]=
Here the local constant in the inner With is renamed to avoid a conflict.
In[10]:= With[{x = y}, Hold[With[{y = 4}, x + y]]]
Out[10]=
There is no conflict between names in this case, so no renaming is done.
In[11]:= With[{x = y}, Hold[With[{z = x + 2}, z + 2]]]
Out[11]=
The local variable y in the module is renamed to avoid a conflict.
In[12]:= With[{x = y}, Hold[Module[{y}, x + y]]]
Out[12]=
If you execute the module, however, the local variable is renamed again to make its name unique.
In[13]:= ReleaseHold[%]
Out[13]=
Mathematica treats transformation rules as scoping constructs, in which the names you give to patterns are local. You can set up named patterns either using x_, x__ and so on, or using x:patt.
The x in the h goes with the x_, and is considered local to the rule.
In[14]:= With[{x = 5}, g[x_, x] > h[x]]
Out[14]=
In a rule like f[x_] > x + y, the x which appears on the righthand side goes with the name of the x_ pattern. As a result, this x is treated as a variable local to the rule, and cannot be modified by other scoping constructs.
The y, on the other hand, is not local to the rule, and can be modified by other scoping constructs. When this happens, Mathematica renames the patterns in the rule to prevent the possibility of a conflict.
Mathematica renames the x in the rule to prevent a conflict.
In[15]:= With[{w = x}, f[x_] > w + x]
Out[15]=
When you use With on a scoping construct, Mathematica automatically performs appropriate renamings. In some cases, however, you may want to make substitutions inside scoping constructs, without any renaming. You can do this using the /. operator.
When you substitute for y using With, the x in the pure function is renamed to prevent a conflict.
In[16]:= With[{y = x + a}, Function[{x}, x + y]]
Out[16]=
If you use /. rather than With, no such renaming is done.
In[17]:= Function[{x}, x + y] /. y > a + x
Out[17]=
When you apply a rule such as f[x_] > rhs, or use a definition such as f[x_] := rhs, Mathematica implicitly has to substitute for x everywhere in the expression rhs. It effectively does this using the /. operator. As a result, such substitution does not respect scoping constructs. However, when the insides of a scoping construct are modified by the substitution, the other variables in the scoping construct are renamed.
This defines a function for creating pure functions.
In[18]:= mkfun[var_, body_] := Function[{var}, body]
The x and x^2 are explicitly inserted into the pure function, effectively by using the /. operator.
In[19]:= mkfun[x, x^2]
Out[19]=
This defines a function that creates a pair of nested pure functions.
In[20]:= mkfun2[var_, body_] := Function[{x}, Function[{var}, body + x]]
The x in the outer pure function is renamed in this case.
In[21]:= mkfun2[x, x^2]
Out[21]=
