Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

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

Search the Archive

Re: Foo /: Plus[b_Foo] := b

  • To: mathgroup at smc.vnet.net
  • Subject: [mg114564] Re: Foo /: Plus[b_Foo] := b
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Wed, 8 Dec 2010 06:39:32 -0500 (EST)

Hi kj,

Now you have entered the forbidden territory :)

Here is a very crude way to fix your example:

ClearAttributes[Plus, OneIdentity]

In[5]:= 1 + Foo[1]

Out[5]= 1 + Foo[1]

SetAttributes[Plus, OneIdentity]

Have a look at attributes Flat and OneIdentity (they tend to be considered
together, and
for a good reason), both in the Help, and, even better, in some past
Mathgroup discussions,
for the explanation. But this is not for the faint-hearted :).

The bottom line: your UpValue is not at all innocent, given the set of
Arttributes of Plus.

Hope this helps.

Regards,
Leonid


On Tue, Dec 7, 2010 at 2:46 PM, kj <no.email at please.post> wrote:

> 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: FileName Widget
  • Next by Date: Re: Foo /: Plus[b_Foo] := b
  • Previous by thread: Foo /: Plus[b_Foo] := b
  • Next by thread: Re: Foo /: Plus[b_Foo] := b