The KnuthBendix class
Class containing an implementation of the Knuth-Bendix Algorithm.
On this page we describe the functionality relating to the Knuth-Bendix
algorithm for semigroups and monoids in libsemigroups_pybind11
. This page
contains details of the member functions of the class
KnuthBendix
. This class is used to represent a string rewriting system
defining a 1- or 2-sided congruence on a finitely presented monoid or
semigroup.
KnuthBendix
inherits from Runner
and has the
nested class KnuthBendix.options
.
See also
Important
The class KnuthBendix
has all the methods of the Runner
class but, for
boring technical reasons, is not a subclass of Runner
. If thing
is
an instance of KnuthBendix
, then you can use thing
as if it were an instance
of Runner
but isinstance(thing, Runner)
will return False
.
>>> from libsemigroups_pybind11 import (KnuthBendix, Presentation,
... presentation, congruence_kind)
>>> p = Presentation("abc")
>>> presentation.add_rule(p, "aaaa", "a")
>>> presentation.add_rule(p, "bbbb", "b")
>>> presentation.add_rule(p, "cccc", "c")
>>> presentation.add_rule(p, "abab", "aaa")
>>> presentation.add_rule(p, "bcbc", "bbb")
>>> kb = KnuthBendix(congruence_kind.twosided, p)
>>> not kb.confluent()
True
>>> kb.run()
>>> kb.number_of_active_rules()
31
>>> kb.confluent()
True
Contents
Class containing an implementation of the Knuth-Bendix Algorithm. |
|
Return a copy of the active rules. |
|
Add a generating pair. |
|
Overloaded function. |
|
Check confluence of the current rules. |
|
Check if the current system knows the state of confluence of the current rules. |
|
Check containment of a pair of words. |
|
Copy a |
|
Check whether a pair of words is already known to belong to the congruence. |
|
Get the generating pairs of the congruence. |
|
Return the Gilman |
|
Return the node labels of the Gilman |
|
Overloaded function. |
|
The kind of the congruence (1- or 2-sided). |
|
Overloaded function. |
|
Overloaded function. |
|
Overloaded function. |
|
Return the current number of active rules. |
|
Compute the number of classes in the congruence. |
|
Returns the number of generating pairs. |
|
Return the current number of inactive rules. |
|
Overloaded function. |
|
Get the presentation used to define a |
|
Reduce a word. |
|
Reduce a word. |
|
Return the number of rules that have been created |
Full API
- class KnuthBendix
- __init__(*args, **kwargs)
Overloaded function.
- __init__(word: type, rewriter: str) None
Default constructor.
This function default constructs an uninitialised
KnuthBendix
instance.- Keyword Arguments:
word (type) – the type of words to use, must be either
str
orlist[int]
rewriter (str) – the type of rewriter to use, must be either
"RewriteTrie"
or"RewriteFromLeft"
.
- __init__(self: KnuthBendix, knd: congruence_kind, p: Presentation) None
Construct from
congruence_kind
andPresentation
.This function constructs a
KnuthBendix
instance representing a congruence of kind knd over the semigroup or monoid defined by the presentation p.- Parameters:
knd (congruence_kind) – the kind (onesided or twosided) of the congruence.
p (Presentation) – the presentation.
- Raises:
LibsemigroupsError – if p is not valid.
- active_rules(self: KnuthBendix) collections.abc.Iterator[tuple[str, str]]
Return a copy of the active rules.
This member function returns an iterator yielding of the pairs of strings which represent the rewriting rules. The first entry in every such pair is greater than the second according to the reduction ordering of the
KnuthBendix
instance. The rules are sorted according to the reduction ordering used by the rewriting system, on the first entry.- Returns:
An iterator yielding the currently active rules.
- Return type:
- add_generating_pair(self: KnuthBendix, u: list[int] | str, v: list[int] | str) KnuthBendix
Add a generating pair.
This function adds a generating pair to the congruence represented by a
KnuthBendix
instance.- Parameters:
- Returns:
self.
- Return type:
- Raises:
LibsemigroupsError – if any of the values in u or v is out of range, i.e. they do not belong to
presentation().alphabet()
andPresentation.throw_if_letter_not_in_alphabet
raises.LibsemigroupsError – if
Runner.started
returnsTrue
.
- check_confluence_interval(*args, **kwargs)
Overloaded function.
- check_confluence_interval(self: KnuthBendix) int
Return the interval at which confluence is checked.
The function
Runner.run
periodically checks if the system is already confluent. This function can be used to return how frequently this happens. It is the number of new overlaps that should be considered before checking confluence.- Returns:
The interval at which confluence is checked.
- Return type:
See also
- check_confluence_interval(self: KnuthBendix, val: int | LimitMax) KnuthBendix
Set the interval at which confluence is checked.
The function
Runner.run()
periodically checks if the system is already confluent. This function can be used to set how frequently this happens. It is the number of new overlaps that should be considered before checking confluence. Setting this value too low can adversely affect the performance ofRunner.run
.The default value is
4096
, and should be set toLIMIT_MAX
ifRunner.run
should never check if the system is already confluent.- Parameters:
- Returns:
self.
- Return type:
- confluent(self: KnuthBendix) bool
Check confluence of the current rules.
- Returns:
True
if theKnuthBendix
instance is confluent andFalse
if it is not.- Return type:
- confluent_known(self: KnuthBendix) bool
Check if the current system knows the state of confluence of the current rules.
- Returns:
True
if the confluence of the rules in theKnuthBendix
instance is known, andFalse
if it is not.- Return type:
- contains(self: KnuthBendix, u: list[int] | str, v: list[int] | str) bool
Check containment of a pair of words.
This function checks whether or not the words u and v are contained in the congruence represented by a
KnuthBendix
instance.- Parameters:
- Returns:
Whether or not the pair belongs to the congruence.
- Return type:
- Raises:
LibsemigroupsError – if any of the values in u or v is out of range, i.e. they do not belong to
presentation().alphabet()
andPresentation.throw_if_letter_not_in_alphabet
raises.
- copy(self: KnuthBendix) KnuthBendix
Copy a
KnuthBendix
object.- Returns:
A copy.
- Return type:
- currently_contains(self: KnuthBendix, u: list[int] | str, v: list[int] | str) tril
Check whether a pair of words is already known to belong to the congruence.
This function checks whether or not the words u and v are already known to be contained in the congruence represented by a
KnuthBendix
instance. This function performs no enumeration, so it is possible for the words to be contained in the congruence, but that this is not currently known.- Parameters:
- Returns:
tril.true
if the words are known to belong to the congruence;tril.false
if the words are known to not belong to the congruence;tril.unknown
otherwise.
- Return type:
- Raises:
LibsemigroupsError – if any of the values in u or v is out of range, i.e. they do not belong to
presentation().alphabet()
andPresentation.throw_if_letter_not_in_alphabet
raises.
- generating_pairs(self: KnuthBendix) list[list[int] | str]
Get the generating pairs of the congruence.
This function returns the generating pairs of the congruence as added via
KnuthBendix.add_generating_pair
.
- gilman_graph(self: KnuthBendix) WordGraph
Return the Gilman
WordGraph
.The Gilman
WordGraph
is a digraph where the labels of the paths from the initial node (corresponding to the empty word) correspond to the short-lex normal forms of the semigroup elements.The semigroup is finite if the graph is acyclic, and infinite otherwise.
Warning
This will terminate when the
KnuthBendix
instance is reduced and confluent, which might be never.See also
- gilman_graph_node_labels(self: KnuthBendix) list[str]
Return the node labels of the Gilman
WordGraph
Return the node labels of the Gilman
WordGraph
, corresponding to the unique prefixes of the left-hand sides of the rules of the rewriting system.See also
- init(*args, **kwargs)
Overloaded function.
- init(self: KnuthBendix) KnuthBendix
Re-initialize a
KnuthBendix
instance.This function puts a
KnuthBendix
instance back into the state that it would have been in if it had just been newly default constructed.- Returns:
self.
- Return type:
- init(self: KnuthBendix, knd: congruence_kind, p: Presentation) KnuthBendix
Re-initialize a
KnuthBendix
instance.This function re-initializes a
KnuthBendix
instance as if it had been newly constructed from knd and p.- Parameters:
knd (
congruence_kind
) – the kind (onesided or twosided) of the congruence.p (Presentation) – the presentation.
- Returns:
self.
- Return type:
- Raises:
LibsemigroupsError – if p is not valid.
- kind(self: Congruence | Kambites | KnuthBendix | ToddCoxeter) congruence_kind
The kind of the congruence (1- or 2-sided).
This function returns the kind of the congruence represented by self. See
congruence_kind
for details.- Returns:
The kind of the congruence (1- or 2-sided).
- Return type:
- Complexity:
Constant.
- max_overlap(*args, **kwargs)
Overloaded function.
- max_overlap(self: KnuthBendix) int | PositiveInfinity
Return the maximum length of overlaps to be considered.
This function returns the maximum length of the overlap of two left hand sides of rules that should be considered in
Runner.run
.- Returns:
The maximum overlap length
- Return type:
See also
- max_overlap(self: KnuthBendix, val: int | PositiveInfinity) KnuthBendix
Set the maximum length of overlaps to be considered.
This function can be used to specify the maximum length of the overlap of two left hand sides of rules that should be considered in
Runner.run
.If this value is less than the longest left hand side of a rule, then
Runner.run
can terminate without the system being confluent.- Parameters:
val (int | PositiveInfinity) – The new value of the maximum overlap length.
- Returns:
self.
- Return type:
See also
- max_pending_rules(*args, **kwargs)
Overloaded function.
- max_pending_rules(self: KnuthBendix) int
Return the number of pending rules that must accumulate before they are reduced, processed, and added to the system.
The default value is
128
. A value of1
meansRunner.run
should attempt to add each rule as it is created without waiting for rules to accumulate.- Returns:
The batch size.
- Return type:
See also
- max_pending_rules(self: KnuthBendix, val: int) KnuthBendix
Specify the number of pending rules that must accumulate before they are reduced, processed, and added to the system.
The default value is
128
, and should be set to1
ifRunner.run
should attempt to add each rule as it is created without waiting for rules to accumulate.- Parameters:
val (int) – The new value of the batch size.
- Returns:
self.
- Return type:
See also
- max_rules(*args, **kwargs)
Overloaded function.
- max_rules(self: KnuthBendix) int | PositiveInfinity
Return the maximum number of rules.
This member function returns the (approximate) maximum number of rules that the system should contain. If this is number is exceeded in calls to
Runner.run
orknuth_bendix.by_overlap_length
, then they will terminate and the system may not be confluent.- Returns:
The maximum number of rules the system should contain.
- Return type:
See also
- max_rules(self: KnuthBendix, val: int | PositiveInfinity) KnuthBendix
Set the maximum number of rules.
This member function sets the (approximate) maximum number of rules that the system should contain. If this is number is exceeded in calls to
Runner.run
orknuth_bendix.by_overlap_length
, then they will terminate and the system may not be confluent.By default this value is
POSITIVE_INFINITY
.- Parameters:
val (int | PositiveInfinity) – The maximum number of rules.
- Returns:
self.
- Return type:
See also
- number_of_active_rules(self: KnuthBendix) int
Return the current number of active rules.
- Returns:
The current number of active rules.
- Return type:
- number_of_classes(self: KnuthBendix) int | PositiveInfinity
Compute the number of classes in the congruence. This function computes the number of classes in the congruence represented by a
KnuthBendix
instance.- Returns:
The number of congruence classes of a
KnuthBendix
instance if this number is finite, orPOSITIVE_INFINITY
in some cases if this number is not finite.- Return type:
- number_of_generating_pairs(self: Congruence | Kambites | KnuthBendix | ToddCoxeter) int
Returns the number of generating pairs.
This function returns the number of generating pairs of the congruence.
- Returns:
The number of generating pairs.
- Return type:
- Complexity:
Constant.
- number_of_inactive_rules(self: KnuthBendix) int
Return the current number of inactive rules.
- Returns:
The current number of inactive rules.
- Return type:
- overlap_policy(*args, **kwargs)
Overloaded function.
- overlap_policy(self: KnuthBendix) KnuthBendix.options.overlap
Return the overlap policy.
This function returns the way that the length of an overlap of two words in the system is measured.
- Returns:
The overlap policy.
- Return type:
See also
- overlap_policy(self: KnuthBendix, val: KnuthBendix.options.overlap) KnuthBendix
Set the overlap policy.
This function can be used to determine the way that the length of an overlap of two words in the system is measured.
- Parameters:
val (KnuthBendix.options.overlap) – The overlap policy.
- Returns:
self.
- Return type:
See also
- presentation(self: KnuthBendix) Presentation
Get the presentation used to define a
KnuthBendix
instance (if any). If aKnuthBendix
instance is constructed or initialised using a presentation, then this presentation is returned by this function.- Returns:
The presentation used to construct or initialise a
KnuthBendix
instance.- Return type:
- reduce(self: KnuthBendix, w: list[int] | str) list[int] | str
Reduce a word.
This function triggers a full enumeration of an
KnuthBendix
object and then reduces the word w. As such the returned word is a normal form for the input word.- Parameters:
- Returns:
A normal form for the input word.
- Return type:
- Raises:
LibsemigroupsError – if any of the values in w is out of range, i.e. they do not belong to
presentation().alphabet()
andPresentation.throw_if_letter_not_in_alphabet
raises.
- reduce_no_run(self: KnuthBendix, w: list[int] | str) list[int] | str
Reduce a word.
If
Runner.finished
returnsTrue
, then this function returns a normal form for the input word w.Otherwise, this function returns the word w rewritten according to the current rules in the
KnuthBendix
instance.- Parameters:
- Returns:
A word equivalent to the input word.
- Return type:
- Raises:
LibsemigroupsError – if any of the values in w is out of range, i.e. they do not belong to
presentation().alphabet()
andPresentation.throw_if_letter_not_in_alphabet
raises.
- total_rules(self: KnuthBendix) int
Return the number of rules that have been created
Return the total number of Rule instances that have been created whilst whilst the Knuth-Bendix algorithm has been running. Note that this is not the sum of
number_of_active_rules
andnumber_of_inactive_rules
, due to the re-initialisation of rules where possible.- Returns:
The total number of rules.
- Return type: