libsemigroups  v3.0.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 <utility> // for move
26#include <vector> // for vector
27
28#include "order.hpp" // for ShortLexCompare
29#include "presentation.hpp" // for Presentation
30
31#include "detail/cong-common-class.hpp" // for CongruenceCommon
32#include "detail/knuth-bendix-impl.hpp" // for KnuthBendixImpl
33#include "detail/rewriters.hpp" // for RewriteTrie
34
35namespace libsemigroups {
36 enum class congruence_kind;
37 enum class tril;
38
47
86 template <typename Word,
87 typename Rewriter = detail::RewriteTrie,
88 typename ReductionOrder = ShortLexCompare>
89 class KnuthBendix : public detail::KnuthBendixImpl<Rewriter, ReductionOrder> {
90 private:
91 using KnuthBendixImpl_ = detail::KnuthBendixImpl<Rewriter, ReductionOrder>;
92
93 std::vector<Word> _generating_pairs;
94 Presentation<Word> _presentation;
95
96 public:
97#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
104 struct options {
113 enum class overlap {
115 ABC = 0,
117 AB_BC = 1,
120 };
121 };
122#endif
123
131 using native_word_type = Word;
132
139
147
158 // TODO(1) to tpp file
160 _generating_pairs.clear();
161 _presentation.init();
162 KnuthBendixImpl_::init();
163 return *this;
164 }
165
178
185
192
199
200 ~KnuthBendix();
201
204 init(knd, std::move(p));
205 }
206
208 KnuthBendix& init(congruence_kind knd, Presentation<Word>&& p);
209
223 // call the rval ref constructor
224 : KnuthBendix(knd, Presentation<Word>(p)) {}
225
241 // call the rval ref init
242 return init(knd, Presentation<Word>(p));
243 }
244
263 // TODO(0) remove this
264 template <typename Iterator1, typename Iterator2>
266 Iterator2 last) const {
267 presentation().throw_if_letter_not_in_alphabet(first, last);
268 }
269
270#ifdef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
285 [[nodiscard]] congruence_kind kind() const noexcept;
286
302 [[nodiscard]] size_t number_of_generating_pairs() const noexcept;
303#endif
304
316 [[nodiscard]] std::vector<Word> const& generating_pairs() const noexcept {
317 return _generating_pairs;
318 }
319
336 [[nodiscard]] Presentation<Word> const& presentation() const noexcept {
337 return _presentation;
338 }
339
341 // KnuthBendix - interface requirements - add_generating_pair
343
360 template <typename Iterator1,
361 typename Iterator2,
362 typename Iterator3,
363 typename Iterator4>
365 Iterator2 last1,
366 Iterator3 first2,
367 Iterator4 last2);
368
383 template <typename Iterator1,
384 typename Iterator2,
385 typename Iterator3,
386 typename Iterator4>
388 Iterator2 last1,
389 Iterator3 first2,
390 Iterator4 last2) {
391 // Call detail::CongruenceCommon version so that we perform bound checks
392 // in KnuthBendix and not KnuthBendixImpl
393 return detail::CongruenceCommon::add_generating_pair<KnuthBendix>(
394 first1, last1, first2, last2);
395 }
396
398 // KnuthBendix - interface requirements - contains
400
401 // NOTE: contains_no_checks and currently_contains_no_checks are implemented
402 // and documented in KnuthBendixImpl
403
422 template <typename Iterator1,
423 typename Iterator2,
424 typename Iterator3,
425 typename Iterator4>
426 tril currently_contains(Iterator1 first1,
427 Iterator2 last1,
428 Iterator3 first2,
429 Iterator4 last2) const {
430 // Call CongruenceCommon version so that we perform bound checks
431 // in KnuthBendix and not KnuthBendixImpl_
432 return detail::CongruenceCommon::currently_contains<KnuthBendix>(
433 first1, last1, first2, last2);
434 }
435
453 template <typename Iterator1,
454 typename Iterator2,
455 typename Iterator3,
456 typename Iterator4>
457 bool contains(Iterator1 first1,
458 Iterator2 last1,
459 Iterator3 first2,
460 Iterator4 last2);
461
463 // KnuthBendix - interface requirements - reduce
465
466 // NOTE: reduce_no_checks and reduce_no_run_no_checks are implemented and
467 // documented in KnuthBendixImpl
468
488 template <typename OutputIterator,
489 typename InputIterator1,
490 typename InputIterator2>
491 OutputIterator reduce_no_run(OutputIterator d_first,
492 InputIterator1 first,
493 InputIterator2 last) const {
494 // Call CongruenceCommon version so that we perform bound checks
495 // in KnuthBendix and not KnuthBendixImpl_
496 return detail::CongruenceCommon::reduce_no_run<KnuthBendix>(
497 d_first, first, last);
498 }
499
520 template <typename OutputIterator,
521 typename InputIterator1,
522 typename InputIterator2>
523 OutputIterator reduce(OutputIterator d_first,
524 InputIterator1 first,
525 InputIterator2 last) {
526 // Call CongruenceCommon version so that we perform bound checks
527 // in KnuthBendix and not KnuthBendixImpl_
528 return detail::CongruenceCommon::reduce<KnuthBendix>(
529 d_first, first, last);
530 }
531
533 // KnuthBendix specific stuff
535
546 // TODO(1) should be const
548
559 std::vector<Word> gilman_graph_node_labels();
560 }; // class KnuthBendix
561
570 template <typename Word>
571 KnuthBendix(congruence_kind, Presentation<Word> const&) -> KnuthBendix<Word>;
572
582 template <typename Word>
584
593 template <typename Word>
594 KnuthBendix(KnuthBendix<Word> const&) -> KnuthBendix<Word>;
595
604 template <typename Word>
605 KnuthBendix(KnuthBendix<Word>&&) -> KnuthBendix<Word>;
606
607} // namespace libsemigroups
608
609#include "knuth-bendix-class.tpp"
610#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:159
void throw_if_letter_not_in_alphabet(Iterator1 first, Iterator2 last) const
Throws if any letter in a range is out of bounds.
Definition knuth-bendix-class.hpp:265
KnuthBendix()
Default constructor.
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.
Definition knuth-bendix-class.hpp:387
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:426
OutputIterator reduce(OutputIterator d_first, InputIterator1 first, InputIterator2 last)
Reduce a word.
Definition knuth-bendix-class.hpp:523
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:316
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:336
OutputIterator reduce_no_run(OutputIterator d_first, InputIterator1 first, InputIterator2 last) const
Reduce a word with no enumeration.
Definition knuth-bendix-class.hpp:491
std::pair< Word, Word > rule_type
Type of the rules in the system.
Definition knuth-bendix-class.hpp:138
Word native_word_type
Type of the letters in the relations of the presentation stored in a KnuthBendix instance.
Definition knuth-bendix-class.hpp:131
Presentation(Presentation< Word > const &) -> Presentation< Word >
Deduction guide.
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
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:104
overlap
Values for specifying how to measure the length of an overlap.
Definition knuth-bendix-class.hpp:113
@ MAX_AB_BC
Definition knuth-bendix-class.hpp:119
@ AB_BC
Definition knuth-bendix-class.hpp:117
@ ABC
Definition knuth-bendix-class.hpp:115
A stateless struct with binary call operator using shortlex_compare.
Definition order.hpp:356