I don't understand why $a$ and $b$ are not local in the following code:
Clear[lint]; lint[f_, x_] := Module[{a, b}, f /. Replace[x, {a_, b_} -> a -> b]]; Clear[a]; lint[x, {x, t}] (* returns t, as I would expect *) a = 3; lint[x, {x, t}] (* returns x *) Clear[a]; a=3; lint1[f_, x_] := Module[{a, b}, f /. Replace[x, {a_, b_} :> a -> b]]; (* returns t *) It appears that the occurrence of $a$ in the pattern inside the replace is being interpreted in the outer scope. Why is that?
Update: Having read @szabolcs comment and the first answer, I still don't quite understand. First, the documentation for Rule says that "Symbols that occur as pattern names in lhs are treated as local to the rule.". Fine, that's what the comment said. But if they are local, then why is $3$ substituted for $a$ in the second example above?
Second, why does using :> solve the problem? The only difference between Rule and RuleDelayed, as far as I can see, is when the right-hand symbols are evaluated. So why does this change the behavior? (See the new fourth example above).
Moduleis unnecessary. If you useRuleDelayedinstead ofRule, then the pattern variables are automatically localized.lint2[f_, x_] := f /. Replace[x, {a_, b_} :> a -> b]$\endgroup$Ruleitself is a sort of scoping construct in Mathematica and it will protect symbols that are used as pattern names in its LHS from being renamed by Module. $\endgroup$Moduledoes localization by renaming variables before evaluation, as you can see by evaluatingModule[{x}, x]. However, it does not rename everything. Things inside other scoping constructs insideModuledo not always get renamed. I do not fully understand when this renaming happens and when it doesn't, nor why (and when) it is necessary. It a very dark corner of Mathematica. The rest is explained by the answers: sinceRuleis a kind of scoping construct, it prevents Module from renaminga, so we still haveainstead ... $\endgroup$a$123(i.e. the "localized" version).ais a global symbol which has a value, to which it gets evaluated. Again the thing to pay attention to here is that there aren't really any local and global variables in Mathematica, only renamed or non-renamed ones. (Side note: WhatBlockdoes is that it temporarily removes the definitions associated with symbols, but the symbols themselves stay the same.) $\endgroup$