libsemigroups  v3.3.0
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
95 template <typename Word,
96 typename Rewriter = detail::RewriteTrie,
97 typename ReductionOrder = ShortLexCompare>
98 class KnuthBendix : public detail::KnuthBendixImpl<Rewriter, ReductionOrder> {
99 private:
100 using KnuthBendixImpl_ = detail::KnuthBendixImpl<Rewriter, ReductionOrder>;
101
102 bool _extra_letter_added;
103 std::vector<Word> _generating_pairs;
104 Presentation<Word> _presentation;
105
106 // defined in detail/kbe.hpp
107 friend class ::libsemigroups::detail::KBE<KnuthBendix>;
108
109 public:
110#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
117 struct options {
126 enum class overlap {
128 ABC = 0,
130 AB_BC = 1,
133 };
134 };
135#endif
136
144 using native_word_type = Word;
145
152
160
171 // TODO(1) to tpp file
173 _extra_letter_added = false;
174 _generating_pairs.clear();
175 _presentation.init();
176 KnuthBendixImpl_::init();
177 return *this;
178 }
179
192
199
206
213
214 ~KnuthBendix();
215
218 init(knd, std::move(p));
219 }
220
222 KnuthBendix& init(congruence_kind knd, Presentation<Word>&& p);
223
244 // call the rval ref constructor
245 : KnuthBendix(knd, Presentation<Word>(p)) {}
246
269 // call the rval ref init
270 return init(knd, Presentation<Word>(p));
271 }
272
300 template <typename Iterator1, typename Iterator2>
301 void throw_if_letter_not_in_alphabet(Iterator1 first, Iterator2 last) const;
302
303#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
318 [[nodiscard]] congruence_kind kind() const noexcept;
319
335 [[nodiscard]] size_t number_of_generating_pairs() const noexcept;
336#endif
337
349 [[nodiscard]] std::vector<Word> const& generating_pairs() const noexcept {
350 return _generating_pairs;
351 }
352
377 [[nodiscard]] Presentation<Word> const& presentation() const noexcept {
378 return _presentation;
379 }
380
382 // KnuthBendix - interface requirements - add_generating_pair
384
402 template <typename Iterator1,
403 typename Iterator2,
404 typename Iterator3,
405 typename Iterator4>
407 Iterator2 last1,
408 Iterator3 first2,
409 Iterator4 last2);
410
426 template <typename Iterator1,
427 typename Iterator2,
428 typename Iterator3,
429 typename Iterator4>
431 Iterator2 last1,
432 Iterator3 first2,
433 Iterator4 last2);
434
436 // KnuthBendix - interface requirements - contains
438
439 // TODO: contains_no_checks is implemented
440 // and documented in KnuthBendixImpl
441
442 template <typename Iterator1,
443 typename Iterator2,
444 typename Iterator3,
445 typename Iterator4>
446 tril currently_contains_no_checks(Iterator1 first1,
447 Iterator2 last1,
448 Iterator3 first2,
449 Iterator4 last2) const {
451 detail::citow(this, first1),
452 detail::citow(this, last1),
453 detail::citow(this, first2),
454 detail::citow(this, last2));
455 }
456
477 template <typename Iterator1,
478 typename Iterator2,
479 typename Iterator3,
480 typename Iterator4>
481 tril currently_contains(Iterator1 first1,
482 Iterator2 last1,
483 Iterator3 first2,
484 Iterator4 last2) const {
485 // Call CongruenceCommon version so that we perform bound checks
486 // in KnuthBendix and not KnuthBendixImpl_
487 return detail::CongruenceCommon::currently_contains<KnuthBendix>(
488 first1, last1, first2, last2);
489 }
490
509 template <typename Iterator1,
510 typename Iterator2,
511 typename Iterator3,
512 typename Iterator4>
513 bool contains(Iterator1 first1,
514 Iterator2 last1,
515 Iterator3 first2,
516 Iterator4 last2);
517
519 // KnuthBendix - interface requirements - reduce
521
542 template <typename OutputIterator,
543 typename InputIterator1,
544 typename InputIterator2>
545 OutputIterator reduce_no_run_no_checks(OutputIterator d_first,
546 InputIterator1 first,
547 InputIterator2 last) const {
548 return KnuthBendixImpl_::reduce_no_run_no_checks(
549 detail::itow(this, d_first),
550 detail::citow(this, first),
551 detail::citow(this, last))
552 .get();
553 }
554
576 template <typename OutputIterator,
577 typename InputIterator1,
578 typename InputIterator2>
579 OutputIterator reduce_no_checks(OutputIterator d_first,
580 InputIterator1 first,
581 InputIterator2 last) {
582 return KnuthBendixImpl_::reduce_no_checks(detail::itow(this, d_first),
583 detail::citow(this, first),
584 detail::citow(this, last))
585 .get();
586 }
587
608 template <typename OutputIterator,
609 typename InputIterator1,
610 typename InputIterator2>
611 OutputIterator reduce_no_run(OutputIterator d_first,
612 InputIterator1 first,
613 InputIterator2 last) const {
614 // Call CongruenceCommon version so that we perform bound checks
615 // in KnuthBendix and not KnuthBendixImpl_
616 return detail::CongruenceCommon::reduce_no_run<KnuthBendix>(
617 d_first, first, last);
618 }
619
642 template <typename OutputIterator,
643 typename InputIterator1,
644 typename InputIterator2>
645 OutputIterator reduce(OutputIterator d_first,
646 InputIterator1 first,
647 InputIterator2 last) {
648 // Call CongruenceCommon version so that we perform bound checks
649 // in KnuthBendix and not KnuthBendixImpl_
650 return detail::CongruenceCommon::reduce<KnuthBendix>(
651 d_first, first, last);
652 }
653
655 // KnuthBendix specific stuff
657
668 // TODO(1) should be const
670
681 std::vector<Word> gilman_graph_node_labels();
682
683 private:
684 void run_impl();
685
686 [[nodiscard]] bool requires_extra_letter() const {
687 return (!generating_pairs().empty()
688 && detail::CongruenceCommon::kind() == congruence_kind::onesided);
689 }
690 }; // class KnuthBendix
691
700 template <typename Word>
701 KnuthBendix(congruence_kind, Presentation<Word> const&) -> KnuthBendix<Word>;
702
712 template <typename Word>
714
723 template <typename Word>
724 KnuthBendix(KnuthBendix<Word> const&) -> KnuthBendix<Word>;
725
734 template <typename Word>
735 KnuthBendix(KnuthBendix<Word>&&) -> KnuthBendix<Word>;
736
737} // namespace libsemigroups
738
739#include "knuth-bendix-class.tpp"
740#endif // LIBSEMIGROUPS_KNUTH_BENDIX_CLASS_HPP_
For an implementation of presentations for semigroups or monoids.
Definition presentation.hpp:103
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:172
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:579
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:481
OutputIterator reduce(OutputIterator d_first, InputIterator1 first, InputIterator2 last)
Reduce a word.
Definition knuth-bendix-class.hpp:645
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:349
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:377
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:545
OutputIterator reduce_no_run(OutputIterator d_first, InputIterator1 first, InputIterator2 last) const
Reduce a word with no enumeration.
Definition knuth-bendix-class.hpp:611
std::pair< Word, Word > rule_type
Type of the rules in the system.
Definition knuth-bendix-class.hpp:151
Word native_word_type
Type of the letters in the relations of the presentation stored in a KnuthBendix instance.
Definition knuth-bendix-class.hpp:144
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:117
overlap
Values for specifying how to measure the length of an overlap.
Definition knuth-bendix-class.hpp:126
@ MAX_AB_BC
Definition knuth-bendix-class.hpp:132
@ AB_BC
Definition knuth-bendix-class.hpp:130
@ ABC
Definition knuth-bendix-class.hpp:128
A stateless struct with binary call operator using shortlex_compare.
Definition order.hpp:380