Method Selection

Steve Linton

0.1  Method Selection - The Problem

0.2  Some Terminology

Use of one name to refer to multiple functions (or objects)
Use of the same code to compute with different types of data

0.3  Method Selection 1 - Dodge the Problem

0.4  Method Selection 2 - Monster Case Statements

0.5  Method Selection 3 - The Object Oriented Approach

0.6  Limitations of Object Orientation 1

GAP3 experience found two main limitations.

0.7  Limitations of Object Orientation 2

0.8  The GAP4 Method Selection Concept

0.9  Example

MyExponent := NewOperation("MyExponent",[IsGroup]);

InstallMethod(MyExponent, "Abelian method based on generators",
        true, [HasGeneratorsOfGroup and IsAbelian and IsGroup], 0,
    return Lcm(List(GeneratorsOfGroup(g),Order));
InstallMethod(MyExponent, "Default method",
        true, [IsGroup], 0, 
    local e,x; e := 1;
    for x in g do e := Lcm(e,Order(x)); od;
    return e;

0.10  Example Continued

gap> gps := [SmallGroup(7,1),SmallGroup(9,1),SmallGroup(9,2)];
[ Group( [ f1 ], ... ), Group( [ f1, f2 ], ... ), 
Group( [ f1, f2 ], ... ) ]
gap> TraceMethods(MyExponent);
gap> List(gps,MyExponent);
#I  MyExponent: Abelian method based on generators
#I  MyExponent: Default method
#I  MyExponent: Default method
[ 7, 9, 3 ]
gap> HasIsAbelian(gps[2]);
gap> IsAbelian(gps[2]);
gap> MyExponent(gps[2]);;
#I  MyExponent: Abelian method based on generators

0.11  Method Installation in More Detail

0.12  Filters and Flag lists

0.13  Method Selection

0.14  Attributes and Properties

0.15  Types of Filter

These distinctions have no effect on method selection, but alter the way filters are created and set

These are fixed when an object is created, and control what basic operations are defined for the object (a bit like abstract classes in C++) (eg IsAdditiveElement implies that a + is defined)
Fixed when an object is created, describe how the object is stored, used to select low-level access methods
Attribute testers
Set when an attribute value is stored (eg HasIsAbelian)
Set when a property is known to hold (eg IsAssociative)
A small number of special filters ( eg CanEasilyComputePcgs)

0.16  Relations between Objects

0.17  Families

0.18  True Overloading

0.19  Other Features 1

Kernel objects
Objects like permutations have an implicit type, which cannot change. For basic operations (`+', `*', PrintObj, etc.) the kernel selects the method. Other operations see the implicit type.
A few functions, like AbelianGroup take a flags list as first argument. The method selected is one that will return an object with at least those flags (contravariant)
No method selection for functions of variable numbers of arguments - just one method allowed

Often these functions can call an underlying method with fixed number of args - Print, PrintObj

0.20  Other Features 2

This does what it says. If you have two logically different methods whose applicability is NOT captured by the method selection, one can try to solve the problem and then fall through to the other.
Caching and Hashing
Various caches are used to ensure that two equal families are always identical (same memory location) and two equal types usually are. This costs time when they are created but saves time and space later.

Each operation caches the most recent (sets of) types it was called with (currently five) and the method selected (including next methods). Most calls hit this cache.

Also cache the result of and for flags lists and a few other things

0.21  Other Features 3

Immediate Methods
Also called ``Zero Cost Methods'' These one-argument methods are run whenever an object acquires the required filters. They are used to ``fill in'' cheap consequences of new information
Virtual Lists and Records
The list access and assignment operators \[\], \[\]\:\= and related things are Operations. An object can have the IsList filter and methods installed for these operations and be a ``virtual'' list (possibly infinite).
In a similar way, the for statement will call the Iterator operation to obtain a way fo running through an object.

File translated from TEX by TTH, version 2.87.
On 10 May 2004, 09:34.