introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
4.48 kB
import OAI.Combinatorics.LowSpaceSubsetSum.Streams.BufferReplay
namespace OAI
/-! # Cost-carrying tie-suffix replay
Every performed reader, key and callback evaluation contributes its own
counter. The result is exactly the bounded replay. The work bound
uses its actual stopped query/report counts, rather than the supplied fuel.
-/
namespace LowSpaceSubsetSum.CountedReplay
universe u v w
variable {Ξ± : Type u} {Οƒ : Type v} {Ο„ : Type w}
def runAcc (read : Οƒ β†’ Option (Ξ± Γ— Οƒ) Γ— β„•) (key : Ξ± β†’ β„€ Γ— β„•) (K : β„€)
(report : Ξ± β†’ Ο„ β†’ (Ο„ Γ— Bool) Γ— β„•) :
β„• β†’ Οƒ β†’ Ο„ β†’ β„• β†’ β„• β†’ TieReplay.Result Οƒ Ο„ Γ— β„•
| 0, s, a, q, r => (⟨s, a, q, r, false, false⟩, 1)
| fuel + 1, s, a, q, r =>
let fetched := read s
match fetched.1 with
| none => (⟨s, a, q + 1, r, true, false⟩, fetched.2 + 5)
| some (x, s') =>
let found := key x
if found.1 = K then
let emitted := report x a
if emitted.1.2 then
(⟨s', emitted.1.1, q + 1, r + 1, false, true⟩,
fetched.2 + found.2 + emitted.2 + 8)
else
let rest := runAcc read key K report fuel s' emitted.1.1 (q + 1) (r + 1)
(rest.1, fetched.2 + found.2 + emitted.2 + 8 + rest.2)
else (⟨s, a, q + 1, r, true, false⟩, fetched.2 + found.2 + 6)
theorem runAcc_value (read : Οƒ β†’ Option (Ξ± Γ— Οƒ) Γ— β„•) (key : Ξ± β†’ β„€ Γ— β„•) (K : β„€)
(report : Ξ± β†’ Ο„ β†’ (Ο„ Γ— Bool) Γ— β„•) (fuel : β„•) (s : Οƒ) (a : Ο„) (q r : β„•) :
(runAcc read key K report fuel s a q r).1 =
TieReplay.runAcc (fun s => (read s).1) (fun x => (key x).1) K
(fun x a => (report x a).1) fuel s a q r := by
induction fuel generalizing s a q r with
| zero => rfl
| succ fuel ih =>
cases hr : (read s).1 with
| none => simp only [runAcc, TieReplay.runAcc, hr]
| some z =>
rcases z with ⟨x, s'⟩
simp only [runAcc, TieReplay.runAcc, hr]
split
Β· split
Β· rfl
Β· exact ih _ _ _ _
Β· rfl
theorem runAcc_work (read : Οƒ β†’ Option (Ξ± Γ— Οƒ) Γ— β„•) (key : Ξ± β†’ β„€ Γ— β„•) (K : β„€)
(report : Ξ± β†’ Ο„ β†’ (Ο„ Γ— Bool) Γ— β„•) (R L C : β„•)
(hr : βˆ€ s, (read s).2 ≀ R) (hk : βˆ€ x, (key x).2 ≀ L)
(hc : βˆ€ x a, (report x a).2 ≀ C)
(fuel : β„•) (s : Οƒ) (a : Ο„) (q r : β„•) :
(runAcc read key K report fuel s a q r).2 + (R + L + 8) * q + C * r ≀
(R + L + 8) * (runAcc read key K report fuel s a q r).1.queries +
C * (runAcc read key K report fuel s a q r).1.reports + 1 := by
induction fuel generalizing s a q r with
| zero => simp only [runAcc]; omega
| succ fuel ih =>
have hread := hr s
cases hv : (read s).1 with
| none =>
simp only [runAcc, hv, Nat.mul_add, Nat.mul_one]
omega
| some z =>
rcases z with ⟨x, s'⟩
have hkey := hk x
simp only [runAcc, hv]
split
Β· have hcallback := hc x a
split
Β· simp only [Nat.mul_add, Nat.mul_one]; omega
Β· have hrest := ih s' (report x a).1.1 (q + 1) (r + 1)
simp only [Nat.mul_add, Nat.mul_one] at hrest
dsimp only
omega
Β· simp only [Nat.mul_add, Nat.mul_one]; omega
def run (read : Οƒ β†’ Option (Ξ± Γ— Οƒ) Γ— β„•) (key : Ξ± β†’ β„€ Γ— β„•) (K : β„€)
(report : Ξ± β†’ Ο„ β†’ (Ο„ Γ— Bool) Γ— β„•) (fuel : β„•) (s : Οƒ) (a : Ο„) :
TieReplay.Result Οƒ Ο„ Γ— β„• := runAcc read key K report fuel s a 0 0
theorem run_value (read : Οƒ β†’ Option (Ξ± Γ— Οƒ) Γ— β„•) (key : Ξ± β†’ β„€ Γ— β„•) (K : β„€)
(report : Ξ± β†’ Ο„ β†’ (Ο„ Γ— Bool) Γ— β„•) (fuel : β„•) (s : Οƒ) (a : Ο„) :
(run read key K report fuel s a).1 =
TieReplay.run (fun s => (read s).1) (fun x => (key x).1) K
(fun x a => (report x a).1) fuel s a := runAcc_value _ _ _ _ _ _ _ _ _
theorem run_work (read : Οƒ β†’ Option (Ξ± Γ— Οƒ) Γ— β„•) (key : Ξ± β†’ β„€ Γ— β„•) (K : β„€)
(report : Ξ± β†’ Ο„ β†’ (Ο„ Γ— Bool) Γ— β„•) (R L C : β„•)
(hr : βˆ€ s, (read s).2 ≀ R) (hk : βˆ€ x, (key x).2 ≀ L)
(hc : βˆ€ x a, (report x a).2 ≀ C) (fuel : β„•) (s : Οƒ) (a : Ο„) :
(run read key K report fuel s a).2 ≀
(R + L + 8) * (run read key K report fuel s a).1.queries +
C * (run read key K report fuel s a).1.reports + 1 := by
simpa only [run, Nat.mul_zero, Nat.add_zero] using
runAcc_work read key K report R L C hr hk hc fuel s a 0 0
end LowSpaceSubsetSum.CountedReplay
end OAI