Skip to content

Monad ↔ Arrow Bridges

The bridges translate between the monad and arrow interfaces (Hughes, 2000). Kleisli presents functions that return monadic values as Kleisli arrows, and ArrowMonad gives a monad interface to an arrow that supports application: the identity monad for the function arrow and the monad m for Kleisli(m), while any other arrow raises TypeError. The kleisli and arrow_monad helpers build them (bridges between the hierarchies), and each construction is lawful only when the identity and composition laws of its argument hold (Moggi, 1991; Wadler, 1992).

bridges

Bridges between the monad and arrow typeclass towers.

Three canonical conversions:

  • kleisli — given a Monad instance m, produce a Kleisli(m) arrow whose hom from A to B is the V-Rel morphism set Hom(A, m(B)).
  • arrow_monad — given an ArrowApply instance a, produce a Monad whose underlying functor is ArrowMonad(a)(α) = a(1, α).
  • cokleisli — comonadic dual of kleisli.

Each bridge is realized at the value level as concrete morphism constructions: compose is Kleisli composition (fmap-then-join), first is the monad's strength applied to the second-factor input, app is the canonical evaluator over the function-space encoding [A → B]. Hughes develops the relation between monads and arrows Hughes (2000). Recovering a monad from a suitable Kleisli arrow uses the identification of arrows out of the unit with monadic values; this is a correspondence under the required laws, not a claim that these runtime objects or theory records are literally identical.

Kleisli didactic-model

Kleisli(*, monad: object = field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY))

Bases: ValueModel

The Kleisli arrow of a monad.

Hom from A to B is the set of morphisms A → m(B) in V-Rel. Composition is Kleisli composition (fmap, then join); identity is Monad.pure. first is realized via the canonical monad strength; app evaluates a function-space embedding through the monad's Monad.apply.

monad is held opaquely so the typeclass-ABC reference survives in-process identity through with_ without panproto schema validation; the field does not round-trip through JSON. It compares by identity: two Kleisli categories are equal exactly when they wrap the same monad object.

ATTRIBUTE DESCRIPTION
monad

The underlying monad instance.

TYPE: Monad

compose

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

Kleisli composition f ; g = join ∘ fmap(g) ∘ f.

f : A → m(B), g : B → m(C) ⊢ g ∘_K f : A → m(C).

Source code in src/quivers/monadic/bridges.py
def compose(self, f: Morphism, g: Morphism) -> Morphism:
    """Kleisli composition ``f ; g = join ∘ fmap(g) ∘ f``.

    ``f : A → m(B)``, ``g : B → m(C)`` ⊢ ``g ∘_K f : A → m(C)``.
    """
    m = self.monad
    if not isinstance(f, Morphism) or not isinstance(g, Morphism):
        raise TypeError(
            "Kleisli.compose requires Morphism arguments; "
            f"got {type(f).__name__}, {type(g).__name__}"
        )
    # Identify the underlying B: f.codomain = m(B), so we recover
    # B by undoing fmap_obj. Since fmap_obj is injective on its
    # representable domain (per the monad's instance contract),
    # we rely on the caller-supplied g.domain to be B.
    B = g.domain
    C = g.codomain  # m's image of the real C
    # The real C is not directly recoverable; we use the
    # monad's fmap to produce m(g) : m(B) → m(m(C')) where
    # C' is whatever B-image the user produced. Then join
    # collapses the double-application.
    fmap_g = m.fmap(B, C, g)
    # Determine the underlying value-codomain by inspecting
    # whether the result already lives inside the monad.
    # For most monads, g.codomain = m(C') for some C'; we
    # recover C' by passing C' = the inverse of fmap_obj.
    # Concretely we call join on the monad applied at C'.
    # Since C is m(C'), we recover C' via the user-supplied
    # signature: if g.codomain.size == m.fmap_obj(C').size for
    # some C', that's our C'. We rely on g being well-typed.
    C_prime = _recover_value_type(m, C)
    join_C = m.join(C_prime)
    return f >> fmap_g >> join_C

first

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

first(f) : (A × C) → m(B × C) via the monad's strength.

Realized as (f × id_C) ; strength_m(B, C).

Source code in src/quivers/monadic/bridges.py
def first(self, f: Morphism, C: SetObject) -> Morphism:
    """``first(f) : (A × C) → m(B × C)`` via the monad's strength.

    Realized as ``(f × id_C) ; strength_m(B, C)``.
    """
    m = self.monad
    mB = f.codomain
    # Recover the underlying B from m(B).
    B = _recover_value_type(m, mB)
    # Build (A × C) → (m(B) × C):
    first_f = parallel(f, id_morph(C))
    # Then strength: m(B) × C → m(B × C) — we use the symmetric
    # form by swapping factors first if the strength is
    # right-strength; here we implement directly with the second-
    # factor strength.
    strength = _monad_strength_second(m, B, C)
    return first_f >> strength

app

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

app : m([A → B]) × m(A) → m(B) via the monad's apply.

Routes through the Applicative-level apply, which every Monad has by inclusion.

Source code in src/quivers/monadic/bridges.py
def app(self, A: SetObject, B: SetObject) -> Morphism:
    """``app : m([A → B]) × m(A) → m(B)`` via the monad's apply.

    Routes through the Applicative-level apply, which every
    Monad has by inclusion.
    """
    return self.monad.apply(A, B)

ArrowMonad didactic-model

ArrowMonad(*, arrow: object = field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY))

Bases: ValueModel

The monad induced by an ArrowApply arrow.

For a lawful ArrowApply interface a, the corresponding monadic values have the form ArrowMonad(a)(α) = a(1, α), where 1 is the monoidal unit Hughes (2000). Realized concretely:

  • fmap_obj(A) = A (the underlying carrier coincides with the target type of the arrow's hom from the unit);
  • pure(A) is the arrow's arr(id_A);
  • join(A) is the arrow's app morphism on the unit-anchored arrow values.

arrow is held opaquely and compares by identity: two arrow monads are equal exactly when they wrap the same arrow object.

ATTRIBUTE DESCRIPTION
arrow

The underlying arrow.

TYPE: ArrowApply

fmap_obj

fmap_obj(A: SetObject) -> SetObject

a(1, A): A for the function arrow, m(A) for Kleisli(m).

Source code in src/quivers/monadic/bridges.py
def fmap_obj(self, A: SetObject) -> SetObject:
    """``a(1, A)``: ``A`` for the function arrow, ``m(A)`` for ``Kleisli(m)``."""
    return self._base().fmap_obj(A)

fmap

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

fmap(f): post-composition with arr(f) on a(1, A).

Source code in src/quivers/monadic/bridges.py
def fmap(self, A: SetObject, B: SetObject, f: Morphism) -> Morphism:
    """``fmap(f)``: post-composition with ``arr(f)`` on ``a(1, A)``."""
    return self._base().fmap(A, B, f)

pure

pure(A: SetObject) -> Morphism

pure : A → a(1, A), the value as a constant arrow.

Source code in src/quivers/monadic/bridges.py
def pure(self, A: SetObject) -> Morphism:
    """``pure : A → a(1, A)``, the value as a constant arrow."""
    return self._base().pure(A)

apply

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

apply : a(1, [A → B]) ⊗ a(1, A) → a(1, B).

Source code in src/quivers/monadic/bridges.py
def apply(self, A: SetObject, B: SetObject) -> Morphism:
    """``apply : a(1, [A → B]) ⊗ a(1, A) → a(1, B)``."""
    return self._base().apply(A, B)

join

join(A: SetObject) -> Morphism

join : a(1, a(1, A)) → a(1, A), running the inner arrow by app.

Source code in src/quivers/monadic/bridges.py
def join(self, A: SetObject) -> Morphism:
    """``join : a(1, a(1, A)) → a(1, A)``, running the inner arrow by ``app``."""
    return self._base().join(A)

bind

bind(A: SetObject, B: SetObject, k: Morphism) -> Morphism

bind(k) = join ∘ fmap(k) for k : A → a(1, B).

Source code in src/quivers/monadic/bridges.py
def bind(self, A: SetObject, B: SetObject, k: Morphism) -> Morphism:
    """``bind(k) = join ∘ fmap(k)`` for ``k : A → a(1, B)``."""
    return self.fmap(A, self.fmap_obj(B), k) >> self.join(B)

lift_a2

lift_a2(A: SetObject, B: SetObject, C: SetObject, f: Morphism) -> Morphism

lift_a2(f) : a(1, A) × a(1, B) → a(1, C).

Source code in src/quivers/monadic/bridges.py
def lift_a2(
    self, A: SetObject, B: SetObject, C: SetObject, f: Morphism
) -> Morphism:
    """``lift_a2(f) : a(1, A) × a(1, B) → a(1, C)``."""
    return self._base().lift_a2(A, B, C, f)

CoKleisli didactic-model

CoKleisli(*, comonad: object = field(default=None, opaque=True, extras=OPAQUE_BY_IDENTITY))

Bases: ValueModel

The CoKleisli category of a comonad.

Hom from A to B is the set of morphisms W(A) → B. Composition is the comonadic CoKleisli composition f =>> g = g ∘ W(f) ∘ δ_A; identity is the counit ε_A.

CoKleisli is registered as Category_ rather than Arrow because the arrow first derivation requires a comonadic costrength W(A × C) → W(A) × C, which is not canonical for an arbitrary comonad. When the underlying comonad supplies a costrength(A, C) method, that morphism composes with parallel(f, ε_C ∘ W(π_2)) to realise first as the full Arrow primitive; the helper first_via_costrength constructs that morphism given an explicit costrength.

comonad is held opaquely so the typeclass-ABC reference survives in-process identity without panproto schema validation. It compares by identity: two CoKleisli categories are equal exactly when they wrap the same comonad object.

ATTRIBUTE DESCRIPTION
comonad

The underlying comonad instance.

TYPE: Comonad

first_via_costrength

first_via_costrength(f: Morphism, C: SetObject, costrength: Morphism) -> Morphism

Realise first(f) given an explicit comonad costrength.

costrength : W(A × C) → W(A) × C is the comonad-specific morphism (analogous to the monad strength). With it, first(f, C) = costrength ; (f × id_C) is a valid CoKleisli arrow W(A × C) ⇝ B × C.

PARAMETER DESCRIPTION
f

A CoKleisli arrow W(A) → B.

TYPE: Morphism

C

The fixed second-factor object.

TYPE: SetObject

costrength

The comonad's costrength W(A × C) → W(A) × C.

TYPE: Morphism

Source code in src/quivers/monadic/bridges.py
def first_via_costrength(
    self, f: Morphism, C: SetObject, costrength: Morphism
) -> Morphism:
    """Realise ``first(f)`` given an explicit comonad costrength.

    ``costrength : W(A × C) → W(A) × C`` is the comonad-specific
    morphism (analogous to the monad strength). With it,
    ``first(f, C) = costrength ; (f × id_C)`` is a valid CoKleisli
    arrow ``W(A × C) ⇝ B × C``.

    Parameters
    ----------
    f : Morphism
        A CoKleisli arrow ``W(A) → B``.
    C : SetObject
        The fixed second-factor object.
    costrength : Morphism
        The comonad's costrength ``W(A × C) → W(A) × C``.
    """
    return costrength >> parallel(f, id_morph(C))

kleisli

kleisli(monad: Monad) -> Kleisli

Wrap a Monad as a Kleisli arrow.

PARAMETER DESCRIPTION
monad

The monad whose Kleisli category the arrow realises.

TYPE: Monad

RETURNS DESCRIPTION
Kleisli

The Kleisli arrow of monad.

Source code in src/quivers/monadic/bridges.py
def kleisli(monad: Monad) -> Kleisli:
    """Wrap a Monad as a Kleisli arrow.

    Parameters
    ----------
    monad : Monad
        The monad whose Kleisli category the arrow realises.

    Returns
    -------
    Kleisli
        The Kleisli arrow of ``monad``.
    """
    return Kleisli(monad=monad)

arrow_monad

arrow_monad(arrow: ArrowApply) -> ArrowMonad

Wrap an ArrowApply arrow as a Monad.

PARAMETER DESCRIPTION
arrow

The arrow whose app supplies the monad's join.

TYPE: ArrowApply

RETURNS DESCRIPTION
ArrowMonad

The monad of arrow.

Source code in src/quivers/monadic/bridges.py
def arrow_monad(arrow: ArrowApply) -> ArrowMonad:
    """Wrap an `ArrowApply` arrow as a Monad.

    Parameters
    ----------
    arrow : ArrowApply
        The arrow whose ``app`` supplies the monad's ``join``.

    Returns
    -------
    ArrowMonad
        The monad of ``arrow``.
    """
    return ArrowMonad(arrow=arrow)

cokleisli

cokleisli(comonad: Comonad) -> CoKleisli

Wrap a Comonad as a CoKleisli category.

The result is registered as Category_ (always) but not Arrow; promoting to an Arrow requires the user-supplied costrength routed through CoKleisli.first_via_costrength.

PARAMETER DESCRIPTION
comonad

The comonad whose CoKleisli category is built.

TYPE: Comonad

RETURNS DESCRIPTION
CoKleisli

The CoKleisli category of comonad.

Source code in src/quivers/monadic/bridges.py
def cokleisli(comonad: Comonad) -> CoKleisli:
    """Wrap a Comonad as a CoKleisli category.

    The result is registered as `Category_` (always) but not
    `Arrow`; promoting to an Arrow requires the user-supplied
    costrength routed through `CoKleisli.first_via_costrength`.

    Parameters
    ----------
    comonad : Comonad
        The comonad whose CoKleisli category is built.

    Returns
    -------
    CoKleisli
        The CoKleisli category of ``comonad``.
    """
    return CoKleisli(comonad=comonad)

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.

Eugenio Moggi. Notions of Computation and Monads. Information and Computation, 93(1):55–92, 1991. URL: https://www.sciencedirect.com/science/article/pii/0890540191900524, doi:10.1016/0890-5401(91)90052-4.

Philip Wadler. The Essence of Functional Programming. In Proceedings of the 19th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, 1–14. ACM, 1992. URL: https://doi.org/10.1145/143165.143169, doi:10.1145/143165.143169.