libsemigroups  v3.1.2
C++ library for semigroups and monoids
Loading...
Searching...
No Matches
knuth-bendix-class.hpp
1//
2// libsemigroups - C++ library for semigroups and monoids
3// Copyright (C) 2025 James D. Mitchell + Joseph Edwards
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <http://www.gnu.org/licenses/>.
17//
18
19// This file contains the declaration of the class template KnuthBendix which
20// is really just a facade for detail::KnuthBendixImpl.
21
22#ifndef LIBSEMIGROUPS_KNUTH_BENDIX_CLASS_HPP_
23#define LIBSEMIGROUPS_KNUTH_BENDIX_CLASS_HPP_
24
25#include <algorithm> // for for_each
26#include <utility> // for move
27#include <vector> // for vector
28
29#include "order.hpp" // for ShortLexCompare
30#include "presentation.hpp" // for Presentation
31
32#include "detail/citow.hpp" // for citow
33#include "detail/cong-common-class.hpp" // for CongruenceCommon
34#include "detail/knuth-bendix-impl.hpp" // for KnuthBendixImpl
35#include "detail/rewriters.hpp" // for RewriteTrie
36
37namespace libsemigroups {
38 enum class congruence_kind;
39 enum class tril;
40
49
93 template <typename Word,
94 typename Rewriter = detail::RewriteTrie,
95 typename ReductionOrder = ShortLexCompare>
96 class KnuthBendix : public detail::KnuthBendixImpl<Rewriter, ReductionOrder> {
97 private:
98 using KnuthBendixImpl_ = detail::KnuthBendixImpl<Rewriter, ReductionOrder>;
99
100 bool _extra_letter_added;
101 std::vector<Word> _generating_pairs;
102 Presentation<Word> _presentation;
103
104 // defined in detail/kbe.hpp
105 friend class ::libsemigroups::detail::KBE<KnuthBendix>;
106
107 public:
108#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
115 struct options {
124 enum class overlap {
126 ABC = 0,
128 AB_BC = 1,
131 };
132 };
133#endif
134
142 using native_word_type = Word;
143
150
158
169 // TODO(1) to tpp file
171 _extra_letter_added = false;
172 _generating_pairs.clear();
173 _presentation.init();
174 KnuthBendixImpl_::init();
175 return *this;
176 }
177
190
197
204
211
212 ~KnuthBendix();
213
216 init(knd, std::move(p));
217 }
218
220 KnuthBendix& init(congruence_kind knd, Presentation<Word>&& p);
221
242 // call the rval ref constructor
243 : KnuthBendix(knd, Presentation<Word>(p)) {}
244
267 // call the rval ref init
268 return init(knd, Presentation<Word>(p));
269 }
270
298 template <typename Iterator1, typename Iterator2>
299 void throw_if_letter_not_in_alphabet(Iterator1 first, Iterator2 last) const;
300
301#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
316 [[nodiscard]] congruence_kind kind() const noexcept;
317
333 [[nodiscard]] size_t number_of_generating_pairs() const noexcept;
334#endif
335
347 [[nodiscard]] std::vector<Word> const& generating_pairs() const noexcept {
348 return _generating_pairs;
349 }
350
375 [[nodiscard]] Presentation<Word> const& presentation() const noexcept {
376 return _presentation;
377 }
378
380 // KnuthBendix - interface requirements - add_generating_pair
382
400 template <typename Iterator1,
401 typename Iterator2,
402 typename Iterator3,
403 typename Iterator4>
405 Iterator2 last1,
406 Iterator3 first2,
407 Iterator4 last2);
408
424 template <typename Iterator1,
425 typename Iterator2,
426 typename Iterator3,
427 typename Iterator4>
429 Iterator2 last1,
430 Iterator3 first2,
431 Iterator4 last2);
432
434 // KnuthBendix - interface requirements - contains
436
437 // TODO: contains_no_checks is implemented
438 // and documented in KnuthBendixImpl
439
440 template <typename Iterator1,
441 typename Iterator2,
442 typename Iterator3,
443 typename Iterator4>
444 tril currently_contains_no_checks(Iterator1 first1,
445 Iterator2 last1,
446 Iterator3 first2,
447 Iterator4 last2) const {
449 detail::citow(this, first1),
450 detail::citow(this, last1),
451 detail::citow(this, first2),
452 detail::citow(this, last2));
453 }
454
475 template <typename Iterator1,
476 typename Iterator2,
477 typename Iterator3,
478 typename Iterator4>
479 tril currently_contains(Iterator1 first1,
480 Iterator2 last1,
481 Iterator3 first2,
482 Iterator4 last2) const {
483 // Call CongruenceCommon version so that we perform bound checks
484 // in KnuthBendix and not KnuthBendixImpl_
485 return detail::CongruenceCommon::currently_contains<KnuthBendix>(
486 first1, last1, first2, last2);
487 }
488
507 template <typename Iterator1,
508 typename Iterator2,
509 typename Iterator3,
510 typename Iterator4>
511 bool contains(Iterator1 first1,
512 Iterator2 last1,
513 Iterator3 first2,
514 Iterator4 last2);
515
517 // KnuthBendix - interface requirements - reduce
519
540 template <typename OutputIterator,
541 typename InputIterator1,
542 typename InputIterator2>
543 OutputIterator reduce_no_run_no_checks(OutputIterator d_first,
544 InputIterator1 first,
545 InputIterator2 last) const {
546 return KnuthBendixImpl_::reduce_no_run_no_checks(
547 detail::itow(this, d_first),
548 detail::citow(this, first),
549 detail::citow(this, last))
550 .get();
551 }
552
574 template <typename OutputIterator,
575 typename InputIterator1,
576 typename InputIterator2>
577 OutputIterator reduce_no_checks(OutputIterator d_first,
578 InputIterator1 first,
579 InputIterator2 last) {
580 return KnuthBendixImpl_::reduce_no_checks(detail::itow(this, d_first),
581 detail::citow(this, first),
582 detail::citow(this, last))
583 .get();
584 }
585
606 template <typename OutputIterator,
607 typename InputIterator1,
608 typename InputIterator2>
609 OutputIterator reduce_no_run(OutputIterator d_first,
610 InputIterator1 first,
611 InputIterator2 last) const {
612 // Call CongruenceCommon version so that we perform bound checks
613 // in KnuthBendix and not KnuthBendixImpl_
614 return detail::CongruenceCommon::reduce_no_run<KnuthBendix>(
615 d_first, first, last);
616 }
617
640 template <typename OutputIterator,
641 typename InputIterator1,
642 typename InputIterator2>
643 OutputIterator reduce(OutputIterator d_first,
644 InputIterator1 first,
645 InputIterator2 last) {
646 // Call CongruenceCommon version so that we perform bound checks
647 // in KnuthBendix and not KnuthBendixImpl_
648 return detail::CongruenceCommon::reduce<KnuthBendix>(
649 d_first, first, last);
650 }
651
653 // KnuthBendix specific stuff
655
666 // TODO(1) should be const
668
679 std::vector<Word> gilman_graph_node_labels();
680
681 private:
682 void run_impl();
683
684 [[nodiscard]] bool requires_extra_letter() const {
685 return (!generating_pairs().empty()
686 && detail::CongruenceCommon::kind() == congruence_kind::onesided);
687 }
688 }; // class KnuthBendix
689
698 template <typename Word>
699 KnuthBendix(congruence_kind, Presentation<Word> const&) -> KnuthBendix<Word>;
700
710 template <typename Word>
712
721 template <typename Word>
722 KnuthBendix(KnuthBendix<Word> const&) -> KnuthBendix<Word>;
723
732 template <typename Word>
733 KnuthBendix(KnuthBendix<Word>&&) -> KnuthBendix<Word>;
734
735} // namespace libsemigroups
736
737#include "knuth-bendix-class.tpp"
738#endif // LIBSEMIGROUPS_KNUTH_BENDIX_CLASS_HPP_
For an implementation of presentations for semigroups or monoids.
Definition presentation.hpp:102
auto active_rules()
Return a range object containing the active rules.
KnuthBendix(congruence_kind, Presentation< Word > const &) -> KnuthBendix< Word >
Deduction guide.
KnuthBendix & operator=(KnuthBendix const &)
Copy assignment operator.
KnuthBendix & init()
Remove the presentation and rewriter data.
Definition knuth-bendix-class.hpp:170
void throw_if_letter_not_in_alphabet(Iterator1 first, Iterator2 last) const
Throws if any letter in a range is out of bounds.
KnuthBendix()
Default constructor.
OutputIterator reduce_no_checks(OutputIterator d_first, InputIterator1 first, InputIterator2 last)
Reduce a word with no checks.
Definition knuth-bendix-class.hpp:577
KnuthBendix & add_generating_pair_no_checks(Iterator1 first1, Iterator2 last1, Iterator3 first2, Iterator4 last2)
Add generating pair via iterators.
KnuthBendix & add_generating_pair(Iterator1 first1, Iterator2 last1, Iterator3 first2, Iterator4 last2)
Add generating pair via iterators.
tril currently_contains(Iterator1 first1, Iterator2 last1, Iterator3 first2, Iterator4 last2) const
Check containment of a pair of words via iterators.
Definition knuth-bendix-class.hpp:479
OutputIterator reduce(OutputIterator d_first, InputIterator1 first, InputIterator2 last)
Reduce a word.
Definition knuth-bendix-class.hpp:643
size_t number_of_generating_pairs() const noexcept
std::vector< Word > const & generating_pairs() const noexcept
Get the generating pairs of the congruence (if any).
Definition knuth-bendix-class.hpp:347
tril currently_contains_no_checks(Iterator1 first1, Iterator2 last1, Iterator3 first2, Iterator4 last2) const
bool contains(Iterator1 first1, Iterator2 last1, Iterator3 first2, Iterator4 last2)
Check containment of a pair of words via iterators.
congruence_kind kind() const noexcept
The kind of the congruence (1- or 2-sided).
Presentation< Word > const & presentation() const noexcept
Return the presentation used to construct or initialize a KnuthBendix object.
Definition knuth-bendix-class.hpp:375
OutputIterator reduce_no_run_no_checks(OutputIterator d_first, InputIterator1 first, InputIterator2 last) const
Reduce a word with no run and no checks.
Definition knuth-bendix-class.hpp:543
OutputIterator reduce_no_run(OutputIterator d_first, InputIterator1 first, InputIterator2 last) const
Reduce a word with no enumeration.
Definition knuth-bendix-class.hpp:609
std::pair< Word, Word > rule_type
Type of the rules in the system.
Definition knuth-bendix-class.hpp:149
Word native_word_type
Type of the letters in the relations of the presentation stored in a KnuthBendix instance.
Definition knuth-bendix-class.hpp:142
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
@ onesided
Value representing a one-sided congruence.
Definition types.hpp:69
T move(T... args)
Namespace for everything in the libsemigroups library.
Definition action.hpp:44
Struct containing various options that can be used to control the behaviour of Knuth-Bendix.
Definition knuth-bendix-class.hpp:115
overlap
Values for specifying how to measure the length of an overlap.
Definition knuth-bendix-class.hpp:124
@ MAX_AB_BC
Definition knuth-bendix-class.hpp:130
@ AB_BC
Definition knuth-bendix-class.hpp:128
@ ABC
Definition knuth-bendix-class.hpp:126
A stateless struct with binary call operator using shortlex_compare.
Definition order.hpp:362