27#ifndef LIBSEMIGROUPS_DETAIL_KNUTH_BENDIX_IMPL_HPP_
28#define LIBSEMIGROUPS_DETAIL_KNUTH_BENDIX_IMPL_HPP_
43#include <unordered_map>
47#include "libsemigroups/constants.hpp"
48#include "libsemigroups/debug.hpp"
49#include "libsemigroups/obvinf.hpp"
50#include "libsemigroups/order.hpp"
51#include "libsemigroups/presentation.hpp"
52#include "libsemigroups/ranges.hpp"
53#include "libsemigroups/types.hpp"
54#include "libsemigroups/word-graph.hpp"
55#include "libsemigroups/word-range.hpp"
57#include "cong-common-class.hpp"
60#include "rewriters.hpp"
67 template <
typename KnuthBendix_>
133 template <
typename Rewriter = detail::RewriteTrie,
134 typename ReductionOrder = ShortLexCompare>
135 class KnuthBendixImpl :
public CongruenceCommon {
137 friend class ::libsemigroups::detail::KBE<KnuthBendixImpl>;
144 struct OverlapMeasure {
146 operator()(detail::Rule
const*,
147 detail::Rule
const* examples,
148 detail::internal_string_type::const_iterator
const&)
150 virtual ~OverlapMeasure() {}
162 using rule_type = std::pair<std::string, std::string>;
163 using native_word_type = std::string;
164 using rewriter_type = Rewriter;
171 enum class overlap { ABC = 0, AB_BC = 1, MAX_AB_BC = 2 };
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<std::
string> _gilman_graph_node_labels;
212 bool _internal_is_same_as_external;
213 std::unique_ptr<OverlapMeasure> _overlap_measure;
215 mutable Rewriter _rewriter;
217 mutable Stats _stats;
218 mutable std::
string _tmp_element1;
226 KnuthBendixImpl& init();
227 KnuthBendixImpl(KnuthBendixImpl const& that);
229 KnuthBendixImpl(KnuthBendixImpl&&);
231 KnuthBendixImpl& operator=(KnuthBendixImpl const&);
233 KnuthBendixImpl& operator=(KnuthBendixImpl&&);
250 void init_from_generating_pairs();
251 void init_from_internal_presentation();
262 template <typename Iterator1,
266 KnuthBendixImpl& add_generating_pair_no_checks(Iterator1 first1,
270 LIBSEMIGROUPS_ASSERT(!
started());
271 return CongruenceCommon::add_internal_generating_pair_no_checks<
272 KnuthBendixImpl>(first1, last1, first2, last2);
275 template <
typename Iterator1,
283 LIBSEMIGROUPS_ASSERT(!
started());
284 return CongruenceCommon::add_generating_pair<KnuthBendixImpl>(
285 first1, last1, first2, last2);
334 template <
typename Iterator1,
341 Iterator4 last2)
const;
346 template <
typename Iterator1,
350 [[nodiscard]]
tril currently_contains(Iterator1 first1,
353 Iterator4 last2)
const {
354 return CongruenceCommon::currently_contains<KnuthBendixImpl>(
355 first1, last1, first2, last2);
374 template <
typename Iterator1,
382 return CongruenceCommon::contains_no_checks<KnuthBendixImpl>(
383 first1, last1, first2, last2);
389 template <
typename Iterator1,
393 [[nodiscard]]
bool contains(Iterator1 first1,
397 return CongruenceCommon::contains<KnuthBendixImpl>(
398 first1, last1, first2, last2);
425 template <
typename OutputIterator,
426 typename InputIterator1,
427 typename InputIterator2>
429 InputIterator1 first,
430 InputIterator2 last)
const;
435 template <
typename OutputIterator,
436 typename InputIterator1,
437 typename InputIterator2>
438 OutputIterator reduce_no_run(OutputIterator d_first,
439 InputIterator1 first,
440 InputIterator2 last)
const {
441 return CongruenceCommon::reduce_no_run<KnuthBendixImpl>(
442 d_first, first, last);
466 template <
typename OutputIterator,
467 typename InputIterator1,
468 typename InputIterator2>
470 InputIterator1 first,
471 InputIterator2 last) {
472 return CongruenceCommon::reduce_no_checks<KnuthBendixImpl>(
473 d_first, first, last);
479 template <
typename OutputIterator,
480 typename InputIterator1,
481 typename InputIterator2>
482 OutputIterator reduce(OutputIterator d_first,
483 InputIterator1 first,
484 InputIterator2 last) {
485 return CongruenceCommon::reduce<KnuthBendixImpl>(d_first, first, last);
515 _settings.max_pending_rules = val;
539 return _settings.max_pending_rules;
565 _settings.check_confluence_interval = val;
589 return _settings.check_confluence_interval;
614 _settings.max_overlap = val;
637 return _settings.max_overlap;
661 _settings.max_rules = val;
686 return _settings.max_rules;
725 return _settings.overlap_policy;
733 template <
typename Iterator1,
typename Iterator2>
734 void throw_if_letter_not_in_alphabet(Iterator1 first,
735 Iterator2 last)
const {
736 internal_presentation().throw_if_letter_not_in_alphabet(first, last);
740 internal_presentation() const noexcept {
741 return _presentation;
778 return _rewriter.number_of_inactive_rules();
800 return _rewriter.stats().total_rules;
808 [[nodiscard]]
auto active_rules();
872 [[nodiscard]]
std::vector<
std::
string> const& gilman_graph_node_labels() {
874 return _gilman_graph_node_labels;
882 void report_presentation()
const;
883 void report_before_run();
884 void report_progress_from_thread(std::atomic_bool
const&);
885 void report_after_run();
887 void stats_check_point();
890 uint_to_internal_char(
size_t a);
891 [[nodiscard]]
static size_t
895 uint_to_internal_string(
size_t i);
915 [[nodiscard]]
size_t max_active_word_length()
const {
916 return _rewriter.max_active_word_length();
919 void run_real(std::atomic_bool&);
920 [[nodiscard]]
bool stop_running()
const;
926 void run_impl()
override;
927 bool finished_impl()
const override;
952#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
953 template <
typename Word,
typename Rewriter,
typename ReductionOrder>
956 KnuthBendix<Word, Rewriter, ReductionOrder>
const& kb);
958 template <
typename Rewriter,
typename ReductionOrder>
961 detail::KnuthBendixImpl<Rewriter, ReductionOrder>
const& kb);
982#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
983 template <
typename Word,
typename Rewriter,
typename ReductionOrder>
987 template <
typename Rewriter,
typename ReductionOrder>
994 template <
typename Result,
typename Rewriter,
typename ReductionOrder>
995 auto to(detail::KnuthBendixImpl<Rewriter, ReductionOrder>& kb)
997 std::is_same_v<Presentation<typename Result::word_type>, Result>,
1002#include "knuth-bendix-impl.tpp"
For an implementation of presentations for semigroups or monoids.
Definition presentation.hpp:102
bool started() const noexcept
Check if run has been called at least once before.
Definition runner.hpp:578
Class for representing word graphs.
Definition word-graph.hpp:82
For a rewriting rule.
Definition rewriters.hpp:83
std::string to_human_readable_repr(AhoCorasick const &ac)
Return a string representation.
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
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:777
bool confluent_known() const noexcept
Check if the current system knows the state of confluence of the current rules.
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:799
OutputIterator reduce_no_checks(OutputIterator d_first, InputIterator1 first, InputIterator2 last)
Reduce a word with no checks.
Definition knuth-bendix-impl.hpp:469
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:378
tril currently_contains_no_checks(Iterator1 first1, Iterator2 last1, Iterator3 first2, Iterator4 last2) const
Check containment of a pair of words via iterators.
OutputIterator reduce_no_run_no_checks(OutputIterator d_first, InputIterator1 first, InputIterator2 last) const
Reduce a word with no run and no checks.
KnuthBendixImpl & check_confluence_interval(size_t val)
Definition knuth-bendix-impl.hpp:564
KnuthBendixImpl & max_pending_rules(size_t val)
Definition knuth-bendix-impl.hpp:514
KnuthBendixImpl & max_overlap(size_t val)
Definition knuth-bendix-impl.hpp:613
KnuthBendixImpl & max_rules(size_t val)
Definition knuth-bendix-impl.hpp:660
KnuthBendixImpl & overlap_policy(typename options::overlap val)
Presentation(Presentation< Word > const &) -> Presentation< Word >
Deduction guide.
std::string internal_string_type
Definition rewriters.hpp:58
char external_char_type
Definition rewriters.hpp:63
std::string external_string_type
Definition rewriters.hpp:53
char internal_char_type
Definition rewriters.hpp:68
auto to(detail::KnuthBendixImpl< Rewriter, ReductionOrder > &kb) -> std::enable_if_t< std::is_same_v< Presentation< typename Result::word_type >, Result >, Result >
No doc.
std::vector< letter_type > word_type
Type for a word over the generators of a semigroup.
Definition types.hpp:101
tril
Enum to indicate true, false or not currently knowable.
Definition types.hpp:56
congruence_kind
Enum to indicate the sided-ness of a congruence.
Definition types.hpp:69
Namespace for everything in the libsemigroups library.
Definition action.hpp:44