  
  [1X36 [33X[0;0YWords[133X[101X
  
  [33X[0;0YThis  chapter  describes  categories  of [13Xwords[113X and [13Xnonassociative words[113X, and
  operations  for  them.  For information about [13Xassociative words[113X, which occur
  for example as elements in free groups, see Chapter [14X37[114X.[133X
  
  
  [1X36.1 [33X[0;0YCategories of Words and Nonassociative Words[133X[101X
  
  [1X36.1-1 IsWord[101X
  
  [33X[1;0Y[29X[2XIsWord[102X( [3Xobj[103X ) [32X Category[133X
  [33X[1;0Y[29X[2XIsWordWithOne[102X( [3Xobj[103X ) [32X Category[133X
  [33X[1;0Y[29X[2XIsWordWithInverse[102X( [3Xobj[103X ) [32X Category[133X
  
  [33X[0;0YGiven a free multiplicative structure [22XM[122X that is freely generated by a subset
  [22XX[122X, any expression of an element in [22XM[122X as an iterated product of elements in [22XX[122X
  is called a [13Xword[113X over [22XX[122X.[133X
  
  [33X[0;0YInteresting  cases  of  free  multiplicative  structures  are  those of free
  semigroups,  free  monoids,  and  free  groups,  where the multiplication is
  associative (see [2XIsAssociative[102X ([14X35.4-7[114X)), which are described in Chapter [14X37[114X,
  and also the case of free magmas, where the multiplication is nonassociative
  (see [2XIsNonassocWord[102X ([14X36.1-3[114X)).[133X
  
  [33X[0;0YElements in free magmas (see [2XFreeMagma[102X ([14X36.4-1[114X)) lie in the category [2XIsWord[102X;
  similarly,  elements in free magmas-with-one (see [2XFreeMagmaWithOne[102X ([14X36.4-2[114X))
  lie in the category [2XIsWordWithOne[102X, and so on.[133X
  
  [33X[0;0Y[2XIsWord[102X  is  mainly a [21Xcommon roof[121X for the two [13Xdisjoint[113X categories [2XIsAssocWord[102X
  ([14X37.1-1[114X)  and  [2XIsNonassocWord[102X  ([14X36.1-3[114X)  of  associative  and nonassociative
  words.  This  means that associative words are [13Xnot[113X 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 [14X36.5[114X and [14X37.7[114X).[133X
  
  [33X[0;0YNote  that  elements  in  finitely  presented  groups  and  also elements in
  polycyclic  groups in [5XGAP[105X are [13Xnot[113X in [2XIsWord[102X although they are usually called
  words, see Chapters [14X47[114X and [14X46[114X.[133X
  
  [33X[0;0YWords  are  [13Xconstants[113X  (see [14X12.6[114X),  that  is,  they are not copyable and not
  mutable.[133X
  
  [33X[0;0YThe  usual  way  to create words is to form them as products of known words,
  starting  from [13Xgenerators[113X of a free structure such as a free magma or a free
  group (see [2XFreeMagma[102X ([14X36.4-1[114X), [2XFreeGroup[102X ([14X37.2-1[114X)).[133X
  
  [33X[0;0YWords  are  also  used  to implement free algebras, in the same way as group
  elements are used to implement group algebras (see [14X62.3[114X and Chapter [14X65[114X).[133X
  
  [4X[32X  Example  [32X[104X
    [4X[25Xgap>[125X [27Xm:= FreeMagmaWithOne( 2 );;  gens:= GeneratorsOfMagmaWithOne( m );[127X[104X
    [4X[28X[ x1, x2 ][128X[104X
    [4X[25Xgap>[125X [27Xw1:= gens[1] * gens[2] * gens[1];[127X[104X
    [4X[28X((x1*x2)*x1)[128X[104X
    [4X[25Xgap>[125X [27Xw2:= gens[1] * ( gens[2] * gens[1] );[127X[104X
    [4X[28X(x1*(x2*x1))[128X[104X
    [4X[25Xgap>[125X [27Xw1 = w2;  IsAssociative( m );[127X[104X
    [4X[28Xfalse[128X[104X
    [4X[28Xfalse[128X[104X
    [4X[25Xgap>[125X [27XIsWord( w1 );  IsAssocWord( w1 );  IsNonassocWord( w1 );[127X[104X
    [4X[28Xtrue[128X[104X
    [4X[28Xfalse[128X[104X
    [4X[28Xtrue[128X[104X
    [4X[25Xgap>[125X [27Xs:= FreeMonoid( 2 );;  gens:= GeneratorsOfMagmaWithOne( s );[127X[104X
    [4X[28X[ m1, m2 ][128X[104X
    [4X[25Xgap>[125X [27Xu1:= ( gens[1] * gens[2] ) * gens[1];[127X[104X
    [4X[28Xm1*m2*m1[128X[104X
    [4X[25Xgap>[125X [27Xu2:= gens[1] * ( gens[2] * gens[1] );[127X[104X
    [4X[28Xm1*m2*m1[128X[104X
    [4X[25Xgap>[125X [27Xu1 = u2;  IsAssociative( s );[127X[104X
    [4X[28Xtrue[128X[104X
    [4X[28Xtrue[128X[104X
    [4X[25Xgap>[125X [27XIsWord( u1 );  IsAssocWord( u1 );  IsNonassocWord( u1 );[127X[104X
    [4X[28Xtrue[128X[104X
    [4X[28Xtrue[128X[104X
    [4X[28Xfalse[128X[104X
    [4X[25Xgap>[125X [27Xa:= (1,2,3);;  b:= (1,2);;[127X[104X
    [4X[25Xgap>[125X [27Xw:= a*b*a;;  IsWord( w );[127X[104X
    [4X[28Xfalse[128X[104X
  [4X[32X[104X
  
  [1X36.1-2 IsWordCollection[101X
  
  [33X[1;0Y[29X[2XIsWordCollection[102X( [3Xobj[103X ) [32X Category[133X
  
  [33X[0;0Y[2XIsWordCollection[102X   is   the  collections  category  (see [2XCategoryCollections[102X
  ([14X30.2-4[114X)) of [2XIsWord[102X ([14X36.1-1[114X).[133X
  
  [4X[32X  Example  [32X[104X
    [4X[25Xgap>[125X [27XIsWordCollection( m );  IsWordCollection( s );[127X[104X
    [4X[28Xtrue[128X[104X
    [4X[28Xtrue[128X[104X
    [4X[25Xgap>[125X [27XIsWordCollection( [ "a", "b" ] );[127X[104X
    [4X[28Xfalse[128X[104X
  [4X[32X[104X
  
  [1X36.1-3 IsNonassocWord[101X
  
  [33X[1;0Y[29X[2XIsNonassocWord[102X( [3Xobj[103X ) [32X Category[133X
  [33X[1;0Y[29X[2XIsNonassocWordWithOne[102X( [3Xobj[103X ) [32X Category[133X
  
  [33X[0;0YA  [13Xnonassociative  word[113X  in  [5XGAP[105X  is  an  element  in a free magma or a free
  magma-with-one (see [14X36.4[114X).[133X
  
  [33X[0;0YThe   default   methods  for  [2XViewObj[102X  ([14X6.3-5[114X)  and  [2XPrintObj[102X  ([14X6.3-5[114X)  show
  nonassociative  words  as  products  of  letters,  where  the  succession of
  multiplications is determined by round brackets.[133X
  
  [33X[0;0YIn this sense each nonassociative word describes a [21Xprogram[121X 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  [2XMappedWord[102X ([14X36.3-1[114X) implements a way to
  apply  such  a  program.  A  more  general  way is provided by straight line
  programs (see [14X37.8[114X).[133X
  
  [33X[0;0YNote  that  associative  words  (see Chapter [14X37[114X) are [13Xnot[113X regarded as special
  cases of nonassociative words (see [2XIsWord[102X ([14X36.1-1[114X)).[133X
  
  [1X36.1-4 IsNonassocWordCollection[101X
  
  [33X[1;0Y[29X[2XIsNonassocWordCollection[102X( [3Xobj[103X ) [32X Category[133X
  [33X[1;0Y[29X[2XIsNonassocWordWithOneCollection[102X( [3Xobj[103X ) [32X Category[133X
  
  [33X[0;0Y[2XIsNonassocWordCollection[102X       is       the       collections       category
  (see [2XCategoryCollections[102X   ([14X30.2-4[114X))   of   [2XIsNonassocWord[102X   ([14X36.1-3[114X),   and
  [2XIsNonassocWordWithOneCollection[102X    is    the    collections    category   of
  [2XIsNonassocWordWithOne[102X ([14X36.1-3[114X).[133X
  
  
  [1X36.2 [33X[0;0YComparison of Words[133X[101X
  
  [1X36.2-1 \=[101X
  
  [33X[1;0Y[29X[2X\=[102X( [3Xw1[103X, [3Xw2[103X ) [32X operation[133X
  
  [33X[0;0YTwo words are equal if and only if they are words over the same alphabet and
  with  equal external representations (see [14X36.5[114X and [14X37.7[114X). For nonassociative
  words,  the  latter  means  that  the  words  arise  from the letters of the
  alphabet by the same sequence of multiplications.[133X
  
  [1X36.2-2 \<[101X
  
  [33X[1;0Y[29X[2X\<[102X( [3Xw1[103X, [3Xw2[103X ) [32X operation[133X
  
  [33X[0;0YWords   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 [14X36.5[114X; associative words are
  ordered by the shortlex ordering via [10X<[110X (see [14X37.7[114X).[133X
  
  [33X[0;0YNote that the alphabet of a word is determined by its family (see [14X13.1[114X), and
  that  the result of each call to [2XFreeMagma[102X ([14X36.4-1[114X), [2XFreeGroup[102X ([14X37.2-1[114X) etc.
  consists  of words over a new alphabet. In particular, there is no [21Xuniversal[121X
  empty  word,  every  families of words in [2XIsWordWithOne[102X ([14X36.1-1[114X) has its own
  empty word.[133X
  
  [4X[32X  Example  [32X[104X
    [4X[25Xgap>[125X [27Xm:= FreeMagma( "a", "b" );;[127X[104X
    [4X[25Xgap>[125X [27Xx:= FreeMagma( "a", "b" );;[127X[104X
    [4X[25Xgap>[125X [27Xmgens:= GeneratorsOfMagma( m );[127X[104X
    [4X[28X[ a, b ][128X[104X
    [4X[25Xgap>[125X [27Xxgens:= GeneratorsOfMagma( x );[127X[104X
    [4X[28X[ a, b ][128X[104X
    [4X[25Xgap>[125X [27Xa:= mgens[1];;  b:= mgens[2];;[127X[104X
    [4X[25Xgap>[125X [27Xa = xgens[1];[127X[104X
    [4X[28Xfalse[128X[104X
    [4X[25Xgap>[125X [27Xa*(a*a) = (a*a)*a;  a*b = b*a;  a*a = a*a;[127X[104X
    [4X[28Xfalse[128X[104X
    [4X[28Xfalse[128X[104X
    [4X[28Xtrue[128X[104X
    [4X[25Xgap>[125X [27Xa < b;  b < a;  a < a*b;[127X[104X
    [4X[28Xtrue[128X[104X
    [4X[28Xfalse[128X[104X
    [4X[28Xtrue[128X[104X
  [4X[32X[104X
  
  
  [1X36.3 [33X[0;0YOperations for Words[133X[101X
  
  [33X[0;0YTwo  words  can  be  multiplied  via  [10X*[110X only if they are words over the same
  alphabet (see [14X36.2[114X).[133X
  
  [1X36.3-1 MappedWord[101X
  
  [33X[1;0Y[29X[2XMappedWord[102X( [3Xw[103X, [3Xgens[103X, [3Ximgs[103X ) [32X operation[133X
  
  [33X[0;0Y[2XMappedWord[102X  returns the object that is obtained by replacing each occurrence
  in the word [3Xw[103X of a generator in the list [3Xgens[103X by the corresponding object in
  the list [3Ximgs[103X. The lists [3Xgens[103X and [3Ximgs[103X must of course have the same length.[133X
  
  [33X[0;0Y[2XMappedWord[102X  needs to do some preprocessing to get internal generator numbers
  etc.  When  mapping many (several thousand) words, an explicit loop over the
  words syllables might be faster.[133X
  
  [33X[0;0YFor  example,  if  the  elements  in  [3Ximgs[103X  are  all  [13Xassociative words[113X (see
  Chapter [14X37[114X) in the same family as the elements in [3Xgens[103X, and some of them are
  equal  to  the  corresponding  generators in [3Xgens[103X, then those may be omitted
  from  [3Xgens[103X and [3Ximgs[103X. In this situation, the special case that the lists [3Xgens[103X
  and  [3Ximgs[103X  have  only length [22X1[122X is handled more efficiently by [2XEliminatedWord[102X
  ([14X37.4-6[114X).[133X
  
  [4X[32X  Example  [32X[104X
    [4X[25Xgap>[125X [27Xm:= FreeMagma( "a", "b" );;  gens:= GeneratorsOfMagma( m );;[127X[104X
    [4X[25Xgap>[125X [27Xa:= gens[1];  b:= gens[2];[127X[104X
    [4X[28Xa[128X[104X
    [4X[28Xb[128X[104X
    [4X[25Xgap>[125X [27Xw:= (a*b)*((b*a)*a)*b;[127X[104X
    [4X[28X(((a*b)*((b*a)*a))*b)[128X[104X
    [4X[25Xgap>[125X [27XMappedWord( w, gens, [ (1,2), (1,2,3,4) ] );[127X[104X
    [4X[28X(2,4,3)[128X[104X
    [4X[25Xgap>[125X [27Xa:= (1,2);; b:= (1,2,3,4);;  (a*b)*((b*a)*a)*b;[127X[104X
    [4X[28X(2,4,3)[128X[104X
    [4X[25Xgap>[125X [27Xf:= FreeGroup( "a", "b" );;[127X[104X
    [4X[25Xgap>[125X [27Xa:= GeneratorsOfGroup(f)[1];;  b:= GeneratorsOfGroup(f)[2];;[127X[104X
    [4X[25Xgap>[125X [27Xw:= a^5*b*a^2/b^4*a;[127X[104X
    [4X[28Xa^5*b*a^2*b^-4*a[128X[104X
    [4X[25Xgap>[125X [27XMappedWord( w, [ a, b ], [ (1,2), (1,2,3,4) ] );[127X[104X
    [4X[28X(1,3,4,2)[128X[104X
    [4X[25Xgap>[125X [27X(1,2)^5*(1,2,3,4)*(1,2)^2/(1,2,3,4)^4*(1,2);[127X[104X
    [4X[28X(1,3,4,2)[128X[104X
    [4X[25Xgap>[125X [27XMappedWord( w, [ a ], [ a^2 ] );[127X[104X
    [4X[28Xa^10*b*a^4*b^-4*a^2[128X[104X
  [4X[32X[104X
  
  
  [1X36.4 [33X[0;0YFree Magmas[133X[101X
  
  [33X[0;0YThe  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 [2XGeneratorsOfMagma[102X ([14X35.4-1[114X) in the case
  of  a free magma, and via [2XGeneratorsOfMagmaWithOne[102X ([14X35.4-2[114X) in the case of a
  free magma-with-one.[133X
  
  
  [1X36.4-1 [33X[0;0YFreeMagma[133X[101X
  
  [33X[1;0Y[29X[2XFreeMagma[102X( [3Xrank[103X[, [3Xname[103X] ) [32X function[133X
  [33X[1;0Y[29X[2XFreeMagma[102X( [3Xname1[103X, [3Xname2[103X, [3X...[103X ) [32X function[133X
  [33X[1;0Y[29X[2XFreeMagma[102X( [3Xnames[103X ) [32X function[133X
  [33X[1;0Y[29X[2XFreeMagma[102X( [3Xinfinity[103X, [3Xname[103X, [3Xinit[103X ) [32X function[133X
  
  [33X[0;0YCalled  with a positive integer [3Xrank[103X, [2XFreeMagma[102X returns a free magma on [3Xrank[103X
  generators.  If  the optional argument [3Xname[103X is given then the generators are
  printed as [3Xname[103X[10X1[110X, [3Xname[103X[10X2[110X etc., that is, each name is the concatenation of the
  string  [3Xname[103X  and  an  integer  from [10X1[110X to [3Xrange[103X. The default for [3Xname[103X is the
  string [10X"m"[110X.[133X
  
  [33X[0;0YCalled  in  the  second  form,  [2XFreeMagma[102X  returns  a  free magma on as many
  generators as arguments, printed as [3Xname1[103X, [3Xname2[103X etc.[133X
  
  [33X[0;0YCalled  in  the  third  form,  [2XFreeMagma[102X  returns  a  free  magma on as many
  generators as the length of the list [3Xnames[103X, the [22Xi[122X-th generator being printed
  as [3Xnames[103X[10X[[110X[22Xi[122X[10X][110X.[133X
  
  [33X[0;0YCalled in the fourth form, [2XFreeMagma[102X returns a free magma on infinitely many
  generators,  where the first generators are printed by the names in the list
  [3Xinit[103X, and the other generators by [3Xname[103X and an appended number.[133X
  
  
  [1X36.4-2 [33X[0;0YFreeMagmaWithOne[133X[101X
  
  [33X[1;0Y[29X[2XFreeMagmaWithOne[102X( [3Xrank[103X[, [3Xname[103X] ) [32X function[133X
  [33X[1;0Y[29X[2XFreeMagmaWithOne[102X( [3Xname1[103X, [3Xname2[103X, [3X...[103X ) [32X function[133X
  [33X[1;0Y[29X[2XFreeMagmaWithOne[102X( [3Xnames[103X ) [32X function[133X
  [33X[1;0Y[29X[2XFreeMagmaWithOne[102X( [3Xinfinity[103X, [3Xname[103X, [3Xinit[103X ) [32X function[133X
  
  [33X[0;0YCalled  with  a  positive  integer  [3Xrank[103X,  [2XFreeMagmaWithOne[102X  returns  a free
  magma-with-one  on  [3Xrank[103X  generators. If the optional argument [3Xname[103X is given
  then  the generators are printed as [3Xname[103X[10X1[110X, [3Xname[103X[10X2[110X etc., that is, each name is
  the  concatenation  of  the  string [3Xname[103X and an integer from [10X1[110X to [3Xrange[103X. The
  default for [3Xname[103X is the string [10X"m"[110X.[133X
  
  [33X[0;0YCalled in the second form, [2XFreeMagmaWithOne[102X returns a free magma-with-one on
  as many generators as arguments, printed as [3Xname1[103X, [3Xname2[103X etc.[133X
  
  [33X[0;0YCalled  in the third form, [2XFreeMagmaWithOne[102X returns a free magma-with-one on
  as many generators as the length of the list [3Xnames[103X, the [22Xi[122X-th generator being
  printed as [3Xnames[103X[10X[[110X[22Xi[122X[10X][110X.[133X
  
  [33X[0;0YCalled in the fourth form, [2XFreeMagmaWithOne[102X returns a free magma-with-one on
  infinitely  many  generators,  where the first generators are printed by the
  names  in  the  list  [3Xinit[103X, and the other generators by [3Xname[103X and an appended
  number.[133X
  
  [4X[32X  Example  [32X[104X
    [4X[25Xgap>[125X [27XFreeMagma( 3 );[127X[104X
    [4X[28X<free magma on the generators [ x1, x2, x3 ]>[128X[104X
    [4X[25Xgap>[125X [27XFreeMagma( "a", "b" );[127X[104X
    [4X[28X<free magma on the generators [ a, b ]>[128X[104X
    [4X[25Xgap>[125X [27XFreeMagma( infinity );[127X[104X
    [4X[28X<free magma with infinity generators>[128X[104X
    [4X[25Xgap>[125X [27XFreeMagmaWithOne( 3 );[127X[104X
    [4X[28X<free magma-with-one on the generators [ x1, x2, x3 ]>[128X[104X
    [4X[25Xgap>[125X [27XFreeMagmaWithOne( "a", "b" );[127X[104X
    [4X[28X<free magma-with-one on the generators [ a, b ]>[128X[104X
    [4X[25Xgap>[125X [27XFreeMagmaWithOne( infinity );[127X[104X
    [4X[28X<free magma-with-one with infinity generators>[128X[104X
  [4X[32X[104X
  
  [33X[0;0YRemember  that  the names of generators used for printing do not necessarily
  distinguish letters of the alphabet; so it is possible to create arbitrarily
  weird situations by choosing strange letter names.[133X
  
  [4X[32X  Example  [32X[104X
    [4X[25Xgap>[125X [27Xm:= FreeMagma( "x", "x" );  gens:= GeneratorsOfMagma( m );;[127X[104X
    [4X[28X<free magma on the generators [ x, x ]>[128X[104X
    [4X[25Xgap>[125X [27Xgens[1] = gens[2];[127X[104X
    [4X[28Xfalse[128X[104X
  [4X[32X[104X
  
  
  [1X36.5 [33X[0;0YExternal Representation for Nonassociative Words[133X[101X
  
  [33X[0;0YThe  external  representation of nonassociative words is defined as follows.
  The  [22Xi[122X-th  generator  of  the  family  of  elements in question has external
  representation  [22Xi[122X,  the  identity (if exists) has external representation [22X0[122X,
  the  inverse  of  the [22Xi[122X-th generator (if exists) has external representation
  [22X-i[122X.  If  [22Xv[122X  and [22Xw[122X are nonassociative words with external representations [22Xe_v[122X
  and  [22Xe_w[122X,  respectively then the product [22Xv * w[122X has external representation [22X[
  e_v,  e_w  ][122X.  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.[133X
  
  [33X[0;0YOne can create a nonassociative word from a family of words and the external
  representation of a nonassociative word using [2XObjByExtRep[102X ([14X79.16-1[114X).[133X
  
  [4X[32X  Example  [32X[104X
    [4X[25Xgap>[125X [27Xm:= FreeMagma( 2 );;  gens:= GeneratorsOfMagma( m );[127X[104X
    [4X[28X[ x1, x2 ][128X[104X
    [4X[25Xgap>[125X [27Xw:= ( gens[1] * gens[2] ) * gens[1];[127X[104X
    [4X[28X((x1*x2)*x1)[128X[104X
    [4X[25Xgap>[125X [27XExtRepOfObj( w );  ExtRepOfObj( gens[1] );[127X[104X
    [4X[28X[ [ 1, 2 ], 1 ][128X[104X
    [4X[28X1[128X[104X
    [4X[25Xgap>[125X [27X ExtRepOfObj( w*w );[127X[104X
    [4X[28X[ [ [ 1, 2 ], 1 ], [ [ 1, 2 ], 1 ] ][128X[104X
    [4X[25Xgap>[125X [27XObjByExtRep( FamilyObj( w ), 2 );[127X[104X
    [4X[28Xx2[128X[104X
    [4X[25Xgap>[125X [27XObjByExtRep( FamilyObj( w ), [ 1, [ 2, 1 ] ] );[127X[104X
    [4X[28X(x1*(x2*x1))[128X[104X
  [4X[32X[104X
  
