Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Foo /: Plus[b_Foo] := b

  • To: mathgroup at smc.vnet.net
  • Subject: [mg114542] Foo /: Plus[b_Foo] := b
  • From: kj <no.email at please.post>
  • Date: Tue, 7 Dec 2010 06:46:27 -0500 (EST)

An innocent-looking 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




  • Prev by Date: Re: WolframAlpha[] give results but not parse!
  • Next by Date: Re: How to use "Apply" to do differentiation ?
  • Previous by thread: Re: CMake module for Mathematica
  • Next by thread: Re: Foo /: Plus[b_Foo] := b