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

/-!
# 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.
-/

universe u
/-- Removing an element from a list does not increase length -/
theorem 
remove_length_le: ∀ {α : Type u} [inst : DecidableEq α] (a : α) (l : List α), List.length (List.remove a l) List.length l
remove_length_le
{
α: Type u
α
:
Type u: Type (u+1)
Type u
} [
DecidableEq: Sort ?u.4 → Sort (max1?u.4)
DecidableEq
α: Type u
α
](
a: α
a
:
α: Type u
α
) (
l: List α
l
:
List: Type ?u.15 → Type ?u.15
List
α: Type u
α
) : (
List.remove: {α : Type ?u.19} → [inst : DecidableEq α] → αList αList α
List.remove
a: α
a
l: List α
l
).
length: {α : Type ?u.61} → List α
length
l: List α
l
.
length: {α : Type ?u.65} → List α
length
:=

Goals accomplished! 🐙
α: Type u

inst✝: DecidableEq α

a: α

l: List α


α: Type u

inst✝: DecidableEq α

a: α

l: List α


α: Type u

inst✝: DecidableEq α

a: α


nil

Goals accomplished! 🐙
α: Type u

inst✝: DecidableEq α

a: α

l: List α


α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: List.length (List.remove a t) List.length t


cons
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: List.length (List.remove a t) List.length t


cons
List.length (if a = h' then List.remove a t else h' :: List.remove a t) Nat.succ (List.length t)
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: List.length (List.remove a t) List.length t


cons
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: List.length (List.remove a t) List.length t

h✝: a = h'


cons.inl
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: List.length (List.remove a t) List.length t

h✝: ¬a = h'


cons.inr
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: List.length (List.remove a t) List.length t


cons
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: List.length (List.remove a t) List.length t

h✝: a = h'


cons.inl
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: List.length (List.remove a t) List.length t

h✝: ¬a = h'


cons.inr
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: List.length (List.remove a t) List.length t

h✝: a = h'


cons.inl.h
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: List.length (List.remove a t) List.length t

h✝: a = h'


cons.inl
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: List.length (List.remove a t) List.length t

h✝: ¬a = h'


cons.inr

Goals accomplished! 🐙
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: List.length (List.remove a t) List.length t


cons
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: List.length (List.remove a t) List.length t

h✝: ¬a = h'


cons.inr
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: List.length (List.remove a t) List.length t

h✝: ¬a = h'


cons.inr
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: List.length (List.remove a t) List.length t

h✝: ¬a = h'


cons.inr
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: List.length (List.remove a t) List.length t

h✝: ¬a = h'


cons.inr
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: List.length (List.remove a t) List.length t

h✝: ¬a = h'


cons.inr
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: List.length (List.remove a t) List.length t

h✝: ¬a = h'


cons.inr.a
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: List.length (List.remove a t) List.length t

h✝: ¬a = h'


cons.inr

Goals accomplished! 🐙
/-- Removing a member from a list shortens the list -/ theorem
remove_mem_length: ∀ {α : Type u} [inst : DecidableEq α] {a : α} {l : List α}, a lList.length (List.remove a l) < List.length l
remove_mem_length
{
α: Type u
α
:
Type u: Type (u+1)
Type u
} [
DecidableEq: Sort ?u.2332 → Sort (max1?u.2332)
DecidableEq
α: Type u
α
]{
a: α
a
:
α: Type u
α
} {
l: List α
l
:
List: Type ?u.2343 → Type ?u.2343
List
α: Type u
α
} (
hyp: a l
hyp
:
a: α
a
l: List α
l
) : (
List.remove: {α : Type ?u.2403} → [inst : DecidableEq α] → αList αList α
List.remove
a: α
a
l: List α
l
).
length: {α : Type ?u.2444} → List α
length
<
l: List α
l
.
length: {α : Type ?u.2448} → List α
length
:=

Goals accomplished! 🐙
α: Type u

inst✝: DecidableEq α

a: α

l: List α

hyp: a l


α: Type u

inst✝: DecidableEq α

a: α

l: List α

hyp: a l


α: Type u

inst✝: DecidableEq α

a: α

hyp: a []


nil

Goals accomplished! 🐙
α: Type u

inst✝: DecidableEq α

a: α

l: List α

hyp: a l


α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t


cons
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t


cons
List.length (if a = h' then List.remove a t else h' :: List.remove a t) < Nat.succ (List.length t)
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t


cons
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t

h✝: a = h'


cons.inl
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t

h✝: ¬a = h'


cons.inr
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t


cons
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t

h✝: a = h'


cons.inl
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t

h✝: ¬a = h'


cons.inr
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t

h✝: a = h'


cons.inl.a
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t

h✝: a = h'


cons.inl
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t

h✝: ¬a = h'


cons.inr

Goals accomplished! 🐙
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t


cons
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t

h✝: ¬a = h'


cons.inr
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t

h✝: ¬a = h'


cons.inr
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t

h✝: ¬a = h'


cons.inr
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t

h✝: ¬a = h'


cons.inr
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t

h✝: ¬a = h'


cons.inr
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t

h✝: ¬a = h'


cons.inr.a
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t

h✝: ¬a = h'


cons.inr
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t

h✝: ¬a = h'


cons.inr.a

Goals accomplished! 🐙
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t

h✝: ¬a = h'


a t
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t

h✝: ¬a = h'


a t

Goals accomplished! 🐙
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t

h✝: ¬a = h'


¬a = h'

Goals accomplished! 🐙
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t

h✝: ¬a = h'


a t
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

h✝, this: ¬a = h'

hyp: a t


a t
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t

h✝: ¬a = h'


a t

Goals accomplished! 🐙
α: Type u

inst✝: DecidableEq α

a, h': α

t: List α

ih: a tList.length (List.remove a t) < List.length t

hyp: a h' :: t

h✝: ¬a = h'


cons.inr

Goals accomplished! 🐙
/-! 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 `mod n`. -/ /-- A random number in `Fin n` -/ def
IO.randFin: (n : ) → 0 < nIO (Fin n)
IO.randFin
(
n:
n
:
: Type
)(
h: 0 < n
h
:
0: ?m.6449
0
<
n:
n
) :
IO: TypeType
IO
<|
Fin: Type
Fin
n:
n
:= do let
r: ?m.6592
r
IO.rand: IO
IO.rand
0: ?m.6530
0
(
n:
n
-
1: ?m.6536
1
)
pure: {f : Type ?u.6595 → Type ?u.6594} → [self : Pure f] → {α : Type ?u.6595} → αf α
pure
r: ?m.6592
r
%
n:
n
,
Nat.mod_lt: ∀ (x : ) {y : }, y > 0x % y < y
Nat.mod_lt
r: ?m.6592
r
h: 0 < n
h
List.mem_remove_iff.{u_1} {α : Type u_1} [inst✝ : DecidableEq α] {a b : α} {as : List α} : b ∈ List.remove a as ↔ b ∈ as ∧ b ≠ a
List.mem_remove_iff: ∀ {α : Type u_1} [inst : DecidableEq α] {a b : α} {as : List α}, b List.remove a as b as b a
List.mem_remove_iff
-- ∀ {α : Type u_1} [inst : DecidableEq α] {a b : α} {as : List α}, b ∈ List.remove a as ↔ b ∈ as ∧ b ≠ a
List.length_pos_of_mem.{u_1} {α : Type u_1} {a : α} {l : List α} (a✝ : a ∈ l) : 0 < List.length l
List.length_pos_of_mem: ∀ {α : Type u_1} {a : α} {l : List α}, a l0 < List.length l
List.length_pos_of_mem
-- ∀ {α : Type u_1} {a : α} {l : List α}, a ∈ l → 0 < List.length l
List.get_mem.{u_1} {α : Type u_1} (l : List α) (n : ℕ) (h : n < List.length l) : List.get l { val := n, isLt := h } ∈ l
List.get_mem: ∀ {α : Type u_1} (l : List α) (n : ) (h : n < List.length l), List.get l { val := n, isLt := h } l
List.get_mem
-- ∀ {α : Type u_1} (l : List α) (n : ℕ) (h : n < List.length l), List.get l { val := n, isLt := h } ∈ l /-- A random element with a given property from a list, within `IO` -/ def
pickElemIO: {α : Type} → [inst : DecidableEq α] → (l : List α) → (p : αBool) → (t, t l p t = true) → IO { t // t l p t = true }
pickElemIO
[
DecidableEq: Sort ?u.6949 → Sort (max1?u.6949)
DecidableEq
α: ?m.6946
α
](
l: List α
l
:
List: Type ?u.6958 → Type ?u.6958
List
α: ?m.6946
α
)(
p: αBool
p
:
α: ?m.6946
α
Bool: Type
Bool
)(
h: t, t l p t = true
h
: ∃
t: α
t
:
α: ?m.6946
α
,
t: α
t
l: List α
l
p: αBool
p
t: α
t
=
true: Bool
true
) :
IO: TypeType
IO
{
t: α
t
:
α: ?m.6946
α
//
t: α
t
l: List α
l
p: αBool
p
t: α
t
=
true: Bool
true
} := do have
h': 0 < List.length l
h'
:
0: ?m.7049
0
<
l: List α
l
.
length: {α : Type ?u.7064} → List α
length
:=

Goals accomplished! 🐙
α: Type

inst✝: DecidableEq α

l: List α

p: αBool

h: t, t l p t = true


α: Type

inst✝: DecidableEq α

l: List α

p: αBool

h: t, t l p t = true

t: α

h₀: t l p t = true


α: Type

inst✝: DecidableEq α

l: List α

p: αBool

h: t, t l p t = true



Goals accomplished! 🐙
let
index: ?m.7114
index
IO.randFin: (n : ) → 0 < nIO (Fin n)
IO.randFin
l: List α
l
.
length: {α : Type ?u.7106} → List α
length
h': 0 < List.length l
h'
let
a: ?m.7117
a
:=
l: List α
l
.
get: {α : Type ?u.7118} → (as : List α) → Fin (List.length as)α
get
index: ?m.7114
index
if
c: ?m.7208
c
:
p: αBool
p
a: ?m.7117
a
=
true: Bool
true
then return
a: ?m.7117
a
,

Goals accomplished! 🐙
α: Type

inst✝: DecidableEq α

l: List α

p: αBool

h: t, t l p t = true

h': 0 < List.length l

index: Fin (List.length l)

a:= List.get l index: α

c: p a = true


a l p a = true
α: Type

inst✝: DecidableEq α

l: List α

p: αBool

h: t, t l p t = true

h': 0 < List.length l

index: Fin (List.length l)

a:= List.get l index: α

c: p a = true


List.get l index l
α: Type

inst✝: DecidableEq α

l: List α

p: αBool

h: t, t l p t = true

h': 0 < List.length l

index: Fin (List.length l)

a:= List.get l index: α

c: p a = true


a l p a = true

Goals accomplished! 🐙
else let
l': ?m.7211
l'
:=
l: List α
l
.
remove: {α : Type ?u.7212} → [inst : DecidableEq α] → αList αList α
remove
a: ?m.7117
a
have
h': t, t l' p t = true
h'
: ∃
t: α
t
:
α: Type
α
,
t: α
t
l': ?m.7211
l'
p: αBool
p
t: α
t
=
true: Bool
true
:=

Goals accomplished! 🐙
α: Type

inst✝: DecidableEq α

l: List α

p: αBool

h: t, t l p t = true

h': 0 < List.length l

index: Fin (List.length l)

a:= List.get l index: α

c: ¬p a = true

l':= List.remove a l: List α


t, t l' p t = true
α: Type

inst✝: DecidableEq α

l: List α

p: αBool

h: t, t l p t = true

h': 0 < List.length l

index: Fin (List.length l)

a:= List.get l index: α

c: ¬p a = true

l':= List.remove a l: List α

t: α

h₁: t l

h₂: p t = true


t, t l' p t = true
α: Type

inst✝: DecidableEq α

l: List α

p: αBool

h: t, t l p t = true

h': 0 < List.length l

index: Fin (List.length l)

a:= List.get l index: α

c: ¬p a = true

l':= List.remove a l: List α


t, t l' p t = true
α: Type

inst✝: DecidableEq α

l: List α

p: αBool

h: t, t l p t = true

h': 0 < List.length l

index: Fin (List.length l)

a:= List.get l index: α

c: ¬p a = true

l':= List.remove a l: List α

t: α

h₁: t l

h₂: p t = true


t l' p t = true
α: Type

inst✝: DecidableEq α

l: List α

p: αBool

h: t, t l p t = true

h': 0 < List.length l

index: Fin (List.length l)

a:= List.get l index: α

c: ¬p a = true

l':= List.remove a l: List α


t, t l' p t = true
α: Type

inst✝: DecidableEq α

l: List α

p: αBool

h: t, t l p t = true

h': 0 < List.length l

index: Fin (List.length l)

a:= List.get l index: α

c: ¬p a = true

l':= List.remove a l: List α

t: α

h₁: t l

h₂: p t = true


¬t = List.get l index
α: Type

inst✝: DecidableEq α

l: List α

p: αBool

h: t, t l p t = true

h': 0 < List.length l

index: Fin (List.length l)

a:= List.get l index: α

c: ¬p a = true

l':= List.remove a l: List α


t, t l' p t = true
α: Type

inst✝: DecidableEq α

l: List α

p: αBool

h: t, t l p t = true

h': 0 < List.length l

index: Fin (List.length l)

a:= List.get l index: α

l':= List.remove a l: List α

t: α

h₁: t l

h₂: p t = true

c: p (List.get l index) = false


¬t = List.get l index
α: Type

inst✝: DecidableEq α

l: List α

p: αBool

h: t, t l p t = true

h': 0 < List.length l

index: Fin (List.length l)

a:= List.get l index: α

c: ¬p a = true

l':= List.remove a l: List α


t, t l' p t = true
α: Type

inst✝: DecidableEq α

l: List α

p: αBool

h: t, t l p t = true

h': 0 < List.length l

index: Fin (List.length l)

a:= List.get l index: α

l':= List.remove a l: List α

t: α

h₁: t l

h₂: p t = true

c: p (List.get l index) = false

contra: t = List.get l index


α: Type

inst✝: DecidableEq α

l: List α

p: αBool

h: t, t l p t = true

h': 0 < List.length l

index: Fin (List.length l)

a:= List.get l index: α

c: ¬p a = true

l':= List.remove a l: List α


t, t l' p t = true

Goals accomplished! 🐙
have :
l': ?m.7211
l'
.
length: {α : Type ?u.7390} → List α
length
<
l: List α
l
.
length: {α : Type ?u.7394} → List α
length
:=

Goals accomplished! 🐙
α: Type

inst✝: DecidableEq α

l: List α

p: αBool

h: t, t l p t = true

h'✝: 0 < List.length l

index: Fin (List.length l)

a:= List.get l index: α

c: ¬p a = true

l':= List.remove a l: List α

h': t, t l' p t = true


α: Type

inst✝: DecidableEq α

l: List α

p: αBool

h: t, t l p t = true

h'✝: 0 < List.length l

index: Fin (List.length l)

a:= List.get l index: α

c: ¬p a = true

l':= List.remove a l: List α

h': t, t l' p t = true


hyp
a l
α: Type

inst✝: DecidableEq α

l: List α

p: αBool

h: t, t l p t = true

h'✝: 0 < List.length l

index: Fin (List.length l)

a:= List.get l index: α

c: ¬p a = true

l':= List.remove a l: List α

h': t, t l' p t = true



Goals accomplished! 🐙
let
t: α
t
,
h₁: t l'
h₁
,
h₂: p t = true
h₂
⟩ ←
pickElemIO: {α : Type} → [inst : DecidableEq α] → (l : List α) → (p : αBool) → (t, t l p t = true) → IO { t // t l p t = true }
pickElemIO
l': ?m.7211
l'
p: αBool
p
h': t, t l' p t = true
h'
have
m: t l
m
:
t: α
t
l: List α
l
:=
List.mem_of_mem_remove: ∀ {α : Type ?u.7518} [inst : DecidableEq α] {a b : α} {as : List α}, b List.remove a asb as
List.mem_of_mem_remove
h₁: t l'
h₁
return
t: α
t
,
m: t l
m
,
h₂: p t = true
h₂
termination_by _ _ _ l _ _ =>
l: List α
l
.
length: {α : Type ?u.10278} → List α
length
/-- 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 `p` -/ def
pickElemD: {α : Type ?u.34043} → [inst : DecidableEq α] → (l : List α) → (p : αBool) → (default : α) → default lp default = true{ t // t l p t = true }
pickElemD
[
DecidableEq: Sort ?u.34034 → Sort (max1?u.34034)
DecidableEq
α: ?m.34031
α
](
l: List α
l
:
List: Type ?u.34043 → Type ?u.34043
List
α: ?m.34031
α
)(
p: αBool
p
:
α: ?m.34031
α
Bool: Type
Bool
)(
default: α
default
:
α: ?m.34031
α
)(
h₁: default l
h₁
:
default: α
default
l: List α
l
)(
h₂: p default = true
h₂
:
p: αBool
p
default: α
default
=
true: Bool
true
) : {
t: α
t
:
α: ?m.34031
α
//
t: α
t
l: List α
l
p: αBool
p
t: α
t
=
true: Bool
true
} := (
pickElemIO: {α : Type} → [inst : DecidableEq α] → (l : List α) → (p : αBool) → (t, t l p t = true) → IO { t // t l p t = true }
pickElemIO
l: List α
l
p: αBool
p
default: α
default
,
h₁: default l
h₁
,
h₂: p default = true
h₂
⟩).
run': {ε σ α : Type ?u.34205} → EStateM ε σ ασOption α
run'
(): Unit
()
|>.
getD: {α : Type ?u.34217} → Option ααα
getD
default: α
default
,
h₁: default l
h₁
,
h₂: p default = true
h₂
⟩ /-! ## 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. -/
StdGen : Type
StdGen: Type
StdGen
mkStdGen (s : ℕ := 0) : StdGen
mkStdGen: optParam 0StdGen
mkStdGen
-- mkStdGen (s : ℕ := 0) : StdGen
randNat.{u} {gen : Type u} [inst✝ : RandomGen gen] (g : gen) (lo hi : ℕ) : ℕ × gen
randNat: {gen : Type u} → [inst : RandomGen gen] → gen × gen
randNat
-- randNat.{u} {gen : Type u} [inst✝ : RandomGen gen] (g : gen) (lo hi : ℕ) : ℕ × gen def
RandomM: (α : ?m.34437) → ?m.34441 α
RandomM
α: ?m.34437
α
:=
StdGen: Type
StdGen
α: ?m.34437
α
×
StdGen: Type
StdGen
IO.rand (lo hi : ℕ) : IO ℕ
IO.rand: IO
IO.rand
-- IO.rand (lo hi : ℕ) : IO ℕ namespace RandomM def
rand: RandomM
rand
(
lo:
lo
hi:
hi
:
: Type
):
RandomM: Type ?u.34460 → Type ?u.34460
RandomM
: Type
:= fun
gen: ?m.34467
gen
randNat: {gen : Type ?u.34469} → [inst : RandomGen gen] → gen × gen
randNat
gen: ?m.34467
gen
lo:
lo
hi:
hi
def
run: {α : Type u_1} → RandomM αStdGenα × StdGen
run
(
x: RandomM α
x
:
RandomM: Type ?u.34530 → Type ?u.34530
RandomM
α: ?m.34527
α
) :
StdGen: Type
StdGen
α: ?m.34527
α
×
StdGen: Type
StdGen
:=
x: RandomM α
x
def
run': {α : Type ?u.34613} → RandomM αoptParam StdGen defaultα
run'
(
x: RandomM α
x
:
RandomM: Type ?u.34613 → Type ?u.34613
RandomM
α: ?m.34610
α
)(
gen: optParam StdGen default
gen
:
StdGen: Type
StdGen
:=
default: {α : Sort ?u.34617} → [self : Inhabited α] → α
default
) :
α: ?m.34610
α
:= (
x: RandomM α
x
gen: optParam StdGen default
gen
).
1: {α : Type ?u.35124} → {β : Type ?u.35123} → α × βα
1
3
rand: RandomM
rand
0: ?m.35213
0
10: ?m.35224
10
|>.
run': {α : Type ?u.35230} → RandomM αoptParam StdGen defaultα
run'
instance: Monad RandomM
instance
:
Monad: (Type ?u.35698 → Type ?u.35697) → Type (max(?u.35698+1)?u.35697)
Monad
RandomM: Type ?u.35699 → Type ?u.35699
RandomM
where pure := fun
x: ?m.35852
x
fun
gen: ?m.35855
gen
↦ (
x: ?m.35852
x
,
gen: ?m.35855
gen
) map := fun
f: ?m.35725
f
x: ?m.35729
x
fun
gen: ?m.35732
gen
let (
a: α✝
a
,
gen': StdGen
gen'
) :=
x: ?m.35729
x
gen: ?m.35732
gen
(
f: ?m.35725
f
a: α✝
a
,
gen': StdGen
gen'
) bind := fun
x: ?m.35960
x
b: ?m.35963
b
fun
gen: ?m.35968
gen
let (
a: α✝
a
,
gen': StdGen
gen'
) :=
x: ?m.35960
x
gen: ?m.35968
gen
b: ?m.35963
b
a: α✝
a
gen': StdGen
gen'
def
randBool: RandomM Bool
randBool
:
RandomM: Type ?u.38008 → Type ?u.38008
RandomM
Bool: Type
Bool
:= do let
n: ?m.38067
n
rand: RandomM
rand
0: ?m.38048
0
1: ?m.38059
1
pure: {f : Type ?u.38070 → Type ?u.38069} → [self : Pure f] → {α : Type ?u.38070} → αf α
pure
(
n: ?m.38067
n
==
0: ?m.38088
0
)
false
randBool: RandomM Bool
randBool
|>.
run': {α : Type ?u.38302} → RandomM αoptParam StdGen defaultα
run'
(
mkStdGen: optParam 0StdGen
mkStdGen
84938743: ?m.38309
84938743
) def
randList: RandomM (List )
randList
(
lo:
lo
hi:
hi
n:
n
:
: Type
) :
RandomM: Type ?u.38748 → Type ?u.38748
RandomM
<|
List: Type ?u.38749 → Type ?u.38749
List
: Type
:= do match
n:
n
with |
0:
0
=>
pure: {f : Type ?u.38791 → Type ?u.38790} → [self : Pure f] → {α : Type ?u.38791} → αf α
pure
[]: List ?m.38808
[]
|
k:
k
+ 1 => do let
x: ?m.38935
x
rand: RandomM
rand
lo:
lo
hi:
hi
let
xs: ?m.38957
xs
randList: RandomM (List )
randList
lo:
lo
hi:
hi
k:
k
pure: {f : Type ?u.38960 → Type ?u.38959} → [self : Pure f] → {α : Type ?u.38960} → αf α
pure
<|
x: ?m.38935
x
::
xs: ?m.38957
xs
[8, 7, 10, 7, 10, 9, 4]
randList: RandomM (List )
randList
1: ?m.39562
1
10: ?m.39573
10
7: ?m.39579
7
|>.
run': {α : Type ?u.39585} → RandomM αoptParam StdGen defaultα
run'
(
mkStdGen: optParam 0StdGen
mkStdGen
84938743: ?m.39592
84938743
) def
setSeed: RandomM Unit
setSeed
(
n:
n
:
: Type
) :
RandomM: Type ?u.40051 → Type ?u.40051
RandomM
Unit: Type
Unit
:= fun
_: ?m.40056
_
↦ (
(): Unit
()
,
mkStdGen: optParam 0StdGen
mkStdGen
n:
n
) def
withSeed: {α : Type ?u.40123} → RandomM αRandomM α
withSeed
(
n:
n
:
: Type
) (
c: RandomM α
c
:
RandomM: Type ?u.40123 → Type ?u.40123
RandomM
α: ?m.40118
α
) :
RandomM: Type ?u.40126 → Type ?u.40126
RandomM
α: ?m.40118
α
:= do
setSeed: RandomM Unit
setSeed
n:
n
c: RandomM α
c
[9, 10, 2, 6, 9, 3, 10, 5, 4, 10, 2, 4]
(
withSeed: {α : Type} → RandomM αRandomM α
withSeed
376872: ?m.40340
376872
<|
randList: RandomM (List )
randList
1: ?m.40352
1
10: ?m.40359
10
12: ?m.40365
12
) |>.
run': {α : Type ?u.40372} → RandomM αoptParam StdGen defaultα
run'
end RandomM def
StateM': Type u_1 → Type u_2 → Type (maxu_1u_2)
StateM'
σ: ?m.40850
σ
α: ?m.40857 σ
α
:=
σ: ?m.40850
σ
α: ?m.40857 σ
α
×
σ: ?m.40850
σ
instance: {σ : Type u_1} → Monad (StateM' σ)
instance
:
Monad: (Type ?u.40891 → Type ?u.40890) → Type (max(?u.40891+1)?u.40890)
Monad
<|
StateM': Type ?u.40893 → Type ?u.40892 → Type (max?u.40893?u.40892)
StateM'
σ: ?m.40887
σ
where pure := fun
x: ?m.41052
x
fun
gen: ?m.41055
gen
↦ (
x: ?m.41052
x
,
gen: ?m.41055
gen
) map := fun
f: ?m.40922
f
x: ?m.40926
x
fun
gen: ?m.40929
gen
let (
a: α✝
a
,
gen': σ
gen'
) :=
x: ?m.40926
x
gen: ?m.40929
gen
(
f: ?m.40922
f
a: α✝
a
,
gen': σ
gen'
) bind := fun
x: ?m.41160
x
b: ?m.41163
b
fun
gen: ?m.41168
gen
let (
a: α✝
a
,
gen': σ
gen'
) :=
x: ?m.41160
x
gen: ?m.41168
gen
b: ?m.41163
b
a: α✝
a
gen': σ
gen'
namespace StateM' def
run: {σ : Type u_1} → {α : Type u_2} → StateM' σ ασα × σ
run
(
x: StateM' σ α
x
:
StateM': Type ?u.42806 → Type ?u.42805 → Type (max?u.42806?u.42805)
StateM'
σ: ?m.42796
σ
α: ?m.42802
α
) :
σ: ?m.42796
σ
α: ?m.42802
α
×
σ: ?m.42796
σ
:=
x: StateM' σ α
x
def
run': {σ : Type ?u.42897} → {α : Type ?u.42896} → [inst : Inhabited σ] → StateM' σ αoptParam σ defaultα
run'
[
Inhabited: Sort ?u.42884 → Sort (max1?u.42884)
Inhabited
σ: ?m.42881
σ
](
x: StateM' σ α
x
:
StateM': Type ?u.42897 → Type ?u.42896 → Type (max?u.42897?u.42896)
StateM'
σ: ?m.42881
σ
α: ?m.42890
α
)(
s: optParam σ default
s
:
σ: ?m.42881
σ
:=
default: {α : Sort ?u.42901} → [self : Inhabited α] → α
default
) :
α: ?m.42890
α
:= (
x: StateM' σ α
x
s: optParam σ default
s
).
1: {α : Type ?u.43410} → {β : Type ?u.43409} → α × βα
1
def
setState: {σ : Type ?u.43494} → σStateM' σ Unit
setState
(
s: σ
s
:
σ: ?m.43488
σ
) :
StateM': Type ?u.43494 → Type ?u.43493 → Type (max?u.43494?u.43493)
StateM'
σ: ?m.43488
σ
Unit: Type
Unit
:= fun
_: ?m.43501
_
↦ (
(): Unit
()
,
s: σ
s
) def
getState: {σ : Type ?u.43562} → StateM' σ σ
getState
:
StateM': Type ?u.43562 → Type ?u.43561 → Type (max?u.43562?u.43561)
StateM'
σ: ?m.43558
σ
σ: ?m.43558
σ
:= fun
s: ?m.43567
s
↦ (
s: ?m.43567
s
,
s: ?m.43567
s
) def
withState: {σ : Type ?u.43626} → {α : Type ?u.43625} → σStateM' σ αStateM' σ α
withState
(
s: σ
s
:
σ: ?m.43612
σ
) (
c: StateM' σ α
c
:
StateM': Type ?u.43626 → Type ?u.43625 → Type (max?u.43626?u.43625)
StateM'
σ: ?m.43612
σ
α: ?m.43620
α
) :
StateM': Type ?u.43630 → Type ?u.43629 → Type (max?u.43630?u.43629)
StateM'
σ: ?m.43612
σ
α: ?m.43620
α
:= do
setState: {σ : Type ?u.43679} → σStateM' σ Unit
setState
s: σ
s
c: StateM' σ α
c
end StateM' def
RandomM': Type u_1 → Type u_1
RandomM'
:=
StateM': Type ?u.43841 → Type ?u.43840 → Type (max?u.43841?u.43840)
StateM'
StdGen: Type
StdGen
namespace RandomM' end RandomM'