We study and visualize string-rewriting systems and confluence in the context of solving word-problems in (finitely-presented) monoids. We implement the Knuth-Bendix Algorithm for monoids in Wolfram Language.
Introduction
Introduction
Let be a finitely presented monoid. Then the set of relations partitions the free monoid into equivalence classes. When the set of these equivalence classes is equipped with the natural binary operation, we get . Given two words in , it is very natural to ask if there is an algorithm to deduce if they are equivalent in , i.e., lie in the same equivalence class. This problem is known is known as the word-problem for finitely-presented monoids, but, unfortunately, it is undecidable as was shown by Emil Post [1] in 1947. Still, Post’s result is not as devastating as it sounds since we can still attempt to solve the word-problem when has some special structure. This is what this essay is about: Trying to solve the word problem when we have special structure.
MMon〈X|R〉
R
F
X
M
F
X
M
M
Rewriting Systems and Reducing Orders
Rewriting Systems and Reducing Orders
Let be a finite alphabet equipped with a linear order, and let be the (strict) ShortLex order on , i.e., we sort words by length first, and then we sort them lexicographically based on the linear order on the set . As is usually the case, when is a subset of the lower-case English alphabet (which comes with a linear order), we can implement the ShortLex order in Wolfram Language in the following manner:
X
≺
F
X
X
X
In[]:=
shortLex[x_String,y_String]:=False/;x==y(*EnsuresshortLexisastrictorder*)shortLex[x_String,y_String]:=StringLength[x]<StringLength[y]/;Not[StringLength[x]==StringLength[y]](*Sortsstringsbylength*)shortLex[x_String,y_String]:=OrderedQ[{x,y}]/;And[StringLength[x]==StringLength[y],x!=y](*Sortsequallengthstringsbylexicogrpahic(dictionary)order*)
For example, we have and in :
ab≺aba
ab≺ba
F
{a,b}
In[]:=
And[shortLex["ab","aba"],shortLex["ab","ba"]]
Out[]=
True
Notice that the order satisfies the following two properties:
≺
◼
(,≺)
F
X
◼
≺
a,b,c,d∈
F
X
a≺b
≺
(Note: For our purposes, we can work with any ordering that satisfies the two properties above. We choose ShortLex as the default order. The choice of ordering may matter for the results we get, and in some cases it may be more favorable to use some other order.)
DEFINITION. A rewriting system on is a set of rules of the form , with and .
R
F
X
ab
a,b∈
F
X
b≺a
A rewriting system has the ability to reduce words in by making them smaller with respect to by performing string-substitutions based on the (one-directional) rules it contains.
F
X
≺
DEFINITION.The monoid defined by a rewriting system is the monoid we get when the rules in the rewriting system are considered as relations.
For example, if the rewriting system on is , then the monoid defined by it is Mon .
F
{a,b}
{ababa,baab}
〈a,b|ababa,baab〉
DEFINITION. Let be a string rewriting system on , given two words in ,
R
F
X
u,v
F
X
◼
Say that if and are equivalent in the monoid defined by .
u∼v
u
v
R
◼
Say that if there is a rule in such that and
uv
ab
R
uxay
vxby.
◼
Say that if there is a sequence ,…, such that u, v and for each we have .
u↦v
u
1
u
n
u
1
u
n
1≤i<n
u
i
u
i+1
We have the following easy to see proposition that connects and :
∼
↦
PROPOSITION 1. Let be a string-rewriting system on , given two words , we have that if and only if there exists a sequence ,…, such that u, v and for each we have ↦ or ↦.
R
F
X
u,v∈
F
X
u∼v
u
1
u
n
u
1
u
n
1≤i<n
u
i
u
i+1
u
i+1
u
i
THEOREM. Let be a string-rewriting system on , then following three properties are equivalent.
R
F
X
◼
(Church-Rosser) For all words if , then there is a such that and .
u,v
u∼v
q∈
F
X
u↦q
v↦q
◼
(Confluence) For all words if and then there exists a such that and .
w,v,u
w↦v
w↦u
q∈
F
X
v↦q
u↦q
◼
(Local Confluence) For all words if and then there exists a such that and .
w,v,u
wv
wu
q∈
F
X
v↦q
u↦q
PROOF. See Proposition 2.5 in [2].
Let be a string-rewriting system on , we say a word is reduced with respect to if it can not be rewritten any further using . Since is a well order, every word can be rewritten with into a reduced word.
When is a subset of the lower-case English alphabet, the following algorithm rewrites a word into a reduced word with respect to a rewriting system.
R
F
X
↦
R
≺
↦
When
X
In[]:=
replace[word_,rules_]:=StringReplace[word,rules](*Replacesoccurencesofleftsidesofruleswithrightsides*)reduce[word_,rules_]:=FixedPoint[replace[#,rules]&,word](*Repeatedlyevaluatesreplaceuntilfixed-point*)
For example we have:
In[]:=
reduce["ababc",{"ab"->"b","b"->""}]
Out[]=
c
Now, if satisfies the Church Rosser property, then the monoid defined by solves the word-problem: If we want to check whether , we reduce both and into reduced words. If these reduced words are the same, then the words are equivalent, and if they are different, by the Church-Rosser property, the words lie in different equivalence classes. Let us now look at an example of a rewriting system: Consider the rewriting system over . This defines the monoid . We can visualize the graph of all words up to a certain length(in this case 5), where there is a directed edge between the words , if , in the following manner:
R
R
u∼v
u
v
R{aba,abab}
F
{a,b}
MMon〈a,b|aba,abab〉
v
w
vw
In[]:=
alphabet={"a","b"};substrings=Flatten[Table[Tuples[alphabet,n],{n,0,5}],1];stringSubstrings=StringJoin/@substrings;(*Allsubstringsoflengthlessthanorequalto5*)allReplacements[str_String,substr_String,newstr_String]:=Module[{positions,replaceAtPos},positions=StringPosition[str,substr];replaceAtPos[pos_]:=StringReplacePart[str,newstr,pos];replaceAtPos/@positions]all[word_,rules_]:=Flatten[Table[allReplacements[word,Keys[rules][[i]],Values[rules][[i]]],{i,Length[rules]}]](*Allwordssuchthatword*)fixedpoint[word_,rules_]:=If[reduce[word,rules]==word,True,False](*Checksifawordisreduced*)SimpleGraph[Union[Union[Flatten[Table[Table[stringSubstrings[[x]]all[stringSubstrings[[x]],{"ab"->"a","aba"->"b"}][[i]],{i,Length[all[stringSubstrings[[x]],{"ab"->"a","aba"->"b"}]]}],{x,1,Length[stringSubstrings]}]],Map[#->#&,Select[stringSubstrings,fixedpoint[#,{"ab"->"a","aba"->"b"}]&]]]],(*Alledgesoftheformspecified*)GraphLayout->"SpringElectricalEmbedding",VertexStyle->Union[Map[#->Black&,Select[stringSubstrings,Not[fixedpoint[#,{"ab"->"a","aba"->"b"}]]&]](*Colorsnon-reducedwordsblack*),Map[#->Red&,Select[stringSubstrings,fixedpoint[#,{"ab"->"a","aba"->"b"}]&]](*Colorsreducedwordsred*)],VertexSize->0.4,EdgeShapeFunction->{{"Arrow","ArrowSize"->0.015}},EdgeStyle->Darker[Blue]]//Quiet
v
v
Out[]=
(WARNING: Don’t be fooled by the connectivity of the graph above. The graph above is a subgraph of the actual(infinite) graph consisting of all word, and it is possible that two words are in the same weakly-connected component in the real graph but are not connected in the graph above because showing their connectivity requires going to a word above length 5.)Of course, by Proposition 1, if two words are in the same weakly connected component, then they are equivalent in the monoid. So, the multiple red reduced words we see in some weakly-connected components are causing failure in the Church-Rosser property and preventing us from solving the word-problem using the strategy described earlier. More generally, we can define the following function that makes the visualisation above for any rewriting system over :
F
{a,b}
In[]:=
graphvis[rules_]:=SimpleGraph[Union[Union[Flatten[Table[Table[stringSubstrings[[x]]all[stringSubstrings[[x]],rules][[i]],{i,Length[all[stringSubstrings[[x]],rules]]}],{x,1,Length[stringSubstrings]}]]],Map[#->#&,Select[stringSubstrings,fixedpoint[#,rules]&]]],GraphLayout->"SpringElectricalEmbedding",VertexStyle->Union[Map[#->Black&,Select[stringSubstrings,Not[fixedpoint[#,rules]]&]],Map[#->Red&,Select[stringSubstrings,fixedpoint[#,rules]&]]],VertexSize->0.4,EdgeShapeFunction->{{"Arrow","ArrowSize"->0.015}},EdgeStyle->Darker[Blue]]
Local Confluence
Local Confluence
As we said earlier, the Church-Rosser property guarantees the ability to solve the word-problem. At face value, the Church-Rosser property looks hard to check; however, it is equivalent to local confluence, which is easy to check thanks to the following theorem:
THEOREM. Let be a rewriting system. Suppose is not locally confluent, say a word is a minimal failure of local confluence if no sub word of it causes a failure in local confluence. Then if is a minimal failure of local confluence one of the following holds:
R
R
w
w
◼
w
R
R
◼
w
c
ab
bc
R.
PROOF. It suffices to show that we must have an “overlap” between two rules in . Suppose that there is no overlap between any two rules in , then for any two reductions , , we can apply to the rule that was applied to to get and apply to the rule that was applied to to get . Doing so, would give us the same word, but this is a contradiction with the fact that is a failure of local confluence.
w
w
wu
wv
u
w
v
v
w
u
w
In the next section, we shall use the theorem above to create a (semi-decision) algorithm that converts a rewriting system into a locally confluent system, and hence, a system satisfying the Church-Rosser property, without changing .
∼
The Knuth-Bendix Algorithm
The Knuth-Bendix Algorithm
Let be a rewriting system. Let be a pair of rules in ,
R
(P,Q)
R
◼
Say a word is an overlap of of type 1 if = abc where is not the empty word and is the left side of and is the left side of .
w
(P,Q)
w
c
ab
P
bc
Q
Implements the Knuth-Bendix algorithm once.
We can now repeatedly apply the above algorithm until we get a fixed-point. This is the Knuth-Bendix algorithm.
We can also visualize the new edges that have been added due to the Knuth-Bendix algorithm:
Simplifying the result we get
For example we have:
And,
As expected.
Acknowledgements
Acknowledgements
The author would like to thank the instructors and TAs of the Wolfram Research program for several helpful conversations.
Bibliography
Bibliography
1. Emil L. Post. Recursive Unsolvability of a Problem of Thue. The Journal of Symbolic Logic, Vol. 12, No. 1. (Mar., 1947), pp. 1-11. URL: https://www.wolframscience.com/prizes/tm23/images/Post2.pdf
2. Charles C. Sims. Computation with finitely presented groups. Cambridge University Press, 1994.
2. Charles C. Sims. Computation with finitely presented groups. Cambridge University Press, 1994.
CITE THIS NOTEBOOK
CITE THIS NOTEBOOK
Monoids, String-Rewriting, Confluence, and the Knuth-Bendix Algorithm
by Vivaan Daga
Wolfram Community, STAFF PICKS, July 11, 2024
https://community.wolfram.com/groups/-/m/t/3217387
by Vivaan Daga
Wolfram Community, STAFF PICKS, July 11, 2024
https://community.wolfram.com/groups/-/m/t/3217387