Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1999
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1999

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

Search the Archive

Re: Standard Evaluation with UpValues

  • To: mathgroup at smc.vnet.net
  • Subject: [mg19384] Re: Standard Evaluation with UpValues
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Mon, 23 Aug 1999 13:57:05 -0400
  • References: <7pl6bi$cdi@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Ersek, Ted R <ErsekTR at navair.navy.mil> wrote in message
news:7pl6bi$cdi at smc.vnet.net...
> Section 7.1.3 of the excellent book "Power Programming With Mathematica
The
> Kernel" by David B. Wagner explains the main evaluation loop.  In that
> Section it indicates UpValues for a symbol are applied before DownValues
for
> the symbol are applied.
>
> OK then consider the case below where (f) has both an UpValue and a
> DownValue defined by the user.
>
> At Out[4] the UpValue for (f) was used.  In that case the DownValue
couldn't
> be used to evaluate f[t].
> Then at Out[5] the DownValue for (f) is used to evaluate  f[E] --> 1+E
> before the kernel checked to see if the UpValue would apply (and it would
> have).  I get the same result using Versions 3 and 4.
>
> When the kernel evaluates g[f[E]] it must figure out that the Head is (g)
> before it evaluates f[E].  After determining that the Head is (g) it
checks
> to see if (g) has any Hold* attributes, and continues with evaluation as
> required.  So by the time the kernel evaluates f[E] it has all the
> information it needs to know that the UpValue for (f) can be used.
However,
> the DownValue is used instead.  Wait a minute, aren't UpValues applied
> before DownValues are applied?
>
> Can someone convince me that the main evaluation loop performs as
explained
> by David Wagner when evaluating In[5] below?  Also can someone give a
> different example that more clearly shows that UpValues are used first?
> ------------------------------
>
> In[1]:=
> ClearAll[f,g,t];
> f[x_?NumericQ]:=1+x;
> f/:g[f[x_]]:=x+4;
>
>
> In[4]:=
> g[f[t]]
>
> Ou[4]=
> 4+t
>
>
> In[5]:=
> g[f[E]]
>
> Out[5]=
> g[1+E]
>
> --------------------
> Regards,
> Ted Ersek
>
> For Mathematica tips, tricks see
> http://www.dot.net.au/~elisha/ersek/Tricks.html
>
>

Ted:

ClearAll[f, g, t];

f[x_?NumericQ] := 1 + x;  (*R1*)
f /: g[f[x_]] := x + 4;         (*R2*)

g[f[t]]
    4 + t

g[f[E]]
    g[1 + E]

The evaluation steps are

g[f[t]]
- evaluate f[t];
- look for applicable UpValues of t (ie the left side of which matche f[t]);
there are none;
- look for applicable DownValues of f; there are none, since t is not
numeric;
- get g[f[t]], unchanged;
- look for applicable UpValues of f; find R2;
-get 4+t

g[f[E]]
- evaluate f[E];
- look for applicable UpValues of t (ie the left side of which matches
f[E]); there are none;
- look for applicable DownValues of f; find R2, since E is numeric;
- get g[1+E];
- look for applicable UpValues of Plus; there are none;
- look for applicable DownValues of g; there are none;
- get g[1+E]


Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565




  • Prev by Date: Re: strange problem with tickmarks
  • Next by Date: Re: ContourShading with ListContourPlot
  • Previous by thread: Re: Standard Evaluation with UpValues
  • Next by thread: RE: Standard Evaluation with UpValues