Types of Objects

Every &GAP; object has a type. The type of an object is the information which is used to decide whether an operation is admissible or possible with that object as an argument, and if so, how it is to be performed (see Chapter ).

For example, the types determine whether two objects can be multiplied and what function is called to compute the product. Analogously, the type of an object determines whether and how the size of the object can be computed. It is sometimes useful in discussing the type system, to identify types with the set of objects that have this type. Partial types can then also be regarded as sets, such that any type is the intersection of its parts.

The type of an object consists of two main parts, which describe different aspects of the object.

The family determines the relation of the object to other objects. For example, all permutations form a family. Another family consists of all collections of permutations, this family contains the set of permutation groups as a subset. A third family consists of all rational functions with coefficients in a certain family.

The other part of a type is a collection of filters (actually stored as a bit-list indicating, from the complete set of possible filters, which are included in this particular type). These filters are all treated equally by the method selection, but, from the viewpoint of their creation and use, they can be divided (with a small number of unimportant exceptions) into categories, representations, attribute testers and properties. Each of these is described in more detail below.

This chapter does not describe how types and their constituent parts can be created. Information about this topic can be found in Chapter .

Note:
Detailed understanding of the type system is not required to use &GAP;. It can be helpful, however, to understand how things work and why &GAP; behaves the way it does.

A discussion of the type system can be found in .

Families

The family of an object determines its relationship to other objects.

More precisely, the families form a partition of all &GAP; objects such that the following two conditions hold: objects that are equal w.r.t. = lie in the same family; and the family of the result of an operation depends only on the families of its operands.

The first condition means that a family can be regarded as a set of elements instead of a set of objects. Note that this does not hold for categories and representations (see below), two objects that are equal w.r.t. = need not lie in the same categories and representations. For example, a sparsely represented matrix can be equal to a densely represented matrix. Similarly, each domain is equal w.r.t. = to the sorted list of its elements, but a domain is not a list, and a list is not a domain.

<#Include Label="FamilyObj">

Filters

A filter is a special unary &GAP; function that returns either true or false, depending on whether or not the argument lies in the set defined by the filter. Filters are used to express different aspects of information about a &GAP; object, which are described below (see , , , , , ).

Presently any filter in &GAP; is implemented as a function which corresponds to a set of positions in the bitlist which forms part of the type of each &GAP; object, and returns true if and only if the bitlist of the type of the argument has the value true at all of these positions.

The intersection (or meet) of two filters filt1, filt2 is again a filter, it can be formed as

and
filt1 and filt2

See for more details.

For example, IsList and IsEmpty is a filter that returns true if its argument is an empty list, and false otherwise. The filter is defined as the intersection of the category and the property .

A filter that is not the meet of other filters is called a simple filter. For example, each attribute tester (see ) is a simple filter. Each simple filter corresponds to a position in the bitlist currently used as part of the data structure representing a type.

Every filter has a rank, which is used to define a ranking of the methods installed for an operation, see Section . The rank of a filter can be accessed with .

For simple filters, an incremental rank is defined when the filter is created, see the sections about the creation of filters: , , , . For an arbitrary filter, its rank is given by the sum of the incremental ranks of the involved simple filters; in addition to the implied filters, these are also the required filters of attributes (again see the sections about the creation of filters). In other words, for the purpose of computing the rank and only for this purpose, attribute testers are treated as if they would imply the requirements of their attributes.
returns a list of names of the implied simple filters of the filter filt, these are all those simple filters imp such that every object in filt also lies in imp. For implications between filters, see as well as sections , , , .
<#Include Label="ShowImpliedFilters">

Categories

The categories of an object are filters (see ) that determine what operations an object admits. For example, all integers form a category, all rationals form a category, and all rational functions form a category. An object which claims to lie in a certain category is accepting the requirement that it should have methods for certain operations (and perhaps that their behaviour should satisfy certain axioms). For example, an object lying in the category must have methods for , and the list element access operation .

An object can lie in several categories. For example, a row vector lies in the categories and ; each list lies in the category , and depending on whether or not it is mutable, it may lie in the category . Every domain lies in the category .

Of course some categories of a mutable object may change when the object is changed. For example, after assigning values to positions of a mutable non-dense list, this list may become part of the category .

However, if an object is immutable then the set of categories it lies in is fixed.

All categories in the library are created during initialization, in particular they are not created dynamically at runtime.

The following list gives an overview of important categories of arithmetic objects. Indented categories are to be understood as subcategories of the non indented category listed above it. IsObject IsExtLElement IsExtRElement IsMultiplicativeElement IsMultiplicativeElementWithOne IsMultiplicativeElementWithInverse IsExtAElement IsAdditiveElement IsAdditiveElementWithZero IsAdditiveElementWithInverse Every object lies in the category .

The categories and contain objects that can be multiplied with other objects via * from the left and from the right, respectively. These categories are required for the operands of the operation *.

The category contains objects that can be multiplied from the left and from the right with objects from the same family. contains objects obj for which a multiplicatively neutral element can be obtained by taking the 0-th power obj^0. contains objects obj for which a multiplicative inverse can be obtained by forming obj^-1.

Likewise, the categories , , and contain objects that can be added via + to other objects, objects that can be added to objects of the same family, objects for which an additively neutral element can be obtained by multiplication with zero, and objects for which an additive inverse can be obtained by multiplication with -1.

So a vector lies in , and . A ring element must additionally lie in .

As stated above it is not guaranteed by the categories of objects whether the result of an operation with these objects as arguments is defined. For example, the category is a subcategory of . Clearly not every matrix has a multiplicative inverse. But the category makes each matrix an admissible argument of the operation , which may sometimes return fail. Likewise, two matrices can be multiplied only if they are of appropriate shapes.

Analogous to the categories of arithmetic elements, there are categories of domains of these elements. IsObject IsDomain IsMagma IsMagmaWithOne IsMagmaWithInversesIfNonzero IsMagmaWithInverses IsAdditiveMagma IsAdditiveMagmaWithZero IsAdditiveMagmaWithInverses IsExtLSet IsExtRSet Of course is a subcategory of . A domain that is closed under multiplication * is called a magma and it lies in the category . If a magma is closed under taking the identity, it lies in , and if it is also closed under taking inverses, it lies in . The category denotes closure under taking inverses only for nonzero elements, every division ring lies in this category.

Note that every set of categories constitutes its own notion of generation, for example a group may be generated as a magma with inverses by some elements, but to generate it as a magma with one it may be necessary to take the union of these generators and their inverses.

<#Include Label="CategoriesOfObject">

Representation

The representation of an object is a set of filters (see ) that determines how an object is actually represented. For example, a matrix or a polynomial can be stored sparsely or densely; all dense polynomials form a representation. An object which claims to lie in a certain representation is accepting the requirement that certain fields in the data structure be present and have specified meanings.

&GAP; distinguishes four essentially different ways to represent objects. First there are the representations IsInternalRep for internal objects such as integers and permutations, and IsDataObjectRep for other objects that are created and whose data are accessible only by kernel functions. The data structures underlying such objects cannot be manipulated at the &GAP; level.

All other objects are either in the representation IsComponentObjectRep or in the representation IsPositionalObjectRep, see and .

An object can belong to several representations in the sense that it lies in several subrepresentations of IsComponentObjectRep or of IsPositionalObjectRep. The representations to which an object belongs should form a chain and either two representations are disjoint or one is contained in the other. So the subrepresentations of IsComponentObjectRep and IsPositionalObjectRep each form trees. In the language of Object Oriented Programming, we support only single inheritance for representations.

These trees are typically rather shallow, since for one representation to be contained in another implies that all the components of the data structure implied by the containing representation, are present in, and have the same meaning in, the smaller representation (whose data structure presumably contains some additional components).

Objects may change their representation, for example a mutable list of characters can be converted into a string.

All representations in the library are created during initialization, in particular they are not created dynamically at runtime.

Examples of subrepresentations of IsPositionalObjectRep are IsModulusRep, which is used for residue classes in the ring of integers, and IsDenseCoeffVectorRep, which is used for elements of algebras that are defined by structure constants.

An important subrepresentation of IsComponentObjectRep is IsAttributeStoringRep, which is used for many domains and some other objects. It provides automatic storing of all attribute values (see below).

<#Include Label="RepresentationsOfObject">

Attributes

The attributes of an object describe knowledge about it.

An attribute is a unary operation without side-effects.

An object may store values of its attributes once they have been computed, and claim that it knows these values. Note that store and know have to be understood in the sense that it is very cheap to get such a value when the attribute is called again.

The stored value of an attribute is in general immutable (see ), except if the attribute had been specially constructed as mutable attribute.

It depends on the representation of an object (see ) which attribute values it stores. An object in the representation IsAttributeStoringRep stores all attribute values once they are computed. Moreover, for an object in this representation, subsequent calls to an attribute will return the same object; this is achieved via a special method for each attribute setter that stores the attribute value in an object in IsAttributeStoringRep, and a special method for the attribute itself that fetches the stored attribute value. (These methods are called the system setter and the system getter of the attribute, respectively.)system gettersystem setter

Note also that it is impossible to get rid of a stored attribute value because the system may have drawn conclusions from the old attribute value, and just removing the value might leave the data structures in an inconsistent state. If necessary, a new object can be constructed.

Several attributes have methods for more than one argument. For example is an attribute for a G-set that can also be called for the two arguments, being a group G and its action domain. If attributes are called with more than one argument then the return value is not stored in any of the arguments.

Properties are a special form of attributes that have the value true or false, see section .

Examples of attributes for multiplicative elements are , , and . is an attribute for domains, is an attribute for magmas, and is an attribute for groups.

<#Include Label="KnownAttributesOfObject">

Setter and Tester for Attributes
setter
tester
For every attribute, the attribute setter and the attribute tester are defined.

To check whether an object belongs to an attribute attr, the tester of the attribute is used, see . To store a value for the attribute attr in an object, the setter of the attribute is used, see .

For an attribute attr, Tester(attr) is a filter (see ) that returns true or false, depending on whether or not the value of attr for the object is known. For example, Tester( Size )( obj ) is true if the size of the object obj is known.
For an attribute attr, Setter(attr) is called automatically when the attribute value has been computed for the first time. One can also call the setter explicitly, for example, Setter( Size )( obj, val ) sets val as size of the object obj if the size was not yet known.

For each attribute attr that is declared with resp. (see ), tester and setter are automatically made accessible by the names Hasattr and Setattr, respectively. For example, the tester for is called HasSize, and the setter is called SetSize.

gap> g:=Group((1,2,3,4),(1,2));;Size(g); 24 gap> HasSize(g); true gap> SetSize(g,99); gap> Size(g); 24

For two properties prop1 and prop2, the intersection prop1 and prop2 (see ) is again a property for which a setter and a tester exist. Setting the value of this intersection to true for a &GAP; object means to set the values of prop1 and prop2 to true for this object.

gap> prop:= IsFinite and IsCommutative; <Property "<<and-filter>>"> gap> g:= Group( (1,2,3), (4,5) );; gap> Tester( prop )( g ); false gap> Setter( prop )( g, true ); gap> Tester( prop )( g ); prop( g ); true true

It is not allowed to set the value of such an intersection to false for an object.

gap> Setter( prop )( Rationals, false ); You cannot set an "and-filter" except to true not in any function Entering break read-eval-print loop ... you can 'quit;' to quit to outer loop, or you can type 'return true;' to set all components true (but you might really want to reset just one component) to continue brk>
If the value of the attribute attr is already stored for obj, AttributeValueNotSet simply returns this value. Otherwise the value of attr( obj ) is computed and returned without storing it in obj. This can be useful when large attribute values (such as element lists) are needed only once and shall not be stored in the object.

gap> HasAsSSortedList(g); false gap> AttributeValueNotSet(AsSSortedList,g); [ (), (4,5), (1,2,3), (1,2,3)(4,5), (1,3,2), (1,3,2)(4,5) ] gap> HasAsSSortedList(g); false

<#Include Label="[1]{attr}">
<#Include Label="InfoAttributes"> <#Include Label="DisableAttributeValueStoring"> <#Include Label="EnableAttributeValueStoring">

Properties

The properties of an object are those of its attributes (see ) whose values can only be true or false.

The main difference between attributes and properties is that a property defines two sets of objects, namely the usual set of all objects for which the value is known, and the set of all objects for which the value is known to be true.

(Note that it makes no sense to consider a third set, namely the set of objects for which the value of a property is true whether or not it is known, since there may be objects for which the containment in this set cannot be decided.)

For a property prop, the containment of an object obj in the first set is checked again by applying Tester( prop ) to obj, and obj lies in the second set if and only if Tester( prop )( obj ) and prop( obj ) is true.

If a property value is known for an immutable object then this value is also stored, as part of the type of the object. To some extent, property values of mutable objects also can be stored, for example a mutable list all of whose entries are immutable can store whether it is strictly sorted. When the object is mutated (for example by list assignment) the type may need to be adjusted.

Important properties for domains are , , , and , which mean that the multiplication of elements in the domain satisfies ( a * b ) * c = a * ( b * c ), a * b = b * a, a * b = - ( b * a ), a * ( b + c ) = a * b + a * c, and ( a + b ) * c = a * c + b * c, respectively, for all a, b, c in the domain.

<#Include Label="KnownPropertiesOfObject"> <#Include Label="KnownTruePropertiesOfObject">

Other Filters

There are situations where one wants to express a kind of knowledge that is based on some heuristic.

For example, the filters (see ) and are defined in the &GAP; library. Note that such filters do not correspond to a mathematical concept, contrary to properties (see ). Also it need not be defined what easily means for an arbitrary &GAP; object, and in this case one cannot compute the value for an arbitrary &GAP; object. In order to access this kind of knowledge as a part of the type of an object, &GAP; provides filters for which the value is false by default, and it is changed to true in certain situations, either explicitly (for the given object) or via a logical implication (see ) from other filters.

For example, a true value of for a group means that certain methods are applicable that use a pcgs (see ) for the group. There are logical implications to set the filter value to true for permutation groups that are known to be solvable, and for groups that have already a (sufficiently nice) pcgs stored. In the case one has a solvable matrix group and wants to enable methods that use a pcgs, one can set the value to true for this particular group.

A filter filt of the kind described here is different from the other filters introduced in the previous sections. In particular, filt is not a category (see ) or a property (see ) because its value may change for a given object, and filt is not a representation (see ) because it has nothing to do with the way an object is made up from some data. filt is similar to an attribute tester (see ), the only difference is that filt does not refer to an attribute value; note that filt is also used in the same way as an attribute tester; namely, the true value may be required for certain methods to be applicable.

Types

We stated above (see ) that, for an object obj, its type is formed from its family and its filters. There is a also a third component, used in a few situations, namely defining data of the type.

<#Include Label="TypeObj">

The last part of the type, defining data, has not been mentioned before and seems to be of minor importance. It can be used, e.g., for cosets U g of a group U, where the type of each coset may contain the group U as defining data. As a consequence, two such cosets mod U and V can have the same type only if U = V. The defining data of the type type can be accessed via .