This expression is taken from a talk given by the late Robby Villegas. The UpValues to some symbol a is defined below as:

a /: _[___, a, ___] := (Print["a fired"]; Null)

Nothing I tried could Clear the definitions associated with the symbol. Is there a way to clear the associated UpValues.

  • 5
    $\begingroup$ Regardless of the method to solve this problem, I will mention that giving such generic UpValues to symbols is outright dangerous. Speaking from experience here, I have tried to use this kind of rules before, and it was invariably biting me later - so I never ended up leaving such constructs in final code in whatever I was doing. $\endgroup$ – Leonid Shifrin Sep 8 '18 at 17:33

Note that you could clear all of the *Values by using Clear["a"] or ClearAll["a"]. However, if you only want to clear this particular UpValues you could make use of the fact that HoldAllComplete prevents UpValues from firing. So, temporarily give TagUnset this attribute. Here's your UpValues definition:

a /: _[___,a,___] := (Print["a fired"];Null)

Give TagUnset the HoldAllComplete attribute, and unset the above definition:

    SetAttributes[TagUnset, HoldAllComplete];
    TagUnset[a, _[___,a,___]]




  • $\begingroup$ Thanks. Very neat. $\endgroup$ – Ali Hashmi Sep 9 '18 at 20:04
a /: Except[TagUnset, _][___, a, ___] := (Print["a fired"]; Null);

(* a fired *)

(* As we can see below, usage of ClearAll does not work *)

(* a fired *)

(* now removing the associated values using the TagUnset Head *)

TagUnset[Unevaluated[a],HoldPattern[Except[TagUnset, _][___, a, ___]]];

(* f[a] *)
  • $\begingroup$ I think both the Unevaluated and the HoldPattern are unnecessary. $\endgroup$ – Carl Woll Sep 9 '18 at 6:48
  • $\begingroup$ @Carl .. though unnecessary but I think it is a safe practice $\endgroup$ – Ali Hashmi Sep 9 '18 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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