[Date Index]
[Thread Index]
[Author Index]
Re: Context Problem
*To*: mathgroup at smc.vnet.net
*Subject*: [mg109429] Re: Context Problem
*From*: "David Park" <djmpark at comcast.net>
*Date*: Tue, 27 Apr 2010 04:04:43 -0400 (EDT)
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
djmpark at comcast.net
http://home.comcast.net/~djmpark/
From: David Park [mailto:djmpark 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
djmpark at comcast.net
<http://home.comcast.net/~djmpark> http://home.comcast.net/~djmpark/
Prev by Date:
**Re: Database memory usage**
Next by Date:
**Re: Dynamic evaluation of layered networks**
Previous by thread:
**Re: Context Problem**
Next by thread:
**Re: Context Problem**
| |