MathGroup Archive 1998

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

Search the Archive

Re: Scoped variables

  • To: mathgroup at
  • Subject: [mg13924] Re: Scoped variables
  • From: "Allan Hayes" <hay at>
  • Date: Mon, 7 Sep 1998 01:22:49 -0400
  • References: <6ssuub$>
  • Sender: owner-wri-mathgroup at

Ersek, Ted R wrote in message <6ssuub$m87 at>...
>Consider the lines below.  You get nothing very interesting at Out[1].
>{a_->x^2,  x_->x^2,  x_}//InputForm
>{a_ -> 4,  x_ -> 4,  x_}
>It works a little different if you do the same thing inside With (see
>With[{x=7},{a_->x^2,  x_->x^2,  x_}]//InputForm
>{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


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




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


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]       ,,


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

With[{x=7},{a_->x^2, x_->x^2, x_}]//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.

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

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

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

(we also get several warnings)


------------------------------------------------------------- Allan
Training and Consulting
Leicester UK
hay at
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?