File size: 15,721 Bytes
5d5048e | 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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | import OAI.Combinatorics.LowSpaceSubsetSum.Search.BudgetedReplay
import OAI.Combinatorics.LowSpaceSubsetSum.Search.BudgetedReplayLoops
import OAI.Combinatorics.LowSpaceSubsetSum.Streams.CountedJoinOne
namespace OAI
/-! # Prepaid buffered replay and suffix restoration
The fixed overhead and snapshot allowance are reserved before their operations.
The suffix receives only the allowance left by the completed buffer replay.
No branch tests the cost of an operation after executing it.
-/
namespace LowSpaceSubsetSum.BudgetedJoinOne
universe u v w
variable {α : Type u} {σ : Type v} {τ : Type w}
def execute (charge copyCharge : ℕ) (read : σ → Option (α × σ) × ℕ)
(key : α → ℤ × ℕ) (K : ℤ) (report : α → τ → (τ × Bool) × ℕ)
(clone : σ → σ × ℕ) (buffer : Array α) (saved : σ) (long : Bool)
(fuel : ℕ) (a : τ) (budget : ℕ) : Option (TieReplay.Result σ τ × ℕ) × ℕ :=
if 6 ≤ budget then
let first := BudgetedReplay.execute charge (fun i => (BufferReplay.read buffer i, 4))
key K report buffer.size 0 a 0 0 (budget - 6)
match first.1 with
| none => (none, first.2 + 6)
| some (out, left) =>
if out.halted then
(some (⟨saved, out.state, out.queries, out.reports, false, true⟩, left), first.2 + 6)
else if long then
if copyCharge ≤ left then
let copied := clone saved
let rest := BudgetedReplay.execute charge read key K report fuel copied.1
out.state 0 0 (left - copyCharge)
(rest.1.map (fun (tail, remaining) =>
(⟨tail.cursor, tail.state, out.queries + tail.queries,
out.reports + tail.reports, tail.complete, tail.halted⟩, remaining)),
first.2 + copied.2 + rest.2 + 6)
else (none, first.2 + 7)
else
(some (⟨saved, out.state, out.queries, out.reports, true, false⟩, left), first.2 + 6)
else (none, 1)
theorem execute_value (charge copyCharge : ℕ) (read : σ → Option (α × σ) × ℕ)
(key : α → ℤ × ℕ) (K : ℤ) (report : α → τ → (τ × Bool) × ℕ)
(clone : σ → σ × ℕ) (buffer : Array α) (saved : σ) (long : Bool)
(fuel : ℕ) (a : τ) (budget : ℕ) (out : TieReplay.Result σ τ) (remaining : ℕ)
(h : (execute charge copyCharge read key K report clone buffer saved long fuel a budget).1 =
some (out, remaining)) :
out = (CountedJoinOne.run read key K report clone buffer saved long fuel a).1 ∧
remaining ≤ budget := by
let first := BudgetedReplay.execute charge (fun i => (BufferReplay.read buffer i, 4))
key K report buffer.size 0 a 0 0 (budget - 6)
have hfirst : BudgetedReplay.execute charge (fun i => (BufferReplay.read buffer i, 4))
key K report buffer.size 0 a 0 0 (budget - 6) = first := rfl
rw [execute] at h
split at h
next hb =>
simp only [hfirst] at h
cases hf : first.1 with
| none => simp only [hf] at h; contradiction
| some p =>
rcases p with ⟨front, left⟩
have hv := BudgetedReplay.execute_result charge (fun i => (BufferReplay.read buffer i, 4))
key K report buffer.size 0 a 0 0 (budget - 6) front left hf
obtain ⟨hv, hvleft⟩ := hv
change front = (CountedReplay.run _ key K report buffer.size 0 a).1 at hv
simp only [hf] at h
rw [CountedJoinOne.run, ← hv]
split at h
next hh =>
cases h
exact ⟨by simp only [hh, ↓reduceIte], hvleft.trans (Nat.sub_le _ _)⟩
next hh =>
simp only [hh, Bool.false_eq_true, ↓reduceIte]
split at h
next hl =>
simp only [hl, ↓reduceIte]
split at h
next hc =>
dsimp only at h
obtain ⟨pair, hp, he⟩ := Option.map_eq_some_iff.mp h
rcases pair with ⟨tail, rest⟩
have ht := BudgetedReplay.execute_result charge read key K report fuel (clone saved).1
front.state 0 0 (left - copyCharge) tail rest hp
cases he
exact ⟨by rw [ht.1]; rfl,
ht.2.trans ((Nat.sub_le _ _).trans (hvleft.trans (Nat.sub_le _ _)))⟩
next _ => contradiction
next hl =>
cases h
exact ⟨by simp only [hl, Bool.false_eq_true, ↓reduceIte],
hvleft.trans (Nat.sub_le _ _)⟩
next _ => contradiction
/-- Every outcome, including either failed guard, stays within the allowance
plus its final failed comparison. -/
theorem execute_work (read : σ → Option (α × σ) × ℕ) (key : α → ℤ × ℕ) (K : ℤ)
(report : α → τ → (τ × Bool) × ℕ) (clone : σ → σ × ℕ)
(R L C charge copyCharge : ℕ) (hr : ∀ s, (read s).2 ≤ R)
(hk : ∀ x, (key x).2 ≤ L) (hc : ∀ x a, (report x a).2 ≤ C)
(hcharge : max R 4 + L + C + 8 ≤ charge) (hcopy : ∀ s, (clone s).2 ≤ copyCharge)
(buffer : Array α) (saved : σ) (long : Bool) (fuel : ℕ) (a : τ) (budget : ℕ) :
(execute charge copyCharge read key K report clone buffer saved long fuel a budget).2 ≤
budget + 1 := by
have hcb : 4 + L + C + 8 ≤ charge := by omega
have hcr : R + L + C + 8 ≤ charge := by omega
let first := BudgetedReplay.execute charge (fun i => (BufferReplay.read buffer i, 4))
key K report buffer.size 0 a 0 0 (budget - 6)
have hfirst : BudgetedReplay.execute charge (fun i => (BufferReplay.read buffer i, 4))
key K report buffer.size 0 a 0 0 (budget - 6) = first := rfl
have hw := BudgetedReplay.execute_work (fun i => (BufferReplay.read buffer i, 4)) key K report
4 L C charge (fun _ => le_rfl) hk hc hcb buffer.size 0 a 0 0 (budget - 6)
change first.2 ≤ budget - 6 + 1 at hw
rw [execute]
split
next hb =>
simp only [hfirst]
cases hf : first.1 with
| none => (try dsimp only); omega
| some p =>
rcases p with ⟨front, left⟩
have hv := BudgetedReplay.execute_result charge (fun i => (BufferReplay.read buffer i, 4))
key K report buffer.size 0 a 0 0 (budget - 6) front left hf
have hs := BudgetedReplay.execute_spent (fun i => (BufferReplay.read buffer i, 4)) key K report
4 L C charge (fun _ => le_rfl) hk hc hcb buffer.size 0 a 0 0 (budget - 6) front left hf
change first.2 ≤ budget - 6 - left at hs
dsimp only
split
· (try dsimp only); omega
· split
· split
· have ht := BudgetedReplay.execute_work read key K report R L C charge hr hk hc hcr
fuel (clone saved).1 front.state 0 0 (left - copyCharge)
have hcp := hcopy saved
try dsimp only
omega
· (try dsimp only); omega
· (try dsimp only); omega
next _ => (try dsimp only); omega
/-- Successful nested calls leave the claimed allowance unspent. -/
theorem execute_spent (read : σ → Option (α × σ) × ℕ) (key : α → ℤ × ℕ) (K : ℤ)
(report : α → τ → (τ × Bool) × ℕ) (clone : σ → σ × ℕ)
(R L C charge copyCharge : ℕ) (hr : ∀ s, (read s).2 ≤ R)
(hk : ∀ x, (key x).2 ≤ L) (hc : ∀ x a, (report x a).2 ≤ C)
(hcharge : max R 4 + L + C + 8 ≤ charge) (hcopy : ∀ s, (clone s).2 ≤ copyCharge)
(buffer : Array α) (saved : σ) (long : Bool) (fuel : ℕ) (a : τ) (budget : ℕ)
(out : TieReplay.Result σ τ) (remaining : ℕ)
(h : (execute charge copyCharge read key K report clone buffer saved long fuel a budget).1 =
some (out, remaining)) :
(execute charge copyCharge read key K report clone buffer saved long fuel a budget).2 ≤
budget - remaining := by
have hcb : 4 + L + C + 8 ≤ charge := by omega
have hcr : R + L + C + 8 ≤ charge := by omega
let first := BudgetedReplay.execute charge (fun i => (BufferReplay.read buffer i, 4))
key K report buffer.size 0 a 0 0 (budget - 6)
have hfirst : BudgetedReplay.execute charge (fun i => (BufferReplay.read buffer i, 4))
key K report buffer.size 0 a 0 0 (budget - 6) = first := rfl
rw [execute] at h ⊢
split at h
next hb =>
simp only [hb, ↓reduceIte, hfirst] at h ⊢
cases hf : first.1 with
| none => simp only [hf] at h; contradiction
| some p =>
rcases p with ⟨front, left⟩
have hv := BudgetedReplay.execute_result charge (fun i => (BufferReplay.read buffer i, 4))
key K report buffer.size 0 a 0 0 (budget - 6) front left hf
have hs := BudgetedReplay.execute_spent (fun i => (BufferReplay.read buffer i, 4)) key K report
4 L C charge (fun _ => le_rfl) hk hc hcb buffer.size 0 a 0 0 (budget - 6) front left hf
change first.2 ≤ budget - 6 - left at hs
simp only [hf] at h ⊢
split at h
next hh =>
simp only [hh, ↓reduceIte]
cases h
try dsimp only
omega
next hh =>
simp only [hh, Bool.false_eq_true, ↓reduceIte] at h ⊢
split at h
next hl =>
simp only [hl, ↓reduceIte] at h ⊢
split at h
next hp =>
simp only [hp, ↓reduceIte] at h ⊢
obtain ⟨pair, ht, he⟩ := Option.map_eq_some_iff.mp h
rcases pair with ⟨tail, rest⟩
have hrst := BudgetedReplay.execute_spent read key K report R L C charge hr hk hc hcr
fuel (clone saved).1 front.state 0 0 (left - copyCharge) tail rest ht
have hrleft := (BudgetedReplay.execute_result charge read key K report fuel
(clone saved).1 front.state 0 0 (left - copyCharge) tail rest ht).2
have hcp := hcopy saved
cases he
try dsimp only
omega
next _ => contradiction
next hl =>
simp only [hl, Bool.false_eq_true, ↓reduceIte]
cases h
try dsimp only
omega
next _ => contradiction
/-- Completion is governed by performed queries and actual restores, not
by multiplying the two advertised stream fuel bounds. -/
theorem execute_complete (charge copyCharge : ℕ) (read : σ → Option (α × σ) × ℕ)
(key : α → ℤ × ℕ) (K : ℤ) (report : α → τ → (τ × Bool) × ℕ)
(clone : σ → σ × ℕ) (buffer : Array α) (saved : σ) (long : Bool)
(fuel : ℕ) (a : τ) (budget : ℕ)
(hbudget : charge * ((CountedJoinOne.run read key K report clone buffer saved long fuel a).1.queries + 2) +
(if long then copyCharge else 0) + 6 ≤ budget) :
∃ remaining, (execute charge copyCharge read key K report clone buffer saved long fuel a budget).1 =
some ((CountedJoinOne.run read key K report clone buffer saved long fuel a).1, remaining) := by
let front := (CountedReplay.run (fun i => (BufferReplay.read buffer i, 4))
key K report buffer.size 0 a).1
have hfdef : (CountedReplay.run (fun i => (BufferReplay.read buffer i, 4))
key K report buffer.size 0 a).1 = front := rfl
have hq : front.queries ≤ (CountedJoinOne.run read key K report clone buffer saved long fuel a).1.queries := by
simp only [CountedJoinOne.run, hfdef]
split
· exact le_rfl
· split
· (try dsimp only); omega
· exact le_rfl
have hb : 6 ≤ budget := by omega
have hfallow : charge * (front.queries + 1) ≤ budget - 6 := by
have hm := Nat.mul_le_mul_left charge (show front.queries + 1 ≤
(CountedJoinOne.run read key K report clone buffer saved long fuel a).1.queries + 2 by omega)
omega
obtain ⟨left, hfirst⟩ := BudgetedReplay.execute_complete charge
(fun i => (BufferReplay.read buffer i, 4)) key K report buffer.size 0 a 0 0 (budget - 6)
(by change charge * (front.queries - 0 + 1) ≤ budget - 6
simpa only [Nat.sub_zero] using hfallow)
change (BudgetedReplay.execute charge (fun i => (BufferReplay.read buffer i, 4)) key K report
buffer.size 0 a 0 0 (budget - 6)).1 = some (front, left) at hfirst
have hremaining := BudgetedReplay.execute_remaining charge
(fun i => (BufferReplay.read buffer i, 4)) key K report buffer.size 0 a 0 0 (budget - 6)
front left hfirst
simp only [Nat.sub_zero] at hremaining
rw [execute, ite_eq_left hb]
simp only [hfirst]
by_cases hh : front.halted = true
· refine ⟨left, ?_⟩
simp only [hh, ↓reduceIte, CountedJoinOne.run, hfdef]
· simp only [hh, Bool.false_eq_true, ↓reduceIte]
by_cases hl : long = true
· simp only [hl, ↓reduceIte]
have hm : charge * ((CountedReplay.run read key K report fuel (clone saved).1 front.state).1.queries + 1) +
copyCharge ≤ left := by
simp only [CountedJoinOne.run, hfdef, hh, Bool.false_eq_true, hl, ↓reduceIte] at hbudget
simp only [Nat.mul_add, Nat.mul_one] at hbudget hremaining ⊢
omega
have hcopy : copyCharge ≤ left := by omega
rw [ite_eq_left hcopy]
obtain ⟨remaining, hrest⟩ := BudgetedReplay.execute_complete charge read key K report
fuel (clone saved).1 front.state 0 0 (left - copyCharge)
(by simpa only [CountedReplay.run, Nat.sub_zero] using (show
charge * ((CountedReplay.run read key K report fuel (clone saved).1 front.state).1.queries + 1) ≤
left - copyCharge by omega))
refine ⟨remaining, ?_⟩
simp only [hrest, Option.map_some, CountedJoinOne.run, hfdef, hh, Bool.false_eq_true,
↓reduceIte]
rfl
· refine ⟨left, ?_⟩
simp only [hl, Bool.false_eq_true, ↓reduceIte, CountedJoinOne.run, hfdef, hh]
/-- At most one charge per query, two terminal charges, and one snapshot
allowance are discarded by a completed inner replay. -/
theorem execute_remaining (charge copyCharge : ℕ) (read : σ → Option (α × σ) × ℕ)
(key : α → ℤ × ℕ) (K : ℤ) (report : α → τ → (τ × Bool) × ℕ)
(clone : σ → σ × ℕ) (buffer : Array α) (saved : σ) (long : Bool)
(fuel : ℕ) (a : τ) (budget : ℕ) (out : TieReplay.Result σ τ) (remaining : ℕ)
(h : (execute charge copyCharge read key K report clone buffer saved long fuel a budget).1 =
some (out, remaining)) :
budget ≤ remaining + charge * (out.queries + 2) + (if long then copyCharge else 0) + 6 := by
let first := BudgetedReplay.execute charge (fun i => (BufferReplay.read buffer i, 4))
key K report buffer.size 0 a 0 0 (budget - 6)
have hfirst : BudgetedReplay.execute charge (fun i => (BufferReplay.read buffer i, 4))
key K report buffer.size 0 a 0 0 (budget - 6) = first := rfl
rw [execute] at h
split at h
next hb =>
simp only [hfirst] at h
cases hf : first.1 with
| none => simp only [hf] at h; contradiction
| some p =>
rcases p with ⟨front, left⟩
have hs := BudgetedReplay.execute_remaining charge (fun i => (BufferReplay.read buffer i, 4))
key K report buffer.size 0 a 0 0 (budget - 6) front left hf
simp only [Nat.sub_zero, Nat.mul_add, Nat.mul_one] at hs
simp only [hf] at h
split at h
next hh =>
cases h
simp only [Nat.mul_add]
omega
next hh =>
split at h
next hl =>
split at h
next hp =>
obtain ⟨pair, ht, he⟩ := Option.map_eq_some_iff.mp h
rcases pair with ⟨tail, rest⟩
have hrst := BudgetedReplay.execute_remaining charge read key K report fuel
(clone saved).1 front.state 0 0 (left - copyCharge) tail rest ht
simp only [Nat.sub_zero, Nat.mul_add, Nat.mul_one] at hrst
cases he
simp only [hl, ↓reduceIte, Nat.mul_add]
omega
next _ => contradiction
next hl =>
cases h
simp only [Nat.mul_add]
omega
next _ => contradiction
end LowSpaceSubsetSum.BudgetedJoinOne
end OAI
|