Skip to content

Commit cf3d7bd

Browse files
committed
total_ordering boxes
add functorial-learning
1 parent 98e1618 commit cf3d7bd

File tree

2 files changed

+440
-29
lines changed

2 files changed

+440
-29
lines changed

discopy/cat.py

+9-29
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
>>> assert F(arrow) == (h >> f >> g)[::-1]
2222
"""
2323

24-
from functools import reduce as fold
24+
from functools import total_ordering
2525
from discopy import messages
2626

2727

28+
@total_ordering
2829
class Ob:
2930
"""
3031
Defines an object in a free category, only distinguished by its name.
@@ -89,6 +90,9 @@ def __eq__(self, other):
8990
def __hash__(self):
9091
return hash(self.name)
9192

93+
def __lt__(self, other):
94+
return self.name < other.name
95+
9296

9397
class Arrow:
9498
"""
@@ -278,34 +282,6 @@ def __rshift__(self, other):
278282
def __lshift__(self, other):
279283
return other.then(self)
280284

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-
309285
def dagger(self):
310286
"""
311287
Returns the dagger of `self`, this method is called using the unary
@@ -382,6 +358,7 @@ class AxiomError(Exception):
382358
"""
383359

384360

361+
@total_ordering
385362
class Box(Arrow):
386363
""" Defines a box as an arrow with the list of only itself as boxes.
387364
@@ -477,6 +454,9 @@ def __eq__(self, other):
477454
return len(other) == 1 and other[0] == self
478455
return False
479456

457+
def __lt__(self, other):
458+
return self.name < other.name
459+
480460

481461
class Functor:
482462
"""

0 commit comments

Comments
 (0)