Direct use. This proves a standard linear search returns the earliest position holding the key, or minus one exactly when the key is missing. Reliable software.
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
70import datasets.verina.verina_basic_62.task
namespace Submission
theorem search_spec (a : Array Int) (key : Int) :
∀ (n index : Nat), a.size - index ≤ n →
(Find.search a key index = -1 ∧ ∀ i : Nat, index ≤ i → i < a.size → a[i]! ≠ key) ∨
(∃ j : Nat, Find.search a key index = (j : Int) ∧ index ≤ j ∧ j < a.size ∧ a[j]! = key ∧
∀ i : Nat, index ≤ i → i < j → a[i]! ≠ key) := by
intro n
induction n with
| zero =>
intro index hle
have hS : Find.search a key index = -1 := by
conv =>
lhs
rw [Find.search]
rw [if_neg (show ¬ (index < a.size) by omega)]
exact Or.inl ⟨hS, fun i h1 h2 => absurd h2 (by omega)⟩
| succ n ih =>
intro index hle
by_cases hlt : index < a.size
· by_cases h : a[index]! = key
· have hS : Find.search a key index = (index : Int) := by
conv =>
lhs
rw [Find.search]
rw [if_pos hlt, if_pos h]
rfl
exact Or.inr ⟨index, hS, Nat.le_refl _, hlt, h, fun i h1 h2 => absurd h2 (by omega)⟩
· have hS : Find.search a key index = Find.search a key (index + 1) := by
conv =>
lhs
rw [Find.search]
rw [if_pos hlt, if_neg h]
rw [hS]
rcases ih (index + 1) (by omega) with ⟨hn, hall⟩ | ⟨j, hj, hj1, hj2, hj3, hj4⟩
· exact Or.inl ⟨hn, fun i h1 h2 => by
by_cases hi : i = index
· subst hi
exact h
· exact hall i (by omega) h2⟩
· exact Or.inr ⟨j, hj, by omega, hj2, hj3, fun i h1 h2 => by
by_cases hi : i = index
· subst hi
exact h
· exact hj4 i (by omega) h2⟩
· have hS : Find.search a key index = -1 := by
conv =>
lhs
rw [Find.search]
rw [if_neg hlt]
exact Or.inl ⟨hS, fun i h1 h2 => absurd h2 (by omega)⟩
theorem solution : type_of% @Find_spec_satisfied := by
intro a key h_precond
have hF : Find a key h_precond = Find.search a key 0 := rfl
rw [hF]
unfold Find_postcond
rcases search_spec a key a.size 0 (Nat.sub_le a.size 0) with
⟨hS, hall⟩ | ⟨j, hj, hj1, hj2, hj3, hj4⟩
· rw [hS]
exact ⟨Or.inl rfl, fun h => (h rfl).elim, fun _ i hi => hall i (Nat.zero_le i) hi⟩
· rw [hj]
have hm : (j : Int).toNat = j := by simp
rw [hm]
exact ⟨Or.inr ⟨by omega, (by show (j : Int) < (a.size : Int); omega)⟩,
fun _ => ⟨hj3, fun i hi => hj4 i (Nat.zero_le i) hi⟩,
fun h => absurd h (by omega)⟩
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 ea95a261-dff1-4421-8e64-be815e877e59.lean proof-key.pem with OpenSSL 3.