MathGroup Archive 1998

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

Search the Archive

Re: Scoped variables

  • To: mathgroup at smc.vnet.net
  • Subject: [mg13924] Re: Scoped variables
  • From: "Allan Hayes" <hay at haystack.demon.cc.uk>
  • Date: Mon, 7 Sep 1998 01:22:49 -0400
  • References: <6ssuub$m87@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Ersek, Ted R wrote in message <6ssuub$m87 at smc.vnet.net>...
>Consider the lines below.  You get nothing very interesting at Out[1].
>
>In[n]:=
>Clear[a];
>x=2;
>{a_->x^2,  x_->x^2,  x_}//InputForm
>
>Out[1]=
>{a_ -> 4,  x_ -> 4,  x_}
>
>
>It works a little different if you do the same thing inside With (see
>below).
>
>In[2]:=
>With[{x=7},{a_->x^2,  x_->x^2,  x_}]//InputForm
>
>Out[2]=
>{a$_ -> 49,  x_ -> 4,  Pattern[7, _]}
>
>
>I understand there were extensive discussions on this sort of thing in
>this group in recent years.  However, I can't find anything, mostly
>because I haven't figured out how to use the MathGroup search engine.
>Anyway I figured some members would still be  interested.
>
>Ted Ersek
>
>

Ted:

Scoping is a mechanism for avoiding clashes of variable If we enter

        f[y_] = Function[x,y+x]

then we might expect that f[p] (p not equal to x) would be the function
Function[x,p+x] that adds p to its input but that f[x] would be
Function[x,x+x], which is the function that multiplies its input by 2.

We are saved from this by a renaming mechanism that gives

In[1]:=
f[p]

Out[1]
Function[x$,p+x$]

In[2]:=
f[x]
Out[2]=
Function[x$,x+x$]


Here is an attempt to describe what seems to go on (some of the terms
are used in my own way). I hope that it is not too sketchy as to be
useful and I would welcome observations and suggestions for
improvement.

Definitions

The expressions outlined on the right below are called scoping
constructs; the displayed x scopes all x in body. In any expression, if
a symbol scopes or is scoped then it is bound,otherwise it is free.
The targets of a variable such as the displayed x are defined in the
right column. A target of the expression is any target of such an x.

Outline expression                      Targets of x

(.. x:pat ..)->(or:>)body               all x in body (.. x:pat ..)=
(or:=)body                    ,, (.. x:pat ..)/; body                  
,, Function[{..x..}, body]                 all x free in body
With[{..x=.,or:=,..}, body]                  ,, Module[{..x,or x=., or
x:=.,..}, body]       ,,


Evaluation

When the following are used in an evaluation, each scoping construct in
body that contains a target of the whole expression in its body has
each of its bound symbols v that is not a target of the expression
replaced by v$. The evaluation then proceeds

Replacement by {(..)->(or:>) body,...} A call to stored (..)=(or:=) body
(..)/; body
Function[{..}, body][..]
With[{..}, body]
Module[{..}, body]

Applying these ideas to your example

In[3]:=
With[{x=7},{a_->x^2, x_->x^2, x_}]//InputForm

Out[3]//InputForm=
{a$_ -> 49, x_ -> x^2, Pattern[7, _]}


we see that x in {x=7) has as target the x in a_->x^2 and the x in x_,
but not the x's in x_->x^2, which are not free. So a is replaced by a$
and then, by the way that With evaluates, each free x in {a$_ -> x^2,
x_ -> x^2, Pattern[x, _]} is replaced by 7. The evaluation then
continues to give the output shown above.

Here are some more examples.


In[4]:=
Function[x,{a_->x^2, x_->x^2, x_}][7]//InputForm Out[4]//InputForm=
{a$_ -> 49, x_ -> x^2, Pattern[7, _]}

In[5]:=
Module[{x=7},{a_->x^2, x_->x^2, x_}]//InputForm Out[5]//InputForm=
{a$_ -> 49, x_ -> x^2, x$5_}

(Module works via an assignment x$5 = 7 and Pattern has the attribute
HoldFirstm so x$5_ does not become 7_ )

In[6]:=
7/.x_->{a_->x^2, x_->x^2, x_}
Out[6]=
{a$_ -> 49, Pattern[7, _] -> 49, Pattern[7, _]}

(we also get several warnings)

Allan

------------------------------------------------------------- Allan
Hayes
Training and Consulting
Leicester UK
http://www.haystack.demon.co.uk
hay at haystack.demon.co.uk
voice: +44 (0)116 271 4198
fax: +44(0)116 271 8642











  • Prev by Date: Employment offer
  • Next by Date: Re: Graphics Line Styles
  • Previous by thread: Scoped variables
  • Next by thread: diary command?