36 Words This chapter describes categories of words and nonassociative words, and operations for them. For information about associative words, which occur for example as elements in free groups, see ChapterĀ 37. 36.1 Categories of Words and Nonassociative Words 36.1-1 IsWord IsWord( obj )  Category IsWordWithOne( obj )  Category IsWordWithInverse( obj )  Category Given a free multiplicative structure M that is freely generated by a subset X, any expression of an element in M as an iterated product of elements in X is called a word over X. Interesting cases of free multiplicative structures are those of free semigroups, free monoids, and free groups, where the multiplication is associative (seeĀ IsAssociative (35.4-7)), which are described in ChapterĀ 37, and also the case of free magmas, where the multiplication is nonassociative (seeĀ IsNonassocWord (36.1-3)). Elements in free magmas (seeĀ FreeMagma (36.4-1)) lie in the category IsWord; similarly, elements in free magmas-with-one (seeĀ FreeMagmaWithOne (36.4-2)) lie in the category IsWordWithOne, and so on. IsWord is mainly a common roof for the two disjoint categories IsAssocWord (37.1-1) and IsNonassocWord (36.1-3) of associative and nonassociative words. This means that associative words are not regarded as special cases of nonassociative words. The main reason for this setup is that we are interested in different external representations for associative and nonassociative words (seeĀ 36.5 and 37.7). Note that elements in finitely presented groups and also elements in polycyclic groups in GAP are not in IsWord although they are usually called words, see ChaptersĀ 47 andĀ 46. Words are constants (seeĀ 12.6), that is, they are not copyable and not mutable. The usual way to create words is to form them as products of known words, starting from generators of a free structure such as a free magma or a free group (seeĀ FreeMagma (36.4-1), FreeGroup (37.2-1)). Words are also used to implement free algebras, in the same way as group elements are used to implement group algebras (seeĀ 62.3 and ChapterĀ 65).  Example  gap> m:= FreeMagmaWithOne( 2 );; gens:= GeneratorsOfMagmaWithOne( m ); [ x1, x2 ] gap> w1:= gens[1] * gens[2] * gens[1]; ((x1*x2)*x1) gap> w2:= gens[1] * ( gens[2] * gens[1] ); (x1*(x2*x1)) gap> w1 = w2; IsAssociative( m ); false false gap> IsWord( w1 ); IsAssocWord( w1 ); IsNonassocWord( w1 ); true false true gap> s:= FreeMonoid( 2 );; gens:= GeneratorsOfMagmaWithOne( s ); [ m1, m2 ] gap> u1:= ( gens[1] * gens[2] ) * gens[1]; m1*m2*m1 gap> u2:= gens[1] * ( gens[2] * gens[1] ); m1*m2*m1 gap> u1 = u2; IsAssociative( s ); true true gap> IsWord( u1 ); IsAssocWord( u1 ); IsNonassocWord( u1 ); true true false gap> a:= (1,2,3);; b:= (1,2);; gap> w:= a*b*a;; IsWord( w ); false  36.1-2 IsWordCollection IsWordCollection( obj )  Category IsWordCollection is the collections category (seeĀ CategoryCollections (30.2-4)) of IsWord (36.1-1).  Example  gap> IsWordCollection( m ); IsWordCollection( s ); true true gap> IsWordCollection( [ "a", "b" ] ); false  36.1-3 IsNonassocWord IsNonassocWord( obj )  Category IsNonassocWordWithOne( obj )  Category A nonassociative word in GAP is an element in a free magma or a free magma-with-one (seeĀ 36.4). The default methods for ViewObj (6.3-5) and PrintObj (6.3-5) show nonassociative words as products of letters, where the succession of multiplications is determined by round brackets. In this sense each nonassociative word describes a program to form a product of generators. (Also associative words can be interpreted as such programs, except that the exact succession of multiplications is not prescribed due to the associativity.) The function MappedWord (36.3-1) implements a way to apply such a program. A more general way is provided by straight line programs (seeĀ 37.8). Note that associative words (see ChapterĀ 37) are not regarded as special cases of nonassociative words (seeĀ IsWord (36.1-1)). 36.1-4 IsNonassocWordCollection IsNonassocWordCollection( obj )  Category IsNonassocWordWithOneCollection( obj )  Category IsNonassocWordCollection is the collections category (seeĀ CategoryCollections (30.2-4)) of IsNonassocWord (36.1-3), and IsNonassocWordWithOneCollection is the collections category of IsNonassocWordWithOne (36.1-3). 36.2 Comparison of Words 36.2-1 \= \=( w1, w2 )  operation Two words are equal if and only if they are words over the same alphabet and with equal external representations (seeĀ 36.5 and 37.7). For nonassociative words, the latter means that the words arise from the letters of the alphabet by the same sequence of multiplications. 36.2-2 \< \<( w1, w2 )  operation Words are ordered according to their external representation. More precisely, two words can be compared if they are words over the same alphabet, and the word with smaller external representation is smaller. For nonassociative words, the ordering is defined inĀ 36.5; associative words are ordered by the shortlex ordering via < (seeĀ 37.7). Note that the alphabet of a word is determined by its family (seeĀ 13.1), and that the result of each call to FreeMagma (36.4-1), FreeGroup (37.2-1) etc. consists of words over a new alphabet. In particular, there is no universal empty word, every families of words in IsWordWithOne (36.1-1) has its own empty word.  Example  gap> m:= FreeMagma( "a", "b" );; gap> x:= FreeMagma( "a", "b" );; gap> mgens:= GeneratorsOfMagma( m ); [ a, b ] gap> xgens:= GeneratorsOfMagma( x ); [ a, b ] gap> a:= mgens[1];; b:= mgens[2];; gap> a = xgens[1]; false gap> a*(a*a) = (a*a)*a; a*b = b*a; a*a = a*a; false false true gap> a < b; b < a; a < a*b; true false true  36.3 Operations for Words Two words can be multiplied via * only if they are words over the same alphabet (seeĀ 36.2). 36.3-1 MappedWord MappedWord( w, gens, imgs )  operation MappedWord returns the object that is obtained by replacing each occurrence in the word w of a generator in the list gens by the corresponding object in the list imgs. The lists gens and imgs must of course have the same length. MappedWord needs to do some preprocessing to get internal generator numbers etc. When mapping many (several thousand) words, a dedicated loop might be faster. For example, if the elements in imgs are all associative words (see ChapterĀ 37) in the same family as the elements in gens, and some of them are equal to the corresponding generators in gens, then those may be omitted from gens and imgs. In this situation, the special case that the lists gens and imgs have only length 1 is handled more efficiently by EliminatedWord (37.4-6). If the word is from a free group, it is permitted to give inverses of (some) of the generators as extra generators. This can speed up the execution by removing the need to calculate inverses anew.  Example  gap> m:= FreeMagma( "a", "b" );; gens:= GeneratorsOfMagma( m );; gap> a:= gens[1]; b:= gens[2]; a b gap> w:= (a*b)*((b*a)*a)*b; (((a*b)*((b*a)*a))*b) gap> MappedWord( w, gens, [ (1,2), (1,2,3,4) ] ); (2,4,3) gap> a:= (1,2);; b:= (1,2,3,4);; (a*b)*((b*a)*a)*b; (2,4,3) gap> f:= FreeGroup( "a", "b" );; gap> a:= GeneratorsOfGroup(f)[1];; b:= GeneratorsOfGroup(f)[2];; gap> w:= a^5*b*a^2/b^4*a; a^5*b*a^2*b^-4*a gap> MappedWord( w, [ a, b ], [ (1,2), (1,2,3,4) ] ); (1,3,4,2) gap> MappedWord( w, [ a, b, b^-1 ], [ (1,2), (1,2,3,4), (1,4,3,2) ] ); (1,3,4,2) gap> (1,2)^5*(1,2,3,4)*(1,2)^2/(1,2,3,4)^4*(1,2); (1,3,4,2) gap> MappedWord( w, [ a ], [ a^2 ] ); a^10*b*a^4*b^-4*a^2  36.4 Free Magmas The easiest way to create a family of words is to construct the free object generated by these words. Each such free object defines a unique alphabet, and its generators are simply the words of length one over this alphabet; These generators can be accessed via GeneratorsOfMagma (35.4-1) in the case of a free magma, and via GeneratorsOfMagmaWithOne (35.4-2) in the case of a free magma-with-one. 36.4-1 FreeMagma FreeMagma( rank[, name] )  function FreeMagma( name1[, name2[, ...]] )  function FreeMagma( names )  function FreeMagma( infinity[, name][, init] )  function FreeMagma returns a free magma. The number of generators, and the labels given to the generators, can be specified in several different ways. Warning: the labels of generators are only an aid for printing, and do not necessarily distinguish generators; see the examples at the end of FreeSemigroup (51.1-10) for more information.  1: For a given rank, and an optional generator name prefix  Called with a positive integer rank, FreeMagma returns a free magma on rank generators. The optional argument name must be a string; its default value is "x". If name is not given but the generatorNames option is, then this option is respected as described in SectionĀ 50.1-16. Otherwise, the generators of the returned free magma are labelled name1, ..., namek, where k is the value of rank. 2: For given generator names Called with various (at least one) nonempty strings, FreeMagma returns a free magma on as many generators as arguments, which are labelled name1, name2, etc. 3: For a given list of generator names Called with a finite nonempty list names of nonempty strings, FreeMagma returns a free magma on Length(names) generators, whose i-th generator is labelled names[i].  4: For the rank infinity, an optional default generator name prefix, and an optional finite list of generator names  Called in the fourth form, FreeMagma returns a free magma on infinitely many generators. The optional argument name must be a string; its default value is "x", and the optional argument init must be a finite list of nonempty strings; its default value is an empty list. The generators are initially labelled according to the list init, followed by namei for each i in the range from Length(init)+1 to infinity.  Example  gap> FreeMagma( 4 );  gap> FreeMagma( 3, "a" );  gap> FreeMagma( "a", "b" );  gap> FreeMagma( [ "a", "b" ] );  gap> FreeMagma( infinity );  gap> F := FreeMagma( infinity, "gen" );; gap> GeneratorsOfMagma( F ){[ 1 .. 4 ]}; [ gen1, gen2, gen3, gen4 ] gap> F := FreeMagma( infinity, [ "z", "a" ] );; gap> GeneratorsOfMagma( F ){[ 1 .. 3 ]}; [ z, a, x3 ] gap> F := FreeMagma( infinity, "y", [ "z", "a" ] );; gap> GeneratorsOfMagma( F ){[ 1 .. 4 ]}; [ z, a, y3, y4 ] gap> FreeMagma( 3 : generatorNames := "elt" );  gap> FreeMagma( 2 : generatorNames := [ "u", "v", "w" ] );   36.4-2 FreeMagmaWithOne FreeMagmaWithOne( rank[, name] )  function FreeMagmaWithOne( [name1[, name2[, ...]]] )  function FreeMagmaWithOne( names )  function FreeMagmaWithOne( infinity[, name][, init] )  function FreeMagmaWithOne returns a free magma-with-one. The number of generators, and the labels given to the generators, can be specified in several different ways. Warning: the labels of generators are only an aid for printing, and do not necessarily distinguish generators; see the examples at the end of FreeSemigroup (51.1-10) for more information.  1: For a given rank, and an optional generator name prefix  Called with a nonnegative integer rank, FreeMagmaWithOne returns a free magma-with-one on rank generators. The optional argument name must be a string; its default value is "x". If name is not given but the generatorNames option is, then this option is respected as described in SectionĀ 50.1-16. Otherwise, the generators of the returned free magma-with-one are labelled name1, ..., namek, where k is the value of rank. 2: For given generator names Called with various nonempty strings, FreeMagmaWithOne returns a free magma-with-one on as many generators as arguments, which are labelled name1, name2, etc. 3: For a given list of generator names Called with a finite list names of nonempty strings, FreeMagmaWithOne returns a free magma-with-one on Length(names) generators, whose i-th generator is labelled names[i].  4: For the rank infinity, an optional default generator name prefix, and an optional finite list of generator names  Called in the fourth form, FreeMagmaWithOne returns a free magma-with-one on infinitely many generators. The optional argument name must be a string; its default value is "x", and the optional argument init must be a finite list of nonempty strings; its default value is an empty list. The generators are initially labelled according to the list init, followed by namei for each i in the range from Length(init)+1 to infinity.  Example  gap> FreeMagmaWithOne( 4 );  gap> FreeMagmaWithOne( 3, "a" );  gap> FreeMagmaWithOne( "a", "b" );  gap> FreeMagmaWithOne( [ "a", "b" ] );  gap> FreeMagmaWithOne( infinity );  gap> F := FreeMagmaWithOne( infinity, "gen" );; gap> GeneratorsOfMagmaWithOne( F ){[ 1 .. 4 ]}; [ gen1, gen2, gen3, gen4 ] gap> F := FreeMagmaWithOne( infinity, [ "z", "a" ] );; gap> GeneratorsOfMagmaWithOne( F ){[ 1 .. 3 ]}; [ z, a, x3 ] gap> F := FreeMagmaWithOne( infinity, "y", [ "z", "a" ] );; gap> GeneratorsOfMagmaWithOne( F ){[ 1 .. 4 ]}; [ z, a, y3, y4 ] gap> FreeMagmaWithOne( 0 );  gap> FreeMagmaWithOne( 3 : generatorNames := "elt" );  gap> FreeMagmaWithOne( 2 : generatorNames := [ "u", "v", "w" ] );   36.5 External Representation for Nonassociative Words The external representation of nonassociative words is defined as follows. The i-th generator of the family of elements in question has external representation i, the identity (if exists) has external representation 0, the inverse of the i-th generator (if exists) has external representation -i. If v and w are nonassociative words with external representations e_v and e_w, respectively then the product v * w has external representation [ e_v, e_w ]. So the external representation of any nonassociative word is either an integer or a nested list of integers and lists, where each list has length two. One can create a nonassociative word from a family of words and the external representation of a nonassociative word using ObjByExtRep (79.8-1).  Example  gap> m:= FreeMagma( 2 );; gens:= GeneratorsOfMagma( m ); [ x1, x2 ] gap> w:= ( gens[1] * gens[2] ) * gens[1]; ((x1*x2)*x1) gap> ExtRepOfObj( w ); ExtRepOfObj( gens[1] ); [ [ 1, 2 ], 1 ] 1 gap>  ExtRepOfObj( w*w ); [ [ [ 1, 2 ], 1 ], [ [ 1, 2 ], 1 ] ] gap> ObjByExtRep( FamilyObj( w ), 2 ); x2 gap> ObjByExtRep( FamilyObj( w ), [ 1, [ 2, 1 ] ] ); (x1*(x2*x1))