11
$\begingroup$

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).

$\endgroup$
13
  • 4
    $\begingroup$ Your use of Module is unnecessary. If you use RuleDelayed instead of Rule, then the pattern variables are automatically localized. lint2[f_, x_] := f /. Replace[x, {a_, b_} :> a -> b] $\endgroup$
    – rm -rf
    Feb 15, 2014 at 16:10
  • 2
    $\begingroup$ No time for a full answer, but in short: Rule itself 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$
    – Szabolcs
    Feb 15, 2014 at 16:34
  • 9
    $\begingroup$ If using version 9, might try setting SetSystemOptions["StrictLexicalScoping"->True]` and see if that changes things in a useful way. $\endgroup$ Feb 18, 2014 at 3:02
  • 3
    $\begingroup$ @rogerl As for the why, the main reason is that Module does localization by renaming variables before evaluation, as you can see by evaluating Module[{x}, x]. However, it does not rename everything. Things inside other scoping constructs inside Module do 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: since Rule is a kind of scoping construct, it prevents Module from renaming a, so we still have a instead ... $\endgroup$
    – Szabolcs
    Feb 18, 2014 at 18:19
  • 3
    $\begingroup$ ... of a$123 (i.e. the "localized" version). a is 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: What Block does is that it temporarily removes the definitions associated with symbols, but the symbols themselves stay the same.) $\endgroup$
    – Szabolcs
    Feb 18, 2014 at 18:21

1 Answer 1

13
$\begingroup$

As Szabolcs say in comments, "Rule itself 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." Let us check this:

Clear["`*"];
Module[{a, b}, {a_, b_} -> a -> b]

(* =>  {a_, b_} -> a -> b *)

One can see that Module has not renamed a and b as Szabolcs state. But this protection takes place only if both variables on LHS are defined via Blank (Mathematica 8.0.4):

Clear["`*"];
Module[{a, b}, {a_, b} -> a -> b]

(* => {a$_, b$96} -> a$ -> b$96 *)

Here we see a limitation of Module's cleverness...

$\endgroup$
2
  • $\begingroup$ This is clear in the Module docs.. Module substitutes new symbols for each of the local variables that appear anywhere in expr except as local variables in scoping constructs. -- still a bit disturbing $\endgroup$
    – george2079
    Feb 15, 2014 at 19:41
  • 1
    $\begingroup$ @george2079 Could you explain the second example in my answer? $\endgroup$ Feb 16, 2014 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.