File size: 25,200 Bytes
9b6e014
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
import Mathlib
import OAI.Probability.Contingency.ConcreteKernel

namespace OAI

/-! Concrete Contexts. -/

noncomputable section

open scoped BigOperators
open scoped BigOperators
namespace Contingency.PaddedGraph
variable {m n : ℕ}

def smallRank (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) (s : SmallSlot U r c) : Fin (m*n) :=
  finProdFinEquiv s.val

theorem smallRank_injective (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) :
    Function.Injective (smallRank U r c) := by
  intro s t h
  exact Subtype.ext (finProdFinEquiv.injective h)

 

noncomputable def smallIndex (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) :
    Fin (Fintype.card (SmallSlot U r c)) ≃ SmallSlot U r c := by
  letI := LinearOrder.lift' (smallRank U r c) (smallRank_injective U r c)
  exact (monoEquivOfFin (SmallSlot U r c) rfl).toEquiv

theorem smallRank_lt_iff (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (s t : SmallSlot U r c) : smallRank U r c s < smallRank U r c t ↔ Before s.val t.val := by
  change s.val.2.val+n*s.val.1.val < t.val.2.val+n*t.val.1.val ↔ _
  have hs := s.val.2.isLt
  have ht := t.val.2.isLt
  unfold Before
  constructor
  · intro hh
    by_cases hi : s.val.1 < t.val.1
    · exact Or.inl hi
    · have hti : t.val.1 ≤ s.val.1 := le_of_not_gt hi
      have he : s.val.1=t.val.1 := by
        apply Fin.ext
        by_contra hn
        have hlt : t.val.1.val+1 ≤ s.val.1.val := by omega
        have hm := Nat.mul_le_mul_left n hlt
        simp only [Nat.mul_add,Nat.mul_one] at hm
        omega
      exact Or.inr ⟨he,by rw [he] at hh; omega⟩
  · rintro (hi | ⟨hi,hj⟩)
    · have hm := Nat.mul_le_mul_left n (show s.val.1.val+1 ≤ t.val.1.val by omega)
      simp only [Nat.mul_add,Nat.mul_one] at hm
      omega
    · rw [hi]; omega

theorem smallIndex_lt (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (s t : Fin (Fintype.card (SmallSlot U r c))) :
    Before (smallIndex U r c s).val (smallIndex U r c t).val ↔ s < t := by
  let := LinearOrder.lift' (smallRank U r c) (smallRank_injective U r c)
  rw [← smallRank_lt_iff]
  exact @OrderIso.lt_iff_lt _ _ _
    (LinearOrder.lift' (smallRank U r c) (smallRank_injective U r c)).toPreorder
    (monoEquivOfFin (SmallSlot U r c) rfl) s t

 

noncomputable def contextCoordinates (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) :
    SmallCoord U r c ≃ ((Fin k × Bool) ⊕ PairCoords Bool a) :=
  (((smallIndex U r c).symm.trans (finCongr h)).trans finSumFinEquiv.symm |>.prodCongr (Equiv.refl Bool)).trans
    ((Equiv.sumProdDistrib (Fin k) (Fin (a+1)) Bool).trans
      (Equiv.sumCongr (Equiv.refl _) (smallCoords a).symm))

noncomputable def contextKernel (η : ℝ) (U L C : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1))
    (σ : Fin k → Fin (U+1)) (p : PairCoords Bool a → ℤ) : ℝ :=
  smallKernel η U L C r c ((Sum.elim (balanceProfile (fun _ => U) σ) p) ∘
    contextCoordinates U r c k a h)

theorem balanceProfile_box (U k : ℕ) (σ : Fin k → Fin (U+1)) :
    InBox (fun _ => (U:ℤ)) (balanceProfile (fun _ => U) σ) := by
  intro ⟨s,b⟩
  have hs := (σ s).isLt
  cases b <;> dsimp [balanceProfile] <;> omega

theorem balanceProfile_total (U k : ℕ) (σ : Fin k → Fin (U+1)) :
    (∑ w, balanceProfile (fun _ => U) σ w) = (k:ℤ)*U := by
  rw [Fintype.sum_prod_type]
  simp [balanceProfile]

 

theorem contextKernel_signature (η : ℝ) (hη : 0 < η) (hη1 : η < 1)
    (U L C : ℕ) (hC : 2 ≤ C) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1)) :
    PositiveSlice (contextKernel η U L C r c k a h σ) (fun _ => (U:ℤ)) (((a+1:ℕ):ℤ)*U) ∧
    HasSignature (contextKernel η U L C r c k a h σ) (fun _ => (U:ℤ)) (((a+1:ℕ):ℤ)*U) := by
  obtain ⟨hp,hs⟩ := smallKernel_signature η hη hη1 U L C hC r c
  have hp' := hp.reindex (contextCoordinates U r c k a h)
  have hs' := hs.reindex (contextCoordinates U r c k a h)
  have hbox : (fun _ : SmallCoord U r c => (U:ℤ)) ∘ (contextCoordinates U r c k a h).symm =
      Sum.elim (fun _ : Fin k × Bool => (U:ℤ)) (fun _ : PairCoords Bool a => (U:ℤ)) := by
    funext w; cases w <;> rfl
  rw [hbox] at hp' hs'
  have hp'' := hp'.fixedContext _ (balanceProfile_box U k σ)
  have hs'' := hs'.fixedContext _ (balanceProfile_box U k σ)
  rw [balanceProfile_total,h] at hp'' hs''
  have he : ((k+(a+1):ℕ):ℤ)*U-(k:ℤ)*U = ((a+1:ℕ):ℤ)*U := by push_cast; ring
  rw [he] at hp'' hs''
  exact ⟨hp'',hs''⟩

theorem contextKernel_continuous (U L C : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1))
    (σ : Fin k → Fin (U+1)) (p : PairCoords Bool a → ℤ) :
    Continuous (fun η => contextKernel η U L C r c k a h σ p) :=
  smallKernel_continuous U L C r c _

theorem contextKernel_nonneg (U L C : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1))
    (σ : Fin k → Fin (U+1)) (p : PairCoords Bool a → ℤ) :
    0 ≤ contextKernel 0 U L C r c k a h σ p :=
  smallKernel_nonneg 0 (by norm_num) U L C r c _

theorem context_child_no_holes (U L C : ℕ) (hU : 2 ≤ U) (hC : 2 ≤ C)
    (r : Fin m → ℕ) (c : Fin n → ℕ) (k a : ℕ)
    (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (u v j : ℕ) (huj : u ≤ j) (hjv : j ≤ v) (hv : v ≤ U) :
    min (nodeMass U a (contextKernel 0 U L C r c k a h σ) u)
      (nodeMass U a (contextKernel 0 U L C r c k a h σ) v) ≤
      nodeMass U a (contextKernel 0 U L C r c k a h σ) j :=
  nodeMass_min U hU a _ (contextKernel_continuous U L C r c k a h σ)
    (fun η hη hη' => (contextKernel_signature η hη hη' U L C hC r c k a h σ).1)
    (fun η hη hη' => (contextKernel_signature η hη hη' U L C hC r c k a h σ).2)
    u v j huj hjv hv

end Contingency.PaddedGraph

 

 

open scoped BigOperators
namespace Contingency
universe u

noncomputable def branchEquiv (C : ℕ) : (n : ℕ) → Branches C n ≃ (Fin n → Fin (C+1))
  | 0 => { toFun := fun _ i => Fin.elim0 i
           invFun := fun _ => PUnit.unit
           left_inv := fun x => by cases x; rfl
           right_inv := fun x => by funext i; exact Fin.elim0 i }
  | n+1 => ((Equiv.refl (Fin (C+1))).prodCongr (branchEquiv C n)).trans
      (piHeadEquiv (fun _ : Fin (n+1) => Fin (C+1))).symm

theorem branchEquiv_value (C n : ℕ) (a : Branches C n) (s : Fin n) :
    branchEquiv C n a s = branchValue C n a s := by
  induction n with
  | zero => exact Fin.elim0 s
  | succ n ih =>
    refine Fin.cases ?_ (fun t => ?_) s
    · rfl
    · exact ih a.2 t

theorem allBalance_sum_leaf {ι : Type u} (C n : ℕ)
    (f : (PairCoords ι n → ℤ) → ℝ) (p : ι → ℤ) :
    allBalance C n f p = ∑ a : Branches C n, f (leafDisplay C n p a) := by
  induction n generalizing ι with
  | zero => simp [allBalance,leafDisplay]
  | succ n ih =>
    change (∑ j ∈ Finset.range (C+1), allBalance C n f (pairAssembly p (j:ℤ) ((C:ℤ)-j))) = _
    rw [← Fin.sum_univ_eq_sum_range]
    simp_rw [ih]
    change _ = ∑ a : Fin (C+1) × Branches C n, f (leafDisplay C (n+1) p a)
    rw [Fintype.sum_prod_type]
    rfl

noncomputable def mixedDisplay {ι : Type u} (n : ℕ) (D : Fin n → ℕ)
    (p : ι → ℤ) (a : ∀ s, Fin (D s+1)) : PairCoords ι n → ℤ :=
  Sum.elim p (balanceProfile D a) ∘ pairCoordsEquiv ι n

@[simp] theorem mixedDisplay_base {ι : Type u} (n : ℕ) (D : Fin n → ℕ)
    (p : ι → ℤ) (a : ∀ s, Fin (D s+1)) (i : ι) :
    mixedDisplay n D p a ((pairCoordsEquiv ι n).symm (.inl i)) = p i := by
  simp [mixedDisplay]

@[simp] theorem mixedDisplay_slot {ι : Type u} (n : ℕ) (D : Fin n → ℕ)
    (p : ι → ℤ) (a : ∀ s, Fin (D s+1)) (s : Fin n) (b : Bool) :
    mixedDisplay n D p a ((pairCoordsEquiv ι n).symm (.inr (s,b))) =
      if b then ((a s:ℕ):ℤ) else (D s:ℤ)-(a s:ℕ) := by
  simp [mixedDisplay,balanceProfile]

theorem leafDisplay_mixed {ι : Type u} (C n : ℕ) (p : ι → ℤ) (a : Branches C n) :
    leafDisplay C n p a = mixedDisplay n (fun _ => C) p (branchEquiv C n a) := by
  funext i
  obtain ⟨q,rfl⟩ := (pairCoordsEquiv ι n).symm.surjective i
  rcases q with i | ⟨s,b⟩
  · rw [leafDisplay_base,mixedDisplay_base]
  · rw [leafDisplay_slot,mixedDisplay_slot,branchEquiv_value]

theorem mixedDisplay_injective {ι : Type u} (n : ℕ) (D : Fin n → ℕ) (p : ι → ℤ) :
    Function.Injective (mixedDisplay n D p) := by
  intro a b hab
  funext s
  have hh := congrFun hab ((pairCoordsEquiv ι n).symm (.inr (s,true)))
  simp only [mixedDisplay_slot,ite_true] at hh
  apply Fin.ext
  exact_mod_cast hh

theorem leafDisplay_injective {ι : Type u} (C n : ℕ) (p : ι → ℤ) :
    Function.Injective (leafDisplay C n p) := by
  intro a b hab
  simp only [leafDisplay_mixed] at hab
  exact (branchEquiv C n).injective (mixedDisplay_injective n _ p hab)

noncomputable def mixedBalance {ι : Type u} (n : ℕ) (D : Fin n → ℕ)
    (f : (PairCoords ι n → ℤ) → ℝ) (p : ι → ℤ) : ℝ :=
  ∑ a : ∀ s, Fin (D s+1), f (mixedDisplay n D p a)

theorem allBalance_mixed {ι : Type u} (C n : ℕ)
    (f : (PairCoords ι n → ℤ) → ℝ) (p : ι → ℤ) :
    allBalance C n f p = mixedBalance n (fun _ => C) f p := by
  rw [allBalance_sum_leaf]
  simp only [leafDisplay_mixed,mixedBalance]
  exact Equiv.sum_comp (branchEquiv C n) (fun a => f (mixedDisplay n (fun _ => C) p a))

 

theorem mixedDisplay_succ {ι : Type u} (n : ℕ) (D : Fin (n+1) → ℕ)
    (p : ι → ℤ) (a : ∀ s, Fin (D s+1)) :
    mixedDisplay (n+1) D p a = mixedDisplay n (fun s => D s.succ)
      (pairAssembly p (a 0:ℕ) ((D 0:ℤ)-(a 0:ℕ))) (fun s => a s.succ) := by
  funext i
  obtain ⟨q,rfl⟩ := (pairCoordsEquiv (ι ⊕ Bool) n).symm.surjective i
  rcases q with (i | b) | ⟨s,b⟩ <;>
    simp only [mixedDisplay, Function.comp_apply, pairCoordsEquiv, Equiv.trans_apply,
      Equiv.apply_symm_apply, joinPairHead, Equiv.coe_fn_mk, balanceProfile, pairAssembly,
      Sum.elim_inl, Sum.elim_inr]

theorem mixedBalance_succ {ι : Type u} (n : ℕ) (D : Fin (n+1) → ℕ)
    (f : (PairCoords ι (n+1) → ℤ) → ℝ) (p : ι → ℤ) :
    mixedBalance (n+1) D f p = ∑ j : Fin (D 0+1),
      mixedBalance n (fun s => D s.succ) f (pairAssembly p (j:ℕ) ((D 0:ℤ)-(j:ℕ))) := by
  unfold mixedBalance
  rw [← Equiv.sum_comp (piHeadEquiv (fun s => Fin (D s+1))).symm]
  rw [Fintype.sum_prod_type]
  apply Finset.sum_congr rfl; intro j hj
  apply Finset.sum_congr rfl; intro a ha
  congr 1
  rw [mixedDisplay_succ]
  rfl

noncomputable def surplusCaps (C n : ℕ) (t : Fin n) (s : Fin n) : ℕ :=
  if s=t then C+1 else C

 

theorem allSurplus_sum_types {ι : Type u} (C n : ℕ)
    (f : (PairCoords ι n → ℤ) → ℝ) (p : ι → ℤ) :
    allSurplus C n f p = ∑ t : Fin n, mixedBalance n (surplusCaps C n t) f p := by
  induction n generalizing ι with
  | zero => simp [allSurplus]
  | succ n ih =>
    rw [Fin.sum_univ_succ]
    change balancedMarginal (allBalance C n f) (C+1) p +
      balancedMarginal (allSurplus C n f) C p = _
    congr 1
    · rw [mixedBalance_succ]
      simp only [surplusCaps,ite_eq_left,Fin.succ_ne_zero,ite_false]
      unfold balancedMarginal
      rw [← Fin.sum_univ_eq_sum_range]
      apply Finset.sum_congr rfl; intro j hj
      exact allBalance_mixed C n f _
    · have hs (t : Fin n) : mixedBalance (n+1) (surplusCaps C (n+1) t.succ) f p =
          ∑ j : Fin (C+1), mixedBalance n (surplusCaps C n t) f
            (pairAssembly p (j:ℕ) ((C:ℤ)-(j:ℕ))) := by
        have he : surplusCaps C (n+1) t.succ = Fin.cases C (surplusCaps C n t) := by
          funext s
          refine Fin.cases ?_ (fun v => ?_) s
          · simp [surplusCaps,Ne.symm (Fin.succ_ne_zero t)]
          · simp [surplusCaps]
        rw [he,mixedBalance_succ]
        rfl
      simp_rw [hs]
      rw [Finset.sum_comm]
      unfold balancedMarginal
      rw [← Fin.sum_univ_eq_sum_range]
      apply Finset.sum_congr rfl; intro j hj
      exact ih f _

end Contingency

 

 

open scoped BigOperators
namespace Contingency.PaddedGraph
variable {m n : ℕ}

noncomputable def splitSlots (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) :
    SmallSlot U r c ≃ (Fin k ⊕ Fin (a+1)) :=
  ((smallIndex U r c).symm.trans (finCongr h)).trans finSumFinEquiv.symm

noncomputable def prefixSlot (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (s : Fin k) :=
  (splitSlots U r c k a h).symm (.inl s)

noncomputable def suffixSlot (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (s : Fin (a+1)) :=
  (splitSlots U r c k a h).symm (.inr s)

@[simp] theorem contextCoordinates_prefix (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (s : Fin k) (b : Bool) :
    contextCoordinates U r c k a h (prefixSlot U r c k a h s,b) = .inl (s,b) := by
  change ((Equiv.sumCongr (Equiv.refl _) (smallCoords a).symm)
    ((Equiv.sumProdDistrib (Fin k) (Fin (a+1)) Bool)
      (splitSlots U r c k a h ((splitSlots U r c k a h).symm (.inl s)),b))) = _
  rw [Equiv.apply_symm_apply]; rfl

@[simp] theorem contextCoordinates_suffix (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (s : Fin (a+1)) (b : Bool) :
    contextCoordinates U r c k a h (suffixSlot U r c k a h s,b) =
      .inr ((smallCoords a).symm (s,b)) := by
  change ((Equiv.sumCongr (Equiv.refl _) (smallCoords a).symm)
    ((Equiv.sumProdDistrib (Fin k) (Fin (a+1)) Bool)
      (splitSlots U r c k a h ((splitSlots U r c k a h).symm (.inr s)),b))) = _
  rw [Equiv.apply_symm_apply]; rfl

theorem prefix_before_suffix (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (s : Fin k) (t : Fin (a+1)) :
    Before (prefixSlot U r c k a h s).val (suffixSlot U r c k a h t).val := by
  apply (smallIndex_lt U r c _ _).2
  change ((finCongr h).symm (finSumFinEquiv (.inl s))) <
    ((finCongr h).symm (finSumFinEquiv (.inr t)))
  change s.val < k+t.val
  omega

theorem suffix_before (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (s t : Fin (a+1)) (hst : s<t) :
    Before (suffixSlot U r c k a h s).val (suffixSlot U r c k a h t).val := by
  apply (smallIndex_lt U r c _ _).2
  change k+s.val < k+t.val
  exact Nat.add_lt_add_left hst k

theorem slot_cases (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (w : SmallSlot U r c) :
    (∃ s, w=prefixSlot U r c k a h s) ∨ (∃ s, w=suffixSlot U r c k a h s) := by
  obtain ⟨v,rfl⟩ := (splitSlots U r c k a h).symm.surjective w
  cases v with
  | inl s => exact Or.inl ⟨s,rfl⟩
  | inr s => exact Or.inr ⟨s,rfl⟩

noncomputable def contextState (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (p : PairCoords Bool a → ℤ) : State m n :=
  coordinatesState U r c (Sum.elim (balanceProfile (fun _ => U) σ) p ∘ contextCoordinates U r c k a h)

@[simp] theorem contextState_prefix (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (p : PairCoords Bool a → ℤ) (s : Fin k) :
    (contextState U r c k a h σ p).x (prefixSlot U r c k a h s).val = (σ s:ℕ) ∧
    (contextState U r c k a h σ p).q (prefixSlot U r c k a h s).val = (σ s:ℕ) := by
  simp [contextState,balanceProfile]

@[simp] theorem contextState_suffix (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (p : PairCoords Bool a → ℤ) (s : Fin (a+1)) :
    (contextState U r c k a h σ p).x (suffixSlot U r c k a h s).val = p ((smallCoords a).symm (s,true)) ∧
    (contextState U r c k a h σ p).q (suffixSlot U r c k a h s).val = U-p ((smallCoords a).symm (s,false)) := by
  simp [contextState]

theorem contextState_supported (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (p : PairCoords Bool a → ℤ) (w : Cell m n) (hw : IsLarge U r c w) :
    (contextState U r c k a h σ p).x w=0 ∧ (contextState U r c k a h σ p).q w=0 :=
  coordinatesState_supported U r c _ w hw

theorem contextState_injective (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1)) :
    Function.Injective (contextState U r c k a h σ) := by
  intro p q hpq
  have he := congrArg (stateCoordinates U r c) hpq
  dsimp [contextState] at he
  simp only [stateCoordinates_coordinatesState] at he
  funext i
  have hh := congrFun he ((contextCoordinates U r c k a h).symm (.inr i))
  simpa using hh

theorem contextKernel_weight (U L C : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ)
    (hrc : (∑ i, r i)=∑ j, c j) (hC : ∀ i, paddedRow U L r c i ≤ C)
    (k a : ℕ) (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))
    (p : PairCoords Bool a → ℤ) :
    contextKernel 0 U L C r c k a h σ p = (weight U L r c (contextState U r c k a h σ p):ℝ) := by
  classical
  let X := contextState U r c k a h σ p
  by_cases hx : Feasible U r c X
  · rw [weight,ite_eq_left hx]
    have he := smallKernel_zero_count U L C r c hrc X hx hC
    simpa only [X,contextState,stateCoordinates_coordinatesState,contextKernel] using he
  · rw [weight,ite_eq_right hx,Nat.cast_zero]
    exact smallKernel_zero_infeasible U L C r c _ hx

end Contingency.PaddedGraph

 

 

open scoped BigOperators
namespace Contingency.PaddedGraph
variable {m n : ℕ}

@[ext] theorem State.ext {X Y : State m n} (hx : X.x=Y.x) (hq : X.q=Y.q) : X=Y := by
  cases X; cases Y; simp_all

variable (U : ℕ) (r : Fin m → ℕ) (c : Fin n → ℕ) (k a : ℕ)
    (h : Fintype.card (SmallSlot U r c)=k+(a+1)) (σ : Fin k → Fin (U+1))

@[simp] theorem suffixSlot_val_inj (s t : Fin (a+1)) :
    (suffixSlot U r c k a h s).val=(suffixSlot U r c k a h t).val ↔ s=t := by
  constructor
  · intro he
    have he' := Subtype.ext he
    exact Sum.inr.inj ((splitSlots U r c k a h).symm.injective he')
  · rintro rfl; rfl

@[simp] theorem prefix_suffix_ne (s : Fin k) (t : Fin (a+1)) :
    (prefixSlot U r c k a h s).val ≠ (suffixSlot U r c k a h t).val := by
  intro he
  have he' := Subtype.ext he
  have hh := (splitSlots U r c k a h).symm.injective he'
  cases hh

@[simp] theorem mixedDisplay_current (D : Fin a → ℕ) (p : Bool → ℤ)
    (α : ∀ s, Fin (D s+1)) (b : Bool) :
    mixedDisplay a D p α ((smallCoords a).symm (0,b)) = p b := by
  exact mixedDisplay_base a D p α b

@[simp] theorem mixedDisplay_ordinary (D : Fin a → ℕ) (p : Bool → ℤ)
    (α : ∀ s, Fin (D s+1)) (s : Fin a) (b : Bool) :
    mixedDisplay a D p α ((smallCoords a).symm (s.succ,b)) =
      if b then ((α s:ℕ):ℤ) else (D s:ℤ)-(α s:ℕ) := by
  exact mixedDisplay_slot a D p α s b

 

def defectProfile (j : ℕ) : Bool → ℤ := fun b => if b then (j:ℤ) else (U:ℤ)-j-1

@[simp] theorem adjacent_removePair (j : ℕ) :
    removePair (adjacentDisplay U j) true false = defectProfile U j := by
  funext b; cases b <;> simp [removePair,adjacentDisplay,defectProfile]

noncomputable def defectState (j : ℕ) (t : Fin a) (α : ∀ v, Fin (surplusCaps U a t v+1)) : State m n :=
  contextState U r c k a h σ (mixedDisplay a (surplusCaps U a t) (defectProfile U j) α)

@[simp] theorem defectState_current (j : ℕ) (t : Fin a) (α : ∀ v, Fin (surplusCaps U a t v+1)) :
    (defectState U r c k a h σ j t α).x (suffixSlot U r c k a h 0).val = j ∧
    (defectState U r c k a h σ j t α).q (suffixSlot U r c k a h 0).val = (j:ℤ)+1 := by
  have hh := contextState_suffix U r c k a h σ (mixedDisplay a (surplusCaps U a t) (defectProfile U j) α) 0
  refine ⟨hh.1.trans ?_,hh.2.trans ?_⟩
  · simp only [mixedDisplay_current,defectProfile,ite_true]
  · simp only [mixedDisplay_current,defectProfile,Bool.false_eq_true,ite_false]
    ring

@[simp] theorem defectState_ordinary (j : ℕ) (t v : Fin a) (α : ∀ v, Fin (surplusCaps U a t v+1)) :
    (defectState U r c k a h σ j t α).x (suffixSlot U r c k a h v.succ).val = (α v:ℕ) ∧
    (defectState U r c k a h σ j t α).q (suffixSlot U r c k a h v.succ).val =
      (α v:ℕ)-(if v=t then (1:ℤ) else 0) := by
  have hh := contextState_suffix U r c k a h σ (mixedDisplay a (surplusCaps U a t) (defectProfile U j) α) v.succ
  simp only [mixedDisplay_ordinary,Bool.false_eq_true,ite_true,ite_false] at hh
  refine ⟨hh.1,hh.2.trans ?_⟩
  unfold surplusCaps
  split_ifs <;> push_cast <;> ring

theorem defectState_defect (j : ℕ) (t : Fin a) (α : ∀ v, Fin (surplusCaps U a t v+1)) :
    DefectAt U r c (suffixSlot U r c k a h 0).val (suffixSlot U r c k a h t.succ).val
      (defectState U r c k a h σ j t α) := by
  refine ⟨(suffixSlot U r c k a h 0).property,(suffixSlot U r c k a h t.succ).property,?_,?_⟩
  · rw [ne_eq,suffixSlot_val_inj]
    exact Ne.symm (Fin.succ_ne_zero t)
  · funext w
    by_cases hw : IsSmall U r c w
    · rcases slot_cases U r c k a h ⟨w,hw⟩ with ⟨v,hv⟩ | ⟨v,hv⟩
      · have he : w=(prefixSlot U r c k a h v).val := congrArg Subtype.val hv
        subst w
        have hp := contextState_prefix U r c k a h σ
          (mixedDisplay a (surplusCaps U a t) (defectProfile U j) α) v
        dsimp [defectState] at *
        rw [hp.1,hp.2]
        simp [unit]
      · have he : w=(suffixSlot U r c k a h v).val := congrArg Subtype.val hv
        subst w
        refine Fin.cases ?_ (fun v => ?_) v
        · rw [(defectState_current U r c k a h σ j t α).1,
            (defectState_current U r c k a h σ j t α).2]
          simp [unit,Ne.symm (Fin.succ_ne_zero t)]
        · rw [(defectState_ordinary U r c k a h σ j t v α).1,
            (defectState_ordinary U r c k a h σ j t v α).2]
          simp [unit]
    · have hl : IsLarge U r c w := Classical.not_not.mp hw
      have hh := contextState_supported U r c k a h σ
        (mixedDisplay a (surplusCaps U a t) (defectProfile U j) α) w hl
      have hs : w ≠ (suffixSlot U r c k a h 0).val := by
        rintro rfl; exact (suffixSlot U r c k a h 0).property hl
      have ht : w ≠ (suffixSlot U r c k a h t.succ).val := by
        rintro rfl; exact (suffixSlot U r c k a h t.succ).property hl
      change _ = _-unit _ _+unit _ _
      simp only [defectState] at *
      rw [hh.1,hh.2]; simp [unit,hs,ht]

noncomputable def stateSuffix (X : State m n) (hf : Feasible U r c X) : Fin a → Fin (U+1) :=
  fun v => ⟨(X.x (suffixSlot U r c k a h v.succ).val).toNat,by
    have hh := hf.1 (suffixSlot U r c k a h v.succ).val; omega⟩

 

theorem contextState_reconstruct (X : State m n) (hf : Feasible U r c X)
    (htr : Transversal X) (hp : ∀ v, X.x (prefixSlot U r c k a h v).val=(σ v:ℕ))
    (j : ℕ) (hj : X.x (suffixSlot U r c k a h 0).val=(j:ℤ)) :
    contextState U r c k a h σ (mixedDisplay a (fun _ => U) (childProfile U j)
      (stateSuffix U r c k a h X hf)) = X := by
  apply State.ext
  · funext w
    by_cases hw : IsSmall U r c w
    · rcases slot_cases U r c k a h ⟨w,hw⟩ with ⟨v,hv⟩ | ⟨v,hv⟩
      · have he : w=(prefixSlot U r c k a h v).val := congrArg Subtype.val hv
        subst w
        rw [(contextState_prefix U r c k a h σ _ v).1,hp]
      · have he : w=(suffixSlot U r c k a h v).val := congrArg Subtype.val hv
        subst w
        rw [(contextState_suffix U r c k a h σ _ v).1]
        refine Fin.cases ?_ (fun t => ?_) v
        · simpa [childProfile] using hj.symm
        · simp only [mixedDisplay_ordinary,ite_true,stateSuffix]
          exact Int.toNat_of_nonneg (hf.1 _).1
    · exact (contextState_supported U r c k a h σ _ w (Classical.not_not.mp hw)).1.trans
        ((hf.2.1 w (Classical.not_not.mp hw)).1.symm)
  · funext w
    by_cases hw : IsSmall U r c w
    · rcases slot_cases U r c k a h ⟨w,hw⟩ with ⟨v,hv⟩ | ⟨v,hv⟩
      · have he : w=(prefixSlot U r c k a h v).val := congrArg Subtype.val hv
        subst w
        rw [(contextState_prefix U r c k a h σ _ v).2,htr,hp]
      · have he : w=(suffixSlot U r c k a h v).val := congrArg Subtype.val hv
        subst w
        rw [(contextState_suffix U r c k a h σ _ v).2,htr]
        refine Fin.cases ?_ (fun t => ?_) v
        · simp [childProfile,hj]
        · simp only [mixedDisplay_ordinary,Bool.false_eq_true,ite_false,stateSuffix]
          rw [Int.toNat_of_nonneg (hf.1 _).1]; ring
    · exact (contextState_supported U r c k a h σ _ w (Classical.not_not.mp hw)).2.trans
        ((hf.2.1 w (Classical.not_not.mp hw)).2.symm)

end Contingency.PaddedGraph

end

end OAI