One example of how rules can produce unwanted results is the rule a*0 <-- 0. This would always seem to be true. However, when a is a vector, like a:={b,c,d} , then a*0 should actually return {0,0,0}, that is, a zero vector. The rule a*0 <-- 0 actually changes the type of the expression from a vector to an integer! This can have severe consequences when other functions using this expressions as an argument expect a vector, or even worse, have a definition of how to work on vectors, and a different one for working on numbers.
When writing rules for an operator, it is assumed that the operator working on arguments, like Cos or *, will always have the same properties regardless of the arguments. The Taylor series expansion of Cos(a) is the same regardless of whether a is a real number, complex number or even a matrix. Certain trigonometric identities should hold for the Cos function, regardless of the type of its argument.
If a function is defined which does not adhere to these rules when applied to another type, a different function name should be used, to avoid confusion.
By default, if a variable has not been bound yet, it is assumed to be a number. If it is in fact a more complex object, e.g. a vector, then you can declare it to be an "incomplete type" vector, using Object("IsVector",x) instead of x. This expression will evaluate to x if and only if x is a vector at that moment of evaluation. Otherwise it returns unevaluated, and thus stays an incomplete type.
So this means the type of a variable is numeric unless otherwise stated by the user, using the "Object" command. No rules should ever work on incomplete types. It is just meant for delayed simplification.
The topic of implicit type of an object is important, since many rules need to assume something about their argument types.
A rule with precedence 100 is defined by the syntax such as
100 # f(_x + _y) <-- f(x) + f(y); |
The problem mentioned above with a rule for vectors and scalars could be solved by making two rules:
So vector multiplication would be tried first.
The ordering of the precedence of the rules in the standard math scripts is currently: