Skip to main content

Exhaustive examples of variable replacement due to nested scoping constructs


Consider the following examples, where I have replaced dollar signs in the output by a D.



3/.x_:> Rule[var_,x]
3/.x_:> RuleDelayed[var_,x]
3/.x_:> Function[var, x]
3/.x_:> Hold[Compile[{var, _Integer}, x]]
3/.x_:> Hold[SetDelayed[var_, x]]
3/.x_:> Hold[Set[var_,x]]
3/.x_:> Hold[TagSet[2,var_,x]]
3/.x_:> Hold[UpSet[var_,x]]
3/.x_:> Hold[UpSetDelayed[var_, x]]
3/.x_:> Hold[TagSetDelayed[2, var_, x]]

3/.x_:> Hold[With[{var=b},x]]
3/.x_:> Hold[Module[{var=b},x]]

(*not Block*)
(*3/.x_:> Hold[Block[{var=b},x]]*)
(*not DynamicModule*)
(*3/.x_:> ToString@Hold[DynamicModule[{var=b},x]]*)
(*not the Table family*)
(*3 /. x_ :> Hold[Table[x, {var, 5}]] *)


Function[x,Condition[var_,x]][3] (*ReplaceAll interacts with Condition, so I use Function*)

(*not RuleCondition*)
(*Function[x, Hold[RuleCondition[var_, x]]][3]*)


varD_->3
varD_:>3
Function[varD,3]
Hold[Compile[{varD,_Integer},3]]

Hold[varD_:=3]
Hold[varD_=3]
Hold[2/:varD_=3]
Hold[varD_^=3]
Hold[varD_^:=3]
Hold[2/:varD_:=3]
Hold[With[{varD=b},3]]
Hold[Module[{varD=b},3]]
varD_/;3


and these ones


{"Replace",Replace[3,x_:> Rule[var_, x]]}
{"ReplaceAll",ReplaceAll[3, x_ :> Rule[var_, x]]}
{"ReplaceRepeated",ReplaceRepeated[{3},{x_Integer}:> Rule[var_, x]]}
{"With",With[{x=3},Rule[var_,x]]}
{"Module",Module[{x=3},Rule[var_,x]]}
(*not Block*)
(*{"Block",Block[{x=3},Rule[var_,x]]}*)
{"DynamicModule",ToString[DynamicModule[{x=3}, Rule[var_,x]],InputForm]}
(*The example below works for any "assignment function" (Set)*)

Clear[f]
{"Replacement by evaluation",Set[f[x_],RuleDelayed[var_,x]],f[3]}
(*not Function with Slots*)
(*Function[Rule[var_,#]][3]*)
{"FunctionNamedArg",Function[x,Rule[var_,x]][3]}


{Replace,varD_->3}
{ReplaceAll,varD_->3}
{ReplaceRepeated,varD_->3}

{With,varD_->3}
{Module,varD_->3}
{DynamicModule,DynamicModule[{x = 3}, varD_ -> 3, DynamicModuleValues :> {}]}
{Replacement by Evaluation, var_:>x, varD_:>3}
{FunctionNamedArg,varD_->3}

Understanding the examples


A general explanation of such examples can be found in the Q&A called "Enforcing correct variable bindings and avoiding renamings for conflicting variables in nested scoping constructs", in this answer. However I felt a bit more could be said about this.


The first list of examples basically lists all functions that could be considered "inner scoping constructs". The "inner scoping constructs" are searched for by "outer scoping constructs" in their bodies. The outer scoping constructs are listed in the second set of examples.


Replacement due to evaluation



Note that in the second list of examples there is an example with Set, labeled "Replacement by Evaluation". This example is different from the others, in that it is not Set here that is the function that looks for scoping constructs in its body. Rather, it appears that whenever the kernel evaluates an expression x and makes a replacement because x matches the pattern of a rule, the kernel emulates searching for scoping constructs in the RHS of this rule. The kernel already does a lot of this kind of bookkeeping, so maybe this is why it is not a big performance hit. In the example we use Set, but it appears that any definition made using "assignment functions" (SetDelayed, Set, TagSet, UpSet, TagSetDelayed, UpSetDelayed) will cause replacement of such variables. This replacement will happen for definitions corresponding to DownValues, UpValues as well as SubValues. It cannot happen for definitions that correspond to OwnValues as it is not possible to create an OwnValues with a pattern on the LHS of the (generated) rule.


Explanation of the question


I think interaction between inner and outer scoping constructs depends on the combination of the scoping constructs in a kind of independent manner. I.e. all the outer scoping constructs have the same strategy for to resolve a conflict between a local variable and the second argument of Rule. If this is true, all the interaction between inner and outer scoping constructs can be illustrated with only a few examples. I would consider the list to be exhaustive if all the cases where variable replacement happens can be predicted from the list, because all inner and outer scoping constructs are present.


Question


Are my examples exhaustive?


Related


StringReplace, ReplaceAll and Rule interact in a bizarre way (answer by Leonid)




Comments

Popular posts from this blog

plotting - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Mathematica: 3D plot based on combined 2D graphs

I have several sigmoidal fits to 3 different datasets, with mean fit predictions plus the 95% confidence limits (not symmetrical around the mean) and the actual data. I would now like to show these different 2D plots projected in 3D as in but then using proper perspective. In the link here they give some solutions to combine the plots using isometric perspective, but I would like to use proper 3 point perspective. Any thoughts? Also any way to show the mean points per time point for each series plus or minus the standard error on the mean would be cool too, either using points+vertical bars, or using spheres plus tubes. Below are some test data and the fit function I am using. Note that I am working on a logit(proportion) scale and that the final vertical scale is Log10(percentage). (* some test data *) data = Table[Null, {i, 4}]; data[[1]] = {{1, -5.8}, {2, -5.4}, {3, -0.8}, {4, -0.2}, {5, 4.6}, {1, -6.4}, {2, -5.6}, {3, -0.7}, {4, 0.04}, {5, 1.0}, {1, -6.8}, {2, -4.7}, {3, -1.