File size: 4,479 Bytes
4a56cd6 | 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 | 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
|