Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Pattern evaluation depending on order of definitions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg74638] Re: Pattern evaluation depending on order of definitions
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Thu, 29 Mar 2007 02:32:38 -0500 (EST)
  • Organization: The Open University, Milton Keynes, UK
  • References: <eud36i$kp4$1@smc.vnet.net>

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
> 
> 

Stopping the pattern matching search as soon as a match is found is the 
normal behavior of Mathematica.

Mathematica natural mechanism is to sort the definitions from the most 
specific to the most general.

However, the first set of definitions (for g and h) blocks this 
mechanism, and this is now the user's responsibility to give the desired 
or "correct" order.

The second set of definitions (gg and hh), the usual way of defining 
functions indeed, allows Mathematica to sort them as expected.

You can check the order in which the definitions will be checked by 
using the Information command (short cut ?).

_g[1] := -1;
_g[n_Integer] := 1;
g["something"][1]
Information["g", LongForm -> False]

-1
Global`g
_g[1] := -1
_g[n_Integer] := 1

_h[n_Integer] := 1;
_h[1] := -1;
h["something"][1]
Information["h", LongForm -> False]

1
Global`h
_h[n_Integer] := 1
_h[1] := -1

gg[1] := -1;
gg[n_Integer] := 1;
gg[1]
Information["gg", LongForm -> False]

-1
Global`gg
gg[1] := -1
gg[n_Integer] := 1

hh[n_Integer] := 1;
hh[1] := -1;
hh[1]
Information["hh", LongForm -> False]

-1
Global`hh
hh[1] := -1
hh[n_Integer] := 1

Regards,
Jean-Marc


  • Prev by Date: Re: Sequence as a universal UpValue
  • Next by Date: Re: Re: Re: Definite Integration in Mathematica
  • Previous by thread: Re: Pattern evaluation depending on order of definitions
  • Next by thread: Solving a nasty rational differential equation