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
41import datasets.verina.verina_basic_74.task
namespace Submission
privatetheorem scan (a : Array Int) (i : Nat) (c : Int)
(hp : ∀ j, j < i → j < a.size → a[j]! ≤ c)
(hw : ∃ j, j < a.size ∧ c = a[j]!) :
let r := maxArray_aux a i c
(∀ j, j < a.size → a[j]! ≤ r) ∧ (∃ j, j < a.size ∧ r = a[j]!) := by
dsimp only
rw [maxArray_aux]
by_cases hi : i < a.size
· rw [if_pos hi]
let next := if c > a[i]! then c else a[i]!
have hb : ∀ j, j < i + 1 → j < a.size → a[j]! ≤ next := by
intro j hj hja
by_cases he : j = i
· subst j
dsimp [next]
split <;> omega
· have hh := hp j (by omega) hja
dsimp [next]
split <;> omega
have he : ∃ j, j < a.size ∧ next = a[j]! := by
dsimp [next]
split
· exact hw
· exact ⟨i, hi, rfl⟩
exact scan a (i + 1) next hb he
· rw [if_neg hi]
exact ⟨fun j hj => hp j (by omega) hj, hw⟩
termination_by a.size - i
decreasing_by omega
theorem solution : type_of% @maxArray_spec_satisfied := by
intro a h
have hn : 0 < a.size := h
exact scan a 1 a[0]! (by
intro j hj _
have he : j = 0 := by omega
subst j
exact Int.le_refl _) ⟨0, hn, rfl⟩
end Submission
Check it yourself
Project
verina
Commit
66b3ce5014edafb20cb26b87c681e7f31847249f
Lean
leanprover/lean4:v4.18.0
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.
Download the receipt verifier and public key. Run python3 verify-proof.py certificate.json 06e3cfb5-37cb-4cab-902f-6e9c7b1dc2c1.lean proof-key.pem with OpenSSL 3.
Show full file and definitions
-- !benchmark @start import type=solution-- !benchmark @end import-- !benchmark @start solution_aux-- !benchmark @end solution_aux-- !benchmark @start precond_aux-- !benchmark @end precond_aux
@[reducible, simp]
def maxArray_precond (a : Array Int) : Prop :=
-- !benchmark @start precond
a.size > 0
-- !benchmark @end precond-- !benchmark @start code_auxdef maxArray_aux (a : Array Int) (index : Nat) (current : Int) : Int :=
if index < a.size then
let new_current := if current > a[index]! then current else a[index]!
maxArray_aux a (index + 1) new_current
else
current
-- !benchmark @end code_auxdef maxArray (a : Array Int) (h_precond : maxArray_precond (a)) : Int :=
-- !benchmark @start code
maxArray_aux a 1 a[0]!
-- !benchmark @end code-- !benchmark @start postcond_aux-- !benchmark @end postcond_aux
@[reducible, simp]
def maxArray_postcond (a : Array Int) (result: Int) (h_precond : maxArray_precond (a)) :=
-- !benchmark @start postcond
(∀ (k : Nat), k < a.size → result >= a[k]!) ∧ (∃ (k : Nat), k < a.size ∧ result = a[k]!)
-- !benchmark @end postcond-- !benchmark @start proof_aux-- !benchmark @end proof_auxtheorem maxArray_spec_satisfied (a: Array Int) (h_precond : maxArray_precond (a)) :
maxArray_postcond (a) (maxArray (a) h_precond) h_precond := by-- !benchmark @start proof
sorry
-- !benchmark @end proof