Skip to content

Instantly share code, notes, and snippets.

@bmorphism
Created July 3, 2024 23:54
Show Gist options
  • Save bmorphism/4b3873dc23ebd4f391288e18f6a0f268 to your computer and use it in GitHub Desktop.
Save bmorphism/4b3873dc23ebd4f391288e18f6a0f268 to your computer and use it in GitHub Desktop.
hypermeat.py
from discopy.monoidal import Ty, Box, Diagram
from pprint import PrettyPrinter
from discopy.drawing import Equation
x, y, z, w = Ty(*"xyzw")
f, g = Box('f', x, y), Box('g', z, w)
assert f @ g == Diagram.decode(
dom=x @ z,
boxes_and_offsets=[(f, 0), (g, 1)])
pprint = PrettyPrinter(sort_dicts=False).pprint
unit, counit = Box('unit', Ty(), x), Box('counit', x, Ty())
cup, cap = Box('cup', x @ x, Ty()), Box('cap', Ty(), x @ x)
for box in [unit, counit, cup, cap]:
box.draw_as_spider, box.color = True, "black"
def spiral(n_cups):
result = unit
for i in range(n_cups):
result = result >> x ** i @ cap @ x ** (i + 1)
result = result >> x ** n_cups @ counit @ x ** n_cups
for i in range(n_cups):
result = result >>\
x ** (n_cups - i - 1) @ cup @ x ** (n_cups - i - 1)
return result
rewrite_steps = spiral(3).normalize()
Diagram.to_gif(
*rewrite_steps,
draw_box_labels=False,
draw_type_labels=False,
loop=True, aspect="equal",
path="spiral.gif")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment