Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[mg6226] Re: [mg6208] contexts: nailing jelly to a tree?

0 views
Skip to first unread message

David Withoff

unread,
Feb 28, 1997, 3:00:00 AM2/28/97
to

> Every time I think I am beginning to get close to starting to
> understand packages and contexts, something new surprises me. Here's
> the latest. Explanations welcome!
>
> I indicate two Mathematica sessions below. Each was done with a
> "fresh Mathematica". The version is 3.0.
>
> (* New session #1: *)
>
> BeginPackage["test`"]
> Begin["Private`"]
> y1 = 99;
> Print[Context[y1]]
> End[]
> EndPackage[]
>
> (* output *)
>
> test`
>
> Private`
>
> Private` (* that's what I expect *)
>
> Private`
>
> (*******************************)
>
> (* New session #2: *)
>
> (* input *)
>
> BeginPackage["test`"];
> Begin["Private`"];
> y1 = 99;
> Print[Context[y1]];
> End[];
> EndPackage[];
>
> (* output *)
>
> Global` (* this is the surprise *)
>
> (**********************)
>
> Does this mean that surpressing return of a result
> (via terminal ; on a line) from BeginPackage[...] or
> Begin[...] does not create the context?
>
> --
> Murray Eisenberg Internet: mur...@math.umass.edu
> Mathematics & Statistics Dept. Voice: 413-545-2859 (W)
> University of Massachusetts 413-549-1020 (H)
> Amherst, MA 01003 Fax: 413-545-1801

===================================================================

I'd like to start out by saying that I am really glad to see that
you are taking the time to ask all of these questions about contexts.
I suspect that there are a lot of other people who have the same
questions.

Unless I miss my guess, the behavior that you reported is more
closely related to the way that multi-line input is handled, rather
than to contexts.

My guess is that you entered

BeginPackage["test`"];
Begin["Private`"];
y1 = 99;
Print[Context[y1]];
End[];
EndPackage[];

into an Version 3.0 input cell using StandardForm (the default) as
the FormatType.

In a StandardForm input cell, the input above will be processed
as one single input. It is not processed one line at a time. The
same is true of TraditionalForm. In InputForm, this input will be
processed one line at a time.

The first step in processing input is to scan the input for symbols
and other syntax, and construct an expression.

The next step is to evaluate the expression.

Symbols are created in the first step.

BeginPackage["test`"] and Begin["Private`"] are evaluated in the
second step.

You can probably figure out the rest. When the input is processed
all at once, all of the symbols are found or created using the
current settings for $Context and $ContextPath. The symbol y1 in
your input is created in Global` context, since that is the context
that happens to be current when the input is scanned for symbols. It
is only later than BeginPackage["test`"] and Begin["Private`"] are
evaluated, but by then it is too later. The symbol y1 has already
been created in Global` context.

If you enter these same lines into an input cell for which the FormatType
is InputForm, or if you get rid of some of the semicolons, then these
lines will be processed one at a time, or at least the lines that
contain BeginPackage["test`"] and Begin["Private`"] will be processed
before the line that creates y1, and you will get the behavior that
you expected.

I haven't thought about this carefully enough to decide whether or not
I like this behavior. At first sight it seems a little quirky, but to
be fair, I'll need to consider the implications of alternate designs
before making any qualitative comments. When I first saw this, however,
it did take me a while to figure it out, and I am nominally one of the
world's experts on this stuff.

Dave Withoff
Wolfram Research


0 new messages