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