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
36import datasets.verina.verina_basic_83.task
namespace Submission
privatetheorem loop_correct (a b : Array Int) (i : Nat) (c : Array Int)
(hc : c.size = a.size + b.size)
(hp : ∀ j, j < i → j < (a.size + b.size) → c[j]! = (if j < a.size then a[j]! else b[j - a.size]!)) :
(concat.loop a b (a.size + b.size) i c).size = a.size + b.size ∧
∀ j, j < (a.size + b.size) → (concat.loop a b (a.size + b.size) i c)[j]! = (if j < a.size then a[j]! else b[j - a.size]!) := by
rw [concat.loop]
by_cases hi : i < (a.size + b.size)
· simp only [hi, if_true]
apply loop_correct
· simpa using hc
· intro j hj hja
have hjc : j < c.size := by omega
by_cases h : j = i
· subst j
simp [getElem!_pos, hjc, hi]
· have hji : j < i := by omega
simpa [getElem!_pos, hjc, h, Ne.symm h, hi] using hp j hji hja
· simp only [hi, if_false]
exact ⟨hc, fun j hj => hp j (by omega) hj⟩
termination_by (a.size + b.size) - i
decreasing_by omega
theorem solution : type_of% @concat_spec_satisfied := by
intro a b h
obtain ⟨hs, hp⟩ := loop_correct a b 0 (Array.mkArray (a.size + b.size) 0) (by simp)
(fun j hj => False.elim (Nat.not_lt_zero j hj))
refine ⟨hs, ?_, ?_⟩
· intro j hj
simpa [hj] using hp j (by omega)
· intro j hj
have hn : ¬ j + a.size < a.size := by omega
simpa [hn] using hp (j + a.size) (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 59d236dd-a1de-499b-b9a0-250441d810a7.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 concat_precond (a : Array Int) (b : Array Int) : Prop :=
-- !benchmark @start precond
True
-- !benchmark @end precond-- !benchmark @start code_aux-- !benchmark @end code_auxdef concat (a : Array Int) (b : Array Int) (h_precond : concat_precond (a) (b)) : Array Int :=
-- !benchmark @start code
let n := a.size + b.size
let rec loop (i : Nat) (c : Array Int) : Array Int :=
if i < n then
let value := if i < a.size then a[i]! else b[i - a.size]!
loop (i + 1) (c.set! i value)
else
c
loop 0 (Array.mkArray n 0)
-- !benchmark @end code-- !benchmark @start postcond_aux-- !benchmark @end postcond_aux
@[reducible, simp]
def concat_postcond (a : Array Int) (b : Array Int) (result: Array Int) (h_precond : concat_precond (a) (b)) :=
-- !benchmark @start postcond
result.size = a.size + b.size
∧ (∀ k, k < a.size → result[k]! = a[k]!)
∧ (∀ k, k < b.size → result[k + a.size]! = b[k]!)
-- !benchmark @end postcond-- !benchmark @start proof_aux-- !benchmark @end proof_auxtheorem concat_spec_satisfied (a: Array Int) (b: Array Int) (h_precond : concat_precond (a) (b)) :
concat_postcond (a) (b) (concat (a) (b) h_precond) h_precond := by-- !benchmark @start proof
sorry
-- !benchmark @end proof