# binary methods

The method for computing a sum x+y depends on the types of x and y. For example, the method for adding an integer x and a polynomial y differs from the method for adding two integers modulo 111. Because both the type of x and the type of y must enter into the selection of the method, we refer to these methods as binary methods. Each binary method is a function of two variables, and is stored either in the class of x or in the class of y.

Let's assume that X is the class (or type) of x, and that Y is the class of y. The way to install a method for the addition of an instance x of class X to an instance y of class Y is with a statement of the form

X + Y := (x,y) -> ( ... )
where ( ... ) represents the body of the function, consisting of suitable code for the operation at hand.

The method installed by the code above is automatically inherited by subclasses of X and Y. Here is a brief description of the way this works. Suppose X is the parent of P and Y is the parent of X. When a sum p+q is evaluated where the class of p is P and the class of q is Q, then the binary method for P+Q is applied, unless there isn't one, in which case the binary method for P+Y is applied, unless there isn't one, in which case the binary method for X+Q is applied, unless there isn't one, in which case the binary method for P+Q is applied. In general this search for a binary method continues all the way up the chain of parents to the topmost ancestor of everything, which is called Thing. (See also lookup.)

As an extreme example of inheritance, the code

Thing + Thing := (x,y) -> ( ... )
will install a binary method for adding any two things, which will take effect as a last resort whenever more a specifically defined method isn't found.

The new function also uses a ternary lookup table to find the initialization function for the new thing, and should be thought of as a ternary operator. The initialization function for a new expression created by

new Z of x from y
is obtained as
lookup(NewMethod,Z,X,Y)
Here X is class x, and Y is class y. The initialization function can be installed with
new Z of X from Y := (z,y) -> ...
where z denotes the new hash table of class Z and parent x provided to the routine by the system.