chapter fourteen

14 Third abstract nonsense interlude: the probability monad

 

This chapter covers

  • The discrete distribution type is an additive monad
  • A critique of the work so far

In the first abstract nonsense interlude (Chapter 7) we saw how we can view generic type systems through the lens of category theory. Generic types can be thought of as “endofunctors”: functions that take and return types. In the second interlude (Chapter 11) we looked at the monad design pattern, also inspired by category theory, and saw that generic types as diverse as nullable, sequence, lazy and task types all have a way to transform workflows of “regular” types into identical workflows of the generic types. In this short final interlude, we’ll look at one more example: the probability distribution type is also a monad. any workflow that manipulates normal values can be made to also manipulate random values.

14.1 The requirements to be a monad

In chapter 11 we identified three key parts of the monad pattern: it’s a generic type that adds a new capability, it has a “unit” operation that can wrap any value, and it can bind a new function to an existing workflow. The probability distribution type meets two requirements:

14.2 Probability distributions as additive monads

14.3 A critique

14.4 Summary