Puzzled regarding avoiding evaluations and using results of computations as functions

You can temporarily unset ev while creating the definition for fun[...] = ...

Block[{ev},
  fun[c_, k_, kF_, LatticeSize_] = generate[];
]

The resulting definition for fun will contain ev now:

fun[c_,k_,kF_,LatticeSize_]=2 ev[c,k,kF,LatticeSize]

Thus you will need to maintain the value of the global variable ev for this to work.

What if we want to inline the value of ev into the definition? We can do this:

Block[{CompiledFunction},
  fun[c_, k_, kF_, LatticeSize_] = generate[];
]

This will prevent the errors from CompiledFuction by disabling it during the definition process. The resulting definition will be

fun[c_,k_,kF_,LatticeSize_]=2 CompiledFunction[...][c,k,kF,LatticeSize]

Note that even if you don't disable it temporarily, the errors can be safely ignored. The definition will still be usable, if ugly.


Unfortunately, all these are really just hacks. They are convenient hacks for the problem at hand, but they do not generalize well.

A more general problem would be inlining ev without knowing its value beforehand. Here I made use of the fact that the value of ev was a CompiledFunction. I knew precisely what to disable using Block to prevent unwanted evaluation.

In order to solve this more general problem, you may need to operate on the DownValues of fun and use advanced methods such as the Trott–Strzebonski in-place evaluation technique.

I would be very interested to see a nice and general solution to this more complicated problem without resorting to such esoteric techniques. EDIT: See @MrWizard's solution.


This kind of partial evaluation can be done using my step function from How do I evaluate only one step of an expression?

Function[Null, fun[c_, k_, kF_, LatticeSize_] := #, HoldAll] @@ step[generate[]]

?fun
fun[c_, k_, kF_, LatticeSize_] := 2 ev[c, k, kF, LatticeSize]

The advantage of this over Block is that you do not need to know in advance what ev is.