-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFibonacci.lean
172 lines (156 loc) · 4.22 KB
/
Fibonacci.lean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
-- Fibonacci.lean
-- Equivalence of definitions of the Fibonacci function.
-- José A. Alonso Jiménez <https://jaalonso.github.io>
-- Seville, August 29, 2024
-- ---------------------------------------------------------------------
-- ---------------------------------------------------------------------
-- In Lean4, the Fibonacci function can be defined as
-- def fibonacci : Nat → Nat
-- | 0 => 0
-- | 1 => 1
-- | n + 2 => fibonacci n + fibonacci (n+1)
--
-- Another more efficient definition is
-- def fib (n : Nat) : Nat :=
-- (loop n).1
-- where
-- loop : Nat → Nat × Nat
-- | 0 => (0, 1)
-- | n + 1 =>
-- let p := loop n
-- (p.2, p.1 + p.2)
--
-- Prove that both definitions are equivalent; that is,
-- fibonacci = fib
-- ---------------------------------------------------------------------
-- Proof in natural language
-- =========================
-- From the definition of the mirror function, we have the following lemma
-- fib_suma : fib (n + 2) = fib n + fib (n + 1)
--
-- We need to prove that, for all n ∈ ℕ,
-- fibonacci n = fib n
-- We will prove this by induction on n
--
-- Case 1: Suppose that n = 0. Then,
-- fibonacci n = fibonacci 0
-- = 1
-- and
-- fib n = fib 0
-- = (loop 0).1
-- = (0, 1).1
-- = 1
-- Therefore,
-- fibonacci n = fib n
--
-- Case 2: Suppose that n = 1. Then,
-- fibonacci n = 1
-- and
-- fib 1 = (loop 1).1
-- = (p.2, p.1 + p.2).1
-- donde p = loop 0
-- = ((0, 1).2, (0, 1).1 + (0, 1).2).1
-- = (1, 0 + 1).1
-- = 1
-- Therefore,
-- fibonacci n = fib n
--
-- Case 3: Suppose that n = m + 2 and that the induction hypotheses hold,
-- ih1 : fibonacci n = fib n
-- ih2 : fibonacci (n + 1) = fib (n + 1)
-- Then,
-- fibonacci n
-- = fibonacci (m + 2)
-- = fibonacci m + fibonacci (m + 1)
-- = fib m + fib (m + 1) [por ih1, ih2]
-- = fib (m + 2) [por fib_suma]
-- = fib n
-- Proof with Lean4
-- ================
open Nat
set_option pp.fieldNotation false
def fibonacci : Nat → Nat
| 0 => 0
| 1 => 1
| n + 2 => fibonacci n + fibonacci (n+1)
def fib (n : Nat) : Nat :=
(loop n).1
where
loop : Nat → Nat × Nat
| 0 => (0, 1)
| n + 1 =>
let p := loop n
(p.2, p.1 + p.2)
-- Auxiliary lemma
-- ===============
theorem fib_suma (n : Nat) : fib (n + 2) = fib n + fib (n + 1) :=
by rfl
-- Proof 1
-- =======
example : fibonacci = fib :=
by
ext n
-- n : Nat
-- ⊢ fibonacci n = fib n
induction n using fibonacci.induct with
| case1 =>
-- ⊢ fibonacci 0 = fib 0
rfl
| case2 =>
-- ⊢ fibonacci 1 = fib 1
rfl
| case3 n ih1 ih2 =>
-- n : Nat
-- ih1 : fibonacci n = fib n
-- ih2 : fibonacci (n + 1) = fib (n + 1)
-- ⊢ fibonacci (succ (succ n)) = fib (succ (succ n))
rw [fib_suma]
-- ⊢ fibonacci (succ (succ n)) = fib n + fib (n + 1)
rw [fibonacci]
-- ⊢ fibonacci n + fibonacci (n + 1) = fib n + fib (n + 1)
rw [ih1]
-- ⊢ fib n + fibonacci (n + 1) = fib n + fib (n + 1)
rw [ih2]
-- Proof 2
-- =======
example : fibonacci = fib :=
by
ext n
-- n : Nat
-- ⊢ fibonacci n = fib n
induction n using fibonacci.induct with
| case1 =>
-- ⊢ fibonacci 0 = fib 0
rfl
| case2 =>
-- ⊢ fibonacci 1 = fib 1
rfl
| case3 n ih1 ih2 =>
-- n : Nat
-- ih1 : fibonacci n = fib n
-- ih2 : fibonacci (n + 1) = fib (n + 1)
-- ⊢ fibonacci (succ (succ n)) = fib (succ (succ n))
calc fibonacci (succ (succ n))
= fibonacci n + fibonacci (n + 1) := by rw [fibonacci]
_ = fib n + fib (n + 1) := by rw [ih1, ih2]
_ = fib (succ (succ n)) := by rw [fib_suma]
-- Proof 3
-- =======
example : fibonacci = fib :=
by
ext n
-- n : Nat
-- ⊢ fibonacci n = fib n
induction n using fibonacci.induct with
| case1 =>
-- ⊢ fibonacci 0 = fib 0
rfl
| case2 =>
-- ⊢ fibonacci 1 = fib 1
rfl
| case3 n ih1 ih2 =>
-- n : Nat
-- ih1 : fibonacci n = fib n
-- ih2 : fibonacci (n + 1) = fib (n + 1)
-- ⊢ fibonacci (succ (succ n)) = fib (succ (succ n))
simp [ih1, ih2, fibonacci, fib_suma]