# Upvalues, TagSet and UpSet, what's the difference, when should a use each?

Some of the big mysteries of Universe Mathematica for me is the application of UpValues. I know about this question and know the very poor Mathematica documentation on that. I'm very curious to know if someone that not Leonid uses this too.

Just to get some clue on how it works, I would like to know what's the difference between TagSet and UpSet. Can I say that UpSet is subset of TagSet?

For instance, I can say these examples are equivalent.

name[alien]^= "Alf"
alien /: name[alien] = "Alf"


In what situations should I apply each case?

-
In addition to the answer of @Mr.Wizard, I discussed exactly this issue in this answer (last subsection), where it is put in a wider context of UpValues in general. – Leonid Shifrin Jan 25 '13 at 10:11
Tks @LeonidShifrin, It was very helpfull. – Murta Jan 26 '13 at 0:52
Murta, I see that you did not Accept my answer. Is there something I can do to make it more satisfactory? – Mr.Wizard Feb 27 '13 at 8:31
No! Just forgot. Tks! – Murta Feb 27 '13 at 11:14

UpSet and UpSetDelayed make multiple assignments:

UpSet associates an assignment with all the distinct symbols that occur either directly as arguments of lhs, or as the heads of arguments of lhs.

f[a[__], b[__], c[__]] ^:= "UpValue"

UpValues[a]
UpValues[b]
UpValues[c]

{HoldPattern[f[a[__], b[__], c[__]]] :> "UpValue"}

{HoldPattern[f[a[__], b[__], c[__]]] :> "UpValue"}

{HoldPattern[f[a[__], b[__], c[__]]] :> "UpValue"}


TagSet and TagSetDelayed make specific assignments:

k /: g[i[__], j[__], k[__]] := "UpValue"

UpValues[i]
UpValues[j]
UpValues[k]

{}

{}

{HoldPattern[g[i[__], j[__], k[__]]] :> "UpValue"}

-
Is there any meaning to which symbol a rule is assigned to? Maybe who I have to Clear to get rid of it? – masterxilo Jul 6 at 21:09
@masterxilo I may not understand the question. In the first example above the rule is bound to all three Symbols a, b, and c, whereas in the second example it is only bound to k. – Mr.Wizard Jul 6 at 22:08
Yes, I understand that. But does using TagSet have any observable consequence other than that I have to do Clear[a,b,c] in the first example and just Clear[k] to get rid of this rule? – masterxilo Jul 7 at 0:38
@masterxilo Ah, okay, sorry I didn't understand that the first time. I believe I have experienced at least one case where there are, but at the moment I can't think of an example. Let me get back to you on that. – Mr.Wizard Jul 7 at 0:44