  
  [1X2 Rewriting Systems[0X
  
  This chapter describes functions to construct rewriting systems for finitely
  presented groups which store rewriting information. The main example used is
  a  presentation  of the quaternion group [10Xq8[0m with generators a,b and relators
  [a^4, b^4, abab^-1, a^2b^2].
  
  
  [1X2.1 Identity Y-sequences[0X
  
  A  typical input for [5XIdRel[0m is an fp-group presentation. This requires a free
  group  [10XF[0m  on  a set of generators and a set of relators [10XR[0m (words in the free
  group). The module of identities among relators for this presentation has as
  its  elements  the Peiffer equivalence classes of all products of conjugates
  of relators which represent the identity in the free group.
  
  In   this   package   the  identities  among  relators  are  represented  by
  Y-sequences,  which  are  lists [[r_1, u_1],...,[r_k,u_k]] where r_1,...,r_k
  are  the  group relators or their inverses, and u_1,...,u_k are words in the
  free   group   [10XF[0m.   A   Y-sequence   is   evaluated  in  [10XF[0m  as  the  product
  (u_1^-1r_1u_1)...(u_k^-1r_ku_k)   and   is  an  identity  Y-sequence  if  it
  evaluates  to  the  identity  in  [10XF[0m.  An  identity  Y-sequence represents an
  identity  among the relators of the group presentation. The main function of
  the  package is to produce a set of Y-sequences which generate the module of
  identites among relators, and further, that this set be minimal in the sense
  that every element in it is needed to generate the module.
  
  Before  starting  on  the  main  example,  we  consider  a  simpler  example
  illustrating  the  use  of  [5XIdRel[0m.  All  the functions used are described in
  detail in this manual. We compute a reduced set of identities among relators
  for  the  presentation  of  the  symmetric  group [10Xs3[0m with generators a,b and
  relators  [a^3  ,  b^2,  (ab)^2].  In  the listing below, [10Xs3_M1[0m is the first
  monoid  generator  for  [10Xs3[0m,  [10Xs3_R2[0m is the second relator, while [10Xs3_Y4[0m is the
  fourth Y-sequence for [10Xs3[0m.
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> F := FreeGroup( 2 );;[0X
    [4Xgap> a := F.l;; b:= F.2;;[0X
    [4Xgap> rels3 := [ a^3 , b^2, a*b*a*b];[0X
    [4X[ fl^3 , f2^2, fl*f2*fl*f2] [0X
    [4Xgap> s3 := F/rels3;[0X
    [4X<fp group on the generators [ fl, f2 ]> [0X
    [4Xgap> SetName( s3, "s3" ); [0X
    [4Xgap> idy3 := IdentityYSequences( s3 );; [0X
    [4Xgap> Length( idy3 ); [0X
    [4X18[0X
    [4Xgap> Y4 := idy3[4]; [0X
    [4X[ [ s3_R1^-1, f1^-1 ], [ s3_R1, <identity ...> ] ][0X
    [4Xgap> Y6 := idy3[6];[0X
    [4X[ [ s3_R3^-1, f1^-1 ], [ s3_R1, <identity ...> ], [ s3_R3^-1, f1 ],[0X
    [4X  [ s3_R2, f1^-1*f2^-1 ], [ s3_R1, f2^-1 ], [ s3_R3^-1, f1*f2^-1 ],[0X
    [4X  [ s3_R2, <identity ...> ], [ s3_R2, f1^-1 ] ][0X
    [4Xgap> Y7 := idy3[7];[0X
    [4X[ [ s3_R3^-1, f1*f2^-1 ], [ s3_R2, <identity ...> ], [ s3_R3, <identity ...> ],[0X
    [4X  [ s3_R2^-1, <identity ...> ] ][0X
    [4Xgap> Y8 := idy3[8];[0X
    [4X[ [ s3_R2^-1, f2^-1 ], [ s3_R2, <identity ...> ] ][0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  Of  the  18  Y-sequences  formed,  6  are empty, and [10XY4[0m is the [13Xroot identity[0m
  [10X((a^3)^-1)^(a^-1).(a^3)[0m.  If  we  write  r=a^3,  s=b^2,  t=(ab)^2 then [10XY4[0m is
  [10X(r^-1)^(a^-1).r[0m.  Similarly, [10XY8[0m is the second root identity [10X(s^-1)^(b^-1).s[0m,
  while [10XY7[0m is the third root identity [10X(t^-1)^(ab^-1).t[0m. The identity [10XY6[0m, which
  is  not  a root identity, is obtained by walking around the Schreier diagram
  of  the presentation, which is a somewhat truncated triangular prism, taking
  the     appropriate     conjugate     of     each    face    in    turn:    [10X
  Y6=(t^-1)^(a^-1).r.(t^-1)^a.s^(a^-1b^-1).r^(b^-1).(t^-1)^(ab^-1).s.s^(a^-1)
  [0m. These four identities generate the module of identities for [10Xs3[0m.
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> idrels3 := IdentitiesAmongRelators( s3 );;[0X
    [4Xgap> Display( idrels3[1] );[0X
    [4X[ ( s3_Y4*( s3_M2*s3_M1), s3_R1*( s3_M1 - <identity ...>) ),[0X
    [4X  ( s3_Y8*( s3_M2*s3_M1), s3_R2*( s3_M2 - <identity ...>) ),[0X
    [4X  ( s3_Y7*( s3_M2*s3_M1), s3_R3*( s3_M2 - s3_M1) ),[0X
    [4X  ( s3_Y6*( -s3_M2*s3_M1), s3_R1*( -s3_M2*s3_M1 - s3_M1) + s3_R2*( -s3_M1*s3_M\[0X
    [4X2 - s3_M1 - <identity ...>) + s3_R3*( s3_M3 + s3_M2 + <identity ...>) )[0X
    [4X  ][0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  
  [1X2.2 Monoid Presentations of FpGroups[0X
  
  [1X2.2-1 FreeRelatorGroup[0m
  
  [2X> FreeRelatorGroup( [0X[3Xgrp[0X[2X ) _________________________________________[0Xattribute
  [2X> FreeRelatorHomomorphism( [0X[3Xgrp[0X[2X ) __________________________________[0Xattribute
  
  The function [10XFreeRelatorGroup[0m returns a free group on the set of relators of
  the  given  fp-group  [10XG[0m.  If [10XHasName(G)[0m is [10Xtrue[0m then a name is automatically
  assigned to the free group.
  
  The function [10XFreeRelatorHomomorphism[0m returns the group homomorphism from the
  free group on the relators to the free group on the generators of [10XG[0m, mapping
  each generator to the corresponding word.
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> F := FreeGroup( 2 );;[0X
    [4Xgap> a := F.1;; b:= F.2;;[0X
    [4Xgap> rels := [ a^4, b^4, a*b*a*b^-1, a^2*b^2];;[0X
    [4Xgap> q8 := F/rels;;[0X
    [4Xgap> SetName( q8, "q8" );[0X
    [4Xgap> frq8 := FreeRelatorGroup( q8 );[0X
    [4Xq8_R [0X
    [4Xgap> GeneratorsOfGroup( frq8 );[0X
    [4X[ q8_R1, q8_R2, q8_R3, q8_R4][0X
    [4Xgap> frhomq8 := FreeRelatorHomomorphism( q8 );[0X
    [4X[ q8_R1, q8_R2, q8_R3, q8_R4] -> [ f1^4, f2^4, f1*f2*f1*f2^-1, f1^2*f2^2][0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  [1X2.2-2 MonoidPresentationFpGroup[0m
  
  [2X> MonoidPresentationFpGroup( [0X[3Xgrp[0X[2X ) ________________________________[0Xattribute
  [2X> FreeGroupOfPresentation( [0X[3Xmon[0X[2X ) __________________________________[0Xattribute
  [2X> GroupRelatorsOfPresentation( [0X[3Xmon[0X[2X ) ______________________________[0Xattribute
  [2X> InverseRelatorsOfPresentation( [0X[3Xmon[0X[2X ) ____________________________[0Xattribute
  [2X> HomomorphismOfPresentation( [0X[3Xmon[0X[2X ) _______________________________[0Xattribute
  
  A  monoid  presentation  for  a  finitely  presented  group [10XG[0m has two monoid
  generators  g^+,g^-  for  each group generator g. The relators of the monoid
  presentation comprise the group relators, and relators g^+g^- specifying the
  inverses.   The   function   [10XMonoidPresentationFpGroup[0m  returns  the  monoid
  presentation  derived  in  this  way  from  an  fp-presentation. (Note: this
  function   should   always   be   followed   by   a   double   semicolon  --
  [10XMonoidPresentationFpGroup(G);;[0m  --  because an error occurs in attempting to
  display the results on the screen: the [10XElementsFamily[0m needs to be fixed.)
  
  The  function  [10XFreeGroupOfPresentation[0m  returns the free group on the monoid
  generators.
  
  The  function  [10XGroupRelatorsOfPresentation[0m  returns  those  relators  of the
  monoid which correspond to the relators of the group. All negative powers in
  the group relators are converted to positive powers of the g^-.
  
  The  function  [10XInverseRelatorsOfPresentation[0m  returns relators which specify
  the inverse pairs of the monoid generators.
  
  The  function  [10XHomomorphismOfPresentation[0m  returns the homomorphism from the
  free  group  of  the  monoid  presentation  to  the  free group of the group
  presentation.
  
  In  the  example  below,  the  four monoid generators a^+, b^+, a^-, b^- are
  named [10Xq8\_M1, q8\_M2, q8\_M3, q8\_M4[0m.
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> mon := MonoidPresentationFpgroup( q8 );; [0X
    [4Xgap> fgmon := FreeGroupOfPresentation( mon);[0X
    [4X<free group on the generators [ q8_Ml, q8_M2, q8_M3, q8_M4]> [0X
    [4Xgap> genfgmon := GeneratorsOfGroup( fgmon);[0X
    [4X[ q8_Ml, q8_M2, q8_M3, q8_M4] [0X
    [4Xgap> gprels := GroupRelatorsOfPresentation( mon );[0X
    [4X[ q8_Ml^4, q8_M2^4, q8_Ml*q8_M2*q8_Ml*q8_M4, q8_Ml^2*q8_M2^2] [0X
    [4Xgap> invrels := InverseRelatorsOfPresentation( mon);[0X
    [4X[ q8_Ml*q8_M3, q8_M2*q8_M4, q8_M3*q8_Ml, q8_M4*q8_M2] [0X
    [4Xgap> hompres := HomomorphismOfPresentation( mon );[0X
    [4X[ q8_Ml, q8_M2, q8_M3, q8_M4] -> [ fl, f2, fl^-l, f2^-1 ][0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  
  [1X2.3 Rewriting systems for FpGroups[0X
  
  These  functions  duplicate  the  standard  Knuth Bendix functions which are
  available  in  the  [5XGAP[0m  library.  There are two reasons for this: (1) these
  functions  were  first written before the standard functions were available;
  (2)  we  require  logged  versions  of  the  functions,  and  these are most
  conveniently extended versions of the non-logged code.
  
  [1X2.3-1 RewritingSystemFpGroup[0m
  
  [2X> RewritingSystemFpGroup( [0X[3Xgrp[0X[2X ) ___________________________________[0Xattribute
  
  This  function  attempts to return a complete rewrite system for the group [10XG[0m
  obtained  from  the  monoid  presentation [10Xmon[0m, with a length-lexicographical
  ordering  on the words in [10Xfgmon[0m, by applying Knuth-Bendix completion. Such a
  rewrite  system can be obtained for all finite groups. The rewrite rules are
  (partially)  ordered,  starting  with  the inverse relators, followed by the
  rules which reduce the word length the most.
  
  In our [10Xq8[0m example there are 16 rewrite rules.
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> rws := RewritingSystemFpGroup( q8 );[0X
    [4X[ [q8_Ml*q8_M3, <identity ...>], [ q8_M2*q8_M4, <identity ...> ], [0X
    [4X  [q8_M3*q8_Ml, <identity ...>], [ q8_M4*q8_M2, <identity ...> ], [0X
    [4X  [q8_M1^2*q8_M4, q8_M2], [q8_Ml^2*q8_M2, q8_M4], [ q8_Ml^3, q8_M3 ], [0X
    [4X  [ q8_M4^2, q8_Ml^2], [ q8_M4*q8_M3, q8_Ml*q8_M4], [0X
    [4X  [ q8_M4*q8_Ml, q8_Ml*q8_M2], [q8_M3*q8_M4, q8_Ml*q8_M2], [0X
    [4X  [ q8_M3^2, q8_Ml^2], [q8_M3*q8_M2, q8_Ml*q8_M4], [0X
    [4X  [ q8_M2*q8_M3, q8_Ml*q8_M2], [q8_M2^2, q8_Ml^2], [0X
    [4X  [ q8_M2*q8_Ml, q8_Ml*q8_M4] ][0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  The functions called by [10XRewritingSystemFpGroup[0m are as follows.
  
  [1X2.3-2 OnePassReduceWord[0m
  
  [2X> OnePassReduceWord( [0X[3Xword, rules[0X[2X ) ________________________________[0Xoperation
  [2X> ReduceWordKB( [0X[3Xword, rules[0X[2X ) _____________________________________[0Xoperation
  
  Assuming  that  [10Xword[0m  is  an element of a free monoid and [10Xrules[0m is a list of
  ordered  pairs  of  such  words, the function [10XOnePassReduceWord[0m searches the
  list  of rules until it finds that the left-hand side of a [10Xrule[0m is a [10Xsubword[0m
  of  [10Xword[0m, whereupon it replaces that [10Xsubword[0m with the right-hand side of the
  matching  rule.  The  search  is  continued from the next [10Xrule[0m in [10Xrules[0m, but
  using the new [10Xword[0m. When the end of [10Xrules[0m is reached, one pass is considered
  to  have been made and the reduced [10Xword[0m is returned. If no matches are found
  then the original [10Xword[0m is returned.
  
  The  function [10XReduceWordKB[0m repeatedly applies the function [10XOnePassReduceWord[0m
  until  the [10Xword[0m remaining contains no left-hand side of a [10Xrule[0m as a [10Xsubword[0m.
  If  [10Xrules[0m  is  a  complete rewrite system, then the irreducible [10Xword[0m that is
  returned is unique, otherwise the order of the rules in [10Xrules[0m will determine
  which  irreducible  word  is  returned.  In  the  example we see that b^9a^9
  reduces to ba, and we shall see later that this is not a normal form.
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> monrels := Concatenation( gprels, invrels );[0X
    [4X[ q8_Ml^4, q8_M2^4, q8_Ml*q8_M2*q8_Ml*q8_M4, q8_Ml^2*q8_M2^2, q8_Ml*q8_M3, [0X
    [4X  q8_M2*q8_M4, q8_M3*q8_Ml, q8_M4*q8_M2] [0X
    [4Xgap> id := One( monrels[l] );;[0X
    [4Xgap> r0 := List( monrels, r -> [ r, id ] );[0X
    [4X[ [ q8_Ml^4, <identity ...> ], [ q8_M2^4, <identity. ..> ], [0X
    [4X  [ q8_Ml*q8_M2*q8_Ml*q8_M4, <identity ...> ], [0X
    [4X  [ q8_Ml^2*q8_M2^2, <identity. ..>], [ q8_Ml*q8_M3, <identity ...> ], [0X
    [4X  [ q8_M2*q8_M4, <identity ...> ], [ q8_M3*q8_Ml, <identity. ..>], [0X
    [4X  [ q8_M4*q8_M2, <identity ...> ] ] [0X
    [4Xgap> ap := genfgmon[l];; bp := genfgmon[2];;   ## p for plus[0X
    [4Xgap> am := genfgmon[3];; bm := genfgmon[4];;   ## m for minus[0X
    [4Xgap> w0 := bp^9 * ap^9;[0X
    [4Xq8_M2^9*q8_M1^9[0X
    [4Xgap> w1 := OnePassReduceWord( w0, r0 );[0X
    [4Xq8_M2^5*q8_M1^5[0X
    [4Xgap> w2 := ReduceWordKB( w0, r0 );[0X
    [4Xq8_M2*q8_M1[0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  [1X2.3-3 OnePassKB[0m
  
  [2X> OnePassKB( [0X[3Xrules[0X[2X ) ______________________________________________[0Xoperation
  [2X> RewriteReduce( [0X[3Xrules[0X[2X ) __________________________________________[0Xoperation
  [2X> KnuthBendix( [0X[3Xrules[0X[2X ) ____________________________________________[0Xoperation
  [2X> ShorterRule( [0X[3Xrule1, rule2[0X[2X ) _____________________________________[0Xoperation
  
  The  function  [10XOnePassKB[0m  implements  the  main  loop  of  the  Knuth-Bendix
  completion algorithm. Rules are compared with each other; all critical pairs
  are  calculated;  and  the  irreducible  critical  pairs are orientated with
  respect  to  the  length-lexicographical  ordering  and added to the rewrite
  system.
  
  The  function  [10XRewriteReduce[0m  will  remove  unnecessary rules from a rewrite
  system.  A  rule  is  deemed to be unnecessary if it is implied by the other
  rules,  i.e. if both sides can be reduced to the same thing by the remaining
  rules.
  
  The  function  [10XKnuthBendix[0m implements the Knuth-Bendix algorithm, attempting
  to  complete  a  rewrite  system  with  respect  to  a  length-lexicographic
  ordering.  It  calls  first  [10XOnePassKB[0m,  which  adds  rules,  and  then (for
  efficiency) [10XRewriteReduce[0m which removes any unnecessary ones. This procedure
  is  repeated  until  [10XOnePassKB[0m  adds  no  more  rules.  It  will  not always
  terminate,  but for many examples (all finite groups) it will be successful.
  The  rewrite system returned is complete, that is: it will rewrite any given
  word  in  the  free monoid to a unique irreducible; there is one irreducible
  for  each  element  of the quotient monoid; and any two elements of the free
  monoid which are in the same class will rewrite to the same irreducible.
  
  The  function [10XShorterRule[0m gives an ordering on rules. Rules (g_lg_2,id) that
  identify  two generators (or one generator with the inverse of another) come
  first  in  the  ordering.  Otherwise  one precedes another if it reduces the
  length of a word by a greater amount.
  
  One  pass  of  this  procedure  for  our  [10Xq8[0m example adds 13 relators to the
  original  8, and these 21 are then reduced to 9. A second pass and reduction
  gives  a  list  of  16  rules  which forms a complete rewrite system for the
  group.
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> r1 := OnePassKB( r0 );[0X
    [4X[ [ q8_Ml^4, <identity ...> ], [ q8_M2^4, <identity ...> ], [0X
    [4X  [ q8_Ml*q8_M2*q8_Ml*q8_M4, <identity ...> ], [0X
    [4X  [ q8_Ml^2*q8_M2^2, <identity. ..> ], [ q8_Ml*q8_M3, <identity ...> ], [0X
    [4X  [ q8_M2*q8_M4, <identity ...> ], [ q8_M3*q8_Ml, <identity ...> ], [0X
    [4X  [ q8_M4*q8_M2, <identity ...> ], [ q8_M2*q8_Ml*q8_M4, q8_Ml^3], [0X
    [4X  [ q8_Ml*q8_M2^2, q8_Ml^3 ], [ q8_M2^2, q8_Ml^2 ], [q8_Ml^3, q8_M3 ], [0X
    [4X  [ q8_M2^3, q8_M4 ], [ q8_Ml*q8_M2*q8_Ml, q8_M2], [0X
    [4X  [ q8_M2^3, q8_Ml^2*q8_M2], [ q8_M2^2, q8_Ml^2 ], [q8_Ml^2*q8_M2, q8_M4 ], [0X
    [4X  [ q8_Ml^3, q8_M3 ], [ q8_M2*q8_Ml*q8_M4, q8_M3 ], [q8_Ml*q8_M2^2, q8_M3 ], [0X
    [4X  [ q8_M2^3, q8_M4 ] ] [0X
    [4Xgap> r1 := RewriteReduce( r1 );[0X
    [4X[ [ q8_Ml*q8_M3, <identity ...> ], [ q8_M2^2, q8_Ml^2 ], [0X
    [4X  [ q8_M2*q8_M4, <identity ...> ], [ q8_M3*q8_Ml, <identity ...> ], [0X
    [4X  [ q8_M4*q8_M2, <identity ...> ], [ q8_Ml^3, q8_M3 ], [0X
    [4X  [ q8_Ml^2*q8_M2, q8_M4 ], [ q8_Ml*q8_M2*q8_Ml, q8_M2 ], [0X
    [4X  [ q8_M2*q8_Ml*q8_M4, q8_M3 ] ] [0X
    [4Xgap> r2 := KnuthBendix( r1 );[0X
    [4X[ [ q8_Ml*q8_M3, <identity ...> ], [ q8_M2*q8_Ml, q8_Ml*q8_M4 ], [0X
    [4X  [ q8_M2^2, q8_Ml^2], [ q8_M2*q8_M3, q8_Ml*q8_M2 ], [0X
    [4X  [ q8_M2*q8_M4, <identity ...> ], [ q8_M3*q8_Ml, <identity ...> ], [0X
    [4X  [ q8_M3*q8_M2, q8_Ml*q8_M4 ], [ q8_M3^2, q8_Ml^2 ], [0X
    [4X  [ q8_M3*q8_M4, q8_Ml*q8_M2 ], [ q8_M4*q8_Ml, q8_Ml*q8_M2 ], [0X
    [4X  [ q8_M4*q8_M2, <identity ...> ], [ q8_M4*q8_M3, q8_Ml*q8_M4 ], [0X
    [4X  [ q8_M4^2, q8_Ml^2], [ q8_Ml^3, q8_M3 ], [q8_Ml^2*q8_M2, q8_M4 ], [0X
    [4X  [ q8_Ml^2*q8_M4, q8_M2 ] ][0X
    [4Xgap> w2 := ReduceWordKB( w0, r2 );[0X
    [4Xq8_M1*q8_M4[0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
  
  [1X2.4 Enumerating elements[0X
  
  [1X2.4-1 ElementsOfMonoidPresentation[0m
  
  [2X> ElementsOfMonoidPresentation( [0X[3Xmon[0X[2X ) _____________________________[0Xattribute
  
  The function [10XElementsOfMonoidPresentation[0m returns a list of normal forms for
  the  elements  of the group given by the monoid presentation [10Xmon[0m. The normal
  forms  are  the  least  elements  in each equivalence class (with respect to
  length-lex  order).  When  [10Xrules[0m is a complete rewrite system for [10XG[0m the list
  returned is a set of normal forms for the group elements.
  
  [4X---------------------------  Example  ----------------------------[0X
    [4X[0X
    [4Xgap> elq8 := Elements( q8 );[0X
    [4X[ <identity .. .>, fl, f2, fl^2, fl*f2, fl^3*f2, fl^3, fl^2*f2 ] [0X
    [4Xgap> elmonq8 := ElementsOfMonoidPresentation( monq8 );[0X
    [4X[ <identity. ..>, q8_Ml, q8_M2, q8_M3, q8_M4, q8_Ml^2, q8_Ml*q8_M2, [0X
    [4X  q8_Ml*q8_M4 ][0X
    [4X[0X
  [4X------------------------------------------------------------------[0X
  
