8

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.

masterxilo
  • 5,739
  • 17
  • 39
vapor
  • 7,911
  • 2
  • 22
  • 55
  • 1
    Take a look here: http://mathematica.stackexchange.com/q/115849/5478 – Kuba May 30 '16 at 16:39
  • 1
    $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. – Szabolcs May 30 '16 at 16:45
  • @Szabolcs so you mean there is a parsing step that adds Context to symbols first, which is not in http://reference.wolfram.com/language/tutorial/Evaluation.html. Is the parsing step executed before the whole evaluation? – vapor May 30 '16 at 16:51
  • 1
    @Szabolcs would you mind telling me how you got this information? Is it from the docs or some smart investigation? – vapor May 30 '16 at 17:00
  • "Parsing" means converting the text that you entered into an actual expression. When Mathematica reads x, it immediately needs to decide whether this refers to a`x or b`x or something else. Evaluation can only start after a full expression is read it. When you add parentheses, you make those separate lines into a single expression. – Szabolcs May 30 '16 at 17:16
  • 1
    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. – Szabolcs May 30 '16 at 17:18
  • @Szabolcs thanks you very much for sharing this, I will try to investigate more individually in the future – vapor May 30 '16 at 17:21
  • I added another example illustrating a similar thing. – masterxilo Jun 30 '16 at 14:58
  • @happyfish is my recent answer enough to mark it a duplicate? Where does a package have to be loaded? – Kuba Jun 30 '16 at 15:08
  • @Kuba of course. I really appreciate your detail explanation on this problem. – vapor Jul 01 '16 at 06:26
  • @happyfish I'm glad you find it useful. Feedback appreciated, if anything is not clear or could be rephrased for readers benefit just tell me :) – Kuba Jul 01 '16 at 06:27

1 Answers1

1

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.

masterxilo
  • 5,739
  • 17
  • 39