Random sampling for an element #
We implement sampling to find an element with a given property, for instance being prime or being coprime to a given number. For this we need a hypothesis that such an element exists.
We use the
IO monad to generate random numbers. This is because a random number is not a function, in the sense of having value determined by arguments.
The basic way we sample is to choose an element at random from the list, and then check if it satisfies the property. If it does, we return it. If not, we remove it from the list and try again. To show termination we see (following a lab) that the length of the list decreases by at least one each time.
We pick an index of the list
l, which is of type
Fin l.length. Rather than proving that the random number generator has this property we pass
A random element with a given property from a list. As IO may in principle give an error, we specify a default to fallback and the conditions that this is in the list and has the property
Random Monad #
We used the IO Monad which has a lot of stuff besides randomness. We will now demistify this by constructing a Monad for randomness only.