ar_factory

Contents

ar_factory#

discopy.cat.ar_factory(cls)[source]#

Allows the identity and composition of an Arrow subclass to remain within the subclass.

Parameters:

cls – Some subclass of Arrow.

Note

The factory method pattern (FMP) is used all over DisCoPy.

Example

Let’s create Circuit as a subclass of Arrow with an Ob subclass Qubit as domain and codomain.

>>> from discopy.cat import Ob, Arrow, Box
>>> class Qubit(Ob):
...     pass
>>> @ar_factory
... class Circuit(Arrow):
...     ob = Qubit

The Circuit subclass itself has a subclass Gate as boxes.

>>> class Gate(Box, Circuit):
...     pass

The identity and composition of Circuit is again a Circuit.

>>> X = Gate('X', Qubit(), Qubit())
>>> assert isinstance(X >> X, Circuit)
>>> assert isinstance(Circuit.id(), Circuit)
>>> assert isinstance(Circuit.id().dom, Qubit)