Can anyone explain to me this behaviour? I've been having more than a couple of similar doubts these last weeks.
For example
f[_?NumericQ] := 8;
Now, if I do
With[{a = f[a]}, HoldForm@Block[{NumericQ = True &}, a]]
I get
Block[{NumericQ = True &}, f[a]]
And if I do
Block[{NumericQ = True &}, f[a]]
I get
8
So far so good... Another so-far-so-goodie is (notice the :=)
With[{a := f[a]}, Block[{NumericQ = True &}, a]]
8
Question: Can anyone help me understand why this output?
With[{a = f[a]}, Block[{NumericQ = True &}, a]]
f[a]
Could it be that With (= version) not only evaluates and replaces, but also guarantees that the replaced expression won't be reevaluated no matter what until the With is exited? If that's the case I wasn't expecting that. What's happening here?
EDIT
Question also applies to Function
Block[{NumericQ = True &}, #] &[f[a]]
f[a]
And not just those too. Everything I try behaves the same way... A couple of other examples
Block @@ (Hold[{NumericQ = True &}, exp] /. exp -> f[a])
With[{a := Evaluate@f[a]}, Block[{NumericQ = True &}, a]]
both give f[a]
EDIT
With[{g = h}, h = 8; Print[g] ]
prints 8, not h, so clearly h is reevaluated inside the With in this case, so g is not so constant.
EDIT
Ok, another couple of examples
In[10]:= ClearAll[h, f];
h := 8 /; NumericQ["a"];
f[_?NumericQ] := 8;
Now, both h and f[a] remain unevaluated
In[21]:= {h, f[a]}
Out[21]= {h, f[a]}
Now, with the OwnValues everything works as expected
In[17]:= With[{g = h},
Block[{NumericQ = True &}, g]]
Out[17]= 8
But with the DownValues, it doesn't
In[19]:= With[{g = f[a]},
Block[{NumericQ = True &}, g]]
Out[19]= f[a]
Similarly
f[a_?NumericQ] := 8;
g[e_] := Block[{NumericQ = True &}, e]
f[a] evaluates to f[a], but weirdly
In[17]:= g[Unevaluated@f[a]]
Out[17]= 8
In[18]:= g[f[a]]
Out[18]= f[a]
TracePrintdidn't showNumericQbeing evaluated for the second time. – Szabolcs Feb 15 '12 at 13:15Traceand saw that there was no second evaluation. I just read your answer first, and realized that withoutTrace, this wouldn't be something I'd guess right away. – Leonid Shifrin Feb 15 '12 at 13:17