3

I am trying to define some rules for CenterDot. One thing I don't understand is that if I define

SetAttributes[CenterDot, Flat]
CenterDot[var_] := var

CenterDot[x] will induce infinite recursion. I have tried to use Trace, but could not find why there was a recursion. I am expecting CenterDot[x] being evaluated to x.

atbug
  • 685
  • 4
  • 10
  • 1
    It seems to depend on the order of definitions, compare these two: f1[x_] := x; SetAttributes[f1, Flat] and SetAttributes[f2, Flat]; f2[x_] := x. Only f2 is affected. – Szabolcs Oct 27 '20 at 21:43

1 Answers1

7

This is age-old behavior (likely back to ~Mathematica 1.0). Perhaps at that time in the 1990s it would be solidly considered a bug, but by 2020 it's been the behavior for so long that I think it's unlikely to be changed.

I have two suggestions for working around the behavior:

In[1]:= ClearAll[f];
SetAttributes[f, Flat];
Verbatim[f][x_] := x

In[4]:= f[1]

Out[4]= 1

In[5]:= ClearAll[f2] SetAttributes[f2, {OneIdentity, Flat}]; f2[x_] := x

In[8]:= f2[1]

Out[8]= 1

ktm
  • 4,242
  • 20
  • 28
  • I wonder why it never got fixed. Anybody relying on this behavior? – atbug Oct 27 '20 at 22:33
  • I think in 2020 that’s a factor. Maybe not explicitly relying on this behavior, but worrying about any unintended downstream effects from the change. – ktm Oct 27 '20 at 23:37