7
$\begingroup$

I thought when we type a symbol without specifying its Context, it will search all the Context in $ContextPath for existing symbol.

But in Module/Block, or even CompoundExpression, all the symbols will be converted to Global Context regardless of the content of $ContextPath.

Unfortunately, I didn't find any information in the documentation explaining this behavior.Is it a feature? What is purpose of this design?

Here is my example:

enter image description here

enter image description here

enter image description here

enter image description here

Code Here:

AppendTo[$ContextPath, "w`"];
w`ww = 1;
Context[ww]

Similar behaviour can be observed as follows:

Quiet@Remove["A`x", "B`x", "Global`x"];
{A`x, B`x} = {"a", "b"};
$Context = "A`";
Print@x;
$Context = "B`";
Print@x;
$Context = "Global`";

gives a, b, but

(
 Quiet@Remove["A`x", "B`x", "Global`x"];
 {A`x, B`x} = {"a", "b"};
 $Context = "A`";
 Print@x;
 $Context = "B`";
 Print@x;
 $Context = "Global`";
 )

gives Removed[x] twice.

$\endgroup$
  • 1
    $\begingroup$ Take a look here: mathematica.stackexchange.com/q/115849/5478 $\endgroup$ – Kuba May 30 '16 at 16:39
  • 1
    $\begingroup$ $ContextPath affects parsing, not evaluation. Because of the parentheses, (AppendTo[$ContextPath, "w`"]; w`ww = 1; Context[ww]) is parsed in its entirety first, and evaluation begins only afterwards. Before the first part of this is evaluated, $ContextPath isn't changed yet. $\endgroup$ – Szabolcs May 30 '16 at 16:45
  • $\begingroup$ @Szabolcs so you mean there is a parsing step that adds Context to symbols first, which is not in reference.wolfram.com/language/tutorial/Evaluation.html. Is the parsing step executed before the whole evaluation? $\endgroup$ – happy fish May 30 '16 at 16:51
  • 1
    $\begingroup$ @Szabolcs would you mind telling me how you got this information? Is it from the docs or some smart investigation? $\endgroup$ – happy fish May 30 '16 at 17:00
  • 1
    $\begingroup$ Well, it's just a hypothesis, but it's a very reasonable one. To come up with it, there's no other investigation needed than what you already tried and thinking a bit about how Mathematica could theoretically be implemented. You can devise additional tests with ToExpression to try to falsify this hypothesis and gain more confidence. This behaviour was discussed on this site multiple times, but I was too lazy to find it. I assumed Kuba's link pointed to one. $\endgroup$ – Szabolcs May 30 '16 at 17:18
1
$\begingroup$

I think every line or statement that you enter into the frontend is parsed separately, so, when you use a CompoundStatement there is only one parsing process, while there is one for each line when you split them.

From tutorial/Contexts we learn that unknown symbols are created in the $Context when they are not found in $ContextPath. Most likely the lookup for unqualified symbols happens at the parsing stage with the values that $Context, $ContextPath have at that time.

I assume that internally, an "unqualified symbol" (one with an empty Context) is not even a thing.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.