Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

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

Search the Archive

Re: Context Problem

  • To: mathgroup at smc.vnet.net
  • Subject: [mg109478] Re: Context Problem
  • From: "David Park" <djmpark at comcast.net>
  • Date: Thu, 29 Apr 2010 02:52:31 -0400 (EDT)

Rui,

That seems like a good solution. Many thanks for suggesting it. 

Here is a new test package. newsymbols will be a list of rules for all of
the symbols used in exported rules. One precaution is that the definitions
have to be delayed so that $Context is not evaluated in the package.

This construction also works if one wishes to generate an expression in some
variable and we wish to give the variable a default value.

BeginPackage["PackageContext`"]; 
Rule1::usage="Rule1 is a test exported rule."; 
Routine1::usage="Routine1[] returns a test polynomial with a default
variable x."; 
Begin["`Private`"]; 
newsymbols:={x->Symbol[$Context<>"x"],y->Symbol[$Context<>"y"]}; 
Rule1:=(y_ Sin[x_ y_]/;AtomQ[y]->{x,y})//.newsymbols; 
Routine1[x_:Automatic]:=
 Module[{var},
  var=If[x===Automatic,Symbol[$Context<>"x"],x];
  var+var^2]; 
End[]; 
EndPackage[];  


Rule1 
a Sin[a b] /. Rule1 
Routine1[]

I tested this with a PackageContext`m file, reading it in both in a regular
Global` context notebook, and in a notebook with its own Notebook context. I
could go back and forth and it evaluated properly in both notebooks and the
x,y symbols were in their respective notebook contexts.

Another solution for rules, suggested to me by Maxim Rytin, is to use Formal
symbols:

formalsymbols = {x -> \[FormalX], y -> \[FormalY]}

Rules seem to be a proper place to use Formal symbols because they are never
given values and being in the System` Context display without Context
information. But these were introduced in Version 6 and may not be familiar
to many users. They have gray dots above and below the characters. They are
not suitable for default variables because we may wish to later give those
values. 


David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/  



From: Rui [mailto:rui.rojo at gmail.com] 

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.
> 

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.





  • Prev by Date: Re: Database memory usage
  • Next by Date: Re: BinCounts to InterpolatingFunction
  • Previous by thread: Re: Context Problem
  • Next by thread: Database memory usage