Ty#

class discopy.monoidal.Ty(*inside, dom=None, cod=None, _scan=True, **kwargs)[source]#

Bases: discopy.cat.Ob, FreeMonoid

A type is a composable path of objects with Ty.tensor() as concatenation.

Parameters:
  • inside (str | cat.Ob) – The objects inside the type (or their names).

  • dom (C0) –

  • cod (C0) –

  • _scan (bool) –

Tip

Types can be instantiated with a name rather than object.

>>> assert Ty('x') == Ty(cat.Ob('x'))

Tip

A type can be exponentiated by a natural number.

>>> assert Ty('x') ** 3 == Ty('x', 'x', 'x')

Tip

Types can also be instantiated by keyword, passing the path of generators as inside=; this is what the free-category machinery uses internally, while the variadic form above is the user-friendly Ty('x', 'y') API.

>>> assert Ty(inside=(Wire('x'), Wire('y'))) == Ty('x', 'y')

Note

Types can be indexed and sliced using square brackets. Indexing behaves like that of strings, i.e. when we index a type we get a type back. The objects inside the type are still accessible using .inside.

>>> t = Ty(*"xyz")
>>> assert t[0] == t[:1] == Ty('x')
>>> assert t[0] != t.inside[0] == Wire('x')
>>> assert t[1:] == t[-2:] == Ty('y', 'z')
ob#

alias of Colour

generator_factory#

alias of Wire

cast_wire(x)[source]#

Turn a constructor argument into a self.generator_factory.

Old dumps and pickles used a plain cat.Ob, with no colour, as the generators: upgrade it to Wire(x.name) for subclasses whose generators are plain Wire.

Parameters:

x (str | Ob) –

Return type:

Ob

count(obj)[source]#

Counts the occurrence of a given object (or a type of length 1).

Parameters:

obj (Ob) – The object to count.

Return type:

int

Example

>>> x = Ty('x')
>>> xs = x ** 5
>>> assert xs.count(x) == xs.inside.count(x.inside[0])
unwind()[source]#

Rotate an atomic type to winding number zero.

This is the identity for monoidal types, which have no winding. It is overridden by rigid.Ty to give a canonical representative for the compact quotient, i.e. the base type on which spiders are labelled.

Return type:

Ty

property is_atomic: bool#

Whether a type is atomic, i.e. it has length 1.

wire_offsets()[source]#

The x-position of each wire of the type relative to the first, i.e. the sum of the cell widths max(1, right_margin) of the objects before it: each wire takes up at least a unit, more if its label is longer.

>>> assert Ty('x', 'y').to_drawing().wire_offsets() == [0, 1]
Return type:

list

factory#

alias of Ty