0
$\begingroup$

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?

$\endgroup$
3
  • 2
    $\begingroup$ MMA will always sort DownValues with more specific rules before more general ones. I do not know a way to circumvent this. What you could do, is to create your own list of rules and apply them "by hand" $\endgroup$ Mar 23, 2022 at 15:58
  • 1
    $\begingroup$ Related is mathematica.stackexchange.com/q/106068/45431 $\endgroup$
    – Carl Woll
    Mar 23, 2022 at 16:07
  • $\begingroup$ Is this your real function? This example doesn't make any sense, so it'd be better to see your actual function (or at least something better approximating it). In your example, you want everything to return "Success", so you either just define it that way, or just re-evaluate thetestFunction[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. $\endgroup$
    – lericr
    Mar 23, 2022 at 16:32

0