5
$\begingroup$

I would like to inactivate all occurrences of a symbol to prevent it from evaluating, regardless where in the evaluation process it appears. A minimal example:

g[x_] := foo;
f[x_] := x + g[x];
Inactivate[f[2] + g[3], g]
(*== output ==*)
2 + f00 + Inactive[g][3]

My desired output would be 2 + Inactive[g][2] + Inactive[g][3].

I understand that there would be thorny issues in general how to handle scoping constructs such as Block and Module but my naïve wish is "inactivate everywhere there is a head g and don't care about anything else".

(Additional comments: I don't want to write a temporary overwriting definition of g as the function of my actual interest has several down values which take quite some time to compute. Something like [the fictitious] SetAttributes[g,NeverEvaluates] could be something though.)

$\endgroup$

1 Answer 1

11
$\begingroup$

I believe that this works, your final paragraph notwithstanding as the downvalues of g are not lost by using Block.

Block[{g = Inactive[g]}, f[2] + g[3]]
2 + Inactive[g][2] + Inactive[g][3]

Since Inactive has HoldFirst infinite recursion does not occur.

$\endgroup$
1
  • 1
    $\begingroup$ This works as long as g is not redefined inside a Block inside another definition (like f[x_]:=Block[{g}, g[y_]:=bar; x + g[x]]), which is rather contrived indeed. It does work for my purposes so I accept and smile. Thanks! $\endgroup$ Commented Oct 29, 2018 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.