rigid.Diagram

class discopy.rigid.Diagram(dom, cod, boxes, offsets, layers=None)

Bases: discopy.monoidal.Diagram

Implements diagrams in the free rigid monoidal category.

>>> I, n, s = Ty(), Ty('n'), Ty('s')
>>> Alice, jokes = Box('Alice', I, n), Box('jokes', I, n.r @ s)
>>> boxes, offsets = [Alice, jokes, Cup(n, n.r)], [0, 1, 0]
>>> d = Diagram(Alice.dom @ jokes.dom, s, boxes, offsets)
>>> print(d)
Alice >> Id(n) @ jokes >> Cup(n, n.r) @ Id(s)
>>> d.draw(figsize=(3, 2),
...        path='docs/_static/imgs/rigid/diagram-example.png')
../_images/diagram-example.png

Methods

ba(left, right)

Backward application.

bc(left, middle, right)

Backward composition.

bx(left, middle, right)

Backward crossed composition.

caps(left, right)

Constructs nested cups witnessing adjointness of x and y.

cup(x, y)

cups(left, right)

Constructs nested cups witnessing adjointness of x and y.

curry(diagram[, n_wires, left])

Diagram currying.

dagger()

Returns the dagger of self, this method is called using the unary operator [::-1], i.e. self[::-1] == self.dagger().

fa(left, right)

Forward application.

fc(left, middle, right)

Forward composition.

foliate([yield_slices])

>>> x = Ty('x')

fx(left, middle, right)

Forward crossed composition.

normal_form([normalizer])

Implements the normalisation of rigid monoidal categories, see arxiv:1601.05372, definition 2.12.

normalize([left])

Return a generator which yields normalization steps.

permutation(perm[, dom, inverse])

Returns the diagram that encodes a permutation of wires.

spiders(n_legs_in, n_legs_out, typ)

Constructs spiders with compound types.

swap(left, right)

Returns a diagram that swaps the left with the right wires.

transpose([left])

>>> a, b = Ty('a'), Ty('b')

transpose_box(i[, left])

Attributes

l

r

static swap(left, right)

Returns a diagram that swaps the left with the right wires.

Parameters
Returns

diagram – with diagram.dom == left @ right

Return type

monoidal.Diagram

static permutation(perm, dom=None, inverse=False)

Returns the diagram that encodes a permutation of wires.

Warning

This method used to return the inverse permutation up to and including discopy v0.4.2.

Parameters
  • perm (list of int) – such that i goes to perm[i]

  • dom (monoidal.Ty, optional) – of the same length as perm, default is PRO(len(perm)).

  • inverse (bool) – whether to return the inverse permutation.

Returns

diagram

Return type

monoidal.Diagram

foliate(yield_slices=False)
>>> x = Ty('x')
>>> f = Box('f', x, x)
>>> gen = (f @ Id(x) >> (f @ f)).foliate()
>>> print(next(gen))
f @ Id(x) >> Id(x) @ f >> f @ Id(x)
static cups(left, right)

Constructs nested cups witnessing adjointness of x and y.

>>> a, b = Ty('a'), Ty('b')
>>> assert Diagram.cups(a, a.r) == Cup(a, a.r)
>>> assert Diagram.cups(a @ b, (a @ b).r) ==\
...     Id(a) @ Cup(b, b.r) @ Id(a.r) >> Cup(a, a.r)
>>> Diagram.cups(a @ b, (a @ b).r).draw(figsize=(3, 1),\
... margins=(0.3, 0.05), path='docs/_static/imgs/rigid/cups.png')
../_images/cups.png
static caps(left, right)

Constructs nested cups witnessing adjointness of x and y.

>>> a, b = Ty('a'), Ty('b')
>>> assert Diagram.caps(a, a.l) == Cap(a, a.l)
>>> assert Diagram.caps(a @ b, (a @ b).l) == (Cap(a, a.l)
...                 >> Id(a) @ Cap(b, b.l) @ Id(a.l))
static spiders(n_legs_in, n_legs_out, typ)

Constructs spiders with compound types.

static fa(left, right)

Forward application.

static ba(left, right)

Backward application.

static fc(left, middle, right)

Forward composition.

static bc(left, middle, right)

Backward composition.

static fx(left, middle, right)

Forward crossed composition.

static bx(left, middle, right)

Backward crossed composition.

static curry(diagram, n_wires=1, left=False)

Diagram currying.

dagger()

Returns the dagger of self, this method is called using the unary operator [::-1], i.e. self[::-1] == self.dagger().

Returns

arrow – Such that arrow.boxes == [box[::-1] for box in self[::-1]].

Return type

cat.Arrow

Notes

We can check the axioms of dagger (i.e. a contravariant involutive identity-on-objects endofunctor):

>>> x, y, z = Ob('x'), Ob('y'), Ob('z')
>>> f, g = Box('f', x, y), Box('g', y, z)
>>> assert f[::-1][::-1] == f
>>> assert Id(x)[::-1] == Id(x)
>>> assert (f >> g)[::-1] == g[::-1] >> f[::-1]
transpose(left=False)
>>> a, b = Ty('a'), Ty('b')
>>> double_snake = Id(a @ b).transpose()
>>> two_snakes = Id(b).transpose() @ Id(a).transpose()
>>> double_snake == two_snakes
False
>>> *_, two_snakes_nf = monoidal.Diagram.normalize(two_snakes)
>>> assert double_snake == two_snakes_nf
>>> f = Box('f', a, b)
>>> a, b = Ty('a'), Ty('b')
>>> double_snake = Id(a @ b).transpose(left=True)
>>> snakes = Id(b).transpose(left=True) @ Id(a).transpose(left=True)
>>> double_snake == two_snakes
False
>>> *_, two_snakes_nf = monoidal.Diagram.normalize(
...     snakes, left=True)
>>> assert double_snake == two_snakes_nf
>>> f = Box('f', a, b)
normal_form(normalizer=None, **params)

Implements the normalisation of rigid monoidal categories, see arxiv:1601.05372, definition 2.12.

normalize(left=False)

Return a generator which yields normalization steps.

>>> from discopy.rigid import *
>>> n, s = Ty('n'), Ty('s')
>>> cup, cap = Cup(n, n.r), Cap(n.r, n)
>>> f, g, h = Box('f', n, n), Box('g', s @ n, n), Box('h', n, n @ s)
>>> diagram = g @ cap >> f[::-1] @ Id(n.r) @ f >> cup @ h
>>> for d in diagram.normalize(): print(d)  
g... >> Cup(n, n.r) @ Id(n)...
g >> f[::-1] >> Id(n) @ Cap(n.r, n) >> Cup(n, n.r) @ Id(n) >> f >> h
g >> f[::-1] >> f >> h
layer_factory

alias of Layer

id

alias of Id