7
$\begingroup$

In a recent question [1] OP asked how to make Module scope working inside Set, so how to get:

f[x$123_] = x$123

instead of

f[x_] = x

from

Module[{x}, Defer[f[x_] = x] ]

It is stated in Details of Module ref page that:

Before evaluating expr, Module substitutes new symbols for each of the local variables that appear anywhere in expr except as local variables in scoping constructs.

My question is - why this exception is present? What common usage cases dictated this behaviour? Does this makes something safer?

I don't complain, I'm just curious. I don't know why Module can't just change every x it sees to x$1232? Current rule looks like an exception which causes only troubles.


It is even more interesting when we introduce y which will rename x

Module[{x , y = 1},
 Defer[f[x_] = x y]
 ]
f[x$_] = x$ y$7519

[1] How to scope Pattern labels in rules/set?

$\endgroup$
13
  • $\begingroup$ Sorry, I didn't follow everything. Is this about weirdness like this? x = 1; Module[{x = 2}, Print[x_ -> x]] Note how the x within the Module is actually evaluating to its global value. $\endgroup$
    – Szabolcs
    Commented Jul 3, 2016 at 10:21
  • 1
    $\begingroup$ SetSystemOptions["StrictLexicalScoping" -> True] fixes that. $\endgroup$
    – Szabolcs
    Commented Jul 3, 2016 at 10:56
  • $\begingroup$ Is this a duplicate? Be sure to read all the comments. I asked there why StrictLexicalScoping is not turned on by default. $\endgroup$
    – Szabolcs
    Commented Jul 3, 2016 at 11:04
  • $\begingroup$ @Szabolcs I'm aware of that option but it gives f[x$_] = x$ not x$1231. Moreover, the question is why a particular implementation was chosen. $\endgroup$
    – Kuba
    Commented Jul 3, 2016 at 11:04
  • 1
    $\begingroup$ @Mr.Wizard While Leonid says he will explain "what and why" he rather explains "what and how". There is a statement "such renaming h -> h$123 has to be prevented." which my question really ask about. Why it has to be prevented. You have started to explain it too but with not real examples "why" it is not desired for Set and Rule. So if you want to extend your thought about that specific part, here is a good place ;) $\endgroup$
    – Kuba
    Commented Jul 8, 2016 at 7:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.