       Re: Scoping and named patterns

• To: mathgroup at smc.vnet.net
• Subject: [mg18179] Re: Scoping and named patterns
• From: "Allan Hayes" <hay at haystack.demon.co.uk>
• Date: Sat, 19 Jun 1999 23:54:35 -0400
• References: <7kcjrn\$lql@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Here is my reading of what is happening

n = 2; x = 3;
f[a^b] /. f[x : _^n_] -> p[x, n]

p[3, 2]

Because p[x,a] evaluates before the scoping is implemented..
But

n = 2; x = 3;
f[a^b] /. f[x : _^n_] :> p[x, n]

p[a^b, b]

Module[{x, n}, f[a^b] /. f[x : _^n_] -> p[x, n]]

p[3, 2]

Because Module is Hold All the scoping is done with  f[x:_^n_]->p[x,n]
unevaluated,
This means that its x an n are bound. They are therefore not replaced by
x\$k, n\$k and the evaluation of  f[a^b]/.f[x:_^n_]->p[x,n] is as above.
The evaluation below comes to the same; since Clear[x,n] becomes
Clear[x\$k,n\$k]

Module[{x, n}, Clear[x, n]; f[a^b] /. f[x : _^n_] -> p[x, n]]

Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

p[3, 2]
Dr Dan <drdanw at my-deja.com> wrote in message news:7kcjrn\$lql at smc.vnet.net...
> I am having trouble with name conflicts between global symbols and
> named patterns.
>
> This example from The Book works fine:
>
> In:= f[a^b] /. f[x : _^n_] -> p[x, n]
> Out= p[a^b, b]
>
> But if the symbols used as pattern names have values:
>
> In:= n = 2; x = 3;
>         f[a^b] /. f[x : _^n_] -> p[x, n]
> Out= p[3, 2]
>
> My usual favorite scoping structure, Module, doesn't help:
>
> In:= Module[{x, n}, f[a^b] /. f[x : _^n_] -> p[x, n]]
> Out= p[3, 2]
>
> This shows that the global symbol is used as the pattern name and not
> the symbol local to the scoping construct:
>
> In:= Module[{x, n}, Clear[x, n]; f[a^b] /. f[x : _^n_] -> p[x, n]]
> Out= p[3, 2]
>
> Since local symbols are ignored, it is necessary to use Block:
>
> In:= Block[{x, n}, f[a^b] /. f[x : _^n_] -> p[x, n]]
> Out= p[a^b, b]
>
> This looks like a bug to me.  If I use a symbol in a local context I
> expect the local symbol and never the global.  I am a little concerned
> that the pattern itself doesn't scope its pattern names, that I can
> make one seemingly small change in my notebook and my patterned
> replacements begin crashing.
>
> Any comments, or a better workaround than Block?
>
>
> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.
>

```

• Prev by Date: Trying for better partials notation
• Next by Date: Re: ValueBox ?
• Previous by thread: RE: Scoping and named patterns
• Next by thread: Re: Scoping and named patterns