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