Re: Don't understand behavior of ClearAttributes[Plus,
- To: mathgroup at smc.vnet.net
- Subject: [mg84934] Re: Don't understand behavior of ClearAttributes[Plus,
- From: danl at wolfram.com
- Date: Sun, 20 Jan 2008 03:35:53 -0500 (EST)
- References: <200801160830.DAA10128@smc.vnet.net> <fmmk8b$epl$1@smc.vnet.net>
On Jan 19, 5:10 am, Alain Cochard <al... at geophysik.uni-muenchen.de> wrote: > Thanks much Daniel, for your detailed answer. > [...] > I find it difficult to understand your view. I don't see why > advertising for the ordering rules prevents Mathematica from changing them > (advertising the new ones), leaving to the user the responsibility to > be aware of those new rules, if he cares at all. My understanding is > that one can always determine an order by use of Sort or OrderedQ; but > seems to me it's just extra work than could be avoided. I can give two reasons. (1) It would entail a level of documentation that was difficult to write, prone to error, and applicable to but few users. To me that is a recipe for doing more harm than good. (2) There really are programming semantics that, in my opinion, should be regarded as "details of the implementation". I feel this is one of them. I will grant that you may be able to formulate god reasons for disputing this. But I don't think that will be the case for the situation at hand, where you also are redefining an attribute of a basic arithmetic operation. As a general remark about such things, I believe there are always better ways to work with, say, noncommutative rings/algebras, than to attempt to overload Plus by changing its attributes. > Along the same line, the manual says that > > "Strings are ordered as they would be in a dictionary, with the > upper-case versions of letters coming after lower-case ones." > > It says nothing about non letters, but when I see > > In[58]:= OrderedQ[{">","/"}] > > Out[58]= True > > I conclude that > comes before / in the "Mathematica alphabet of strings". But > then, how should I interpret (and anticipate) the following: > > In[70]:= OrderedQ[{">b","/a"}] > > Out[70]= False Good question. If nobody responds to the group, I'll ask in house. I now agree this inconsistency is a bit bothersome, and, to our eyes, at least, appears to be in violation of documented behavior. > > I will comment on the last example. It appears to be a bug in > > handling of Plus, when set not to be Orderless. This is simply an > > indication that, much as we try, we do not fully support making such > > a change. Please bear in mind that basic arithmetic operations > > (Plus, Times, and Power) are used extensively in Mathematica > > implementation, and specialized handling that places numbers before > > other things in Plus and Times is not particularly easy to undo, > > even when attributes change. Which is one reason I strongly > > recommend not changing attributes of those functions (read: It may > > be a bug, but I've no intention of investigating it, let alone > > attempting a "fix"). > > Here too, I am a bit surprised. I can't help thinking that if Mathematica > supports something, it has to try as best as it can to make it bug > free. I'd be curious to know what other users think about that. I'll leave the philosophical aspects to others, and speak (well, write) as a Mathematica kernel developer. The general gist is Mathematica supports, more or less, clearing the Orderless attribute from many (most?) functions that have it set by default. But there may be glitches insofar as some work at too basic a level. Examples of this caveat are Plus and Times, which are used throughout the implementation of Mathematica, and often are called in ways that bypass the full evaluation semantics. This is in part for speed, and (I think) also in part as a matter of bootstrapping the building of the Mathematica kernel. > On more general grounds, I would like to know if clearing the > Orderless attribute of Plus only for some instructions (for which the > user is convinced that the output is correct), resetting the > attributes just after those instructions, can have unexpected > consequences later on. If the answer is "no", as I expect, then I > don't see what's the big deal of doing it. > > Alain It is not a big deal unless your processing involves, internally, other calls to Plus that do require that it be Orderless for correct handling. So it's a case of caveat emptor, I guess. As for consequences later on, I doubt there would be any other than possible reordering of addends on later reevaluation on expressions. This would be fine, actually (though might catch a user by surprise). A possible bug would be if Mathematica failed to trigger a reevaluation that should have happened due to Plus regaining its Orderless attribute. Daniel Lichtblau Wolfram Research
- References:
- Don't understand behavior of ClearAttributes[Plus,Orderless]
- From: Alain Cochard <alain@geophysik.uni-muenchen.de>
- Don't understand behavior of ClearAttributes[Plus,Orderless]