forked from leanprover/human-eval-lean
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHumanEval106.lean
More file actions
336 lines (280 loc) · 9.14 KB
/
HumanEval106.lean
File metadata and controls
336 lines (280 loc) · 9.14 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
module
import Lean.LibrarySuggestions.Default
import Std.Tactic.Do
/-!
This file provides three solutions for problem 106, progressing from most simple to most efficient.
-/
set_option mvcgen.warning false
open Std.Do
-- missing grind annotations
attribute [grind =] Nat.length_toList_rco Nat.length_toList_rcc Std.PRange.Nat.succMany?_eq Nat.not_le
section NaiveImpl
/-!
## A naïve implementation
-/
def f (n : Nat) : List Nat := Id.run do
let mut ret : List Nat := []
for i in 1...=n do
if i % 2 = 0 then
let mut x := 1
for j in 1...=i do x := x * j
ret := x :: ret
else
let mut x := 0
for j in 1...=i do x := x + j
ret := x :: ret
return ret.reverse
/-!
### Tests
-/
example : f 5 = [1, 2, 6, 24, 15] := by cbv
example : f 7 = [1, 2, 6, 24, 15, 720, 28] := by cbv
example : f 1 = [1] := by cbv
example : f 3 = [1, 2, 6] := by cbv
/-!
### Verification
-/
@[grind =]
def factorial : Nat → Nat
| 0 => 1
| n + 1 => factorial n * (n + 1)
@[grind =]
def triangle : Nat → Nat
| 0 => 0
| n + 1 => triangle n + (n + 1)
example : factorial 1 = 1 := by decide
example : factorial 4 = 24 := by decide
example : triangle 1 = 1 := by decide
example : triangle 4 = 10 := by decide
theorem length_f {n : Nat} :
(f n).length = n := by
generalize hwp : f n = w
apply Std.Do.Id.of_wp_run_eq hwp
mvcgen
invariants
| inv1 => ⇓⟨cur, xs⟩ => ⌜xs.length = cur.prefix.length⌝
| inv2 => ⇓⟨cur, xs⟩ => ⌜True⌝ -- factorial loop
| inv3 => ⇓⟨cur, xs⟩ => ⌜True⌝ -- sum loop
with grind
theorem getElem_f {n : Nat} {k : Nat} (hlt : k < n) :
(f n)[k]'(by grind [length_f]) = if k % 2 = 0 then triangle (k + 1) else factorial (k + 1) := by
rw [List.getElem_eq_iff]
generalize hwp : f n = w
apply Std.Do.Id.of_wp_run_eq hwp
mvcgen
invariants
-- outer loop
| inv1 => ⇓⟨cur, xs⟩ => ⌜xs.length = cur.prefix.length ∧
((_ : k < xs.length) → xs[xs.length - 1 - k] =
if k % 2 = 0 then triangle (k + 1) else factorial (k + 1))⌝
-- factorial loop
| inv2 => ⇓⟨cur, x⟩ => ⌜x = factorial cur.prefix.length⌝
-- sum loop
| inv3 => ⇓⟨cur, x⟩ => ⌜x = triangle cur.prefix.length⌝
-- OUTER LOOP
case vc7 => simp -- base case
case vc8 => grind -- postcondition
-- FACTORIAL LOOP
case vc3 => -- step
have := Std.Rcc.eq_succMany?_of_toList_eq_append_cons ‹_›
grind
case vc1 => -- postcondition
simp_all [Std.Rcc.eq_succMany?_of_toList_eq_append_cons ‹_›, factorial, Nat.add_comm 1]
-- SUM LOOP
case vc6 => -- step
have := Std.Rcc.eq_succMany?_of_toList_eq_append_cons ‹_›
grind
case vc4 => -- postcondition
simp_all [Std.Rcc.eq_succMany?_of_toList_eq_append_cons ‹_›, triangle, Nat.add_comm 1]
end NaiveImpl
section EfficientImpl
/-!
## An efficient implementation
-/
def f' (n : Nat) : Array Nat := Id.run do
if n ≤ 2 then
return #[1, 2].take n
let mut ret : Array Nat := .emptyWithCapacity n
ret := ret.push 1 -- 1st entry should be `triangle 1`
ret := ret.push 2 -- 2nd entry should be `factorial 2`
for i in 3...=n do
if i % 2 = 0 then
-- It would be nicer if we could use `ret[i - 2]`, but it is unclear how to use the
-- invariants `ret.size ≥ 2` and `i = ret.size` intrinsically.
ret := ret.push (ret[i - 3]! * (i - 1) * i)
else
ret := ret.push (ret[i - 3]! + 2 * i - 1)
return ret
/-!
### Tests
-/
example : f' 5 = #[1, 2, 6, 24, 15] := by cbv
example : f' 7 = #[1, 2, 6, 24, 15, 720, 28] := by cbv
example : f' 1 = #[1] := by cbv
example : f' 3 = #[1, 2, 6] := by cbv
/-!
### Verification
-/
theorem size_f' {n : Nat} :
(f' n).size = n := by
generalize hwp : f' n = w
apply Std.Do.Id.of_wp_run_eq hwp
mvcgen
all_goals try infer_instance
case inv1 => exact ⇓⟨cur, xs⟩ => ⌜xs.size = cur.prefix.length + 2⌝
all_goals try (simp_all; done) -- relies on `Nat.size_Rcc`
grind
theorem getElem_f' {n : Nat} {k : Nat} (hlt : k < n) :
(f' n)[k]'(by grind [size_f']) = if k % 2 = 0 then triangle (k + 1) else factorial (k + 1) := by
rw [Array.getElem_eq_iff]
generalize hwp : f' n = w
apply Std.Do.Id.of_wp_run_eq hwp
mvcgen
invariants
| inv1 => ⇓⟨cur, xs⟩ => ⌜xs.size = cur.prefix.length + 2 ∧ ∀ j : Nat, (_ : j < xs.size) →
(j % 2 = 1 → xs[j] = factorial (j + 1)) ∧ (j % 2 = 0 → xs[j] = triangle (j + 1))⌝
case vc1 hn => -- verification of the early return
grind
case vc4 => -- base case of the loop
grind
case vc2 hmod h => -- `then` branch
have := Std.Rcc.eq_succMany?_of_toList_eq_append_cons ‹_›
grind
case vc3 => -- `else` branch
have := Std.Rcc.eq_succMany?_of_toList_eq_append_cons ‹_›
grind
case vc5 => -- postcondition
grind
end EfficientImpl
section MoreEfficientImpl
/-!
## An efficient implementation avoiding `[·]!`
-/
def f'' (n : Nat) : Array Nat := Id.run do
if n ≤ 2 then
return #[1, 2].take n
let mut ret : Array Nat := .emptyWithCapacity n
ret := ret.push 1 -- 1st entry should be `triangle 1`
ret := ret.push 2 -- 2nd entry should be `factorial 2`
let mut odd := 1 -- `triangle 1 = 1`
let mut even := 2 -- `factorial 2 = 2`
for i in 3...=n do
if i % 2 = 0 then
even := even * (i - 1) * i -- `factorial i = factorial (i - 2) * i * i + 1`
ret := ret.push even
else
odd := odd + 2 * i - 1 -- `triangle i = triangle (i - 2) + 2 * i - 1`
ret := ret.push odd
return ret
/-!
### Tests
-/
example : f'' 5 = #[1, 2, 6, 24, 15] := by cbv
example : f'' 7 = #[1, 2, 6, 24, 15, 720, 28] := by cbv
example : f'' 1 = #[1] := by cbv
example : f'' 3 = #[1, 2, 6] := by cbv
/-!
### Verification
-/
theorem size_f'' {n : Nat} :
(f'' n).size = n := by
generalize hwp : f'' n = w
apply Std.Do.Id.of_wp_run_eq hwp
mvcgen
invariants
| inv1 => ⇓⟨cur, _, _, xs⟩ => ⌜xs.size = cur.prefix.length + 2⌝
with grind
def lastFactorial (n : Nat) := factorial (n / 2 * 2)
def lastTriangle (n : Nat) := triangle ((n - 1) / 2 * 2 + 1)
@[grind =] theorem lastTriangle_two : lastTriangle 2 = 1 := by decide
@[grind =] theorem lastFactorial_two : lastFactorial 2 = 2 := by decide
@[grind →]
theorem lastTriangle_of_odd (h : n % 2 = 1) : lastTriangle n = triangle n := by
grind [lastTriangle]
@[grind →]
theorem lastFactorial_of_even (h : n % 2 = 0) : lastFactorial n = factorial n := by
grind [lastFactorial]
theorem lastTriangle_add_one (h : n % 2 = 1) :
lastTriangle (n + 1) = lastTriangle n := by
grind [lastTriangle]
@[grind =]
theorem lastFactorial_add_one_of_odd (h : n % 2 = 1) :
lastFactorial (n + 1) = lastFactorial n * n * (n + 1) := by
have : (n + 1) / 2 * 2 = n / 2 * 2 + 2 := by grind
grind [lastFactorial]
@[grind =]
theorem lastTriangle_add_one_of_odd (h : n % 2 = 1) :
lastTriangle (n + 1) = lastTriangle n := by
grind [lastTriangle]
@[grind =]
theorem lastTriangle_add_one_of_even (h : n % 2 = 0) (h' : 0 < n) :
lastTriangle (n + 1) = lastTriangle n + 2 * n + 1 := by
have : n / 2 * 2 = (n - 1) / 2 * 2 + 2 := by grind
grind [lastTriangle]
@[grind =]
theorem lastFactorial_add_one_of_even (h : n % 2 = 0) :
lastFactorial (n + 1) = lastFactorial n := by
grind [lastFactorial]
theorem getElem_f'' {n : Nat} {k : Nat} (hlt : k < n) :
(f'' n)[k]'(by grind [size_f'']) = if k % 2 = 0 then triangle (k + 1) else factorial (k + 1) := by
rw [Array.getElem_eq_iff]
generalize hwp : f'' n = w
apply Std.Do.Id.of_wp_run_eq hwp
mvcgen
invariants
| inv1 =>
⇓⟨cur, even, odd, xs⟩ => ⌜xs.size = cur.prefix.length + 2 ∧ even = lastFactorial xs.size ∧ odd = lastTriangle xs.size ∧ ∀ j : Nat, (_ : j < xs.size) →
(j % 2 = 1 → xs[j] = factorial (j + 1)) ∧ (j % 2 = 0 → xs[j] = triangle (j + 1))⌝
case vc1 hn => -- verification of the early return
-- the return value is a prefix of `[1, 2]` and `k` is the index that needs to be verified
match k with
| 0 => grind
| 1 => grind
| n + 2 => grind
case vc4 => -- base case of the loop
grind
case vc2 hmod h => -- `then` branch
have := Std.Rcc.eq_succMany?_of_toList_eq_append_cons ‹_›
grind
case vc3 => -- `else` branch
have := Std.Rcc.eq_succMany?_of_toList_eq_append_cons ‹_›
grind
case vc5 => -- postcondition
grind
end MoreEfficientImpl
/-!
## Prompt
```python3
def f(n):
""" Implement the function f that takes n as a parameter,
and returns a list of size n, such that the value of the element at index i is the factorial of i if i is even
or the sum of numbers from 1 to i otherwise.
i starts from 1.
the factorial of i is the multiplication of the numbers from 1 to i (1 * 2 * ... * i).
Example:
f(5) == [1, 2, 6, 24, 15]
"""
```
## Canonical solution
```python3
ret = []
for i in range(1,n+1):
if i%2 == 0:
x = 1
for j in range(1,i+1): x *= j
ret += [x]
else:
x = 0
for j in range(1,i+1): x += j
ret += [x]
return ret
```
## Tests
```python3
def check(candidate):
assert candidate(5) == [1, 2, 6, 24, 15]
assert candidate(7) == [1, 2, 6, 24, 15, 720, 28]
assert candidate(1) == [1]
assert candidate(3) == [1, 2, 6]
```
-/