forked from leanprover/human-eval-lean
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHumanEval0.lean
More file actions
128 lines (103 loc) · 4.03 KB
/
HumanEval0.lean
File metadata and controls
128 lines (103 loc) · 4.03 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
module
import Std
import all Init.Data.List.Sort.Basic
import all Init.Data.Slice.Array.Lemmas
import all Init.Data.Range.Polymorphic.UpwardEnumerable -- UpwardEnumerable.least not exposed
open Std Std.Do
set_option mvcgen.warning false
/-!
## Missing API
-/
theorem Nat.eq_add_of_toList_rio_eq_append_cons {a : Nat} {pref cur suff}
(h : (*...a).toList = pref ++ cur :: suff) :
cur = pref.length := by
have := Rio.eq_succMany?_of_toList_eq_append_cons h
simpa [PRange.UpwardEnumerable.least, PRange.least?] using this
/-!
## Implementation
-/
def hasCloseElements (xs : Array Rat) (threshold : Rat) : Bool := Id.run do
let sorted := xs.mergeSort
for h : i in *...(sorted.size - 1) do
if (sorted[i + 1] - sorted[i]).abs < threshold then
return true
return false
/-!
## Tests
-/
example : hasCloseElements #[1.0, 2.0, 3.9, 4.0, 5.0, 2.2] 0.3 = true := by cbv
example : hasCloseElements #[1.0, 2.0, 3.9, 4.0, 5.0, 2.2] 0.05 = false := by cbv
example : hasCloseElements #[1.0, 2.0, 5.9, 4.0, 5.0] 0.95 = true := by cbv
example : hasCloseElements #[1.0, 2.0, 5.9, 4.0, 5.0] 0.8 = false := by cbv
example : hasCloseElements #[1.0, 2.0, 3.0, 4.0, 5.0, 2.0] 0.1 = true := by cbv
example : hasCloseElements #[1.1, 2.2, 3.1, 4.1, 5.1] 1.0 = true := by cbv
example : hasCloseElements #[1.1, 2.2, 3.1, 4.1, 5.1] 0.5 = false := by cbv
/-!
## Verification
-/
theorem hasCloseElements_iff_mergeSort {xs threshold} :
hasCloseElements xs threshold ↔ ∃ (i : Nat) (_ : i < xs.mergeSort.size - 1), (xs.mergeSort[i + 1] - xs.mergeSort[i]).abs < threshold := by
generalize hwp : hasCloseElements xs threshold = wp
apply Id.of_wp_run_eq hwp
mvcgen
invariants
· .withEarlyReturn
(fun cur _ => ⌜∀ i < cur.prefix.length, threshold ≤ (xs.mergeSort[i + 1]! - xs.mergeSort[i]!).abs⌝)
(fun r _ => ⌜r = true ∧ ∃ (i : Nat) (_ : i < xs.mergeSort.size - 1), (xs.mergeSort[i + 1] - xs.mergeSort[i]).abs < threshold⌝)
with grind [Nat.eq_add_of_toList_rio_eq_append_cons, Rio.length_toList, Nat.size_rio]
theorem hasCloseElements_iff {xs threshold} :
hasCloseElements xs threshold ↔ ¬ xs.toList.Pairwise (fun a b => threshold ≤ (b - a).abs) := by
simp only [hasCloseElements_iff_mergeSort]
have := xs.mergeSort_perm (le := (· ≤ ·))
rw [← this.pairwise_iff]
· apply Iff.intro
· simp only [List.pairwise_iff_getElem, Classical.not_forall]
grind
· simp only [List.pairwise_iff_getElem, Classical.not_forall]
rintro ⟨i, j, hi, hj, hij, h⟩
refine ⟨i, by grind, ?_⟩
have h_sorted := xs.pairwise_mergeSort (le := (· ≤ ·)) (by grind) (by grind)
rw [List.pairwise_iff_getElem] at h_sorted
rw [Rat.abs_of_nonneg (by grind)] at ⊢ h
have : xs.mergeSort[i + 1]'(by grind) ≤ xs.mergeSort[j] := by by_cases i + 1 = j <;> grind
grind
· simp [Rat.abs_sub_comm]
/-!
## Prompt
```python3
from typing import List
def has_close_elements(numbers: List[float], threshold: float) -> bool:
""" Check if in given list of numbers, are any two numbers closer to each other than
given threshold.
>>> has_close_elements([1.0, 2.0, 3.0], 0.5)
False
>>> has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3)
True
"""
```
## Canonical solution
```python3
for idx, elem in enumerate(numbers):
for idx2, elem2 in enumerate(numbers):
if idx != idx2:
distance = abs(elem - elem2)
if distance < threshold:
return True
return False
```
## Tests
```python3
METADATA = {
'author': 'jt',
'dataset': 'test'
}
def check(candidate):
assert candidate([1.0, 2.0, 3.9, 4.0, 5.0, 2.2], 0.3) == True
assert candidate([1.0, 2.0, 3.9, 4.0, 5.0, 2.2], 0.05) == False
assert candidate([1.0, 2.0, 5.9, 4.0, 5.0], 0.95) == True
assert candidate([1.0, 2.0, 5.9, 4.0, 5.0], 0.8) == False
assert candidate([1.0, 2.0, 3.0, 4.0, 5.0, 2.0], 0.1) == True
assert candidate([1.1, 2.2, 3.1, 4.1, 5.1], 1.0) == True
assert candidate([1.1, 2.2, 3.1, 4.1, 5.1], 0.5) == False
```
-/