- Given
a: Option αandf : α → βwe get a term of typeOption βby writinga.map f. - Given
a: Option αandf: α → Option βwe get a term of typeOption βby writinga.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.
IO Monad #
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.
This does not lead to a contradiction, though in a way that may be somewhat surprising.
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)