# Monads in Elm

## 1 Chaining Things Together

Elm has functions like these

`andThen : (a -> Maybe b) -> Maybe a -> Maybe bandThen : (a -> Parser b) -> Parser a -> Parser b`
`List.head : List a -> Maybe a`
`type Maybe a = Just a | Nothing`
`> List.head [1,2,3]  Just 1 : Maybe number> List.head []  Nothing : Maybe a`
`> [[1,2,3]] |> List.headJust [1,2,3]`
`x |> f |> g`
`Maybe.andThen : (a -> Maybe b) -> Maybe a -> Maybe b`
`> [[1,2,3]] |> List.head |> Maybe.andThen List.headJust 1 : Maybe number`
`> [[]] |> List.head |> Maybe.andThen List.headNothing : Maybe b`

# 2 A Pattern

The general pattern for` andThen`, whether it be` Maybe.andThen` or` Parser.andThen`, or something else, is

`andThen : (a -> M b) -> M a -> M b`
`b : M a ->  (a -> M b) -> M b`
`(>>=) : M a -> (a -> M b) -> M b`

# 3 Composition

Consider functions` f: a -> b` and` g : b -> c`. One can form the composite function` g o f : a -> c`. More generally, composition can be formed whenever the type of the codomain of` f` is the same as the type of the domain of` g`.

`f : a -> M bg : b -> M c`
`g o' f = \x -> andThen g (f x)`
`g o' f = \x -> (f x) |> andThen g`
`g o' f = \x -> (f x) >>= g`

# 4 Properties

There is a bit more to the monad story than has be recounted so far. The new composition law, like the usual one, should satisfy the identities

`id o' f = f o' id = f`
`f o' (g o' h) = (f o' g) o' h`

# 5 Summary

• Elm has monads.
• Monads are good because they make it possible to compose functions where this seems to be impossible.
• Monads are not mysterious: they are type constructors (or functors) with certain additional properties, one of which is to make a composition operation possible.

# 6 References

• Kleisli categories, in Bartosz Milewski’s Programming Cafe: Category Theory, Haskell, Concurrency, C++. Studying this reference was what finally turned on the lightbulb for me. My advice is to skip over the C++ parts and concentrate on the Haskell parts. Even better: his book (PDF).
• Joël Quenneville, The Mechanics of Maybe. I’ve adapted the example of deeply nested lists from this article.

--

--