|
21 | 21 | >>> assert F(arrow) == (h >> f >> g)[::-1]
|
22 | 22 | """
|
23 | 23 |
|
24 |
| -from functools import reduce as fold |
| 24 | +from functools import total_ordering |
25 | 25 | from discopy import messages
|
26 | 26 |
|
27 | 27 |
|
| 28 | +@total_ordering |
28 | 29 | class Ob:
|
29 | 30 | """
|
30 | 31 | Defines an object in a free category, only distinguished by its name.
|
@@ -89,6 +90,9 @@ def __eq__(self, other):
|
89 | 90 | def __hash__(self):
|
90 | 91 | return hash(self.name)
|
91 | 92 |
|
| 93 | + def __lt__(self, other): |
| 94 | + return self.name < other.name |
| 95 | + |
92 | 96 |
|
93 | 97 | class Arrow:
|
94 | 98 | """
|
@@ -278,34 +282,6 @@ def __rshift__(self, other):
|
278 | 282 | def __lshift__(self, other):
|
279 | 283 | return other.then(self)
|
280 | 284 |
|
281 |
| - def compose(self, *others, backwards=False): |
282 |
| - """ |
283 |
| - Returns the composition of self with a list of other arrows. |
284 |
| -
|
285 |
| - Parameters |
286 |
| - ---------- |
287 |
| - others : list |
288 |
| - Other arrows. |
289 |
| - backwards : bool, optional |
290 |
| - Whether to compose in reverse, default is :code`False`. |
291 |
| -
|
292 |
| - Returns |
293 |
| - ------- |
294 |
| - arrow : cat.Arrow |
295 |
| - Such that :code:`arrow == self >> others[0] >> ... >> others[-1]` |
296 |
| - if :code:`backwards` else |
297 |
| - :code:`arrow == self << others[0] << ... << others[-1]`. |
298 |
| -
|
299 |
| - Examples |
300 |
| - -------- |
301 |
| - >>> x, y, z = Ob('x'), Ob('y'), Ob('z') |
302 |
| - >>> f, g, h = Box('f', x, y), Box('g', y, z), Box('h', z, x) |
303 |
| - >>> assert Arrow.compose(f, g, h) == f >> g >> h |
304 |
| - >>> assert f.compose(g, h) == Id(x).compose(f, g, h) == f >> g >> h |
305 |
| - >>> assert h.compose(g, f, backwards=True) == h << g << f |
306 |
| - """ |
307 |
| - return fold(lambda f, g: f << g if backwards else f >> g, others, self) |
308 |
| - |
309 | 285 | def dagger(self):
|
310 | 286 | """
|
311 | 287 | Returns the dagger of `self`, this method is called using the unary
|
@@ -382,6 +358,7 @@ class AxiomError(Exception):
|
382 | 358 | """
|
383 | 359 |
|
384 | 360 |
|
| 361 | +@total_ordering |
385 | 362 | class Box(Arrow):
|
386 | 363 | """ Defines a box as an arrow with the list of only itself as boxes.
|
387 | 364 |
|
@@ -477,6 +454,9 @@ def __eq__(self, other):
|
477 | 454 | return len(other) == 1 and other[0] == self
|
478 | 455 | return False
|
479 | 456 |
|
| 457 | + def __lt__(self, other): |
| 458 | + return self.name < other.name |
| 459 | + |
480 | 460 |
|
481 | 461 | class Functor:
|
482 | 462 | """
|
|
0 commit comments