Re: Re: locally changing Options
- To: mathgroup at smc.vnet.net
- Subject: [mg108364] Re: [mg108337] Re: locally changing Options
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Mon, 15 Mar 2010 00:06:42 -0500 (EST)
- References: <hnakgk$5oa$1@smc.vnet.net> <hndb35$dee$1@smc.vnet.net>
Hi, Instead, lets define System`f[x___] := {x}; > > The default notebook $ContextPath is similar to {..., "System`", > "Global`"}; > > The same code above, Block[{f=System`f[1,##]&},f[2]], will now become > recursive. > > For some reason, it's REMOVING the System` context on System`f during > evaluation, which is causing it to get scoped by Block since > obviously, Block[{f=f[1,##]&},f[1]], will be recursive. > No, it is not removing anything. What's happening is different. Your code above is completely equivalent to Block[{System`f=System`f[1,##]&},System`f[2]], hence the recursion. Look: System`f[x___] := {x}; In[142]:= Block[{f}, Context[f]] Out[142]= "System`" (I assumed that <f> was not defined before in Global` or elsewhere on the $ContextPath). So, the point is not that some context is "removed" on the r.h.s of your variable declaration in Block (there is no such operation as "removing the context", by the way - such a thing would not make sense, since any symbol is in some context. My understanding is that Mathematica internally never works with "short names" - short names exist only as an output formatting for our convenience). What happens is that once <f> is in some context foo` which is on the $ContextPath (and first to contain <f>), then Block[{f=something},body] is equivalent to Block[{foo`f = something}, body]. Moreover, any appearance of fully qualified foo`f will be replaced according to our instruction in the declaration of Block, even if a short form (f) was used there: In[143]:= Block[{f = System`g}, System`f[2]] Out[143]= g[2] since f and foo`f are referring to the same symbol, anywhere inside Block. In a way, this is the essence of dynamic scoping - it works with existing symbols (when they exist). Hope this helps. Regards, Leonid