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[2] 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[1]:= ClearAll[f] In[2]:= f[p_] := p In[3]:= SetAttributes[f, Flat]; In[4]:= f[a] Out[4]= a In[5]:= f[f[a, b], f[c]] Out[5]= f[a, b, c] In[6]:= f[1, 2] Out[6]= 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[4] below ? > > In[5]:= > MatchQ[f[1,2],HoldPattern[f[f[_Integer,_Integer]]]] > Out[5]= > False In this case there is no match whether you use HoldPattern or not. In[7]:= ClearAll[f] In[8]:= SetAttributes[f, Flat]; In[9]:= MatchQ[f[1, 2], f[f[_Integer, _Integer]]] Out[9]= 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[10]:= MatchQ[f[1, 2], f[_Integer, _Integer]] Out[10]= False > > Even stranger is the next line where the pattern is much more general! > Notice that is a triple blank inside (f). > > In[6]:= > MatchQ[f[1,2],HoldPattern[f[f[___]]]] > Out[6]= > 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[11]:= MatchQ[f[1, 2], f[f[___]]] Out[11]= 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[13]:= MatchQ[f[a], f[f[x_]]] Out[13]= True In[14]:= MatchQ[f[a], f[x_]] Out[14]= 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[15]:= MatchQ[f[a], HoldPattern[f[f[x_]]]] Out[15]= False In the other case the f[a] on the left is replaced by f[f[a]] so HoldPattern has no effect: In[16]:= MatchQ[f[a], HoldPattern[f[x_]]] Out[16]= 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