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?
Answer
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"}
Comments
Post a Comment