7
$\begingroup$

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[Test`Private`var -> 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 Test`Private`var, 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[Test`Private`var -> 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.

$\endgroup$
3
  • $\begingroup$ 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. $\endgroup$
    – Kuba
    Commented Jan 22, 2016 at 6:49
  • $\begingroup$ related: Loading Packages with Parameters $\endgroup$
    – Kuba
    Commented Jan 22, 2016 at 6:53
  • $\begingroup$ @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. $\endgroup$ Commented Jan 22, 2016 at 14:20

3 Answers 3

2
$\begingroup$

Try

replace[rules_] := ReplaceAll[Global`var, rules]

in your function. Then

replace[var -> 0]
replace[Test`Private`var -> 0]

yields

0
var

as I think you'd like.

$\endgroup$
7
  • 1
    $\begingroup$ What is the outcome of making this change? $\endgroup$
    – bbgodfrey
    Commented Jan 22, 2016 at 1:23
  • 3
    $\begingroup$ It won't work if the user works in a notebook that has a CellContext->Notebook. $\endgroup$
    – Kuba
    Commented Jan 22, 2016 at 9:37
  • 1
    $\begingroup$ @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. $\endgroup$
    – Chris K
    Commented Jan 22, 2016 at 12:18
  • 2
    $\begingroup$ @ChrisK I use Evaluation>NoteBook's Default Context>Unique to This Notebook routinely to avoid cross-talk among open notebooks (including Mathematica documentation). $\endgroup$
    – bbgodfrey
    Commented Jan 22, 2016 at 14:02
  • 2
    $\begingroup$ @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. $\endgroup$
    – Edmund
    Commented Jan 22, 2016 at 15:35
4
$\begingroup$

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 *)

enter image description here

Hope this helps.

$\endgroup$
1
  • $\begingroup$ Does this work for any symbol already on the $ContextPath during both package initialization and in the notebook, but not defined? $\endgroup$
    – lirtosiast
    Commented Jul 22, 2019 at 2:43
3
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.