# How to put replacement rules into a package

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?

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

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.

• 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 :) ). – fales Mar 12 '16 at 16:16
• And also I have Mathematica 10.0. – fales Mar 12 '16 at 16:18
• @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[]). – m_goldberg Mar 12 '16 at 17:19
• Works fine for me. – user1066 Mar 12 '16 at 18:00
• 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 – fales Mar 12 '16 at 18:55

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 = {Globala->2,Globalb->3};
Rules[expr_]:=expr//.RulesDef

End[]
EndPackage[]


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