Continued Fractions in Lean A Newbies Adventure Kevin Kappelmann - - PowerPoint PPT Presentation

continued fractions in lean
SMART_READER_LITE
LIVE PREVIEW

Continued Fractions in Lean A Newbies Adventure Kevin Kappelmann - - PowerPoint PPT Presentation

Continued Fractions in Lean A Newbies Adventure Kevin Kappelmann June 14, 2019 Vrije Universiteit Amsterdam Lets Go on an Adventure Choose a Weapon perhaps because I am interning at VU Amsterdam Choose a Weapon perhaps because I


slide-1
SLIDE 1

Continued Fractions in Lean

A Newbie’s Adventure

Kevin Kappelmann June 14, 2019

Vrije Universiteit Amsterdam

slide-2
SLIDE 2

Let’s Go on an Adventure

slide-3
SLIDE 3

Choose a Weapon

…perhaps because I am interning at VU Amsterdam

slide-4
SLIDE 4

Choose a Weapon

…perhaps because I am interning at VU Amsterdam

slide-5
SLIDE 5

Choose a Weapon

…perhaps because I am interning at VU Amsterdam

slide-6
SLIDE 6

Choose a Weapon

…perhaps because I am interning at VU Amsterdam

slide-7
SLIDE 7

The Adventurer’s Skill Set

  • Some experience using Isabelle
  • First project with a dependent type theorem prover
  • Basic maths and functional programming knowledge
slide-8
SLIDE 8

The Adventurer’s Skill Set

  • Some experience using Isabelle
  • First project with a dependent type theorem prover
  • Basic maths and functional programming knowledge
slide-9
SLIDE 9

The Adventurer’s Skill Set

  • Some experience using Isabelle
  • First project with a dependent type theorem prover
  • Basic maths and functional programming knowledge
slide-10
SLIDE 10

The Adventurer’s Skill Set

  • Some experience using Isabelle
  • First project with a dependent type theorem prover
  • Basic maths and functional programming knowledge
slide-11
SLIDE 11
slide-12
SLIDE 12

Definitions

slide-13
SLIDE 13

Generalized Continued Fractions

A generalized continued fraction is… b a0 b0 a1 b1 a2 b2 a3 b3 ...

  • b is called the integer part
  • each ai is a partial numerator
  • each bi is a partial denominator
slide-14
SLIDE 14

Generalized Continued Fractions

A generalized continued fraction is… b + a0 b0 + a1 b1 + a2 b2 + a3 b3 + ...

  • b is called the integer part
  • each ai is a partial numerator
  • each bi is a partial denominator
slide-15
SLIDE 15

Generalized Continued Fractions

A generalized continued fraction is… b + a0 b0 + a1 b1 + a2 b2 + a3 b3 + ...

  • b is called the integer part
  • each ai is a partial numerator
  • each bi is a partial denominator
slide-16
SLIDE 16

Generalized Continued Fractions

A generalized continued fraction is… b + a0 b0 + a1 b1 + a2 b2 + a3 b3 + ...

  • b is called the integer part
  • each ai is a partial numerator
  • each bi is a partial denominator
slide-17
SLIDE 17

Generalized Continued Fractions

A generalized continued fraction is… b + a0 b0 + a1 b1 + a2 b2 + a3 b3 + ...

  • b is called the integer part
  • each ai is a partial numerator
  • each bi is a partial denominator
slide-18
SLIDE 18

Generalized Continued Fractions of π

Continued fraction

π = 3 + 1 7 + 1 15 + 1 1 + 1 292 + 1 1 + ...

Generalized continued fraction 3 12 6 32 6 52 6 72 6 92 6 ...

slide-19
SLIDE 19

Generalized Continued Fractions of π

Continued fraction

π = 3 + 1 7 + 1 15 + 1 1 + 1 292 + 1 1 + ...

Generalized continued fraction 3 12 6 32 6 52 6 72 6 92 6 ...

slide-20
SLIDE 20

Generalized Continued Fractions of π

Continued fraction

π = 3 + 1 7 + 1 15 + 1 1 + 1 292 + 1 1 + ...

Generalized continued fraction π = 3 + 12 6 + 32 6 + 52 6 + 72 6 + 92 6 + ...

slide-21
SLIDE 21

Generalized Continued Fractions of π

Continued fraction

π = 3 + 1 7 + 1 15 + 1 1 + 1 292 + 1 1 + ...

Generalized continued fraction π = 3 + 12 6 + 32 6 + 52 6 + 72 6 + 92 6 + ...

slide-22
SLIDE 22

Generalized Continued Fractions in Lean

b + a0 b0 + a1 b1 + a2 b2 + a3 b3 + ...

1 /- Fix a type -/ 2 variable (α : Type*)

slide-23
SLIDE 23

Generalized Continued Fractions in Lean

b + a0 b0 + a1 b1 + a2 b2 + a3 b3 + ...

1 /- Fix a type -/ 2 variable (α : Type*)

slide-24
SLIDE 24

Generalized Continued Fractions in Lean

b + a0 b0 + a1 b1 + a2 b2 + a3 b3 + ...

1

/- Fix a type -/

2

variable (α : Type*)

3 4

/-- A gcf_pair consists of a partial numerator a and partial denominator b -/

֒ → 5

structure gcf_pair := (a : α) (b : α)

slide-25
SLIDE 25

Generalized Continued Fractions in Lean

b + a0 b0 + a1 b1 + a2 b2 + a3 b3 + ...

/- Fix a type -/ variable (α : Type*) /-- A gcf_pair consists of a partial numerator a and partial denominator b -/ ֒ → structure gcf_pair := (a : α) (b : α)

  • - Once a sequence hits none, it stays none

def seq := {f : ℕ → option α // ∀ {n}, f n = none → f (n + 1) = none}

֒ →

slide-26
SLIDE 26

Generalized Continued Fractions in Lean

b + a0 b0 + a1 b1 + a2 b2 + a3 b3 + ...

/- Fix a type -/ variable (α : Type*) /-- A gcf_pair consists of a partial numerator a and partial denominator b -/ ֒ → structure gcf_pair := (a : α) (b : α)

def seq := {f : ℕ → option α // ∀ {n}, f n = none → f (n + 1) = none}

֒ →

/-- A generalized continued fraction consists of a leading head term (the "integer part") and a sequence of partial partial numerators an and partial denominators bn -/

֒ → ֒ → ֒ →

structure gcf := (head : α) (seq : seq (gcf_pair α))

֒ →

slide-27
SLIDE 27

Evaluate Generalized Continued Fractions

1 def convergents (g : gcf α) (n : ℕ) : α := 2 g.head + if n = 0 then 0 else aux n g.seq

slide-28
SLIDE 28

Evaluate Generalized Continued Fractions

1 def aux : ℕ → seq (gcf_pair α) → α 2 | 0 s := match s.head with 3

| none := 0

4

| some ⟨a, b⟩ := a / b

5

end

6 | (n + 1) s := match s.head with 7

| none := 0

8

| some ⟨a, b⟩ := a / (b + aux n s.tail)

9

end

10 11 def convergents (g : gcf α) (n : ℕ) : α := 12 g.head + if n = 0 then 0 else aux n g.seq

slide-29
SLIDE 29

Continued Fractions

b + 1 b0 + 1 b1 + 1 b2 + 1 b3 + ...

1 /-- A continued fraction is a gcf whose partial

numerators are equal to 1. -/

2 def cf := {g : gcf α // ∀ (n : ℕ) (a : α),

(partial_numerators g).nth n = some a → a = 1}

First impression: Pretty Sweet!

slide-30
SLIDE 30

Continued Fractions

b + 1 b0 + 1 b1 + 1 b2 + 1 b3 + ...

1 /-- A continued fraction is a gcf whose partial

numerators are equal to 1. -/

֒ → 2 def cf := {g : gcf α // ∀ (n : ℕ) (a : α),

(partial_numerators g).nth n = some a → a = 1}

֒ →

First impression: Pretty Sweet!

slide-31
SLIDE 31

Continued Fractions

b + 1 b0 + 1 b1 + 1 b2 + 1 b3 + ...

1 /-- A continued fraction is a gcf whose partial

numerators are equal to 1. -/

֒ → 2 def cf := {g : gcf α // ∀ (n : ℕ) (a : α),

(partial_numerators g).nth n = some a → a = 1}

֒ →

First impression: Pretty Sweet!

slide-32
SLIDE 32

Continued Fractions

b + 1 b0 + 1 b1 + 1 b2 + 1 b3 + ...

1 /-- A continued fraction is a gcf whose partial

numerators are equal to 1. -/

֒ → 2 def cf := {g : gcf α // ∀ (n : ℕ) (a : α),

(partial_numerators g).nth n = some a → a = 1}

֒ →

First impression: Pretty Sweet!

slide-33
SLIDE 33

Fun with Subtypes

So, since cf is a subtype of gcf, we can do

1 def convergents (g : gcf α) (n : ℕ) : α := ... 2 3 variable (c : cf α) 4 #check convergents c 0

NOPE!

Oh, I see – I need to cast!

slide-34
SLIDE 34

Fun with Subtypes

So, since cf is a subtype of gcf, we can do

1 def convergents (g : gcf α) (n : ℕ) : α := ... 2 3 variable (c : cf α) 4 #check convergents c 0

NOPE!

Oh, I see – I need to cast!

slide-35
SLIDE 35

Fun with Subtypes

So, since cf is a subtype of gcf, we can do

1 def convergents (g : gcf α) (n : ℕ) : α := ... 2 3 variable (c : cf α) 4 #check convergents c 0

Oh, I see – I need to cast!

slide-36
SLIDE 36

Fun with Subtypes

So, since cf is a subtype of gcf, we can do

1 def convergents (g : gcf α) (n : ℕ) : α := ... 2 3 variable (c : cf α) 4 #check convergents c 0

Oh, I see – I need to cast!

slide-37
SLIDE 37

Fun with Subtypes

So, since cf is a subtype of gcf, we can do

1 def convergents (g : gcf α) (n : ℕ) : α := ... 2 3 variable (c : cf α) 4 #check convergents (c : gcf α) 0

NOPE!

…alright, let’s go on Zulip

slide-38
SLIDE 38

Fun with Subtypes

So, since cf is a subtype of gcf, we can do

1 def convergents (g : gcf α) (n : ℕ) : α := ... 2 3 variable (c : cf α) 4 #check convergents (c : gcf α) 0

NOPE!

…alright, let’s go on Zulip

slide-39
SLIDE 39

Fun with Subtypes

So, since cf is a subtype of gcf, we can do

1 def convergents (g : gcf α) (n : ℕ) : α := ... 2 3 variable (c : cf α) 4 #check convergents (c : gcf α) 0

…alright, let’s go on Zulip

slide-40
SLIDE 40

Fun with Subtypes

So, since cf is a subtype of gcf, we can do

1 def convergents (g : gcf α) (n : ℕ) : α := ... 2 3 variable (c : cf α) 4 #check convergents (c : gcf α) 0

…alright, let’s go on Zulip

slide-41
SLIDE 41

Please Help Me

A few minutes and messages from Kevin Buzzard later…

slide-42
SLIDE 42

The “Solution”

We first need to define the casting

1 instance cf_to_gcf : has_coe (cf β) (gcf β) 2 := by {unfold cf, apply_instance} 3 4 /- Best practice: create a lemma for your cast -/ 5 @[simp, elim_cast] 6 lemma coe_cf (c : cf β) : (↑c : gcf β) = c.val 7 := by refl

Now this works:

1 variable (c : cf α) 2 #check convergents (c : gcf α) 0

slide-43
SLIDE 43

The “Solution”

We first need to define the casting

1 instance cf_to_gcf : has_coe (cf β) (gcf β) 2 := by {unfold cf, apply_instance} 3 4 /- Best practice: create a lemma for your cast -/ 5 @[simp, elim_cast] 6 lemma coe_cf (c : cf β) : (↑c : gcf β) = c.val 7 := by refl

Now this works:

1 variable (c : cf α) 2 #check convergents (c : gcf α) 0

slide-44
SLIDE 44

The “Solution”

We first need to define the casting

1 instance cf_to_gcf : has_coe (cf β) (gcf β) 2 := by {unfold cf, apply_instance} 3 4 /- Best practice: create a lemma for your cast -/ 5 @[simp, elim_cast] 6 lemma coe_cf (c : cf β) : (↑c : gcf β) = c.val 7 := by refl

Now this works:

1 variable (c : cf α) 2 #check convergents (c : gcf α) 0

slide-45
SLIDE 45

The “Solution”

We first need to define the casting

1 instance cf_to_gcf : has_coe (cf β) (gcf β) 2 := by {unfold cf, apply_instance} 3 4 /- Best practice: create a lemma for your cast -/ 5 @[simp, elim_cast] 6 lemma coe_cf (c : cf β) : (↑c : gcf β) = c.val 7 := by refl

This, however, still does not work:

1 variable (c : cf α) 2 #check convergents c 0

slide-46
SLIDE 46

Proofs

slide-47
SLIDE 47

The Proof Is Trivial

1 lemma floor_rat_eq_num_div_denom (n d : ℤ) : 2

⌊rat.mk n d⌋ = n / d

slide-48
SLIDE 48

The Proof Is Trivial

1 lemma floor_rat_eq_num_div_denom (n d : ℤ) : 2

⌊rat.mk n d⌋ = n / d

Wait, let’s do some examples first…

slide-49
SLIDE 49

The Proof Is Trivial

1 lemma floor_rat_eq_num_div_denom (n d : ℤ) : 2

⌊rat.mk n d⌋ = n / d

Alright, I am sold!

slide-50
SLIDE 50

Proving… Please Wait

slide-51
SLIDE 51

Proving… Please Wait

Something seems wrong

slide-52
SLIDE 52

Now It Is Trivial

1 lemma floor_rat_eq_num_div_denom (n : ℤ) (d : ℕ) : 2

⌊rat.mk n d⌋ = n / d

That’s better!

slide-53
SLIDE 53

A Short Note About Tactics

<Show two short examples in VS Code>

slide-54
SLIDE 54

Results

slide-55
SLIDE 55

Collected Treasures

slide-56
SLIDE 56

Collected Treasures

Definition of (generalized) continued fractions and their evaluation

1 structure gcf := (head : α) (seq : seq (gcf_pair

α))

֒ → 2 def cf := {g : gcf α // ∀ (n : ℕ) (a : α),

(partial_numerators g).nth n = some a → a = 1}

֒ → 3 def convergents (g : gcf α) (n : ℕ) : α := ...

slide-57
SLIDE 57

Collected Treasures

Computable continued fractions for discrete linear ordered floor fields

1 def get_cf [discrete_linear_ordered_field α]

[floor_ring α] (v : α) : cf α := ...

֒ →

Also works for – just not computable…

slide-58
SLIDE 58

Collected Treasures

Computable continued fractions for discrete linear ordered floor fields

1 def get_cf [discrete_linear_ordered_field α]

[floor_ring α] (v : α) : cf α := ...

֒ →

Also works for R – just not computable…

slide-59
SLIDE 59

Collected Treasures

Termination proof for archimedian fields

1 theorem termination_iff_rat [archimedean α] (v : α)

:

֒ → 2

Terminates (get_gcf v) ↔ ∃ (q : ℚ), v = (q : α)

Including a theorem a mathematician would never prove:

1 theorem translate_rat_get_cf {q : ℚ} 2 (v_eq_q : v = q) : 3

((get_gcf q : gcf ℚ) : gcf α) = get_gcf v :=

slide-60
SLIDE 60

Collected Treasures

Termination proof for archimedian fields

1 theorem termination_iff_rat [archimedean α] (v : α)

:

֒ → 2

Terminates (get_gcf v) ↔ ∃ (q : ℚ), v = (q : α)

Including a theorem a mathematician would never prove:

1 theorem translate_rat_get_cf {q : ℚ} 2 (v_eq_q : v = q) : 3

((get_gcf q : gcf ℚ) : gcf α) = get_gcf v :=

slide-61
SLIDE 61

Collected Treasures

Termination proof for archimedian fields

1 theorem termination_iff_rat [archimedean α] (v : α)

:

֒ → 2

Terminates (get_gcf v) ↔ ∃ (q : ℚ), v = (q : α)

Including a theorem a mathematician would never prove:

1 theorem translate_rat_get_cf {q : ℚ} 2 (v_eq_q : v = q) : 3

((get_gcf q : gcf ℚ) : gcf α) = get_gcf v :=

slide-62
SLIDE 62

Collected Treasures

Finite correctness of the computation

1 theorem get_gcf_finite_correctness 2 (terminates: Terminates (get_gcf v)) : 3

∃ (n : ℕ), v = convergents (get_gcf v) n

slide-63
SLIDE 63

Collected Treasures

Some interesting inequalities, and finally:

1 theorem epsilon_convergence : ∀ (ε > (0 : α)), 2

∃ (N : ℕ), ∀ (n ≥ N),

3

|v - convergents (get_gcf v) n| < ε :=

But sadly no library for sequence limits in Lean :(

slide-64
SLIDE 64

Collected Treasures

Some interesting inequalities, and finally:

1 theorem epsilon_convergence : ∀ (ε > (0 : α)), 2

∃ (N : ℕ), ∀ (n ≥ N),

3

|v - convergents (get_gcf v) n| < ε :=

But sadly no library for sequence limits in Lean :(

slide-65
SLIDE 65

End of the Story

slide-66
SLIDE 66

Lessons Learnt

  • Lean’s type system is very expressive and great for

definitions…

  • …if one knows the gotchas.
  • Support on Zulip is fantastic.
  • Existing tactics help a LOT…
  • …but no integration of automated theorem provers yet.
slide-67
SLIDE 67

Lessons Learnt

  • Lean’s type system is very expressive and great for

definitions…

  • …if one knows the gotchas.
  • Support on Zulip is fantastic.
  • Existing tactics help a LOT…
  • …but no integration of automated theorem provers yet.
slide-68
SLIDE 68

Lessons Learnt

  • Lean’s type system is very expressive and great for

definitions…

  • …if one knows the gotchas.
  • Support on Zulip is fantastic.
  • Existing tactics help a LOT…
  • …but no integration of automated theorem provers yet.
slide-69
SLIDE 69

Lessons Learnt

  • Lean’s type system is very expressive and great for

definitions…

  • …if one knows the gotchas.
  • Support on Zulip is fantastic.
  • Existing tactics help a LOT…
  • …but no integration of automated theorem provers yet.
slide-70
SLIDE 70

Lessons Learnt

  • Lean’s type system is very expressive and great for

definitions…

  • …if one knows the gotchas.
  • Support on Zulip is fantastic.
  • Existing tactics help a LOT…
  • …but no integration of automated theorem provers yet.
slide-71
SLIDE 71

Lessons Learnt

  • Lean’s type system is very expressive and great for

definitions…

  • …if one knows the gotchas.
  • Support on Zulip is fantastic.
  • Existing tactics help a LOT…
  • …but no integration of automated theorem provers yet.
slide-72
SLIDE 72

We Need You!

Help us making interactive theorem proving an even better place!

slide-73
SLIDE 73

Formalisation can be found at github.com/kappelmann/lean-continued-fractions Thanks 1 for 1 your 1 attention

Any questions?

slide-74
SLIDE 74

Formalisation can be found at github.com/kappelmann/lean-continued-fractions Thanks + 1 for + 1 your + 1 attention!

Any questions?

slide-75
SLIDE 75

Formalisation can be found at github.com/kappelmann/lean-continued-fractions Thanks + 1 for + 1 your + 1 attention!

Any questions?

slide-76
SLIDE 76

Image Sources i

  • Salt shaker: Modified from bit.ly/2K8Jw8s
  • Link 1: bit.ly/2wMGOwE
  • Link 2: bit.ly/2RaypfX
  • Link 3: bit.ly/2MNGUPt
  • Clock: bit.ly/2HOc9GC
  • Melting clock: bit.ly/2MKWknv