Diagram#

class discopy.closed.Diagram(inside, dom, cod, _scan=True)[source]#

Bases: discopy.markov.Diagram, discopy.biclosed.Diagram, discopy.abc.ClosedCategory

A closed diagram is both a markov and a biclosed diagram.

A diagram applied to another post-composes their tensor with an Eval.

Parameters:
  • inside (tuple[Layer, ...])

  • dom (C0)

  • cod (C0)

ob#

alias of Ty

to_compact()[source]#

Open the curry bubbles into coevaluation and feedback, which stays a Diagram as a closed category is traced: each curry becomes its argument followed by Coeval, traced over the curried wires, and each term is evaluated first.

Example

>>> x, y, z = map(Ty, "xyz")
>>> f = Box("f", x @ y, z)
>>> assert f.curry().to_compact() == (
...     f >> Coeval(z << y, left=True)).trace()
Return type:

Diagram

to_term()[source]#

Read a causal diagram as a term in fine-grain call-by-value style: one let statement per box in topological order with a variable for each wire, going through Hypergraph so that the copy, discard and swap structure simplifies away into the spiders.

The free variables of the term are the inputs that the diagram actually uses; a diagram with no box is a tuple of variables.

Example

>>> X, Y = Ty("X"), Ty("Y")
>>> f, g = Box("f", X, Y @ Y), Box("g", Y @ Y, Y)
>>> diagram = Diagram.copy(X) >> f @ Diagram.discard(X) >> g
>>> print(diagram.to_term())
let(f(x0), lambda x1, x2: g(Tuple(x1, x2)))
>>> assert Diagram.swap(X, Y).to_term()\
...     == Tuple(Variable("x1", Y), Variable("x0", X))
Return type:

Term

classmethod box_to_term(box, args)[source]#

The application of a box as a constant to variables for its inputs.

coeval_factory#

alias of Coeval

copy_factory#

alias of Copy

curry_factory#

alias of Curry

discard_factory#

alias of Discard

eval_factory#

alias of Eval

factory#

alias of Diagram

functor_factory#

alias of Functor

pack_factory#

alias of Pack

permutation_factory#

alias of Permutation

sum_factory#

alias of Sum

swap_factory#

alias of Swap

trace_factory#

alias of Trace

unpack_factory#

alias of Unpack