File size: 8,310 Bytes
e5a97c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.Combinatorics.LowSpaceSubsetSum.Counting.ParentRestrictionUniverse
import OAI.Combinatorics.LowSpaceSubsetSum.Streams.PrefixCheckList

namespace OAI

/-! # Task-start bounds for the executable prefix recursion -/

noncomputable section

namespace LowSpaceSubsetSum.RepresentationParentData

variable {n : ℕ} (D : RepresentationParentData n)

def overflowCount (B : ℕ) : List (Fin n) → MaskRestriction n → ℕ
  | order, P =>
    if ({ D with maskRestriction := P }.collect B).state.user.keys.overflow then
      match order with
      | [] => 1
      | i :: rest => 1 + overflowCount B rest (P.force i false) + overflowCount B rest (P.force i true)
    else 0

def taskStarts (B : ℕ) : List (Fin n) → MaskRestriction n → ℕ
  | [], _ => 1
  | i :: rest, P =>
    if ({ D with maskRestriction := P }.collect B).state.user.keys.overflow then
      1 + taskStarts B rest (P.force i false) + taskStarts B rest (P.force i true)
    else 1

theorem taskStarts_le_overflow (B : ℕ) (order : List (Fin n)) (P : MaskRestriction n) :
    D.taskStarts B order P ≤ 1 + 2 * D.overflowCount B order P := by
  induction order generalizing P with
  | nil => simp [taskStarts, overflowCount]
  | cons i order ih =>
    simp only [taskStarts, overflowCount]
    split
    · change 1 + D.taskStarts B order (P.force i false) +
          D.taskStarts B order (P.force i true) ≤
        1 + 2 * (1 + D.overflowCount B order (P.force i false) +
          D.overflowCount B order (P.force i true))
      have h0 := ih (P.force i false)
      have h1 := ih (P.force i true)
      omega
    · omega

theorem entryUniverse_force_card_add (P : MaskRestriction n)
    (hd : ∀ i j, i ≠ j → Disjoint (D.leaves i).top (D.leaves j).top)
    (i : Fin n) (hi : i ∈ RepresentationUniverse.topDomain D.leaves) :
    ({ D with maskRestriction := P.force i false }).entryUniverse.card +
      ({ D with maskRestriction := P.force i true }).entryUniverse.card =
        ({ D with maskRestriction := P }).entryUniverse.card := by
  classical
  rw [D.entryUniverse_force P hd i hi false, D.entryUniverse_force P hd i hi true]
  have h := Finset.card_filter_add_card_filter_not
    (s := ({ D with maskRestriction := P }).entryUniverse) (p := fun e => i ∈ e.mask)
  simpa only [decide_eq_false_iff_not, decide_eq_true_eq, add_comm] using h

theorem overflowCount_capacity (B : ℕ)
    (hd : ∀ i j, i ≠ j → Disjoint (D.leaves i).top (D.leaves j).top)
    (hbounded : ∀ P, ∀ e ∈ ({ D with maskRestriction := P }).rawEntries,
      WeightEncoding.BoundedWeight D.input e.value)
    (order : List (Fin n)) (horder : ∀ i ∈ order, i ∈ RepresentationUniverse.topDomain D.leaves)
    (P : MaskRestriction n) :
    D.capacity * D.overflowCount B order P ≤
      (order.length + 1) * ({ D with maskRestriction := P }).entryUniverse.card := by
  induction order generalizing P with
  | nil =>
    simp only [overflowCount, List.length_nil]
    split
    · rename_i ho
      have hc : D.capacity < ({ D with maskRestriction := P }).entryUniverse.card :=
        (({ D with maskRestriction := P }).collect_overflow_iff_card B (hbounded P)).mp ho
      simp only [Nat.zero_add, one_mul, mul_one]
      omega
    · simp
  | cons i order ih =>
    simp only [overflowCount, List.length_cons]
    split
    · rename_i ho
      have hc : D.capacity < ({ D with maskRestriction := P }).entryUniverse.card :=
        (({ D with maskRestriction := P }).collect_overflow_iff_card B (hbounded P)).mp ho
      have h0 := ih (fun j hj => horder j (by simp [hj])) (P.force i false)
      have h1 := ih (fun j hj => horder j (by simp [hj])) (P.force i true)
      have hs := D.entryUniverse_force_card_add P hd i (horder i (by simp))
      generalize ({ D with maskRestriction := P }).entryUniverse.card = U at hc hs ⊢
      generalize ({ D with maskRestriction := P.force i false }).entryUniverse.card = U0 at h0 hs
      generalize ({ D with maskRestriction := P.force i true }).entryUniverse.card = U1 at h1 hs
      nlinarith only [hc, h0, h1, hs]
    · simp

theorem taskStarts_capacity (B : ℕ)
    (hd : ∀ i j, i ≠ j → Disjoint (D.leaves i).top (D.leaves j).top)
    (hbounded : ∀ P, ∀ e ∈ ({ D with maskRestriction := P }).rawEntries,
      WeightEncoding.BoundedWeight D.input e.value)
    (order : List (Fin n)) (horder : ∀ i ∈ order, i ∈ RepresentationUniverse.topDomain D.leaves)
    (P : MaskRestriction n) :
    D.capacity * D.taskStarts B order P ≤ D.capacity +
      2 * (order.length + 1) * ({ D with maskRestriction := P }).entryUniverse.card := by
  have ht := D.taskStarts_le_overflow B order P
  have ho := D.overflowCount_capacity B hd hbounded order horder P
  nlinarith

theorem completedArrays_length_le (B : ℕ) (order : List (Fin n)) (P : MaskRestriction n) :
    (D.completedArrays B order P).length ≤ D.taskStarts B order P := by
  induction order generalizing P with
  | nil =>
    simp only [completedArrays, taskStarts]
    split <;> simp
  | cons i order ih =>
    simp only [completedArrays, taskStarts]
    split
    · change (D.completedArrays B order (P.force i false) ++
          D.completedArrays B order (P.force i true)).length ≤
        1 + D.taskStarts B order (P.force i false) + D.taskStarts B order (P.force i true)
      simp only [List.length_append]
      have h0 := ih (P.force i false)
      have h1 := ih (P.force i true)
      omega
    · simp

theorem completedArrays_size_sum_le (B : ℕ)
    (hd : ∀ i j, i ≠ j → Disjoint (D.leaves i).top (D.leaves j).top)
    (hbounded : ∀ P, ∀ e ∈ ({ D with maskRestriction := P }).rawEntries,
      WeightEncoding.BoundedWeight D.input e.value)
    (order : List (Fin n)) (horder : ∀ i ∈ order, i ∈ RepresentationUniverse.topDomain D.leaves)
    (P : MaskRestriction n) :
    ((D.completedArrays B order P).map Array.size).sum ≤
      ({ D with maskRestriction := P }).entryUniverse.card := by
  induction order generalizing P with
  | nil =>
    simp only [completedArrays]
    split
    · simp
    · rename_i hc
      have hclear := Bool.eq_false_iff.mpr hc
      simpa only [List.map_singleton, List.sum_singleton, entries] using
        (({ D with maskRestriction := P }).completed_size_eq_card B (hbounded P) hclear).le
  | cons i order ih =>
    simp only [completedArrays]
    split
    · simp only [List.map_append, List.sum_append]
      have h0 := ih (fun j hj => horder j (by simp [hj])) (P.force i false)
      have h1 := ih (fun j hj => horder j (by simp [hj])) (P.force i true)
      have hs := D.entryUniverse_force_card_add P hd i (horder i (by simp))
      omega
    · rename_i hc
      have hclear := Bool.eq_false_iff.mpr hc
      simpa only [List.map_singleton, List.sum_singleton, entries] using
        (({ D with maskRestriction := P }).completed_size_eq_card B (hbounded P) hclear).le

private theorem input_sum (L : ℕ) (arrays : List (Array (Entry (Fin n)))) :
    (arrays.map (fun a => L + a.size)).sum = (arrays.map Array.size).sum + L * arrays.length := by
  induction arrays with
  | nil => simp
  | cons a arrays ih =>
    simp only [List.map_cons, List.sum_cons, List.length_cons, ih, Nat.mul_add, Nat.mul_one]
    omega

/-- Includes every reuse of the retained left array at a completed right
prefix; it is the actual check list's input-occurrence bound. -/
theorem completed_check_inputs_le (B L : ℕ) (hL : L ≤ D.capacity)
    (hd : ∀ i j, i ≠ j → Disjoint (D.leaves i).top (D.leaves j).top)
    (hbounded : ∀ P, ∀ e ∈ ({ D with maskRestriction := P }).rawEntries,
      WeightEncoding.BoundedWeight D.input e.value)
    (order : List (Fin n)) (horder : ∀ i ∈ order, i ∈ RepresentationUniverse.topDomain D.leaves)
    (P : MaskRestriction n) :
    ((D.completedArrays B order P).map (fun a => L + a.size)).sum ≤
      L + (2 * order.length + 3) * ({ D with maskRestriction := P }).entryUniverse.card := by
  rw [input_sum]
  have hn := D.completedArrays_length_le B order P
  have he := D.completedArrays_size_sum_le B hd hbounded order horder P
  have ht := D.taskStarts_le_overflow B order P
  have ho := D.overflowCount_capacity B hd hbounded order horder P
  have hscale := Nat.mul_le_mul_left (D.overflowCount B order P) hL
  nlinarith

end LowSpaceSubsetSum.RepresentationParentData

end

end OAI