RE: 2nd arg to BeginPackage

*To*: mathgroup at smc.vnet.net*Subject*: [mg24772] RE: [mg24760] 2nd arg to BeginPackage*From*: Wolf Hartmut <hwolf at debis.com>*Date*: Thu, 10 Aug 2000 00:31:54 -0400 (EDT)*Sender*: owner-wri-mathgroup at wolfram.com

> -----Original Message----- > From: Daniel Reeves [SMTP:dreeves at eecs.umich.edu] To: mathgroup at smc.vnet.net > Sent: Wednesday, August 09, 2000 8:33 AM > To: mathgroup at smc.vnet.net > Subject: [mg24760] 2nd arg to BeginPackage > > My first question is, there seems to be a discrepancy between the > documentation for BeginPackage: > BeginPackage["Package`", {"Needed1`", "Needed2`", ...}] > vs how standard packages use it: > BeginPackage["Package`", "Needed1`", "Needed2`", ...] > > My next question is, why doesn't either form do what I expect here: > > BeginPackage["TestPkg`", "Statistics`"]; > ff::usage = "Some function that uses CDF." > Begin["`Private`"]; > ff[a_] := CDF[NormalDistribution[0,1], a] > End[]; (* Private context *) > EndPackage[]; > > In the definition of ff, it thinks the context for CDF (and > NormalDistr) is TestPkg`Private` instead of Statistics`... > > How can I avoid spelling out the contexts for things like CDF? > > Thanks a lot, > Daniel Reeves > > -- -- -- -- -- -- -- -- -- -- -- -- > Daniel Reeves http://ai.eecs.umich.edu/people/dreeves/ > > "You think you know when you learn, > are more sure when you can write, > even more when you can teach, > but certain when you can program." -- Alan Perlis > > [Hartmut Wolf] Dear Daniel, to your second question first: this seems to be a weekness with the package "Statistics`ContinuousDistributions`". See: In[1]:= BeginPackage["TestPkg`", "Statistics`ContinuousDistributions`"]; {$Context, $ContextPath} ff::usage = "Some function that uses CDF"; Begin["`Private`"]; ff[a_] := CDF[NormalDistribution[0, 1], a] End[]; (*Private context*) EndPackage[]; Out[2]= {"TestPkg`", {"TestPkg`", "Statistics`ContinuousDistributions`", "System`"}} In[8]:= ?? ff >From In[8]:= "Some function that uses CDF" >From In[8]:= ff[TestPkg`Private`a_] := TestPkg`Private`CDF[ NormalDistribution[0, 1], TestPkg`Private`a] In[9]:= CDF[NormalDistribution[0, 1], a] Out[9]= 1/2*(1 + Erf[a/Sqrt[2]]) In[10]:= ff[a] Out[10]= TestPkg`Private`CDF[TestPkg`Private`NormalDistribution[0, 1], a] You see, NormalDistribution has been loaded, yet the context did not appear on $ContextPath within your package. So the Symbols could not be resolved and the definition of ff goes awry. If you explicitly include them within your "needs list" you'll succeed: >>>shut down and restart the kernel<< In[1]:= BeginPackage["TestPkg`", "Statistics`Common`DistributionsCommon`", "Statistics`NormalDistribution`", "Statistics`ContinuousDistributions`"]; {$Context, $ContextPath} ff::usage = "Some function that uses CDF"; Begin["`Private`"]; ff[a_] := CDF[NormalDistribution[0, 1], a] End[]; (*Private context*) EndPackage[]; Out[2]= {"TestPkg`", {"TestPkg`", "Statistics`Common`DistributionsCommon`", "Statistics`NormalDistribution`", "Statistics`ContinuousDistributions`", "System`"}} In[7]:= ?? ff >From In[7]:= "Some function that uses CDF" >From In[7]:= ff[TestPkg`Private`a_] := CDF[NormalDistribution[0, 1], TestPkg`Private`a] In[8]:= CDF[NormalDistribution[0, 1], a] Out[8]= 1/2*(1 + Erf[a/Sqrt[2]]) In[9]:= ff[a] Out[9]= 1/2*(1 + Erf[a/Sqrt[2]]) As to your first question: there are a lot of System functions working outside of their documented range (and used so by Wolfram.Inc programmers), so don't mind. In[10]:= Unprotect[Needs] In[11]:= Needs[context_] := Print["Context = ", context, " was needed"] In[12]:= BeginPackage["Package`", {"Needed1`", "Needed2`"}] EndPackage[] >From In[12]:= "Context = "\[InvisibleSpace]"Needed1`"\[InvisibleSpace]" was needed" >From In[12]:= "Context = "\[InvisibleSpace]"Needed2`"\[InvisibleSpace]" was needed" Out[12]= "Package`" In[14]:= BeginPackage["Package`", "Needed1`", "Needed2`"] EndPackage[] >From In[14]:= "Context = "\[InvisibleSpace]"Needed1`"\[InvisibleSpace]" was needed" >From In[14]:= "Context = "\[InvisibleSpace]"Needed2`"\[InvisibleSpace]" was needed" Out[14]= "Package`" Kind regards, Hartmut