TagSet and the listability of Plus[ ]
- To: mathgroup at smc.vnet.net
- Subject: [mg61234] TagSet and the listability of Plus[ ]
- From: "Trevor Baca" <trevorbaca at gmail.com>
- Date: Thu, 13 Oct 2005 01:39:41 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
This is a question about overriding Plus[ ] for a particular type of expression. Say you define a foo to be the list of an integer and a string: FooQ[expr_] := MatchQ[expr, {_Integer, _String}]. Then, for example, with ... f1 = {3, "bar"}; f2 = {4, "tick"}; ... we see that Plus[ ] does what it always does and threads across lists ... f1 + f2 {4, bar+tick}. But let's say that we want to define addition on foo as the arithmetic sum of the *first* parts but with the string join of the *second* parts. Here's a not terribly successful strategy with TagSet: Unprotect[Plus]; Plus /: m_?FooQ + n_?FooQ := {First[m] + First[n], StringJoin[Last[m], Last[n]]}; Protect[Plus] This interprets correctly and we see DownValues[Plus] {HoldPattern[m_?FooQ + n_?FooQ] :> {First[m] + First[n], Last[m] <> Last[n]}}. However, when we try ... f1 + f2 ... we get ... {7, bar+tick} ... rather than ... {7, bartick} ... unfortunately. I'm pretty sure the reason for this is that the interpreter looks at f1 + f2 and immediately produces the full form ... List[7, Plus["bar", "tick"]] ... which means that the clever downvalue for Plus[ ] does absolutely nothing. A reasonable solution is to use a head other than list, as in foo[3, bar] + foo[4, tick] ... in which case upvalues work fine: foo[a_, b_] + foo[j_, k_] ^:= foo[a+j, StringJoin[b, k]]. But now I've developed something of a small obsession to override Plus without introducing a special head. Any suggestions?