8
$\begingroup$

I have a very simple question about redefining basic built-in functions, such as Plus. I'm pretty sure someone must have asked a similar question around here, but I couldn't find one. Consider the following code:

Unprotect[Plus];
x_ + y_ := x y
Protect[Plus];

For symbols this works as expected:

x+y
(* ---> x y *)

For numbers, however, the new definition does not work:

1+2
(* ---> 3 *)

Of course I know that it is stupid to use something like this and that defining functions or using upvalues etc. is the way to go here, but I'm really interested in why this happens. Can the pattern for Plus be defined in such a way that it works for numbers as well? Is Plus[1,2] evaluated internally before the pattern can be matched?

$\endgroup$
8
  • 2
    $\begingroup$ And here is a very simple answer: don't do it. $\endgroup$ Commented Feb 6, 2013 at 0:04
  • 4
    $\begingroup$ IMO, this internal mechanics of Plus should not be relevant to the user. To me, this seems one of those internal details which can not serve you well even if you know them. Plus, Times and perhaps a few more fundamental functions have deviations from the standard evaluation semantics, so that certain buit-in rules like the one you illustrated apply before the user-defined rules. The whole fact that this was done in this way shows that those rules were considered very fundamental by developers, which brings the question again: how would the knowledge of exact semantics here be helpful? $\endgroup$ Commented Feb 6, 2013 at 0:13
  • 3
    $\begingroup$ Operations like Plus and Times are deeply wired in the system and cannot be modified beyond a certain point, as they're essential to pretty much everything else. For example, try: Unprotect@Plus; Remove@Plus; 1+1 Look ma! No Plus! But you still get 2. However Plus[1,1] remains unevaluated. $\endgroup$
    – rm -rf
    Commented Feb 6, 2013 at 0:14
  • 1
    $\begingroup$ I somehow thought it was interesting to find out where this point, to which they can be modified is, no matter if this is useful or not. But now I'm thinking of deleting the question. The fundamental difference between how Plus works for numbers and symbols was just something that seemed strange to me. $\endgroup$
    – einbandi
    Commented Feb 6, 2013 at 0:17
  • 2
    $\begingroup$ I think that this is a rather important detail, and perhaps having this question around would be nice, since people come back to this every now and then. Just a day ago there was a similar question on Mathgroup. It is just this kind of detail where pretty much all one can say is "you do want to know about the mere fact of it, but you don't want to know anything deeper than that". $\endgroup$ Commented Feb 6, 2013 at 0:21

1 Answer 1

15
$\begingroup$

See Plus in the reference manual:

Unlike other functions, Plus applies built-in rules before user-defined ones. As a result, it is not possible to make definitions such as 2+2=5.

The ability for user-defined rules to supersede built-in ones was lost in Version 3

$\endgroup$
1
  • $\begingroup$ Note the same restriction applies to Times. $\endgroup$
    – Michael E2
    Commented Jan 31, 2016 at 12:57

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.