# GAP's Type System I

## by Markus Pfeiffer

The following posts are somewhat inspired by my talk at the CoDiMa training school in Edinburgh. The Jupyter notebooks that I used are available as ipynbipynb htmlhtml pdfpdf

I figured that the talk was very dense, so I will expand it a bit more into multiple blog posts.

My current plan is to describe the technicalities of the GAP type system, but also do a reasonably self-contained post that describes a practical approach to making the best use of GAP’s type system.

Please note that I will usually be using the latest `master`

branch of GAP, or
even patches that I came up with while writing these posts. Usually while
writing I notice that GAP’s output is not helpful, and so I change it. If you
have a suggestion or question, send me an email!

Lets get started.

## GAP4’s Types, Families, and Filters

With every programming language one learns one of the things one has to learn are some internals that help explaining why a program behaves the way it does.

One of those things is the Type System that the language of choice implements, and for GAP the type system is quite peculiar.

Every object in GAP has a *type*. If you’re wondering what is and what
isn’t an object, you can test this by trying to assign it to a variable:

1 2 3 x := 5; y := Group((1,2,3)); z := if;

In GAP 4 a type is a *pair* consisting of a *family* and a *filter*,
and the first thing to keep in mind is that

- Families partition the set of all objects,
- Filters form a hierarchy on objects

If we want to find the type of an object in GAP, we can use the function `TypeObj`

1 2 3 4 5 6 gap> TypeObj(1); <Type: (CyclotomicsFamily, [ IsInt, IsRat, IsCyc, ... ])> gap> TypeObj([1,2,3]); <Type: (CollectionsFamily(...), [ IsMutable, IsCopyable, IsList, ... ])> gap> TypeObj(Group((1,2,3))); <Type: (CollectionsFamily(...), [ IsComponentObjectRep, IsAttributeStoringRep, IsListOrCollection, ... ])>

There is also a way to get more verbose information about a type:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 gap> Display(TypeObj(1)); family: CollectionsFamily(...) filters: IsComponentObjectRep IsAttributeStoringRep IsListOrCollection IsCollection IsFinite Tester(IsFinite) CanEasilyCompareElements Tester(CanEasilyCompareElements) CanEasilySortElements Tester(CanEasilySortElements) CanComputeSize IsDuplicateFree Tester(IsDuplicateFree) IsExtLElement CategoryCollections(IsExtLElement) IsExtRElement CategoryCollections(IsExtRElement) CategoryCollections(IsMultiplicativeElement) CategoryCollections(IsMultiplicativeElementWithOne) CategoryCollections(IsMultiplicativeElementWithInverse) IsOddAdditiveNestingDepthObject CategoryCollections(IsAssociativeElement) CategoryCollections(IsFiniteOrderElement) IsGeneralizedDomain CategoryCollections(IsPerm) IsMagma IsMagmaWithOne IsMagmaWithInversesIfNonzero IsMagmaWithInverses Tester(GeneratorsOfMagmaWithInverses) IsGeneratorsOfMagmaWithInverses Tester(IsGeneratorsOfMagmaWithInverses) IsAssociative Tester(IsAssociative) IsCommutative Tester(IsCommutative) Tester(MultiplicativeNeutralElement) IsGeneratorsOfSemigroup Tester(IsGeneratorsOfSemigroup) IsSimpleSemigroup Tester(IsSimpleSemigroup) IsRegularSemigroup Tester(IsRegularSemigroup) IsInverseSemigroup Tester(IsInverseSemigroup) IsCompletelyRegularSemigroup Tester(IsCompletelyRegularSemigroup) IsCompletelySimpleSemigroup Tester(IsCompletelySimpleSemigroup) IsGroupAsSemigroup Tester(IsGroupAsSemigroup) IsMonoidAsSemigroup Tester(IsMonoidAsSemigroup) IsOrthodoxSemigroup Tester(IsOrthodoxSemigroup) IsCyclic Tester(IsCyclic) IsFinitelyGeneratedGroup Tester(IsFinitelyGeneratedGroup) IsSubsetLocallyFiniteGroup Tester(IsSubsetLocallyFiniteGroup) CanEasilyTestMembership CanEasilyComputeWithIndependentGensAbelianGroup CanComputeSizeAnySubgroup KnowsHowToDecompose Tester(KnowsHowToDecompose) IsNilpotentGroup Tester(IsNilpotentGroup) IsSupersolvableGroup Tester(IsSupersolvableGroup) IsMonomialGroup Tester(IsMonomialGroup) IsSolvableGroup Tester(IsSolvableGroup) IsPolycyclicGroup Tester(IsPolycyclicGroup) CanEasilyComputePcgs CanComputeFittingFree IsNilpotentByFinite Tester(IsNilpotentByFinite)

That output is quite daunting, but it reflects the current knowledge that GAP has about the object.

## Families

The idea behind families in GAP is to describe relationships between objects, and which operations can be applied between them. For example it makes little sense to try and multiply an integer by a permutation.

Families are created at runtime. For example when we
create a finitely presented group, GAP creates a family
of elements for *that specific group*.

1 2 3 4 5 6 7 8 9 10 11 gap> F := FreeGroup(2);; gap> G := F / [ F.1^2, F.2^2 ]; <fp group on the generators [ f1, f2 ]> gap> FamilyObj(Reprensentative(G)); <Family: "FamilyElementsFpGroup"> gap> H := F / [ F.1^3, F.2 * F.1, F.1^5 ]; <fp group on the generators [ f1, f2 ]> gap> FamilyObj(Representative(H)); <Family: "FamilyElementsFpGroup"> gap> FamilyObj(Representative(G)) = FamilyObj(Representative(H)); false

## Filters

We’ll get back to families later, for now the more interesting bit is the filter.

A filter is

- an elementary filter, or
- a conjunction of elementary filters

All elementary filters in a GAP session are identified by a unique integer, and a filter is thus represented as a (bit-)list of elementary filters.

Filters are special unary predicates on all objects. By convention most filters’ names
begin with `Is`

, with the exception of some filters whose name starts with `Has`

.

The list of filters that return `true`

for any given object in a GAP
session can actually expand. This is to say that in GAP objects can
*change their type* during their lifetime.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 gap> IsObject(1); true gap> IsObject((1,2,3)); true gap> IsInt((1,2,3)); false gap> IsInt; <Category "IsInt"> gap> G := Group((1,2), (3,4));; gap> IsMagma(G); true; gap> f := IsMagma and IsRing; <Filter "(IsMagma and (((IsNearAdditiveGroup and (IsNearAdditiveMagma and IsAdditivelyCommutative)) and IsMagma) and (IsLDistributive and IsRDistributive)))"> gap> f(G); false gap> PositionSublist(DisplayString(TypeObj(G)), "IsCommutative"); fail gap> IsCommutative(G); true gap> PositionSublist(DisplayString(TypeObj(G)), "IsCommutative"); 1084

The above code does cheat a little bit. I leave it as an exercise to find out why.

## Next time

Filters can roughly be classified as *Categories*, *Representations*, *Properties* and *Others*.
In the next post we will first find out about *Categories* and *Representations*.
These filters are set when an object is created, and don’t change during its lifetime.
This is in contrast with properties and other filters which can change.