diff --git a/src/present.cpp b/src/present.cpp index 16bc2157..727fe07a 100644 --- a/src/present.cpp +++ b/src/present.cpp @@ -1581,16 +1581,18 @@ modified version. * :any:`is_strongly_compressible` )pbdoc"); + m.def( "presentation_to_gap_string", [](Presentation_ const& p, std::string const& var_name) { return presentation::to_gap_string(p, var_name); }, py::arg("p"), - py::arg("var_name"), + py::arg("var_name") = "S", R"pbdoc( :sig=(p: Presentation, var_name: str) -> str: :only-document-once: + Return the code that would create *p* in GAP. This function returns the string of GAP code that could be used to create an @@ -1600,12 +1602,26 @@ are created by taking quotients of free semigroups or monoids. :param p: the presentation. :type p: Presentation -:param var_name: the name of the variable to be used in GAP. +:param var_name: the name of the variable to be used in GAP (defaults to ``"S"``). :type var_name: str :returns: The GAP string. :rtype: str + +.. doctest:: + + >>> from libsemigroups_pybind11 import Presentation, presentation + >>> p = Presentation("ab") + >>> presentation.add_rule(p, "ab", "ba") + >>> print(presentation.to_gap_string(p), end="") + F := FreeSemigroup("a", "b"); + AssignGeneratorVariables(F);; + R := [ + [a * b, b * a] + ]; + S := F / R; )pbdoc"); + m.def( "presentation_to_ace_string", [](Presentation_ const& p) { return presentation::to_ace_string(p); }, diff --git a/tests/test_present.py b/tests/test_present.py index 8142eee7..bb431644 100644 --- a/tests/test_present.py +++ b/tests/test_present.py @@ -819,6 +819,38 @@ def check_to_ace_string(W): ) +def check_to_gap_string(W): + p = Presentation(W([0, 1])) + presentation.add_rule(p, W([0, 1]), W([1, 0])) + + a, b = ("s0", "s1") if W is to_word else ("a", "b") + assert ( + presentation.to_gap_string(p) + == f"""F := FreeSemigroup("{a}", "{b}"); +AssignGeneratorVariables(F);; +R := [ + [{a} * {b}, {b} * {a}] + ]; +S := F / R; +""" + ) + + a, b = ("m0", "m1") if W is to_word else ("a", "b") + p.contains_empty_word(True) + presentation.add_rule(p, W([0, 0]), W([])) + assert ( + presentation.to_gap_string(p, "M") + == f"""F := FreeMonoid("{a}", "{b}"); +AssignGeneratorVariables(F);; +R := [ + [{a} * {b}, {b} * {a}], + [{a} * {a}, One(F)] + ]; +M := F / R; +""" + ) + + ############################################################################### # Test functions begin ############################################################################### @@ -1780,3 +1812,8 @@ def test_add_idempotent_rules(): def test_to_ace_string(): check_to_ace_string(to_word) check_to_ace_string(to_string) + + +def test_to_gap_string(): + check_to_gap_string(to_word) + check_to_gap_string(to_string)