Re: New version, old bugs
- To: mathgroup at smc.vnet.net
- Subject: [mg44221] Re: New version, old bugs
- From: Maxim <dontsendhere@.>
- Date: Wed, 29 Oct 2003 03:34:04 -0500 (EST)
- References: <bn5e16$6ba$1@smc.vnet.net> <bnaouu$4j3$1@smc.vnet.net> <bnfsjr$o59$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Consider In[1]:= With[{x = 1}, Function[x, x]] With[{x = 1}, Function[x, x] -> 1] Out[1]= Function[x, x] Function::flpar: Parameter specification 1 in Function[1, 1] should be a symbol or a list of symbols. Out[2]= Function[1, 1] -> 1 In the second example x inside Function is still Function's local variable; moreover, no new local symbols are introduced because only pattern names are local to Rule. So this seems to be a hole in the implementation of scoping. It is a bit surprising that it has survived up to version 5, but this is a minor problem which will probably be fixed in the next couple of years or so. Mathematica scoping has deeper problems. What happens in the following example? In[1]:= Module[ {sub,f}, sub[a_,b_]:=Module[ {aa=a,x}, aa-b ]; f[x_]=sub[x,x] ] Out[1]= x$-x$38 To understand it we have to start with how Mathematica deals with name conflicts in cases like In[2]:= Module[{f}, f[y_] := Hold @ Module[{x = 1}, y]; f[x] ] Out[2]= Hold[Module[{x$ = 1}, x]] And what if we pass x$ as the argument? In[3]:= Module[{f}, f[y_] := Hold @ Module[{x = 1}, y]; f[x$] ] Out[3]= Hold[Module[{x$ = 1}, x$] So this would come out differently, which is why by convention names like x$ shouldn't be used explicitly. But can this collision arise internally? Now, this is never an issue in the case of nested functions, because before function body is evaluated, binding with actual parameters must take place, and therefore x$ will not appear when body evaluation is performed. But there is a situation where this may happen: with Set and Rule, right-hand sides of which are evaluated immediately. And then indeed we have a problem: In[4]:= Block[{f,g}, f[x_] := Module[{y = 1}, x]; g[y_] = f[y] ] Module[{f,g}, f[x_] := Module[{y = 1}, x]; g[y_] = f[y] ] Out[4]= y Out[5]= 1 The only difference is that we have f in one case and f$1 in another (forcing f$1[y] to be transformed to f$1[y$]), and we get two different outcomes. Putting it another way, the result in the second case will depend on the name I choose for the local variable in module f. The conclusion is that Mathematica scoping breaks down here. It's not clear if the current renaming heuristics is sufficient to fix this problem. Maxim Rytin m.r at prontomail.com