       Re: Flat, OneIdentity Again

• To: mathgroup at smc.vnet.net
• Subject: [mg21652] Re: [mg21637] Flat, OneIdentity Again
• From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
• Date: Fri, 21 Jan 2000 04:00:01 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```Below are a few somewhat speculative comments on some points you have
raised.

> At http://support.wolfram.com/Kernel/Symbols/System/Flat.html
> it says in so many words ....
> If (f) has the Flat attribute you better not have a definition like
> f[p_]:=p
> because if you do, then an attempt to evaluate f[1,2] will not work and the
> kernel will have to quit when the infinite iteration limit is exceeded.
>
> In addition I found that you can't even evaluate f in the above case, and
> it doesn't help if (f) also has the OneIdentity attribute!

What happens is that you if you evaluate f[1,2] you get into an infinite
loop since Mathematica trying to match the pattenrn re-writes this as
f[f[1,2]] then re-writes the inner f[1,2] as f[f[1,2]] and so on. However,
you can avoid this problem by using the (non-standard) approach of giving f
the Flat attribute only after the definition of f. You get most of the
properties of a flat function without the infinite loop:

In:=
ClearAll[f]
In:=
f[p_] := p
In:=
SetAttributes[f, Flat];
In:=
f[a]
Out=
a
In:=
f[f[a, b], f[c]]
Out=
f[a, b, c]
In:=
f[1, 2]
Out=
f[1, 2]

>
> But if the pattern matcher treats f[1,2] as f[f[1,2]]
> why doesn't MatchQ return True in Out below ?
>
> In:=
> MatchQ[f[1,2],HoldPattern[f[f[_Integer,_Integer]]]]
> Out=
> False

In this case there is no match whether you use HoldPattern or not.
In:=
ClearAll[f]
In:=
SetAttributes[f, Flat];
In:=
MatchQ[f[1, 2], f[f[_Integer, _Integer]]]
Out=
False

I think this is basically the same problem as in your first message on this
topic. Mathemtica converts f[1,2] to f[f[1,2]]. Now it tries to make the
match f[f[1,2]] and HoldPattern[f[f[_Integer,_Integer]]. For the match to
hold however, f[1,2] would have to match f[_Integer,_Integer]. However, if f
has the attribute Flat this is not so:

In:=
MatchQ[f[1, 2], f[_Integer, _Integer]]
Out=
False

>
> Even stranger is the next line where the pattern is much more general!
> Notice that is a triple blank inside (f).
>
> In:=
> MatchQ[f[1,2],HoldPattern[f[f[___]]]]
> Out=
> False

It seems to me that this is a quite differerent problem from the above one.
This time without HoldPattern we do have a match:

In:=
MatchQ[f[1, 2], f[f[___]]]
Out=
True

so this seems to contradtict your theory that holdPattern is being ignored.
It seems to me that the reason can be seen when we compare these two
results:

In:=
MatchQ[f[a], f[f[x_]]]
Out=
True
In:=
MatchQ[f[a], f[x_]]
Out=
True

Both of these work because f[something] is matched with f[f[something]], but
in a different way. In the first case f[f[x_]] is replaced by f[x_] so
inserting HoldPattern prevents matching:

In:=
MatchQ[f[a], HoldPattern[f[f[x_]]]]
Out=
False

In the other case the f[a] on the left is replaced by f[f[a]] so HoldPattern
has no effect:

In:=
MatchQ[f[a], HoldPattern[f[x_]]]
Out=
True

Of course this is esentially speculation because there is no reliable
documentation. While we know that  expressions like x and f[x]  are matched
in the presence of the Flat or  Flat and OneIdentity attributes exactly how
it is done is not really clearly documented. I believe this is due to the
fact that both of these attributes, particulalry OneIdentity are not in
their final version, and this behaviour will probably change in the future.

--
Andrzej Kozlowski
Toyama International University
JAPAN
http://sigma.tuins.ac.jp

```

• Prev by Date: Re: Distance between permutations
• Next by Date: Re: Flat, OneIdentity Again
• Previous by thread: Re: Flat, OneIdentity Again
• Next by thread: Re: Flat, OneIdentity Again