File size: 16,490 Bytes
2f887db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import OAI.Computability.PerfectCompleteness.Decoding.LowerCutRowsLemmas
import OAI.Computability.PerfectCompleteness.Foundations.WholeCutReplayLemmas
import OAI.Computability.PerfectCompleteness.Foundations.WholeReplaySubtreeLemmas

namespace OAI

section

namespace PerfectCompleteness.LowerCutBucketLocality

noncomputable section

open scoped Classical
open RecursiveSpaces DescendantSpaces TreeSourceSpaces HierarchicalArrays
open WholeCutGrouping WholeArraySubtreeSplit

abbrev F2 := ZMod 2

variable {branch : Nat → Nat} {n k m t : Nat}

def rootBuckets (rows repeats : Nat → Nat) :
    {k m : Nat} → (pDown : Path branch k (m + 1)) → m + 1 < k →
      (slots : Slots branch k → Fin t → MixedSupport.Slot) →
      Exterior rows repeats pDown slots →
      LowerCutCallSplit.Assembled rows repeats pDown slots →
        BucketSampler.Direction (rows k) → H slots
  | _, _, .refl _, hproper, _, _, _, _ => False.elim ((Nat.lt_irrefl _) hproper)
  | _, _, .step i pDown, _, slots, exterior, assembled, direction =>
      LowerCutScalar.reconstruct F2 repeats (.step i pDown) (LeafDomain slots)
        (fun terminal => assembled.1 (.inl (direction, terminal))) (exterior.1 direction)

def buckets (rows repeats : Nat → Nat) :
    {n k m : Nat} → (pUp : Path branch n k) → (pDown : Path branch k (m + 1)) →
      m + 1 < k → (slots : Slots branch n → Fin t → MixedSupport.Slot) →
      Exterior rows repeats (pUp.append pDown) slots →
      LowerCutCallSplit.Assembled rows repeats (pUp.append pDown) slots →
        BucketSampler.Direction (rows k) → H (subtreeSlots pUp slots)
  | _, _, _, .refl _, pDown, hproper, slots, exterior, assembled =>
      rootBuckets rows repeats pDown hproper slots exterior assembled
  | _, _, _, .step i pUp, pDown, hproper, slots, exterior, assembled =>
      buckets rows repeats pUp pDown hproper (childSlots slots i) exterior.2.1
        (fun call => assembled.1 (.inr call), assembled.2)

theorem buckets_congr_otherCalls (rows repeats : Nat → Nat) (pUp : Path branch n k) :
    ∀ {m : Nat} (pDown : Path branch k (m + 1)) (hproper : m + 1 < k)
      (slots : Slots branch n → Fin t → MixedSupport.Slot)
      (exterior : Exterior rows repeats (pUp.append pDown) slots)
      (x y : LowerCutCallSplit.Assembled rows repeats (pUp.append pDown) slots),
      (∀ call, (∀ row, call ≠ WholeCutCalls.directRow rows repeats (pUp.append pDown) row) →
        y.1 call = x.1 call) →
      buckets rows repeats pUp pDown hproper slots exterior y =
        buckets rows repeats pUp pDown hproper slots exterior x := by
  induction pUp with
  | refl k =>
      intro m pDown hproper slots exterior x y hcalls
      cases pDown with
      | refl => exact False.elim ((Nat.lt_irrefl _) hproper)
      | step i pDown =>
          funext direction
          have hvalues :
              ((fun terminal => y.1 (.inl (direction, terminal))) :
                LowerCutScalar.Values F2 repeats (.step i pDown) (LeafDomain slots)) =
              (fun terminal => x.1 (.inl (direction, terminal))) := by
            funext terminal
            apply hcalls (.inl (direction, terminal))
            intro row h
            cases h
          simp only [buckets, rootBuckets]
          rw [hvalues]
  | step i pUp ih =>
      intro m pDown hproper slots exterior x y hcalls
      apply ih pDown hproper (childSlots slots i) exterior.2.1
        (fun call => x.1 (.inr call), x.2) (fun call => y.1 (.inr call), y.2)
      intro call hcall
      apply hcalls (.inr call)
      intro row h
      exact hcall row (Sum.inr.inj h)

theorem buckets_splitTape (rows repeats : Nat → Nat) (pUp : Path branch n k) :
    ∀ {m : Nat} (pDown : Path branch k (m + 1)) (hproper : m + 1 < k)
      (slots : Slots branch n → Fin t → MixedSupport.Slot)
      (tape : WholeCutSampler.Tape rows repeats (pUp.append pDown) slots),
      buckets rows repeats pUp pDown hproper slots
          (splitTape rows repeats (pUp.append pDown) slots tape).1
          (CutChildGrouping.assemble (cutSlots (pUp.append pDown) slots) rows
            (splitTape rows repeats (pUp.append pDown) slots tape).2) =
        WholeCutSubtree.rootBuckets rows repeats pDown hproper (subtreeSlots pUp slots)
          (WholeCutSubtree.extract rows repeats pUp pDown slots tape) := by
  induction pUp with
  | refl k =>
      intro m pDown hproper slots tape
      cases pDown with
      | refl => exact False.elim ((Nat.lt_irrefl _) hproper)
      | step i pDown =>
          funext direction
          exact LowerCutScalar.reconstruct_splitTape F2 repeats (.step i pDown)
            (LeafDomain slots) (tape.1 direction)
  | step i pUp ih =>
      intro m pDown hproper slots tape
      exact ih pDown hproper (childSlots slots i) tape.2.1

def knownBuckets (rows repeats : Nat → Nat)
    (slots : Slots branch n → Fin t → MixedSupport.Slot) (upper : Nodes branch n)
    (pDown : Path branch (Nodes.height upper) (m + 1))
    (hproper : m + 1 < Nodes.height upper)
    (W : Submodule F2 (OwnInputReference.UpperVector rows upper))
    (exterior : Exterior rows repeats ((Nodes.path upper).append pDown) slots)
    (assembled : LowerCutCallSplit.Assembled rows repeats ((Nodes.path upper).append pDown) slots) :
    HiddenBucketBias.VisibleDirection W → OwnInputReference.UpperSpace slots upper :=
  fun v => buckets rows repeats (Nodes.path upper) pDown hproper slots exterior assembled v.val

def knownBucketsOfComplement (rows repeats : Nat → Nat)
    (slots : Slots branch n → Fin t → MixedSupport.Slot) (upper : Nodes branch n)
    (pDown : Path branch (Nodes.height upper) (m + 1))
    (hproper : m + 1 < Nodes.height upper)
    (W : Submodule F2 (OwnInputReference.UpperVector rows upper))
    (exterior : Exterior rows repeats ((Nodes.path upper).append pDown) slots)
    (complement : LowerCutCallSplit.Complement rows repeats ((Nodes.path upper).append pDown) slots) :
    HiddenBucketBias.VisibleDirection W → OwnInputReference.UpperSpace slots upper :=
  knownBuckets rows repeats slots upper pDown hproper W exterior
    (LowerCutCallSplit.merge rows repeats ((Nodes.path upper).append pDown) slots complement 0)

theorem knownBuckets_merge (rows repeats : Nat → Nat)
    (slots : Slots branch n → Fin t → MixedSupport.Slot) (upper : Nodes branch n)
    (pDown : Path branch (Nodes.height upper) (m + 1))
    (hproper : m + 1 < Nodes.height upper)
    (W : Submodule F2 (OwnInputReference.UpperVector rows upper))
    (exterior : Exterior rows repeats ((Nodes.path upper).append pDown) slots)
    (complement : LowerCutCallSplit.Complement rows repeats ((Nodes.path upper).append pDown) slots)
    (fresh : LowerCutCallSplit.NativeRows rows ((Nodes.path upper).append pDown) slots) :
    knownBuckets rows repeats slots upper pDown hproper W exterior
        (LowerCutCallSplit.merge rows repeats ((Nodes.path upper).append pDown) slots complement fresh) =
      knownBucketsOfComplement rows repeats slots upper pDown hproper W exterior complement := by
  have h := buckets_congr_otherCalls rows repeats (Nodes.path upper) pDown hproper slots exterior
    (LowerCutCallSplit.merge rows repeats ((Nodes.path upper).append pDown) slots complement 0)
    (LowerCutCallSplit.merge rows repeats ((Nodes.path upper).append pDown) slots complement fresh)
    (fun call hcall =>
      (LowerCutCallSplit.merge_otherCall rows repeats ((Nodes.path upper).append pDown) slots
        complement fresh ⟨call, hcall⟩).trans
      (LowerCutCallSplit.merge_otherCall rows repeats ((Nodes.path upper).append pDown) slots
        complement 0 ⟨call, hcall⟩).symm)
  funext v
  exact congrFun h v.val

theorem knownBuckets_eq_complement (rows repeats : Nat → Nat)
    (slots : Slots branch n → Fin t → MixedSupport.Slot) (upper : Nodes branch n)
    (pDown : Path branch (Nodes.height upper) (m + 1))
    (hproper : m + 1 < Nodes.height upper)
    (W : Submodule F2 (OwnInputReference.UpperVector rows upper))
    (exterior : Exterior rows repeats ((Nodes.path upper).append pDown) slots)
    (assembled : LowerCutCallSplit.Assembled rows repeats ((Nodes.path upper).append pDown) slots) :
    knownBuckets rows repeats slots upper pDown hproper W exterior assembled =
      knownBucketsOfComplement rows repeats slots upper pDown hproper W exterior
        (LowerCutCallSplit.complement rows repeats ((Nodes.path upper).append pDown) slots assembled) := by
  have h := buckets_congr_otherCalls rows repeats (Nodes.path upper) pDown hproper slots exterior
    (LowerCutCallSplit.merge rows repeats ((Nodes.path upper).append pDown) slots
      (LowerCutCallSplit.complement rows repeats ((Nodes.path upper).append pDown) slots assembled) 0)
    assembled (fun call hcall =>
      (LowerCutCallSplit.merge_otherCall rows repeats ((Nodes.path upper).append pDown) slots
        (LowerCutCallSplit.complement rows repeats ((Nodes.path upper).append pDown) slots assembled)
        0 ⟨call, hcall⟩).symm)
  funext v
  exact congrFun h v.val

theorem knownBucketsAtNode_eq_complement (rows repeats : Nat → Nat)
    (slots : Slots branch n → Fin t → MixedSupport.Slot) (upper : Nodes branch n)
    (pDown : Path branch (Nodes.height upper) (m + 1))
    (hproper : m + 1 < Nodes.height upper)
    (W : Submodule F2 (OwnInputReference.UpperVector rows upper))
    (tape : WholeCutSampler.Tape rows repeats ((Nodes.path upper).append pDown) slots) :
    WholeCutSubtree.knownBucketsAtNode rows repeats slots upper pDown hproper W tape =
      knownBucketsOfComplement rows repeats slots upper pDown hproper W
        (splitTape rows repeats ((Nodes.path upper).append pDown) slots tape).1
        (LowerCutCallSplit.complement rows repeats ((Nodes.path upper).append pDown) slots
          (CutChildGrouping.assemble (cutSlots ((Nodes.path upper).append pDown) slots) rows
            (splitTape rows repeats ((Nodes.path upper).append pDown) slots tape).2)) := by
  have hreader := buckets_splitTape rows repeats (Nodes.path upper) pDown hproper slots tape
  have hknown : WholeCutSubtree.knownBucketsAtNode rows repeats slots upper pDown hproper W tape =
      knownBuckets rows repeats slots upper pDown hproper W
        (splitTape rows repeats ((Nodes.path upper).append pDown) slots tape).1
        (CutChildGrouping.assemble (cutSlots ((Nodes.path upper).append pDown) slots) rows
          (splitTape rows repeats ((Nodes.path upper).append pDown) slots tape).2) := by
    funext v
    exact (congrFun hreader v.val).symm
  exact hknown.trans (knownBuckets_eq_complement rows repeats slots upper pDown hproper W _ _)

theorem knownBucketsAtNode_join (rows repeats : Nat → Nat)
    (slots : Slots branch n → Fin t → MixedSupport.Slot) (upper : Nodes branch n)
    (pDown : Path branch (Nodes.height upper) (m + 1))
    (hproper : m + 1 < Nodes.height upper)
    (W : Submodule F2 (OwnInputReference.UpperVector rows upper))
    (exterior : Exterior rows repeats ((Nodes.path upper).append pDown) slots)
    (raw : CutChildGrouping.Raw (C := WholeCutCalls.Index rows repeats ((Nodes.path upper).append pDown))
      (cutSlots ((Nodes.path upper).append pDown) slots) rows) :
    WholeCutSubtree.knownBucketsAtNode rows repeats slots upper pDown hproper W
        ((splitTape rows repeats ((Nodes.path upper).append pDown) slots).symm (exterior, raw)) =
      knownBucketsOfComplement rows repeats slots upper pDown hproper W exterior
        (LowerCutCallSplit.complement rows repeats ((Nodes.path upper).append pDown) slots
          (CutChildGrouping.assemble (cutSlots ((Nodes.path upper).append pDown) slots) rows raw)) := by
  have h := knownBucketsAtNode_eq_complement rows repeats slots upper pDown hproper W
    ((splitTape rows repeats ((Nodes.path upper).append pDown) slots).symm (exterior, raw))
  rw [Equiv.apply_symm_apply] at h
  exact h

theorem knownBucketsAtNode_erase_eq (rows repeats : Nat → Nat)
    (slots : Slots branch n → Fin t → MixedSupport.Slot) (upper : Nodes branch n)
    (pDown : Path branch (Nodes.height upper) (m + 1))
    (hproper : m + 1 < Nodes.height upper) (clean : Fin (branch m) → Prop)
    (W : Submodule F2 (OwnInputReference.UpperVector rows upper))
    (tape : WholeCutSampler.Tape rows repeats ((Nodes.path upper).append pDown) slots)
    (hzero : WholeCutZero.ZeroAtClean rows repeats ((Nodes.path upper).append pDown) slots clean tape) :
    WholeReplaySubtree.knownBucketsAtNode rows repeats slots upper pDown hproper clean W
        (WholeCutReplay.erase rows repeats ((Nodes.path upper).append pDown) slots clean tape) =
      knownBucketsOfComplement rows repeats slots upper pDown hproper W
        (splitTape rows repeats ((Nodes.path upper).append pDown) slots tape).1
        (LowerCutCallSplit.complement rows repeats ((Nodes.path upper).append pDown) slots
          (CutChildGrouping.assemble (cutSlots ((Nodes.path upper).append pDown) slots) rows
            (splitTape rows repeats ((Nodes.path upper).append pDown) slots tape).2)) :=
  (WholeCutSubtree.knownBucketsAtNode_erase rows repeats slots upper pDown hproper clean W tape hzero).trans
    (knownBucketsAtNode_eq_complement rows repeats slots upper pDown hproper W tape)

theorem knownBucketsAtNode_assemble_splitTape (rows repeats : Nat → Nat)
    (slots : Slots branch n → Fin t → MixedSupport.Slot) (upper : Nodes branch n)
    (pDown : Path branch (Nodes.height upper) (m + 1))
    (hproper : m + 1 < Nodes.height upper) (clean : Fin (branch m) → Prop)
    (W : Submodule F2 (OwnInputReference.UpperVector rows upper))
    (tape : WholeCutSampler.Tape rows repeats ((Nodes.path upper).append pDown) slots)
    (hzero : WholeCutZero.ZeroAtClean rows repeats ((Nodes.path upper).append pDown) slots clean tape) :
    WholeReplaySubtree.knownBucketsAtNode rows repeats slots upper pDown hproper clean W
        (WholeCutReplayGrouping.assemble rows repeats ((Nodes.path upper).append pDown) slots clean
          (splitTape rows repeats ((Nodes.path upper).append pDown) slots tape).1
          (fun child => (splitTape rows repeats ((Nodes.path upper).append pDown) slots tape).2 child.val)) =
      knownBucketsOfComplement rows repeats slots upper pDown hproper W
        (splitTape rows repeats ((Nodes.path upper).append pDown) slots tape).1
        (LowerCutCallSplit.complement rows repeats ((Nodes.path upper).append pDown) slots
          (CutChildGrouping.assemble (cutSlots ((Nodes.path upper).append pDown) slots) rows
            (splitTape rows repeats ((Nodes.path upper).append pDown) slots tape).2)) := by
  exact (congrArg (WholeReplaySubtree.knownBucketsAtNode rows repeats slots upper pDown hproper clean W)
    (WholeCutReplayGrouping.assemble_splitTape rows repeats ((Nodes.path upper).append pDown)
      slots clean tape)).trans
    (knownBucketsAtNode_erase_eq rows repeats slots upper pDown hproper clean W tape hzero)

theorem knownBucketsAtNode_assemble (rows repeats : Nat → Nat)
    (slots : Slots branch n → Fin t → MixedSupport.Slot) (upper : Nodes branch n)
    (pDown : Path branch (Nodes.height upper) (m + 1))
    (hproper : m + 1 < Nodes.height upper) (clean : Fin (branch m) → Prop)
    (W : Submodule F2 (OwnInputReference.UpperVector rows upper))
    (exterior : Exterior rows repeats ((Nodes.path upper).append pDown) slots)
    (raw : CutChildGrouping.Raw (C := WholeCutCalls.Index rows repeats ((Nodes.path upper).append pDown))
      (cutSlots ((Nodes.path upper).append pDown) slots) rows)
    (hzero : WholeCutZero.ZeroAtClean rows repeats ((Nodes.path upper).append pDown) slots clean
      ((splitTape rows repeats ((Nodes.path upper).append pDown) slots).symm (exterior, raw))) :
    WholeReplaySubtree.knownBucketsAtNode rows repeats slots upper pDown hproper clean W
        (WholeCutReplayGrouping.assemble rows repeats ((Nodes.path upper).append pDown) slots clean
          exterior (fun child => raw child.val)) =
      knownBucketsOfComplement rows repeats slots upper pDown hproper W exterior
        (LowerCutCallSplit.complement rows repeats ((Nodes.path upper).append pDown) slots
          (CutChildGrouping.assemble (cutSlots ((Nodes.path upper).append pDown) slots) rows raw)) := by
  have h := knownBucketsAtNode_assemble_splitTape rows repeats slots upper pDown hproper clean W
    ((splitTape rows repeats ((Nodes.path upper).append pDown) slots).symm (exterior, raw)) hzero
  rw [Equiv.apply_symm_apply] at h
  exact h

end
end PerfectCompleteness.LowerCutBucketLocality

end

end OAI