File size: 4,486 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
import OAI.Combinatorics.LowSpaceSubsetSum.Counting.CountedReplay

namespace OAI

/-! # Cost-carrying bounded tie-buffer construction and lookahead -/

namespace LowSpaceSubsetSum.CountedFill

universe u v
variable {α : Type u} {σ : Type v}

def fillAcc (read : σ → Option (α × σ) × ℕ) (key : α → ℤ × ℕ)
    (cells : α → ℕ) (K : ℤ) : ℕ → σ → Array α → ℕ → TieBuffer.Result α σ × ℕ
  | 0, s, buf, q => (⟨buf, s, q⟩, 1)
  | fuel + 1, s, buf, q =>
    let fetched := read s
    match fetched.1 with
    | none => (⟨buf, s, q + 1⟩, fetched.2 + 5)
    | some (x, s') =>
      let found := key x
      if found.1 = K then
        let rest := fillAcc read key cells K fuel s' (buf.push x) (q + 1)
        (rest.1, fetched.2 + found.2 + cells x + 8 + rest.2)
      else (⟨buf, s, q + 1⟩, fetched.2 + found.2 + 6)

theorem fillAcc_value (read : σ → Option (α × σ) × ℕ) (key : α → ℤ × ℕ)
    (cells : α → ℕ) (K : ℤ) (fuel : ℕ) (s : σ) (buf : Array α) (q : ℕ) :
    (fillAcc read key cells K fuel s buf q).1 =
      TieBuffer.fillAcc (fun s => (read s).1) (fun x => (key x).1) K fuel s buf q := by
  induction fuel generalizing s buf q with
  | zero => rfl
  | succ fuel ih =>
    cases hr : (read s).1 with
    | none => simp only [fillAcc, TieBuffer.fillAcc, hr]
    | some z =>
      rcases z with ⟨x, s'⟩
      simp only [fillAcc, TieBuffer.fillAcc, hr]
      split
      · exact ih _ _ _
      · rfl

theorem fillAcc_work (read : σ → Option (α × σ) × ℕ) (key : α → ℤ × ℕ)
    (cells : α → ℕ) (K : ℤ) (R L S : ℕ)
    (hr : ∀ s, (read s).2 ≤ R) (hk : ∀ x, (key x).2 ≤ L) (hs : ∀ x, cells x ≤ S)
    (fuel : ℕ) (s : σ) (buf : Array α) (q : ℕ) :
    (fillAcc read key cells K fuel s buf q).2 + (R + L + S + 8) * q ≤
      (R + L + S + 8) * (fillAcc read key cells K fuel s buf q).1.queries + 1 := by
  induction fuel generalizing s buf q with
  | zero => simp only [fillAcc]; omega
  | succ fuel ih =>
    have hread := hr s
    cases hv : (read s).1 with
    | none => simp only [fillAcc, hv, Nat.mul_add, Nat.mul_one]; omega
    | some z =>
      rcases z with ⟨x, s'⟩
      have hkey := hk x
      have hcell := hs x
      simp only [fillAcc, hv]
      split
      · have hrest := ih s' (buf.push x) (q + 1)
        simp only [Nat.mul_add, Nat.mul_one] at hrest
        dsimp only
        omega
      · simp only [Nat.mul_add, Nat.mul_one]; omega

def fill (read : σ → Option (α × σ) × ℕ) (key : α → ℤ × ℕ)
    (cells : α → ℕ) (K : ℤ) (B : ℕ) (s : σ) : TieBuffer.Result α σ × ℕ :=
  fillAcc read key cells K B s #[] 0

theorem fill_value (read : σ → Option (α × σ) × ℕ) (key : α → ℤ × ℕ)
    (cells : α → ℕ) (K : ℤ) (B : ℕ) (s : σ) :
    (fill read key cells K B s).1 =
      TieBuffer.fill (fun s => (read s).1) (fun x => (key x).1) K B s :=
  fillAcc_value _ _ _ _ _ _ _ _

theorem fill_work (read : σ → Option (α × σ) × ℕ) (key : α → ℤ × ℕ)
    (cells : α → ℕ) (K : ℤ) (R L S : ℕ)
    (hr : ∀ s, (read s).2 ≤ R) (hk : ∀ x, (key x).2 ≤ L) (hs : ∀ x, cells x ≤ S)
    (B : ℕ) (s : σ) :
    (fill read key cells K B s).2 ≤
      (R + L + S + 8) * (fill read key cells K B s).1.queries + 1 := by
  simpa only [fill, Nat.mul_zero, Nat.add_zero] using
    fillAcc_work read key cells K R L S hr hk hs B s #[] 0

def hasKey (read : σ → Option (α × σ) × ℕ) (key : α → ℤ × ℕ) (K : ℤ) (s : σ) : Bool × ℕ :=
  let fetched := read s
  match fetched.1 with
  | none => (false, fetched.2 + 3)
  | some (x, _) =>
    let found := key x
    (decide (found.1 = K), fetched.2 + found.2 + 4)

theorem hasKey_value (read : σ → Option (α × σ) × ℕ) (key : α → ℤ × ℕ) (K : ℤ) (s : σ) :
    (hasKey read key K s).1 = TieBuffer.hasKey (fun s => (read s).1) (fun x => (key x).1) K s := by
  cases hv : (read s).1 <;> simp [hasKey, TieBuffer.hasKey, hv]

theorem hasKey_work (read : σ → Option (α × σ) × ℕ) (key : α → ℤ × ℕ) (K : ℤ)
    (R L : ℕ) (hr : ∀ s, (read s).2 ≤ R) (hk : ∀ x, (key x).2 ≤ L) (s : σ) :
    (hasKey read key K s).2 ≤ R + L + 4 := by
  have hread := hr s
  cases hv : (read s).1 with
  | none => simp only [hasKey, hv]; omega
  | some z =>
    rcases z with ⟨x, s'⟩
    have hkey := hk x
    simp only [hasKey, hv]
    omega

end LowSpaceSubsetSum.CountedFill

end OAI