Skip to content

Commit b3e171b

Browse files
committed
67
1 parent b789083 commit b3e171b

File tree

1 file changed

+75
-7
lines changed

1 file changed

+75
-7
lines changed

HumanEvalLean/HumanEval67.lean

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,75 @@
11
module
2+
import Std.Data.String.ToNat
23

3-
def fruit_distribution : Unit :=
4-
()
4+
def fruitDistribution (s : String) (n : Nat) : Nat :=
5+
n - ((s.split ' ').filterMap String.Slice.toNat?).fold (init := 0) (· + ·)
6+
7+
example : fruitDistribution "5 apples and 6 oranges" 19 = 8 := by native_decide
8+
example : fruitDistribution "5 apples and 6 oranges" 21= 10 := by native_decide
9+
example : fruitDistribution "0 apples and 1 oranges" 3 = 2 := by native_decide
10+
example : fruitDistribution "1 apples and 0 oranges" 3 = 2 := by native_decide
11+
example : fruitDistribution "2 apples and 3 oranges" 100 = 95 := by native_decide
12+
example : fruitDistribution "2 apples and 3 oranges" 5 = 0 := by native_decide
13+
example : fruitDistribution "1 apples and 100 oranges" 120 = 19 := by native_decide
14+
15+
inductive Word
16+
| number : Nat → Word
17+
| nonNumber : (s : String) →
18+
(hs : s.isNat = false := by simp [← Bool.not_eq_true, String.isNat_iff]) →
19+
(hs' : ' ' ∉ s.toList := by simp) → Word
20+
21+
def Word.toNat? : Word → Option Nat
22+
| .number n => some n
23+
| .nonNumber .. => none
24+
25+
def Word.toString : Word → String
26+
| .number n => ToString.toString n
27+
| .nonNumber s _ _ => s
28+
29+
@[simp]
30+
theorem Word.toNat?_comp_toString : String.toNat? ∘ Word.toString = Word.toNat? := by
31+
ext1 w
32+
match w with
33+
| .number n => simp [Word.toNat?, Word.toString]
34+
| .nonNumber s hs hs' => simp [Word.toNat?, Word.toString, hs]
35+
36+
@[simp]
37+
theorem Word.space_notin_toList_toString {w : Word} : ' ' ∉ w.toString.toList := by
38+
match w with
39+
| .number n =>
40+
simp only [toString, Nat.toString_eq_repr, Nat.toList_repr, ↓Char.isValue]
41+
intro h
42+
simpa using Nat.isDigit_of_mem_toDigits (by decide) (by decide) h
43+
| .nonNumber _ _ hs' => exact hs'
44+
45+
@[simp]
46+
theorem String.isNat_empty : "".isNat = false := by
47+
simp [← Bool.not_eq_true, isNat_iff]
48+
49+
@[simp]
50+
theorem String.toNat?_empty : "".toNat? = none := by
51+
simp
52+
53+
theorem fruitDistribution_intercalate_toString (l : List Word) (n : Nat) :
54+
fruitDistribution (" ".intercalate (l.map Word.toString)) n = n - (l.filterMap Word.toNat?).sum := by
55+
rw [fruitDistribution]
56+
simp +instances only [String.reduceToSingleton]
57+
rw [← Std.Iter.foldl_toList, Std.Iter.toList_filterMap]
58+
rw [← String.Slice.toNat?_comp_copy, ← List.filterMap_map,
59+
String.toList_split_intercalate (by simp), ← List.sum_eq_foldl]
60+
simp only [List.map_eq_nil_iff]
61+
split <;> simp_all
62+
63+
theorem fruitDistribution_sentence {n₁ n₂ n : Nat} :
64+
fruitDistribution (toString n₁ ++ " apples and " ++ toString n₂ ++ " oranges") n = n - n₁ - n₂ := by
65+
have := fruitDistribution_intercalate_toString [.number n₁, .nonNumber "apples", .nonNumber "and", .number n₂, .nonNumber "oranges"] n
66+
simp only [List.map_cons, Word.toString, Nat.toString_eq_repr, List.map_nil,
67+
String.intercalate_cons_cons, String.reduceAppend, String.intercalate_singleton, Word.toNat?,
68+
Option.some.injEq, List.filterMap_cons_some, List.filterMap_cons_none, List.filterMap_nil,
69+
List.sum_cons, List.sum_nil, Nat.add_zero, ← Nat.sub_sub] at this
70+
rw [← this]
71+
congr 1
72+
simp [← String.toList_inj]
573

674
/-!
775
## Prompt
@@ -10,10 +78,10 @@ def fruit_distribution : Unit :=
1078
1179
def fruit_distribution(s,n):
1280
"""
13-
In this task, you will be given a string that represents a number of apples and oranges
14-
that are distributed in a basket of fruit this basket contains
15-
apples, oranges, and mango fruits. Given the string that represents the total number of
16-
the oranges and apples and an integer that represent the total number of the fruits
81+
In this task, you will be given a string that represents a number of apples and oranges
82+
that are distributed in a basket of fruit this basket contains
83+
apples, oranges, and mango fruits. Given the string that represents the total number of
84+
the oranges and apples and an integer that represent the total number of the fruits
1785
in the basket return the number of the mango fruits in the basket.
1886
for examble:
1987
fruit_distribution("5 apples and 6 oranges", 19) ->19 - 5 - 6 = 8
@@ -47,4 +115,4 @@ def check(candidate):
47115
assert candidate("2 apples and 3 oranges",5) == 0
48116
assert candidate("1 apples and 100 oranges",120) == 19
49117
```
50-
-/
118+
-/

0 commit comments

Comments
 (0)