I have a symbol that has a very large list of Downvalues. One of the rules in this list should be applied before the others because it simplifies many expressions, so when I define this symbol I would like that rule to be at the top of the Downvalues. But I am having trouble doing such a thing because whenever I refer to that rule as I try to manipulate the Downvalues mathematica will apply a conflicting rule.
Here is an example. I want testFunction[0,0] to evaluate to "Success" by prepending the newRule to testFunction's Downvalues:
ClearAll[testFunction];
testFunction[0, 0] := "Failure";
newRule := testFunction[x_, y_] :> "Success";
DownValues[testFunction] =
PrependTo[DownValues[testFunction], newRule];
however we find
testFunction[0, 0]
Out[1]=2
this is no mystery because we can inspect the downvalues:
Downvalues[testFunction]
{HoldPattern["Success"] :> "Success", HoldPattern[testFunction[0, 0]] :> "Failure"}
What is the robust, general way to accomplish this?
testFunction[0,0] :=expression after changing "Failure" to "Success". DownValues will be sorted by specificity, but after that, they are just sorted by the order in which each rule was defined. So, you either need to make a more specific rule or change the order in which definitions are executed. – lericr Mar 23 '22 at 16:32