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
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ libsemigroups_jll = "99c76e05-e2a6-5e47-b69a-d74ec2f0d12c"
[compat]
AbstractAlgebra = "0.43, 0.48"
CxxWrap = "0.17"
libsemigroups_jll = "=3.5.5"
julia = "1.9"

[extras]
Expand Down
2 changes: 2 additions & 0 deletions deps/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ message(STATUS "libsemigroups libraries: ${LIBSEMIGROUPS_LIBRARIES}")
add_library(libsemigroups_julia SHARED
libsemigroups_julia.cpp
bmat8.cpp
cong-common.cpp
constants.cpp
froidure-pin-base.cpp
froidure-pin.cpp
Expand All @@ -56,6 +57,7 @@ add_library(libsemigroups_julia SHARED
word-range.cpp
presentation.cpp
presentation-examples.cpp
knuth-bendix.cpp
)

# Include directories
Expand Down
166 changes: 166 additions & 0 deletions deps/src/cong-common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
//
// 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/>.
//

// 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 <>
struct IsMirroredType<libsemigroups::detail::CongruenceCommon>
: std::false_type {};

template <>
struct SuperType<libsemigroups::detail::CongruenceCommon> {
using type = libsemigroups::Runner;
};
} // 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.
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
8 changes: 8 additions & 0 deletions deps/src/constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ namespace libsemigroups_julia {
m.method("NEGATIVE_INFINITY_Int64",
[]() -> int64_t { return libsemigroups::NEGATIVE_INFINITY; });

// congruence_kind enum for sided-ness of a congruence
m.add_bits<libsemigroups::congruence_kind>("congruence_kind",
jl::julia_type("CppEnum"));
m.set_const("congruence_kind_onesided",
libsemigroups::congruence_kind::onesided);
m.set_const("congruence_kind_twosided",
libsemigroups::congruence_kind::twosided);

// tril enum for three-valued logic (true, false, unknown)
m.add_bits<libsemigroups::tril>("tril", jl::julia_type("CppEnum"));
m.set_const("tril_FALSE", libsemigroups::tril::FALSE);
Expand Down
Loading
Loading