continued fractions in lean
play

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


  1. Continued Fractions in Lean A Newbie’s Adventure Kevin Kappelmann June 14, 2019 Vrije Universiteit Amsterdam

  2. Let’s Go on an Adventure

  3. Choose a Weapon …perhaps because I am interning at VU Amsterdam

  4. Choose a Weapon …perhaps because I am interning at VU Amsterdam

  5. Choose a Weapon …perhaps because I am interning at VU Amsterdam

  6. Choose a Weapon …perhaps because I am interning at VU Amsterdam

  7. The Adventurer’s Skill Set • Some experience using Isabelle • First project with a dependent type theorem prover • Basic maths and functional programming knowledge

  8. The Adventurer’s Skill Set • Some experience using Isabelle • First project with a dependent type theorem prover • Basic maths and functional programming knowledge

  9. The Adventurer’s Skill Set • Some experience using Isabelle • First project with a dependent type theorem prover • Basic maths and functional programming knowledge

  10. The Adventurer’s Skill Set • Some experience using Isabelle • First project with a dependent type theorem prover • Basic maths and functional programming knowledge

  11. Definitions

  12. • each a i is a partial numerator • each b i is a partial denominator Generalized Continued Fractions A generalized continued fraction is… b a 0 b 0 a 1 b 1 a 2 b 2 a 3 b 3 ... • b is called the integer part

  13. • each a i is a partial numerator • each b i is a partial denominator Generalized Continued Fractions A generalized continued fraction is… a 0 a 1 a 2 a 3 • b is called the integer part b + b 0 + b 1 + b 2 + b 3 + ...

  14. • each a i is a partial numerator • each b i is a partial denominator Generalized Continued Fractions A generalized continued fraction is… a 0 a 1 a 2 a 3 • b is called the integer part b + b 0 + b 1 + b 2 + b 3 + ...

  15. • each b i is a partial denominator Generalized Continued Fractions A generalized continued fraction is… a 0 a 1 a 2 a 3 • b is called the integer part b + b 0 + b 1 + b 2 + b 3 + ... • each a i is a partial numerator

  16. Generalized Continued Fractions A generalized continued fraction is… a 0 a 1 a 2 a 3 • b is called the integer part b + b 0 + b 1 + b 2 + b 3 + ... • each a i is a partial numerator • each b i is a partial denominator

  17. ... 3 6 9 2 6 7 2 6 5 2 6 3 2 6 1 2 Generalized continued fraction Continued fraction 1 1 1 1 1 Generalized Continued Fractions of π π = 3 + 7 + 15 + 1 + 292 + 1 + ...

  18. ... 3 6 9 2 6 7 2 6 5 2 6 3 2 6 1 2 Generalized continued fraction Continued fraction 1 1 1 1 1 Generalized Continued Fractions of π π = 3 + 7 + 15 + 1 + 292 + 1 + ...

  19. 1 Continued fraction 7 2 1 5 2 1 3 2 1 1 2 1 Generalized continued fraction 9 2 Generalized Continued Fractions of π π = 3 + π = 3 + 7 + 6 + 15 + 6 + 1 + 6 + 292 + 6 + 1 + ... 6 + ...

  20. 1 Continued fraction 7 2 1 5 2 1 3 2 1 1 2 1 Generalized continued fraction 9 2 Generalized Continued Fractions of π π = 3 + π = 3 + 7 + 6 + 15 + 6 + 1 + 6 + 292 + 6 + 1 + ... 6 + ...

  21. 1 /- Fix a type -/ 2 variable (α : Type *) Generalized Continued Fractions in Lean a 0 a 1 a 2 a 3 b + b 0 + b 1 + b 2 + b 3 + ...

  22. Generalized Continued Fractions in Lean a 0 a 1 a 2 a 3 b + b 0 + b 1 + b 2 + b 3 + ... 1 /- Fix a type -/ 2 variable (α : Type *)

  23. Generalized Continued Fractions in Lean 4 a 0 a 1 a 2 a 3 1 2 5 3 b + b 0 + b 1 + b 2 + b 3 + ... /- Fix a type -/ variable (α : Type *) /-- A gcf_pair consists of a partial numerator a and partial denominator b -/ ֒ → structure gcf_pair := (a : α) (b : α)

  24. Generalized Continued Fractions in Lean a 3 a 0 a 1 a 2 b + /- Fix a type -/ variable (α : Type *) b 0 + b 1 + /-- A gcf_pair consists of a partial numerator a and partial denominator b -/ b 2 + ֒ → structure gcf_pair := (a : α) (b : α) b 3 + ... -- Once a sequence hits none, it stays none def seq := {f : ℕ → option α // ∀ {n}, f n = none → f (n + 1) = none} ֒ →

  25. Generalized Continued Fractions in Lean a 1 a 2 a 3 a 0 b + /- Fix a type -/ variable (α : Type *) b 0 + b 1 + /-- A gcf_pair consists of a partial numerator a and partial denominator b -/ b 2 + ֒ → structure gcf_pair := (a : α) (b : α) b 3 + ... 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 a n and ֒ → partial denominators b n -/ ֒ → structure gcf := (head : α) (seq : seq (gcf_pair α)) ֒ →

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

  27. Evaluate Generalized Continued Fractions 9 3 4 5 end 7 10 8 end 1 def aux : ℕ → seq (gcf_pair α) → α 2 | 0 s := match s.head with | none := 0 | some ⟨a, b⟩ := a / b 6 | (n + 1) s := match s.head with | none := 0 | some ⟨a, b⟩ := a / (b + aux n s.tail) 11 def convergents (g : gcf α) (n : ℕ) : α := 12 g.head + if n = 0 then 0 else aux n g.seq

  28. 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} Continued Fractions 1 1 1 1 First impression: Pretty Sweet! b + b 0 + b 1 + b 2 + b 3 + ...

  29. Continued Fractions 1 First impression: 1 Pretty Sweet! 1 1 b + b 0 + b 1 + b 2 + b 3 + ... 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} ֒ →

  30. Continued Fractions 1 First impression: 1 Pretty Sweet! 1 1 b + b 0 + b 1 + b 2 + b 3 + ... 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} ֒ →

  31. Continued Fractions 1 1 First impression: Pretty Sweet! 1 1 b + b 0 + b 1 + b 2 + b 3 + ... 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} ֒ →

  32. Fun with Subtypes So, since cf is a subtype of gcf , we can do 2 NOPE! Oh, I see – I need to cast! 1 def convergents (g : gcf α) (n : ℕ) : α := ... 3 variable (c : cf α) 4 # check convergents c 0

  33. Fun with Subtypes So, since cf is a subtype of gcf , we can do 2 NOPE! Oh, I see – I need to cast! 1 def convergents (g : gcf α) (n : ℕ) : α := ... 3 variable (c : cf α) 4 # check convergents c 0

  34. Fun with Subtypes So, since cf is a subtype of gcf , we can do 2 Oh, I see – I need to cast! 1 def convergents (g : gcf α) (n : ℕ) : α := ... 3 variable (c : cf α) 4 # check convergents c 0

  35. Fun with Subtypes So, since cf is a subtype of gcf , we can do 2 Oh, I see – I need to cast! 1 def convergents (g : gcf α) (n : ℕ) : α := ... 3 variable (c : cf α) 4 # check convergents c 0

  36. Fun with Subtypes So, since cf is a subtype of gcf , we can do 2 NOPE! …alright, let’s go on Zulip 1 def convergents (g : gcf α) (n : ℕ) : α := ... 3 variable (c : cf α) 4 # check convergents (c : gcf α) 0

  37. Fun with Subtypes So, since cf is a subtype of gcf , we can do 2 NOPE! …alright, let’s go on Zulip 1 def convergents (g : gcf α) (n : ℕ) : α := ... 3 variable (c : cf α) 4 # check convergents (c : gcf α) 0

  38. Fun with Subtypes So, since cf is a subtype of gcf , we can do 2 …alright, let’s go on Zulip 1 def convergents (g : gcf α) (n : ℕ) : α := ... 3 variable (c : cf α) 4 # check convergents (c : gcf α) 0

  39. Fun with Subtypes So, since cf is a subtype of gcf , we can do 2 …alright, let’s go on Zulip 1 def convergents (g : gcf α) (n : ℕ) : α := ... 3 variable (c : cf α) 4 # check convergents (c : gcf α) 0

  40. Please Help Me A few minutes and messages from Kevin Buzzard later…

  41. 1 variable (c : cf α) 2 # check convergents (c : gcf α) 0 1 instance cf_to_gcf : has_coe (cf β) (gcf β) 2 := by { unfold cf, apply_instance} 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 The “Solution” We first need to define the casting 3 Now this works:

  42. 1 variable (c : cf α) 2 # check convergents (c : gcf α) 0 The “Solution” We first need to define the casting 3 Now this works: 1 instance cf_to_gcf : has_coe (cf β) (gcf β) 2 := by { unfold cf, apply_instance} 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

  43. The “Solution” We first need to define the casting 3 Now this works: 1 instance cf_to_gcf : has_coe (cf β) (gcf β) 2 := by { unfold cf, apply_instance} 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 1 variable (c : cf α) 2 # check convergents (c : gcf α) 0

Recommend


More recommend