File size: 11,365 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | import OAI.Combinatorics.LowSpaceSubsetSum.Streams.BufferReplay
namespace OAI
/-!
# Reporting one item's matches against a buffered tie group
The immutable prefix buffer is reused. A generator suffix is restored only
when the group was longer than the buffer. The callback may stop in either
part; its work and stream queries are bounded on every outcome.
-/
namespace LowSpaceSubsetSum
namespace JoinOne
universe u v w
variable {α : Type u} {σ : Type v} {τ : Type w}
def run (read : σ → Option (α × σ)) (key : α → ℤ) (K : ℤ)
(report : α → τ → τ × Bool) (buffer : Array α) (saved : σ)
(long : Bool) (fuel : ℕ) (a : τ) : TieReplay.Result σ τ :=
let first := BufferReplay.run key K report buffer a
if first.halted then
⟨saved, first.state, first.queries, first.reports, false, true⟩
else if long then
let rest := TieReplay.run read key K report fuel saved first.state
⟨rest.cursor, rest.state, first.queries + rest.queries,
first.reports + rest.reports, rest.complete, rest.halted⟩
else ⟨saved, first.state, first.queries, first.reports, true, false⟩
theorem reports_le (read : σ → Option (α × σ)) (key : α → ℤ) (K : ℤ)
(report : α → τ → τ × Bool) (view : σ → List α) (hread : RowMerge.IsReader read view)
(buffer : Array α) (saved : σ) (long : Bool) (fuel : ℕ) (a : τ) :
(run read key K report buffer saved long fuel a).reports ≤
buffer.size + (TieBuffer.groupView key K view saved).length := by
have hb := BufferReplay.reports_le key K report buffer a
dsimp only [run]
split
· exact hb.trans (Nat.le_add_right _ _)
· split
· have hr := TieReplay.run_reports_le_potential read key K report view hread
fuel saved (BufferReplay.run key K report buffer a).state
exact Nat.add_le_add hb hr
· exact hb.trans (Nat.le_add_right _ _)
theorem queries_le (read : σ → Option (α × σ)) (key : α → ℤ) (K : ℤ)
(report : α → τ → τ × Bool) (view : σ → List α) (hread : RowMerge.IsReader read view)
(buffer : Array α) (saved : σ) (long : Bool) (fuel : ℕ) (a : τ) :
(run read key K report buffer saved long fuel a).queries ≤
buffer.size + (TieBuffer.groupView key K view saved).length + 1 := by
have hb := BufferReplay.queries_le key K report buffer a
dsimp only [run]
split
· dsimp only
omega
· split
· have hr := TieReplay.run_queries_le_potential read key K report view hread
fuel saved (BufferReplay.run key K report buffer a).state
dsimp only
omega
· dsimp only
omega
theorem halted_false (read : σ → Option (α × σ)) (key : α → ℤ) (K : ℤ)
(report : α → τ → τ × Bool) (hreport : ∀ x a, (report x a).2 = false)
(buffer : Array α) (saved : σ) (long : Bool) (fuel : ℕ) (a : τ) :
(run read key K report buffer saved long fuel a).halted = false := by
have hb : (BufferReplay.run key K report buffer a).halted = false :=
TieReplay.run_halted_false _ _ _ _ hreport _ _ _
simp only [run, hb, Bool.false_eq_true, ite_false]
split
· exact TieReplay.run_halted_false _ _ _ _ hreport _ _ _
· rfl
/-- Completed reporting is precisely prefix reporting followed by suffix
reporting; a short group has no suffix to restore. -/
theorem state_complete (read : σ → Option (α × σ)) (key : α → ℤ) (K : ℤ)
(report : α → τ → τ × Bool) (hreport : ∀ x a, (report x a).2 = false)
(view : σ → List α) (hread : RowMerge.IsReader read view)
(buffer : Array α) (hkeys : ∀ x ∈ buffer.toList, key x = K)
(saved : σ) (fuel : ℕ) (a : τ)
(hfuel : (TieBuffer.groupView key K view saved).length ≤ fuel) :
(run read key K report buffer saved (TieBuffer.hasKey read key K saved) fuel a).state =
(buffer.toList ++ TieBuffer.groupView key K view saved).foldl
(fun a x => (report x a).1) a := by
have hb : (BufferReplay.run key K report buffer a).halted = false :=
TieReplay.run_halted_false _ _ _ _ hreport _ _ _
have hs := BufferReplay.state_complete key K report hreport buffer a hkeys
simp only [run, hb, Bool.false_eq_true, ite_false]
by_cases hl : TieBuffer.hasKey read key K saved = true
· simp only [hl, ite_true]
rw [TieReplay.run_state_complete read key K report hreport view hread fuel saved _ hfuel,
hs, List.foldl_append]
· have he : TieBuffer.groupView key K view saved = [] := by
by_contra he
exact hl ((TieBuffer.hasKey_iff read key K view hread saved).mpr he)
have hfalse : TieBuffer.hasKey read key K saved = false := Bool.eq_false_iff.mpr hl
simp only [hfalse, Bool.false_eq_true, ite_false, he, List.append_nil]
exact hs
theorem suffix_empty (read : σ → Option (α × σ)) (key : α → ℤ) (K : ℤ)
(report : α → τ → τ × Bool) (hreport : ∀ x a, (report x a).2 = false)
(view : σ → List α) (hread : RowMerge.IsReader read view)
(buffer : Array α) (saved : σ) (fuel : ℕ) (a : τ)
(hfuel : (TieBuffer.groupView key K view saved).length ≤ fuel) :
TieBuffer.groupView key K view
(run read key K report buffer saved (TieBuffer.hasKey read key K saved) fuel a).cursor = [] := by
have hb : (BufferReplay.run key K report buffer a).halted = false :=
TieReplay.run_halted_false _ _ _ _ hreport _ _ _
simp only [run, hb, Bool.false_eq_true, ite_false]
by_cases hl : TieBuffer.hasKey read key K saved = true
· simp only [hl, ite_true]
have h := (TieReplay.runAcc_nonhalting_spec read key K report hreport view hread
fuel saved (BufferReplay.run key K report buffer a).state 0 0).2.2.1
exact h.trans (List.drop_eq_nil_of_le hfuel)
· simp only [Bool.eq_false_iff.mpr hl, Bool.false_eq_true, ite_false]
by_contra he
exact hl ((TieBuffer.hasKey_iff read key K view hread saved).mpr he)
theorem view_complete (read : σ → Option (α × σ)) (key : α → ℤ) (K : ℤ)
(report : α → τ → τ × Bool) (hreport : ∀ x a, (report x a).2 = false)
(view : σ → List α) (hread : RowMerge.IsReader read view)
(buffer : Array α) (saved : σ) (fuel : ℕ) (a : τ)
(hfuel : (TieBuffer.groupView key K view saved).length ≤ fuel) :
view saved = TieBuffer.groupView key K view saved ++
view (run read key K report buffer saved (TieBuffer.hasKey read key K saved) fuel a).cursor := by
have hb : (BufferReplay.run key K report buffer a).halted = false :=
TieReplay.run_halted_false _ _ _ _ hreport _ _ _
simp only [run, hb, Bool.false_eq_true, ite_false]
by_cases hl : TieBuffer.hasKey read key K saved = true
· simp only [hl, ite_true]
have h := (TieReplay.runAcc_nonhalting_spec read key K report hreport view hread
fuel saved (BufferReplay.run key K report buffer a).state 0 0).2.2.2
simpa only [TieReplay.run, List.take_of_length_le hfuel] using h
· have he : TieBuffer.groupView key K view saved = [] := by
by_contra he
exact hl ((TieBuffer.hasKey_iff read key K view hread saved).mpr he)
simp only [Bool.eq_false_iff.mpr hl, Bool.false_eq_true, ite_false, he, List.nil_append]
def runFilled (read : σ → Option (α × σ)) (key : α → ℤ) (K : ℤ)
(report : α → τ → τ × Bool) (B fuel : ℕ) (s : σ) (a : τ) : TieReplay.Result σ τ :=
let filled := TieBuffer.fill read key K B s
run read key K report filled.buffer filled.cursor
(TieBuffer.hasKey read key K filled.cursor) fuel a
theorem filled_reports_le (read : σ → Option (α × σ)) (key : α → ℤ) (K : ℤ)
(report : α → τ → τ × Bool) (view : σ → List α) (hread : RowMerge.IsReader read view)
(B fuel : ℕ) (s : σ) (a : τ) :
(runFilled read key K report B fuel s a).reports ≤
(TieBuffer.groupView key K view s).length := by
have h := reports_le read key K report view hread (TieBuffer.fill read key K B s).buffer
(TieBuffer.fill read key K B s).cursor
(TieBuffer.hasKey read key K (TieBuffer.fill read key K B s).cursor) fuel a
have hb := congrArg List.length (TieBuffer.fill_buffer read key K view hread B s)
rw [TieBuffer.fill_suffix read key K view hread B s] at h
simp only [Array.length_toList, List.length_take] at hb
simp only [List.length_drop] at h
dsimp only [runFilled]
omega
theorem filled_queries_le (read : σ → Option (α × σ)) (key : α → ℤ) (K : ℤ)
(report : α → τ → τ × Bool) (view : σ → List α) (hread : RowMerge.IsReader read view)
(B fuel : ℕ) (s : σ) (a : τ) :
(runFilled read key K report B fuel s a).queries ≤
(TieBuffer.groupView key K view s).length + 1 := by
have h := queries_le read key K report view hread (TieBuffer.fill read key K B s).buffer
(TieBuffer.fill read key K B s).cursor
(TieBuffer.hasKey read key K (TieBuffer.fill read key K B s).cursor) fuel a
have hb := congrArg List.length (TieBuffer.fill_buffer read key K view hread B s)
rw [TieBuffer.fill_suffix read key K view hread B s] at h
simp only [Array.length_toList, List.length_take] at hb
simp only [List.length_drop] at h
dsimp only [runFilled]
omega
theorem filled_state_complete (read : σ → Option (α × σ)) (key : α → ℤ) (K : ℤ)
(report : α → τ → τ × Bool) (hreport : ∀ x a, (report x a).2 = false)
(view : σ → List α) (hread : RowMerge.IsReader read view)
(B fuel : ℕ) (s : σ) (a : τ)
(hfuel : (TieBuffer.groupView key K view s).length ≤ fuel) :
(runFilled read key K report B fuel s a).state =
(TieBuffer.groupView key K view s).foldl (fun a x => (report x a).1) a := by
have hf : (TieBuffer.groupView key K view (TieBuffer.fill read key K B s).cursor).length ≤
fuel := by
rw [TieBuffer.fill_suffix read key K view hread B s, List.length_drop]
omega
have h := state_complete read key K report hreport view hread
(TieBuffer.fill read key K B s).buffer
(BufferReplay.filled_buffer_keys read key K view hread B s)
(TieBuffer.fill read key K B s).cursor fuel a hf
rw [TieBuffer.fill_buffer read key K view hread B s,
TieBuffer.fill_suffix read key K view hread B s, List.take_append_drop] at h
exact h
theorem filled_view_complete (read : σ → Option (α × σ)) (key : α → ℤ) (K : ℤ)
(report : α → τ → τ × Bool) (hreport : ∀ x a, (report x a).2 = false)
(view : σ → List α) (hread : RowMerge.IsReader read view)
(B fuel : ℕ) (s : σ) (a : τ)
(hfuel : (TieBuffer.groupView key K view s).length ≤ fuel) :
view s = TieBuffer.groupView key K view s ++
view (runFilled read key K report B fuel s a).cursor := by
have hf : (TieBuffer.groupView key K view (TieBuffer.fill read key K B s).cursor).length ≤
fuel := by
rw [TieBuffer.fill_suffix read key K view hread B s, List.length_drop]
omega
have hs := view_complete read key K report hreport view hread
(TieBuffer.fill read key K B s).buffer (TieBuffer.fill read key K B s).cursor fuel a hf
have h := (TieBuffer.fillAcc_spec read key K view hread B s #[] 0).2.1
change view s = (TieBuffer.groupView key K view s).take B ++
view (TieBuffer.fill read key K B s).cursor at h
rw [hs, TieBuffer.fill_suffix read key K view hread B s, ← List.append_assoc,
List.take_append_drop] at h
exact h
end JoinOne
end LowSpaceSubsetSum
end OAI
|