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[1] := SetAttributes[p, {OneIdentity, Flat, Orderless}]
I also want it to reduce expressions with only one argument to just the
argument:
In[2] := p[a_]=a;
However, if I now enter an expression like p[x,y], this happens:
In[3] := p[x, y]
$RecursionLimit::reclim: Recursion depth of 256 exceeded.
Out[3]= 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[1]:= p[a_]=a;
In[2]:= SetAttributes[p, {OneIdentity, Flat, Orderless}]
In[3]:= p[x, y]
Out[3]= 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[4]:= p[a_]=a;
In[5]:= p[x, y]
$RecursionLimit::reclim: Recursion depth of 256 exceeded.
Out[5]= Hold[p[x, y]]
In[6]:= ?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[6]:= Attributes[p]={};
In[7]:= p[x,y]
$RecursionLimit::reclim: Recursion depth of 256 exceeded.
Out[7]= 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?
Where can I find more information on this subject?
Thank you for your help.
--
Best regards,
Torben Winther Graversen
http://www.student.dtu.dk/~immtwg