MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: named pattern variable scoped as global, should be local

  • To: mathgroup at smc.vnet.net
  • Subject: [mg56754] Re: named pattern variable scoped as global, should be local
  • From: dh <dh at metrohm.ch>
  • Date: Thu, 5 May 2005 06:01:36 -0400 (EDT)
  • References: <d59kg5$6ch$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi Lee,
It certainly looks rather strange and I think it is more a bug than a 
feature. I am not 100% sure how it works, but here is my attempt for  an 
explanation. It has to do with the difference between Rule and RuleDelayed.
Although the manual for "Rule" says:
"Symbols that occur as pattern names in lhs are treated as local to the 
rule. This is true when the symbols appear on the right-hand side of /; 
conditions in lhs, and when the symbols appear anywhere in rhs, even 
inside other scoping constructs.".  And in A.3.8 we read:
"In general, symbols with names of the form xxx are renamed xxx$"
Now, consider
lhs->rhs
here rhs seems to be evaluated first without localization of variable 
names. Only in the return value of rhs are  the variables localized and 
replaced. Consider:

x=.;
a/.x_ ->(Print[x]; x)

this prints x and returns a. On evaluation of the rhs x is not yet 
renamed to x$ and prints simply as x. If x has a value, this value is 
inserted before any localization of variables. This can be seen in:

x=2;
a/.x_ -> (Print[x]; x)
this prints 2 and returns 2.

Now there seesm to be a way to localize variables before evaluating the 
rhs, namly RuleDelayed:

x =.;
a /. x_ :> (Print[x]; x)
this prints a and returns a.

Now we are finally coming to the core of the problem. Consider:

x =.;
Module[{x}, Print[x]; a /. x_ -> (Print[x]; x)]
this prints x$nnn (nnn=number, this is the way Module localizes 
variables) followed by x and returns a.
As can be seen, Module does NOT localize variables in a rule statement! 
The reason is probably that Rule should localize its variables itself. 
But this happens properly only with RuleDelayed:

x =.;
Module[{x}, Print[x]; a /. x_ :> (Print[x]; x)]
this prints x$nnn followed by a and returns a.

The lesson is, beware of Rule, use rather RuleDelayed.

Sincerely, Daniel

leenewman at gmail.com wrote:
> When using a named pattern variable within a module, it should be
> scoped locally within the pattern.  However, this does not seem to work
> as advertised.  For example, shouldn't the pattern variable x in the
> statements below be local to the pattern?  Does anyone know whether
> this is a bug, or whether I am just missing something about the usage
> of variables in patterns/condition constructs?
> 
> (* this returns 14! *)
> x = 7;
> Module[{x},{1, 2, 3} /. x_ -> 2x];
> 
> (* this returns {2,4,6}, assuming q is not globally defined. *)
> Remove[q];
> Module[{q},{1, 2, 3} /. q_ -> 2q];
> 
> 
> Lee
> 


  • Prev by Date: Re: how call a function by same name in 2 different contexts?
  • Next by Date: Re: letrec/named let
  • Previous by thread: Re: named pattern variable scoped as global, should be local
  • Next by thread: Re: named pattern variable scoped as global, should be local