Skip to content

Commit

Permalink
more docs, cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
toumix committed Apr 12, 2024
1 parent 4e9d436 commit 7452cff
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
38 changes: 31 additions & 7 deletions discopy/feedback.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,32 @@ def __repr__(self):


class FollowedBy(Box):
""" The isomorphism between `x.head @ x.tail.delay()` and `x`. """
"""
The isomorphism between `x.head @ x.tail.delay()` and `x`.
In the category of streams, this is just the identity.
Example
-------
>>> from discopy import stream
>>> x = Ty(Ob('x', is_constant=False))
>>> FollowedBy(x).draw(path="docs/_static/feedback/followed-by.png")
.. image:: /_static/feedback/followed-by.png
:align: center
>>> F = Functor({x: stream.Ty.sequence('x')}, cod=stream.Category())
>>> X, Xh, Xtd = map(F, (x, x.head, x.tail.delay()))
>>> for xh, xtd in [(Xh.now, Xtd.now),
... (Xh.later.now, Xtd.later.now),
... (Xh.later.later.now, Xtd.later.later.now)]:
... print(f"({xh}, {xtd})")
(x0, Ty())
(Ty(), x1)
(Ty(), x2)
>>> eq_up_to_F = lambda f, g: F(f).unroll(2).now == F(g).unroll(2).now
>>> assert eq_up_to_F(FollowedBy(x), Id(x))
"""
def __init__(self, arg: Ty, is_dagger=False, time_step=0):
self.arg = arg
dagger_name = ", is_dagger=True" if is_dagger else ""
Expand Down Expand Up @@ -606,19 +631,18 @@ class Functor(markov.Functor):

def __call__(self, other):
if isinstance(other, (Ob, Box)) and other.time_step:
cod = getattr(self.cod, "ob" if isinstance(other, Ob) else "ar")
cod = self.cod.ob if isinstance(other, Ob) else self.cod.ar
if hasattr(cod, "delay"):
result = self(other.reset())
for _ in range(other.time_step):
result = result.delay()
return result
if isinstance(other, (HeadOb, TailOb, Head, Tail)):
result = self(other.arg)
cod = self.cod.ar if isinstance(
other, (Head, Tail)) else self.cod.ob
attr = "head" if isinstance(other, (HeadOb, Head)) else "tail"
if hasattr(result, attr):
return getattr(result, attr)
return (
self.ar if isinstance(other, (Head, Tail)) else self.ob)[other]
if hasattr(cod, attr):
return getattr(self(other.arg), attr)
if isinstance(
other, FollowedBy) and hasattr(self.cod.ar, "followed_by"):
arg = other.dom if other.is_dagger else other.cod
Expand Down
Binary file added docs/_static/feedback/followed-by.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 0 additions & 6 deletions test/semantics/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,3 @@ def test_python_stream():
s.unroll().check_later()
assert s.feedback(T(x), T(y), T(m)).unroll(2).now(1, 2, 3) == (
True, False, True, '1 2 3')

x, y, z, m, n = map(Ty.sequence, "xyzmn")
x_, y_, z_, m_, n_ = [Ty.sequence(symmetric.Ty(name + "_")) for name in "xyzmn"]
f, g = Stream.sequence('f', x, y, m), Stream.sequence('g', y, z, n)
f_, g_ = Stream.sequence('f_', x_, y_, m_), Stream.sequence('g_', y_, z_, n_)
LHS, RHS = (f >> g) @ (f_ @ g_), f @ f_ >> g @ g_

0 comments on commit 7452cff

Please sign in to comment.