Monads in Elm

1 Chaining Things Together

Elm has functions like these

andThen : (a -> Maybe b) -> Maybe a -> Maybe b
andThen : (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.head
Just [1,2,3]
x |> f |> g
Maybe.andThen : (a -> Maybe b) -> Maybe a -> Maybe b
> [[1,2,3]] |> List.head |> Maybe.andThen List.head
Just 1 : Maybe number
> [[]] |> List.head |> Maybe.andThen List.head
Nothing : 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 b
g : 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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store