27#ifndef LIBSEMIGROUPS_DETAIL_KNUTH_BENDIX_IMPL_HPP_
28#define LIBSEMIGROUPS_DETAIL_KNUTH_BENDIX_IMPL_HPP_
43#include <unordered_map>
47#include "libsemigroups/adapters.hpp"
48#include "libsemigroups/constants.hpp"
49#include "libsemigroups/debug.hpp"
50#include "libsemigroups/obvinf.hpp"
51#include "libsemigroups/order.hpp"
52#include "libsemigroups/paths-count.hpp"
53#include "libsemigroups/presentation.hpp"
54#include "libsemigroups/ranges.hpp"
55#include "libsemigroups/types.hpp"
56#include "libsemigroups/word-graph-helpers.hpp"
57#include "libsemigroups/word-graph.hpp"
58#include "libsemigroups/word-range.hpp"
60#include "cong-common-class.hpp"
63#include "rewriters.hpp"
70 template <
typename KnuthBendix_>
136 template <
typename Rewriter = detail::RewriteTrie,
137 typename ReductionOrder = ShortLexCompare>
138 class KnuthBendixImpl :
public CongruenceCommon {
144 using native_word_type =
typename Rewriter::native_word_type;
145 using rule_type = std::pair<native_word_type, native_word_type>;
146 using rewriter_type = Rewriter;
153 enum class overlap { ABC = 0, AB_BC = 1, MAX_AB_BC = 2 };
162 struct OverlapMeasure {
164 operator()(detail::Rule
const*,
165 detail::Rule
const* examples,
166 typename native_word_type::const_iterator
const&)
168 virtual ~OverlapMeasure() {}
177 Settings& init() noexcept;
179 Settings(Settings const&) noexcept = default;
180 Settings(Settings&&) noexcept = default;
181 Settings& operator=(Settings const&) noexcept = default;
182 Settings& operator=(Settings&&) noexcept = default;
184 size_t max_pending_rules;
185 size_t check_confluence_interval;
188 typename options::overlap overlap_policy;
193 Stats& init() noexcept;
195 Stats(Stats const&) noexcept = default;
196 Stats(Stats&&) noexcept = default;
197 Stats& operator=(Stats const&) noexcept = default;
198 Stats& operator=(Stats&&) noexcept = default;
200 size_t prev_active_rules;
201 size_t prev_inactive_rules;
202 size_t prev_total_rules;
209 bool _gen_pairs_initted;
210 WordGraph<uint32_t> _gilman_graph;
211 std::vector<native_word_type> _gilman_graph_node_labels;
212 std::unique_ptr<OverlapMeasure> _overlap_measure;
214 mutable Rewriter _rewriter;
216 mutable Stats _stats;
217 mutable native_word_type _tmp_element1;
225 KnuthBendixImpl& init();
226 KnuthBendixImpl(KnuthBendixImpl const& that);
228 KnuthBendixImpl(KnuthBendixImpl&&);
230 KnuthBendixImpl& operator=(KnuthBendixImpl const&);
232 KnuthBendixImpl& operator=(KnuthBendixImpl&&);
251 void init_from_generating_pairs();
252 void init_from_internal_presentation();
263 template <typename Iterator1,
267 KnuthBendixImpl& add_generating_pair_no_checks(Iterator1 first1,
271 LIBSEMIGROUPS_ASSERT(!
started());
272 return CongruenceCommon::add_internal_generating_pair_no_checks<
273 KnuthBendixImpl>(first1, last1, first2, last2);
276 template <
typename Iterator1,
284 LIBSEMIGROUPS_ASSERT(!
started());
285 return CongruenceCommon::add_generating_pair<KnuthBendixImpl>(
286 first1, last1, first2, last2);
335 template <
typename Iterator1,
342 Iterator4 last2)
const;
347 template <
typename Iterator1,
351 [[nodiscard]]
tril currently_contains(Iterator1 first1,
354 Iterator4 last2)
const {
355 return CongruenceCommon::currently_contains<KnuthBendixImpl>(
356 first1, last1, first2, last2);
375 template <
typename Iterator1,
383 return CongruenceCommon::contains_no_checks<KnuthBendixImpl>(
384 first1, last1, first2, last2);
390 template <
typename Iterator1,
394 [[nodiscard]]
bool contains(Iterator1 first1,
398 return CongruenceCommon::contains<KnuthBendixImpl>(
399 first1, last1, first2, last2);
406 template <
typename OutputIterator,
407 typename InputIterator1,
408 typename InputIterator2>
409 OutputIterator reduce_no_run_no_checks(OutputIterator d_first,
410 InputIterator1 first,
411 InputIterator2 last)
const;
416 template <
typename OutputIterator,
417 typename InputIterator1,
418 typename InputIterator2>
419 OutputIterator reduce_no_run(OutputIterator d_first,
420 InputIterator1 first,
421 InputIterator2 last)
const {
422 return CongruenceCommon::reduce_no_run<KnuthBendixImpl>(
423 d_first, first, last);
426 template <
typename OutputIterator,
427 typename InputIterator1,
428 typename InputIterator2>
429 OutputIterator reduce_no_checks(OutputIterator d_first,
430 InputIterator1 first,
431 InputIterator2 last) {
432 return CongruenceCommon::reduce_no_checks<KnuthBendixImpl>(
433 d_first, first, last);
439 template <
typename OutputIterator,
440 typename InputIterator1,
441 typename InputIterator2>
442 OutputIterator
reduce(OutputIterator d_first,
443 InputIterator1 first,
444 InputIterator2 last) {
445 return CongruenceCommon::reduce<KnuthBendixImpl>(d_first, first, last);
475 _settings.max_pending_rules = val;
499 return _settings.max_pending_rules;
525 _settings.check_confluence_interval = val;
549 return _settings.check_confluence_interval;
574 _settings.max_overlap = val;
597 return _settings.max_overlap;
621 _settings.max_rules = val;
646 return _settings.max_rules;
685 return _settings.overlap_policy;
693 template <
typename Iterator1,
typename Iterator2>
694 void throw_if_letter_not_in_alphabet(Iterator1 first,
695 Iterator2 last)
const {
696 internal_presentation().throw_if_letter_not_in_alphabet(first, last);
700 internal_presentation() const noexcept {
701 return _presentation;
738 return _rewriter.number_of_inactive_rules();
762 return _rewriter.number_of_pending_rules();
784 return _rewriter.stats().total_rules;
787 Rewriter& rewriter() noexcept {
796 [[nodiscard]]
auto active_rules();
823 void rewrite_inplace(native_word_type& w);
826 [[nodiscard]] native_word_type rewrite(native_word_type w) {
883 [[nodiscard]]
std::vector<native_word_type> const&
884 gilman_graph_node_labels() {
886 return _gilman_graph_node_labels;
891 void run_impl()
override;
898 void report_presentation()
const;
899 void report_before_run();
900 void report_progress_from_thread(std::atomic_bool
const&);
901 void report_after_run();
903 void stats_check_point();
905 void add_octo(native_word_type& w)
const;
906 void rm_octo(native_word_type& w)
const;
908 void add_rule_impl(native_word_type
const& p, native_word_type
const& q);
910 void overlap(detail::Rule
const* u, detail::Rule
const* v);
912 [[nodiscard]]
size_t max_active_word_length()
const {
913 return _rewriter.max_active_word_length();
916 void run_real(std::atomic_bool&);
917 [[nodiscard]]
bool stop_running()
const;
923 bool finished_impl()
const override;
948#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
949 template <
typename Word,
typename Rewriter,
typename ReductionOrder>
952 KnuthBendix<Word, Rewriter, ReductionOrder>
const& kb);
954 template <
typename Rewriter,
typename ReductionOrder>
957 detail::KnuthBendixImpl<Rewriter, ReductionOrder>
const& kb);
978#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
979 template <
typename Word,
typename Rewriter,
typename ReductionOrder>
983 template <
typename Rewriter,
typename ReductionOrder>
990 template <
typename Result,
typename Rewriter,
typename ReductionOrder>
991 auto to(detail::KnuthBendixImpl<Rewriter, ReductionOrder>& kb)
993 std::is_same_v<Presentation<typename Result::word_type>, Result>,
998#include "knuth-bendix-impl.tpp"
For an implementation of presentations for semigroups or monoids.
Definition presentation.hpp:103
bool started() const noexcept
Check if run has been called at least once before.
Definition runner.hpp:626
Class for representing word graphs.
Definition word-graph.hpp:83
std::string to_human_readable_repr(Action< Element, Point, Func, Traits, LeftOrRight > const &action)
Return a human readable representation of an Action object.
std::ostringstream & operator<<(std::ostringstream &os, BMat8 const &x)
Insertion operator.
void add_generating_pair(Thing &thing, typename Thing::native_word_type const &u, typename Thing::native_word_type const &v)
Helper for adding a generating pair of words.
Definition cong-common-helpers.hpp:200
Thing::native_word_type reduce(Thing const &thing, typename Thing::native_word_type const &w)
Reduce a word.
size_t number_of_active_rules() const noexcept
Return the current number of active rules in the KnuthBendix instance.
WordGraph< uint32_t > const & gilman_graph()
size_t number_of_inactive_rules() const noexcept
Definition knuth-bendix-impl.hpp:737
bool confluent_known() const noexcept
Check if the current system knows the state of confluence of the current rules.
size_t number_of_pending_rules() const noexcept
Return the number of pending rules.
Definition knuth-bendix-impl.hpp:761
bool confluent() const
Check confluence of the current rules.
size_t total_rules() const noexcept
Return the number of rules that KnuthBendix has created.
Definition knuth-bendix-impl.hpp:783
KnuthBendixImpl & process_pending_rules()
Process any pending rules.
Definition knuth-bendix-impl.hpp:816
uint64_t number_of_classes()
Compute the number of classes in the congruence.
bool contains_no_checks(Iterator1 first1, Iterator2 last1, Iterator3 first2, Iterator4 last2)
Check containment of a pair of words via iterators.
Definition knuth-bendix-impl.hpp:379
tril currently_contains_no_checks(Iterator1 first1, Iterator2 last1, Iterator3 first2, Iterator4 last2) const
Check containment of a pair of words via iterators.
KnuthBendixImpl & check_confluence_interval(size_t val)
Definition knuth-bendix-impl.hpp:524
KnuthBendixImpl & max_pending_rules(size_t val)
Definition knuth-bendix-impl.hpp:474
KnuthBendixImpl & max_overlap(size_t val)
Definition knuth-bendix-impl.hpp:573
KnuthBendixImpl & max_rules(size_t val)
Definition knuth-bendix-impl.hpp:620
KnuthBendixImpl & overlap_policy(typename options::overlap val)
Presentation(Presentation< Word > const &) -> Presentation< Word >
Deduction guide.
tril
Enum to indicate true, false or not currently knowable.
Definition types.hpp:54
congruence_kind
Enum to indicate the sided-ness of a congruence.
Definition types.hpp:67
Namespace for everything in the libsemigroups library.
Definition action.hpp:44
auto to(detail::KnuthBendixImpl< Rewriter, ReductionOrder > &kb) -> std::enable_if_t< std::is_same_v< Presentation< typename Result::word_type >, Result >, Result >
No doc.