MathGroup Archive 2006

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

Search the Archive

Re: Matching with a "Flat" function

  • To: mathgroup at
  • Subject: [mg70922] Re: [mg70857] Matching with a "Flat" function
  • From: Andrzej Kozlowski <akoz at>
  • Date: Wed, 1 Nov 2006 03:56:22 -0500 (EST)
  • References: <>

On 30 Oct 2006, at 19:32, Szabolcs wrote:

> Could someone explain this behaviour to me? I'd expect f[_Integer] to
> match f[2] even after I set attribute Flat on f.
> Szabolcs
> In[1]:= MatchQ[f[2],f[_]]
> Out[1]= True
> In[2]:= MatchQ[f[2],f[_Integer]]
> Out[2]= True
> In[3]:= SetAttributes[f,Flat]
> In[4]:= MatchQ[f[2],f[_]]
> Out[4]= True
> In[5]:= MatchQ[f[2],f[_Integer]]
> Out[5]= False

This is because of something many years ago Allan Hayes called f- 
grouping:if f has the Flat attribute, f[2] is converted to f[f[2]]  
before the match is attempted, and then, of course, it does not match  
f[_Integer]. In order for this not to happen you need to give f also  
the attribute OneIdentity.

SetAttributes[f, {Flat, OneIdentity}]

MatchQ[f[2], f[_Integer]]


Note also that even without OneIdentity attribute, the following will  


MatchQ[f[g[2]], f[g[_Integer]]]


The presence of g prevents f-grouping just as the attribute  
OneIdentity does. A full explanation of this behaviour is complicated  
and has already been done on this about 6 years ago. If you are  
interested search for posts by Allan Hayes and Hartmut Wolf.

Andrzej Kozlowski

  • Prev by Date: Context headache
  • Next by Date: Re: Curiosity concerning transformation rules for List
  • Previous by thread: Re: Context headache
  • Next by thread: Re: Matching with a "Flat" function