38 Rewriting Systems Rewriting systems in GAP are a framework for dealing with the very general task of rewriting elements of a free (or term) algebra in some normal form. Although most rewriting systems currently in use are string rewriting systems (where the algebra has only one binary operation which is associative) the framework in GAP is general enough to encompass the task of rewriting algebras of any signature from groups to semirings. Rewriting systems are already implemented in GAP for finitely presented semigroups and for pc groups. The use of these particular rewriting systems is described in the corresponding chapters. We describe here only the general framework of rewriting systems with a particular emphasis on material which would be helpful for a developer implementing a rewriting system. We fix some definitions and terminology for the rest of this chapter. Let T be a term algebra in some signature. A term rewriting system for T is a set of ordered pairs of elements of T of the form (l, r). Viewed as a set of relations, the rewriting system determines a presentation for a quotient algebra A of T. When we take into account the fact that the relations are expressed as ordered pairs, we have a way of reducing the elements of T. Suppose an element u of T has a subword l and (l, r) is a rule of the rewriting system, then we can replace the subterm l of u by the term r and obtain a new word v. We say that we have rewritten u as v. Note that u and v represent the same element of A. If u cannot be rewritten using any rule of the rewriting system we sat that u is reduced. 38.1 Operations on rewriting systems 38.1-1 IsRewritingSystem IsRewritingSystem( obj )  Category This is the category in which all rewriting systems lie. 38.1-2 Rules Rules( rws )  attribute The rules comprising the rewriting system. Note that these may change through the life of the rewriting system, however they will always be a set of defining relations of the algebra described by the rewriting system. 38.1-3 OrderOfRewritingSystem OrderOfRewritingSystem( rws )  attribute OrderingOfRewritingSystem( rws )  attribute return the ordering of the rewriting system rws. 38.1-4 ReducedForm ReducedForm( rws, u )  operation Given an element u in the free (or term) algebra T over which rws is defined, rewrite u by successive applications of the rules of rws until no further rewriting is possible, and return the resulting element of T. 38.1-5 IsConfluent IsConfluent( rws )  property IsConfluent( A )  property For a rewriting system rws, IsConfluent returns true if and only if rws is confluent. A rewriting system is confluent if, for every two words u and v in the free algebra T which represent the same element of the algebra A defined by rws, ReducedForm( rws, u ) = ReducedForm( rws, v) as words in the free algebra T. This element is the unique normal form of the element represented by u. For an algebra A with a canonical rewriting system associated with it, IsConfluent checks whether that rewriting system is confluent. Also seeĀ IsConfluent (46.4-7). 38.1-6 ConfluentRws ConfluentRws( rws )  attribute Return a new rewriting system defining the same algebra as rws which is confluent. 38.1-7 IsReduced IsReduced( rws )  property A rewriting system is reduced if for each rule (l, r), l and r are both reduced. 38.1-8 ReduceRules ReduceRules( rws )  operation Reduce rules and remove redundant rules to make rws reduced. 38.1-9 AddRule AddRule( rws, rule )  operation Add rule to a rewriting system rws. 38.1-10 AddRuleReduced AddRuleReduced( rws, rule )  operation Add rule to rewriting system rws. Performs a reduction operation on the resulting system, so that if rws is reduced it will remain reduced. 38.1-11 MakeConfluent MakeConfluent( rws )  operation Add rules (and perhaps reduce) in order to make rws confluent 38.1-12 GeneratorsOfRws GeneratorsOfRws( rws )  attribute Returns the list of generators of the rewriting system rws. 38.2 Operations on elements of the algebra In this section let u denote an element of the term algebra T representing [u] in the quotient algebra A. 38.2-1 ReducedProduct ReducedProduct( rws, u, v )  operation ReducedSum( rws, left, right )  operation ReducedOne( rws )  operation ReducedAdditiveInverse( rws, obj )  operation ReducedComm( rws, left, right )  operation ReducedConjugate( rws, left, right )  operation ReducedDifference( rws, left, right )  operation ReducedInverse( rws, obj )  operation ReducedLeftQuotient( rws, left, right )  operation ReducedPower( rws, obj, pow )  operation ReducedQuotient( rws, left, right )  operation ReducedScalarProduct( rws, left, right )  operation ReducedZero( rws )  operation The result of ReducedProduct is w where [w] equals [u][v] in A and w is in reduced form. The remaining operations are defined similarly when they are defined (as determined by the signature of the term algebra). 38.3 Properties of rewriting systems 38.3-1 IsBuiltFromAdditiveMagmaWithInverses IsBuiltFromAdditiveMagmaWithInverses( obj )  property IsBuiltFromMagma( obj )  property IsBuiltFromMagmaWithOne( obj )  property IsBuiltFromMagmaWithInverses( obj )  property IsBuiltFromSemigroup( obj )  property IsBuiltFromGroup( obj )  property These properties may be used to identify the type of term algebra over which the rewriting system is defined. 38.4 Rewriting in Groups and Monoids One application of rewriting is to reduce words in finitely presented groups and monoids. The rewriting system still has to be built for a finitely presented monoid (using IsomorphismFpMonoid for conversion). Rewriting then can take place for words in the underlying free monoid. (These can be obtained from monoid elements with the command UnderlyingElement.)  Example  gap> f:=FreeGroup(3);; gap> rels:=[f.1*f.2^2/f.3,f.2*f.3^2/f.1,f.3*f.1^2/f.2];; gap> g:=f/rels;  gap> mhom:=IsomorphismFpMonoid(g); MappingByFunction( , , function( x ) ... end, function( x ) ... end ) gap> mon:=Image(mhom);  gap> k:=KnuthBendixRewritingSystem(mon); Knuth Bendix Rewriting System for Monoid(  [ f1, f1^-1, f2, f2^-1, f3, f3^-1 ] ) with rules  [ [ f1*f1^-1, ], [ f1^-1*f1, ],   [ f2*f2^-1, ], [ f2^-1*f2, ],   [ f3*f3^-1, ], [ f3^-1*f3, ],   [ f1*f2^2*f3^-1, ], [ f2*f3^2*f1^-1, ]  , [ f3*f1^2*f2^-1, ] ] gap> MakeConfluent(k); gap> a:=Product(GeneratorsOfMonoid(mon)); f1*f1^-1*f2*f2^-1*f3*f3^-1 gap> ReducedForm(k,UnderlyingElement(a));   To rewrite a word in the finitely presented group, one has to convert it to a word in the monoid first, rewrite in the underlying free monoid and convert back (by forming first again an element of the fp monoid) to the finitely presented group.  Example  gap> r:=PseudoRandom(g);; gap> Length(r); 3704 gap> melm:=Image(mhom,r);; gap> red:=ReducedForm(k,UnderlyingElement(melm)); f1^-1^3*f2^-1*f1^2 gap> melm:=ElementOfFpMonoid(FamilyObj(One(mon)),red); f1^-1^3*f2^-1*f1^2 gap> gpelm:=PreImagesRepresentative(mhom,melm); f1^-3*f2^-1*f1^2 gap> r=gpelm; true gap> CategoriesOfObject(red); [ "IsExtLElement", "IsExtRElement", "IsMultiplicativeElement",   "IsMultiplicativeElementWithOne", "IsAssociativeElement", "IsWord" ] gap> CategoriesOfObject(melm); [ "IsExtLElement", "IsExtRElement", "IsMultiplicativeElement",   "IsMultiplicativeElementWithOne", "IsAssociativeElement",   "IsElementOfFpMonoid" ] gap> CategoriesOfObject(gpelm); [ "IsExtLElement", "IsExtRElement", "IsMultiplicativeElement",   "IsMultiplicativeElementWithOne", "IsMultiplicativeElementWithInverse",   "IsAssociativeElement", "IsElementOfFpGroup" ]  Note, that the elements red (free monoid) melm (fp monoid) and gpelm (group) differ, though they are displayed identically. Under Unix, it is possible to use the kbmag package to replace the built-in rewriting by this packages efficient C implementation. You can do this (after loading the kbmag package) by assigning the variable KB_REW (52.5-2) to KBMAG_REW. Assignment to GAPKB_REW reverts to the built-in implementation.  Example  gap> LoadPackage("kbmag"); true gap> KB_REW:=KBMAG_REW;;  38.5 Developing rewriting systems The key point to note about rewriting systems is that they have properties such as IsConfluent (38.1-5) and attributes such as Rules (38.1-2), however they are rarely stored, but rather computed afresh each time they are asked for, from data stored in the private members of the rewriting system object. This is because a rewriting system often evolves through a session, starting with some rules which define the algebra A as relations, and then adding more rules to make the system confluent. For example, in the case of Knuth-Bendix rewriting systems (see ChapterĀ 52), the function CreateKnuthBendixRewritingSystem creating the rewriting system (in the file lib/kbsemi.gi) uses  Example  kbrws := Objectify(NewType(rwsfam,   IsMutable and IsKnuthBendixRewritingSystem and   IsKnuthBendixRewritingSystemRep),   rec(family:= fam,  reduced:=false,  tzrules:=List(relwco,i->  [LetterRepAssocWord(i[1]),LetterRepAssocWord(i[2])]),  pairs2check:=CantorList(Length(r)),  ordering:=wordord,  freefam:=freefam));  In particular, since we don't use the filter IsAttributeStoringRep in the Objectify (79.1-1), whenever IsConfluent (38.1-5) is called, the appropriate method to determine confluence is called.