Documentation

PnP2023.Lec_03_01.RandomIO

Monads and randomness #

We will first recall the Option monad and its properties.

For both these it is more pleasant to use the do notation.

Equivalent to the second is that there is a function Option Option α → Option α called join.

IO Monad #

Roughly wraps with the state of the real world.

For example, a random number is wrapped in this.

Question:: Why not just ?

We still do want a natural number. To get this we can run the IO computation.

def rnd (lo : ) (hi : ) :

A random natural number

Instances For

    This does not lead to a contradiction, though in a way that may be somewhat surprising.

    def a :

    A random number between 0 and 100

    Instances For
      def b :

      A random number between 0 and 100

      Instances For

        We can run these (every run gives a different result).

        #eval a -- 23
        #eval b -- 96
        

        Interestingly, we can also run the pair of them and get a result on the diagonal.

        #eval (a, b) -- (87, 87)
        
        def rndPair (lo : ) (hi : ) :

        A random pair

        Instances For
          #eval a -- 23
          #eval b -- 96
          
          #eval (a, b) -- (87, 87)