Skip to content

Commit

Permalink
WORKAROUND: Avoid problems in Fr/Kan packages
Browse files Browse the repository at this point in the history
The Kan package copied library code and thus uses undocumented entries
of rewriting systems. Added workaround to avoid triggering an error
in the test.

fr package messes with the .tzrules without handling the pairs2check.

This resolves #5478
  • Loading branch information
hulpke committed Jul 26, 2023
1 parent aafd490 commit 5447671
Showing 1 changed file with 58 additions and 12 deletions.
70 changes: 58 additions & 12 deletions lib/kbsemi.gi
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
## This file contains code for the Knuth-Bendix rewriting system for semigroups
## and monoids.
##
# gaplint: disable = analyse-lvars

InstallGlobalFunction(EmptyKBDAG,function(genids)
local offset,deadend;
Expand Down Expand Up @@ -182,6 +183,9 @@ DeclareRepresentation("IsKnuthBendixRewritingSystemRep",
## refers to one of the rules. A pair corresponds to a pair
## of rules where confluence was not yet checked (according to
## the Knuth Bendix algorithm).
## Pairs might also be given in the form of a 3-entry list
## ['A',x,l] to denote all pairs of the form [x,y] for y in l,
## respectively ['B',l,y] for pairs [x,y[] with x in l.
##
## Note that at this stage the kb rws obtained might not be reduced
## (the same relation might even appear several times).
Expand Down Expand Up @@ -392,12 +396,37 @@ InstallOtherMethod(AddRuleReduced,
and IsKnuthBendixRewritingSystemRep, IsList ], 0,
function(kbrws,v)

local u,a,b,c,k,n,s,add_rule,remove_rule,fam,ptc,kbdag,abi,rem,
remove_rules;
local u,a,b,c,k,n,s,add_rule,remove_rule,fam,ptc,kbdag,abi,rem,
remove_rules;

# did anyone fiddle with the rules that there are invalid pairs?
# This happens e.g. in the fr package.
if IsBound(kbrws!.pairs2check)
# only do this if its within the fr package to not slow down other uses
and not IsBound(kbrws!.kbdag) then
b:=kbrws!.pairs2check;
k:=false;
a:=Length(kbrws!.tzrules);
for n in [1..Length(b)] do
s:=b[n];
if (Length(s)=2 and s[1]>a or s[2]>a)
or (s[1]='A' and s[2]>a) or (s[1]='B' and s[3]>a) then
b[n]:=fail; k:=true;
elif s[1]='A' and ForAny(s[3],x->x>a) then
s[3]:=Filtered(s[3],x->x<=a);

Check warning on line 416 in lib/kbsemi.gi

View check run for this annotation

Codecov / codecov/patch

lib/kbsemi.gi#L416

Added line #L416 was not covered by tests
elif s[1]='B' and ForAny(s[2],x->x>a) then
s[2]:=Filtered(s[2],x->x<=a);

Check warning on line 418 in lib/kbsemi.gi

View check run for this annotation

Codecov / codecov/patch

lib/kbsemi.gi#L418

Added line #L418 was not covered by tests
fi;
od;
if k then kbrws!.pairs2check:=Filtered(b,x->x<>fail);fi;
fi;



# the fr package assigns initial tzrules on its own, this messes up
# the dag structure. Delete ...
if IsBound(kbrws!.kbdag) and Length(kbrws!.kbdag.backpoint)<>Length(kbrws!.tzrules) then
if IsBound(kbrws!.kbdag) and
Length(kbrws!.kbdag.backpoint)<>Length(kbrws!.tzrules) then
Info(InfoPerformance,2,
"Cannot use dag for lookup since rules were assigned directly");
#a:=EmptyKBDAG(kbrws!.kbdag.genids);
Expand Down Expand Up @@ -505,6 +534,7 @@ function(kbrws,v)
od;
kbrws!.pairs2check:=l;
fi;

end;

#given a Knuth Bendix Rewriting System, kbrws,
Expand Down Expand Up @@ -767,6 +797,22 @@ function ( rws )
" rules" );
end);

# We store compressed data -- expand, (and also delete old stuff)
BindGlobal("KBRWSUnpackPairsAt",function(kbrws,p)
local i,a;
i:=kbrws!.pairs2check[p];
if IsChar(i[1]) then
# We store compressed data -- expand, (and also delete old stuff)
if i[1]='A' then
a:=List(i[3],x->[i[2],x]);
elif i[1]='B' then
a:=List(i[2],x->[x,i[3]]);
else Error("kind"); fi;
kbrws!.pairs2check:=Concatenation(a,kbrws!.pairs2check{[p+1..Length(kbrws!.pairs2check)]});
p:=1;
fi;
return p;
end);



Expand All @@ -776,7 +822,15 @@ end);
BindGlobal("KBOverlaps",function(ui,vi,kbrws,p)
local u,v,m,k,a,c,lsu,lsv,lu,eq,i,j;

# work around copied code in kan package
if IsChar(ui) then # must unpack
p:=KBRWSUnpackPairsAt(kbrws,p);
vi:=kbrws!.pairs2check[p];
ui:=vi[1];vi:=vi[2];
fi;

u:=kbrws!.tzrules[ui]; v:=kbrws!.tzrules[vi];

lsu:=u[1];
lu:=Length(lsu);
lsv:=v[1];
Expand Down Expand Up @@ -860,15 +914,7 @@ local pn,lp,rl,p,i,a;
i:=kbrws!.pairs2check[p];
if IsChar(i[1]) then
# We store compressed data -- expand, (and also delete old stuff)
if i[1]='A' then
a:=List(i[3],x->[i[2],x]);
elif i[1]='B' then
a:=List(i[2],x->[x,i[3]]);
else Error("kind"); fi;
kbrws!.pairs2check:=Concatenation(a,kbrws!.pairs2check{[p+1..Length(kbrws!.pairs2check)]});
p:=1;
i:=kbrws!.pairs2check[p];
lp:=Length(kbrws!.pairs2check);
p:=KBRWSUnpackPairsAt(kbrws,p);
fi;

p:=KBOverlaps(i[1],i[2],kbrws,p)+1;
Expand Down

0 comments on commit 5447671

Please sign in to comment.