Skip to content

Arrow Typeclass Hierarchy

Arrows organize computations by their input and output interfaces (Hughes, 2000). The hierarchy is Category_, Arrow, ArrowChoice, ArrowApply, ArrowLoop, ArrowZero, and ArrowPlus. Sequential composition connects matching interfaces, first applies a computation to one component of a pair and carries the other along, and the choice and loop classes add branching and feedback. The bridges relate arrows to monads.

typeclasses

Hughes-style arrow typeclass interfaces.

Each abstract typeclass declares the operations an arrow instance must provide. Concrete instances (Function, VRel, Stochastic, Kleisli(M), Cokleisli(W), LinearMap) live in quivers.arrows.instances.

References

Category_

Bases: ABC

A category of computations.

Provides identity at every object plus binary composition. The underscore disambiguates from quivers.dsl.ast_nodes.CategoryDecl and quivers.stochastic.categories.Category which use the same word in different senses.

Laws:

  • compose(id, f) = f (left identity)
  • compose(f, id) = f (right identity)
  • compose(compose(f, g), h) = compose(f, compose(g, h)) (associativity)

id_arr abstractmethod

id_arr(A: SetObject) -> Morphism

id_A : A ⇝ A.

Source code in src/quivers/arrows/typeclasses.py
@abstractmethod
def id_arr(self, A: SetObject) -> Morphism:
    """``id_A : A ⇝ A``."""

compose abstractmethod

compose(f: Morphism, g: Morphism) -> Morphism

f : A ⇝ B, g : B ⇝ C ⊢ g ∘ f : A ⇝ C.

Source code in src/quivers/arrows/typeclasses.py
@abstractmethod
def compose(self, f: Morphism, g: Morphism) -> Morphism:
    """``f : A ⇝ B, g : B ⇝ C  ⊢  g ∘ f : A ⇝ C``."""

Arrow

Bases: Category_, ABC

An arrow: Category_ with arr and first.

Adds:

  • arr — lift a pure morphism into the arrow.
  • first — apply on the left of a pair, threading a context.

Defaults derivable from these and compose: second f = swap >>> first f >>> swap; f *** g = first f >>> second g (the parallel-product combinator); f &&& g = arr (\x. (x, x)) >>> (f *** g).

Hughes 2000 §3 lists the seven arrow laws.

arr abstractmethod

arr(A: SetObject, B: SetObject, f: Morphism) -> Morphism

Lift a pure morphism: arr(f) : A ⇝ B from f : A → B.

Source code in src/quivers/arrows/typeclasses.py
@abstractmethod
def arr(self, A: SetObject, B: SetObject, f: Morphism) -> Morphism:
    """Lift a pure morphism: ``arr(f) : A ⇝ B`` from ``f : A → B``."""

first abstractmethod

first(f: Morphism, C: SetObject) -> Morphism

f : A ⇝ B ⊢ first(f, C) : (A ⊗ C) ⇝ (B ⊗ C).

Source code in src/quivers/arrows/typeclasses.py
@abstractmethod
def first(self, f: Morphism, C: SetObject) -> Morphism:
    """``f : A ⇝ B  ⊢  first(f, C) : (A ⊗ C) ⇝ (B ⊗ C)``."""

ArrowChoice

Bases: Arrow, ABC

An arrow with sum-elimination: left and (derivably) right.

Adds:

  • left_arr — apply on the left of a coproduct.

Derived: right_arr, +++, |||.

left_arr abstractmethod

left_arr(f: Morphism, C: SetObject) -> Morphism

f : A ⇝ B ⊢ left(f, C) : (A + C) ⇝ (B + C).

Source code in src/quivers/arrows/typeclasses.py
@abstractmethod
def left_arr(self, f: Morphism, C: SetObject) -> Morphism:
    """``f : A ⇝ B  ⊢  left(f, C) : (A + C) ⇝ (B + C)``."""

ArrowApply

Bases: Arrow, ABC

An arrow that can apply arrow-valued data: app.

ArrowApply instances are equivalent in expressive power to quivers.monadic.typeclasses.Monad; the bridges in quivers.monadic.bridges realise the conversion in both directions.

app abstractmethod

app(A: SetObject, B: SetObject) -> Morphism

app : Hom(A, B) ⊗ A ⇝ B.

Hom(A, B) is the internal hom of the arrow's underlying symmetric monoidal closed structure.

Source code in src/quivers/arrows/typeclasses.py
@abstractmethod
def app(self, A: SetObject, B: SetObject) -> Morphism:
    """``app : Hom(A, B) ⊗ A ⇝ B``.

    ``Hom(A, B)`` is the internal hom of the arrow's underlying
    symmetric monoidal closed structure.
    """

ArrowLoop

Bases: Arrow, ABC

An arrow with feedback: loop.

Encodes traced symmetric monoidal structure on the arrow category.

For an arrow f : (A ⊗ C) ⇝ (B ⊗ C), loop(f) : A ⇝ B closes the loop on C. VRel implements finite diagonal contraction, the compact-closed trace for weighted relations over a commutative semiring. Total finite functions and normalized stochastic kernels do not have this general trace, so Function and Stochastic are not registered here. A chart's closure algorithm is a separate operation; registering this interface does not establish a recursive chart solver.

loop_arr abstractmethod

loop_arr(f: Morphism, C: SetObject) -> Morphism

f : (A ⊗ C) ⇝ (B ⊗ C) ⊢ loop(f) : A ⇝ B.

Source code in src/quivers/arrows/typeclasses.py
@abstractmethod
def loop_arr(self, f: Morphism, C: SetObject) -> Morphism:
    """``f : (A ⊗ C) ⇝ (B ⊗ C)  ⊢  loop(f) : A ⇝ B``."""

ArrowZero

Bases: Arrow, ABC

An arrow with a designated bottom at every hom: zero_arr.

zero_arr abstractmethod

zero_arr(A: SetObject, B: SetObject) -> Morphism

zero : A ⇝ B — the bottom element of Hom(A, B).

Source code in src/quivers/arrows/typeclasses.py
@abstractmethod
def zero_arr(self, A: SetObject, B: SetObject) -> Morphism:
    """``zero : A ⇝ B`` — the bottom element of ``Hom(A, B)``."""

ArrowPlus

Bases: ArrowZero, ABC

An ArrowZero with pointwise alternative: alt_arr.

alt_arr plays the role of Alternative.alt lifted to the arrow category's hom-sets.

alt_arr abstractmethod

alt_arr(A: SetObject, B: SetObject) -> Morphism

Hom(A, B) ⊗ Hom(A, B) ⇝ Hom(A, B).

Source code in src/quivers/arrows/typeclasses.py
@abstractmethod
def alt_arr(self, A: SetObject, B: SetObject) -> Morphism:
    """``Hom(A, B) ⊗ Hom(A, B) ⇝ Hom(A, B)``."""

Package constants

The arrow typeclass theories of quivers.arrows, each a TypeclassTheory.

Hughes-style arrow hierarchy for compositional computation.

Arrows present effectful computation as a category of computations rather than as a sequence of binds. Following Hughes 2000, the hierarchy is:

Category_
├── Arrow
│   ├── ArrowChoice
│   ├── ArrowApply       — equivalent power to Monad via ArrowMonad
│   ├── ArrowLoop        — feedback / fixed points
│   └── ArrowZero
│       └── ArrowPlus

Each typeclass is an ABC declaring the operations an arrow instance must provide; default derivations are supplied where Hughes admits them. Concrete instances (Function, VRel, Stochastic, Kleisli(M), Cokleisli(W), LinearMap) live alongside in quivers.arrows.instances.

Bridges between the monad and arrow hierarchies live in quivers.monadic.bridges: every Monad gives a Kleisli arrow, every ArrowApply gives a Monad. The two presentations of effectful computation are interchangeable through these conversions.

References

ThCategory_ module-attribute

ThCategory_ = TypeclassTheory(name='ThCategory_', operations=(operation('id_arr'), operation('compose', 'f', 'g')), equations=(law('category_left_identity', 'compose(id_arr(), f)', 'f'), law('category_right_identity', 'compose(f, id_arr())', 'f'), law('category_associativity', 'compose(compose(f, g), h)', 'compose(f, compose(g, h))')), extends=(ThCombinators,))

The theory of Category_.

ThProducts module-attribute

ThProducts = TypeclassTheory(name='ThProducts', operations=(operation('pair', 'x', 'y'), operation('fst'), operation('snd'), operation('cross', 'f', 'g'), operation('assoc'), operation('unassoc'), operation('swap')), equations=(law('product_fst', 'app(fst(), pair(x, y))', 'x'), law('product_snd', 'app(snd(), pair(x, y))', 'y'), law('product_cross', 'app(cross(f, g), pair(x, y))', 'pair(app(f, x), app(g, y))'), law('product_assoc', 'app(assoc(), pair(pair(x, y), z))', 'pair(x, pair(y, z))'), law('product_unassoc', 'app(unassoc(), pair(x, pair(y, z)))', 'pair(pair(x, y), z)'), law('product_swap', 'app(swap(), pair(x, y))', 'pair(y, x)')), extends=(ThCombinators,))

Pairs and the functions on them the arrow laws use.

ThSums module-attribute

ThSums = TypeclassTheory(name='ThSums', operations=(operation('inl', 'x'), operation('inr', 'y'), operation('inlfn'), operation('plus', 'f', 'g')), equations=(law('sum_inlfn', 'app(inlfn(), x)', 'inl(x)'), law('sum_plus_inl', 'app(plus(f, g), inl(x))', 'inl(app(f, x))'), law('sum_plus_inr', 'app(plus(f, g), inr(y))', 'inr(app(g, y))')), extends=(ThCombinators,))

Injections and the functions on sums the choice laws use.

ThArrow module-attribute

ThArrow = TypeclassTheory(name='ThArrow', operations=(operation('arr', 'f'), operation('first', 'a')), equations=(law('arrow_identity', 'arr(id())', 'id_arr()'), law('arrow_composition', 'arr(app(app(dot(), g), f))', 'compose(arr(f), arr(g))'), law('arrow_first_arr', 'first(arr(f))', 'arr(cross(f, id()))'), law('arrow_first_compose', 'first(compose(f, g))', 'compose(first(f), first(g))'), law('arrow_first_fst', 'compose(first(f), arr(fst()))', 'compose(arr(fst()), f)'), law('arrow_first_cross', 'compose(first(f), arr(cross(id(), g)))', 'compose(arr(cross(id(), g)), first(f))'), law('arrow_first_assoc', 'compose(first(first(f)), arr(assoc()))', 'compose(arr(assoc()), first(f))')), extends=(ThCategory_, ThProducts))

The theory of Arrow.

ThArrowChoice module-attribute

ThArrowChoice = TypeclassTheory(name='ThArrowChoice', operations=(operation('left_arr', 'a'),), equations=(law('choice_left_arr', 'left_arr(arr(f))', 'arr(plus(f, id()))'), law('choice_left_compose', 'left_arr(compose(f, g))', 'compose(left_arr(f), left_arr(g))'), law('choice_left_inl', 'compose(f, arr(inlfn()))', 'compose(arr(inlfn()), left_arr(f))')), extends=(ThArrow, ThSums))

The theory of ArrowChoice.

ThArrowApply module-attribute

ThArrowApply = TypeclassTheory(name='ThArrowApply', operations=(operation('app_arr'), operation('pairwith', 'x'), operation('arrpair')), equations=(law('apply_pairwith', 'app(pairwith(x), y)', 'pair(x, y)'), law('apply_arrpair', 'app(arrpair(), x)', 'arr(pairwith(x))'), law('apply_identity', 'compose(first(arr(arrpair())), app_arr())', 'id_arr()')), extends=(ThArrow,))

The theory of ArrowApply.

ThArrowLoop module-attribute

ThArrowLoop = TypeclassTheory(name='ThArrowLoop', operations=(operation('loop_arr', 'a'), operation('second', 'a')), equations=(law('loop_second', 'second(f)', 'compose(arr(swap()), compose(first(f), arr(swap())))'), law('loop_left_tightening', 'loop_arr(compose(first(h), f))', 'compose(h, loop_arr(f))'), law('loop_right_tightening', 'loop_arr(compose(f, first(h)))', 'compose(loop_arr(f), h)'), law('loop_sliding', 'loop_arr(compose(f, arr(cross(id(), k))))', 'loop_arr(compose(arr(cross(id(), k)), f))'), law('loop_vanishing', 'loop_arr(loop_arr(f))', 'loop_arr(compose(arr(unassoc()), compose(f, arr(assoc()))))'), law('loop_superposing', 'second(loop_arr(f))', 'loop_arr(compose(arr(assoc()), compose(second(f), arr(unassoc()))))')), extends=(ThArrow,))

The theory of ArrowLoop.

ThArrowZero module-attribute

ThArrowZero = TypeclassTheory(name='ThArrowZero', operations=(operation('zero_arr'),), extends=(ThArrow,))

The theory of ArrowZero.

ThArrowPlus module-attribute

ThArrowPlus = TypeclassTheory(name='ThArrowPlus', operations=(operation('alt_arr', 'f', 'g'),), equations=(law('plus_left_identity', 'alt_arr(zero_arr(), f)', 'f'), law('plus_right_identity', 'alt_arr(f, zero_arr())', 'f'), law('plus_associativity', 'alt_arr(alt_arr(f, g), h)', 'alt_arr(f, alt_arr(g, h))')), extends=(ThArrowZero,))

The theory of ArrowPlus.

Cited sources

John Hughes. Generalising Monads to Arrows. Science of Computer Programming, 37(1–3):67–111, 2000. URL: https://www.sciencedirect.com/science/article/pii/S0167642399000234, doi:10.1016/S0167-6423(99)00023-4.