Re: Context Problem

*To*: mathgroup at smc.vnet.net*Subject*: [mg109441] Re: Context Problem*From*: Albert Retey <awnl at gmx-topmail.de>*Date*: Tue, 27 Apr 2010 04:06:54 -0400 (EDT)*References*: <hr3k5q$c44$1@smc.vnet.net>

Hi, > I would like to export rules from a package in such a way that the pattern > symbols did not include any contexts and in which I also did not have to > export the symbols used in the pattern. In addition, If possible, I would > like this to work even if the package was loaded from a notebook that had > something other than the Global` context. > > > > Here is a sample package and exported rule: > > > > BeginPackage["PackageContext`"]; > > > > Rule1::usage = "Rule1 is a test exported rule."; > > > > Begin["`Private`"]; > > > > Rule1 = Cos[x_]^2 + Sin[x_]^2 -> 1; > > > > End[]; > > > > EndPackage[]; > > > > Then, if we evaluate Rule1 we obtain a very clumsy version of the rule, > although I suppose it will work. > > > > Rule1 > > Cos[PackageContext`Private`x_]^2 + Sin[PackageContext`Private`x_]^2 -> 1 > > > > What I would like is the rule as written in the package. Is there a way to > do this? There is a problem with your expectation: I think every symbol is per definition in a context, so it is strictly not possible to define a symbol which "does not include a context". Whether you see the "clumsy version" or just the symbol name depends on whether the symbols context is part of $ContextPath at the time the output is shown or not. Considering this one way to achieve something that behaves as you probably want is this: Rule1 := Block @@ Join[ List /@ ToExpression["x", InputForm, Hold], Hold[(Cos[y_]^2 + Sin[y_]^2 -> 1) /. y -> Symbol["x"]] ] What this does is to create the symbol x not at the time the rule is defined but only when the rule is called. So it will create the symbol x in the context where Rule1 is used and hence should in all but very exotic cases be shown as just x without its context shown. Since you don't know the context where the x will be created it makes sense to ensure it will not cause problems if there are definitions for this new x. This makes the whole definition somewhat clumsy itself, but the user will usually not see it. The whole procedure is of course rather inefficient since it will run the above code every time you call it. I don't know if this is worth the advantage of a "nicer looking output" in your case, but depending on the purpose of the package efficiency might not be so crucial... hth, albert

**Re: Database memory usage**

**Re: Database memory usage**

**Re: Context Problem**

**Re: Context Problem**