In part I of this blog series I showed a simple InfinityMonad, which allows to treat special calculations as infinity and in the second part I showed the UndoMonad, which defines an environment which allows to undo and redo state changes.
This and the following posts are based on a famous paper by Martin Erwig and Steve Kollmansberger called "Functional Pearls: Probabilistic functional programming in Haskell".
Let’s start by looking at a small scenario:
This simple query calculates the probability of the event, that an dice roll gives a value greater than 3 and an independent coin flip gives “Heads”. In order to do this the DistributionMonad enumerates all possibilities and calculates the joint probability using the following formula:
If we want the nice syntactic sugar we can easily define a computation expression builder:
In order to allow easier access to our monad, we define some helper functions and basic distributions for fair coins and dices:
In the next part of this blog series I will show how we can utilize the DistributionMonad in order to solve the famous Monty Hall problem.
Tags: F#, monad