File size: 5,586 Bytes
e5a97c6 | 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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | import OAI.Combinatorics.LowSpaceSubsetSum.Streams.MergeCorrectness
namespace OAI
/-!
# Sortedness of complete and stopped row merges
Only each row's individual order is required. The tournament minimum is
no larger than every remaining occurrence; replacing its front by its
successor preserves this invariant. Every finite trace is therefore sorted,
including traces stopped by the operation cap.
-/
namespace LowSpaceSubsetSum
namespace RowMerge
universe u v
variable {α : Type u} {σ : Type v} {d : ℕ}
def RowsSorted (key : α → ℤ) (view : σ → List α)
(t : Tournament.Tree (Front d α σ) d) : Prop :=
∀ p, (remaining view t p).Pairwise (fun x y => key x ≤ key y)
theorem replacement_remaining_subset (key : α → ℤ) (read : σ → Option (α × σ))
(view : σ → List α) (hread : IsReader read view)
(t : Tournament.Tree (Front d α σ) d)
(ht : Tournament.Valid (fun y => key y.value) t) (halign : Aligned t)
{x : Front d α σ} (hx : Tournament.winner t = some x) (p : Path d) :
remaining view (Tournament.replace (fun y => key y.value) t x.row
(front read x.row x.after)) p ⊆ remaining view t p := by
have hnew := replace_remaining key read view hread t x p
have hrow := winner_at_own_row key t ht halign hx
rw [hnew]
by_cases hp : x.row = p
· subst p
rw [ite_eq_left rfl]
simp only [remaining, hrow]
intro y hy
exact List.mem_cons_of_mem _ hy
· rw [ite_eq_right hp]
theorem replacement_rowsSorted (key : α → ℤ) (read : σ → Option (α × σ))
(view : σ → List α) (hread : IsReader read view)
(t : Tournament.Tree (Front d α σ) d)
(ht : Tournament.Valid (fun y => key y.value) t) (halign : Aligned t)
(hsort : RowsSorted key view t) {x : Front d α σ}
(hx : Tournament.winner t = some x) :
RowsSorted key view (Tournament.replace (fun y => key y.value) t x.row
(front read x.row x.after)) := by
intro p
rw [replace_remaining key read view hread t x p]
by_cases hp : x.row = p
· subst p
rw [ite_eq_left rfl]
have hs := hsort x.row
have hrow := winner_at_own_row key t ht halign hx
simp only [remaining, hrow] at hs
exact (List.pairwise_cons.mp hs).2
· rw [ite_eq_right hp]
exact hsort p
theorem winner_le_remaining (key : α → ℤ) (view : σ → List α)
(t : Tournament.Tree (Front d α σ) d)
(ht : Tournament.Valid (fun y => key y.value) t) (hsort : RowsSorted key view t)
{x : Front d α σ} (hx : Tournament.winner t = some x)
(p : Path d) {y : α} (hy : y ∈ remaining view t p) : key x.value ≤ key y := by
cases hp : Tournament.lookup t p with
| none => simp [remaining, hp] at hy
| some z =>
have hmin := winner_is_minimum key t ht hx p hp
have hs := hsort p
simp only [remaining, hp] at hy hs
rcases List.mem_cons.mp hy with hy | hy
· simpa [hy] using hmin
· exact hmin.trans ((List.pairwise_cons.mp hs).1 y hy)
theorem trace_lower_bound (key : α → ℤ) (read : σ → Option (α × σ))
(view : σ → List α) (hread : IsReader read view) (k : ℕ)
(t : Tournament.Tree (Front d α σ) d)
(ht : Tournament.Valid (fun y => key y.value) t) (halign : Aligned t)
(c : ℤ) (hc : ∀ p, ∀ y ∈ remaining view t p, c ≤ key y) :
∀ y ∈ trace key read k t, c ≤ key y := by
induction k generalizing t with
| zero => simp [trace]
| succ k ih =>
cases hw : Tournament.winner t with
| none => simp [trace, advance, hw]
| some x =>
let t' := Tournament.replace (fun y => key y.value) t x.row (front read x.row x.after)
have ha : advance key read t = some (x.value, t') := by simp [advance, hw, t']
have ht' := advance_valid key read t ht ha
have halign' := advance_aligned key read t halign ha
have hrow := winner_at_own_row key t ht halign hw
have hfront : c ≤ key x.value := hc x.row x.value (by simp [remaining, hrow])
have hc' : ∀ p, ∀ y ∈ remaining view t' p, c ≤ key y := by
intro p y hy
exact hc p y (replacement_remaining_subset key read view hread t ht halign hw p hy)
rw [trace, ha]
intro y hy
rcases List.mem_cons.mp hy with hy | hy
· simpa [hy] using hfront
· exact ih t' ht' halign' hc' y hy
theorem trace_sorted (key : α → ℤ) (read : σ → Option (α × σ))
(view : σ → List α) (hread : IsReader read view) (k : ℕ)
(t : Tournament.Tree (Front d α σ) d)
(ht : Tournament.Valid (fun y => key y.value) t) (halign : Aligned t)
(hsort : RowsSorted key view t) :
(trace key read k t).Pairwise (fun x y => key x ≤ key y) := by
induction k generalizing t with
| zero => simp [trace]
| succ k ih =>
cases hw : Tournament.winner t with
| none => simp [trace, advance, hw]
| some x =>
let t' := Tournament.replace (fun y => key y.value) t x.row (front read x.row x.after)
have ha : advance key read t = some (x.value, t') := by simp [advance, hw, t']
have ht' := advance_valid key read t ht ha
have halign' := advance_aligned key read t halign ha
have hsort' := replacement_rowsSorted key read view hread t ht halign hsort hw
rw [trace, ha]
apply List.pairwise_cons.mpr
refine ⟨?_, ih t' ht' halign' hsort'⟩
apply trace_lower_bound key read view hread k t' ht' halign' (key x.value)
intro p y hy
exact winner_le_remaining key view t ht hsort hw p
(replacement_remaining_subset key read view hread t ht halign hw p hy)
end RowMerge
end LowSpaceSubsetSum
end OAI
|