File size: 22,028 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
import OAI.Computability.PerfectCompleteness.Decoding.ChildAssemblyProjection
import OAI.Computability.PerfectCompleteness.Decoding.LowerCutPairBackground
import OAI.Computability.PerfectCompleteness.Decoding.LowerCutPairLemmas

namespace OAI

section

namespace PerfectCompleteness.LowerCutFiberDisintegration

noncomputable section

open scoped Classical
open RecursiveSpaces DescendantSpaces TreeSourceSpaces HierarchicalArrays
open WholeCutGrouping
open UniqueGamesTheorem.Foundations.Games

variable {branch : Nat → Nat} {n m t : Nat}
  (rows repeats : Nat → Nat) (path : Path branch n (m + 1))
  (slots : Slots branch n → Fin t → MixedSupport.Slot)
  (a : BucketSampler.Direction (rows (m + 1)))

abbrev Scalar := H (cutSlots path slots)
abbrev Quotient := LowerDirectionFiber.Complement a (Scalar path slots)
abbrev Background := LowerCutPairBackground.Complement rows repeats path slots ×
  Quotient rows path slots a
abbrev Coordinates := Background rows repeats path slots a ×
  (Scalar path slots × Scalar path slots)

def coordinateEquiv : LowerCutPairBackground.Observation rows repeats path slots ≃
    Coordinates rows repeats path slots a where
  toFun z := ((z.1, LowerDirectionFiber.complement a z.2.1),
    (z.2.1 (LowerDirectionFiber.pivot a), z.2.2))
  invFun z := (z.1.1, (LowerDirectionFiber.restore a z.1.2 z.2.1, z.2.2))
  left_inv z := by
    change (z.1, (LowerDirectionFiber.restore a
      (LowerDirectionFiber.complement a z.2.1) (z.2.1 (LowerDirectionFiber.pivot a)),
      z.2.2)) = z
    exact Prod.ext rfl (Prod.ext (LowerDirectionFiber.restore_complement a z.2.1) rfl)
  right_inv z := by
    apply Prod.ext
    · exact Prod.ext rfl (LowerDirectionFiber.complement_restore a z.1.2 z.2.1)
    · exact Prod.ext (LowerDirectionFiber.restore_pivot a z.1.2 z.2.1) rfl

@[simp] theorem coordinateEquiv_apply (z : LowerCutPairBackground.Observation rows repeats path slots) :
    coordinateEquiv rows repeats path slots a z =
      ((z.1, LowerDirectionFiber.complement a z.2.1),
        (z.2.1 (LowerDirectionFiber.pivot a), z.2.2)) := rfl

def coordinates (raw : LowerCutPair.Raw rows repeats path slots) :
    Coordinates rows repeats path slots a :=
  coordinateEquiv rows repeats path slots a
    (LowerCutPairBackground.observe rows repeats path slots raw)

def law : FiniteDistribution (Coordinates rows repeats path slots a) :=
  ((FiniteDistribution.uniform (LowerCutPairBackground.Complement rows repeats path slots)).product
    (FiniteDistribution.uniform (Quotient rows path slots a))).product
      ((FiniteDistribution.uniform (Scalar path slots)).product
        (FiniteDistribution.uniform (Scalar path slots)))

theorem coordinates_law :
    (CutChildGrouping.rawLaw (C := Option (LowerCutPair.Calls rows repeats path))
      (cutSlots path slots) rows).pushforward (coordinates rows repeats path slots a) =
      law rows repeats path slots a := by
  change (CutChildGrouping.rawLaw (C := Option (LowerCutPair.Calls rows repeats path))
    (cutSlots path slots) rows).pushforward
      (fun raw => coordinateEquiv rows repeats path slots a
        (LowerCutPairBackground.observe rows repeats path slots raw)) = _
  rw [← FiniteDistribution.pushforward_comp,
    LowerCutPairBackground.observe_law]
  have h := UniformConditioning.uniform_transport
    (coordinateEquiv rows repeats path slots a)
  rw [FiniteDistribution.transport_eq_pushforward] at h
  simpa only [law, WholeCutSampler.uniform_product] using h

def arraysAt (exterior : Exterior rows repeats path slots)
    (background : Background rows repeats path slots a) (fresh : Scalar path slots) :
    Arrays slots rows :=
  LowerCutPairBackground.installRows rows repeats path slots exterior background.1
    (LowerDirectionFiber.restore a background.2 fresh)

theorem first_eq_arraysAt (exterior : Exterior rows repeats path slots)
    (raw : LowerCutPair.Raw rows repeats path slots) :
    LowerCutPair.first rows repeats path slots exterior raw =
      arraysAt rows repeats path slots a exterior
        (coordinates rows repeats path slots a raw).1
        (coordinates rows repeats path slots a raw).2.1 := by
  simp only [arraysAt, coordinates, coordinateEquiv_apply, LowerDirectionFiber.restore_complement]
  rw [LowerCutPairBackground.observe_rows rows repeats path slots exterior]
  exact LowerCutPairBackground.first_eq_installRows rows repeats path slots exterior raw

theorem second_eq_arraysAt (exterior : Exterior rows repeats path slots)
    (raw : LowerCutPair.Raw rows repeats path slots) :
    LowerCutPair.second rows repeats path slots exterior a raw =
      arraysAt rows repeats path slots a exterior
        (coordinates rows repeats path slots a raw).1
        (coordinates rows repeats path slots a raw).2.2 := by
  rw [LowerCutPairBackground.second_eq_installRows,
    LowerCutPair.selected_second]
  simp only [arraysAt, coordinates, coordinateEquiv_apply,
    LowerDirectionFiber.replace, LowerCutPairBackground.observe_rows rows repeats path slots exterior,
    LowerCutPairBackground.observe_fresh]

def pair (exterior : Exterior rows repeats path slots)
    (z : Coordinates rows repeats path slots a) : Arrays slots rows × Arrays slots rows :=
  (arraysAt rows repeats path slots a exterior z.1 z.2.1,
    arraysAt rows repeats path slots a exterior z.1 z.2.2)

theorem pair_coordinates (exterior : Exterior rows repeats path slots)
    (raw : LowerCutPair.Raw rows repeats path slots) :
    pair rows repeats path slots a exterior (coordinates rows repeats path slots a raw) =
      LowerCutPair.arraysPair rows repeats path slots exterior a raw :=
  Prod.ext (first_eq_arraysAt rows repeats path slots a exterior raw).symm
    (second_eq_arraysAt rows repeats path slots a exterior raw).symm

theorem arraysPair_law (exterior : Exterior rows repeats path slots) :
    (CutChildGrouping.rawLaw (C := Option (LowerCutPair.Calls rows repeats path))
      (cutSlots path slots) rows).pushforward
        (LowerCutPair.arraysPair rows repeats path slots exterior a) =
      (law rows repeats path slots a).pushforward (pair rows repeats path slots a exterior) := by
  have h := congrArg
    (fun μ : FiniteDistribution (Coordinates rows repeats path slots a) =>
      μ.pushforward (pair rows repeats path slots a exterior))
    (coordinates_law rows repeats path slots a)
  rw [FiniteDistribution.pushforward_comp] at h
  simpa only [pair_coordinates] using h

theorem probability_pair (exterior : Exterior rows repeats path slots)
    (event : Arrays slots rows × Arrays slots rows → Bool) :
    (CutChildGrouping.rawLaw (C := Option (LowerCutPair.Calls rows repeats path))
      (cutSlots path slots) rows).probability
        (fun raw => event (LowerCutPair.arraysPair rows repeats path slots exterior a raw)) =
      (law rows repeats path slots a).probability
        (fun z => event (pair rows repeats path slots a exterior z)) := by
  rw [← FiniteDistribution.probability_pushforward,
    arraysPair_law, FiniteDistribution.probability_pushforward]

end
end PerfectCompleteness.LowerCutFiberDisintegration

end

section

namespace PerfectCompleteness.LowerCutProjection

open RecursiveSpaces DescendantSpaces TreeSourceSpaces HierarchicalArrays
open UniqueGamesTheorem.Foundations.Games
open scoped Classical

noncomputable section

section Linear

variable {H K : Type*} [AddCommGroup H] [AddCommGroup K]
  [Module F2 H] [Module F2 K] {ℓ : Nat}

theorem linearMap_replace (f : H →ₗ[F2] K) (a : BucketSampler.Direction ℓ)
    (S : Fin ℓ → H) (fresh : H) :
    (fun i => f (LowerDirectionFiber.replace a S fresh i)) =
      LowerDirectionFiber.replace a (fun i => f (S i)) (f fresh) := by
  rw [LowerDirectionFiber.replace_eq, LowerDirectionFiber.replace_eq]
  funext i
  simp only [Pi.add_apply, BucketSampler.rankOne, map_add, map_smul, map_sub]

end Linear

section Local

variable {branch : Nat → Nat} {n t : Nat} {C : Type*} [Fintype C]
  {slots projected : Slots branch (n + 1) → Fin t → MixedSupport.Slot}

omit [Fintype C] in
theorem original_pullback (rows : Nat → Nat)
    (p : ∀ s k, MixedSupport.Projection (slots s k) (projected s k))
    (raw : CutCallExtension.Extended (C := C) projected rows) :
    CutCallExtension.original slots rows
        (ChildAssemblyProjection.rawPullback (C := Option C) rows p raw) =
      ChildAssemblyProjection.rawPullback (C := C) rows p
        (CutCallExtension.original projected rows raw) := rfl

omit [Fintype C] in
theorem fresh_pullback (rows : Nat → Nat)
    (p : ∀ s k, MixedSupport.Projection (slots s k) (projected s k))
    (raw : CutCallExtension.Extended (C := C) projected rows) :
    CutCallExtension.fresh slots rows
        (ChildAssemblyProjection.rawPullback (C := Option C) rows p raw) =
      HPullback p (CutCallExtension.fresh projected rows raw) :=
  ChildAssemblyProjection.recursiveSum_pullback p (fun i => (raw i).1 none)

def observePullback (rows : Nat → Nat)
    (p : ∀ s k, MixedSupport.Projection (slots s k) (projected s k)) :
    (CutChildGrouping.Assembled (C := C) projected rows × H projected) →ₗ[F2]
      (CutChildGrouping.Assembled (C := C) slots rows × H slots) :=
  (ChildAssemblyProjection.assembledPullback (C := C) rows p).prodMap (HPullback p)

omit [Fintype C] in
theorem observe_pullback (rows : Nat → Nat)
    (p : ∀ s k, MixedSupport.Projection (slots s k) (projected s k))
    (raw : CutCallExtension.Extended (C := C) projected rows) :
    CutCallExtension.observe slots rows
        (ChildAssemblyProjection.rawPullback (C := Option C) rows p raw) =
      observePullback rows p (CutCallExtension.observe projected rows raw) := by
  apply Prod.ext
  · change CutChildGrouping.assemble slots rows
      (CutCallExtension.original slots rows
        (ChildAssemblyProjection.rawPullback (C := Option C) rows p raw)) =
      ChildAssemblyProjection.assembledPullback rows p
        (CutChildGrouping.assemble projected rows (CutCallExtension.original projected rows raw))
    rw [original_pullback]
    exact ChildAssemblyProjection.assemble_pullback rows p _
  · exact fresh_pullback rows p raw

theorem pushforward_observe (rows : Nat → Nat)
    (p : ∀ s k, MixedSupport.Projection (slots s k) (projected s k))
    (μ : FiniteDistribution (CutCallExtension.Extended (C := C) projected rows)) :
    (μ.pushforward (ChildAssemblyProjection.rawPullback (C := Option C) rows p)).pushforward
        (CutCallExtension.observe slots rows) =
      (μ.pushforward (CutCallExtension.observe projected rows)).pushforward
        (observePullback rows p) := by
  rw [FiniteDistribution.pushforward_comp, FiniteDistribution.pushforward_comp]
  congr 1
  funext raw
  exact observe_pullback rows p raw

theorem uniform_projected_observe (rows : Nat → Nat)
    (p : ∀ s k, MixedSupport.Projection (slots s k) (projected s k)) :
    ((CutChildGrouping.rawLaw (C := Option C) projected rows).pushforward
        (ChildAssemblyProjection.rawPullback rows p)).pushforward
          (CutCallExtension.observe slots rows) =
      ((FiniteDistribution.uniform (CutChildGrouping.Assembled (C := C) projected rows)).product
        (FiniteDistribution.uniform (H projected))).pushforward (observePullback rows p) := by
  rw [pushforward_observe, CutCallExtension.observe_law]

theorem HPullback_replace {ℓ : Nat}
    (p : ∀ s k, MixedSupport.Projection (slots s k) (projected s k))
    (a : BucketSampler.Direction ℓ) (S : Fin ℓ → H projected) (fresh : H projected) :
    (fun i => HPullback p (LowerDirectionFiber.replace a S fresh i)) =
      LowerDirectionFiber.replace a (fun i => HPullback p (S i)) (HPullback p fresh) :=
  linearMap_replace (HPullback p) a S fresh

end Local

section Selected

variable {branch : Nat → Nat} {n m t : Nat}
  (rows repeats : Nat → Nat) (path : Path branch n (m + 1))
  {slots projected : Slots branch n → Fin t → MixedSupport.Slot}

theorem firstRows_pullback
    (p : ∀ s k, MixedSupport.Projection
      (WholeCutGrouping.cutSlots path slots s k) (WholeCutGrouping.cutSlots path projected s k))
    (exterior : WholeCutGrouping.Exterior rows repeats path slots)
    (projectedExterior : WholeCutGrouping.Exterior rows repeats path projected)
    (raw : LowerCutPair.Raw rows repeats path projected) :
    SelectedArrayReplacement.selectedRows rows path slots
        (LowerCutPair.first rows repeats path slots exterior
          (ChildAssemblyProjection.rawPullback rows p raw)) =
      (fun i => HPullback p
        (SelectedArrayReplacement.selectedRows rows path projected
          (LowerCutPair.first rows repeats path projected projectedExterior raw) i)) := by
  simp only [LowerCutPair.first, LowerCutRows.selectedRows_reconstruct_eq]
  rw [original_pullback]
  exact congrArg (LowerCutRows.selectRows rows repeats path slots)
    (ChildAssemblyProjection.assemble_pullback rows p
      (CutCallExtension.original (WholeCutGrouping.cutSlots path projected) rows raw))

theorem selectedPair_pullback
    (p : ∀ s k, MixedSupport.Projection
      (WholeCutGrouping.cutSlots path slots s k) (WholeCutGrouping.cutSlots path projected s k))
    (exterior : WholeCutGrouping.Exterior rows repeats path slots)
    (projectedExterior : WholeCutGrouping.Exterior rows repeats path projected)
    (a : BucketSampler.Direction (rows (m + 1)))
    (raw : LowerCutPair.Raw rows repeats path projected) :
    LowerCutPair.selectedPair rows repeats path slots exterior a
        (ChildAssemblyProjection.rawPullback rows p raw) =
      ((fun i => HPullback p
          ((LowerCutPair.selectedPair rows repeats path projected projectedExterior a raw).1 i)),
       (fun i => HPullback p
          ((LowerCutPair.selectedPair rows repeats path projected projectedExterior a raw).2 i))) := by
  simp only [LowerCutPair.selectedPair, LowerCutPair.selected_second]
  apply Prod.ext
  · exact firstRows_pullback rows repeats path p exterior projectedExterior raw
  · rw [firstRows_pullback rows repeats path p exterior projectedExterior raw]
    simp only [LowerCutPair.fresh]
    rw [fresh_pullback]
    exact (HPullback_replace p a _ _).symm

end Selected
end
end PerfectCompleteness.LowerCutProjection

end

section

namespace PerfectCompleteness.LowerCutSingleFiberDisintegration

noncomputable section

open scoped Classical
open RecursiveSpaces DescendantSpaces TreeSourceSpaces HierarchicalArrays
open WholeCutGrouping
open UniqueGamesTheorem.Foundations.Games

variable {branch : Nat → Nat} {n m t : Nat}
  (rows repeats : Nat → Nat) (path : Path branch n (m + 1))
  (slots : Slots branch n → Fin t → MixedSupport.Slot)
  (a : BucketSampler.Direction (rows (m + 1)))

abbrev Raw := CutChildGrouping.Raw (C := LowerCutPair.Calls rows repeats path)
  (cutSlots path slots) rows

abbrev Coordinates := LowerCutFiberDisintegration.Background rows repeats path slots a ×
  H (cutSlots path slots)

def coordinateEquiv :
    (LowerCutCallSplit.Complement rows repeats path slots ×
      LowerCutCallSplit.NativeRows rows path slots) ≃
        Coordinates rows repeats path slots a where
  toFun z := ((z.1, LowerDirectionFiber.complement a z.2),
    z.2 (LowerDirectionFiber.pivot a))
  invFun z := (z.1.1, LowerDirectionFiber.restore a z.1.2 z.2)
  left_inv z := Prod.ext rfl (LowerDirectionFiber.restore_complement a z.2)
  right_inv z := Prod.ext
    (Prod.ext rfl (LowerDirectionFiber.complement_restore a z.1.2 z.2))
    (LowerDirectionFiber.restore_pivot a z.1.2 z.2)

@[simp] theorem coordinateEquiv_apply
    (z : LowerCutCallSplit.Complement rows repeats path slots ×
      LowerCutCallSplit.NativeRows rows path slots) :
    coordinateEquiv rows repeats path slots a z =
      ((z.1, LowerDirectionFiber.complement a z.2),
        z.2 (LowerDirectionFiber.pivot a)) := rfl

def coordinates (raw : Raw rows repeats path slots) :
    Coordinates rows repeats path slots a :=
  coordinateEquiv rows repeats path slots a
    (LowerCutCallSplit.splitAssembled rows repeats path slots
      (CutChildGrouping.assemble (cutSlots path slots) rows raw))

@[simp] theorem coordinates_complement (raw : Raw rows repeats path slots) :
    (coordinates rows repeats path slots a raw).1.1 =
      LowerCutCallSplit.complement rows repeats path slots
        (CutChildGrouping.assemble (cutSlots path slots) rows raw) := rfl

theorem coordinates_law :
    (CutChildGrouping.rawLaw (C := LowerCutPair.Calls rows repeats path)
      (cutSlots path slots) rows).pushforward (coordinates rows repeats path slots a) =
      ((FiniteDistribution.uniform
        (LowerCutPairBackground.Complement rows repeats path slots)).product
          (FiniteDistribution.uniform
            (LowerCutFiberDisintegration.Quotient rows path slots a))).product
        (FiniteDistribution.uniform (H (cutSlots path slots))) := by
  have hsplit :
      (CutChildGrouping.rawLaw (C := LowerCutPair.Calls rows repeats path)
        (cutSlots path slots) rows).pushforward (fun raw =>
          LowerCutCallSplit.splitAssembled rows repeats path slots
            (CutChildGrouping.assemble (cutSlots path slots) rows raw)) =
        FiniteDistribution.uniform
          (LowerCutCallSplit.Complement rows repeats path slots ×
            LowerCutCallSplit.NativeRows rows path slots) := by
    rw [← FiniteDistribution.pushforward_comp, CutChildGrouping.assemble_law]
    exact UniformLinearImage.uniform_pushforward_linearMap
      (LowerCutCallSplit.splitAssembled rows repeats path slots)
      (LowerCutCallSplit.splitAssembled_surjective rows repeats path slots)
  have h := congrArg
    (fun μ : FiniteDistribution
        (LowerCutCallSplit.Complement rows repeats path slots ×
          LowerCutCallSplit.NativeRows rows path slots) =>
      μ.pushforward (coordinateEquiv rows repeats path slots a)) hsplit
  rw [FiniteDistribution.pushforward_comp] at h
  have hcoord := UniformConditioning.uniform_transport
    (coordinateEquiv rows repeats path slots a)
  rw [FiniteDistribution.transport_eq_pushforward] at hcoord
  have hfinal := h.trans hcoord
  change (CutChildGrouping.rawLaw (C := LowerCutPair.Calls rows repeats path)
    (cutSlots path slots) rows).pushforward (fun raw =>
      coordinateEquiv rows repeats path slots a
        (LowerCutCallSplit.splitAssembled rows repeats path slots
          (CutChildGrouping.assemble (cutSlots path slots) rows raw))) = _
  calc
    _ = FiniteDistribution.uniform (Coordinates rows repeats path slots a) := hfinal
    _ = _ := by
      apply FiniteDistribution.eq_of_weight_eq
      intro z
      simp only [FiniteDistribution.uniform, FiniteDistribution.product,
        Coordinates, LowerCutFiberDisintegration.Background,
        Fintype.card_prod, Nat.cast_mul, one_div_mul_one_div]

theorem reconstruct_eq_arraysAt (exterior : Exterior rows repeats path slots)
    (raw : Raw rows repeats path slots) :
    LowerCutRows.reconstruct rows repeats path slots exterior raw =
      LowerCutFiberDisintegration.arraysAt rows repeats path slots a exterior
        (coordinates rows repeats path slots a raw).1
        (coordinates rows repeats path slots a raw).2 := by
  simp only [LowerCutFiberDisintegration.arraysAt, coordinates,
    coordinateEquiv_apply, LowerDirectionFiber.restore_complement]
  rw [← LowerCutAssembled.reconstruct_assemble,
    ← LowerCutPairBackground.reconstruct_merge, LowerCutCallSplit.merge_split]

theorem expectation_fiber (exterior : Exterior rows repeats path slots)
    (f : LowerCutFiberDisintegration.Background rows repeats path slots a →
      Arrays slots rows → ℝ) :
    (CutChildGrouping.rawLaw (C := LowerCutPair.Calls rows repeats path)
      (cutSlots path slots) rows).expectation (fun raw =>
        f (coordinates rows repeats path slots a raw).1
          (LowerCutRows.reconstruct rows repeats path slots exterior raw)) =
      (((FiniteDistribution.uniform
        (LowerCutPairBackground.Complement rows repeats path slots)).product
          (FiniteDistribution.uniform
            (LowerCutFiberDisintegration.Quotient rows path slots a))).product
        (FiniteDistribution.uniform (H (cutSlots path slots)))).expectation
          (fun z => f z.1
            (LowerCutFiberDisintegration.arraysAt rows repeats path slots a exterior z.1 z.2)) := by
  have h := congrArg
    (fun μ : FiniteDistribution (Coordinates rows repeats path slots a) =>
      μ.expectation (fun z => f z.1
        (LowerCutFiberDisintegration.arraysAt rows repeats path slots a exterior z.1 z.2)))
    (coordinates_law rows repeats path slots a)
  rw [FiniteDistribution.expectation_pushforward] at h
  simpa only [← reconstruct_eq_arraysAt rows repeats path slots a exterior] using h

theorem probability_fiber (exterior : Exterior rows repeats path slots)
    (event : LowerCutFiberDisintegration.Background rows repeats path slots a →
      Arrays slots rows → Bool) :
    (CutChildGrouping.rawLaw (C := LowerCutPair.Calls rows repeats path)
      (cutSlots path slots) rows).probability (fun raw =>
        event (coordinates rows repeats path slots a raw).1
          (LowerCutRows.reconstruct rows repeats path slots exterior raw)) =
      (((FiniteDistribution.uniform
        (LowerCutPairBackground.Complement rows repeats path slots)).product
          (FiniteDistribution.uniform
            (LowerCutFiberDisintegration.Quotient rows path slots a))).product
        (FiniteDistribution.uniform (H (cutSlots path slots)))).probability
          (fun z => event z.1
            (LowerCutFiberDisintegration.arraysAt rows repeats path slots a exterior z.1 z.2)) := by
  have h := congrArg
    (fun μ : FiniteDistribution (Coordinates rows repeats path slots a) =>
      μ.probability (fun z => event z.1
        (LowerCutFiberDisintegration.arraysAt rows repeats path slots a exterior z.1 z.2)))
    (coordinates_law rows repeats path slots a)
  rw [FiniteDistribution.probability_pushforward] at h
  simpa only [← reconstruct_eq_arraysAt rows repeats path slots a exterior] using h

end
end PerfectCompleteness.LowerCutSingleFiberDisintegration

end

end OAI