MathGroup Archive 2001

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

Search the Archive

RE: Re: pattern matching quirks

  • To: mathgroup at smc.vnet.net
  • Subject: [mg29382] RE: [mg29367] Re: [mg29351] pattern matching quirks
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.de>
  • Date: Sat, 16 Jun 2001 02:47:53 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Dear Andrzej,

the problem -- or quirk -- seems to have it's origin at the naming of
patterns, (and possibly it's nothing more than a missing error message).

To repeat your observation in a different way:
 
In[2]:= g[x] /. g[a : x | y ...] :> its[a]
Out[2]= g[x]
In[3]:= g[y] /. g[a : x | y ...] :> its[a]
Out[3]= its[y]
In[4]:= g[y, y] /. g[a : x | y ...] :> its[a]
Out[4]= its[y, y]
In[5]:= g[] /. g[a : x | y ...] :> its[a]
Out[5]= g[]
 
We obviously have two missing matches.


Using different names for "incompatible" patterns we get:

In[6]:= g[x] /. g[(a : x) | (b : y ...)] :> its[a, b]
Out[6]= its[x]
In[7]:= g[y] /. g[(a : x) | (b : y ...)] :> its[a, b]
Out[7]= its[y]
In[8]:= g[y, y] /. g[(a : x) | (b : y ...)] :> its[a, b]
Out[8]= its[y, y]
In[9]:= g[] /. g[(a : x) | (b : y ...)] :> its[a, b]
Out[9]= its[]


So far everything fine. But when trying to use the same name:
 
In[10]:= g[x] /. g[(a : x) | (a : y ...)] :> its[a]
>From In[10]:=
Pattern::"patv": "Name \!\(a\) used for both fixed and variable length \
patterns."
>From In[10]:=
Pattern::"patv": "Name \!\(a\) used for both fixed and variable length \
patterns."
Out[10]= its[x]

an error message ... but we get the result!


So just switch it off !

In[11]:= Off[Pattern::"patv"]

In[12]:= g[x] /. g[(a : x) | (a : y ...)] :> its[a]
Out[12]= its[x]
In[13]:= g[y] /. g[(a : x) | (a : y ...)] :> its[a]
Out[13]= its[y]
In[14]:= g[y, y] /. g[(a : x) | (a : y ...)] :> its[a]
Out[14]= its[y, y]
In[15]:= g[] /. g[(a : x) | (a : y ...)] :> its[a]
Out[15]= its[]

MatchQ just seems to ignore pattern names (since it does no transformation
on it). So we may believe in just a single way matching is done in
Mathematica.


Yours, Hartmut Wolf



> -----Original Message-----
> From: Andrzej Kozlowski [mailto:andrzej at tuins.ac.jp]
To: mathgroup at smc.vnet.net
> Sent: Friday, June 15, 2001 8:24 AM
> To: mathgroup at smc.vnet.net
> Subject: [mg29382] [mg29367] Re: [mg29351] pattern matching quirks
> 
> 
> That the default value for a pattern in a function definition 
> must much a
> pattern seems to me obvious. After all, it is by matching the 
> default value
> to the pattern that Mathematica finds out what it should 
> return. How else
> could this be accomplished?
> 
> As for your problem, it seems to be due to a your use of 
> RepeatedNull. It
> can be demonstrated on a simpler example:
> 
> In[1]:=
> f[a:Alternatives[x,y...]]:=1
> In[2]:=
> f[x]
> Out[2]=
> f[x]
> In[3]:=
> f[y]
> Out[3]=
> 1
> 
> 
> But, 
> 
> In[4]:=
> MatchQ[x,Alternatives[x,y...]]
> Out[4]=
> True
> In[5]:=
> MatchQ[y,Alternatives[x,y...]]
> Out[5]=
> True
> 
> The same thing without RepeatedNull (...) works as expected.
> 
> On the other hand, 
> 
> In[6]:=
> Clear[f]
> In[7]:=
> f[x|y...]:=1
> In[8]:=
> f/@{x,y}
> Out[8]=
> {1,1}
> 
> so it looks like the problem is with the combination of 
> RepeatedNull and
> Pattern. In fact it seems it can be further  reduced to the following
> behaviour: 
> 
> In[9]:=
> MatchQ[x,a:(x|y...)]
> Out[9]=
> True
> In[10]:=
> MatchQ[g[x],g[a:(x|y...)]]
> Out[10]=
> False
> 
> but 
> 
> In[11]:=
> MatchQ[g[y],g[a:(x|y...)]]
> Out[11]=
> True
> 
> and 
> 
> In[12]:=
> MatchQ[g[x],g[(x|y...)]]
> Out[12]=
> True
> 
> I admit that this does seem like a "quirk". On the other hand 
> I can't see
> why you should ever need to use a pattern like
> Alternatives[x,RepeatedNull[y]] which ought to be equivalent to simple
> Alternatives[x,y]. 
> 
> 
> -- 
> Andrzej Kozlowski
> Toyama International University
> JAPAN
> 
> http://platon.c.u-tokyo.ac.jp/andrzej/
> http://sigma.tuins.ac.jp/~andrzej/
> 
> 
> 
> on 01.6.14 3:27 PM, Michael at michael at science.edu wrote:
> 
> > I am trying to write a function which matches only on a 
> specific pattern of
> > arguments, some of which can be optional.  The problem I am 
> having is that
> > certain complex patterns aren't being matched under 
> conditions which should
> > match.
> > 
> > For example:
> > 
> > In[1] := test[1, inputs : (X["4"] | X[X[_Integer]] ...) : 
> X["4"]] := inputs
> > 
> > In[2] := test[1]
> > Out[2] := X[4]
> > 
> > This is the expected behavior.  However if I redefine the 
> function so that
> > the alternatives are collected within a single head, it 
> doesn't work:
> > 
> > In[3] := Clear[test];
> > test[1, inputs : (X["4" | X[_Integer] ...]) : X["4"]] := inputs
> > 
> > In[4] := test[1]
> > Out[4] := test[1]
> > 
> > In[5] := MatchQ[X["4"], (X["4" | X[_Integer] ...])]
> > Out[5] := True
> > 
> > MatchQ says that my default values matches the given 
> pattern.  Through some
> > experimentation I had discovered that the function will NOT 
> be called if the
> > default value does not match the pattern for the optional 
> argument.  This is
> > annoying (and undocumented I think) because I would like to insert a
> > function call to generate the default value.  This does not 
> work right
> > either because the default value appears to be evaluated 
> when the function
> > is defined, and wrapping it with Hold causes it to fail the 
> above pattern
> > match.  So instead I have to assign it to some inane value 
> (hence the
> > alternatives) and then do a rule transformation in the 
> function itself (not
> > shown above).
> > 
> > So here are my questions:
> > 1. Why does the default value for a pattern have to match 
> that pattern?
> > 2. Why is the default value seeming to be evaluated when 
> the function is
> > defined?
> > 3. Why do some patterns pass MatchQ but not result in a 
> function call?
> > Notably, X[a|b] versus X[a]|X[b]?
> > 4. What other quirks should I be aware of that I haven't 
> run into yet? :-)
> > 
> > Regards,
> > 
> > Michael Schmidt
> > michael at science.edu
> > 
> > 
> > 
> > 
> > 
> 
> 



  • Prev by Date: Re: Re: pattern matching quirks
  • Next by Date: How does one write a rule that does integration by parts...in steps ?
  • Previous by thread: Re: Re: pattern matching quirks
  • Next by thread: Re: pattern matching quirks