4
$\begingroup$

I have a replacement rules list, but its quite long and therefore I would like to make a standalone package of it. I tried but haven't succeeded. A simple example of my approach is

BeginPackage["Rules`"]

Rules::usage="Rules[x] applies rules on expression x."

Begin["Private`"]

RulesDef = {a->2,b->3};
Rules[expr_]:=expr//.RulesDef

End[]
EndPackage[]

However, when I call Rules[a + b], it gives me a + b instead of 5.

How should I put such a rules into a package, if it is possible?

$\endgroup$
3
  • $\begingroup$ closely related: 104584 $\endgroup$
    – Kuba
    Commented Mar 12, 2016 at 13:07
  • 1
    $\begingroup$ I saw this, however, if I understand it right, I have reverse problem, I wan to pass rules from the package, not into. $\endgroup$
    – fales
    Commented Mar 12, 2016 at 13:36
  • $\begingroup$ Yes, but it's closely related so I've linked it for future visitors. $\endgroup$
    – Kuba
    Commented Mar 12, 2016 at 13:37

2 Answers 2

3
$\begingroup$

Because you want your rules to be evaluated in the same context as the expression you are applying them to, I don't think a real package is what you want. I think saving your rule definition in a package (.m) file without the package trimmings -- which create a new context -- will work for you.

  1. Make a new notebook with just one cell containing

    myRules = {a -> 2, b -> 3};
    
  2. Make the cell a initialization cell.

  3. Save the notebook to FileNameJoin[{$UserBaseDirectory, "Applications"}] as myRules.m.

Now in any note book you can write

<< "myRules.m"
a + b /. myRules

5

As an alternative, if you have V10.2 or later, you could make myRules a LocalSymbol.

$\endgroup$
6
  • $\begingroup$ Thank you for your answer, unfortunately, it is not working for me. I says: "{myRules} is neither a list of replacement rules nor a valid \ dispatch table, and so cannot be used for replacing." Moreover, the set of commands I would like to put into the package also contains some functions which should manipulate the expression in the notebook according to their definition in the package and the replacement rules (idealy also in the same package :) ). $\endgroup$
    – fales
    Commented Mar 12, 2016 at 16:16
  • $\begingroup$ And also I have Mathematica 10.0. $\endgroup$
    – fales
    Commented Mar 12, 2016 at 16:18
  • 1
    $\begingroup$ @fales. You must have done something wrong. To get an idea of what is going wrong, evaluate myRules immediately after loading myRules.m with << -- you should see {a -> 2, b -> 3}. As to your second issue, you could put myRules = {a -> 2, b -> 3}; in an initialization cell at beginning of your package file (before BeginPackage[]). $\endgroup$
    – m_goldberg
    Commented Mar 12, 2016 at 17:19
  • $\begingroup$ Works fine for me. $\endgroup$
    – user1066
    Commented Mar 12, 2016 at 18:00
  • $\begingroup$ Thank you for reaction, you are of course right, it was my fault. I don't know how, anyway, all lines got commented during the save to .m $\endgroup$
    – fales
    Commented Mar 12, 2016 at 18:55
3
$\begingroup$

I think using the usual package syntax is fine. You just need to add the Symbol RulesDef to the global context. Thus your package could look like this:

BeginPackage["Rules`"]

RulesDef::usage="Boo"
Rules::usage="Rules[x] applies rules on expression x."

Begin["Private`"]

RulesDef = {Global`a->2,Global`b->3};
Rules[expr_]:=expr//.RulesDef

End[]
EndPackage[]

Its necessary to addd Global` infront of the variables cause otherwise their context will be private.

$\endgroup$
0

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.