Re: New version, new bugs
- To: mathgroup at smc.vnet.net
- Subject: [mg42876] Re: New version, new bugs
- From: Maxim <.!dontsendhere at uunet.uu.net>
- Date: Fri, 1 Aug 2003 01:25:48 -0400 (EDT)
- References: <bfdqd8$sfi$1@smc.vnet.net> <bg7vmu$hii$1@smc.vnet.net> <bg9mog$phm$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Jens-Peer Kuska wrote: > Hi, > > > Consider four > > nearly identical expressions: > > > > In[1]:= > > f[a] + f[b] + 1 /. HoldPattern[Plus][f[_] ..] /; True :> 0 > > f[a] + f[b] + 1 /. HoldPattern[Plus[f[_] ..]] /; True :> 0 > > f[a] + f[b] + 1 /. HoldPattern[Plus][f[_] ..] :> 0 /; True > > f[a] + f[b] + 1 /. HoldPattern[Plus[f[_] ..]] :> 0 /; True > > > > Exactly one of them evaluates to 1. This is something from Who Wants to Be a > > Millionaire, not a programming language. > > If you don't know what you are doing you should not do it. > > You first expression f[a]+f[b]+1 is ordered as > > 1+f[a]+f[b] > > with out the Flat attribute of Plus[] your patterns above match only for > the > case f[a]+f[b]+f[c]+f[d]+... and *not* for 1+f[a]+f[b]. > > In the first two cases, the Condition[Holdpattern[_],_] is not evaluated > and the > Flat attribute is not applied, in the third case the > HoldPattern[Plus][_] prevent the application of the Flat > attribute ... > > If something is "nearly identical" it does not mean that it > *is* identical. > > If you avoid the useless HoldPattern[] nonsense and write > > f[a] + f[b] + 1 /. Plus[f[_] ..] /; True :> 0 > f[a] + f[b] + 1 /. Plus[f[_] ..] :> 0 /; True > > all works as expected > > > > > Another situation where the result is unpredictable is passing patterns to > > functions: can you guess what the result of Integrate[x_,x_] will be? And of > > Integrate[_,_]? > > It is semantic nonsens to pass a pattern to a function that expect > a realisation of a pattern. If you have a function DriveA[car_] > a call of this function will drive a certain car like > DriveA[Mercedesbenz] but you can't expect that the function > can drive all possible cars at the same time when you say > DriveA[anyCar_] > > > > > Then, of course, there is Mathematica's scoping: > > > > In[1]:= > > Module[ > > {sub,f}, > > sub[a_,b_]:=Module[ > > {aa=a,x}, > > aa-b > > ]; > > f[x_]=sub[x,x] > > ] > > > > Out[1]= > > x$-x$38 > > > > The problem is that the x in aa and the x in b get renamed differently; I am > > not even trying to figure out if it is a bug, I'm just pointing out how > > confusing it is (probably it has to do with the fact that assignment in > > Module initialization doesn't use Set). > > It has to do with the fact that you are using lexical scoping and you > mean > dynamic scoping > > Block[{sub, f}, sub[a_, b_] := Block[{aa = a, x}, aa - b]; > f[x_] = sub[x, x]] > > Mathematica does what you say and not that what you mean ... > > > > > Another example: > > > > In[1]:= > > <<discretemath` > > UndirectedQ[FromAdjacencyMatrix[{{0,1},{0,0}},Type->Directed]] > > <<numbertheory` > > UndirectedQ[FromAdjacencyMatrix[{{0,1},{0,0}},Type->Directed]] > > > > Out[2]= > > False > > > > Out[4]= > > True > > > > Here the situation is clear: the reason for the two different answers is a > > name conflict. But it would be nice to at least see a warning message. > > If you don't get a error message about symbol shadowing you have > switched it > of, but it is still you problem to *say* Mathematica what you mean and > to > write > > <<discretemath` > UndirectedQ[FromAdjacencyMatrix[{{0,1},{0,0}},Type->Directed]] > <<numbertheory` > discretemat`UndirectedQ[discretemath`FromAdjacencyMatrix[{{0,1},{0,0}}, > discretemath`Type->discretemath`Directed]] > > And, yes it is a bug that Mathematica can't read your mind. But AFAIK > WRI is working on this feature ... > > > > > Finally, whose brilliant idea was it to give the output for ??In and ??Out > > unsorted? > > We must have different Mathematica's because my > > DownValues[In] > > are sorted. Or did you mean that In/Out should follow the > Input/Output cell order of the notebook and not the In/Out > commands send to the Kernel ? > > Regards > Jens 1) Firstly, of course you're wrong if you think that we can just "avoid the useless HoldPattern[] nonsense and write" f[a] + f[b] + 1 /. Plus[f[_] ..] /; True :> 0 The Plus in the pattern evaluates, so your rule will also match f[x], not just sums: In[1]:= f[x]/.Plus[f[_]..]/;True:>0 Out[1]= 0 Then you're wrong when you claim that "HoldPattern[Plus][_] prevent the application of the Flat attribute": In[1]:= MatchQ[f[a]+f[b]+1,HoldPattern[Plus][x_,y_]] MatchQ[f[a]+f[b]+1,HoldPattern[Plus[x_,y_]]] Out[1]= True Out[2]= True After all, if it had worked as you described, the pattern in the fourth example (which does evaluate to 1) wouldn't have matched too. 2) I don't know what you mean by "semantic nonsens", but in any case, I'm not discussing what the 'correct' answer to Integrate[_,_] or suchlike should be; I simply claim that if Integrate[_,_] evaluates to _^2/2, then Integrate[x_,x_] should be evaluated too. 3) Basically you're telling me that Block works so-and-so; yes, thanks for the information, but what about my example? Consider that Module[ {sub}, sub[a_,b_]:=Module[ {aa=a,x}, aa-b ]; sub[x,x] ] gives 0, not x$-x$38. If you're going to argue that both answers are as expected, I think you're fighting a lost cause. 4) General::shdw is a warning message actually, not an "error message", and I haven't "switched it of" -- I'm showing the exact output from Mathematica session. 5) When I say ??In, I mean ??In. Mathematica re-sorts some internal list after you have approximately 20 input lines, and the output looks like this (I'm showing the first and the last lines): In[18]:= ??In In[n] is a global object that is assigned to have a delayed value of the nth input line. Attributes[In] = {Listable, Protected} In[8] = Information["In", LongForm -> True] In[10] = Information["In", LongForm -> True] In[12] = Information["In", LongForm -> True] ............................................. In[2] = Information["In", LongForm -> True] In[4] = Information["In", LongForm -> True] In[6] = Information["In", LongForm -> True] It seems to me that all your statements that aren't insults are simply incorrect. Maxim Rytin m.r at prontomail.com