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