Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit 1011601

Browse files
kappelmannjcommelin
andcommitted
feat(algebra/continued_fractions): add termination iff rat lemmas (#4867)
### What Show that the computation of a continued fraction terminates if and only if we compute the continued fraction of a rational number. ### How 1. Show that every intermediate operation involved in the computation of a continued fraction returns a value that has a rational counterpart. This then shows that a terminating continued fraction corresponds to a rational value. 2. Show that the operations involved in the computation of a continued fraction for rational numbers only return results that can be lifted to the result of the same operations done on an equal value in a suitable linear ordered, archimedean field with a floor function. 3. Show that the continued fraction of a rational number terminates. 4. Set up the needed coercions to express the results above starting from [here](https://github.com/leanprover-community/mathlib/compare/kappelmann_termination_iff_rat?expand=1#diff-1dbcf8473152b2d8fca024352bd899af37669b8af18792262c2d5d6f31148971R129). I did not know where to put these lemmas. Please let me know your opinion. 4. Extract 4 auxiliary lemmas that are not specific to continued fraction but more generally about rational numbers, integers, and natural numbers starting from [here](https://github.com/leanprover-community/mathlib/compare/kappelmann_termination_iff_rat?expand=1#diff-1dbcf8473152b2d8fca024352bd899af37669b8af18792262c2d5d6f31148971R28). Again, I did not know where to put these. Please let me know your opinion. Co-authored-by: Kevin Kappelmann <kappelmann@users.noreply.github.com> Co-authored-by: Johan Commelin <johan@commelin.net>
1 parent 9adf9bb commit 1011601

File tree

3 files changed

+367
-21
lines changed

3 files changed

+367
-21
lines changed

src/algebra/continued_fractions/basic.lean

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,25 @@ protected structure generalized_continued_fraction.pair := (a : α) (b : α)
4949
namespace generalized_continued_fraction.pair
5050
open generalized_continued_fraction as gcf
5151

52-
/-- Make a gcf.pair printable. -/
52+
variable {α}
53+
54+
/-- Make a `gcf.pair` printable. -/
5355
instance [has_repr α] : has_repr (gcf.pair α) :=
5456
⟨λ p, "(a : " ++ (repr p.a) ++ ", b : " ++ (repr p.b) ++ ")"
5557

58+
/-- Maps a function `f` on both components of a given pair. -/
59+
def map {β : Type*} (f : α → β) (gp : gcf.pair α) : gcf.pair β :=
60+
⟨f gp.a, f gp.b⟩
61+
5662
section coe
5763
/-! Interlude: define some expected coercions. -/
58-
/- Fix another type `β` and assume `α` can be converted to `β`. -/
59-
variables {α} {β : Type*} [has_coe α β]
64+
/- Fix another type `β` which we will convert to. -/
65+
variables {β : Type*} [has_coe α β]
6066

6167
/-- Coerce a pair by elementwise coercion. -/
62-
instance has_coe_to_generalized_continued_fraction_pair : has_coe (gcf.pair α) (gcf.pair β) :=
63-
⟨λ ⟨a, b⟩, ⟨(a : β), (b : β)⟩⟩
68+
instance has_coe_to_generalized_continued_fraction_pair :
69+
has_coe (gcf.pair α) (gcf.pair β) :=
70+
⟨map coe⟩
6471

6572
@[simp, norm_cast]
6673
lemma coe_to_generalized_continued_fraction_pair {a b : α} :
@@ -70,6 +77,8 @@ rfl
7077
end coe
7178
end generalized_continued_fraction.pair
7279

80+
variable (α)
81+
7382
/--
7483
A *generalised continued fraction* (gcf) is a potentially infinite expression of the form
7584
@@ -120,22 +129,17 @@ def terminates (g : gcf α) : Prop := g.s.terminates
120129

121130
section coe
122131
/-! Interlude: define some expected coercions. -/
123-
-- Fix another type `β` and assume `α` can be converted to `β`.
132+
/- Fix another type `β` which we will convert to. -/
124133
variables {β : Type*} [has_coe α β]
125134

126-
/-- Coerce a sequence by elementwise coercion. -/
127-
def seq.coe_to_seq : has_coe (seq α) (seq β) := ⟨seq.map (λ a, (a : β))⟩
128-
129-
local attribute [instance] seq.coe_to_seq
130-
131135
/-- Coerce a gcf by elementwise coercion. -/
132136
instance has_coe_to_generalized_continued_fraction : has_coe (gcf α) (gcf β) :=
133-
⟨λ ⟨h, s⟩, ⟨(h : β), (s : seq $ gcf.pair β)⟩⟩
137+
⟨λ g, ⟨(g.h : β), (g.s.map coe : seq $ gcf.pair β)⟩⟩
134138

135139
@[simp, norm_cast]
136140
lemma coe_to_generalized_continued_fraction {g : gcf α} :
137-
(↑(g : gcf α) : gcf β) = ⟨(g.h : β), (g.s : seq $ gcf.pair β)⟩ :=
138-
by { cases g, refl }
141+
(↑(g : gcf α) : gcf β) = ⟨(g.h : β), (g.s.map coe : seq $ gcf.pair β)⟩ :=
142+
rfl
139143

140144
end coe
141145
end generalized_continued_fraction

src/algebra/continued_fractions/computation/basic.lean

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ We collect an integer part `b = ⌊v⌋` and fractional part `fr = v - ⌊v⌋`
7272
-/
7373
structure int_fract_pair := (b : ℤ) (fr : K)
7474

75+
variable {K}
76+
7577
/-! Interlude: define some expected coercions and instances. -/
7678
namespace int_fract_pair
7779

@@ -81,17 +83,20 @@ instance [has_repr K] : has_repr (int_fract_pair K) :=
8183

8284
instance inhabited [inhabited K] : inhabited (int_fract_pair K) := ⟨⟨0, (default _)⟩⟩
8385

84-
variable {K}
86+
/--
87+
Maps a function `f` on the fractional components of a given pair.
88+
-/
89+
def mapFr {β : Type*} (f : K → β) (gp : int_fract_pair K) : int_fract_pair β :=
90+
⟨gp.b, f gp.fr⟩
8591

8692
section coe
8793
/-! Interlude: define some expected coercions. -/
88-
/- Fix another type `β` and assume `K` can be converted to `β`. -/
94+
/- Fix another type `β` which we will convert to. -/
8995
variables {β : Type*} [has_coe K β]
9096

9197
/-- Coerce a pair by coercing the fractional component. -/
9298
instance has_coe_to_int_fract_pair : has_coe (int_fract_pair K) (int_fract_pair β) :=
93-
⟨λ ⟨b, fr⟩, ⟨b, (fr : β)⟩⟩
94-
99+
⟨mapFr coe⟩
95100

96101
@[simp, norm_cast]
97102
lemma coe_to_int_fract_pair {b : ℤ} {fr : K} :
@@ -153,16 +158,14 @@ protected def seq1 (v : K) : seq1 $ int_fract_pair K :=
153158

154159
end int_fract_pair
155160

156-
variable {K}
157-
158161
/--
159162
Returns the `generalized_continued_fraction` of a value. In fact, the returned gcf is also
160163
a `continued_fraction` that terminates if and only if `v` is rational (those proofs will be
161164
added in a future commit).
162165
163166
The continued fraction representation of `v` is given by `[⌊v⌋; b₀, b₁, b₂,...]`, where
164167
`[b₀; b₁, b₂,...]` recursively is the continued fraction representation of `1 / (v − ⌊v⌋)`. This
165-
process stops when the fractional part `v- ⌊v⌋` hits 0 at some step.
168+
process stops when the fractional part `v - ⌊v⌋` hits 0 at some step.
166169
167170
The implementation uses `int_fract_pair.stream` to obtain the partial denominators of the continued
168171
fraction. Refer to said function for more details about the computation process.

0 commit comments

Comments
 (0)