File size: 6,386 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
import OAI.Computability.DepthThree.LanguageBitLoopInvariants
import Mathlib.Data.List.GetD

namespace OAI

namespace DepthThreeLowerBound

def xorListAt (c : List Bool) (k : ℕ) (v : Bool) : List Bool :=
  c.set k (Bool.xor (c.getD k false) v)

@[simp] theorem xorListAt_length (c : List Bool) (k : ℕ) (v : Bool) :
    (xorListAt c k v).length = c.length := by
  simp [xorListAt]

theorem xorListAt_getD (c : List Bool) (j : ℕ) (v : Bool)
    (hj : j < c.length) (k : ℕ) :
    (xorListAt c j v).getD k false =
      Bool.xor (c.getD k false) (if j = k then v else false) := by
  by_cases h : j = k
  · subst k
    simp [xorListAt, List.getElem?_set_self hj]
  · simp [xorListAt, h, List.getElem?_set_ne h]

theorem xorListAt_split («prefix» suffix : List Bool) (old v : Bool) :
    xorListAt («prefix» ++ old :: suffix) «prefix».length v =
      «prefix» ++ Bool.xor old v :: suffix := by
  unfold xorListAt
  rw [List.getD_append_right «prefix» (old :: suffix) false «prefix».length le_rfl]
  simp

def convolutionUpdateRow (a b : List Bool) (i : ℕ) :
    List ℕ → List Bool → List Bool
  | [], c => c
  | j :: js, c => convolutionUpdateRow a b i js
      (xorListAt c (i + j) (a.getD i false && b.getD j false))

@[simp] theorem convolutionUpdateRow_nil (a b : List Bool) (i : ℕ) (c : List Bool) :
    convolutionUpdateRow a b i [] c = c := rfl

theorem convolutionUpdateRow_step (a b : List Bool) (i j : ℕ)
    (js : List ℕ) (c : List Bool) :
    convolutionUpdateRow a b i (j :: js) c =
      convolutionUpdateRow a b i js
        (xorListAt c (i + j) (a.getD i false && b.getD j false)) := rfl

@[simp] theorem convolutionUpdateRow_length (a b : List Bool) (i : ℕ)
    (js : List ℕ) (c : List Bool) :
    (convolutionUpdateRow a b i js c).length = c.length := by
  induction js generalizing c with
  | nil => rfl
  | cons j js ih =>
      rw [convolutionUpdateRow_step, ih, xorListAt_length]

theorem convolutionUpdateRow_getD (a b : List Bool) (i : ℕ)
    (js : List ℕ) (c : List Bool) (k : ℕ)
    (hbound : ∀ j ∈ js, i + j < c.length) :
    (convolutionUpdateRow a b i js c).getD k false =
      Bool.xor (c.getD k false)
        (xorBits (js.map fun j =>
          if i + j = k then a.getD i false && b.getD j false else false)) := by
  induction js generalizing c with
  | nil => simp
  | cons j js ih =>
      have hj : i + j < c.length := hbound j (by simp)
      have hrest : ∀ j' ∈ js, i + j' <
          (xorListAt c (i + j) (a.getD i false && b.getD j false)).length := by
        intro j' hj'
        rw [xorListAt_length]
        exact hbound j' (List.mem_cons_of_mem j hj')
      rw [convolutionUpdateRow_step, ih _ hrest,
        xorListAt_getD c (i + j) (a.getD i false && b.getD j false) hj k]
      simp only [List.map_cons, xorBits_cons, Bool.xor_assoc]

def convolutionUpdateRows (a b : List Bool) : List ℕ → List Bool → List Bool
  | [], c => c
  | i :: is, c => convolutionUpdateRows a b is
      (convolutionUpdateRow a b i (List.range b.length) c)

@[simp] theorem convolutionUpdateRows_nil (a b : List Bool) (c : List Bool) :
    convolutionUpdateRows a b [] c = c := rfl

theorem convolutionUpdateRows_step (a b : List Bool) (i : ℕ)
    (is : List ℕ) (c : List Bool) :
    convolutionUpdateRows a b (i :: is) c =
      convolutionUpdateRows a b is
        (convolutionUpdateRow a b i (List.range b.length) c) := rfl

@[simp] theorem convolutionUpdateRows_length (a b : List Bool)
    (is : List ℕ) (c : List Bool) :
    (convolutionUpdateRows a b is c).length = c.length := by
  induction is generalizing c with
  | nil => rfl
  | cons i is ih =>
      rw [convolutionUpdateRows_step, ih, convolutionUpdateRow_length]

theorem convolutionUpdateRows_getD (a b : List Bool)
    (is : List ℕ) (c : List Bool) (k : ℕ)
    (hbound : ∀ i ∈ is, ∀ j < b.length, i + j < c.length) :
    (convolutionUpdateRows a b is c).getD k false =
      Bool.xor (c.getD k false)
        (xorBits (is.map fun i => xorBits (convolutionRow a b i k))) := by
  induction is generalizing c with
  | nil => simp
  | cons i is ih =>
      have hcols : ∀ j ∈ List.range b.length, i + j < c.length := by
        intro j hj
        exact hbound i (by simp) j (List.mem_range.mp hj)
      have hrest : ∀ i' ∈ is, ∀ j < b.length, i' + j <
          (convolutionUpdateRow a b i (List.range b.length) c).length := by
        intro i' hi' j hj
        rw [convolutionUpdateRow_length]
        exact hbound i' (List.mem_cons_of_mem i hi') j hj
      rw [convolutionUpdateRows_step, ih _ hrest,
        convolutionUpdateRow_getD a b i (List.range b.length) c k hcols]
      simp only [List.map_cons, xorBits_cons, convolutionRow, Bool.xor_assoc]

def convolveByUpdates (a b : List Bool) : List Bool :=
  convolutionUpdateRows a b (List.range a.length)
    (List.replicate (a.length + b.length) false)

@[simp] theorem convolveByUpdates_length (a b : List Bool) :
    (convolveByUpdates a b).length = a.length + b.length := by
  simp [convolveByUpdates]

theorem convolutionUpdate_index_lt (a b : List Bool) {i j : ℕ}
    (hi : i < a.length) (hj : j < b.length) :
    i + j < a.length + b.length :=
  Nat.add_lt_add hi hj

theorem convolveByUpdates_getD (a b : List Bool) (k : ℕ) :
    (convolveByUpdates a b).getD k false = convolutionBit a b k := by
  have hbound : ∀ i ∈ List.range a.length, ∀ j < b.length,
      i + j < (List.replicate (a.length + b.length) false).length := by
    intro i hi j hj
    simpa only [List.length_replicate] using
      convolutionUpdate_index_lt a b (List.mem_range.mp hi) hj
  unfold convolveByUpdates
  rw [convolutionUpdateRows_getD a b (List.range a.length)
    (List.replicate (a.length + b.length) false) k hbound]
  simp [convolutionBit, convolutionRows]

theorem convolveByUpdates_eq (a b : List Bool) :
    convolveByUpdates a b = convolveBitLists a b := by
  apply List.ext_getElem
  · simp
  · intro k hk hl
    have hk' : k < a.length + b.length := by
      simpa only [convolveBitLists_length] using hl
    have hleft := convolveByUpdates_getD a b k
    have hright : (convolveBitLists a b).getD k false = convolutionBit a b k := by
      simp [convolveBitLists, List.getElem?_range hk']
    rw [List.getD_eq_getElem _ _ hk] at hleft
    rw [List.getD_eq_getElem _ _ hl] at hright
    exact hleft.trans hright.symm

end DepthThreeLowerBound

end OAI