File size: 26,763 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
import OAI.Computability.PerfectCompleteness.Algebra.HierarchicalMatrixTable
import OAI.Computability.PerfectCompleteness.Construction.OriginalCutBucketReplacement
import OAI.Computability.PerfectCompleteness.Sampling.CutSamplerReplayTransportLemmas
import OAI.Computability.PerfectCompleteness.Sampling.OriginalWholeCutLaw

namespace OAI

section

namespace PerfectCompleteness.OriginalOwnBucketSplit

open RecursiveSpaces DescendantSpaces TreeSourceSpaces HierarchicalArrays TerminalCalls
open OriginalWholeCut OriginalWholeCutTape
open UniqueGamesTheorem.Foundations.Games
open scoped BigOperators Classical

noncomputable section

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

@[instance_reducible] def valuesChildrenFintype (C : Type*) [Fintype C]
    (slots : Slots branch (m + 1) → Fin t → MixedSupport.Slot) (rows : Nat → Nat) :
    Fintype ((C → H slots) × ChildArrays rows slots) :=
  @instFintypeProd (C → H slots) (ChildArrays rows slots) inferInstance inferInstance

attribute [local instance 2000] valuesChildrenFintype

def OtherIndex (rows repeats : Nat → Nat) : {n m : Nat} → Path branch n m → Type
  | _, _, .refl _ => Empty
  | n + 1, _, .step i p =>
      (BucketSampler.Direction (rows (n + 1)) × TerminalIndex repeats (.step i p)) ⊕
        OtherIndex rows repeats p

instance otherIndexFintype (rows repeats : Nat → Nat) (p : Path branch n m) :
    Fintype (OtherIndex rows repeats p) := by
  induction p with
  | refl m => exact inferInstanceAs (Fintype Empty)
  | @step n m i p ih =>
      letI : Fintype (OtherIndex rows repeats p) := ih
      exact inferInstanceAs (Fintype
        ((BucketSampler.Direction (rows (n + 1)) × TerminalIndex repeats (.step i p)) ⊕
          OtherIndex rows repeats p))

private def sumEmpty (A : Type*) : A ≃ A ⊕ Empty where
  toFun := Sum.inl
  invFun := Sum.elim id Empty.elim
  left_inv _ := rfl
  right_inv x := by
    cases x with
    | inl a => rfl
    | inr e => exact Empty.elim e

private def swapFirstSum (R A O : Type*) : R ⊕ (A ⊕ O) ≃ A ⊕ (R ⊕ O) where
  toFun
    | .inl r => .inr (.inl r)
    | .inr (.inl a) => .inl a
    | .inr (.inr o) => .inr (.inr o)
  invFun
    | .inl a => .inr (.inl a)
    | .inr (.inl r) => .inl r
    | .inr (.inr o) => .inr (.inr o)
  left_inv x := by rcases x with r | (a | o) <;> rfl
  right_inv x := by rcases x with a | (r | o) <;> rfl

def indexEquiv (rows repeats : Nat → Nat) : {n m : Nat} → (p : Path branch n m) →
    OriginalCutCalls.Index rows repeats p ≃
      BucketSampler.Direction (rows m) ⊕ OtherIndex rows repeats p
  | _, _, .refl _ => sumEmpty _
  | n + 1, _, .step i p =>
      (Equiv.sumCongr
        (Equiv.refl (BucketSampler.Direction (rows (n + 1)) ×
          TerminalIndex repeats (.step i p)))
        (indexEquiv rows repeats p)).trans (swapFirstSum _ _ _)

@[simp] theorem indexEquiv_ownBucket (rows repeats : Nat → Nat)
    (p : Path branch n m) (a : BucketSampler.Direction (rows m)) :
    indexEquiv rows repeats p (OriginalCutCalls.ownBucket rows repeats p a) = .inl a := by
  revert a
  induction p with
  | refl m => intro a; rfl
  | @step n m i p ih =>
      intro a
      change swapFirstSum _ _ _
        (.inr (indexEquiv rows repeats p (OriginalCutCalls.ownBucket rows repeats p a))) = _
      rw [ih a]
      rfl

@[simp] theorem indexEquiv_symm_inl (rows repeats : Nat → Nat)
    (p : Path branch n m) (a : BucketSampler.Direction (rows m)) :
    (indexEquiv rows repeats p).symm (.inl a) =
      OriginalCutCalls.ownBucket rows repeats p a := by
  apply (indexEquiv rows repeats p).injective
  rw [Equiv.apply_symm_apply, indexEquiv_ownBucket]

def otherCall (rows repeats : Nat → Nat) (p : Path branch n m)
    (call : OtherIndex rows repeats p) : OriginalCutCalls.Index rows repeats p :=
  (indexEquiv rows repeats p).symm (.inr call)

@[simp] theorem indexEquiv_otherCall (rows repeats : Nat → Nat)
    (p : Path branch n m) (call : OtherIndex rows repeats p) :
    indexEquiv rows repeats p (otherCall rows repeats p call) = .inr call :=
  (indexEquiv rows repeats p).apply_symm_apply (.inr call)

theorem otherCall_injective (rows repeats : Nat → Nat) (p : Path branch n m) :
    Function.Injective (otherCall rows repeats p) := by
  intro a b h
  have h' := congrArg (indexEquiv rows repeats p) h
  simpa only [indexEquiv_otherCall, Sum.inr.injEq] using h'

theorem otherCall_ne_ownBucket (rows repeats : Nat → Nat) (p : Path branch n m)
    (call : OtherIndex rows repeats p) (a : BucketSampler.Direction (rows m)) :
    otherCall rows repeats p call ≠ OriginalCutCalls.ownBucket rows repeats p a := by
  intro h
  have h' := congrArg (indexEquiv rows repeats p) h
  rw [indexEquiv_otherCall, indexEquiv_ownBucket] at h'
  cases h'

def valuesEquiv (rows repeats : Nat → Nat) (p : Path branch n m) (A : Type*) :
    (OriginalCutCalls.Index rows repeats p → A) ≃
      (BucketSampler.Direction (rows m) → A) × (OtherIndex rows repeats p → A) :=
  (Equiv.arrowCongr (indexEquiv rows repeats p) (Equiv.refl A)).trans
    (Equiv.sumArrowEquivProdArrow _ _ _)

@[simp] theorem valuesEquiv_own (rows repeats : Nat → Nat) (p : Path branch n m)
    (A : Type*) (values : OriginalCutCalls.Index rows repeats p → A)
    (a : BucketSampler.Direction (rows m)) :
    (valuesEquiv rows repeats p A values).1 a =
      values (OriginalCutCalls.ownBucket rows repeats p a) := by
  change values ((indexEquiv rows repeats p).symm (.inl a)) = _
  rw [indexEquiv_symm_inl]

@[simp] theorem valuesEquiv_other (rows repeats : Nat → Nat) (p : Path branch n m)
    (A : Type*) (values : OriginalCutCalls.Index rows repeats p → A)
    (call : OtherIndex rows repeats p) :
    (valuesEquiv rows repeats p A values).2 call =
      values (otherCall rows repeats p call) := rfl

@[simp] theorem valuesEquiv_symm_own (rows repeats : Nat → Nat) (p : Path branch n m)
    (A : Type*) (own : BucketSampler.Direction (rows m) → A)
    (other : OtherIndex rows repeats p → A) (a : BucketSampler.Direction (rows m)) :
    (valuesEquiv rows repeats p A).symm (own, other)
        (OriginalCutCalls.ownBucket rows repeats p a) = own a := by
  have h := congrArg (fun z => z.1 a)
    ((valuesEquiv rows repeats p A).apply_symm_apply (own, other))
  simpa only [valuesEquiv_own] using h

@[simp] theorem valuesEquiv_symm_other (rows repeats : Nat → Nat) (p : Path branch n m)
    (A : Type*) (own : BucketSampler.Direction (rows m) → A)
    (other : OtherIndex rows repeats p → A) (call : OtherIndex rows repeats p) :
    (valuesEquiv rows repeats p A).symm (own, other)
        (otherCall rows repeats p call) = other call := by
  have h := congrArg (fun z => z.2 call)
    ((valuesEquiv rows repeats p A).apply_symm_apply (own, other))
  simpa only [valuesEquiv_other] using h

theorem valuesEquiv_law (rows repeats : Nat → Nat) (p : Path branch n m)
    {A : Type*} [Fintype A] (μ : FiniteDistribution A) :
    (FiniteProduct.law (fun _ : OriginalCutCalls.Index rows repeats p => μ)).pushforward
        (valuesEquiv rows repeats p A) =
      (FiniteProduct.law (fun _ : BucketSampler.Direction (rows m) => μ)).product
        (FiniteProduct.law (fun _ : OtherIndex rows repeats p => μ)) := by
  rw [← FiniteDistribution.transport_eq_pushforward]
  apply FiniteDistribution.eq_of_weight_eq
  intro z
  change (∏ call : OriginalCutCalls.Index rows repeats p,
      μ.weight (Sum.elim z.1 z.2 (indexEquiv rows repeats p call))) =
    (∏ a : BucketSampler.Direction (rows m), μ.weight (z.1 a)) *
      ∏ other : OtherIndex rows repeats p, μ.weight (z.2 other)
  rw [(indexEquiv rows repeats p).prod_comp
    (fun tag => μ.weight (Sum.elim z.1 z.2 tag)), Fintype.prod_sum_type]
  simp only [Sum.elim_inl, Sum.elim_inr]

abbrev BucketTape (rows : Nat → Nat) (p : Path branch n (m + 1))
    (slots : Slots branch n → Fin t → MixedSupport.Slot) :=
  BucketSampler.Tape (rows (m + 1)) (H (cutSlots p slots))

abbrev OtherValues (rows repeats : Nat → Nat) (p : Path branch n (m + 1))
    (slots : Slots branch n → Fin t → MixedSupport.Slot) :=
  OtherIndex rows repeats p → H (cutSlots p slots)

abbrev Complement (rows repeats : Nat → Nat) (p : Path branch n (m + 1))
    (slots : Slots branch n → Fin t → MixedSupport.Slot) :=
  Exterior rows repeats p slots × (OtherValues rows repeats p slots × BelowArrays rows p slots)

def recordEquiv (rows repeats : Nat → Nat) (p : Path branch n (m + 1))
    (slots : Slots branch n → Fin t → MixedSupport.Slot) :
    Record rows repeats p slots ≃ BucketTape rows p slots × Complement rows repeats p slots where
  toFun record :=
    ((valuesEquiv rows repeats p (H (cutSlots p slots)) record.2.1).1,
      (record.1,
        ((valuesEquiv rows repeats p (H (cutSlots p slots)) record.2.1).2, record.2.2)))
  invFun z :=
    (z.2.1, ((valuesEquiv rows repeats p (H (cutSlots p slots))).symm (z.1, z.2.2.1),
      z.2.2.2))
  left_inv record := by
    rcases record with ⟨exterior, values, below⟩
    change (exterior, ((valuesEquiv rows repeats p (H (cutSlots p slots))).symm
      (valuesEquiv rows repeats p (H (cutSlots p slots)) values), below)) = _
    rw [Equiv.symm_apply_apply]
  right_inv z := by
    rcases z with ⟨own, exterior, other, below⟩
    change
      (((valuesEquiv rows repeats p (H (cutSlots p slots)))
          ((valuesEquiv rows repeats p (H (cutSlots p slots))).symm (own, other))).1,
        (exterior,
          (((valuesEquiv rows repeats p (H (cutSlots p slots)))
            ((valuesEquiv rows repeats p (H (cutSlots p slots))).symm (own, other))).2,
            below))) = _
    rw [Equiv.apply_symm_apply]

def assembleRecord (rows repeats : Nat → Nat) (p : Path branch n (m + 1))
    (slots : Slots branch n → Fin t → MixedSupport.Slot)
    (own : BucketTape rows p slots) (complement : Complement rows repeats p slots) :
    Record rows repeats p slots :=
  (recordEquiv rows repeats p slots).symm (own, complement)

@[simp] theorem recordEquiv_own (rows repeats : Nat → Nat)
    (p : Path branch n (m + 1)) (slots : Slots branch n → Fin t → MixedSupport.Slot)
    (record : Record rows repeats p slots) (a : BucketSampler.Direction (rows (m + 1))) :
    (recordEquiv rows repeats p slots record).1 a =
      record.2.1 (OriginalCutCalls.ownBucket rows repeats p a) :=
  valuesEquiv_own rows repeats p (H (cutSlots p slots)) record.2.1 a

@[simp] theorem split_assembleRecord (rows repeats : Nat → Nat)
    (p : Path branch n (m + 1)) (slots : Slots branch n → Fin t → MixedSupport.Slot)
    (own : BucketTape rows p slots) (complement : Complement rows repeats p slots) :
    recordEquiv rows repeats p slots (assembleRecord rows repeats p slots own complement) =
      (own, complement) :=
  (recordEquiv rows repeats p slots).apply_symm_apply (own, complement)

@[simp] theorem assembleRecord_split (rows repeats : Nat → Nat)
    (p : Path branch n (m + 1)) (slots : Slots branch n → Fin t → MixedSupport.Slot)
    (record : Record rows repeats p slots) :
    assembleRecord rows repeats p slots (recordEquiv rows repeats p slots record).1
        (recordEquiv rows repeats p slots record).2 = record :=
  (recordEquiv rows repeats p slots).symm_apply_apply record

@[simp] theorem assembleRecord_exterior (rows repeats : Nat → Nat)
    (p : Path branch n (m + 1)) (slots : Slots branch n → Fin t → MixedSupport.Slot)
    (own : BucketTape rows p slots) (complement : Complement rows repeats p slots) :
    (assembleRecord rows repeats p slots own complement).1 = complement.1 := rfl

@[simp] theorem assembleRecord_below (rows repeats : Nat → Nat)
    (p : Path branch n (m + 1)) (slots : Slots branch n → Fin t → MixedSupport.Slot)
    (own : BucketTape rows p slots) (complement : Complement rows repeats p slots) :
    (assembleRecord rows repeats p slots own complement).2.2 = complement.2.2 := rfl

@[simp] theorem assembleRecord_own (rows repeats : Nat → Nat)
    (p : Path branch n (m + 1)) (slots : Slots branch n → Fin t → MixedSupport.Slot)
    (own : BucketTape rows p slots) (complement : Complement rows repeats p slots)
    (a : BucketSampler.Direction (rows (m + 1))) :
    (assembleRecord rows repeats p slots own complement).2.1
        (OriginalCutCalls.ownBucket rows repeats p a) = own a :=
  valuesEquiv_symm_own rows repeats p (H (cutSlots p slots)) own complement.2.1 a

@[simp] theorem assembleRecord_other (rows repeats : Nat → Nat)
    (p : Path branch n (m + 1)) (slots : Slots branch n → Fin t → MixedSupport.Slot)
    (own : BucketTape rows p slots) (complement : Complement rows repeats p slots)
    (call : OtherIndex rows repeats p) :
    (assembleRecord rows repeats p slots own complement).2.1 (otherCall rows repeats p call) =
      complement.2.1 call :=
  valuesEquiv_symm_other rows repeats p (H (cutSlots p slots)) own complement.2.1 call

theorem assembleRecord_nonown_eq (rows repeats : Nat → Nat)
    (p : Path branch n (m + 1)) (slots : Slots branch n → Fin t → MixedSupport.Slot)
    (own₁ own₂ : BucketTape rows p slots) (complement : Complement rows repeats p slots)
    (call : OriginalCutCalls.Index rows repeats p)
    (hcall : ∀ a, call ≠ OriginalCutCalls.ownBucket rows repeats p a) :
    (assembleRecord rows repeats p slots own₁ complement).2.1 call =
      (assembleRecord rows repeats p slots own₂ complement).2.1 call := by
  cases he : indexEquiv rows repeats p call with
  | inl a =>
      exfalso
      apply hcall a
      apply (indexEquiv rows repeats p).injective
      rw [indexEquiv_ownBucket]
      exact he
  | inr other =>
      have hc : call = otherCall rows repeats p other := by
        apply (indexEquiv rows repeats p).injective
        rw [indexEquiv_otherCall]
        exact he
      rw [hc, assembleRecord_other, assembleRecord_other]

def complementLaw (rows repeats : Nat → Nat) (p : Path branch n (m + 1))
    (slots : Slots branch n → Fin t → MixedSupport.Slot)
    (scalar : FiniteDistribution (H (cutSlots p slots)))
    (exterior : FiniteDistribution (Exterior rows repeats p slots))
    (below : FiniteDistribution (BelowArrays rows p slots)) :
    FiniteDistribution (Complement rows repeats p slots) :=
  exterior.product ((FiniteProduct.law
    (fun _ : OtherIndex rows repeats p => scalar)).product below)

theorem independent_record_split (rows repeats : Nat → Nat)
    (p : Path branch n (m + 1)) (slots : Slots branch n → Fin t → MixedSupport.Slot)
    (scalar : FiniteDistribution (H (cutSlots p slots)))
    (exterior : FiniteDistribution (Exterior rows repeats p slots))
    (below : FiniteDistribution (BelowArrays rows p slots)) :
    (exterior.product ((FiniteProduct.law
        (fun _ : OriginalCutCalls.Index rows repeats p => scalar)).product below)).pushforward
          (recordEquiv rows repeats p slots) =
      (BucketSampler.tapeLaw (rows (m + 1)) scalar).product
        (complementLaw rows repeats p slots scalar exterior below) := by
  rw [← FiniteDistribution.transport_eq_pushforward]
  apply FiniteDistribution.eq_of_weight_eq
  intro z
  change exterior.weight z.2.1 *
      ((∏ call : OriginalCutCalls.Index rows repeats p,
        scalar.weight (Sum.elim z.1 z.2.2.1 (indexEquiv rows repeats p call))) *
          below.weight z.2.2.2) =
    (∏ a : BucketSampler.Direction (rows (m + 1)), scalar.weight (z.1 a)) *
      (exterior.weight z.2.1 *
        ((∏ other : OtherIndex rows repeats p, scalar.weight (z.2.2.1 other)) *
          below.weight z.2.2.2))
  rw [(indexEquiv rows repeats p).prod_comp
    (fun tag => scalar.weight (Sum.elim z.1 z.2.2.1 tag)), Fintype.prod_sum_type]
  simp only [Sum.elim_inl, Sum.elim_inr]
  ring

def actualComplementLaw (rows repeats : Nat → Nat) (p : Path branch n (m + 1))
    (chosen : Fin (branch m)) (q : Path branch m k)
    (slots : Slots branch n → Fin t → MixedSupport.Slot) :
    FiniteDistribution (Complement rows repeats p slots) :=
  complementLaw rows repeats p slots
    (RecursiveSampler.law F2 repeats (.step chosen q) (LeafDomain (cutSlots p slots)))
    (exteriorLaw rows repeats p slots)
    (OriginalWholeCutLaw.belowArraysLaw rows repeats chosen q (cutSlots p slots))

theorem recordLaw_split (rows repeats : Nat → Nat) (p : Path branch n (m + 1))
    (chosen : Fin (branch m)) (q : Path branch m k)
    (slots : Slots branch n → Fin t → MixedSupport.Slot) :
    (OriginalWholeCutLaw.recordLaw rows repeats p chosen q slots).pushforward
        (recordEquiv rows repeats p slots) =
      (BucketSampler.tapeLaw (rows (m + 1))
        (RecursiveSampler.law F2 repeats (.step chosen q) (LeafDomain (cutSlots p slots)))).product
          (actualComplementLaw rows repeats p chosen q slots) :=
  independent_record_split rows repeats p slots
    (RecursiveSampler.law F2 repeats (.step chosen q) (LeafDomain (cutSlots p slots)))
    (exteriorLaw rows repeats p slots)
    (OriginalWholeCutLaw.belowArraysLaw rows repeats chosen q (cutSlots p slots))

theorem uniform_record_split (rows repeats : Nat → Nat)
    (p : Path branch n (m + 1)) (slots : Slots branch n → Fin t → MixedSupport.Slot) :
    (FiniteDistribution.uniform (Record rows repeats p slots)).pushforward
        (recordEquiv rows repeats p slots) =
      (FiniteDistribution.uniform (BucketTape rows p slots)).product
        (FiniteDistribution.uniform (Complement rows repeats p slots)) := by
  rw [← FiniteDistribution.transport_eq_pushforward,
    UniformConditioning.uniform_transport, WholeCutSampler.uniform_product]

theorem uniform_record_split_bucketLaw (rows repeats : Nat → Nat)
    (p : Path branch n (m + 1)) (slots : Slots branch n → Fin t → MixedSupport.Slot) :
    (FiniteDistribution.uniform (Record rows repeats p slots)).pushforward
        (recordEquiv rows repeats p slots) =
      (BucketSampler.tapeLaw (rows (m + 1))
        (FiniteDistribution.uniform (H (cutSlots p slots)))).product
          (FiniteDistribution.uniform (Complement rows repeats p slots)) := by
  rw [BucketSampler.tapeLaw, UniformLinearImage.law_uniform]
  exact uniform_record_split rows repeats p slots

theorem uniform_assembleRecord (rows repeats : Nat → Nat)
    (p : Path branch n (m + 1)) (slots : Slots branch n → Fin t → MixedSupport.Slot) :
    ((FiniteDistribution.uniform (BucketTape rows p slots)).product
        (FiniteDistribution.uniform (Complement rows repeats p slots))).pushforward
          (fun z => assembleRecord rows repeats p slots z.1 z.2) =
      FiniteDistribution.uniform (Record rows repeats p slots) := by
  rw [WholeCutSampler.uniform_product]
  change (FiniteDistribution.uniform
    (BucketTape rows p slots × Complement rows repeats p slots)).pushforward
      (recordEquiv rows repeats p slots).symm = _
  rw [← FiniteDistribution.transport_eq_pushforward]
  exact UniformConditioning.uniform_transport (recordEquiv rows repeats p slots).symm

def zeroOwnReconstruction (rows repeats : Nat → Nat) (p : Path branch n (m + 1))
    (slots : Slots branch n → Fin t → MixedSupport.Slot)
    (complement : Complement rows repeats p slots) : Arrays slots rows :=
  reconstructRecord rows repeats p slots (assembleRecord rows repeats p slots 0 complement)

theorem reconstruct_assembleRecord_outside (rows repeats : Nat → Nat)
    (p : Path branch n (m + 1)) (slots : Slots branch n → Fin t → MixedSupport.Slot)
    (own₁ own₂ : BucketTape rows p slots) (complement : Complement rows repeats p slots)
    (node : Nodes branch n) (hnode : node ≠ WholeArrayInteriorExterior.upperNode p) :
    reconstructRecord rows repeats p slots (assembleRecord rows repeats p slots own₁ complement)
        node =
      reconstructRecord rows repeats p slots (assembleRecord rows repeats p slots own₂ complement)
        node := by
  change reconstruct rows repeats p slots complement.1
      (assembleRecord rows repeats p slots own₁ complement).2.1 complement.2.2 node =
    reconstruct rows repeats p slots complement.1
      (assembleRecord rows repeats p slots own₂ complement).2.1 complement.2.2 node
  exact OriginalCutBucketReplacement.reconstruct_outside_eq_of_nonown_eq rows repeats p slots
    complement.1 _ _ complement.2.2
    (assembleRecord_nonown_eq rows repeats p slots own₁ own₂ complement) node hnode

def background (rows repeats : Nat → Nat) (p : Path branch n (m + 1))
    (slots : Slots branch n → Fin t → MixedSupport.Slot)
    (complement : Complement rows repeats p slots) :
    HierarchicalMatrixTable.Background (rows := rows) slots (WholeArrayInteriorExterior.upperNode p) :=
  HierarchicalMatrixTable.backgroundOf slots (WholeArrayInteriorExterior.upperNode p)
    (zeroOwnReconstruction rows repeats p slots complement)

theorem background_assembleRecord (rows repeats : Nat → Nat)
    (p : Path branch n (m + 1)) (slots : Slots branch n → Fin t → MixedSupport.Slot)
    (own : BucketTape rows p slots) (complement : Complement rows repeats p slots) :
    HierarchicalMatrixTable.backgroundOf slots (WholeArrayInteriorExterior.upperNode p)
        (reconstructRecord rows repeats p slots (assembleRecord rows repeats p slots own complement)) =
      background rows repeats p slots complement := by
  funext node
  exact reconstruct_assembleRecord_outside rows repeats p slots own 0 complement node.val node.property

theorem background_record (rows repeats : Nat → Nat)
    (p : Path branch n (m + 1)) (slots : Slots branch n → Fin t → MixedSupport.Slot)
    (record : Record rows repeats p slots) :
    HierarchicalMatrixTable.backgroundOf slots (WholeArrayInteriorExterior.upperNode p)
        (reconstructRecord rows repeats p slots record) =
      background rows repeats p slots (recordEquiv rows repeats p slots record).2 := by
  have h := background_assembleRecord rows repeats p slots
    (recordEquiv rows repeats p slots record).1 (recordEquiv rows repeats p slots record).2
  rw [assembleRecord_split] at h
  exact h

local instance backgroundFintype (rows : Nat → Nat) (p : Path branch n (m + 1))
    (slots : Slots branch n → Fin t → MixedSupport.Slot) :
    Fintype (HierarchicalMatrixTable.Background (rows := rows) slots
      (WholeArrayInteriorExterior.upperNode p)) := Fintype.ofFinite _

def backgroundLaw (rows repeats : Nat → Nat) (p : Path branch n (m + 1))
    (slots : Slots branch n → Fin t → MixedSupport.Slot) :
    FiniteDistribution (HierarchicalMatrixTable.Background (rows := rows) slots
      (WholeArrayInteriorExterior.upperNode p)) :=
  (FiniteDistribution.uniform (Complement rows repeats p slots)).pushforward
    (background rows repeats p slots)

def actualBackgroundLaw (rows repeats : Nat → Nat) (p : Path branch n (m + 1))
    (chosen : Fin (branch m)) (q : Path branch m k)
    (slots : Slots branch n → Fin t → MixedSupport.Slot) :
    FiniteDistribution (HierarchicalMatrixTable.Background (rows := rows) slots
      (WholeArrayInteriorExterior.upperNode p)) :=
  (actualComplementLaw rows repeats p chosen q slots).pushforward
    (background rows repeats p slots)

theorem recordLaw_own_background (rows repeats : Nat → Nat)
    (p : Path branch n (m + 1)) (chosen : Fin (branch m)) (q : Path branch m k)
    (slots : Slots branch n → Fin t → MixedSupport.Slot) :
    (OriginalWholeCutLaw.recordLaw rows repeats p chosen q slots).pushforward
        (fun record =>
          ((recordEquiv rows repeats p slots record).1,
            HierarchicalMatrixTable.backgroundOf slots (WholeArrayInteriorExterior.upperNode p)
              (reconstructRecord rows repeats p slots record))) =
      (BucketSampler.tapeLaw (rows (m + 1))
        (RecursiveSampler.law F2 repeats (.step chosen q) (LeafDomain (cutSlots p slots)))).product
          (actualBackgroundLaw rows repeats p chosen q slots) := by
  calc
    _ = ((OriginalWholeCutLaw.recordLaw rows repeats p chosen q slots).pushforward
        (recordEquiv rows repeats p slots)).pushforward
          (fun z => (z.1, background rows repeats p slots z.2)) := by
      rw [FiniteDistribution.pushforward_comp]
      apply congrArg (OriginalWholeCutLaw.recordLaw rows repeats p chosen q slots).pushforward
      funext record
      exact Prod.ext rfl (background_record rows repeats p slots record)
    _ = ((BucketSampler.tapeLaw (rows (m + 1))
        (RecursiveSampler.law F2 repeats (.step chosen q) (LeafDomain (cutSlots p slots)))).product
          (actualComplementLaw rows repeats p chosen q slots)).pushforward
            (fun z => (z.1, background rows repeats p slots z.2)) := by
      rw [recordLaw_split]
    _ = _ := by
      simpa only [id_eq, FiniteDistribution.pushforward_id, actualBackgroundLaw] using
        (FiniteDistribution.product_pushforward
          (BucketSampler.tapeLaw (rows (m + 1))
            (RecursiveSampler.law F2 repeats (.step chosen q) (LeafDomain (cutSlots p slots))))
          (actualComplementLaw rows repeats p chosen q slots)
          (id : BucketTape rows p slots → BucketTape rows p slots)
          (background rows repeats p slots))

theorem uniform_own_background (rows repeats : Nat → Nat)
    (p : Path branch n (m + 1)) (slots : Slots branch n → Fin t → MixedSupport.Slot) :
    (FiniteDistribution.uniform (Record rows repeats p slots)).pushforward
        (fun record =>
          ((recordEquiv rows repeats p slots record).1,
            HierarchicalMatrixTable.backgroundOf slots (WholeArrayInteriorExterior.upperNode p)
              (reconstructRecord rows repeats p slots record))) =
      (FiniteDistribution.uniform (BucketTape rows p slots)).product
        (backgroundLaw rows repeats p slots) := by
  calc
    _ = ((FiniteDistribution.uniform (Record rows repeats p slots)).pushforward
        (recordEquiv rows repeats p slots)).pushforward
          (fun z => (z.1, background rows repeats p slots z.2)) := by
      rw [FiniteDistribution.pushforward_comp]
      apply congrArg ((FiniteDistribution.uniform (Record rows repeats p slots)).pushforward)
      funext record
      exact Prod.ext rfl (background_record rows repeats p slots record)
    _ = ((FiniteDistribution.uniform (BucketTape rows p slots)).product
        (FiniteDistribution.uniform (Complement rows repeats p slots))).pushforward
          (fun z => (z.1, background rows repeats p slots z.2)) := by
      rw [uniform_record_split]
    _ = _ := by
      simpa only [id_eq, FiniteDistribution.pushforward_id, backgroundLaw] using
        (FiniteDistribution.product_pushforward
          (FiniteDistribution.uniform (BucketTape rows p slots))
          (FiniteDistribution.uniform (Complement rows repeats p slots))
          (id : BucketTape rows p slots → BucketTape rows p slots)
          (background rows repeats p slots))

end
end PerfectCompleteness.OriginalOwnBucketSplit

end

end OAI