Skip to content

Profunctors

A profunctor relates objects of two categories without choosing one output for each input (Kelly, 1982). Its finite form here is a weighted table with source and target axes, with values in an IdempotentSemiring, and composing two such tables matches their common axis and reduces over it. The general definition also requires the source and target categories to act compatibly on the table.

profunctors

Profunctors (bimodules) for V-enriched categories.

A V-profunctor R: A ↛ B is a V-valued functor A^op ⊗ B → V, represented here by a tensor of shape (*A.shape, *B.shape). The implementation treats finite weighted relations as profunctor representations; it does not record arbitrary source and target actions.

Between discrete V-categories (finite sets), a profunctor is a V-valued matrix, and composition uses the coend formula:

(R ; S)(a, c) = ∫^b R(a, b) ⊗ S(b, c)

which is matrix composition over V. The coend is a join, so V must be a complete lattice (an IdempotentSemiring); profunctors then compose associatively with the identity matrices as units. This module makes the profunctor structure explicit and provides composition via the coend from ends_coends.py.

This module provides:

Profunctor — explicit profunctor with contravariant/covariant structure

Profunctor

Profunctor(contra: SetObject, co: SetObject, tensor: Tensor, algebra: IdempotentSemiring)

A V-profunctor R: A ↛ B.

Wraps a tensor of shape (*A.shape, *B.shape) with explicit contravariant (A) and covariant (B) objects.

PARAMETER DESCRIPTION
contra

The contravariant (domain/source) object A.

TYPE: SetObject

co

The covariant (codomain/target) object B.

TYPE: SetObject

tensor

The profunctor's tensor of shape (*A.shape, *B.shape).

TYPE: Tensor

algebra

The enrichment algebra, a complete lattice.

TYPE: IdempotentSemiring

RAISES DESCRIPTION
TypeError

If algebra is not an IdempotentSemiring.

ValueError

If the tensor's shape is not (*contra.shape, *co.shape).

Source code in src/quivers/enriched/profunctors.py
def __init__(
    self,
    contra: SetObject,
    co: SetObject,
    tensor: torch.Tensor,
    algebra: IdempotentSemiring,
) -> None:
    self._contra = contra
    self._co = co
    self._tensor = tensor
    self._algebra = require_lattice(algebra, "a profunctor")

    expected_shape = (*contra.shape, *co.shape)

    if tensor.shape != expected_shape:
        raise ValueError(
            f"tensor shape {tensor.shape} does not match expected "
            f"{expected_shape} from contra={contra!r}, co={co!r}"
        )

contra property

contra: SetObject

The contravariant (source) object.

co property

The covariant (target) object.

tensor property

tensor: Tensor

The profunctor tensor.

algebra property

The enrichment algebra.

from_morphism classmethod

from_morphism(morph: Morphism) -> Profunctor

Wrap a finite weighted morphism as a profunctor representation.

PARAMETER DESCRIPTION
morph

A morphism R: A → B.

TYPE: Morphism

RETURNS DESCRIPTION
Profunctor

The profunctor R: A ↛ B with the same tensor.

RAISES DESCRIPTION
TypeError

If the morphism's algebra is not an IdempotentSemiring.

Source code in src/quivers/enriched/profunctors.py
@classmethod
def from_morphism(cls, morph: Morphism) -> Profunctor:
    """Wrap a finite weighted morphism as a profunctor representation.

    Parameters
    ----------
    morph : Morphism
        A morphism R: A → B.

    Returns
    -------
    Profunctor
        The profunctor R: A ↛ B with the same tensor.

    Raises
    ------
    TypeError
        If the morphism's algebra is not an `IdempotentSemiring`.
    """
    return cls(
        contra=morph.domain,
        co=morph.codomain,
        tensor=morph.tensor,
        algebra=require_lattice(morph.algebra, "a profunctor"),
    )

compose

compose(other: Profunctor) -> Profunctor

Profunctor composition via coend.

(R ; S)(a, c) = ∫^b R(a, b) ⊗ S(b, c)

This is equivalent to V-enriched composition via the algebra's compose method.

PARAMETER DESCRIPTION
other

Right profunctor S: B ↛ C.

TYPE: Profunctor

RETURNS DESCRIPTION
Profunctor

Composed profunctor R ; S: A ↛ C.

RAISES DESCRIPTION
TypeError

If the objects do not match or the algebras differ.

Source code in src/quivers/enriched/profunctors.py
def compose(self, other: Profunctor) -> Profunctor:
    """Profunctor composition via coend.

    (R ; S)(a, c) = ∫^b R(a, b) ⊗ S(b, c)

    This is equivalent to V-enriched composition via the
    algebra's compose method.

    Parameters
    ----------
    other : Profunctor
        Right profunctor S: B ↛ C.

    Returns
    -------
    Profunctor
        Composed profunctor R ; S: A ↛ C.

    Raises
    ------
    TypeError
        If the objects do not match or the algebras differ.
    """
    if not self._algebra.is_compatible(other._algebra):
        raise TypeError(
            f"cannot compose a profunctor over {self._algebra.name} with "
            f"one over {other._algebra.name}"
        )
    if self._co != other._contra:
        raise TypeError(
            f"cannot compose: R's covariant object {self._co!r} "
            f"!= S's contravariant object {other._contra!r}"
        )

    # use algebra.compose which is exactly the coend formula
    n_contract = self._co.ndim
    result_tensor = self._algebra.compose(self._tensor, other._tensor, n_contract)

    return Profunctor(
        contra=self._contra,
        co=other._co,
        tensor=result_tensor,
        algebra=self._algebra,
    )

to_morphism

to_morphism() -> ObservedMorphism

Convert back to a morphism (same tensor, different framing).

RETURNS DESCRIPTION
ObservedMorphism

A morphism with domain=contra, codomain=co.

Source code in src/quivers/enriched/profunctors.py
def to_morphism(self) -> ObservedMorphism:
    """Convert back to a morphism (same tensor, different framing).

    Returns
    -------
    ObservedMorphism
        A morphism with domain=contra, codomain=co.
    """
    return observed(
        self._contra,
        self._co,
        self._tensor,
        algebra=self._algebra,
    )

Cited sources

G. M. Kelly. Basic Concepts of Enriched Category Theory. Cambridge University Press, 1982. Reprinted in Reprints in Theory and Applications of Categories 10 (2005), 1–136. URL: https://www.tac.mta.ca/tac/reprints/articles/10/tr10abs.html.