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
106import human_eval.problem_28
namespace Submission
privatetheorem toList_foldl_append (acc : String) (l : List String) :
(l.foldl (fun r s => r ++ s) acc).toList =
acc.toList ++ (l.foldl (fun r s => r ++ s) "").toList := by
induction l generalizing acc with
| nil => simp
| cons hd tl ih =>
rw [List.foldl_cons, List.foldl_cons, ih (acc ++ hd), ih ("" ++ hd)]
simp [List.append_assoc]
privatetheorem toList_join_cons (s : String) (l : List String) :
(String.join (s :: l)).toList = s.toList ++ (String.join l).toList := by
unfold String.join
rw [List.foldl_cons]
simpa using toList_foldl_append ("" ++ s) l
privatetheorem take_append_len (a b : List Char) :
(a ++ b).take a.length = a := by
induction a with
| nil => simp
| cons c cs ih => simp [ih]
privatetheorem take_append_add (a b : List Char) (n : Nat) :
(a ++ b).take (a.length + n) = a ++ b.take n := by
induction a with
| nil => simp
| cons c cs ih =>
have h : (c :: cs).length + n = (cs.length + n) + 1 := by simp; omega
rw [h]
simp [ih]
privatetheorem drop_append_add (a c : List Char) (m : Nat) :
(a ++ c).drop (a.length + m) = c.drop m := by
induction a with
| nil => simp
| cons c0 cs ih =>
have h : (c0 :: cs).length + m = (cs.length + m) + 1 := by simp; omega
rw [h]
simp [ih]
privatetheorem elem_len_le_take_sum (l : List String) (k : Nat) (hk : k < l.length) :
(l[k]!).length ≤ ((l.take (k + 1)).map (fun s => s.length)).sum := by
induction l generalizing k with
| nil => simp at hk
| cons a as ih =>
cases k with
| zero => simp
| succ m =>
have hm : m < as.length := by simp at hk; omega
have hih := ih m hm
have hget : (a :: as)[m + 1]! = as[m]! := by simp
have htake : (a :: as).take (m + 2) = a :: as.take (m + 1) := rfl
rw [hget, htake]
simp only [List.map_cons, List.sum_cons]
omega
privatetheorem join_len (strings : List String) :
(String.join strings).toList.length = (strings.map (fun s => s.length)).sum := by
induction strings with
| nil => simp [String.join]
| cons s tl ih =>
rw [toList_join_cons, List.length_append, ih]
simp
privatetheorem join_slice (strings : List String) (i : Nat) (hi : i < strings.length) :
(((String.join strings).toList.take ((strings.take (i + 1)).map (fun s => s.length)).sum).drop
(((strings.take (i + 1)).map (fun s => s.length)).sum - (strings[i]!).length)).asString =
strings[i]! := by
induction strings generalizing i with
| nil => simp at hi
| cons s tl ih =>
cases i with
| zero =>
rw [toList_join_cons]
have hget : (s :: tl)[0]! = s := by simp
have htake : (s :: tl).take 1 = [s] := rfl
have hlen : s.toList.length = s.length := by simp
rw [hget, htake]
simp only [List.map_cons, List.map_nil, List.sum_cons, List.sum_nil,
Nat.add_zero, Nat.sub_self, List.drop_zero]
rw [← hlen, take_append_len]
simp [List.asString]
| succ k =>
have hk : k < tl.length := by simp at hi; omega
have hle := elem_len_le_take_sum tl k hk
have hget : (s :: tl)[k + 1]! = tl[k]! := by simp
have htake : (s :: tl).take (k + 1 + 1) = s :: tl.take (k + 1) := rfl
have hlen : s.toList.length = s.length := by simp
rw [toList_join_cons, hget, htake]
simp only [List.map_cons, List.sum_cons]
have hsub : s.length + ((tl.take (k + 1)).map (fun s => s.length)).sum - (tl[k]!).length =
s.length + (((tl.take (k + 1)).map (fun s => s.length)).sum - (tl[k]!).length) := by
omega
rw [hsub, ← hlen, take_append_add, drop_append_add]
exact ih k hk
theorem solution : type_of% @correctness := by
intro strings
unfold problem_spec
refine ⟨implementation strings, rfl, ?_⟩
dsimp [implementation]
exact ⟨join_len strings, fun i hi => join_slice strings i hi⟩
end Submission
Check it yourself
Project
clever
Commit
8348039a7ff7730a126d761e71d0439735eeb3e2
Lean
leanprover/lean4:v4.27.0
Mathlib
a3a10db0e9d66acbebf76c5e6a135066525ac900
With Git and Elan installed, put the download in a fresh working directory and run this there. Use a disposable environment. Lean prints the axioms at the end. The checker allows propext, Classical.choice, and Quot.sound.
(
set -eu
proof="$PWD/abf80084-cbaa-46b3-8236-d649745228b5.lean"
git clone https://github.com/trishullab/clever.git clever
git -C clever checkout --detach 8348039a7ff7730a126d761e71d0439735eeb3e2
cd clever/src/lean4
elan toolchain install leanprover/lean4:v4.27.0
lake +leanprover/lean4:v4.27.0 exe cache get
lake +leanprover/lean4:v4.27.0 build +Imports.AllImports
mkdir -p '.lake/build/lib/lean/human_eval'
lake +leanprover/lean4:v4.27.0 env lean -DautoImplicit=false --root=. -o '.lake/build/lib/lean/human_eval/problem_28.olean' 'human_eval/problem_28.lean'
check_dir="$(mktemp -d)"
trap 'rm -f "$check_dir/Check.lean"; rmdir "$check_dir"' EXIT
printf "%s\n" "import Lean" > "$check_dir/Check.lean"
cat "$proof" >> "$check_dir/Check.lean"
cat >> "$check_dir/Check.lean" <<'SOLVE_CHECK'
example : type_of% @_root_.correctness := @_root_.Submission.solution
#print axioms Submission.solution
SOLVE_CHECK
lake +leanprover/lean4:v4.27.0 env lean "$check_dir/Check.lean"
)
Download the receipt verifier and public key. Run python3 verify-proof.py certificate.json abf80084-cbaa-46b3-8236-d649745228b5.lean proof-key.pem with OpenSSL 3.
Show full file and definitions
import Imports.AllImports
-- start_def problem_details
/--
function_signature: "def concatenate(strings: List[str]) -> str"
docstring: |
Concatenate list of strings into a single string
test_cases:
- input: []
expected_output: ""
- input: ["a", "b", "c"]
expected_output: "abc"
-/
-- end_def problem_details-- start_def problem_specdef problem_spec
-- function signature
(implementation: List String → String)
-- inputs
(strings: List String) :=
-- spec
let spec (result: String) :=
let result_chars := result.toList;
result_chars.length = (strings.map (λ s => s.length)).sum ∧
∀ i, i < strings.length →
(let string_in_result := strings[i]!;
let end_idx := ((strings.take (i + 1)).map (λ s => s.length)).sum;
let start_idx := end_idx - string_in_result.length;
let corresponding_string_in_result := ((result_chars.take end_idx).drop start_idx).asString;
corresponding_string_in_result = string_in_result);
-- program termination
∃ result, implementation strings = result ∧
spec result
-- end_def problem_spec-- start_def generated_specdef generated_spec
-- function signature
(implementation: List String → String)
-- inputs
(strings: List String) : Prop :=
-- end_def generated_spec-- start_def generated_spec_body
sorry
-- end_def generated_spec_body-- start_def spec_isomorphismtheorem spec_isomorphism:
∀ implementation,
(∀ strings, problem_spec implementation strings) ↔
(∀ strings, generated_spec implementation strings) :=
-- end_def spec_isomorphism-- start_def spec_isomorphism_proof
sorry
-- end_def spec_isomorphism_proof-- start_def implementation_signaturedef implementation (strings: List String) : String :=
-- end_def implementation_signature-- start_def implementation
String.join strings
-- end_def implementation-- Uncomment the following test cases after implementing the function-- start_def test_cases
#test implementation [] = ""
#test implementation ["a", "b", "c"] = "abc"
-- end_def test_cases-- start_def correctness_definitiontheorem correctness
(strings: List String)
: problem_spec implementation strings
:=
-- end_def correctness_definition-- start_def correctness_proofby
unfold problem_spec
let result := implementation strings
use result
simp [result]
apply And.intro
simp [implementation]
repeat sorry
-- end_def correctness_proof