From 5447671fcde0b2fdd0ea11fca85f37dad05e32e2 Mon Sep 17 00:00:00 2001 From: Alexander Hulpke Date: Wed, 26 Jul 2023 13:38:37 -0600 Subject: [PATCH] WORKAROUND: Avoid problems in Fr/Kan packages 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 --- lib/kbsemi.gi | 70 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 12 deletions(-) diff --git a/lib/kbsemi.gi b/lib/kbsemi.gi index 5b1f508ccce..3072536ad06 100644 --- a/lib/kbsemi.gi +++ b/lib/kbsemi.gi @@ -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; @@ -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). @@ -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); + elif s[1]='B' and ForAny(s[2],x->x>a) then + s[2]:=Filtered(s[2],x->x<=a); + 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); @@ -505,6 +534,7 @@ function(kbrws,v) od; kbrws!.pairs2check:=l; fi; + end; #given a Knuth Bendix Rewriting System, kbrws, @@ -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); @@ -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]; @@ -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;