       Problems with Flat (Again)

• To: mathgroup at smc.vnet.net
• Subject: [mg15524] Problems with Flat (Again)
• From: immtwg at brahe.gbar.dtu.dk (Torben Winther Graversen)
• Date: Mon, 18 Jan 1999 23:47:24 -0500
• Organization: UNI-C
• Sender: owner-wri-mathgroup at wolfram.com

```I apologize for asking about this twice, but the newsserver here didn't
quite behave the first time I posted during fall '98. Anyway, here's my
question:

I want to have an operator, say p[], that has attribute Flat,
OneIdentity, and Orderless:

In := SetAttributes[p, {OneIdentity, Flat, Orderless}]

I also want it to reduce expressions with only one argument to just the
argument:

In := p[a_]=a;

However, if I now enter an expression like p[x,y], this happens:

In := p[x, y]

\$RecursionLimit::reclim: Recursion depth of 256 exceeded.

Out= Hold[p[x, y]]

Looking at the Trace, it seems as though the pattern-matcher matches
p[a_] with p[p[x,y]] infinitely many times, since p is Flat.

The only way I've found around this problem is to set things up in the
opposite order:

In:= p[a_]=a;

In:= SetAttributes[p, {OneIdentity, Flat, Orderless}]

In:= p[x, y]

Out= p[x, y]

*** Question 1:
So my definition will only give the desired result if I enter the rule
before turning on the attribute?

I've noticed that Mathematica will let me have the same rule twice, with
different attributes:

In:= p[a_]=a;

In:= p[x, y]

\$RecursionLimit::reclim: Recursion depth of 256 exceeded.

Out= Hold[p[x, y]]

In:= ?p
Global`p

Attributes[p] = {Flat, OneIdentity, Orderless}

p[a_] = a

p[a_] = a

If I now remove the attributes, the latest defined rule still applies:

In:= Attributes[p]={};

In:= p[x,y]

\$RecursionLimit::reclim: Recursion depth of 256 exceeded.

Out= Hold[p[x, y]]

*** Question 2:
So Mathematica doesn't select rules based on which attributes are
present, but only based on which attributes where present when the rule
was defined?