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: [mg114722] Re: Foo /: Plus[b_Foo] := b
  • From: Charles Gillingham <cgillingham1 at me.com>
  • Date: Tue, 14 Dec 2010 06:55:20 -0500 (EST)

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 at please.post>
> Date: December 7, 2010 3:46:27 AM PST
> To: mathgroup at smc.vnet.net
> Subject: [mg114542] Foo /: Plus[b_Foo] := b
>
> 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: Calculate a numerical integral with enough precision
  • Next by Date: Re: Question: Compile in Mathematica 8.0
  • Previous by thread: Re: Foo /: Plus[b_Foo] := b
  • Next by thread: Are these bugs fixed in Mathematica 8 ?