chapter fourteen

14 Third abstract nonsense interlude: The probability monad

 

This chapter covers

  • Seeing the discrete distribution type as an additive monad
  • Critiquing the work so far

In the first abstract nonsense interlude (chapter 7), we saw how to 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 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 manipulate random values too.

14.1 The requirements for a monad

Chapter 11 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.
  • 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

Summary