Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions deps/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ add_library(libsemigroups_julia SHARED
presentation.cpp
presentation-examples.cpp
knuth-bendix.cpp
todd-coxeter.cpp
)

# Include directories
Expand All @@ -84,6 +85,10 @@ target_link_libraries(libsemigroups_julia
target_compile_options(libsemigroups_julia PRIVATE
"-I${LIBSEMIGROUPS_INCLUDEDIR}"
-fno-char8_t # Work around C++20 char8_t issues with fmt in libsemigroups
# Silence noisy upstream warnings from libsemigroups iterator templates
# under C++20's reversed-operator rules. The semantics are unambiguous;
# only Clang/AppleClang emit this warning.
$<$<CXX_COMPILER_ID:Clang,AppleClang>:-Wno-ambiguous-reversed-operator>
)

# Set RPATH to find libsemigroups at runtime
Expand Down
122 changes: 4 additions & 118 deletions deps/src/cong-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,10 @@
// CRITICAL: libsemigroups_julia.hpp MUST be included first (fmt consteval fix)
#include "libsemigroups_julia.hpp"

#include <libsemigroups/cong-common-helpers.hpp>
#include <libsemigroups/detail/cong-common-class.hpp>
#include <libsemigroups/knuth-bendix-class.hpp>
#include <libsemigroups/knuth-bendix-helpers.hpp>
#include <libsemigroups/runner.hpp>

#include <jlcxx/array.hpp>

#include <cstddef>
#include <cstdint>
#include <type_traits>
#include <vector>

namespace jlcxx {
template <>
Expand All @@ -44,123 +36,17 @@ namespace jlcxx {

namespace libsemigroups_julia {

namespace {
template <typename Thing>
void define_cong_common_word_helpers(jl::Module& m) {
using Word = typename Thing::native_word_type;

// reduce (triggers full enumeration)
m.method("cong_common_reduce",
[](Thing& self, jlcxx::ArrayRef<size_t> w) -> Word {
Word input(w.begin(), w.end());
return libsemigroups::congruence_common::reduce(self, input);
});

// reduce_no_run (no enumeration)
m.method("cong_common_reduce_no_run",
[](Thing const& self, jlcxx::ArrayRef<size_t> w) -> Word {
Word input(w.begin(), w.end());
return libsemigroups::congruence_common::reduce_no_run(self,
input);
});

// contains (triggers full enumeration)
m.method("cong_common_contains",
[](Thing& self,
jlcxx::ArrayRef<size_t> u,
jlcxx::ArrayRef<size_t> v) -> bool {
Word uw(u.begin(), u.end());
Word vw(v.begin(), v.end());
return libsemigroups::congruence_common::contains(
self, uw, vw);
});

// currently_contains (no enumeration, returns tril)
m.method("cong_common_currently_contains",
[](Thing const& self,
jlcxx::ArrayRef<size_t> u,
jlcxx::ArrayRef<size_t> v) -> libsemigroups::tril {
Word uw(u.begin(), u.end());
Word vw(v.begin(), v.end());
return libsemigroups::congruence_common::currently_contains(
self, uw, vw);
});

// add_generating_pair!
m.method("cong_common_add_generating_pair!",
[](Thing& self,
jlcxx::ArrayRef<size_t> u,
jlcxx::ArrayRef<size_t> v) {
Word uw(u.begin(), u.end());
Word vw(v.begin(), v.end());
libsemigroups::congruence_common::add_generating_pair(
self, uw, vw);
});

m.method("cong_common_partition",
[](Thing& self, jlcxx::ArrayRef<jl_value_t*> words)
-> std::vector<std::vector<Word>> {
std::vector<Word> input;
input.reserve(words.size());
for (jl_value_t* word_value : words) {
auto word = jlcxx::ArrayRef<size_t>(
reinterpret_cast<jl_array_t*>(word_value));
input.emplace_back(word.begin(), word.end());
}
return libsemigroups::congruence_common::partition(
self, input.begin(), input.end());
});
}

template <typename Thing>
void define_cong_common_normal_forms(jl::Module& m) {
using Word = typename Thing::native_word_type;

// normal_forms() returns an rx-style range; use
// .at_end()/.get()/.next().
m.method(
"cong_common_normal_forms", [](Thing& self) -> std::vector<Word> {
std::vector<Word> result;
auto range = libsemigroups::congruence_common::normal_forms(self);
while (!range.at_end()) {
result.push_back(range.get());
range.next();
}
return result;
});
}

template <typename Thing>
void define_cong_common_non_trivial_classes(jl::Module& m) {
using Word = typename Thing::native_word_type;

m.method("cong_common_non_trivial_classes",
[](Thing& x, Thing& y) -> std::vector<std::vector<Word>> {
return libsemigroups::congruence_common::non_trivial_classes(
x, y);
});
}
} // namespace

void define_cong_common(jl::Module& m) {
using libsemigroups::Runner;
using CongruenceCommon = libsemigroups::detail::CongruenceCommon;

// No constructors: CongruenceCommon is a shared implementation base.
// Derived algorithms register their concrete methods on their own types.
// Derived algorithms register their concrete methods on their own types,
// and instantiate the cong-common helper templates from cong-common.hpp
// in their own translation units (see knuth-bendix.cpp,
// todd-coxeter.cpp, ...).
m.add_type<CongruenceCommon>("CongruenceCommon",
jlcxx::julia_base_type<Runner>());
}

void define_knuth_bendix_cong_common_helpers(jl::Module& m) {
using libsemigroups::word_type;
using KB = libsemigroups::KnuthBendix<word_type,
libsemigroups::detail::RewriteTrie,
libsemigroups::ShortLexCompare>;

define_cong_common_word_helpers<KB>(m);
define_cong_common_normal_forms<KB>(m);
define_cong_common_non_trivial_classes<KB>(m);
}

} // namespace libsemigroups_julia
162 changes: 162 additions & 0 deletions deps/src/cong-common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
//
// Semigroups.jl
// Copyright (C) 2026, James W. Swent
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

// Shared template family used to instantiate the cong-common helper
// dispatch shims for each concrete congruence-algorithm type (KnuthBendix,
// ToddCoxeter, ...). The actual user-facing Julia helpers in
// `src/cong-common.jl` dispatch on the `CongruenceCommon` supertype, so
// once a derived algorithm registers these shims for its type, all common
// helpers (`reduce`, `contains`, `normal_forms`, `partition`,
// `non_trivial_classes`, ...) work on it for free.
//
// INCLUDE-ORDER REQUIREMENT (silent runtime bug if violated):
// Translation units that instantiate these templates MUST include the
// algorithm-specific helpers header (e.g.
// `<libsemigroups/knuth-bendix-helpers.hpp>` or
// `<libsemigroups/todd-coxeter-helpers.hpp>`) BEFORE including this file.
// The calls to `congruence_common::reduce`, `non_trivial_classes`, etc. are
// resolved by ADL at template-instantiation time; if the algorithm-specific
// overload isn't visible in the TU, the call silently binds to the generic
// base in `<libsemigroups/cong-common-helpers.hpp>`. The link succeeds and
// the wrong code runs at runtime. See `cong-common.cpp` (KnuthBendix) and
// `todd-coxeter.cpp` (ToddCoxeter) for the established pattern; future
// bindings (Kambites, Congruence, ...) must follow it.

#ifndef LIBSEMIGROUPS_JULIA_CONG_COMMON_HPP_
#define LIBSEMIGROUPS_JULIA_CONG_COMMON_HPP_

// CRITICAL: libsemigroups_julia.hpp MUST be included first (fmt consteval fix)
#include "libsemigroups_julia.hpp"

#include <libsemigroups/cong-common-helpers.hpp>

#include <jlcxx/array.hpp>

#include <cstddef>
#include <vector>

namespace libsemigroups_julia {

template <typename Thing>
inline void define_cong_common_word_helpers(jl::Module& m) {
using Word = typename Thing::native_word_type;

// reduce (triggers full enumeration)
m.method("cong_common_reduce",
[](Thing& self, jlcxx::ArrayRef<size_t> w) -> Word {
Word input(w.begin(), w.end());
return libsemigroups::congruence_common::reduce(self, input);
});

// reduce_no_run (no enumeration)
m.method("cong_common_reduce_no_run",
[](Thing const& self, jlcxx::ArrayRef<size_t> w) -> Word {
Word input(w.begin(), w.end());
return libsemigroups::congruence_common::reduce_no_run(self,
input);
});

// contains (triggers full enumeration)
m.method("cong_common_contains",
[](Thing& self,
jlcxx::ArrayRef<size_t> u,
jlcxx::ArrayRef<size_t> v) -> bool {
Word uw(u.begin(), u.end());
Word vw(v.begin(), v.end());
return libsemigroups::congruence_common::contains(self, uw, vw);
});

// currently_contains (no enumeration, returns tril)
m.method("cong_common_currently_contains",
[](Thing const& self,
jlcxx::ArrayRef<size_t> u,
jlcxx::ArrayRef<size_t> v) -> libsemigroups::tril {
Word uw(u.begin(), u.end());
Word vw(v.begin(), v.end());
return libsemigroups::congruence_common::currently_contains(
self, uw, vw);
});

// add_generating_pair!
m.method(
"cong_common_add_generating_pair!",
[](Thing& self, jlcxx::ArrayRef<size_t> u, jlcxx::ArrayRef<size_t> v) {
Word uw(u.begin(), u.end());
Word vw(v.begin(), v.end());
libsemigroups::congruence_common::add_generating_pair(self, uw, vw);
});

m.method("cong_common_partition",
[](Thing& self, jlcxx::ArrayRef<jl_value_t*> words)
-> std::vector<std::vector<Word>> {
std::vector<Word> input;
input.reserve(words.size());
for (jl_value_t* word_value : words) {
auto word = jlcxx::ArrayRef<size_t>(
reinterpret_cast<jl_array_t*>(word_value));
input.emplace_back(word.begin(), word.end());
}
return libsemigroups::congruence_common::partition(
self, input.begin(), input.end());
});
}

template <typename Thing>
inline void define_cong_common_normal_forms(jl::Module& m) {
using Word = typename Thing::native_word_type;

// normal_forms() returns an rx-style range; use
// .at_end()/.get()/.next().
m.method("cong_common_normal_forms", [](Thing& self) -> std::vector<Word> {
std::vector<Word> result;
auto range = libsemigroups::congruence_common::normal_forms(self);
while (!range.at_end()) {
result.push_back(range.get());
range.next();
}
return result;
});
}

template <typename Thing>
inline void define_cong_common_non_trivial_classes(jl::Module& m) {
using Word = typename Thing::native_word_type;

m.method("cong_common_non_trivial_classes",
[](Thing& x, Thing& y) -> std::vector<std::vector<Word>> {
return libsemigroups::congruence_common::non_trivial_classes(x,
y);
});
}

// Aggregator: register the full cong-common helper family for one
// concrete algorithm type. Each new algorithm binding (KnuthBendix,
// ToddCoxeter, Kambites, ...) calls this once at the end of its
// `define_<algorithm>(m)` function. The include-order requirement
// documented above still applies - the algorithm-specific helpers
// header must be included before this file in the calling TU.
template <typename Thing>
inline void define_cong_common_helpers(jl::Module& m) {
define_cong_common_word_helpers<Thing>(m);
define_cong_common_normal_forms<Thing>(m);
define_cong_common_non_trivial_classes<Thing>(m);
}

} // namespace libsemigroups_julia

#endif // LIBSEMIGROUPS_JULIA_CONG_COMMON_HPP_
7 changes: 7 additions & 0 deletions deps/src/knuth-bendix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@
// CRITICAL: libsemigroups_julia.hpp MUST be included first (fmt consteval fix)
#include "libsemigroups_julia.hpp"

// knuth-bendix-helpers.hpp MUST come BEFORE cong-common.hpp so the template
// bodies in cong-common.hpp see KB-specific overloads of congruence_common
// helpers (e.g., non_trivial_classes(KB&, KB&)) at instantiation time.
#include <libsemigroups/knuth-bendix-class.hpp>
#include <libsemigroups/knuth-bendix-helpers.hpp>
#include <libsemigroups/presentation.hpp>
#include <libsemigroups/word-graph.hpp>

#include "cong-common.hpp"

#include <chrono>
#include <cstddef>
#include <cstdint>
Expand Down Expand Up @@ -256,6 +261,8 @@ namespace libsemigroups_julia {
p, std::chrono::nanoseconds(ns));
return static_cast<size_t>(std::distance(p.rules.cbegin(), it));
});

define_cong_common_helpers<KB>(m);
}

} // namespace libsemigroups_julia
2 changes: 1 addition & 1 deletion deps/src/libsemigroups_julia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace libsemigroups_julia {
define_presentation(mod);
define_presentation_examples(mod);
define_knuth_bendix(mod);
define_knuth_bendix_cong_common_helpers(mod);
define_todd_coxeter(mod);
}

} // namespace libsemigroups_julia
4 changes: 2 additions & 2 deletions deps/src/libsemigroups_julia.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#ifndef LIBSEMIGROUPS_JULIA_HPP_
#define LIBSEMIGROUPS_JULIA_HPP_

// JlCxx headers FIRST these pull in standard library headers (<string>,
// JlCxx headers FIRST - these pull in standard library headers (<string>,
// <memory>, etc.) that on libstdc++ use __cpp_lib_is_constant_evaluated to
// decide constexpr-ness. They must be included while the macro is intact.
#include "jlcxx/jlcxx.hpp"
Expand Down Expand Up @@ -69,7 +69,7 @@ namespace libsemigroups_julia {
void define_presentation(jl::Module& mod);
void define_presentation_examples(jl::Module& mod);
void define_knuth_bendix(jl::Module& mod);
void define_knuth_bendix_cong_common_helpers(jl::Module& mod);
void define_todd_coxeter(jl::Module& mod);

} // namespace libsemigroups_julia

Expand Down
Loading
Loading