8
$\begingroup$

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

ClearAll["a"];
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.

$\endgroup$
1
  • 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$ Commented Sep 8, 2018 at 17:33

2 Answers 2

9
$\begingroup$

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:

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

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

Internal`InheritedBlock[{TagUnset},
    SetAttributes[TagUnset, HoldAllComplete];
    TagUnset[a, _[___,a,___]]
]

Check:

UpValues[a]

{}

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

f[a]
(* a fired *)

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

ClearAll[a]
(* a fired *)


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

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

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

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.