Skip to content

Commit bc9dc70

Browse files
committed
proof simplifications
1 parent 940172e commit bc9dc70

File tree

1 file changed

+21
-30
lines changed

1 file changed

+21
-30
lines changed

Batteries/Data/BinaryHeap/Lemmas.lean

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ theorem maxChild_ge_right [Ord α] [Std.OrientedOrd α] (a : Vector α sz)
4444
/-- heapifyDown doesn't modify positions before the starting index. -/
4545
theorem heapifyDown_preserves_prefix [Ord α] (a : Vector α sz) (i k : Fin sz) (hk : k < ↑i) :
4646
(heapifyDown a i)[k] = a[k] := by
47-
induction a, i using heapifyDown.induct <;> grind [heapifyDown]
47+
induction a, i using heapifyDown.induct
48+
<;> grind only [heapifyDown, = Fin.getElem_fin, = Vector.getElem_swap]
4849

4950

5051
section heapifyDown
@@ -117,11 +118,7 @@ theorem heapifyDown_preserves_wf_children_outside [Ord α] {v : Vector α sz} {i
117118
(hleft_ne : 2 * k.val + 1 ≠ i.val) (hright_ne : 2 * k.val + 2 ≠ i.val) :
118119
WF.children (heapifyDown v i) k :=
119120
heapifyDown_preserves_wf_children_of_not_inSubtree
120-
(InSubtree.not_of_lt hki)
121-
rfl
122-
(fun _ => rfl)
123-
(fun _ => rfl)
124-
hwf
121+
(InSubtree.not_of_lt hki) rfl (fun _ => rfl) (fun _ => rfl) hwf
125122

126123
/-- If v dominates all values in the subtree, v dominates the result at root -/
127124
theorem heapifyDown_root_bounded [Ord α] [Std.TransOrd α]
@@ -265,15 +262,15 @@ theorem heapifyUp_wf_bottomUp [Ord α] [Std.TransOrd α] [Std.OrientedOrd α]
265262
(by grind only [Ordering.isLT, Ordering.isGE])
266263

267264
theorem heapifyUp_wf [Ord α] [Std.TransOrd α] [Std.OrientedOrd α]
268-
(a : Vector α sz) (i : Fin sz) (hexcept : WF.exceptAt a i) (hchildren : WF.childLeParent a i) :
265+
{a : Vector α sz} {i : Fin sz} (hexcept : WF.exceptAt a i) (hchildren : WF.childLeParent a i) :
269266
WF.topDown (heapifyUp a i) := by
270267
rw [WF.iff_bottomUp]
271268
simp_all [heapifyUp_wf_bottomUp]
272269

273270
end heapifyUp
274271

275272
theorem mkHeap.loop_wf [Ord α] [Std.TransOrd α] [Std.OrientedOrd α]
276-
(n : Nat) (a : Vector α sz) (h : n ≤ sz)
273+
{n : Nat} {a : Vector α sz} {h : n ≤ sz}
277274
(hinv : ∀ k : Fin sz, n ≤ k.val → WF.children a k) :
278275
∀ k : Fin sz, WF.children (mkHeap.loop n a h) k := by
279276
induction n generalizing a with
@@ -287,11 +284,11 @@ theorem mkHeap.loop_wf [Ord α] [Std.TransOrd α] [Std.OrientedOrd α]
287284
· grind only
288285
· have hlt : i < k := by omega
289286
exact hwf_below k hlt
290-
exact ih _ _ hinv'
287+
exact ih hinv'
291288

292289
public section
293290

294-
theorem mkHeap_wf [Ord α] [Std.TransOrd α] [Std.OrientedOrd α] (a : Vector α sz) :
291+
theorem mkHeap_wf [Ord α] [Std.TransOrd α] [Std.OrientedOrd α] {a : Vector α sz} :
295292
WF.topDown (mkHeap a) := by
296293
unfold mkHeap
297294
apply mkHeap.loop_wf
@@ -302,9 +299,8 @@ theorem mkHeap_wf [Ord α] [Std.TransOrd α] [Std.OrientedOrd α] (a : Vector α
302299
exfalso
303300
omega
304301

305-
theorem insert_wf [Ord α] [Std.TransOrd α] [Std.OrientedOrd α] (self : BinaryHeap α)
306-
(x : α) (h_wf : self.WF) :
307-
(self.insert x).WF := by
302+
theorem insert_wf [Ord α] [Std.TransOrd α] [Std.OrientedOrd α] {self : BinaryHeap α}
303+
{x : α} (h_wf : self.WF) : (self.insert x).WF := by
308304
unfold insert
309305
have h_sz : self.vector.size < (self.vector.push x).size := by grind only [size_insert]
310306
have h_ea : WF.exceptAt (self.vector.push x) ⟨self.vector.size, h_sz⟩ := by
@@ -323,62 +319,57 @@ theorem insert_wf [Ord α] [Std.TransOrd α] [Std.OrientedOrd α] (self : Binary
323319
grind only [vector, size]
324320

325321
/-- Correctness of max: it returns an element ≥ all elements in the heap -/
326-
theorem max_ge_all [Ord α] [Std.TransOrd α] {self : BinaryHeap α} {hwf : WF self} {h : self.size > 0} :
327-
∃ x, self.max = some x ∧ ∀ i : Fin self.size, (compare x ((self.get i))).isGE :=
322+
theorem max_ge_all [Ord α] [Std.TransOrd α]
323+
{self : BinaryHeap α} (hwf : WF self) (h : self.size > 0) :
324+
∃ x, self.max = some x ∧ ∀ i : Fin self.size, (compare x (self.get i)).isGE :=
328325
⟨self.arr[0], by simp [max], (WF.max_ge_all hwf h ·)⟩
329326

330327
theorem max_eq_none_iff {self : BinaryHeap α} : self.max = none ↔ self.size = 0 := by
331328
simp [max, size]
332329

333330

334331
theorem popMax_wf [Ord α] [Std.TransOrd α] [Std.OrientedOrd α]
335-
{self : BinaryHeap α} {h_wf : WF self} :
332+
{self : BinaryHeap α} (h_wf : WF self) :
336333
WF (self.popMax) := by
337334
unfold popMax
338-
generalize hv : self.vector = v
339-
have htd : WF.topDown v := by simp_all [WF]
335+
have htd : WF.topDown self.vector := by simp_all [WF]
340336
simp only
341337
split
342338
. simp_all [WF]
343339
. split <;> apply WF.topDown_toArray
344-
. have hbelow : WF.below (v.swap 0 (v.size - 1) |>.pop) 0 := by
340+
. have hbelow : WF.below (self.vector.swap 0 (self.size - 1) |>.pop) 0 := by
345341
grind only [WF.below_swap_pop htd]
346342
simp_all [WF.topDown_iff_at_below_zero.mp, heapifyDown_wf (i := ⟨0, by omega⟩) hbelow]
347343
. grind only [WF.children, WF.topDown]
348344

349345
theorem decreaseKey_wf [Ord α] [Std.TransOrd α] [Std.OrientedOrd α] {self : BinaryHeap α}
350-
{i : Fin self.size} {x : α} {h_leq : compare x (self.get i) |>.isLE} {h_wf : WF self} :
346+
{i : Fin self.size} (h_wf : WF self) (h_leq : compare x (self.get i) |>.isLE) :
351347
WF (self.decreaseKey i x) := by
352348
unfold decreaseKey
353349

354-
generalize hv : self.vector = v
355-
have htd : WF.topDown v := by simp_all [WF]
356-
have h_leq' : compare x v[i] |>.isLE := by
357-
have : self.size = self.arr.size := by simp [size]
358-
simp_all [get, ← hv, vector]
359-
360350
apply WF.topDown_toArray
351+
have htd : WF.topDown self.vector := by simp_all [WF]
361352

362-
have hbelow : WF.below (v.set i x) i := WF.set_smaller_wf_below htd
353+
have hbelow : WF.below (self.vector.set i x) i := WF.set_smaller_wf_below htd
363354
have ⟨hchildren_i, hbelow_i⟩ := heapifyDown_wf hbelow
364355

365356
intro k
366357
rcases Nat.lt_trichotomy k.val i.val with hki | hki_eq | hik
367358
· by_cases hk_parent : k.val = (i.val - 1) / 20 < i.val
368359
· have hk_eq : k = ⟨(i.val - 1) / 2, by omega⟩ := by ext; exact hk_parent.1
369360
rw [hk_eq]
370-
exact heapifyDown_preserves_wf_parent htd h_leq' hk_parent.2
361+
exact heapifyDown_preserves_wf_parent htd h_leq hk_parent.2
371362
· have hleft_ne : 2 * k.val + 1 ≠ i.val := by omega
372363
have hright_ne : 2 * k.val + 2 ≠ i.val := by omega
373-
have hwf_set : WF.children (v.set i x) k :=
364+
have hwf_set : WF.children (self.vector.set i x) k :=
374365
WF.set_preserves_wf_children_of_ne (htd k) (by omega) hleft_ne hright_ne
375366
exact heapifyDown_preserves_wf_children_outside hki hwf_set hleft_ne hright_ne
376367
· exact Fin.ext hki_eq ▸ hchildren_i
377368
· exact hbelow_i k hik
378369
end
379370

380371
theorem increaseKey_wf [Ord α] [Std.TransOrd α] [Std.OrientedOrd α] {self : BinaryHeap α}
381-
{i : Fin self.size} {x : α} {h_ge : compare x (self.get i) |>.isGE} {h_wf : WF self} :
372+
{i : Fin self.size} (h_wf : WF self) (h_ge : compare x (self.get i) |>.isGE) :
382373
WF (self.increaseKey i x) := by
383374
unfold increaseKey
384375
generalize hv : self.vector = v

0 commit comments

Comments
 (0)