# How can I pass replacement rules to a package-defined function and have it understand their context?

I am writing a package where I would like to be able to pass replacement rules to a function inside the package and have the function manipulate its symbols using those replacement rules. However, I'm having some trouble implementing it.

(I'm rather baffled because the files used to work and no longer do, coinciding with a change from v10 to v10.1. However, I would be surprised if that is actually the cause as I feel the MWE below completely encapsulates the problem and it looks like a language issue, so instead it may be that I ran the files differently in ways that are hard to reconstruct.)

The specific thing I have in mind is this. Make two files, testpackage.m containing

BeginPackage["Test"];
replace::usage = "Returns var/.rules.";
Begin["Private"];
replace[rules_] := ReplaceAll[var, rules]
End[];
EndPackage[];


and testnotebook.nb containing

Needs["Test", "/path/to/testpackage.m"]

replace[var-> 0]
replace[TestPrivatevar -> 0]


Open the notebook file on a fresh session and load the package. I would like the usage replace[var-> 0] to return 0 - that is, I would like replace to be able to tell var to zero itself. However, it returns TestPrivatevar, which upon closer examination of the code is in fact a pretty reasonable thing for it to return. The reasonableness of this is compounded by the fact that replace[TestPrivatevar -> 0] will return 0 as expected.

As I said, the usage replace[var->0] used to work fine for my bigger package, and indeed it can work fine in some circumstances (which I cannot pin down exactly, and which I have not been able to reproduce in this MWE yet) if the notebook file is opened on the same Mathematica instance (and thus kernel session) as the package file, with the package file (in practice, a .m auto-generated from a notebook) having been used to run the definition of replace.

So: I have some wonky code, which is probably returning the results it needs to but not the results I want it to, and I would like to amend it to return it to the desired behaviour. In particular: is there some way to take the call

ReplaceAll[var, rules]


and make it forget contexts for a minute?

Alternatively, I'm painfully aware of the possibility that this simply means that I've completely misunderstood the concept of contexts, and that what I want simply isn't doable. This would mean that the behaviour I want, for example, can only be produced by e.g. adding the symbol var to the Test context, such as by adding the line

var::usage = "var is the variable that gets replaced";


below the usage message for replace. I'm reluctant to take such a step as the package is relatively big (and organically grown, so somewhat messy and without comprehensive unit tests, unfortunately) and I'm afraid of breaking something. If there are no shortcuts then I'm OK with it, but I would like to know if there's a hackier way to get the behaviour I want.

• I feel that the better example is needed since this minimal one is just badly designed. User should only put 0 in replace. I don't recall any MMA function with similar usage. Even for solve etc. you have to tell MMA which are variables we are going to do something with in an expression.
– Kuba
Jan 22, 2016 at 6:49
– Kuba
Jan 22, 2016 at 6:53
• @Kuba I suspect that simply my original code is just badly designed, but it's probably not worth the effort to fix it given the future use it will get. Thanks all for your responses - they are all helpful. I'm accepting Chris's solution as it's the simplest to implement, though it is not very suitable for a package that one will distribute given the issues pointed out by Kuba and bbgodfrey. Jan 22, 2016 at 14:20

Try

replace[rules_] := ReplaceAll[Globalvar, rules]


replace[var -> 0]
replace[TestPrivatevar -> 0]


yields

0
var


as I think you'd like.

• What is the outcome of making this change? Jan 22, 2016 at 1:23
• It won't work if the user works in a notebook that has a CellContext->Notebook.
– Kuba
Jan 22, 2016 at 9:37
• @Kuba Huh, never heard of that option. Is it in widespread use? I'm interested because I'm working on a package that uses that Global context a bit. Jan 22, 2016 at 12:18
• @ChrisK I use Evaluation>NoteBook's Default Context>Unique to This Notebook routinely to avoid cross-talk among open notebooks (including Mathematica documentation). Jan 22, 2016 at 14:02
• @ChrisK I have the context unique to each notebook set as the default. I think of each notebook as its own programme. I don't create anything in the Global context. Jan 22, 2016 at 15:35

You can make use of Formal Symbols.

BeginPackage["Test"];
replace::usage = "Returns \[FormalV]/.rules.";

Begin["Private"];

replace[rules_] := \[FormalV] /.rules

End[];
EndPackage[];


Then in the notebook.

replace[\[FormalV] -> 0]
(* 0 *)


Hope this helps.

• Does this work for any symbol already on the $ContextPath during both package initialization and in the notebook, but not defined? Jul 22, 2019 at 2:43 is there some way to take the call ReplaceAll[var, rules]and make it forget contexts for a minute? BeginPackage["Test"]; replace::usage = "Returns var/.rules."; print; Begin["Private"]; replace[rules_] := ReplaceAll[Symbol[$Context <> "var"], rules]

End[];
EndPackage[];
`

Not general, but works.