MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

Question about how Contex resolving works in Mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg15177] Question about how Contex resolving works in Mathematica
  • From: ozan at matematik.su.se (Ozan Oektem)
  • Date: Fri, 18 Dec 1998 23:20:21 -0500
  • Organization: Department of Mathematics, Stockholm University
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,

  I have real big problems understanding how Mathematica handels
Contexts. Let me give an example to illustrate the things I can't
understand.

Say that we have two packages "A.m" and "B.m" (source code listed at the
end) in a directory called "test" (that is searched by Mathematica). 
They define the procedures "procA" and "procB" which are identical. 
The procedures do the following:
1) Print "$Packages", "$Context" and "$ContextPath".  2) Print the
available options for a external procedure in a nonstandard
   external package. I chose "MeanTest" in
"Statistics`HypothesisTests`". 3) Filter (by using
"Utilities`FilterOptions`") the options for "MeanTest"
   and print the filtered options.
Thus, both packages use the external packages
  "Utilities`FilterOptions`"
  "Statistics`HypothesisTests`"
The only difference between the packages are that "A.m" makes a hidden
import of "Statistics`HypothesisTests`" with the command "Needs" after
"Begin[`Private`]", whereas "B.m" makes a usual import of
"Statistics`HypothesisTests`" (both packages make a hidden import of
"Utilities`FilterOptions`").

I have included four different runs (between each run I restart the
kernel) and for each run I have a set of questions.

Loading A only: Here we see that FilterOptions can not filter out the
  options for "MeanTest". Of course this occurs since the
"Options[MeanTest]"   
  command issued inside "A.m" does not  return the short names of the
options,   
  instead it returns the full names. Why? Even though "A.m" makes a
hidden 
  import of "Statistics`HypothesisTests`", the command
"Options[MeanTest]" is 
  issued inside "A.m". Is there a simple way to filter out the options
for 
  "MeanTest" by using "FilterOptions"?

Loading B only: Things works as supposed, i.e. "Options[MeanTest]"
returns
  the short names for the options. Therefore FilterOptions also works.

Loading A first then B: Now things really become strange. First of all,
why
  does loading "A.m" at all affect print statements made inside "B.m"? 
  "KnownVariance" is probably an option defined in 
            "Statistics`Common`PopulationsCommon`" 
  which is loaded unhidden by "Statistics`HypothesisTests`". It seems
that the 
  fact that "A.m" loads "Statistics`HypothesisTests`" in a hidden way
(which 
  implies that "A.m" also loads "Statistics`Common`PopulationsCommon`"
in a 
  hidden way) prevents "B.m" from loading
"Statistics`Common`PopulationsCommon`"   
  unhidden. This would explain the falior for filtering out the 
  "KnownVariance"-option in "B.m". Thus, one must explicitly load   
  "Statistics`Common`PopulationsCommon`" in "B.m" for "B.m" to behave as
when it 
  was loaded alone. This is strange however, since inside
"Begin["`Private`"]", 
  the package "B.m" should only know about what has been loaded by
itself. Is 
  there a better way of ensuring that "A.m" does not affect "B.m"?

Loading B first then A: Again the same explanation as in the case above
  would apply. This time the loading of "B.m" first causes "A.m" to
behave 
  differently. 

The disturbing thing is that two packages that has nothing to do with
each other (neither calls the other, nor do they define the same
symbols) except that they load the same underlying package, still
affects the internal behavior when loaded at the same time. I really
want to make sure that this does not happend. Is there a simple way to
guarantee that?

Loading A only:
---------------
In[1]:=<<test`A`;

In[2]:=procA[FullReport->False,KnownVariance->True]

       Loaded Packages 
      
       {Utilities`FilterOptions`,
        Statistics`Common`PopulationsCommon`,
        Statistics`Common`DistributionsCommon`,
        Statistics`DescriptiveStatistics`,
        Statistics`NormalDistribution`,
        Statistics`HypothesisTests`,
        test`A`,Global`,System`}

       The current context path is 

       {test`A`,Graphics`Animation`,Global`,System`}

       Availabel optionas for MeanTest and default values:

      
{Statistics`Common`PopulationsCommon`KnownStandardDeviation->None,
        Statistics`HypothesisTests`FullReport->False,
        Statistics`Common`PopulationsCommon`KnownVariance->None,
        Statistics`HypothesisTests`SignificanceLevel->None,
        Statistics`HypothesisTests`TwoSided->False}

       Options for MeanTest by FilterOptions are 

       {}

Loading B only:
---------------
In[1]:=<<test`B`;

In[2]:=procA[FullReport->False,KnownVariance->True]

       Loaded Packages 
      
       {Utilities`FilterOptions`,
        Statistics`Common`PopulationsCommon`,
        Statistics`Common`DistributionsCommon`,
        Statistics`DescriptiveStatistics`,
        Statistics`NormalDistribution`,
        Statistics`HypothesisTests`,
        test`B`,Global`,System`}

       The current context path is 

       {test`B`,Statistics`HypothesisTests`,
        Statistics`Common`PopulationsCommon`,
        Statistics`NormalDistribution`,
        Statistics`Common`DistributionsCommon`,
        Statistics`DescriptiveStatistics`,
        Graphics`Animation`,Global`,
        System`}

       Availabel optionas for MeanTest and default values:

       {KnownStandardDeviation->None,
        FullReport->False,
        KnownVariance->None,
        SignificanceLevel->None,
        TwoSided->False}

       Options for MeanTest by FilterOptions are 

       {FullReport->False,KnownVariance->True}


Loading A first then B:
-----------------------
In[1]:=<<test`A`;
       <<test`B`;

In[2]:=procB[FullReport->False,KnownVariance->True]
 
       Loaded Packages 
      
       {test`B`,Utilities`FilterOptions`,
        Statistics`Common`PopulationsCommon`,
        Statistics`Common`DistributionsCommon`,
        Statistics`DescriptiveStatistics`,
        Statistics`NormalDistribution`,
        Statistics`HypothesisTests`,
        test`A`,Global`,System`}

       The current context path is 

       {test`B`,Statistics`HypothesisTests`,
        test`A`,Graphics`Animation`,Global`,System`}

       Availabel optionas for MeanTest and default values:

      
{Statistics`Common`PopulationsCommon`KnownStandardDeviation->None,
        FullReport->False,
        Statistics`Common`PopulationsCommon`KnownVariance->None,
        SignificanceLevel->None,
        TwoSided->False}

       Options for MeanTest by FilterOptions are 

       {FullReport->False}

In[3]:=procA[FullReport->False,KnownVariance->True]
 
       Loaded Packages 
       
       {test`B`,Utilities`FilterOptions`,
        Statistics`Common`PopulationsCommon`,
        Statistics`Common`DistributionsCommon`,
        Statistics`DescriptiveStatistics`,
        Statistics`NormalDistribution`,
        Statistics`HypothesisTests`,
        test`A`,Global`,System`}

       The current context path is 

       {test`B`,Statistics`HypothesisTests`,
        test`A`,Graphics`Animation`,Global`,System`}

       Availabel optionas for MeanTest and default values:

      
{Statistics`Common`PopulationsCommon`KnownStandardDeviation->None,
        FullReport->False,
        Statistics`Common`PopulationsCommon`KnownVariance->None,
        SignificanceLevel->None,
        TwoSided->False}

       Options for MeanTest by FilterOptions are 

       {FullReport->False}

Loading B first then A:
-----------------------
In[1]:=<<test`B`;
       <<test`A`;

In[2]:=procB[FullReport->False,KnownVariance->True]
 
       Loaded Packages 
      
       {test`A`,Utilities`FilterOptions`,
        Statistics`Common`PopulationsCommon`,
        Statistics`Common`DistributionsCommon`,
        Statistics`DescriptiveStatistics`,
        Statistics`NormalDistribution`,
        Statistics`HypothesisTests`,
        test`B`,Global`,System`}

       The current context path is 

       {test`A`,test`B`,Statistics`HypothesisTests`,
       
Statistics`Common`PopulationsCommon`,Statistics`NormalDistribution`,
        Statistics`Common`DistributionsCommon`,
        Statistics`DescriptiveStatistics`,
        Graphics`Animation`,Global`,System`}

       Availabel optionas for MeanTest and default values:

       {KnownStandardDeviation->None,
        FullReport->False,
        KnownVariance->None,
        SignificanceLevel->None,
        TwoSided->False}

       Options for MeanTest by FilterOptions are 

       {FullReport->False,KnownVariance->True}

In[3]:=procA[FullReport->False,KnownVariance->True]
 
       Loaded Packages
      
       {test`A`,Utilities`FilterOptions`,
        Statistics`Common`PopulationsCommon`,
        Statistics`Common`DistributionsCommon`,
        Statistics`DescriptiveStatistics`,
        Statistics`NormalDistribution`,
        Statistics`HypothesisTests`,
        test`B`,Global`,System`}

       The current context path is 

       {test`A`,test`B`,Statistics`HypothesisTests`,
       
Statistics`Common`PopulationsCommon`,Statistics`NormalDistribution`,
        Statistics`Common`DistributionsCommon`,
        Statistics`DescriptiveStatistics`,
        Graphics`Animation`,Global`,System`}

       Availabel optionas for MeanTest and default values:

       {KnownStandardDeviation->None,
        FullReport->False,
        KnownVariance->None,
        SignificanceLevel->None,
        TwoSided->False}

       Options for MeanTest by FilterOptions are 

       {FullReport->False,KnownVariance->True}


Source code for A.m
-------------------
BeginPackage["test`A`"]

procA::usage="..."

Begin["`Private`"]

Needs["Statistics`HypothesisTests`"] Needs["Utilities`FilterOptions`"]

procA[opts___]:=
  Module[{optsForMeanTest},
  optsForMeanTest=FilterOptions[MeanTest,opts];


  Print["Loaded Packages "];
  Print[$Packages];
  Print["The current context path is "];
  Print[$ContextPath];
  Print["Availabel options for MeanTest and default values:"];
  Print[Options[MeanTest]];
  Print["Options for MeanTest by FilterOptions are "];
  Print[{optsForMeanTest}];
]

End[]

EndPackage[]

Source code for B.m
-------------------
BeginPackage["test`B`","Statistics`HypothesisTests`"]

procB::usage="..."

Begin["`Private`"]

Needs["Utilities`FilterOptions`"]

procB[opts___]:=
  Module[{optsForMeanTest},
  optsForMeanTest=FilterOptions[MeanTest,opts];

  Print["Loaded Packages "];
  Print[$Packages];
  Print["The current context path is "];
  Print[$ContextPath];
  Print["Availabel options for MeanTest and default values:"];
  Print[Options[MeanTest]];
  Print["Options for MeanTest by FilterOptions are "];
  Print[{optsForMeanTest}];
]

End[]

EndPackage[]

-- 
Ozan Oektem                  Phone : +46-8-16 49 15 Department of
Mathematics   Fax   : +46-8-612 67 17 Stockholm University       
E-mail: ozan at matematik.su.se 106 91 Stockholm, Sweden


  • Prev by Date: Re: Compile and NormalDistribution
  • Next by Date: FW: Re: Frequencies function
  • Previous by thread: Re: Including Mathematica notebooks in LaTeX
  • Next by thread: Re: Question about how Contex resolving works in Mathematica