Monads Need Not Be Endofunctors Thorsten Altenkirch, University of - - PowerPoint PPT Presentation

monads need not be endofunctors
SMART_READER_LITE
LIVE PREVIEW

Monads Need Not Be Endofunctors Thorsten Altenkirch, University of - - PowerPoint PPT Presentation

Monads Need Not Be Endofunctors Thorsten Altenkirch, University of Nottingham James Chapman, Institute of Cybernetics, Tallinn Tarmo Uustalu, Institute of Cybernetics, Tallinn ScotCats, Edinburgh, 21 May 2010 Motivation Monads are the most


slide-1
SLIDE 1

Monads Need Not Be Endofunctors

Thorsten Altenkirch, University of Nottingham James Chapman, Institute of Cybernetics, Tallinn Tarmo Uustalu, Institute of Cybernetics, Tallinn ScotCats, Edinburgh, 21 May 2010

slide-2
SLIDE 2

Motivation

Monads are the most successful pattern in functional programming and Type Theory. Useful for modelling effects (e.g. error, state, etc), but also other programming idioms (e.g. generalized syntactic structures). Monads, and constructions on monads (such as monad transformers) are key to reusable strutures. Frequently, we find structures that fail to be monads as if for the only reason that the underlying functor is not an endofunctor. E.g., untyped/typed lambda calculus syntax (over finite contexts), finite-dimensional vector spaces etc. Can/should one develop a theory of such structures?

slide-3
SLIDE 3

Example: Vector spaces

Let F be the skeletal category of finite sets (|F| = N). Jf ∈ F → Set is the obvious embedding. Let (R, +, 0, ×, 1) be a semiring. We define Vec ∈ |F| → |Set| Vec m =df Jf m → R ηm ∈ Jf m → Vec m ηm i =df λj. if i = j then 1 else 0 (−)∗ ∈ (Jf m → Vec n) → (Vec m → Vec n) A∗ a =df λj.

i∈m

a i × A i j Check that: k∗ ◦ ηX = k η∗

X

= idVec X (l∗ ◦ k)∗ = l∗ ◦ k∗

slide-4
SLIDE 4

Relative monads

Given a category C and another category J with a functor J ∈ [J, C]. A relative monad is given by

an object function T ∈ |J| → |C|, for any object X ∈ |J|, a map ηX ∈ C(J X, T X) (unit), for any objects X, Y ∈ |J| and map k ∈ C(J X, T Y ), a map k∗ ∈ C(T X, T Y ) (Kleisli extension)

satisfying

for any X, Y ∈ |J|, k ∈ C(J X, T Y ), k∗ ◦ ηX = k, for any X ∈ |J|, η∗

X = idT X ∈ C(T X, T X),

for any X, Y , Z ∈ |J|, k ∈ C(J X, T Y ), ℓ ∈ C(J Y , T Z), (ℓ∗ ◦ k)∗ = ℓ∗ ◦ k∗ ∈ C(T X, T Z).

T is functorial with T f = (η ◦ J f )∗; η and (−)∗ are natural.

slide-5
SLIDE 5

Relative monads (ctd)

Clearly T = Vec with J = F and J = Jf is an instance. Ordinary monads arise as as the special case where J =df C, J =df IdC. Any monad (T, η, (−)∗) on C restricts to a relative monad (T ♭, η♭, (−)∗♭) on J defined by T ♭X =df T (J X), η♭

X =df ηJ X, k∗♭ =df k∗.

slide-6
SLIDE 6

Example: Untyped lambda calculus syntax

Define T as the initial algebra of F ∈ [F, Set] → [F, Set] defined by F G X =df J X + (G X × G X + G (1 + X)) (the terms of untyped lambda calculus). T is a relative monad, with η the inclusion of variables to terms and (−)∗ substitution.

slide-7
SLIDE 7

Example: Typed lambda calculus syntax

Let Ty be the set of types of typed lambda calculus (over some base types). Let F ↓ Ty be the category whose objects are pairs (Γ, ρ) where Γ ∈ |F| and ρ ∈ Γ → Ty and maps from (Γ, ρ) to (Γ′, ρ′) are maps f ∈ F(Γ, Γ′) such that ρ = ρ′ ◦ f (the contexts and context maps). Let J ∈ F ↓ Ty → [Ty, Set] be the natural embedding. T (the terms) can be defined as an initial algebra of a suitable endofunctor on [F ↓ Ty, [Ty, Set]]. T is a relative monad.

slide-8
SLIDE 8

Example: Indexed Functors

Let U be the category of small sets. The functor JU ∈ [U, Cat] views a small set as a category. IF ∈ [U, Cat] defined by IF A =df [[JU A, U], U] gives rise to a relative monad. The definitions of η and (−)∗ correspond to the continuation monad (apart from the size issue). This showed up in our work on indexed containers (LICS 09), which also form a relative monad.

slide-9
SLIDE 9

Relative adjunctions

Given two categories C, D together with a third category J and a functor J ∈ J → C. Given L ∈ [C, D], R ∈ [D, C]: L ⊣J R (L is a relative left adjoint to R), if C(J X, R Y ) ≃ D(L X, Y ) A relative adjunction gives rise to a relative monad T = R · L. D

R

  • ⊣J

J

L

  • T
  • J

C

slide-10
SLIDE 10

Kleisli and Eilenberg-Moore constructions

Given a relative monad we can define its initial (Kl(T)) and terminal (EM(T)) splitting as a relative adjunction. |Kl(T)| = |J| and Kl(T)(X, Y ) =df C(J X, T Y ). Kleisli categories for the examples: Vector spaces Finite dimensional vector spaces λ calculus (untyped/typed) contexts and substitutions. Indexed Functors Functors between different slices. To define EM(T) we define the notion of an EM-algebra without referring to µ. An EM-algebra is given by family of maps aX ∈ C(J X, A) → C(T X, A) such that a ρ ◦ η = ρ and a(a ρ ◦ k) = a ρ ◦ k∗

slide-11
SLIDE 11

Relative Monads as monoids?

Can we have a monoid form of relative monads? Here is a calculation in the end-coend calculus:

  • X,Y ∈|J|

C(JX, TY ) → C(TX, TY ) ∼ =

  • Y ∈|J|

C( X∈|J| C(JX, TY ) • TX, TY ) ∼ =

  • Y ∈|J|

C(LanJ T (TY ), TY ) ∼ = [J, C](LanJ T · T, T) Assume henceforth that LanJ ∈ [J, C] → [C, C] exists.

slide-12
SLIDE 12

[J, C] is lax monoidal

We can define

for any objects F, G ∈ |[J, C]|, an object G ·J F ∈ |[J, C]| by G ·J F =df LanJ G · F.

We can also define

for any object F ∈ |[J, C]|, a map λF ∈ [J, C](LanJ J · F, F), for any object F ∈ |[J, C]|, a map ρF ∈ [J, C](F, LanJ F · J), for any objects F, G, H ∈ |[J, C]|, a map αH,G,F ∈ [J, C](LanJ (LanJ H·G)·F, LanJ H·LanJ G·F).

([J, C], J, ·J, λ, ρ, α) is a lax monoidal category, i.e., ·J is functorial, λ, ρ, α are natural (however not generally isomorphisms) and satisfy certain coherence conditions.

slide-13
SLIDE 13

Relative monads = lax monoids

Relative monads on J are the same as lax monoids in the lax monoidal structure on [J, C], i.e., triples (T, η, µ) with T ∈ |[J, C]|, η ∈ [J, C](J, T) and µ ∈ [J, C](T ·J T, T) such that J ·J T

η·JT

  • λ

T ·J T

µ

  • T
  • T

T ·J T

µ

  • T ·J J

T·Jη

  • T

ρ

  • T
  • T ·J (T ·J T)

T·Jµ T ·J T µ

  • (T ·J T) ·J T

α

  • µ·JT

T ·J T

µ

T

slide-14
SLIDE 14

Assume further conditions on J. . .

Assume that, in addition to the existence of LanJ, J further satisfies these conditions:

J is fully faithful, i.e., for any X, Y ∈ |J|, there is an inverse to the canonical map JX,Y ∈ J(X, Y ) → C(J X, J Y ) given by JX,Y f =df J f , J is dense, i.e., for any X, Y ∈ |C|, there is an inverse to the canonical map KX,Y ∈ C(X, Y ) → [Jop, Set](C(J −, X), C(J −, Y )) given by KX,Y g f =df g ◦ f , For any F ∈ J → C, X ∈ |J|, Y ∈ |C|, there is an inverse to the canonical map LF

X,Y ∈ LanJ(C(J X, F −)) Y → C(J X, LanJ F Y ).

The functors J ∈ F → Set and J ∈ F ↓ Ty → [Ty, Set] enjoy these properties.

slide-15
SLIDE 15

[J, C] is monoidal, relative monads = monoids

Then ρ, λ, α have inverses definable in terms of J−1, K −1, L−1. Hence [J, C] is (properly) monoidal. A relative monad T on J is a (proper) monoid in [J, C].

slide-16
SLIDE 16

Relative monads extend to monads

We also get that T extends to a monad on C (a monoid in the strict monoidal category ([C, C], Id, ·)). Define

T ♯ =df LanJ T, η♯ =df Id

λ−1

Id

LanJ J

LanJ η LanJ T ,

µ♯ =df LanJ T · LanJ T

α−1

T,T,Id LanJ (LanJ T · T)

LanJ µ LanJ T

(T ♯, η♯, µ♯) is a monad on C. E.g., untyped lambda calculus syntax extends to a monad

  • n Set, typed lambda calculus syntax to a monad on

[Ty, Set].

slide-17
SLIDE 17

Relative monads extend to monads (ctd)

Furthermore, the defining adjunction of LanJ, [C, C]

−·J

[J, C]

LanJ

  • lifts to an adjunction

Mnd(C)

(−)♭

RMnd(J)

(−)♯

slide-18
SLIDE 18

Summary

No conditions on J

Monads restrict to relative monads Huber’s theorem, Kleisli, E-M constructions

LanJ exists

[J, C] lax monoidal, relative monads = lax monoids

Further conditions on J

[J, C] monoidal, relative monads = monoids Relative monads extend to monads, coreflection

slide-19
SLIDE 19

Arrows

Given a category J, a (weak) arrow on J is given by

an object function R ∈ |J| × |J| → Set, for any objects X, Y ∈ |J|, a function pure ∈ J(X, Y ) → R(X, Y ), for any X, Y , Z ∈ |J|, a function (≪) ∈ R(Y , Z) × R(X, Y ) → R(X, Z)

satisfying

pure (g ◦ f ) = pure g ≪ pure f , r ≪ pure id = r, pure id ≪ r = r, t ≪ (s ≪ r) = (t ≪ s) ≪ r.

R extends to a functor Jop × J → Set (an endoprofunctor

  • n J); pure and ≪ are natural.
slide-20
SLIDE 20

Arrows = relative monads on Yoneda

Assume J is small. Let C =df [Jop, Set], J Y X = J(X, Y ) (the Yoneda embedding). LanJ exists, J is well-behaved. An arrow on J (a functor R ∈ Jop × J → Set with structure) is the same as a relative monad on J (a functor T ∈ J → [Jop, Set] with structure).

  • Cf. Jacobs et al. (2006): Arrows on J are the same as

monoids in the monoidal structure on [Jop × J, Set] (the category of endoprofunctors on J).

slide-21
SLIDE 21

Conclusions

Relative monads are a natural generalization of monads. They are smoothly formulated in Manes’s style, the monoid form needs left Kan extensions. A large part of monad theory carries over with minimal

  • adjustments. There is a clear relationship to ordinary

monads. They cover important examples for programming, in particular, examples with size issues. They also subsume arrows.