# Documentation

PnP2023.Lec_03_01.RandomIO

We will first recall the Option monad and its properties.

• If α is a type Option α is a type.
• Given a: α we get a term of type Option α by writing some a.
• Given a: Option α and f : α → β we get a term of type Option β by writing a.map f.
• Given a: Option α and f: α → Option β we get a term of type Option β by writing a.bind f.

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.

Roughly wraps with the state of the real world.

For example, a random number is wrapped in this.

Question:: Why not just ℕ?

• Otherwise we will violate the principle that the value of a function is determined by its arguments.

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 : ) :
IO ()

A random pair

Instances For
#eval a -- 23
#eval b -- 96

#eval (a, b) -- (87, 87)