       Re: scope all wrong? in Mathematica 4.1

• To: mathgroup at smc.vnet.net
• Subject: [mg31836] Re: [mg31827] scope all wrong? in Mathematica 4.1
• From: David Withoff <withoff at wolfram.com>
• Date: Fri, 7 Dec 2001 05:55:50 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```> Consider the mathematica definition
>
> rr[x_] := Block[{x}, x = 5; Print["x is ", x]]

Using the same name for both a function argument and for a
local variable is a fairly obvious programming blunder in
any programming language.  Most programming languages either
won't let you do that, or will generate a warning message
and then give some system-dependent result.

Mathematica generates a warning message for most uses
of such a definition, and in some related cases, such as

In:= f[x_] := x /. x_ -> 5

RuleDelayed::rhs:
Pattern x_ appears on the right-hand side of rule
f[x_] :> (x /. x_ -> 5).

generates a warning message when the definition is entered,
to advise the programmer that the input is (probably) not
what was intended.

>   what do you expect here?

I would expect the person who wrote this program to go back
and fix their program.  There isn't anything sensible
that the programming language could do with this.

> Now look at
>
> uu[x_?((x = 5; Print["x is ", x]; True) &)] := x
>
> Usually when one defines a program, nothing is printed.
> Here, we get
> x is 5
> 5
> x is 5
>
> If we do this:
> x =400
> uu
>      x is 5  printed
> the value returned is 70
> and the global x is 5.

The scoping effect here (the fact that evaluating uu
changes the global value of x) is a demonstration that
PatternTest is not a scoping construct.  That is correct.
It isn't.  I suppose that it could be, although I'm not
sure why someone would expect it to be.

If a programmer did mistakenly assume that PatternTest was
a scoping construct, and didn't read the documentation to
find out otherwise, then there is a separate problem, since
the program is wrong even within the context of that incorrect
assumption.  For example, if all of the x's in that definition
are in the same scope, then uu involves an attempt to
assign 5 as the value of 70, which isn't going to work.

The printing effect is an unrelated issue.  Actually, that
one definition by itself would not do any printing.  I
suspect that this example is referring to the printing that
could happen if a second rule for uu was entered and the
heuristics for ordering the two rules involved evaluating
part of this existing rule.  This behavior has no effect on
the resulting program, and certainly isn't a fundamental
feature of the language.  Mostly it's just a curiosity.

> Try this:
> Module[{x}, uu[x_?((x = 5; Print["x is ", x]; True) &)] := x;
>    Print["in Module, x is " , x]]
>
> The expectation in a system supporting lexical scope
> with Module
> is that no use of x inside the module would escape.
> But it does.  The global x is set to 5.

I don't see any escaping variables here.  For example,

In:= x=99;

In:= Module[{x}, uu[x_?((x = 5; Print["x is ", x]; True) &)] := x;
Print["in Module, x is " , x]]

in Module, x is x\$9

In:= x

Out= 99

leaves the global value of x unchanged, as does

In:= uu

x is 5

Out= 400

In:= x

Out= 99

all of which is as it should be.

> The reason this all came up is in correspondence suggesting
> that programs in one computer algebra system could be
> translated into another.  If systems are semantically
> "surprising", it is more difficult.

I'm not sure what surprises this is referring to.  The examples
shown here would translate readily into other programming
languages, although in some cases that just means translating
a programming blunder.  The translation of rr[x_] := Block[{x},
x = 5; Print["x is ", x]] into C, for example, generated a
compiler warning when I tried it.  The main difficulty with
translating Mathematica into other languages is that few if any
languages support the range of programming constructs that
are available in Mathematica.

> I wonder how much
> of mathematica internally depends on wrong scope, or how
> much of the code is susceptible to bugs because of unexpected
> capture of names.

Certainly nothing in Mathematica depends on "wrong scope".  I've
looked at thousands of Mathematica programs over the years, and
even in programs that are otherwise rather badly written I'm not
sure I've ever seen any mistakes of the type illustrated here.

> (I suspect that this has caused a proliferation
> of package names e.g. MySecretNameSpace`x   in Mathematica
> routines).

I have not seen that.

Dave Withoff
Wolfram Research

```

• Prev by Date: Re: scope all wrong? in Mathematica 4.1
• Next by Date: Re: Re: No joy using gcc 2.95.3 + MathLink v3r9 + Windows 2000 SP2
• Previous by thread: Re: scope all wrong? in Mathematica 4.1
• Next by thread: Re: scope all wrong? in Mathematica 4.1