Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.



Re: Foo /: Plus[b_Foo] := b
Posted:
Dec 14, 2010 6:55 AM


I'm confused. I'm not sure what this example is supposed to show.
The pattern Plus[b_Foo] evaluates to the pattern b_Foo. So the rule is b_Foo := b. Which infinite loops, obviously.
So I think there are two misunderstandings here: 1) Mathematica doesn't stop evaluating at a FixedPoint. It keeps evaluating until no rules match. So kj is mistaken in expecting it to stop. 2) If you don't want the pattern to evaluate, you need to use HoldPattern. E.g., Foo /: HoldPattern[Plus][b_Foo] := b This doesn't infinite loop, of course, but then again, nothing will match it, because Plus[Foo[x]] will be evaluated away before the rule is ever applied.
So, again, I'm not sure what this example is supposed to show. What exactly is the problem here?
Begin forwarded message:
> From: kj <no.email@please.post> > Date: December 7, 2010 3:46:27 AM PST > To: mathgroup@smc.vnet.net > Subject: Foo /: Plus[b_Foo] := b > > An innocentlooking upvalue for a brand new symbol Foo: > > In[1]:= Foo /: Plus[b_Foo] := b; > > > Nothing shocking about this rule: it just recapitulates the default > behavior of Plus when given a single argument. And it seems to > work: > > In[2]:= Plus[Foo[1]] > Out[2]= Foo[1] > > > ...but disaster lurks just around the corner: > > In[3]:= 1 + Foo[1] > During evaluation of In[3]:= $IterationLimit::itlim: Iteration limit > of 4096 exceeded. >> > Out[3]= Hold[1 + Foo[1]] > > > I was expecting that 1 + Foo[1] would evaluate to itself, not an > infinite loop.... So I examined a trace: > > In[4]:= ToString[ > StringForm["``", > FullForm /@ Take[Quiet[Trace[1 + Foo[1]]], 10] // ColumnForm]] > > Out[4]= "HoldForm[Plus[1, Foo[1]]] > HoldForm[Plus[1, Foo[1]]] > HoldForm[Plus[1, Foo[1]]] > HoldForm[Plus[1, Foo[1]]] > HoldForm[Plus[1, Foo[1]]] > HoldForm[Plus[1, Foo[1]]] > HoldForm[Plus[1, Foo[1]]] > HoldForm[Plus[1, Foo[1]]] > HoldForm[Plus[1, Foo[1]]] > HoldForm[Plus[1, Foo[1]]]" > > > > > Judging from the trace, the evaluator is attempting to evaluate > the same expression over and over. The evaluator is supposed to > stop when it reaches a fixed point, and that looks like a fixed > point if I ever saw one. (In fact, in this case, we started at > the fixed point to begin with!) So either there's a very serious > bug in the evaluator, or the documentation I read on the subject > was wrong about how it's supposed to work. (FWIW, this is Mathematica > 7.) > > > Whatever it is that I'm doing wrong, it seems to me *very > fundamental*. > IOW, this does not look to me like some obscure corner case. If > so, I can't understand how I could have missed it in all the reading > I've done about Mathematica over the years. It's occurrences like > this that fuel my *paranoia* about the Mathematica documentation. > > If someone can tell me 1) what I'm doing wrong above, and hopefully > 2) how to fix it, I'd much appreciate it. Also, if you could point > me to where in the documentation I would have learned the answers > to these questions I'd be grateful. > > TIA! > > ~kj > > >



