Skip to content

Commit 7becc73

Browse files
sims: use keyword arg "word" instead of default ctor
1 parent cb034ac commit 7becc73

3 files changed

Lines changed: 89 additions & 35 deletions

File tree

src/libsemigroups_pybind11/sims.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,36 @@
4444

4545
class _SimsBase(_CxxWrapper):
4646
def __init__(self: _Self, *args, **kwargs) -> None:
47-
super().__init__(*args, **kwargs)
47+
super().__init__(*args, optional_kwargs=("word",), **kwargs)
4848
if _to_cxx(self) is not None:
4949
return
5050
if len(args) not in (0, 1):
5151
raise TypeError(
5252
f"expected 0 or 1 positional arguments but found {len(args)}"
5353
)
54-
if len(kwargs) != 0:
54+
if len(args) != 0 and len(kwargs) != 0:
5555
raise TypeError(
56-
f"expected 0 keyword arguments, but found {len(kwargs)}"
56+
"expected either 1 positional argument or 1"
57+
f"keyword argument (but not both), but found {len(args)} and {len(kwargs)}"
5758
)
59+
if len(args) == 0:
60+
if "word" not in kwargs:
61+
raise TypeError(
62+
f'expected the keyword argument "word", but found {tuple(kwargs.keys())}'
63+
)
64+
# for when Sims has a facade
65+
# if kwargs["word"] not in (str, list[int]):
66+
# raise ValueError(
67+
# 'the keyword argument "word" must be str or list[int], '
68+
# f"but found {kwargs['word']}"
69+
# )
70+
if kwargs["word"] != list[int]:
71+
raise ValueError(
72+
f'the keyword argument "word" must be list[int], but found {kwargs["word"]}'
73+
)
5874

5975
if len(args) == 0:
60-
# self.Word = kwargs["Word"]
61-
self.py_template_params = (list[int],)
76+
self.py_template_params = (kwargs["word"],)
6277
else:
6378
if isinstance(args[0], _Presentation):
6479
self.py_template_params = args[0].py_template_params
@@ -240,15 +255,31 @@ class SimsRefinerFaithful(
240255

241256
@_copydoc(_SimsRefinerFaithful.__init__)
242257
def __init__(self: _Self, *args, **kwargs) -> None:
243-
super().__init__(*args, **kwargs)
258+
super().__init__(*args, optional_kwargs=("word",), **kwargs)
244259
if _to_cxx(self) is not None:
245260
return
246261

262+
if len(args) not in (0, 1):
263+
raise TypeError(
264+
f"expected 0 or 1 positional arguments but found {len(args)}"
265+
)
266+
if len(args) != 0 and len(kwargs) != 0:
267+
raise TypeError(
268+
"expected either 1 positional argument or 1"
269+
f"keyword argument (but not both), but found {len(args)} and {len(kwargs)}"
270+
)
271+
if len(args) == 0:
272+
if "word" not in kwargs:
273+
raise TypeError(
274+
f'expected the keyword argument "word", but found {tuple(kwargs.keys())}'
275+
)
276+
if kwargs["word"] != list[int]:
277+
raise ValueError(
278+
f'the keyword argument "word" must be list[int], but found {kwargs["word"]}'
279+
)
247280
if len(args) == 0:
248-
# self.Word = kwargs["Word"]
249-
self.py_template_params = (list[int],)
281+
self.py_template_params = (kwargs["word"],)
250282
else:
251-
assert len(args) == 1
252283
if (
253284
isinstance(args[0], list)
254285
and all(isinstance(x, list) for x in args[0])

src/sims.cpp

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -675,10 +675,13 @@ a congruence.
675675
[](Sims1 const& s1) { return to_human_readable_repr(s1); });
676676

677677
s1.def(py::init<>(), R"pbdoc(
678-
:sig=(self: Sims1) -> None:
679-
Default constructor.
678+
:sig=(self: Sims1, word: type) -> None:
679+
680+
This function returns an uninitialized :any:`Sims1` object that uses
681+
words of type specified by *word*.
680682
681-
This function returns an uninitialized :any:`Sims1` object.
683+
:Keyword Arguments:
684+
* **word** (*type*) -- the type of words to use, must be ``list[int]``.
682685
)pbdoc");
683686

684687
s1.def(py::init<Presentation<word_type> const&>(),
@@ -899,9 +902,13 @@ on the classes of a congruence.
899902
[](Sims2 const& s2) { return to_human_readable_repr(s2); });
900903

901904
s2.def(py::init<>(), R"pbdoc(
902-
Default constructor.
905+
:sig=(self: Sims2, word: type) -> None:
903906
904-
This function returns an uninitialized :any:`Sims2` object.
907+
This function returns an uninitialized :any:`Sims2` object that uses
908+
words of type specified by *word*.
909+
910+
:Keyword Arguments:
911+
* **word** (*type*) -- the type of words to use, must be ``list[int]``.
905912
)pbdoc");
906913

907914
s2.def("__copy__", [](Sims2 const& self) { return Sims2(self); });
@@ -1121,9 +1128,13 @@ returned (with ``0`` nodes and ``0`` edges).
11211128
[](RepOrc const& ro) { return to_human_readable_repr(ro); });
11221129

11231130
ro.def(py::init<>(), R"pbdoc(
1124-
Default constructor.
1131+
:sig=(self: RepOrc, word: type) -> None:
1132+
1133+
This function returns an uninitialized :any:`RepOrc` object that uses
1134+
words of type specified by *word*.
11251135
1126-
This function returns an uninitialized :any:`RepOrc` object.
1136+
:Keyword Arguments:
1137+
* **word** (*type*) -- the type of words to use, must be ``list[int]``.
11271138
)pbdoc");
11281139

11291140
ro.def(
@@ -1279,9 +1290,13 @@ edges).
12791290
});
12801291

12811292
mro.def(py::init<>(), R"pbdoc(
1282-
Default constructor.
1293+
:sig=(self: MinimalRepOrc, word: type) -> None:
1294+
1295+
This function returns an uninitialized :any:`MinimalRepOrc` object that uses
1296+
words of type specified by *word*.
12831297
1284-
This function returns an uninitialized :any:`MinimalRepOrc` object.
1298+
:Keyword Arguments:
1299+
* **word** (*type*) -- the type of words to use, must be ``list[int]``.
12851300
)pbdoc");
12861301

12871302
mro.def(
@@ -1386,9 +1401,13 @@ v)` such that every vertex of the word graph is compatible with :math:`(u, v)`.
13861401
});
13871402

13881403
srf.def(py::init<>(), R"pbdoc(
1389-
Default constructor.
1404+
:sig=(self: SimsRefinerFaithful, word: type) -> None:
13901405
1391-
This function returns an uninitialized :any:`SimsRefinerFaithful` object.
1406+
This function returns an uninitialized :any:`SimsRefinerFaithful` object that uses
1407+
words of type specified by *word*.
1408+
1409+
:Keyword Arguments:
1410+
* **word** (*type*) -- the type of words to use, must be ``list[int]``.
13921411
)pbdoc");
13931412

13941413
srf.def(py::init<std::vector<word_type> const&>(),
@@ -1510,9 +1529,13 @@ or two-sided congruences arising from ideals (Rees congruences).
15101529
});
15111530

15121531
sri.def(py::init<>(), R"pbdoc(
1513-
Default constructor.
1532+
:sig=(self: SimsRefinerIdeals, word: type) -> None:
1533+
1534+
This function returns an uninitialized :any:`SimsRefinerIdeals` object that uses
1535+
words of type specified by *word*.
15141536
1515-
This function returns an uninitialized :any:`SimsRefinerIdeals` object.
1537+
:Keyword Arguments:
1538+
* **word** (*type*) -- the type of words to use, must be ``list[int]``.
15161539
)pbdoc");
15171540
sri.def(py::init<Presentation<word_type> const&>(), R"pbdoc(
15181541
:sig=(self: SimsRefinerIdeals, p: Presentation) -> None:

tests/test_sims.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def test_sims1_000():
8989
presentation.add_rule(p, [1, 1], [1])
9090
presentation.add_rule(p, [0, 1, 0, 1], [0])
9191

92-
S = Sims1()
92+
S = Sims1(word=list[int])
9393
assert S.presentation(p).number_of_threads(2).number_of_congruences(5) == 6
9494
with pytest.raises(LibsemigroupsError):
9595
S.number_of_congruences(0)
@@ -124,7 +124,7 @@ def test_sims1_000():
124124
5, lambda wg: check_right_generating_pairs(S, wg)
125125
)
126126
presentation.reverse(p)
127-
S = Sims1()
127+
S = Sims1(word=list[int])
128128
assert S.presentation(p).number_of_congruences(5) == 9
129129
for wg in S.iterator(5):
130130
assert word_graph.follow_path(
@@ -164,7 +164,7 @@ def test_sims1_001():
164164
presentation.add_rule(p, [1, 1], [1])
165165
presentation.add_rule(p, [0, 2], [0, 0])
166166

167-
S = Sims1()
167+
S = Sims1(word=list[int])
168168
S.presentation(p)
169169
assert S.number_of_congruences(1) == 1
170170
assert S.number_of_congruences(2) == 3
@@ -203,7 +203,7 @@ def test_sims1_002():
203203
presentation.add_rule(p, [0, 0, 5, 0, 4], [6])
204204
presentation.add_rule(p, [0, 4, 2, 2, 1, 5, 2], [6])
205205
presentation.add_rule(p, [1, 3, 0, 2, 4, 4, 4], [6])
206-
S = Sims1()
206+
S = Sims1(word=list[int])
207207
S.presentation(p)
208208

209209
S.for_each(3, lambda wg: check_right_generating_pairs(S, wg))
@@ -290,7 +290,7 @@ def test_sims1_003():
290290
presentation.add_rule(p, "aaCac", "e")
291291
presentation.add_rule(p, "acbbACb", "e")
292292
presentation.add_rule(p, "ABabccc", "e")
293-
S = Sims1()
293+
S = Sims1(word=list[int])
294294
S.presentation(to(p, rtype=(Presentation, list[int])))
295295
assert S.number_of_congruences(3) == 14
296296

@@ -331,12 +331,12 @@ def test_sims1_004():
331331
assert S.number_of_congruences(16) == 105
332332
assert S.number_of_congruences(17) == 105
333333

334-
T = Sims2()
334+
T = Sims2(word=list[int])
335335
T.presentation(p)
336336
T.number_of_threads(2)
337337
assert T.number_of_congruences(16) == 13
338338

339-
orc = MinimalRepOrc()
339+
orc = MinimalRepOrc(word=list[int])
340340
d = (
341341
orc.presentation(p)
342342
.target_size(15)
@@ -359,7 +359,7 @@ def test_sims_refiner_faithful_128():
359359
forbid = [[0], [0, 1], [0, 0], []]
360360
pruno = SimsRefinerFaithful(forbid)
361361

362-
S = Sims1()
362+
S = Sims1(word=list[int])
363363
S.presentation(p)
364364
S.add_pruner(pruno)
365365
assert (
@@ -577,34 +577,34 @@ def test_sims2_return_policy():
577577

578578

579579
def test_rep_orc_return_policy():
580-
ro = RepOrc()
580+
ro = RepOrc(word=list[int])
581581
assert ro.init() is ro
582582
assert ro.max_nodes(10) is ro
583583
assert ro.min_nodes(9) is ro
584584
assert ro.target_size(27) is ro
585585

586586

587587
def test_min_rep_orc_return_policy():
588-
ro = MinimalRepOrc()
588+
ro = MinimalRepOrc(word=list[int])
589589
assert ro.init() is ro
590590
assert ro.target_size(27) is ro
591591

592592

593593
def test_sims_refiner_faithful_return_policy():
594-
srf = SimsRefinerFaithful()
594+
srf = SimsRefinerFaithful(word=list[int])
595595

596596
assert srf.init() is srf
597597
assert srf.init([[0, 1], [0]]) is srf
598598

599599

600600
def test_sims_refiner_faithful_call():
601-
srf = SimsRefinerFaithful()
601+
srf = SimsRefinerFaithful(word=list[int])
602602
wg = WordGraph(2, [[0, 1]])
603603
assert srf(wg)
604604

605605

606606
def test_sims_refiner_ideals_return_policy():
607-
sri = SimsRefinerIdeals()
607+
sri = SimsRefinerIdeals(word=list[int])
608608
assert sri.presentation() is sri.presentation()
609609
assert sri.init() is sri
610610
p = Presentation("ab")

0 commit comments

Comments
 (0)