File size: 27,751 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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
import OAI.Computability.PerfectCompleteness.Decoding.ChildAssemblyProjection
import OAI.Computability.PerfectCompleteness.Decoding.CutProjectionGeometry
import OAI.Computability.PerfectCompleteness.Foundations.OriginalTerminalSplitLemmas
import OAI.Computability.PerfectCompleteness.Sampling.CutSamplerReplayGroupingTransport

namespace OAI

section

namespace PerfectCompleteness.CutGroupedProjection

open PointwiseSpaces RecursiveSpaces DescendantSpaces TreeSourceSpaces HierarchicalArrays
open RecursiveSampler TerminalCalls
open scoped BigOperators Classical

abbrev F2 := ZMod 2

noncomputable section

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

def cutProjection (p : Path branch n (m + 1))
    {left right : Slots branch n → Fin t → MixedSupport.Slot}
    (q : ∀ s k, MixedSupport.Projection (left s k) (right s k)) :
    ∀ s k, MixedSupport.Projection
      (WholeCutGrouping.cutSlots p left s k) (WholeCutGrouping.cutSlots p right s k) :=
  fun s k => q (p.slotEmbedding s) k

def terminalPullback (repeats : Nat → Nat) (p : Path branch n (m + 1))
    {left right : Slots branch n → Fin t → MixedSupport.Slot}
    (q : ∀ s k, MixedSupport.Projection (left s k) (right s k))
    (terminal : CutTerminalSplit.TerminalTape F2 repeats p (LeafDomain right)) :
    CutTerminalSplit.TerminalTape F2 repeats p (LeafDomain left) :=
  fun call child => ChildBlockProjection.squarePullback
    (ChildAssemblyProjection.childProjection (cutProjection p q) child) (terminal call child)

def factorPullback (repeats : Nat → Nat) :
    {n m : Nat} → (p : Path branch n (m + 1)) →
      (left right : Slots branch n → Fin t → MixedSupport.Slot) →
      (∀ s k, MixedSupport.Projection (left s k) (right s k)) →
      (j : DrawIndex repeats p) →
      CutSamplerRefinement.RefinedSpace F2 repeats p (LeafDomain right) j →
        CutSamplerRefinement.RefinedSpace F2 repeats p (LeafDomain left) j
  | _ + 1, _, .refl _, _, _, q, _, x =>
      fun child => ChildBlockProjection.squarePullback
        (ChildAssemblyProjection.childProjection q child) (x child)
  | _ + 1, _, .step i p, left, right, q, j, x =>
      match j with
      | Sum.inl j =>
          ChildBlockProjection.squarePullback (ChildAssemblyProjection.childProjection q j.val) x
      | Sum.inr j =>
          factorPullback repeats p (childSlots left i) (childSlots right i)
            (ChildAssemblyProjection.childProjection q i) j.2 x

def tapePullback (repeats : Nat → Nat) (p : Path branch n (m + 1))
    (left right : Slots branch n → Fin t → MixedSupport.Slot)
    (q : ∀ s k, MixedSupport.Projection (left s k) (right s k))
    (tape : CutSamplerRefinement.Tape F2 repeats p (LeafDomain right)) :
    CutSamplerRefinement.Tape F2 repeats p (LeafDomain left) :=
  fun j => factorPullback repeats p left right q j (tape j)

theorem evaluate_tapePullback (repeats : Nat → Nat) (p : Path branch n (m + 1)) :
    ∀ (left right : Slots branch n → Fin t → MixedSupport.Slot)
      (q : ∀ s k, MixedSupport.Projection (left s k) (right s k))
      (tape : CutSamplerRefinement.Tape F2 repeats p (LeafDomain right)) (x : Domain left),
      (CutSamplerRefinement.evaluate F2 repeats p (LeafDomain left)
        (tapePullback repeats p left right q tape)).val x =
      (CutSamplerRefinement.evaluate F2 repeats p (LeafDomain right) tape).val
        (sourceProjection q x) := by
  induction n generalizing m with
  | zero =>
      have h := p.height_le
      omega
  | succ n ih =>
      cases p with
      | refl =>
          intro left right q tape x
          exact congrArg (fun f : H left => f.val x)
            (ChildAssemblyProjection.recursiveSum_pullback q (tape ()))
      | step i p =>
          intro left right q tape x
          simp only [CutSamplerLocality.evaluate_step, RecursiveSampler.combine,
            RecursiveSampler.combineFunction, Finset.sum_apply, Pi.add_apply, Pi.mul_apply,
            PointwiseSpaces.pullback_apply]
          change
            (∑ j : OffPath i,
              (ChildBlockProjection.squarePullback (ChildAssemblyProjection.childProjection q j.val)
                (tape (.inl j))).val (restrictChild left j.val x)) +
              (∑ h : Fin (repeats (n + 1)),
                (CutSamplerRefinement.evaluate F2 repeats p (LeafDomain (childSlots left i))
                  (tapePullback repeats p (childSlots left i) (childSlots right i)
                    (ChildAssemblyProjection.childProjection q i)
                    (CutSamplerLocality.subTape F2 repeats i p (LeafDomain right) tape h false))).val
                      (restrictChild left i x) *
                (CutSamplerRefinement.evaluate F2 repeats p (LeafDomain (childSlots left i))
                  (tapePullback repeats p (childSlots left i) (childSlots right i)
                    (ChildAssemblyProjection.childProjection q i)
                    (CutSamplerLocality.subTape F2 repeats i p (LeafDomain right) tape h true))).val
                      (restrictChild left i x)) =
            (∑ j : OffPath i, (tape (.inl j)).val (restrictChild right j.val (sourceProjection q x))) +
              (∑ h : Fin (repeats (n + 1)),
                (CutSamplerRefinement.evaluate F2 repeats p (LeafDomain (childSlots right i))
                  (CutSamplerLocality.subTape F2 repeats i p (LeafDomain right) tape h false)).val
                    (restrictChild right i (sourceProjection q x)) *
                (CutSamplerRefinement.evaluate F2 repeats p (LeafDomain (childSlots right i))
                  (CutSamplerLocality.subTape F2 repeats i p (LeafDomain right) tape h true)).val
                    (restrictChild right i (sourceProjection q x)))
          apply congrArg₂ (fun a b : F2 => a + b)
          · exact Finset.sum_congr rfl (fun _ _ => rfl)
          · apply Finset.sum_congr rfl
            intro h _
            apply congrArg₂ (fun a b : F2 => a * b)
            · exact ih p (childSlots left i) (childSlots right i)
                (ChildAssemblyProjection.childProjection q i)
                (CutSamplerLocality.subTape F2 repeats i p (LeafDomain right) tape h false)
                (restrictChild left i x)
            · exact ih p (childSlots left i) (childSlots right i)
                (ChildAssemblyProjection.childProjection q i)
                (CutSamplerLocality.subTape F2 repeats i p (LeafDomain right) tape h true)
                (restrictChild left i x)

theorem terminalFactor_pullback (repeats : Nat → Nat) (p : Path branch n (m + 1)) :
    ∀ (left right : Slots branch n → Fin t → MixedSupport.Slot)
      (q : ∀ s k, MixedSupport.Projection (left s k) (right s k))
      (call : TerminalIndex repeats p)
      (x : CutSamplerRefinement.RefinedSpace F2 repeats p (LeafDomain right)
        (drawIndex repeats p call)),
      CutTerminalSplit.terminalFactorEquiv F2 repeats p (LeafDomain left) call
          (factorPullback repeats p left right q (drawIndex repeats p call) x) =
        fun child => ChildBlockProjection.squarePullback
          (ChildAssemblyProjection.childProjection (cutProjection p q) child)
          (CutTerminalSplit.terminalFactorEquiv F2 repeats p (LeafDomain right) call x child) := by
  induction n generalizing m with
  | zero =>
      have h := p.height_le
      omega
  | succ n ih =>
      cases p with
      | refl =>
          intro left right q call x
          cases call
          rfl
      | step i p =>
          intro left right q call x
          exact ih p (childSlots left i) (childSlots right i)
            (ChildAssemblyProjection.childProjection q i) call.2 x

theorem exteriorFactor_pullback (repeats : Nat → Nat) (p : Path branch n (m + 1)) :
    ∀ (left right : Slots branch n → Fin t → MixedSupport.Slot)
      (q : ∀ s k, MixedSupport.Projection (left s k) (right s k))
      (hs : ∀ s, WholeCutExteriorTransport.Outside p s → left s = right s)
      (_hk : ∀ s (_hout : WholeCutExteriorTransport.Outside p s) k,
        HEq (q s k) (MixedSupport.Projection.keep (left s k)))
      (j : DrawIndex repeats p) (hj : ¬ IsTerminal repeats p j)
      (x : CutSamplerRefinement.RefinedSpace F2 repeats p (LeafDomain right) j),
      factorPullback repeats p left right q j x =
        cast (WholeCutExteriorTransport.factor_eq repeats p right left
          (fun s h => (hs s h).symm) j hj) x := by
  induction n generalizing m with
  | zero =>
      have h := p.height_le
      omega
  | succ n ih =>
      cases p with
      | refl =>
          intro left right q hs hk j hj x
          exact False.elim (hj True.intro)
      | step i p =>
          intro left right q hs hk j hj x
          cases j with
          | inl j =>
              exact CutProjectionGeometry.squarePullback_eq_cast
                (childSlots left j.val) (childSlots right j.val)
                (ChildAssemblyProjection.childProjection q j.val)
                (WholeCutExteriorTransport.ordinarySlots_eq i p left right hs j)
                (fun s k => hk (j.val, s) (WholeCutExteriorTransport.outside_ordinary i p j s) k) x
          | inr j =>
              exact ih p (childSlots left i) (childSlots right i)
                (ChildAssemblyProjection.childProjection q i)
                (fun s h => hs (i, s) (WholeCutExteriorTransport.outside_selected i p s h))
                (fun s h k => hk (i, s) (WholeCutExteriorTransport.outside_selected i p s h) k)
                j.2 hj x

theorem splitTape_tapePullback (repeats : Nat → Nat) (p : Path branch n (m + 1))
    (left right : Slots branch n → Fin t → MixedSupport.Slot)
    (q : ∀ s k, MixedSupport.Projection (left s k) (right s k))
    (hs : ∀ s, WholeCutExteriorTransport.Outside p s → left s = right s)
    (hk : ∀ s (_hout : WholeCutExteriorTransport.Outside p s) k,
      HEq (q s k) (MixedSupport.Projection.keep (left s k)))
    (tape : CutSamplerRefinement.Tape F2 repeats p (LeafDomain right)) :
    CutTerminalSplit.splitTape F2 repeats p (LeafDomain left)
        (tapePullback repeats p left right q tape) =
      (terminalPullback repeats p q (CutTerminalSplit.splitTape F2 repeats p (LeafDomain right) tape).1,
        cast (WholeCutExteriorTransport.scalarExterior_eq repeats p right left
          (fun s h => (hs s h).symm))
          (CutTerminalSplit.splitTape F2 repeats p (LeafDomain right) tape).2) := by
  apply Prod.ext
  · funext call
    exact terminalFactor_pullback repeats p left right q call (tape (drawIndex repeats p call))
  · funext j
    exact (exteriorFactor_pullback repeats p left right q hs hk j.val j.property (tape j.val)).trans
      (CutSamplerReplayGroupingTransport.cast_pi_apply
        (WholeCutExteriorTransport.scalarExterior_eq repeats p right left
          (fun s h => (hs s h).symm))
        (fun j => WholeCutExteriorTransport.factor_eq repeats p right left
          (fun s h => (hs s h).symm) j.val j.property)
        (CutTerminalSplit.splitTape F2 repeats p (LeafDomain right) tape).2 j).symm

theorem evaluate_rebuild (repeats : Nat → Nat) (p : Path branch n (m + 1))
    (left right : Slots branch n → Fin t → MixedSupport.Slot)
    (q : ∀ s k, MixedSupport.Projection (left s k) (right s k))
    (hs : ∀ s, WholeCutExteriorTransport.Outside p s → left s = right s)
    (hk : ∀ s (_hout : WholeCutExteriorTransport.Outside p s) k,
      HEq (q s k) (MixedSupport.Projection.keep (left s k)))
    (terminal : CutTerminalSplit.TerminalTape F2 repeats p (LeafDomain right))
    (exterior : CutTerminalSplit.ExteriorTape F2 repeats p (LeafDomain right)) (x : Domain left) :
    (CutSamplerRefinement.evaluate F2 repeats p (LeafDomain left)
      ((CutTerminalSplit.splitTape F2 repeats p (LeafDomain left)).symm
        (terminalPullback repeats p q terminal,
          cast (WholeCutExteriorTransport.scalarExterior_eq repeats p right left
            (fun s h => (hs s h).symm)) exterior))).val x =
    (CutSamplerRefinement.evaluate F2 repeats p (LeafDomain right)
      ((CutTerminalSplit.splitTape F2 repeats p (LeafDomain right)).symm (terminal, exterior))).val
        (sourceProjection q x) := by
  let tape := (CutTerminalSplit.splitTape F2 repeats p (LeafDomain right)).symm (terminal, exterior)
  have hsplit :
      CutTerminalSplit.splitTape F2 repeats p (LeafDomain left)
          (tapePullback repeats p left right q tape) =
        (terminalPullback repeats p q terminal,
          cast (WholeCutExteriorTransport.scalarExterior_eq repeats p right left
            (fun s h => (hs s h).symm)) exterior) := by
    simpa only [tape, Equiv.apply_symm_apply] using
      splitTape_tapePullback repeats p left right q hs hk tape
  have hleft := congrArg (CutTerminalSplit.splitTape F2 repeats p (LeafDomain left)).symm hsplit
  simp only [Equiv.symm_apply_apply] at hleft
  rw [← hleft]
  exact evaluate_tapePullback repeats p left right q tape x

end
end PerfectCompleteness.CutGroupedProjection

end

section

namespace PerfectCompleteness.OriginalScalarProjection

open PointwiseSpaces RecursiveSpaces DescendantSpaces TreeSourceSpaces HierarchicalArrays
open RecursiveSampler TerminalCalls
open scoped BigOperators Classical

abbrev F2 := ZMod 2

noncomputable section

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

theorem factor_eq (repeats : Nat → Nat) (p : Path branch n m) :
    ∀ (left right : Slots branch n → Fin t → MixedSupport.Slot),
      (∀ s, WholeCutExteriorTransport.Outside p s → left s = right s) →
      ∀ (j : DrawIndex repeats p), ¬ IsTerminal repeats p j →
        DrawSpace F2 repeats p (LeafDomain left) j =
          DrawSpace F2 repeats p (LeafDomain right) j := by
  induction p with
  | refl n =>
      intro left right hs j hj
      exact False.elim (hj True.intro)
  | @step n m i p ih =>
      intro left right hs j hj
      cases j with
      | inl j =>
          exact congrArg
            (fun ss : Slots branch n → Fin t → MixedSupport.Slot =>
              (squareSpace (space F2 branch n (LeafDomain ss)) : Type))
            (WholeCutExteriorTransport.ordinarySlots_eq i p left right hs j)
      | inr j =>
          exact ih (childSlots left i) (childSlots right i)
            (fun s h => hs (i, s) (WholeCutExteriorTransport.outside_selected i p s h)) j.2 hj

theorem scalarExterior_eq (repeats : Nat → Nat) (p : Path branch n (m + 1))
    (left right : Slots branch n → Fin t → MixedSupport.Slot)
    (hs : ∀ s, WholeCutExteriorTransport.Outside p s → left s = right s) :
    OriginalTerminalSplit.ExteriorTape F2 repeats p (LeafDomain left) =
      OriginalTerminalSplit.ExteriorTape F2 repeats p (LeafDomain right) :=
  congrArg
    (fun A : OriginalTerminalSplit.ExteriorIndex repeats p → Type =>
      (j : OriginalTerminalSplit.ExteriorIndex repeats p) → A j)
    (funext (fun j => factor_eq repeats p left right hs j.val j.property))

def terminalPullback (repeats : Nat → Nat) (p : Path branch n (m + 1))
    {left right : Slots branch n → Fin t → MixedSupport.Slot}
    (q : ∀ s k, MixedSupport.Projection (left s k) (right s k))
    (values : OriginalScalarReconstruction.TerminalValues F2 repeats p (LeafDomain right)) :
    OriginalScalarReconstruction.TerminalValues F2 repeats p (LeafDomain left) :=
  fun call => HPullback (CutGroupedProjection.cutProjection p q) (values call)

def factorPullback (repeats : Nat → Nat) :
    {n m : Nat} → (p : Path branch n (m + 1)) →
      (left right : Slots branch n → Fin t → MixedSupport.Slot) →
      (∀ s k, MixedSupport.Projection (left s k) (right s k)) →
      (j : DrawIndex repeats p) →
      RecursiveSampler.DrawSpace F2 repeats p (LeafDomain right) j →
        RecursiveSampler.DrawSpace F2 repeats p (LeafDomain left) j
  | _ + 1, _, .refl _, _, _, q, _, x => HPullback q x
  | _ + 1, _, .step i p, left, right, q, j, x =>
      match j with
      | Sum.inl j =>
          ChildBlockProjection.squarePullback (ChildAssemblyProjection.childProjection q j.val) x
      | Sum.inr j =>
          factorPullback repeats p (childSlots left i) (childSlots right i)
            (ChildAssemblyProjection.childProjection q i) j.2 x

def tapePullback (repeats : Nat → Nat) (p : Path branch n (m + 1))
    (left right : Slots branch n → Fin t → MixedSupport.Slot)
    (q : ∀ s k, MixedSupport.Projection (left s k) (right s k))
    (tape : RecursiveSampler.Tape F2 repeats p (LeafDomain right)) :
    RecursiveSampler.Tape F2 repeats p (LeafDomain left) :=
  fun j => factorPullback repeats p left right q j (tape j)

theorem evaluate_tapePullback (repeats : Nat → Nat) (p : Path branch n (m + 1)) :
    ∀ (left right : Slots branch n → Fin t → MixedSupport.Slot)
      (q : ∀ s k, MixedSupport.Projection (left s k) (right s k))
      (tape : RecursiveSampler.Tape F2 repeats p (LeafDomain right)) (x : Domain left),
      (RecursiveSampler.evaluate F2 repeats p (LeafDomain left)
        (tapePullback repeats p left right q tape)).val x =
      (RecursiveSampler.evaluate F2 repeats p (LeafDomain right) tape).val
        (sourceProjection q x) := by
  induction n generalizing m with
  | zero =>
      have h := p.height_le
      omega
  | succ n ih =>
      cases p with
      | refl =>
          intro left right q tape x
          rfl
      | step i p =>
          intro left right q tape x
          simp only [RecursiveSampler.evaluate_step, RecursiveSampler.combine,
            RecursiveSampler.combineFunction, Finset.sum_apply, Pi.add_apply, Pi.mul_apply,
            PointwiseSpaces.pullback_apply]
          change
            (∑ j : OffPath i,
              (ChildBlockProjection.squarePullback (ChildAssemblyProjection.childProjection q j.val)
                (tape (.inl j))).val (restrictChild left j.val x)) +
              (∑ h : Fin (repeats (n + 1)),
                (RecursiveSampler.evaluate F2 repeats p (LeafDomain (childSlots left i))
                  (tapePullback repeats p (childSlots left i) (childSlots right i)
                    (ChildAssemblyProjection.childProjection q i)
                    (RecursiveSampler.subTape F2 repeats i p (LeafDomain right) tape h false))).val
                      (restrictChild left i x) *
                (RecursiveSampler.evaluate F2 repeats p (LeafDomain (childSlots left i))
                  (tapePullback repeats p (childSlots left i) (childSlots right i)
                    (ChildAssemblyProjection.childProjection q i)
                    (RecursiveSampler.subTape F2 repeats i p (LeafDomain right) tape h true))).val
                      (restrictChild left i x)) =
            (∑ j : OffPath i, (tape (.inl j)).val (restrictChild right j.val (sourceProjection q x))) +
              (∑ h : Fin (repeats (n + 1)),
                (RecursiveSampler.evaluate F2 repeats p (LeafDomain (childSlots right i))
                  (RecursiveSampler.subTape F2 repeats i p (LeafDomain right) tape h false)).val
                    (restrictChild right i (sourceProjection q x)) *
                (RecursiveSampler.evaluate F2 repeats p (LeafDomain (childSlots right i))
                  (RecursiveSampler.subTape F2 repeats i p (LeafDomain right) tape h true)).val
                    (restrictChild right i (sourceProjection q x)))
          apply congrArg₂ (fun a b : F2 => a + b)
          · exact Finset.sum_congr rfl (fun _ _ => rfl)
          · apply Finset.sum_congr rfl
            intro h _
            apply congrArg₂ (fun a b : F2 => a * b)
            · exact ih p (childSlots left i) (childSlots right i)
                (ChildAssemblyProjection.childProjection q i)
                (RecursiveSampler.subTape F2 repeats i p (LeafDomain right) tape h false)
                (restrictChild left i x)
            · exact ih p (childSlots left i) (childSlots right i)
                (ChildAssemblyProjection.childProjection q i)
                (RecursiveSampler.subTape F2 repeats i p (LeafDomain right) tape h true)
                (restrictChild left i x)

theorem terminalFactor_pullback (repeats : Nat → Nat) (p : Path branch n (m + 1)) :
    ∀ (left right : Slots branch n → Fin t → MixedSupport.Slot)
      (q : ∀ s k, MixedSupport.Projection (left s k) (right s k))
      (call : TerminalIndex repeats p)
      (x : RecursiveSampler.DrawSpace F2 repeats p (LeafDomain right)
        (drawIndex repeats p call)),
      TerminalCalls.terminalSpaceEquiv F2 repeats p (LeafDomain left) call
          (factorPullback repeats p left right q (drawIndex repeats p call) x) =
        HPullback (CutGroupedProjection.cutProjection p q)
          (TerminalCalls.terminalSpaceEquiv F2 repeats p (LeafDomain right) call x) := by
  induction n generalizing m with
  | zero =>
      have h := p.height_le
      omega
  | succ n ih =>
      cases p with
      | refl =>
          intro left right q call x
          cases call
          rfl
      | step i p =>
          intro left right q call x
          exact ih p (childSlots left i) (childSlots right i)
            (ChildAssemblyProjection.childProjection q i) call.2 x

theorem exteriorFactor_pullback (repeats : Nat → Nat) (p : Path branch n (m + 1)) :
    ∀ (left right : Slots branch n → Fin t → MixedSupport.Slot)
      (q : ∀ s k, MixedSupport.Projection (left s k) (right s k))
      (hs : ∀ s, WholeCutExteriorTransport.Outside p s → left s = right s)
      (_hk : ∀ s (_hout : WholeCutExteriorTransport.Outside p s) k,
        HEq (q s k) (MixedSupport.Projection.keep (left s k)))
      (j : DrawIndex repeats p) (hj : ¬ IsTerminal repeats p j)
      (x : RecursiveSampler.DrawSpace F2 repeats p (LeafDomain right) j),
      factorPullback repeats p left right q j x =
        cast (factor_eq repeats p right left
          (fun s h => (hs s h).symm) j hj) x := by
  induction n generalizing m with
  | zero =>
      have h := p.height_le
      omega
  | succ n ih =>
      cases p with
      | refl =>
          intro left right q hs hk j hj x
          exact False.elim (hj True.intro)
      | step i p =>
          intro left right q hs hk j hj x
          cases j with
          | inl j =>
              exact CutProjectionGeometry.squarePullback_eq_cast
                (childSlots left j.val) (childSlots right j.val)
                (ChildAssemblyProjection.childProjection q j.val)
                (WholeCutExteriorTransport.ordinarySlots_eq i p left right hs j)
                (fun s k => hk (j.val, s) (WholeCutExteriorTransport.outside_ordinary i p j s) k) x
          | inr j =>
              exact ih p (childSlots left i) (childSlots right i)
                (ChildAssemblyProjection.childProjection q i)
                (fun s h => hs (i, s) (WholeCutExteriorTransport.outside_selected i p s h))
                (fun s h k => hk (i, s) (WholeCutExteriorTransport.outside_selected i p s h) k)
                j.2 hj x

theorem prefixSplit_tapePullback (repeats : Nat → Nat) (p : Path branch n (m + 1))
    (left right : Slots branch n → Fin t → MixedSupport.Slot)
    (q : ∀ s k, MixedSupport.Projection (left s k) (right s k))
    (hs : ∀ s, WholeCutExteriorTransport.Outside p s → left s = right s)
    (hk : ∀ s (_hout : WholeCutExteriorTransport.Outside p s) k,
      HEq (q s k) (MixedSupport.Projection.keep (left s k)))
    (tape : RecursiveSampler.Tape F2 repeats p (LeafDomain right)) :
    OriginalScalarReconstruction.prefixSplit F2 repeats p (LeafDomain left)
        (tapePullback repeats p left right q tape) =
      (terminalPullback repeats p q (OriginalScalarReconstruction.prefixSplit F2 repeats p (LeafDomain right) tape).1,
        cast (scalarExterior_eq repeats p right left
          (fun s h => (hs s h).symm))
          (OriginalScalarReconstruction.prefixSplit F2 repeats p (LeafDomain right) tape).2) := by
  apply Prod.ext
  · funext call
    exact terminalFactor_pullback repeats p left right q call (tape (drawIndex repeats p call))
  · funext j
    exact (exteriorFactor_pullback repeats p left right q hs hk j.val j.property (tape j.val)).trans
      (CutSamplerReplayGroupingTransport.cast_pi_apply
        (scalarExterior_eq repeats p right left
          (fun s h => (hs s h).symm))
        (fun j => factor_eq repeats p right left
          (fun s h => (hs s h).symm) j.val j.property)
        (OriginalScalarReconstruction.prefixSplit F2 repeats p (LeafDomain right) tape).2 j).symm

theorem reconstruct_pullback_apply (repeats : Nat → Nat) (p : Path branch n (m + 1))
    (left right : Slots branch n → Fin t → MixedSupport.Slot)
    (q : ∀ s k, MixedSupport.Projection (left s k) (right s k))
    (hs : ∀ s, WholeCutExteriorTransport.Outside p s → left s = right s)
    (hk : ∀ s (_hout : WholeCutExteriorTransport.Outside p s) k,
      HEq (q s k) (MixedSupport.Projection.keep (left s k)))
    (values : OriginalScalarReconstruction.TerminalValues F2 repeats p (LeafDomain right))
    (exterior : OriginalTerminalSplit.ExteriorTape F2 repeats p (LeafDomain right))
    (x : Domain left) :
    (OriginalScalarReconstruction.reconstruct F2 repeats p (LeafDomain left)
      (fun call => HPullback (CutGroupedProjection.cutProjection p q) (values call))
      (cast (scalarExterior_eq repeats p right left
        (fun s h => (hs s h).symm)) exterior)).val x =
      (OriginalScalarReconstruction.reconstruct F2 repeats p (LeafDomain right)
        values exterior).val (sourceProjection q x) := by
  let tape := (OriginalScalarReconstruction.prefixSplit F2 repeats p (LeafDomain right)).symm
    (values, exterior)
  have hsplit :
      OriginalScalarReconstruction.prefixSplit F2 repeats p (LeafDomain left)
          (tapePullback repeats p left right q tape) =
        (terminalPullback repeats p q values,
          cast (scalarExterior_eq repeats p right left
            (fun s h => (hs s h).symm)) exterior) := by
    simpa only [tape, Equiv.apply_symm_apply] using
      prefixSplit_tapePullback repeats p left right q hs hk tape
  have hleft := congrArg
    (OriginalScalarReconstruction.prefixSplit F2 repeats p (LeafDomain left)).symm hsplit
  simp only [Equiv.symm_apply_apply] at hleft
  unfold OriginalScalarReconstruction.reconstruct
  change (RecursiveSampler.evaluate F2 repeats p (LeafDomain left)
    ((OriginalScalarReconstruction.prefixSplit F2 repeats p (LeafDomain left)).symm
      (terminalPullback repeats p q values,
        cast (scalarExterior_eq repeats p right left
          (fun s h => (hs s h).symm)) exterior))).val x =
    (RecursiveSampler.evaluate F2 repeats p (LeafDomain right) tape).val (sourceProjection q x)
  rw [← hleft]
  exact evaluate_tapePullback repeats p left right q tape x

theorem reconstruct_pullback (repeats : Nat → Nat) (p : Path branch n (m + 1))
    (left right : Slots branch n → Fin t → MixedSupport.Slot)
    (q : ∀ s k, MixedSupport.Projection (left s k) (right s k))
    (hs : ∀ s, WholeCutExteriorTransport.Outside p s → left s = right s)
    (hk : ∀ s (_hout : WholeCutExteriorTransport.Outside p s) k,
      HEq (q s k) (MixedSupport.Projection.keep (left s k)))
    (values : OriginalScalarReconstruction.TerminalValues F2 repeats p (LeafDomain right))
    (exterior : OriginalTerminalSplit.ExteriorTape F2 repeats p (LeafDomain right)) :
    OriginalScalarReconstruction.reconstruct F2 repeats p (LeafDomain left)
      (fun call => HPullback (CutGroupedProjection.cutProjection p q) (values call))
      (cast (scalarExterior_eq repeats p right left
        (fun s h => (hs s h).symm)) exterior) =
      HPullback q (OriginalScalarReconstruction.reconstruct F2 repeats p (LeafDomain right)
        values exterior) := by
  apply Subtype.ext
  funext x
  exact reconstruct_pullback_apply repeats p left right q hs hk values exterior x

end
end PerfectCompleteness.OriginalScalarProjection

end

end OAI