Built with doc-gen4, running Lean4.
Bubbles () indicate interactive fragments: hover for details, tap to reveal contents.
Use Ctrl+โCtrl+โto navigate, Ctrl+๐ฑ๏ธto focus.
On Mac, use Cmdinstead of Ctrl.
import Mathlib
/-!
# Monads and randomness
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`.
-/
pure: {f : Type ?u.5 โ Type ?u.4} โ [self : Puref] โ {ฮฑ : Type ?u.5} โ ฮฑ โ fฮฑ
pure
42: ?m.22
42 -- `pure` is terminology from any Monad.
/-!
* 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`.
-/
IO.rand -- IO.rand (lo hi : โ) : IO โ
/-!
**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.
-/
/-- A random natural number -/
def
100
/-!
We can run these (every run gives a different result).
```lean
#eval a -- 23
#eval b -- 96
```
Interestingly, we can also run the pair of them and get a result on the diagonal.
```lean
#eval (a, b) -- (87, 87)
```
-/