3.11 Implementing New List Objects

This section gives some hints for the quite usual situation that one wants to implement new objects that are lists. More precisely, one either wants to deal with lists that have additional features, or one wants that some objects also behave as lists.

A list in GAP is an object in the category IsList. Basic operations for lists are Length, \[\], and IsBound\[\] (see Basic Operations for Lists in the Reference Manual).

Note that the access to the position pos in the list list via list[pos] is handled by the call \[\]( list, pos ) to the operation \[\]. To explain the somewhat strange name \[\] of this operation, note that non-alphanumeric characters like [ and ] may occur in GAP variable names only if they are escaped by a \ character.

Analogously, the check IsBound( list[pos] ) whether the position pos of the list list is bound is handled by the call IsBound\[\]( list, pos ) to the operation IsBound\[\].

For mutable lists, also assignment to positions and unbinding of positions via the operations \[\]\:\= and Unbind\[\] are basic operations. The assignment list[pos]:= val is handled by the call \[\]\:\=( list, pos, val ), and Unbind( list[pos] ) is handled by the call Unbind\[\]( list, pos ).

All other operations for lists, e.g., Add, Append, Sum, are based on these operations. This means that it is sufficient to install methods for the new list objects only for the basic operations.

So if one wants to implement new list objects then one creates them as objects in the category IsList, and installs methods for Length, \[\], and IsBound\[\]. If the new lists shall be mutable, one needs to install also methods for \[\]\:\= and Unbind\[\].

One application for this is the implementation of enumerators for domains. An enumerator for the domain D is a dense list whose entries are in bijection with the elements of D. If D is large then it is not useful to write down all elements. Instead one can implement such a bijection implicitly. This works also for infinite domains.

In this situation, one implements a new representation of the lists that are already available in GAP, in particular the family of such a list is the same as the family of the domain D.

But it is also possible to implement new kinds of lists that lie in new families, and thus are not equal to lists that were available in GAP before. An example for this is the implementation of matrices whose multiplication via '*' is the Lie product of matrices.

In this situation, it makes no sense to put the new matrices into the same family as the original matrices. Note that the product of two Lie matrices shall be defined but not the product of an ordinary matrix and a Lie matrix. So it is possible to have two lists that have the same entries but that are not equal w.r.t. '=' because they lie in different families.

[Top] [Previous] [Up] [Next] [Index]

GAP 4 manual
February 2000