Method Selection
operation
method
This chapter explains how &GAP; decides which function to call for which types of objects. It assumes that you have read the chapters about objects (Chapter ) and types (Chapter ).

An operation is a special &GAP; function that bundles a set of functions, its methods.

All methods of an operation compute the same result. But each method is installed for specific types of arguments.

If an operation is called with a tuple of arguments, one of the applicable methods is selected and called.

Special cases of methods are partial methods, immediate methods, and logical implications.

Operations and Methods

Operations are functions in the category .

So on the one hand, operations are &GAP; functions, that is, they can be applied to arguments and return a result or cause a side-effect.

On the other hand, operations are more. Namely, an operation corresponds to a set of &GAP; functions, called the methods of the operation.

Each call of an operation causes a suitable method to be selected and then called. The choice of which method to select is made according to the types of the arguments, the underlying mechanism is described in the following sections.

Examples of operations are the binary infix operators =, + etc., and is the operation that is called for each argument of .

Also all attributes and properties are operations. Each attribute has a special method which is called if the attribute value is already stored; this method of course simply returns this value.

The setter of an attribute is called automatically if an attribute value has been computed. Attribute setters are operations, too. They have a default method that ignores the request to store the value. Depending on the type of the object, there may be another method to store the value in a suitable way, and then set the attribute tester for the object to true.

Method Installation

In order to describe what it means to select a method of an operation, we must describe how the methods are connected to their operations.

For attributes and properties there is .

For declaring that a filter is implied by other filters there is .

<#Include Label="InstallMethod"> <#Include Label="InstallOtherMethod">

Applicable Methods and Method Selection

A method installed as above is applicable for an arguments tuple if the following conditions are satisfied.

The number of arguments equals the length of the list args-filts, the i-th argument lies in the filter args-filts[i], and famp returns true when applied to the families of the arguments. The maximal number of arguments supported for methods is six, one gets an error message if one tries to install a method with at least seven arguments.

So args-filt describes conditions for each argument, and famp describes a relation between the arguments.

For unary operations such as attributes and properties, there is no such relation to postulate, famp is for these operations, a function that always returns true. For binary operations, the usual value of famp is , which means that both arguments must lie in the same family.

Note that any properties which occur among the filters in the filter list will not be tested by the method selection if they are not yet known. (More exact: if prop is a property then the filter implicitly uses not prop but Hasprop and prop.) If this is desired you must explicitly enforce a test (see section ) below.

If no method is applicable, the error message no method found is signaled.

Otherwise, the applicable method with highest rank is selected and then called. This rank is given by the sum of the ranks of the filters in the list args-filt, including involved filters, plus the number val used in the call of . So the argument val can be used to raise the priority of a method relative to other methods for opr.

Note that from the applicable methods, an efficient one shall be selected. This is a method that needs only little time and storage for the computations.

It seems to be impossible for &GAP; to select an optimal method in all cases. The present ranking of methods is based on the assumption that a method installed for a special situation shall be preferred to a method installed for a more general situation.

For example, a method for computing a Sylow subgroup of a nilpotent group is expected to be more efficient than a method for arbitrary groups. So the more specific method will be selected if &GAP; knows that the group given as argument is nilpotent.

Of course there is no obvious way to decide between the efficiency of incommensurable methods. For example, take an operation with one method for permutation groups, another method for nilpotent groups, but no method for nilpotent permutation groups, and call this operation with a permutation group known to be nilpotent.

Partial Methods
After a method has been selected and called, the method may recognize that it cannot compute the desired result, and give up by calling TryNextMethod().

In effect, the execution of the method is terminated, and the method selection calls the next method that is applicable w.r.t. the original arguments. In other words, the applicable method is called that is subsequent to the one that called , according to decreasing rank of the methods.

For example, since every finite group of odd order is solvable, one may install a method for the property that checks whether the size of the argument is an odd integer, returns true if so, and gives up otherwise.

Care is needed if a partial method might modify the type of one of its arguments, for example by computing an attribute or property. If this happens, and the type has really changed, then the method should not exit using TryNextMethod() but should call the operation again, as the new information in the type may cause some methods previously judged inapplicable to be applicable. For example, if the above method for actually computes the size, (rather than just examining a stored size), then it must take care to check whether the type of the group has changed.
Redispatching

As mentioned above the method selection will not test unknown properties. In situations, in which algorithms are only known (or implemented) under certain conditions, however such a test might be actually desired.

One way to achieve this would be to install the method under weaker conditions and explicitly test the properties first, exiting via if some of them are not fulfilled. A problem of this approach however is that such methods then automatically are ranked lower and that the code does not look nice.

A much better way is to use redispatching: Before deciding that no method has been found one tests these properties and if they turn out to be true the method selection is started anew (and will then find a method).

This can be achieved via the following function:

<#Include Label="RedispatchOnCondition">

Immediate Methods

Usually a method is called only if its operation has been called and if this method has been selected, see .

For attributes and properties, one can install also immediate methods.

<#Include Label="InstallImmediateMethod">

Logical Implications
<#Include Label="InstallTrueMethod">

Operations and Mathematical Terms
overload
Usually an operation stands for a mathematical concept, and the name of the operation describes this uniquely. Examples are the property and the attribute . But there are cases where the same mathematical term is used to denote different concepts, for example Degree is defined for polynomials, group characters, and permutation actions, and Rank is defined for matrices, free modules, p-groups, and transitive permutation actions.

It is in principle possible to install methods for the operation Rank that are applicable to the different types of arguments, corresponding to the different contexts. But this is not the approach taken in the &GAP; library. Instead there are operations such as for matrices and (in fact these are attributes) which are installed as methods of the ambiguous operations Rank and Degree.

The idea is to distinguish between on the one hand different ways to compute the same thing (e.g. different methods for , , etc.), and on the other hand genuinely different things (such as the degree of a polynomial and a permutation action).

The former is the basic purpose of operations and attributes. The latter is provided as a user convenience where mathematical usage forces it on us and where no conflicts arise. In programming the library, we use the underlying mathematically precise operations or attributes, such as and RankOperation. These should be attributes if appropriate, and the only role of the operation Rank is to decide which attribute the user meant. That way, stored information is stored with full mathematical precision and is less likely to be retrieved for a wrong purpose later.

One word about possible conflicts. A typical example is the mathematical term centre, which is defined as \{ x \in M | a * x = x * a \forall a \in M \} for a magma M, and as \{ x \in L | l * x = 0 \forall l \in L \} for a Lie algebra L. Here it is not possible to introduce an operation that delegates to attributes CentreOfMagma and CentreOfLieAlgebra, depending on the type of the argument. This is because any Lie algebra in &GAP; is also a magma, so both CentreOfMagma and CentreOfLieAlgebra would be defined for a Lie algebra, with different meaning if the characteristic is two. So we cannot achieve that one operation in &GAP; corresponds to the mathematical term centre.

Ambiguous
operations such as Rank are declared in the library file lib/overload.g.