Re: Matching with a "Flat" function

• To: mathgroup at smc.vnet.net
• Subject: [mg70922] Re: [mg70857] Matching with a "Flat" function
• From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
• Date: Wed, 1 Nov 2006 03:56:22 -0500 (EST)
• References: <200610301032.FAA13291@smc.vnet.net>

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

True

Note also that even without OneIdentity attribute, the following will
match:

ClearAttributes[f,OneIdentity]

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

True

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