File size: 11,387 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import OAI.Combinatorics.LowSpaceSubsetSum.Compression.CompressionCallbacks
import OAI.Combinatorics.LowSpaceSubsetSum.Compression.CompressionReader
import OAI.Combinatorics.LowSpaceSubsetSum.Search.BudgetedParentRemaining
import OAI.Combinatorics.LowSpaceSubsetSum.Search.BudgetedParentSpent
import OAI.Combinatorics.LowSpaceSubsetSum.Separators.BudgetedSeparatorFlow
import OAI.Combinatorics.LowSpaceSubsetSum.Streams.JoinUserMap

namespace OAI

/-! # Concrete compression joins with an execution watchdog

The input arrays are shared, snapshots copy only the right tournament, and
callbacks execute the bounded dictionary operations. The charge is independent
of the queried values and of the success event.
-/

namespace LowSpaceSubsetSum.BudgetedCompressionJoin

universe u
variable {n : ℕ} {τ : Type u}
open CompressionReader (State)

def reference (P t : ℤ) (B : ℕ) (right₁ right₂ : Array ℤ) {d₁ d₂ : ℕ}
    (report : ℤ → ℤ → τ → (τ × Bool) × ℕ) (fuel₁ fuel₂ control : ℕ)
    (s₁ : State P .direct right₁ d₁) (s₂ : State P (.complementary t) right₂ d₂) (a : τ) :=
  CountedStreamJoin.runAcc
    (CompressionReader.read P .direct right₁) (CompressionReader.scalarKey P .direct)
    (CompressionReader.read P (.complementary t) right₂)
    (CompressionReader.scalarKey P (.complementary t)) (fun _ => 1) report
    (CompressionReader.snapshot P (.complementary t) right₂)
    B fuel₁ fuel₂ control (StreamJoin.start s₁ s₂ a)

def run (I : Instance n) (P t : ℤ) (B : ℕ) (right₁ right₂ : Array ℤ) {d₁ d₂ : ℕ}
    (report : ℤ → ℤ → τ → (τ × Bool) × ℕ) (fuel₁ fuel₂ control : ℕ)
    (s₁ : State P .direct right₁ d₁) (s₂ : State P (.complementary t) right₂ d₂)
    (a : τ) (budget : ℕ) :=
  let charge := inputScale I ^ 4
  BudgetedJoinExecution.run charge charge charge charge charge B
    (CompressionReader.read P .direct right₁) (CompressionReader.scalarKey P .direct)
    (CompressionReader.read P (.complementary t) right₂)
    (CompressionReader.scalarKey P (.complementary t)) (fun _ => 1) report
    (CompressionReader.snapshot P (.complementary t) right₂)
    B fuel₁ fuel₂ control (StreamJoin.start s₁ s₂ a) budget

theorem reference_value (P t : ℤ) (B : ℕ) (right₁ right₂ : Array ℤ) {d₁ d₂ : ℕ}
    (report : ℤ → ℤ → τ → (τ × Bool) × ℕ) (fuel₁ fuel₂ control : ℕ)
    (s₁ : State P .direct right₁ d₁) (s₂ : State P (.complementary t) right₂ d₂) (a : τ) :
    (reference P t B right₁ right₂ report fuel₁ fuel₂ control s₁ s₂ a).1 =
      StreamJoin.stopped
        (fun s => (CompressionReader.read P .direct right₁ s).1) (fun x => x % P)
        (fun s => (CompressionReader.read P (.complementary t) right₂ s).1)
        (fun y => (t - y) % P) (fun x y a => (report x y a).1)
        B fuel₁ fuel₂ control s₁ s₂ a := by
  exact CountedStreamJoin.runAcc_value _ _ _ _ _ _ _
    (CompressionReader.snapshot_value P (.complementary t) right₂) _ _ _ _ _

theorem run_value (I : Instance n) (P t : ℤ) (B : ℕ)
    (right₁ right₂ : Array ℤ) {d₁ d₂ : ℕ}
    (report : ℤ → ℤ → τ → (τ × Bool) × ℕ) (fuel₁ fuel₂ control : ℕ)
    (s₁ : State P .direct right₁ d₁) (s₂ : State P (.complementary t) right₂ d₂)
    (a : τ) (budget : ℕ) (out : StreamJoin.StoppedResult _ _ τ) (left : ℕ)
    (h : (run I P t B right₁ right₂ report fuel₁ fuel₂ control s₁ s₂ a budget).1 = some (out, left)) :
    out = (reference P t B right₁ right₂ report fuel₁ fuel₂ control s₁ s₂ a).1 ∧ left ≤ budget :=
  BudgetedJoinExecution.run_value _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ h

private theorem operation_charge {u : ℕ} (hu : 10000 ≤ u) : 2 * u ^ 2 + 32 ≤ u ^ 4 := by
  have h1 : 1 ≤ u ^ 2 := Nat.one_le_pow _ _ (by omega)
  have hsq : 34 ≤ u ^ 2 := by nlinarith only [hu]
  calc
    _ ≤ 34 * u ^ 2 := by omega
    _ ≤ u ^ 2 * u ^ 2 := Nat.mul_le_mul_right _ hsq
    _ = _ := by ring

theorem run_work (I : Instance n) (P t : ℤ) (B : ℕ)
    (right₁ right₂ : Array ℤ) {d₁ d₂ : ℕ}
    (report : ℤ → ℤ → τ → (τ × Bool) × ℕ) (fuel₁ fuel₂ control : ℕ)
    (s₁ : State P .direct right₁ d₁) (s₂ : State P (.complementary t) right₂ d₂)
    (a : τ) (budget : ℕ) (hn : 10000 ≤ n) (hd₁ : d₁ ≤ n) (hd₂ : d₂ ≤ n)
    (hreport : ∀ x y a, (report x y a).2 ≤ inputScale I ^ 2)
    (hcopy : 10 * 2 ^ (d₂ + 1) ≤ B) :
    (run I P t B right₁ right₂ report fuel₁ fuel₂ control s₁ s₂ a budget).2 ≤ budget + 1 := by
  have hu : 10000 ≤ inputScale I := by unfold inputScale; omega
  have hb := operation_charge hu
  have hsq : 4 ≤ inputScale I ^ 2 := by nlinarith only [hu]
  have hm : max (inputScale I ^ 2) 4 = inputScale I ^ 2 := max_eq_left hsq
  exact BudgetedJoinExecution.run_work
    (CompressionReader.read P .direct right₁) (CompressionReader.scalarKey P .direct)
    (CompressionReader.read P (.complementary t) right₂)
    (CompressionReader.scalarKey P (.complementary t)) (fun _ => 1) report
    (CompressionReader.snapshot P (.complementary t) right₂)
    (inputScale I ^ 2) 3 (inputScale I ^ 2) 3 1 (inputScale I ^ 2)
    (inputScale I ^ 4) (inputScale I ^ 4) (inputScale I ^ 4) (inputScale I ^ 4) (inputScale I ^ 4) B
    (fun s => CompressionReader.read_scale I hn P .direct right₁ s hd₁)
    (CompressionReader.scalarKey_work P .direct)
    (fun s => CompressionReader.read_scale I hn P (.complementary t) right₂ s hd₂)
    (CompressionReader.scalarKey_work P (.complementary t))
    (fun _ => le_rfl) hreport (by omega) (by omega) (by omega) (by omega)
    (by rw [hm]; omega)
    (fun s => (CompressionReader.snapshot_work P (.complementary t) right₂ s).trans hcopy)
    B fuel₁ fuel₂ control (StreamJoin.start s₁ s₂ a) budget

theorem query_charge (I : Instance n) (hn : 10000 ≤ n) :
    7 * inputScale I ^ 4 + 16 ≤ inputScale I ^ 5 := by
  have hu : 10000 ≤ inputScale I := by unfold inputScale; omega
  have h1 : 1 ≤ inputScale I ^ 4 := Nat.one_le_pow _ _ (by omega)
  have hs : 23 * inputScale I ^ 4 ≤ inputScale I ^ 5 := by
    calc
      _ ≤ inputScale I * inputScale I ^ 4 := Nat.mul_le_mul_right _ (by omega)
      _ = _ := (pow_succ' (inputScale I) 4).symm
  omega

theorem account (I : Instance n) (P t : ℤ) (B : ℕ)
    (right₁ right₂ : Array ℤ) {d₁ d₂ : ℕ}
    (report : ℤ → ℤ → τ → (τ × Bool) × ℕ) (fuel₁ fuel₂ control : ℕ)
    (s₁ : State P .direct right₁ d₁) (s₂ : State P (.complementary t) right₂ d₂)
    (a : τ) (budget : ℕ) (hn : 10000 ≤ n) (hd₁ : d₁ ≤ n) (hd₂ : d₂ ≤ n)
    (hreport : ∀ x y a, (report x y a).2 ≤ inputScale I ^ 2)
    (hcopy : 10 * 2 ^ (d₂ + 1) ≤ B) :
    BudgetedSeparatorFlow.Account budget
      (run I P t B right₁ right₂ report fuel₁ fuel₂ control s₁ s₂ a budget) := by
  refine ⟨run_work I P t B right₁ right₂ report fuel₁ fuel₂ control s₁ s₂ a budget
    hn hd₁ hd₂ hreport hcopy, ?_⟩
  intro out left h
  refine ⟨(run_value I P t B right₁ right₂ report fuel₁ fuel₂ control s₁ s₂ a budget out left h).2, ?_⟩
  have hu : 10000 ≤ inputScale I := by unfold inputScale; omega
  have hb := operation_charge hu
  have hsq : 4 ≤ inputScale I ^ 2 := by nlinarith only [hu]
  have hm : max (inputScale I ^ 2) 4 = inputScale I ^ 2 := max_eq_left hsq
  exact BudgetedJoinExecution.run_spent
    (CompressionReader.read P .direct right₁) (CompressionReader.scalarKey P .direct)
    (CompressionReader.read P (.complementary t) right₂)
    (CompressionReader.scalarKey P (.complementary t)) (fun _ => 1) report
    (CompressionReader.snapshot P (.complementary t) right₂)
    (inputScale I ^ 2) 3 (inputScale I ^ 2) 3 1 (inputScale I ^ 2)
    (inputScale I ^ 4) (inputScale I ^ 4) (inputScale I ^ 4) (inputScale I ^ 4) (inputScale I ^ 4) B
    (fun s => CompressionReader.read_scale I hn P .direct right₁ s hd₁)
    (CompressionReader.scalarKey_work P .direct)
    (fun s => CompressionReader.read_scale I hn P (.complementary t) right₂ s hd₂)
    (CompressionReader.scalarKey_work P (.complementary t))
    (fun _ => le_rfl) hreport (by omega) (by omega) (by omega) (by omega)
    (by rw [hm]; omega)
    (fun s => (CompressionReader.snapshot_work P (.complementary t) right₂ s).trans hcopy)
    B fuel₁ fuel₂ control (StreamJoin.start s₁ s₂ a) budget out left h

theorem run_complete (I : Instance n) (P t : ℤ) (B : ℕ)
    (right₁ right₂ : Array ℤ) {d₁ d₂ : ℕ}
    (report : ℤ → ℤ → τ → (τ × Bool) × ℕ) (fuel₁ fuel₂ control : ℕ)
    (s₁ : State P .direct right₁ d₁) (s₂ : State P (.complementary t) right₂ d₂)
    (a : τ) (budget : ℕ) (hn : 10000 ≤ n)
    (hb : let out := (reference P t B right₁ right₂ report fuel₁ fuel₂ control s₁ s₂ a).1
      inputScale I ^ 5 * out.state.queries + B * out.state.copies + 4 ≤ budget) :
    ∃ left, (run I P t B right₁ right₂ report fuel₁ fuel₂ control s₁ s₂ a budget).1 =
      some ((reference P t B right₁ right₂ report fuel₁ fuel₂ control s₁ s₂ a).1, left) := by
  apply BudgetedJoinExecution.run_complete_queries
  · exact CompressionReader.snapshot_value P (.complementary t) right₂
  · have hmul := Nat.mul_le_mul_right
      (reference P t B right₁ right₂ report fuel₁ fuel₂ control s₁ s₂ a).1.state.queries
      (query_charge I hn)
    change _ ≤ budget
    dsimp only at hb
    have he : 2 * inputScale I ^ 4 + inputScale I ^ 4 + inputScale I ^ 4 + inputScale I ^ 4 +
        2 * inputScale I ^ 4 + 16 = 7 * inputScale I ^ 4 + 16 := by omega
    rw [he]
    exact (Nat.add_le_add_right (Nat.add_le_add_right hmul _) _).trans hb

theorem run_remaining (I : Instance n) (P t : ℤ) (B : ℕ)
    (right₁ right₂ : Array ℤ) {d₁ d₂ : ℕ}
    (report : ℤ → ℤ → τ → (τ × Bool) × ℕ) (fuel₁ fuel₂ control : ℕ)
    (s₁ : State P .direct right₁ d₁) (s₂ : State P (.complementary t) right₂ d₂)
    (a : τ) (budget : ℕ) (hn : 10000 ≤ n) (out : StreamJoin.StoppedResult _ _ τ) (left : ℕ)
    (h : (run I P t B right₁ right₂ report fuel₁ fuel₂ control s₁ s₂ a budget).1 = some (out, left)) :
    budget ≤ left + inputScale I ^ 5 * out.state.queries + B * out.state.copies + 4 := by
  have hr := BudgetedJoinExecution.run_remaining_queries
    (inputScale I ^ 4) (inputScale I ^ 4) (inputScale I ^ 4) (inputScale I ^ 4) (inputScale I ^ 4) B
    (CompressionReader.read P .direct right₁) (CompressionReader.scalarKey P .direct)
    (CompressionReader.read P (.complementary t) right₂)
    (CompressionReader.scalarKey P (.complementary t)) (fun _ => 1) report
    (CompressionReader.snapshot P (.complementary t) right₂)
    (CompressionReader.snapshot_value P (.complementary t) right₂)
    B fuel₁ fuel₂ control s₁ s₂ a budget out left h
  have hmul := Nat.mul_le_mul_right out.state.queries (query_charge I hn)
  have he : 2 * inputScale I ^ 4 + inputScale I ^ 4 + inputScale I ^ 4 + inputScale I ^ 4 +
      2 * inputScale I ^ 4 + 16 = 7 * inputScale I ^ 4 + 16 := by omega
  rw [he] at hr
  omega

end LowSpaceSubsetSum.BudgetedCompressionJoin

end OAI