Re: delayed rule evaluation order
- To: mathgroup at smc.vnet.net
- Subject: [mg68017] Re: [mg67967] delayed rule evaluation order
- From: "Carl K. Woll" <carlw at wolfram.com>
- Date: Thu, 20 Jul 2006 06:04:57 -0400 (EDT)
- References: <200607190921.FAA21363@smc.vnet.net> <44BE2B26.70206@wolfram.com> <acbec1a40607190940k511a06b0ue9e47405a39fe651@mail.gmail.com>
- Sender: owner-wri-mathgroup at wolfram.com
Chris Chiasson wrote: > This is a fairly interesting replacement technique; it reminds me of > the one David Park used for capturing side-effects of Condition > processing. > > http://forums.wolfram.com/mathgroup/archive/2006/May/msg00621.html > > Unfortunately, it is causing unwanted evaluations. Why does it do this? > > In[1]:= > f[1]=1; > f[n_Integer]=n f[n-1]; > f[2`3] > z=Trace[f[2]] > z/.{blah_?ExactNumberQ\[RuleDelayed]With[{p=SetPrecision[blah,3]},p/;True]} > > Out[3]= > f[2.00] > > Out[4]= > {f[2],2 f[-1+2],{{-1+2,1},f[1],1},2 1,2} > > Out[5]= > {2.00,2.00,{{1.00,1.00},1.00,1.00},2.00,2.00} > Chris, First let's look at the input form of z: In[7]:= z // InputForm Out[7]//InputForm= {HoldForm[f[2]], HoldForm[2*f[-1 + 2]], {{HoldForm[-1 + 2], HoldForm[1]}, HoldForm[f[1]], HoldForm[1]}, HoldForm[2*1], HoldForm[2]} So, your question is why HoldForm[f[2]] is being replaced with 2.00. In[7]:= HoldForm[f[2]] /. blah_?ExactNumberQ :> With[{p = SetPrecision[blah, 3]}, p /; True] Out[7]= 2.00 Let's use Reap/Sow to find out what blah is: In[8]:= HoldForm[f[2]] /. blah_?ExactNumberQ :> With[{p = SetPrecision[blah, 3]}, Sow[Hold[blah]]; p /; True] // Reap Out[8]= {2.00, {{Hold[f[2]]}}} So, M-- looks at f[2], which when evaluated is 2, an exact number: In[9]:= ExactNumberQ[f[2]] Out[9]= True Hence, f[2] is replaced with 2.00. I don't know what you are trying to accomplish here, but two possibilities are: In[10]:= Block[{f}, z /. blah_?ExactNumberQ :> With[{p = SetPrecision[blah, 3]}, p /; True]] Out[10]= {f[2.00], 2.00 f[1.00], {{1.00, 1.00}, f[1.00], 1.00}, 2.00, 2.00} and In[11]:= z /. blah_Integer :> With[{p = SetPrecision[blah, 3]}, p /; True] Out[11]= {f[2.00], 2.00 f[-1.00 + 2.00], {{-1.00 + 2.00, 1.00}, f[1.00], 1.00}, 2.00 1.00, 2.00} Carl Woll Wolfram Research > The output is humorous (in a demented way, I guess) if With is > replaced with Module. > > Thanks, > > On 7/19/06, Carl K. Woll <carlw at wolfram.com> wrote: > >> Chris Chiasson wrote: >> > Hold[{2, 3}] /. {{x_, y_} :> x^y} >> > >> > the result is Hold[Power[2,3]] >> > >> > I would like the result to be Hold[8] >> > >> > The original context is post-processing of a large Trace output (via >> > SetPrecision to get rid of digits and improve readability). >> > >> > Any ideas? >> > >> >> This is not an easy one to figure out. One possibility is given in the >> help for ReplaceAll in the last example of the Further Examples section. >> In this example the following construct is used: >> >> In[27]:= Hold[{2, 3}] /. {x_, y_} :> With[{p = x^y}, p /; True] >> >> Out[27]= Hold[8] >> >> Carl Woll >> Wolfram Research >> > >
- References:
- delayed rule evaluation order
- From: "Chris Chiasson" <chris@chiasson.name>
- delayed rule evaluation order