File size: 11,368 Bytes
9dbc64e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import OAI.Computability.DepthThree.LanguageBitConvolutionUpdates

namespace OAI

namespace DepthThreeLowerBound

def modulusUpdateLoop (p : List Bool) (v : Bool) (shift : ℕ) : ℕ → List Bool → List Bool
  | 0, c => c
  | j + 1, c => xorListAt (modulusUpdateLoop p v shift j c)
      (shift + j) (v && p.getD j false)

@[simp] theorem modulusUpdateLoop_zero (p : List Bool) (v : Bool)
    (shift : ℕ) (c : List Bool) : modulusUpdateLoop p v shift 0 c = c := rfl

theorem modulusUpdateLoop_step (p : List Bool) (v : Bool) (shift j : ℕ) (c : List Bool) :
    modulusUpdateLoop p v shift (j + 1) c =
      xorListAt (modulusUpdateLoop p v shift j c) (shift + j) (v && p.getD j false) := rfl

@[simp] theorem modulusUpdateLoop_length (p : List Bool) (v : Bool)
    (shift j : ℕ) (c : List Bool) :
    (modulusUpdateLoop p v shift j c).length = c.length := by
  induction j with
  | zero => rfl
  | succ j ih => rw [modulusUpdateLoop_step, xorListAt_length, ih]

theorem modulusUpdateLoop_getD (p : List Bool) (v : Bool) (shift j : ℕ)
    (c : List Bool) (hbound : shift + j ≤ c.length) (k : ℕ) :
    (modulusUpdateLoop p v shift j c).getD k false =
      Bool.xor (c.getD k false)
        (if shift ≤ k ∧ k < shift + j then v && p.getD (k - shift) false else false) := by
  revert hbound
  induction j with
  | zero =>
      intro hbound
      have hk : ¬(shift ≤ k ∧ k < shift + 0) := by omega
      simp only [modulusUpdateLoop_zero, ite_eq_right hk, Bool.xor_false]
  | succ j ih =>
      intro hbound
      have hj : shift + j < (modulusUpdateLoop p v shift j c).length := by
        rw [modulusUpdateLoop_length]
        omega
      rw [modulusUpdateLoop_step, xorListAt_getD _ _ _ hj k, ih (by omega)]
      by_cases he : shift + j = k
      · have hprev : ¬(shift ≤ k ∧ k < shift + j) := by omega
        have hnext : shift ≤ k ∧ k < shift + (j + 1) := by omega
        have hsub : k - shift = j := by omega
        simp [he, hnext, hsub]
      · have hrange : (shift ≤ k ∧ k < shift + (j + 1)) ↔
            (shift ≤ k ∧ k < shift + j) := by omega
        simp only [ite_eq_right he, Bool.xor_false, hrange]

theorem modulusUpdateLoop_above (p : List Bool) (v : Bool) (shift j : ℕ)
    (c : List Bool) (hbound : shift + j ≤ c.length) (k : ℕ) (hk : shift + j ≤ k) :
    (modulusUpdateLoop p v shift j c).getD k false = c.getD k false := by
  rw [modulusUpdateLoop_getD p v shift j c hbound k]
  have hnot : ¬(shift ≤ k ∧ k < shift + j) := by omega
  simp only [ite_eq_right hnot, Bool.xor_false]

theorem modulusUpdateLoop_high (p : List Bool) (v : Bool) (shift : ℕ)
    (c : List Bool) (hbound : shift + p.length ≤ c.length) :
    (modulusUpdateLoop p v shift p.length c).getD (shift + p.length) false =
      c.getD (shift + p.length) false :=
  modulusUpdateLoop_above p v shift p.length c hbound _ le_rfl

def cancelByUpdates (p c : List Bool) (shift : ℕ) : List Bool :=
  (modulusUpdateLoop p (c.getD (shift + p.length) false) shift p.length c).set
    (shift + p.length) false

@[simp] theorem cancelByUpdates_length (p c : List Bool) (shift : ℕ) :
    (cancelByUpdates p c shift).length = c.length := by
  simp [cancelByUpdates]

theorem cancelByUpdates_getD (p c : List Bool) (shift : ℕ)
    (hbound : shift + p.length < c.length) (k : ℕ) :
    (cancelByUpdates p c shift).getD k false =
      if shift + p.length = k then false else
        Bool.xor (c.getD k false)
          (if shift ≤ k ∧ k < shift + p.length then
            c.getD (shift + p.length) false && p.getD (k - shift) false else false) := by
  have ht : shift + p.length <
      (modulusUpdateLoop p (c.getD (shift + p.length) false) shift p.length c).length := by
    simpa only [modulusUpdateLoop_length] using hbound
  by_cases he : shift + p.length = k
  · subst k
    rw [ite_eq_left rfl]
    unfold cancelByUpdates
    exact List.getD_eq_getElem?_getD.trans
      (congrArg (fun o : Option Bool => o.getD false)
        (List.getElem?_set_self (a := false) ht))
  · have hset : (cancelByUpdates p c shift).getD k false =
        (modulusUpdateLoop p (c.getD (shift + p.length) false) shift p.length c).getD k false := by
      simp only [cancelByUpdates, List.getD_eq_getElem?_getD, List.getElem?_set_ne he]
    rw [hset, modulusUpdateLoop_getD _ _ _ _ _ (Nat.le_of_lt hbound) k, ite_eq_right he]

def reduceByUpdates (p : List Bool) : ℕ → List Bool → List Bool
  | 0, c => c
  | s + 1, c => reduceByUpdates p s (cancelByUpdates p c s)

@[simp] theorem reduceByUpdates_zero (p c : List Bool) : reduceByUpdates p 0 c = c := rfl

theorem reduceByUpdates_step (p c : List Bool) (s : ℕ) :
    reduceByUpdates p (s + 1) c = reduceByUpdates p s (cancelByUpdates p c s) := rfl

@[simp] theorem reduceByUpdates_length (p c : List Bool) (s : ℕ) :
    (reduceByUpdates p s c).length = c.length := by
  induction s generalizing c with
  | zero => rfl
  | succ s ih => rw [reduceByUpdates_step, ih, cancelByUpdates_length]

theorem cancelStep_read {r m : ℕ} (p : BitWord r) (a : BitWord m)
    (shift : ℕ) (hbound : shift + r < m) (i : Fin m) :
    cancelStep p (shift + r) a i =
      if shift + r = i.val then false else
        Bool.xor (a i)
          (if shift ≤ i.val ∧ i.val < shift + r then
            a ⟨shift + r, hbound⟩ && wordGet p (i.val - shift) else false) := by
  by_cases he : shift + r = i.val
  · rw [ite_eq_left he, he]
    exact cancelStep_at p a i (by omega)
  · have hk : r ≤ shift + r ∧ shift + r < m := ⟨by omega, hbound⟩
    have hsub : shift + r - r = shift := by omega
    by_cases ht : a ⟨shift + r, hbound⟩ = true
    · by_cases hw : shift ≤ i.val ∧ i.val < shift + r
      · have hj : i.val - shift < r := by omega
        simp [cancelStep, hk, ht, wordXor, shiftModulus, hsub,
          he, Ne.symm he, hw, wordGet, hj]
      · simp [cancelStep, hk, ht, wordXor, shiftModulus, hsub,
          he, Ne.symm he, hw]
    · have hf : a ⟨shift + r, hbound⟩ = false := Bool.eq_false_of_not_eq_true ht
      simp [cancelStep, hk, ht, he]

theorem cancelByUpdates_eq_cancelStep {r m : ℕ} (p : BitWord r) (a : BitWord m)
    (shift : ℕ) (hbound : shift + r < m) :
    cancelByUpdates (wordList p) (wordList a) shift =
      wordList (cancelStep p (shift + r) a) := by
  have hw : listWord m (cancelByUpdates (wordList p) (wordList a) shift) =
      cancelStep p (shift + r) a := by
    funext i
    change (cancelByUpdates (wordList p) (wordList a) shift).getD i.val false = _
    rw [cancelByUpdates_getD _ _ _ (by simpa only [wordList_length] using hbound)]
    rw [cancelStep_read p a shift hbound i]
    simp only [wordList_length, wordList_getD, wordGet_fin, wordGet_of_lt a hbound]
  calc
    cancelByUpdates (wordList p) (wordList a) shift =
        wordList (listWord m (cancelByUpdates (wordList p) (wordList a) shift)) :=
      (wordList_listWord _ (by simp)).symm
    _ = wordList (cancelStep p (shift + r) a) := congrArg wordList hw

theorem reduceLoop_below {r m : ℕ} (p : BitWord r) (k : ℕ) (a : BitWord m)
    (hk : k ≤ r) : reduceLoop p k a = a := by
  revert hk
  induction k generalizing a with
  | zero => intro hk; rfl
  | succ k ih =>
      intro hk
      have hskip : ¬(r ≤ k ∧ k < m) := by omega
      simpa only [reduceLoop, cancelStep, dite_eq_right hskip] using ih a (by omega)

theorem reduceByUpdates_word_eq {r m : ℕ} (p : BitWord r) (s : ℕ)
    (a : BitWord m) (hbound : r + s ≤ m) :
    reduceByUpdates (wordList p) s (wordList a) =
      wordList (reduceLoop p (r + s) a) := by
  revert hbound
  induction s generalizing a with
  | zero =>
      intro hbound
      simp only [reduceByUpdates_zero, Nat.add_zero, reduceLoop_below p r a le_rfl]
  | succ s ih =>
      intro hbound
      have hs : s + r < m := by omega
      have hs' : r + s ≤ m := by omega
      have hnext : r + (s + 1) = (r + s) + 1 := by omega
      calc
        reduceByUpdates (wordList p) (s + 1) (wordList a) =
            reduceByUpdates (wordList p) s (wordList (cancelStep p (s + r) a)) := by
          rw [reduceByUpdates_step, cancelByUpdates_eq_cancelStep p a s hs]
        _ = wordList (reduceLoop p (r + s) (cancelStep p (s + r) a)) := ih _ hs'
        _ = wordList (reduceLoop p (r + (s + 1)) a) := by
          rw [hnext, reduceLoop, Nat.add_comm s r]

theorem reduceByUpdates_eq_reduceLoop (p c : List Bool) (s : ℕ)
    (hbound : p.length + s ≤ c.length) :
    reduceByUpdates p s c =
      wordList (reduceLoop (listWord p.length p) (p.length + s) (listWord c.length c)) := by
  simpa only [wordList_listWord_length] using
    reduceByUpdates_word_eq (listWord p.length p) s (listWord c.length c) hbound

theorem reduceByUpdates_tail_false (p c : List Bool) (s : ℕ)
    (hfull : p.length + s = c.length) (i : ℕ) (hi : p.length ≤ i) :
    (reduceByUpdates p s c).getD i false = false := by
  rw [reduceByUpdates_eq_reduceLoop p c s (by omega), hfull, wordList_getD]
  by_cases him : i < c.length
  · rw [wordGet_of_lt _ him]
    exact reduceLoop_tail_false (listWord p.length p) c.length (listWord c.length c)
      (by
        intro j _ hj
        exact False.elim (Nat.not_le.mpr j.is_lt hj)) ⟨i, him⟩ hi
  · exact wordGet_of_le _ (Nat.le_of_not_lt him)

theorem wordList_wordResize_take {r m : ℕ} (a : BitWord m) (hr : r ≤ m) :
    wordList (wordResize r a) = (wordList a).take r := by
  apply List.ext_getElem
  · simp only [wordList_length, List.length_take, Nat.min_eq_left hr]
  · intro i hi hj
    have hir : i < r := by simpa only [wordList_length] using hi
    have hleft : (wordList (wordResize r a)).getD i false = wordGet a i := by
      rw [wordList_getD, wordGet_of_lt _ hir]
      rfl
    have hright : ((wordList a).take r).getD i false = wordGet a i := by
      simpa only [List.getD_eq_getElem?_getD, List.getElem?_take_of_lt hir] using
        wordList_getD a i
    rw [List.getD_eq_getElem _ false hi] at hleft
    rw [List.getD_eq_getElem _ false hj] at hright
    exact hleft.trans hright.symm

theorem reduceByUpdates_take_eq_wordReduce (p c : List Bool)
    (hwidth : c.length = p.length + p.length) :
    (reduceByUpdates p p.length c).take p.length =
      wordList (wordReduce (listWord p.length p) (listWord c.length c)) := by
  rw [reduceByUpdates_eq_reduceLoop p c p.length (by omega), ← hwidth]
  rw [wordReduce, wordFreeze_eq, wordList_wordResize_take _ (by omega)]

theorem reduceByUpdates_convolve_eq_multiplyBitLists (r : ℕ) (p a b : List Bool)
    (hp : p.length = r) (ha : a.length = r) (hb : b.length = r) :
    (reduceByUpdates p r (convolveBitLists a b)).take r = multiplyBitLists r p a b := by
  have hwidth : (convolveBitLists a b).length = p.length + p.length := by
    simp only [convolveBitLists_length, ha, hb, hp]
  have h := reduceByUpdates_take_eq_wordReduce p (convolveBitLists a b) hwidth
  rw [convolveBitLists_length, hp] at h
  rw [convolveBitLists_eq, ha, hb] at h ⊢
  simpa only [listWord_wordList, multiplyBitLists, wordMulMod] using h

theorem reduceByUpdates_convolveByUpdates_eq_multiplyBitLists (r : ℕ)
    (p a b : List Bool) (hp : p.length = r) (ha : a.length = r) (hb : b.length = r) :
    (reduceByUpdates p r (convolveByUpdates a b)).take r = multiplyBitLists r p a b := by
  rw [convolveByUpdates_eq]
  exact reduceByUpdates_convolve_eq_multiplyBitLists r p a b hp ha hb

end DepthThreeLowerBound

end OAI