Re: Context Problem

*To*: mathgroup at smc.vnet.net*Subject*: [mg109468] Re: Context Problem*From*: Rui <rui.rojo at gmail.com>*Date*: Wed, 28 Apr 2010 02:00:30 -0400 (EDT)*References*: <hr65qi$joj$1@smc.vnet.net>

On Apr 27, 5:04 am, "David Park" <djmp... at comcast.net> wrote: > I want to thank Ingolf Dahl, Bob Hanlon and Patrick Scheibe for their > answers. Using the Global` context is a good solution, except if the package > had been loaded in something other than the Global` context, say on a > Function page. Also I would like to have a simple way to process simply > written rules in the package such that it would return simple looking and > usable rules to the user. > > There are a number of issues I would like to raise because I'm not certain > of the best procedure if we want a package to generate an expression with > new symbols that are in the Context of the notebook using the package. There > are two solutions that might be useful but are unavailable. > > 1) WRI might be able to create a floating context, which had the property > that it took on the context of the notebook or cell in which it is created. > This could then be used by package writers to return created symbols. > > 2) WRI might provide a way for a package routine to retrieve the current > Context in the InputNotebook[]. This could then be used actively in > generating new symbols. > > Otherwise we can obtain "Context free" output by putting the Rule symbols > into existing Contexts such as the Package Context itself, or the System` > context. But is it good practice to usurp simple symbols such as x, y, z > into some special Context? Maybe it is all right if users always read in the > package first. But suppose a number of packages start doing this? > > In any case, here is the package routine again in a form that statically > determines the Context at the time it is read in. The problem is that if the > user changes evaluation from one notebook to another, that have different > Contexts, then the package must be read in anew. I am taking advantage here > of the fact that SymbolName strips any leading Context from a symbol so we > can just attach the new Context. > > System`loadcontext=Context[]; > > BeginPackage["PackageContext`"]; > > Rule1::usage="Rule1 is a test exported rule."; > > Begin["`Private`"]; > > cs[var_Symbol]:=var->Symbol[System`loadcontext<>SymbolName[var]] > > cs[vars:{__Symbol}]:=cs/@vars > > Rule1=(y_ Sin[x_ y_]/;AtomQ[y]->{x,y})//.cs[{x,y}]; > > End[]; > > EndPackage[]; > > Test: > > Rule1 > a Sin[a b] /. Rule1 > > y_ Sin[x_ y_] /; AtomQ[y] -> {x, y} > {b, a} > > David Park > djmp... at comcast.nethttp://home.comcast.net/~djmpark/ > > From: David Park [mailto:djmp... at comcast.net] > > 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? > > David Park > > djmp... at comcast.net > > <http://home.comcast.net/~djmpark>http://home.comcast.net/~djmpark/ David. I think you could do something like Symbol[$Context<>"x"] to make the symbol belong to the current context when the function is called. However, I guess it makes sense to put it inside a block if you want it for output and don't want conflics. I gotta go now, but I think it should be simple to make a function that behaves SymbolOut[{list of symbols}, expression] that treats every symbol inside the list of symbols as a symbol belonging to the context where it was evaluated, taking local values to prevent conflicts. I guess with a With to replace the symbols by Symbol[$Context<>ToString[symbolss]] and a Block to localize them it should be possible. I'll try it when I'm back.