What are Haskell's monad transformers in categorical terms?
Asked Answered
S

3

33

As a math student, the first thing I did when I learned about monads in Haskell was check that they really were monads in the sense I knew about. But then I learned about monad transformers and those don't quite seem to be something studied in category theory.

In particular I would expect them to be related to distributive laws but they seem to be genuinely different: a monad transformer is expected to apply to an arbitrary monad while a distributive law is an affair between a monad and a specific other monad.

Also, looking at the usual examples of monad transformers, while MaybeT m composes m with Maybe, StateT m is not a composition of m with State in either order.

So my question is what are monad transformer in categorical language?

Sulky answered 28/7, 2011 at 5:1 Comment(6)
They say a monad transformer in C is a pointed endofunctor (a.k.a. pre-monad) on Monad(C). I'm not sure I understand this definition though. Maybe you do? Then please share ;)Kenna
This article looks like a good place to start.Illaudable
@n.m. Unpacking "pointed endofunctor on Monad(C)" you get that a monad transformer t is something that (1) for each monad m gives you a monad t m and (2) for each monad morphism m->n gives you another monad morphism t m -> t n in a functorial way (i.e., respecting compositions) and t comes with (3) for any monad m, a monad morphism m -> t m which is natural (i.e., for a monad morphism the obvious square you get with vertices m, n, t m, t n commutes). (Endofunctor on Monad(C) is (1) and (2), pointed is (3).)Chlamydospore
Oh, all of that is explained in hammar's link! (Including what is meant here by a monad morphism, a term which I used but did not define.)Chlamydospore
OK, I think the article hammar linked to answers my question: for what they are used for in Haskell programming, monad transformers only need to send monads to monads and allow the extra operations of monads to be lifted. So they are just functions from the set of Monads to itself, not even endofunctors on Monad(Hask), although in practice all the useful ones save for ContT are functorial. If you post your link as answer, hammar, I'd be happy to accept it.Chlamydospore
@Omar: I would suggest you expand that comment into an answer of your own question, there is nothing wrong in doing that. Just make sure that it is selfcontained, ie. add hammars link to it =D.Elrod
P
8

Monad transformers aren't exceedingly mathematically pleasant. However, we can get nice (co)products from free monads, and, more generally, ideal monads: See Ghani and Uustalu's "Coproducts of Ideal Monads": http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.2698

Porringer answered 28/7, 2011 at 9:9 Comment(3)
This paper doesn't talk about monad transformers at all (other than a brief mention), so I wouldn't say it answers my question, but it is interesting, so thanks for pointing it out!Chlamydospore
It does let you get monad transformers in a formal and well-defined and pleasant way, as long as you restrict yourself to the universe of ideal monads.Porringer
That's right (and for those that haven't read the paper sclv means that for any ideal monad m you get the monad transformer that sends a monad n to the coproduct (in the category of monads) of m and n, and it has a nice construction too), but what I meant is that although it give these examples of monad transformer, it doesn't talk about the definition of monad transformer.Chlamydospore
S
3

Calculating monad transformers with category theory by Oleksandr Manzyuk is another article on the Monad transformers, and relates the concept to the important concept of adjunction in the category theory.
Also it uses the most pleasant feature of the category theory, in my opinion, i.e. diagram-chasing, which naturalises the concept a lot.
Hope this helps.

Swop answered 23/2, 2015 at 16:26 Comment(1)
A very interesting construction. Unfortunately, it only works in a handful of cases that Manzyuk shows in his paper. Pretty much no other monad transformer can be found in that way.Alternator
A
0

A monad transformer is a pointed endofunctor in the category of monads. "What's the problem?"

Here are some more details:

Start with some category, in which we consider those endofunctors M that are monads. All those monads M form a category whose morphisms are natural transformations M -> M' that satisfy the laws of monad morphisms.

A monad transformer is a pointed endofunctor in this category of monads. What is an endofunctor T in this category of monads? This endofunctor is a mapping from a monad M to a monad T(M), together with a mapping of any monad morphism M -> M' into a monad morphism T(M) -> T(M').

The endofunctor T is "pointed" if there exists a natural transformation from the identity endofunctor (Id) to T.

The identity endofunctor is an identity map of monads and monad morphisms. A natural transformation Id ~> T is defined by its components at all the monads M. Its component at M is a monad morphism M -> T(M). In addition, there must be a naturality law ("monadic naturality") which says that, for any monads M and M' and any monad morphism M -> M', the diagram consisting of M, M', T(M), T(M') commutes.

This data more or less coincides with the usual data required of a monad transformer. The required monad morphism M -> T(M) is the lifting of the "foreign" monad M into the transformed monad.

The construction also includes the "hoist" function (this is the action of the endofunctor T on monad morphisms), that lifts monad morphisms M -> M' to T(M) -> T(M').

If we consider the image of the identity monad, T(Id), this must be some other monad. Call that the "base monad" of the transformer and denote it by B. We have then the monad morphism B ~> T(M) for any M. This is the lifting from the base monad to the transformed monad.

However, this definition excludes the "non-functorial" monad transformers, such as that of the continuation monad and the codensity monad.

The distributive laws are only relevant to certain monad transformers. There are two kinds of transformers where distributive laws exist: for these transformers, the component at M of the natural transformation Id ~> T is given either by M -> B∘M or by M ->M∘B. But other monads have transformers that are not functor compositions, and there are no distributive laws for them.

More details are written up in Chapter 14 of the upcoming book "The Science of Functional Programming": https://github.com/winitzki/sofp

Alternator answered 19/5, 2021 at 10:11 Comment(1)
This is the same as @hammar's answer 10 years ago.Chlamydospore

© 2022 - 2025 — McMap. All rights reserved.