##############################################################################
##
#W  idrel_manual.g                 Idrel Package                 Chris Wensley
#W                                                             & Anne Heyworth
##
#H  @(#)$Id: idrel_manual.g,v 2.02 2006/03/29 gap Exp $
##
#Y  Copyright (C) 1999,2001 University of St. Andrews, North Haugh,
#Y                          St. Andrews, Fife KY16 9SS, Scotland
##
Revision.("idrel/examples/idrel_manual_g") :=
   "@(#)$Id: idrel_manual.g,v 2.02 2006/03/29 gap Exp $";

SetInfoLevel( InfoIdRel, 1 );
Info( InfoIdrel, 1, "setting level of InfoIdRel to 1" );

Print("\n====================== Rewriting Systems =====================\n\n");

F := FreeGroup( 2 );
Print( F, "\n" );
a := F.1;; b := F.2;;
rels := [ a^4, b^4, a*b*a*b^-1, a^2*b^2 ];
Print( rels, "\n" );
q8 := F/rels;;
SetName( q8, "q8" );
frq8 := FreeRelatorGroup( q8 );
Print( frq8, "\n" );
Print( GeneratorsOfGroup( frq8 ), "\n" );
frhomq8 := FreeRelatorHomomorphism( q8 );
Print( frhomq8, "\n" );
mon := MonoidPresentationFpGroup( q8 );;
fgmon := FreeGroupOfPresentation( mon );
Print( fgmon, "\n" );
genfgmon := GeneratorsOfGroup( fgmon );
Print( genfgmon, "\n" );
gprels := GroupRelatorsOfPresentation( mon );
Print( gprels, "\n" );
invrels := InverseRelatorsOfPresentation( mon );
Print( invrels, "\n" );
hompres := HomomorphismOfPresentation( mon );
Print( hompres, "\n" );
rws := RewritingSystemFpGroup( q8 );
Print( rws, "\n" );
monrels := Concatenation( gprels, invrels );
Print( monrels, "\n" );
id := One( monrels[1] );;
r0 := List( monrels, r -> [ r, id ] );
Print( r0, "\n" );
ap := genfgmon[1];;  bp := genfgmon[2];;
am := genfgmon[3];;  bm := genfgmon[4];;
w0 := (ap^3 * bp^3 )^3;
Print( "w0 = ", w0, "\n" );
w1 := OnePassReduceWord( w0, r0 );
Print( "w1 = ", w1, "\n" );
w2 := ReduceWordKB( w0, r0 );
Print( "w2 = ", w2, "\n" );
r1 := OnePassKB( r0 );
Print( "r1 = ", r1, "\n" );
r1 := RewriteReduce( r1 );
Print( "reduced r1 = ", r1, "\n" );
r2 := KnuthBendix( r1 );
Print( "r2 = ", r2, "\n" );
elq8 := Elements( q8 );
Print( elq8, "\n" );
elmonq8 := ElementsOfMonoidPresentation( q8 );
Print( elmonq8, "\n" );

Print("\n=============== Logged Rewriting Systems =====================\n\n");

SetInfoLevel( InfoIdRel, 2 );
Info( InfoIdrel, 1, "setting level of InfoIdRel to 2" );

l0 := ListWithIdenticalEntries( 8, 0 );;
for j in [1..8] do
    r := r0[j];
    if (j<5) then
        l0[j] := [ r[1], [ [j,id] ], r[2] ];
    else
        l0[j] := [ r[1], [ ], r[2] ];
    fi;
od;
Print( "l0 = ", l0, "\n" );
l1 := LoggedOnePassKB( l0 );
Print( "l1 = ", l1, "\n" );
l1 := LoggedRewriteReduce( l1 );
Print( "reduced l1 = ", l1, "\n" );
Print( "\n\nrunning LoggedKnuthBendix\n=========================\n" );
l2 := LoggedKnuthBendix( l1 );
Print( "l2 = ", l2, "\n" );

w0 := (ap^3 * bp^3 )^3;
Print( "\nw0 = ", w0, "\n" );
lw1 := LoggedOnePassReduceWord( w0, l2 );
Print( "lw1 = ", lw1, "\n" );
lw2 := LoggedReduceWordKB( w0, l2 );
Print( "lw2 = ", lw2, "\n" );
y2 := lw2[1];
Print( "y2 has length ", Length(y2), " :-\n", y2, "\n" );
ry2 := ReducedYSequence( y2 );
Print( "ry2 has length ", Length(ry2), " :-\n", ry2, "\n" );
Print( 0, "\n" );

Print("\n====================== Monoid Polynomials ====================\n\n");

rels := RelatorsOfGroup( q8 );
Print( rels, "\n" );
freeq8 := FreeGroupOfFpGroup( q8 );
Print( freeq8, "\n" );
gens := GeneratorsOfGroup( freeq8 );;
famfree := ElementsFamily( FamilyObj( freeq8 ) );
Print( famfree, "\n" );
famfree!.monoidPolyFam := MonoidPolyFam;
Print( famfree!.monoidPolyFam, "\n" );
cg := [6,7];
Print( "cg = ", cg, "\n" );
cr := [3,4,-5,-2];
Print( "cr = ", cr, "\n" );
pg := MonoidPolyFromCoeffsWords( cg, gens );
Print( "pg = ", pg, "\n" );
pr := MonoidPolyFromCoeffsWords( cr, rels );
Print( "pr = ", pr, "\n" );
zeroncp := ZeroMonoidPoly( freeq8 );
Print( "zeroncp = ", zeroncp, "\n" );
Print( "pr has coeffs ", Coeffs( pr ), "\n" );
Print( "pr has terms ", Terms( pr ), "\n" );
Print( "pr has words ", Words( pr ), "\n" );
Print( "pr has lead term ", LeadTerm( pr ), "\n" );
Print( "pr has lead coeff ", LeadCoeffMonoidPoly( pr ), "\n\n" );
mpr := Monic( pr );
Print( "mpr = ", mpr, "\n" );
w := gens[1]^gens[2];
Print( "w = ", w, "\n" );
cw := 3/4;;
wpg := AddTermMonoidPoly( pg, cw, w );
Print( "wpg = ", wpg, "\n" );
Print( "pg = pg ?  ", pg=pg, "\n" );
Print( "pg = pr ?  ", pg=pr, "\n" );
prcw := pr * cw;
Print( "prcw = ", prcw, "\n" );
cwpr := cw * pr;
Print( "cwpr = ", cwpr, "\n" );
Print( "pr = prcw ?  ", pr=prcw, "\n" );
Print( "prcw = cwpr ?  ", prcw=cwpr, "\n" );
Print( "pg + pr = ", pg+pr, "\n" );
Print( "pg - pr = ", pg-pr, "\n" );
Print( "pg * w = ", pg*w, "\n" );
Print( "pg * pr = ", pg*pr, "\n" );
Print( "pr has length ", Length(pr), "\n\n" );

M := genfgmon;
Print( "M = ", M, "r2 = ", r2, "\n" );
pol := MonoidPolyFromCoeffsWords( [3,2,-5], [M[1]*M[3], M[2]^4, M[1]] );
Print( "pol = ", pol, "\n" );
redpol := ReduceMonoidPoly( pol, r2 );
Print( "pol reduces to redpol = ", redpol, "\n" );
Print( "pol > 3*pol ?  ", pol>3*pol, "\n" );
Print( "pol > redpol ?  ", pol>redpol, "\n" );

Print("\n====================== Module Polynomials ====================\n\n");

frq8 := FreeRelatorGroup( q8 );
Print( "frq8 = ", frq8, "\n" );
genfrq8 := GeneratorsOfGroup( frq8 );
Print( "genfrq8 = ", genfrq8, "\n" );
mp1 := MonoidPolyFromCoeffsWords( [3,2,-5], [M[1]*M[3], M[2]^4, M[1]] );
Print( "mp1 = ", mp1, "\n" );
mp2 := MonoidPolyFromCoeffsWords( [1,-4,-1,2], 
    [M[2]*M[3]*M[1], M[3]^3, M[1]*M[2], M[4]] );
Print( "mp2 = ", mp2, "\n" );
s1 := ModulePoly( [ genfrq8[4], genfrq8[1] ], [mp1, mp2 ] );
Print( "s1 = ", s1, "\n" );
s2 := ModulePoly( [ genfrq8[3], genfrq8[2], genfrq8[1] ], 
    [3*mp1, -1*mp2, (mp1+mp2) ] );
Print( "s2 = ", s2, "\n" );
Print( "s1, s2 have lengths ", Length(s1), ", ", Length(s2), "\n" );
Print( "One(s1) = ", One(s1), "\n" );
Print( "s1 has terms ", Terms(s1), "\n" );
Print( "s2 has terms ", Terms(s2), "\n" );
Print( "s1 has lead term ", LeadTerm(s1), "\n" );
Print( "s2 has lead term ", LeadTerm(s2), "\n" );
Print( "s1 has lead monoid poly ", LeadMonoidPoly(s1), "\n" );
Print( "s2 has lead monoid poly ", LeadMonoidPoly(s2), "\n" );
zeromp := ZeroModulePoly( frq8, freeq8 );
Print( "zeromp = ", zeromp, "\n\n" );

s3 := AddTermModulePoly( s1, genfrq8[3], mp1 );
Print( "s3 = ", s3 );
Print( "s1 + s2 = ", s1+s2, "\n" );
Print( "s1 - s2 = ", s1-s2, "\n" );
Print( "s1*1/2 = ", s1*1/2, "\n" );
Print( "s1*M[1] = ", s1*M[1], "\n" );
Print( "s3 has length ", Length(s3), "\n\n" );

Print("\n================= Identities among relators =================\n\n");

polys := IdentityModulePolys( q8 );
Print( "\n", Length(polys), " identity polynomials found\n\n" );
p1 := polys[1];
Print( "first identity polynomial, p1 = \n", p1, "\n" );
ids := IdentitiesAmongRelators( q8 );
Print( "\n", Length(ids[1]), " identities found\n" );
