3
$\begingroup$

I came across a bit of code that uses the syntax /: and I don't know what it does. I can't find its documentation, or maybe I'm just not looking properly. The code snippet is something like:

F/:F[A___,b_?NumberQ B_, C___] := b*F[A,B,C]

(i.e. part of the definition of multi-linearity). How does this behave differently than the following?:

G[A___,b_?NumberQ B_, C___] := b*G[A,B,C]

Thanks!

$\endgroup$
10
  • 13
    $\begingroup$ In the spirit of teaching people how to fish: highlight the /: part of the expression. Press F1. The help file should give links to the relevant functions. $\endgroup$ May 3, 2012 at 17:42
  • 2
    $\begingroup$ Or at the very least, try entering /: into the doc center and see what comes up $\endgroup$
    – rm -rf
    May 3, 2012 at 17:43
  • 1
    $\begingroup$ Check TagSet in docs $\endgroup$
    – kglr
    May 3, 2012 at 17:44
  • $\begingroup$ @J.M.,@R.M,@kguler: Thanks for the tips, I didn't know about the F1 thing. Having read those documentation pages, I still don't understand the difference between my two lines of code. $\endgroup$
    – Ian Hincks
    May 3, 2012 at 17:55
  • 4
    $\begingroup$ @IanHincks In this case, there is none. The docs mention that in the Scope section. For OwnValues (variables) and DownValues (functions), you can use SetDelayed (:=) with the same effect. But for UpValues (which are another kind of global rules), you need to use TagSetDelayed (/:), or perhaps UpSetDelayed (although I favor the former). $\endgroup$ May 3, 2012 at 18:00

1 Answer 1

17
$\begingroup$

/: is the short-hand notation for TagSetDelayed, which is creating UpValues. It's useful for over-loading how a particular function behaves with a specific head. For example:

In[1]:= h /: Plus[x : h[arg1_, arg2_], y : h[arg3_, arg4_]] := Plus[arg1, arg2, arg3, arg4]

In[2]:= h[1, 2] + h[3, 4]
Out[2]= 10

The benefit being you don't have to Unprotect[Plus] to set the definition, and if you Remove[h] this definition will be wiped out as well.

$\endgroup$
4
  • 1
    $\begingroup$ Thanks @nikko. What purpose do the x and y serve in this example? It seems to work without them. $\endgroup$
    – Ian Hincks
    May 3, 2012 at 18:35
  • $\begingroup$ @IanHincks in this example, they don't serve any purpose, but they could be used to refer to the entire object matched by h[arg1_, arg2_]. So, if you had need of the entire pattern, you could use it. $\endgroup$
    – rcollyer
    May 3, 2012 at 18:36
  • 4
    $\begingroup$ In this case, it is TagSetDelayed rather than TagSet. The syntax in fact has to parts: s/:lhs:=rhs or s/:lhs=rhs, so /: alone is not enough to tell which one it is. Also, a correct statement is that TagSetDelayed (or TagSet) can create UpValues (but it can also create OwnValues, DownValues and SubValues, in which case, the tag part is redundant). $\endgroup$ May 3, 2012 at 18:43
  • $\begingroup$ yes, sorry the x: and y: are superfluous here. $\endgroup$
    – nikko
    May 3, 2012 at 18:45

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.