Re: Pattern evaluation depending on order of definitions

*To*: mathgroup at smc.vnet.net*Subject*: [mg74626] Re: Pattern evaluation depending on order of definitions*From*: dh <dh at metrohm.ch>*Date*: Thu, 29 Mar 2007 02:26:28 -0500 (EST)*References*: <eud36i$kp4$1@smc.vnet.net>

Hi Hannes, Mathematica stores UpValues according to the creterion of increasing generality. More specific rules are stored first. Convince yourself by: ??hh and ??gg. During evaluation, Mathematica uses the first matching rule it finds. Now Mathematica has obvioulsy difficulties to determine that _h[1] is more specific than _h[n_Integer] as you can see by ??h. I consider this a bug and Wolfram should take not. What you can do is to make your definition in the correct order. Daniel Hannes Kessler wrote: > Hello Mathematica experts, > > please consider the following 2 examples: > > _g[1] := -1; > _g[n_Integer] := 1; > g["something"][1] > --> -1 > > _h[n_Integer] := 1; > _h[1] := -1; > h["something"][1] > --> 1 > > The first example is what I want: Objects with head g applied to 1 > should return -1 and applied to other integers should return +1. The > only difference in the second example is the order of the definitions. > It appears that Mathematica does not check for further definitions > matching h["something"][1] more accurate. > > This is different in the following two examples: > > gg[1] := -1; > gg[n_Integer] := 1; > gg[1] > --> -1 > > hh[n_Integer] := 1; > hh[1] := -1; > hh[1] > --> -1 > > Here, the order of the definitions has no influence. Mathematica > checks all definitions and chooses the best matching one. What is the > reason for this different behaviour? > > Thanks in advance, > Hannes Kessler > >