next | previous | forward | backward | up | top | index | toc | Macaulay2 web site
Macaulay2Doc > The Macaulay2 language > installing methods

installing methods

The method to be used for computing an expression such as -x depends on the type of x. For example, the method for negating a polynomial differs from the method for negating an integer modulo 111. Each method is a function of one variable, and is stored in the class of x under a key that is referred to as the name of the method. For some built-in methods the method name is a symbol, but for methods created with method, the method name is the same as the function used for calling it up.

Let's assume that X is the class of x. The way to install a method for the negation of an instance x of X is with a statement of the following form.

- X := x ->( ... )
Here ( ... ) 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. Here is a brief description of the way this works. Suppose X is the parent of P. When an expression -p is to be evaluated, where the class of p is P, then the method for -P is applied, unless there isn't one, in which case the method for -X is applied, and so on, all the way up the chain of parents to the topmost ancestor of everything, which is called Thing.

As an extreme example of inheritance, code like

- Thing := x -> ...
will install a method for negating anything, which will take effect as a last resort whenever a more specifically defined method isn't found. It probably isn't a good idea to install such a method, for usually all it can do is to print an error message.

The user may introduce new methods as well as new method names. So it is important to understand how methods are installed and consulted.

Applying a method named C to a thing x whose class is X means that

(lookup(C,X)) x
is evaluated. In other words, C is used as a key to obtain a function from X (or its parent, grandparent, and so on), and the function is applied to x. See lookup.

Installing a method named C for the class X is done with code such as

C X := (x) -> ( ... )
where ( ... ) represents suitable code for the operation at hand.

The routine for making new methods is method.

See also