This shows the default ordering used for the definitions.
In[4]:= DownValues[g]
Out[4]= {HoldPattern[g[x_ + y_]] :> gp[x, y], HoldPattern[g[x_ y_]] :> gm[x,
y]}
This reverses the order of the definitions for g.
In[5]:= DownValues[g] = Reverse[DownValues[g]]
Out[5]= {HoldPattern[g[x_ y_]] :> gm[x, y], HoldPattern[g[x_ + y_]] :> gp[x,
y]}
-----------------------------------
I'm able to duplicate the observations here, and I think I understand the
issues..
In a slightly different case from section 2.5.6 (Making Definitions for
Functions) a specific function and a general function are defined.
---------------------------------
In[1]:= f[x] = u
Out[1]= u
When the specific expression f[x] appears, it is replaced by u. Other
expressions of the form f[argument] are, however, not modified.
In[2]:= f[x] + f[y]
Out[2]= u + f[y]
This defines a value for f with any expression as an "argument".
In[3]:= f[x_] = x^2
Out[3]= \!\(x\^2\)
The old definition for the specific expression f[x] is still used, but the
new general definition for f[x_] is now used to find a value for f[y].
In[4]:= f[x] + f[y]
Out[4]= \!\(u + y\^2\)
----------------------------
If I duplicate this definition set, I can query the rules:
In[213]:= ?f
Global`f
f[x] = u,
f[x_] = x\^2\
In[223]:= DownValues[f]
Out[223]= \!\({HoldPattern[f[x]] :> u, HoldPattern[f[x_]] :> x\^2}\)
But if I attempt to Reverse[] the order using the same approach as that used
in 2.5.13 the result is different:
In[225]:= DownValues[f] = Reverse[DownValues[f]]
Out[225]= \!\({HoldPattern[f[x_]] :> x\^2, HoldPattern[f[x]] :> u}\)
Here the Out seems to confirm the reversal, but a direct check of the order
gives:
In[226]:= DownValues[f]
Out[226]= \!\({HoldPattern[f[x]] :> u, HoldPattern[f[x_]] :> x\^2}\)
and a test of f[x]+f[y], which gives u + y^2, confirms that the order is
unchanged. What's going on? What am I doing wrong? What don't I
understand?
TIA,
Fred Klingener
Mathematica 5.0.0, Examples from Help Browser
you do nothing wrong, but Mathematica orders the patterns by itself
to ensure that more special patterns are applied first and this
happens in your case.
Regards
Jens
I feared that this was the case - book section 2.5.7 is pretty clear on it,
but 2.5.13 gives explicit instructions on how to change the order. It isn't
helpful that the order doesn't matter in the example they use.
Not being able to change the *Value order doesn't bother me as much as the
cheerful way that Mathematica takes the instruction then doesn't do
anything. Some kind of message would have been nice.
Thanks,
Fred Klingener