How to Force Evaluation of Derivative in a Pure Function Definition

f1 = #^2 &;

f1' gives a pure function:

df1 = f1'

2 #1 &

df1[2]

4

Alternatively, you can use Derivative:

df1b = Derivative[1] @ f1

2 #1 &

df1b[3]

6

Re "to additionally manipulate the derivative inside the function", you can use Composition to wrap df1 with desired manipulations:

ndf1 = N@*(f1')

N@*(2 #1 &)

ndf1[2]

4.


Let's examine the problem as given. I will consider it as a general code rewriting problem. We have an expression of the form

h[.., f[ Evaluate[g[x]] ],..]     (* or more generally... *)
h[.., f[ Evaluate[g[x]], y],..]  (* etc. *)

where h is a HoldAll function and we'd like to evaluate g[x] but not f[] to get

h[.., f[g0],..]      (* or *)
h[.., f[g0, y],..]

where g0 = g[x]. Evaluate does not work this way, because Evaluate[expr] is evaluated only when it appears as an argument of h; it is not evaluated when it appears in an argument at a lower level.

For Evaluate to do its work, it needs to appear one level higher around f:

h[.., Evaluate[f[g[x],..]],..]

But that yields

h[.., f0,..]

where f0 = f[g[x],..], which is different that what we said we wanted. For instance, in this example, N[] has no effect:

f2 = #^2/2 &;
Function[{x}, Evaluate@N@D[f2[x], x]]
(*  Function[{x}, x]  *)

One way to get the code rewritten is to extend the semantics of Evaluate with a replacement rule:

h[.., f[Evaluate[g[x]],..],..] /.
 e_Evaluate :> RuleCondition[e, True]

The OP's example:

f1 = #^2 &;
Function[{x}, N@Evaluate@D[f1[x], x]] /.
 e_Evaluate :> RuleCondition[e, True]
(*  Function[{x}, N[2 x]]  *)

Note that the rule will result in all expressions Evaluate[expr] to be evaluated, even if an expression appears in HoldComplete or a function that has the attribute HoldAllComplete.


This behavior is due in this specific case to NHoldAll. Under Scope we find both Derivative and Slot, among many others which also have this attribute.