File size: 45,684 Bytes
92416a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
import OAI.Computability.WitnessedChoice.Counting

namespace OAI


namespace WitnessedSeparation.Grid

noncomputable section

open Classical Finset

variable {n : ℕ} {b : Vertex n → Scalar}

variable {J : Type*} [Fintype J]

theorem occurrences_card (a : J → Atom b) : Fintype.card (Occurrences a) ≤ 6*Fintype.card J := by
  rw [Fintype.card_sigma]
  calc
    _ = ∑ j : J, (touch (a j)).card := by congr 1; ext j; exact Fintype.card_coe _
    _ ≤ ∑ _ : J, 6 := sum_le_sum (fun j _ => touch_card (a j))
    _ = _ := by simp [Nat.mul_comm]

theorem augment_card (a : J → Atom b) : Fintype.card (J ⊕ Occurrences a) ≤ 7*Fintype.card J := by
  rw [Fintype.card_sum]
  have := occurrences_card a
  omega

def namesBelow {P : Type*} [Fintype P] {M : ℕ} (h : Fintype.card P < M) : P ↪ Fin M :=
  (Fintype.equivFin P).toEmbedding.trans (Fin.castLEEmb h.le)

theorem namesBelow_spare {P : Type*} [Fintype P] {M : ℕ} (h : Fintype.card P < M) :
    (⟨Fintype.card P,h⟩ : Fin M) ∉ Counting.tupleNames (namesBelow h) := by
  intro hh
  obtain ⟨j,hj⟩ := (Counting.mem_tupleNames _ _).mp hh
  have hv := congrArg Fin.val hj
  change (Fintype.equivFin P j).val = Fintype.card P at hv
  exact (Fintype.equivFin P j).isLt.ne hv

theorem tuple_partial_shift {M : ℕ} [Nonempty (Atom b)] (names : J ↪ Fin M)
    (a a' : J → Atom b) (hw : 7*Fintype.card J < M)
    (ht : Counting.tupleType (analysisStructure b) names a =
      Counting.tupleType (analysisStructure b) names a') :
    ∃ t : Edge n → Scalar, ∀ j, AtomShift t (a j) (a' j) := by
  have hcard : Fintype.card (J ⊕ Occurrences a) < M := lt_of_le_of_lt (augment_card a) hw
  obtain ⟨y,hy⟩ := Counting.tupleType_append (analysisStructure b) (analysisStructure b)
    names (namesBelow hcard) a a' (occurrenceAtom a) ht
  exact augmented_diagram_shift a a' y
    (sameDiagram_of_type _ _ _ hy ⟨_,hcard⟩ (namesBelow_spare hcard))

theorem AtomShift.stateAt_flow {t : Edge n → Scalar} {x y : Atom b}
    (h : AtomShift t x y) (e : Edge n) (he : e ∈ touch x) :
    (stateAt y e (by rw [←touch_shift h]; exact he)).flow =
      t e + (stateAt x e he).flow := by
  cases h with
  | edge f s =>
    have hef : e = f := by simpa [touch,atomBlock,blockEdges] using he
    subst e
    simp only [stateAt_edge (b := b),shiftLocal_flow]
  | config v s h =>
    have hi : incident v e := by simpa [touch,atomBlock,blockEdges] using he
    change ((s.shift t h).state ⟨e,hi⟩).flow = t e + (s.state ⟨e,hi⟩).flow
    simp

theorem AtomShift.unique_on_touch {t u : Edge n → Scalar} {x y : Atom b}
    (ht : AtomShift t x y) (hu : AtomShift u x y) (e : Edge n) (he : e ∈ touch x) :
    t e = u e := by
  have hh := (ht.stateAt_flow e he).symm.trans (hu.stateAt_flow e he)
  exact add_right_cancel hh

end

end WitnessedSeparation.Grid



namespace WitnessedSeparation.Projection

open Finset

variable {X Y Z : Type*} [Fintype X] [Fintype Y] [Fintype Z]

variable [DecidableEq X] [DecidableEq Y] [DecidableEq Z]

private def ind {α : Type*} [DecidableEq α] (S : Finset α) (a : α) : ℕ :=
  if a ∈ S then 1 else 0

private theorem ind_sq {α : Type*} [DecidableEq α] (S : Finset α) (a : α) :
    ind S a ^ 2 = ind S a := by simp [ind]

private theorem sum_ind {α : Type*} [DecidableEq α] [Fintype α] (S : Finset α) :
    ∑ a, ind S a = S.card := by simp [ind]

theorem card_sq_le_projections (R : Finset (X × Y × Z)) :
    R.card^2 ≤ (R.image (fun v => (v.1,v.2.1))).card *
      (R.image (fun v => (v.1,v.2.2))).card * (R.image (fun v => (v.2.1,v.2.2))).card := by
  let A := R.image (fun v => (v.1,v.2.1))
  let B := R.image (fun v => (v.1,v.2.2))
  let D := R.image (fun v => (v.2.1,v.2.2))
  let a (p : X × Y) := ind A p
  let b (p : X × Z) := ind B p
  let d (p : Y × Z) := ind D p
  let c (p : X × Y) := ∑ k, b (p.1,k) * d (p.2,k)
  have hr : R.card ≤ ∑ p, a p * c p := by
    rw [←sum_ind R]
    simp only [Fintype.sum_prod_type]
    dsimp [c]
    simp only [Finset.mul_sum]
    apply Finset.sum_le_sum
    intro i _
    apply Finset.sum_le_sum
    intro j _
    apply Finset.sum_le_sum
    intro k _
    dsimp [a,b,d]
    by_cases h : (i,j,k) ∈ R
    · have ha : (i,j) ∈ A := mem_image.mpr ⟨(i,j,k),h,rfl⟩
      have hb : (i,k) ∈ B := mem_image.mpr ⟨(i,j,k),h,rfl⟩
      have hd : (j,k) ∈ D := mem_image.mpr ⟨(i,j,k),h,rfl⟩
      simp [ind,h,ha,hb,hd]
    · simp [ind,h]
  have hcs := Finset.sum_mul_sq_le_sq_mul_sq (s := (univ : Finset (X × Y))) a c
  have ha : ∑ p : X × Y, a p^2 = A.card := by simp only [a,ind_sq,sum_ind]
  have hc : ∑ p : X × Y, c p^2 ≤ B.card * D.card := by
    calc
      _ ≤ ∑ p : X × Y, (∑ k, b (p.1,k)^2) * (∑ k, d (p.2,k)^2) := by
        apply Finset.sum_le_sum
        intro p _
        exact Finset.sum_mul_sq_le_sq_mul_sq univ (fun k => b (p.1,k)) (fun k => d (p.2,k))
      _ = B.card * D.card := by
        simp only [b,d,ind_sq,Fintype.sum_prod_type]
        rw [←Finset.sum_mul_sum]
        rw [←Fintype.sum_prod_type,←Fintype.sum_prod_type,sum_ind,sum_ind]
  calc
    R.card^2 ≤ (∑ p, a p*c p)^2 := Nat.pow_le_pow_left hr 2
    _ ≤ (∑ p, a p^2) * ∑ p, c p^2 := hcs
    _ ≤ A.card * (B.card*D.card) := by rw [ha]; exact Nat.mul_le_mul_left _ hc
    _ = _ := by dsimp [A,B,D]; simp only [mul_assoc]

end WitnessedSeparation.Projection



namespace WitnessedSeparation.BoxLines

open Finset

variable {J : Type*} [Fintype J] [DecidableEq J] {n : ℕ}

def projection (R : Finset (Fin (n+1) × J)) : Finset J := R.image Prod.snd

def fullLines (R : Finset (Fin (n+1) × J)) : Finset J :=
  univ.filter fun j => ∀ i, (i,j) ∈ R

def edgeBoundary (R : Finset (Fin (n+1) × J)) : Finset (Fin n × J) :=
  univ.filter fun p => ¬ ((p.1.castSucc,p.2) ∈ R ↔ (p.1.succ,p.2) ∈ R)

private theorem fin_constant {P : Fin (n+1) → Prop}
    (h : ∀ i : Fin n, P i.castSucc ↔ P i.succ) (i : Fin (n+1)) : P i ↔ P 0 := by
  induction i using Fin.induction with
  | zero => rfl
  | succ i ih => exact (h i).symm.trans ih

theorem partial_line_boundary (R : Finset (Fin (n+1) × J)) {j : J}
    (hj : j ∈ projection R \ fullLines R) :
    ∃ i : Fin n, (i,j) ∈ edgeBoundary R := by
  classical
  by_contra hn
  have h : ∀ i : Fin n, (i.castSucc,j) ∈ R ↔ (i.succ,j) ∈ R := by
    intro i
    by_contra hh
    exact hn ⟨i,by simp [edgeBoundary,hh]⟩
  obtain ⟨hj,hf⟩ := mem_sdiff.mp hj
  obtain ⟨⟨i,j'⟩,hi,he⟩ := mem_image.mp hj
  dsimp at he
  subst j'
  have hzero := (fin_constant (P := fun i => (i,j) ∈ R) h i).mp hi
  apply hf
  simp only [fullLines,mem_filter,mem_univ,true_and]
  intro i
  exact (fin_constant (P := fun i => (i,j) ∈ R) h i).mpr hzero

theorem line_count (R : Finset (Fin (n+1) × J)) :
    (n+1) * (projection R).card ≤ (n+1) * (edgeBoundary R).card + R.card := by
  classical
  have hF : fullLines R ⊆ projection R := by
    intro j hj
    have h := (mem_filter.mp hj).2
    exact mem_image.mpr ⟨(0,j),h 0,rfl⟩
  have hfull : (n+1) * (fullLines R).card ≤ R.card := by
    have hh : (univ : Finset (Fin (n+1))) ×ˢ fullLines R ⊆ R := by
      rintro ⟨i,j⟩ hp
      exact (mem_filter.mp (mem_product.mp hp).2).2 i
    simpa only [card_product,card_univ,Fintype.card_fin] using card_le_card hh
  have hpartial : (projection R \ fullLines R).card ≤ (edgeBoundary R).card := by
    have hh : projection R \ fullLines R ⊆ (edgeBoundary R).image Prod.snd := by
      intro j hj
      obtain ⟨i,hi⟩ := partial_line_boundary R hj
      exact mem_image.mpr ⟨(i,j),hi,rfl⟩
    exact (card_le_card hh).trans card_image_le
  have hc := card_sdiff_add_card_eq_card hF
  calc
    (n+1) * (projection R).card =
        (n+1) * (projection R \ fullLines R).card + (n+1) * (fullLines R).card := by
      rw [←hc,Nat.mul_add]
    _ ≤ (n+1) * (edgeBoundary R).card + R.card :=
      Nat.add_le_add (Nat.mul_le_mul_left _ hpartial) hfull

end WitnessedSeparation.BoxLines



namespace WitnessedSeparation.Grid

open Finset

variable {n : ℕ}

def edgeBoundary (R : Finset (Vertex n)) : Finset (Edge n) :=
  univ.filter fun e => ¬(tail e ∈ R ↔ head e ∈ R)

def swapXY (v : Vertex n) : Vertex n := (v.2.1,v.1,v.2.2)

def moveZ (v : Vertex n) : Vertex n := (v.2.2,v.1,v.2.1)

theorem swapXY_injective : Function.Injective (@swapXY n) := by
  rintro ⟨i,j,k⟩ ⟨i',j',k'⟩ h
  simp only [swapXY,Prod.mk.injEq] at h ⊢
  tauto

theorem moveZ_injective : Function.Injective (@moveZ n) := by
  rintro ⟨i,j,k⟩ ⟨i',j',k'⟩ h
  simp only [moveZ,Prod.mk.injEq] at h ⊢
  tauto

private theorem boundary_x_le (R : Finset (Vertex n)) :
    (BoxLines.edgeBoundary R).card ≤ (edgeBoundary R).card := by
  apply card_le_card_of_injOn (fun p => ex p.1 p.2.1 p.2.2)
  · rintro ⟨i,j,k⟩ hp
    have hp' := (Finset.mem_filter.mp (show (i,j,k) ∈ BoxLines.edgeBoundary R from hp)).2
    apply Finset.mem_filter.mpr
    exact ⟨mem_univ _,hp'⟩
  · intro p _ q _ h
    exact Sum.inl.inj h

private theorem boundary_y_le (R : Finset (Vertex n)) :
    (BoxLines.edgeBoundary (R.image swapXY)).card ≤ (edgeBoundary R).card := by
  apply card_le_card_of_injOn (fun p => ey p.2.1 p.1 p.2.2)
  · rintro ⟨j,i,k⟩ hp
    have he (v : Vertex n) : swapXY v ∈ R.image swapXY ↔ v ∈ R :=
      by
        constructor
        · intro h
          obtain ⟨w,hw,he⟩ := mem_image.mp h
          simpa only [swapXY_injective he] using hw
        · intro h; exact mem_image.mpr ⟨v,h,rfl⟩
    have h1 := he (i,j.castSucc,k)
    have h2 := he (i,j.succ,k)
    have hp' := (Finset.mem_filter.mp (show (j,i,k) ∈ BoxLines.edgeBoundary (R.image swapXY) from hp)).2
    apply Finset.mem_filter.mpr
    refine ⟨mem_univ _,?_⟩
    change ¬ ((i,j.castSucc,k) ∈ R ↔ (i,j.succ,k) ∈ R)
    change ¬(swapXY (i,j.castSucc,k) ∈ R.image swapXY ↔ swapXY (i,j.succ,k) ∈ R.image swapXY) at hp'
    simpa only [he] using hp'
  · rintro ⟨j,i,k⟩ _ ⟨j',i',k'⟩ _ h
    simp only [ey,Sum.inr.injEq,Sum.inl.injEq,Prod.mk.injEq] at h ⊢
    tauto

private theorem boundary_z_le (R : Finset (Vertex n)) :
    (BoxLines.edgeBoundary (R.image moveZ)).card ≤ (edgeBoundary R).card := by
  apply card_le_card_of_injOn (fun p => ez p.2.1 p.2.2 p.1)
  · rintro ⟨k,i,j⟩ hp
    have he (v : Vertex n) : moveZ v ∈ R.image moveZ ↔ v ∈ R :=
      by
        constructor
        · intro h
          obtain ⟨w,hw,he⟩ := mem_image.mp h
          simpa only [moveZ_injective he] using hw
        · intro h; exact mem_image.mpr ⟨v,h,rfl⟩
    have h1 := he (i,j,k.castSucc)
    have h2 := he (i,j,k.succ)
    have hp' := (Finset.mem_filter.mp (show (k,i,j) ∈ BoxLines.edgeBoundary (R.image moveZ) from hp)).2
    apply Finset.mem_filter.mpr
    refine ⟨mem_univ _,?_⟩
    change ¬ ((i,j,k.castSucc) ∈ R ↔ (i,j,k.succ) ∈ R)
    change ¬(moveZ (i,j,k.castSucc) ∈ R.image moveZ ↔ moveZ (i,j,k.succ) ∈ R.image moveZ) at hp'
    simpa only [he] using hp'
  · rintro ⟨k,i,j⟩ _ ⟨k',i',j'⟩ _ h
    simp only [ez,Sum.inr.injEq,Prod.mk.injEq] at h ⊢
    tauto

theorem projection_line_bounds (R : Finset (Vertex n)) :
    let A := (R.image fun v => (v.1,v.2.1)).card
    let B := (R.image fun v => (v.1,v.2.2)).card
    let D := (R.image fun v => (v.2.1,v.2.2)).card
    (n+1)*A ≤ (n+1)*(edgeBoundary R).card + R.card ∧
    (n+1)*B ≤ (n+1)*(edgeBoundary R).card + R.card ∧
    (n+1)*D ≤ (n+1)*(edgeBoundary R).card + R.card := by
  have hx := (BoxLines.line_count R).trans
    (Nat.add_le_add_right (Nat.mul_le_mul_left (n+1) (boundary_x_le R)) _)
  have hy := (BoxLines.line_count (R.image swapXY)).trans
    (Nat.add_le_add_right (Nat.mul_le_mul_left (n+1) (boundary_y_le R)) _)
  have hz := (BoxLines.line_count (R.image moveZ)).trans
    (Nat.add_le_add_right (Nat.mul_le_mul_left (n+1) (boundary_z_le R)) _)
  simp only [card_image_of_injective _ swapXY_injective,BoxLines.projection,
    image_image,Function.comp_def,swapXY] at hy
  simp only [card_image_of_injective _ moveZ_injective,BoxLines.projection,
    image_image,Function.comp_def,moveZ] at hz
  exact ⟨hz,hy,hx⟩

end WitnessedSeparation.Grid



namespace WitnessedSeparation.Grid

noncomputable def boxConstant : ℝ := 1 - (2:ℝ)^(-(1:ℝ)/3)

theorem boxConstant_pos : 0 < boxConstant := by
  unfold boxConstant
  have h := Real.rpow_lt_rpow_of_exponent_lt (by norm_num : (1:ℝ) < 2)
    (by norm_num : -(1:ℝ)/3 < 0)
  simpa using h

private theorem boundary_algebra {L r d p : ℝ} (hL : 0 < L) (hr : 0 < r)
    (_hd : 0 ≤ d) (hp : 0 ≤ p) (hhalf : 2*r ≤ L^3)
    (hproj : r^2 ≤ p^3) (hline : L*p ≤ L*d+r) :
    boxConstant * r^((2:ℝ)/3) ≤ d := by
  have hr0 := le_of_lt hr
  have h23 : (r^((2:ℝ)/3))^3 = r^2 := by
    rw [←Real.rpow_natCast,←Real.rpow_mul hr0]
    norm_num
  have h13 : (r^((1:ℝ)/3))^3 = r := by
    rw [←Real.rpow_natCast,←Real.rpow_mul hr0]
    norm_num
  have hc3 : ((2:ℝ)^(-(1:ℝ)/3))^3 = (1:ℝ)/2 := by
    rw [←Real.rpow_natCast,←Real.rpow_mul (by norm_num : (0:ℝ) ≤ 2)]
    norm_num
  have hc : 0 ≤ (2:ℝ)^(-(1:ℝ)/3) := Real.rpow_nonneg (by norm_num) _
  have hp' : r^((2:ℝ)/3) ≤ p := by
    apply (pow_le_pow_iff_left₀ (Real.rpow_nonneg hr0 _) hp (by norm_num : (3:ℕ) ≠ 0)).mp
    simpa only [h23] using hproj
  have hl' : r^((1:ℝ)/3) ≤ (2:ℝ)^(-(1:ℝ)/3)*L := by
    apply (pow_le_pow_iff_left₀ (Real.rpow_nonneg hr0 _)
      (mul_nonneg hc hL.le) (by norm_num : (3:ℕ) ≠ 0)).mp
    rw [h13,mul_pow,hc3]
    linarith
  have hprod : r^((1:ℝ)/3)*r^((2:ℝ)/3) = r := by
    rw [←Real.rpow_add hr]
    norm_num
  have hsmall := mul_le_mul_of_nonneg_right hl' (Real.rpow_nonneg hr0 ((2:ℝ)/3))
  rw [hprod] at hsmall
  have hproj' := mul_le_mul_of_nonneg_left hp' hL.le
  unfold boxConstant
  nlinarith

theorem box_boundary {n : ℕ} (R : Finset (Vertex n)) (hne : R.Nonempty)
    (hhalf : 2*R.card ≤ (n+1)^3) :
    boxConstant * (R.card:ℝ)^((2:ℝ)/3) ≤ (edgeBoundary R).card := by
  let A := (R.image fun v => (v.1,v.2.1)).card
  let B := (R.image fun v => (v.1,v.2.2)).card
  let D := (R.image fun v => (v.2.1,v.2.2)).card
  let p := max A (max B D)
  have ha : A ≤ p := le_max_left _ _
  have hb : B ≤ p := (le_max_left B D).trans (le_max_right _ _)
  have hd : D ≤ p := (le_max_right B D).trans (le_max_right _ _)
  have hp : R.card^2 ≤ p^3 := by
    calc
      _ ≤ A*B*D := Projection.card_sq_le_projections R
      _ ≤ p*p*p := Nat.mul_le_mul (Nat.mul_le_mul ha hb) hd
      _ = p^3 := by ring
  obtain ⟨hA,hB,hD⟩ := projection_line_bounds R
  have hline : (n+1)*p ≤ (n+1)*(edgeBoundary R).card + R.card := by
    dsimp [p]
    rw [mul_max_of_nonneg _ _ (Nat.zero_le _),mul_max_of_nonneg _ _ (Nat.zero_le _)]
    exact max_le hA (max_le hB hD)
  apply boundary_algebra (L := (n+1:ℕ)) (p := p)
  · positivity
  · exact_mod_cast Finset.card_pos.mpr hne
  · positivity
  · positivity
  · exact_mod_cast hhalf
  · exact_mod_cast hp
  · exact_mod_cast hline

end WitnessedSeparation.Grid



namespace WitnessedSeparation.Flows

noncomputable section

open Classical

abbrev Scalar := ZMod 3

variable {V E : Type*} [Fintype V] [Fintype E] [DecidableEq V] [DecidableEq E]

def incidence (tail head : E → V) : (E → Scalar) →ₗ[Scalar] (V → Scalar) where
  toFun t := ∑ e, t e • (Pi.single (head e) 1 - Pi.single (tail e) 1)
  map_add' := by intros; simp [add_smul,Finset.sum_add_distrib]
  map_smul' := by intros; simp [smul_smul,Finset.smul_sum]

omit [Fintype V] in
@[simp] theorem incidence_single (tail head : E → V) (e : E) (z : Scalar) :
    incidence tail head (Pi.single e z) = Pi.single (head e) z - Pi.single (tail e) z := by
  change (∑ x, (Pi.single e z : E → Scalar) x •
    (Pi.single (head x) 1 - Pi.single (tail x) 1)) = _
  rw [Finset.sum_eq_single e]
  · simp [smul_sub,←Pi.single_smul]
  · intro b _ hb; simp [Pi.single_eq_of_ne hb]
  · simp

def edgeRelation (tail head : E → V) (T : Finset E) (x y : V) : Prop :=
  ∃ e, e ∉ T ∧ tail e = x ∧ head e = y

def connected (tail head : E → V) (T : Finset E) : Setoid V :=
  Relation.EqvGen.setoid (edgeRelation tail head T)

abbrev Component (tail head : E → V) (T : Finset E) := Quotient (connected tail head T)

def component (tail head : E → V) (T : Finset E) (v : V) : Finset V :=
  Finset.univ.filter fun w => (connected tail head T).r v w

variable (tail head : E → V) (T : Finset E)

omit [Fintype E] [DecidableEq V] [DecidableEq E] in
@[simp] theorem mem_component (v w : V) : w ∈ component tail head T v ↔
    (connected tail head T).r v w := by simp [component]

omit [Fintype V] [Fintype E] [DecidableEq V] [DecidableEq E] in
theorem edge_connected (e : E) (he : e ∉ T) :
    (connected tail head T).r (tail e) (head e) :=
  Relation.EqvGen.rel _ _ ⟨e,he,rfl,rfl⟩

omit [Fintype V] in
theorem path_flow {v w : V} (h : (connected tail head T).r v w) :
    ∃ t : E → Scalar, (∀ e ∈ T, t e = 0) ∧
      incidence tail head t = Pi.single w 1 - Pi.single v 1 := by
  induction h with
  | rel v w h =>
    obtain ⟨e,he,rfl,rfl⟩ := h
    refine ⟨Pi.single e 1,?_,incidence_single tail head e 1⟩
    intro f hf
    exact Pi.single_eq_of_ne (by intro h; subst f; exact he hf) _
  | refl v => exact ⟨0,by simp,by simp⟩
  | symm v w h ih =>
    obtain ⟨t,ht,hd⟩ := ih
    refine ⟨-t,by simpa using ht,?_⟩
    rw [map_neg,hd]; abel
  | trans u v w _ _ ih ih' =>
    obtain ⟨t,ht,hd⟩ := ih
    obtain ⟨t',ht',hd'⟩ := ih'
    refine ⟨t+t',fun e he => by simp [ht e he,ht' e he],?_⟩
    rw [map_add,hd,hd']; abel

omit [Fintype V] in
private theorem sum_single (U : Finset V) (v : V) (z : Scalar) :
    ∑ x ∈ U, (Pi.single v z : V → Scalar) x = if v ∈ U then z else 0 := by
  simp [Pi.single_apply]

omit [DecidableEq E] in
theorem sum_incidence (t : E → Scalar) : ∑ v, incidence tail head t v = 0 := by
  change (∑ v, (∑ e, t e • (Pi.single (head e) 1 - Pi.single (tail e) 1) : V → Scalar) v) = _
  simp only [Finset.sum_apply,Pi.smul_apply,smul_eq_mul,Pi.sub_apply,mul_sub]
  rw [Finset.sum_comm]
  have h (e : E) : ∑ v, (t e * (Pi.single (head e) (1:Scalar) : V → Scalar) v -
      t e * (Pi.single (tail e) (1:Scalar) : V → Scalar) v) = 0 := by
    rw [Finset.sum_sub_distrib,←Finset.mul_sum,←Finset.mul_sum]
    simp [Pi.single_apply]
  simp only [h,Finset.sum_const_zero]

theorem sum_incidence_component (t : E → Scalar) (ht : ∀ e ∈ T, t e = 0) (v : V) :
    ∑ w ∈ component tail head T v, incidence tail head t w = 0 := by
  change (∑ w ∈ component tail head T v,
    (∑ e, t e • (Pi.single (head e) 1 - Pi.single (tail e) 1) : V → Scalar) w) = _
  simp only [Finset.sum_apply,Pi.smul_apply,smul_eq_mul,Pi.sub_apply,mul_sub]
  rw [Finset.sum_comm]
  apply Finset.sum_eq_zero
  intro e _
  rw [Finset.sum_sub_distrib,←Finset.mul_sum,←Finset.mul_sum,sum_single,sum_single]
  by_cases he : e ∈ T
  · simp [ht e he]
  have hc : head e ∈ component tail head T v ↔ tail e ∈ component tail head T v := by
    simp only [mem_component]
    exact ⟨fun h => (connected tail head T).trans h ((connected tail head T).symm (edge_connected tail head T e he)),
      fun h => (connected tail head T).trans h (edge_connected tail head T e he)⟩
  simp only [hc,sub_self]

theorem solvable_iff (b : V → Scalar) :
    (∃ t : E → Scalar, (∀ e ∈ T, t e = 0) ∧ incidence tail head t = b) ↔
      ∀ v, ∑ w ∈ component tail head T v, b w = 0 := by
  constructor
  · rintro ⟨t,ht,rfl⟩
    exact sum_incidence_component tail head T t ht
  · intro hb
    let q : V → Component tail head T := Quotient.mk (connected tail head T)
    let : Fintype (Component tail head T) := Fintype.ofFinite _
    have hp (v : V) : ∃ t : E → Scalar, (∀ e ∈ T, t e = 0) ∧
        incidence tail head t = Pi.single v 1 - Pi.single ((q v).out) 1 := by
      apply path_flow tail head T
      exact Quotient.exact (Quotient.out_eq (q v))
    choose t ht hd using hp
    refine ⟨∑ v, b v • t v,?_,?_⟩
    · intro e he
      simp [Finset.sum_apply,ht _ e he]
    · rw [map_sum]
      simp only [map_smul,hd,smul_sub,Finset.sum_sub_distrib]
      have hfirst : (∑ v, b v • (Pi.single v (1:Scalar) : V → Scalar)) = b := by
        funext j; simp [Finset.sum_apply,Pi.smul_apply,Pi.single_apply]
      have hsecond : (∑ v, b v • (Pi.single (q v).out (1:Scalar) : V → Scalar)) = 0 := by
        rw [←Fintype.sum_fiberwise q]
        apply Finset.sum_eq_zero
        intro c _
        have hf : (∑ v : {v : V // q v = c}, b v) = 0 := by
          have he (v : V) : q v = c ↔ v ∈ component tail head T c.out := by
            rw [mem_component]
            constructor
            · intro h
              exact Quotient.exact ((Quotient.out_eq c).trans h.symm)
            · intro h
              exact (Quotient.sound h).symm.trans (Quotient.out_eq c)
          let ee := Equiv.subtypeEquivRight he
          have hs := Fintype.sum_equiv ee (fun v : {v : V // q v = c} => b v)
            (fun v : component tail head T c.out => b v) (fun _ => rfl)
          rw [hs]
          exact (Finset.sum_coe_sort (component tail head T c.out) b).trans (hb c.out)
        have hx : (∑ v : {v : V // q v = c}, b v •
            (Pi.single (q v).out (1:Scalar) : V → Scalar)) =
            (∑ v : {v : V // q v = c}, b v) • Pi.single c.out 1 := by
          rw [Finset.sum_smul]
          apply Finset.sum_congr rfl
          intro v _; rw [v.2]
        rw [hx,hf,zero_smul]
      rw [hfirst,hsecond,sub_zero]

theorem partial_flow_iff (σ : E → Scalar) :
    (∃ k : E → Scalar, incidence tail head k = 0 ∧ ∀ e ∈ T, k e = σ e) ↔
      ∀ v, ∑ w ∈ component tail head T v,
        incidence tail head (fun e => if e ∈ T then σ e else 0) w = 0 := by
  let z : E → Scalar := fun e => if e ∈ T then σ e else 0
  constructor
  · rintro ⟨k,hk,he⟩ v
    have ht : ∀ e ∈ T, (k-z) e = 0 := by intro e h; simp [z,h,he e h]
    have h := sum_incidence_component tail head T (k-z) ht v
    simpa [map_sub,hk,Finset.sum_neg_distrib] using h
  · intro h
    have hb : ∀ v, ∑ w ∈ component tail head T v, (-incidence tail head z) w = 0 := by
      intro v; simpa [Finset.sum_neg_distrib] using congrArg Neg.neg (h v)
    obtain ⟨t,ht,hd⟩ := (solvable_iff tail head T (-incidence tail head z)).mpr hb
    refine ⟨z+t,by simp [hd],?_⟩
    intro e he; simp [z,he,ht e he]

end





noncomputable section

open Classical Finset

variable {V E : Type*} [Fintype V] [Fintype E] [DecidableEq V] [DecidableEq E]

variable (tail head : E → V) (T : Finset E)

instance : Fintype (Component tail head T) := Fintype.ofFinite _

def cell (c : Component tail head T) : Finset V := component tail head T c.out

def quotientMap (v : V) : Component tail head T := Quotient.mk (connected tail head T) v

omit [Fintype E] [DecidableEq V] [DecidableEq E] in
@[simp] theorem mem_cell (c : Component tail head T) (v : V) :
    v ∈ cell tail head T c ↔ quotientMap tail head T v = c := by
  rw [cell,mem_component]
  constructor
  · intro h
    exact (Quotient.sound h).symm.trans (Quotient.out_eq c)
  · intro h
    exact Quotient.exact ((Quotient.out_eq c).trans h.symm)

omit [Fintype E] [DecidableEq V] [DecidableEq E] in
theorem cell_nonempty (c : Component tail head T) : (cell tail head T c).Nonempty :=
  ⟨c.out,(mem_cell tail head T c c.out).mpr (Quotient.out_eq c)⟩

omit [Fintype E] [DecidableEq V] [DecidableEq E] in
theorem cells_disjoint {c d : Component tail head T} (h : c ≠ d) :
    Disjoint (cell tail head T c) (cell tail head T d) := by
  apply disjoint_left.mpr
  intro v hc hd
  exact h ((mem_cell tail head T c v).mp hc |>.symm.trans ((mem_cell tail head T d v).mp hd))

omit [Fintype E] [DecidableEq V] [DecidableEq E] in
theorem sum_cells {M : Type*} [AddCommMonoid M] (f : V → M) :
    ∑ c : Component tail head T, ∑ v ∈ cell tail head T c, f v = ∑ v, f v := by
  have he (c : Component tail head T) :
      (∑ v ∈ cell tail head T c, f v) = ∑ v, if quotientMap tail head T v = c then f v else 0 := by
    rw [←sum_filter]
    congr 1
    ext v; simp
  simp only [he]
  rw [sum_comm]
  simp

omit [Fintype E] [DecidableEq V] [DecidableEq E] in
theorem sum_card_cells : ∑ c : Component tail head T, (cell tail head T c).card = Fintype.card V := by
  simpa using sum_cells tail head T (fun _ => (1:ℕ))

def edgeBoundary (U : Finset V) : Finset E := univ.filter fun e => ¬(tail e ∈ U ↔ head e ∈ U)

theorem boundary_cell_subset (c : Component tail head T) :
    edgeBoundary tail head (cell tail head T c) ⊆ T := by
  intro e he
  by_contra hn
  have hc := edge_connected tail head T e hn
  have hq : quotientMap tail head T (tail e) = quotientMap tail head T (head e) := Quotient.sound hc
  have hh := (mem_filter.mp he).2
  exact hh (by simp only [mem_cell,hq])

theorem sum_boundary_cells :
    ∑ c : Component tail head T, (edgeBoundary tail head (cell tail head T c)).card ≤ 2*T.card := by
  have he (c : Component tail head T) :
      (edgeBoundary tail head (cell tail head T c)).card =
      ∑ e ∈ T, if e ∈ edgeBoundary tail head (cell tail head T c) then 1 else 0 := by
    rw [←card_filter]
    congr 1
    ext e
    simp only [mem_filter]
    exact ⟨fun h => ⟨boundary_cell_subset tail head T c h,h⟩,fun h => h.2⟩
  simp only [he]
  rw [sum_comm]
  calc
    _ ≤ ∑ _ ∈ T, 2 := by
      apply sum_le_sum
      intro e _
      calc
        _ ≤ ∑ c : Component tail head T,
            ((if quotientMap tail head T (tail e) = c then 1 else 0) +
             (if quotientMap tail head T (head e) = c then 1 else 0)) := by
          apply sum_le_sum
          intro c _
          simp only [edgeBoundary,mem_filter,mem_univ,true_and,mem_cell]
          by_cases h1 : quotientMap tail head T (tail e) = c <;>
            by_cases h2 : quotientMap tail head T (head e) = c <;> simp [h1,h2]
        _ = 2 := by rw [sum_add_distrib]; simp
    _ = 2*T.card := by simp [mul_comm]

omit [Fintype E] [DecidableEq E] in


theorem small_failing_component (b : V → Scalar) (hb : ∑ v, b v = 0)
    (hfail : ∃ c : Component tail head T, (∑ v ∈ cell tail head T c, b v) ≠ 0) :
    ∃ c : Component tail head T, (∑ v ∈ cell tail head T c, b v) ≠ 0 ∧
      2*(cell tail head T c).card ≤ Fintype.card V := by
  obtain ⟨c,hc⟩ := hfail
  by_cases hsmall : 2*(cell tail head T c).card ≤ Fintype.card V
  · exact ⟨c,hc,hsmall⟩
  have he : ∃ d : Component tail head T, d ≠ c ∧ (∑ v ∈ cell tail head T d, b v) ≠ 0 := by
    by_contra hh
    push Not at hh
    have hsum : (∑ d : Component tail head T, ∑ v ∈ cell tail head T d, b v) =
        ∑ v ∈ cell tail head T c, b v := by
      apply sum_eq_single c
      · intro d _ hd; exact hh d hd
      · simp
    rw [sum_cells,hb] at hsum
    exact hc hsum.symm
  obtain ⟨d,hd,hfd⟩ := he
  refine ⟨d,hfd,?_⟩
  have hdis := cells_disjoint tail head T hd.symm
  have hcard := card_le_card (subset_univ (cell tail head T c ∪ cell tail head T d))
  rw [card_union_of_disjoint hdis,card_univ] at hcard
  omega

end

end WitnessedSeparation.Flows



namespace WitnessedSeparation.Grid

noncomputable section

open Classical Finset

variable {n : ℕ}

private theorem bound_of_boundary {r b : ℝ} (hr : 0 ≤ r) (_hb : 0 ≤ b)
    (h : boxConstant * r^((2:ℝ)/3) ≤ b) : r ≤ (b/boxConstant)^((3:ℝ)/2) := by
  have hu : r^((2:ℝ)/3) ≤ b/boxConstant := by
    apply (le_div_iff₀ boxConstant_pos).mpr
    simpa only [mul_comm] using h
  have hv := Real.rpow_le_rpow (Real.rpow_nonneg hr _) hu (by norm_num : (0:ℝ) ≤ 3/2)
  rw [←Real.rpow_mul hr] at hv
  norm_num at hv
  exact hv

theorem small_cell_bound (T : Finset (Edge n)) (c : Flows.Component tail head T)
    (hsmall : 2*(Flows.cell tail head T c).card ≤ (n+1)^3) :
    ((Flows.cell tail head T c).card:ℝ) ≤
      ((Flows.edgeBoundary tail head (Flows.cell tail head T c)).card / boxConstant)^((3:ℝ)/2) :=
  bound_of_boundary (by positivity) (by positivity)
    (box_boundary _ (Flows.cell_nonempty tail head T c) hsmall)

theorem partial_flow_small_obstruction (T : Finset (Edge n)) (σ : Edge n → Scalar)
    (hfail : ¬ ∃ k : Edge n → Scalar, Flows.incidence tail head k = 0 ∧
      ∀ e ∈ T, k e = σ e) :
    ∃ c : Flows.Component tail head T,
      (∑ v ∈ Flows.cell tail head T c,
        Flows.incidence tail head (fun e => if e ∈ T then σ e else 0) v) ≠ 0 ∧
      2*(Flows.cell tail head T c).card ≤ (n+1)^3 ∧
      ((Flows.cell tail head T c).card:ℝ) ≤
        (T.card/boxConstant)^((3:ℝ)/2) := by
  let z : Edge n → Scalar := fun e => if e ∈ T then σ e else 0
  have hf : ∃ v, (∑ w ∈ Flows.component tail head T v, Flows.incidence tail head z w) ≠ 0 := by
    by_contra hh
    push Not at hh
    exact hfail ((Flows.partial_flow_iff tail head T σ).mpr hh)
  obtain ⟨v,hv⟩ := hf
  have hcell : Flows.cell tail head T (Flows.quotientMap tail head T v) =
      Flows.component tail head T v := by
    ext w
    rw [Flows.mem_cell,Flows.mem_component]
    exact ⟨fun h => Quotient.exact h.symm,fun h => (Quotient.sound h).symm⟩
  obtain ⟨c,hc,hs⟩ := Flows.small_failing_component tail head T (Flows.incidence tail head z)
    (Flows.sum_incidence tail head z) ⟨Flows.quotientMap tail head T v,by simpa only [hcell] using hv⟩
  have hcard : Fintype.card (Vertex n) = (n+1)^3 := by simp only [Vertex,Fintype.card_prod,Fintype.card_fin]; ring
  rw [hcard] at hs
  refine ⟨c,hc,hs,(small_cell_bound T c hs).trans ?_⟩
  apply Real.rpow_le_rpow (div_nonneg (Nat.cast_nonneg _) boxConstant_pos.le) _ (by norm_num)
  apply div_le_div_of_nonneg_right _ boxConstant_pos.le
  exact_mod_cast card_le_card (Flows.boundary_cell_subset tail head T c)

private theorem sum_rpow_le {ι : Type*} (s : Finset ι) (f : ι → ℝ)
    (hf : ∀ i ∈ s, 0 ≤ f i) :
    ∑ i ∈ s, (f i)^((3:ℝ)/2) ≤ (∑ i ∈ s, f i)^((3:ℝ)/2) := by
  classical
  induction s using Finset.induction_on with
  | empty => simp
  | @insert a s ha ih =>
    rw [sum_insert ha,sum_insert ha]
    have hs : ∀ i ∈ s, 0 ≤ f i := fun i hi => hf i (mem_insert_of_mem hi)
    calc
      f a ^ ((3:ℝ)/2) + ∑ i ∈ s, f i ^ ((3:ℝ)/2) ≤
          f a ^ ((3:ℝ)/2) + (∑ i ∈ s, f i) ^ ((3:ℝ)/2) := add_le_add (le_refl _) (ih hs)
      _ ≤ (f a + ∑ i ∈ s, f i) ^ ((3:ℝ)/2) :=
        Real.add_rpow_le_rpow_add (hf a (mem_insert_self _ _)) (sum_nonneg hs) (by norm_num)

theorem small_components_bound (T : Finset (Edge n)) (S : Finset (Flows.Component tail head T))
    (hs : ∀ c ∈ S, 2*(Flows.cell tail head T c).card ≤ (n+1)^3) :
    (∑ c ∈ S, ((Flows.cell tail head T c).card:ℝ)) ≤
      ((2*T.card:ℝ)/boxConstant)^((3:ℝ)/2) := by
  let b (c : Flows.Component tail head T) : ℝ :=
    (Flows.edgeBoundary tail head (Flows.cell tail head T c)).card / boxConstant
  have hn (c) : 0 ≤ b c := div_nonneg (Nat.cast_nonneg _) boxConstant_pos.le
  calc
    _ ≤ ∑ c ∈ S, (b c)^((3:ℝ)/2) := sum_le_sum (fun c hc => small_cell_bound T c (hs c hc))
    _ ≤ (∑ c ∈ S, b c)^((3:ℝ)/2) := sum_rpow_le S b (fun c _ => hn c)
    _ ≤ ((2*T.card:ℝ)/boxConstant)^((3:ℝ)/2) := by
      apply Real.rpow_le_rpow (sum_nonneg (fun c _ => hn c)) _ (by norm_num)
      dsimp [b]
      rw [←sum_div]
      apply div_le_div_of_nonneg_right _ boxConstant_pos.le
      calc
        _ ≤ ∑ c : Flows.Component tail head T,
            ((Flows.edgeBoundary tail head (Flows.cell tail head T c)).card:ℝ) :=
          sum_le_sum_of_subset_of_nonneg (subset_univ _) (fun _ _ _ => by positivity)
        _ ≤ 2*T.card := by exact_mod_cast Flows.sum_boundary_cells tail head T

theorem exists_giant (T : Finset (Edge n))
    (hnum : ((2*T.card:ℝ)/boxConstant)^((3:ℝ)/2) < ((n+1)^3:ℕ)) :
    ∃ c : Flows.Component tail head T, (n+1)^3 < 2*(Flows.cell tail head T c).card := by
  by_contra hh
  push Not at hh
  have h := small_components_bound T univ (fun c _ => hh c)
  rw [←Nat.cast_sum,Flows.sum_card_cells] at h
  have hc : Fintype.card (Vertex n) = (n+1)^3 := by simp only [Vertex,Fintype.card_prod,Fintype.card_fin]; ring
  rw [hc] at h
  exact (not_lt_of_ge h) hnum

end

end WitnessedSeparation.Grid



namespace WitnessedSeparation.Flows

noncomputable section

open Classical Finset

variable {V E : Type*} [Fintype V] [Fintype E] [DecidableEq V] [DecidableEq E]

variable (tail head : E → V)

omit [Fintype V] [Fintype E] [DecidableEq V] [DecidableEq E] in
theorem connected_mono {T U : Finset E} (hT : T ⊆ U) {v w : V}
    (h : (connected tail head U).r v w) : (connected tail head T).r v w := by
  induction h with
  | rel v w h =>
    obtain ⟨e,he,rfl,rfl⟩ := h
    exact edge_connected tail head T e (fun hh => he (hT hh))
  | refl v => exact .refl v
  | symm v w h ih => exact .symm _ _ ih
  | trans v w z h h' ih ih' => exact .trans _ _ _ ih ih'

omit [Fintype E] [DecidableEq V] [DecidableEq E] in
theorem component_mono {T U : Finset E} (hT : T ⊆ U) (v : V) :
    component tail head U v ⊆ component tail head T v := by
  intro w hw
  exact (mem_component _ _ _ _ _).mpr (connected_mono tail head hT ((mem_component _ _ _ _ _).mp hw))

omit [Fintype E] [DecidableEq V] [DecidableEq E] in
theorem component_eq {T : Finset E} {v w : V} (h : (connected tail head T).r v w) :
    component tail head T v = component tail head T w := by
  ext x; simp only [mem_component]
  exact ⟨fun hv => (connected tail head T).trans ((connected tail head T).symm h) hv,
    fun hw => (connected tail head T).trans h hw⟩

def IsGiant (T : Finset E) (v : V) : Prop :=
  Fintype.card V < 2*(component tail head T v).card

omit [Fintype E] [DecidableEq V] [DecidableEq E] in
theorem giant_mono {T U : Finset E} (hT : T ⊆ U) {v : V} (h : IsGiant tail head U v) :
    IsGiant tail head T v := lt_of_lt_of_le h (Nat.mul_le_mul_left 2 (card_le_card (component_mono tail head hT v)))

omit [Fintype E] [DecidableEq E] in
theorem giants_connected {T : Finset E} {v w : V}
    (hv : IsGiant tail head T v) (hw : IsGiant tail head T w) : (connected tail head T).r v w := by
  by_contra hh
  have hd : Disjoint (component tail head T v) (component tail head T w) := by
    apply disjoint_left.mpr
    intro x hx hx'
    exact hh ((connected tail head T).trans ((mem_component _ _ _ _ _).mp hx)
      ((connected tail head T).symm ((mem_component _ _ _ _ _).mp hx')))
  have hk : (component tail head T v).card + (component tail head T w).card ≤ Fintype.card V := by
    rw [←card_union_of_disjoint hd]
    exact card_le_univ _
  dsimp [IsGiant] at hv hw
  omega

omit [Fintype E] [DecidableEq V] [DecidableEq E] in

theorem component_isolated {T : Finset E} (v : V)
    (h : ∀ e, tail e = v ∨ head e = v → e ∈ T) : component tail head T v = {v} := by
  have hh {x y : V} (hc : (connected tail head T).r x y) : x = v ↔ y = v := by
    induction hc with
    | rel x y hr =>
      obtain ⟨e,he,rfl,rfl⟩ := hr
      exact ⟨fun hv => False.elim (he (h e (Or.inl hv))),fun hv => False.elim (he (h e (Or.inr hv)))⟩
    | refl x => rfl
    | symm x y hc ih => exact ih.symm
    | trans x y z hc hc' ih ih' => exact ih.trans ih'
  ext w
  simp only [mem_component,mem_singleton]
  exact ⟨fun hw => (hh hw).mp rfl,fun hw => by subst w; exact (connected tail head T).refl _⟩

omit [Fintype E] [DecidableEq V] [DecidableEq E] in
theorem giant_ne_isolated {T : Finset E} {w v : V} (hV : 2 ≤ Fintype.card V)
    (hw : IsGiant tail head T w) (h : ∀ e, tail e = v ∨ head e = v → e ∈ T) : w ≠ v := by
  intro he; subst w
  have hh := component_isolated tail head v h
  dsimp [IsGiant] at hw
  rw [hh,card_singleton] at hw
  omega

end

end WitnessedSeparation.Flows



namespace WitnessedSeparation.Grid

noncomputable section

open Classical Finset

variable {n : ℕ}

theorem grid_connected (v w : Vertex n) : (Flows.connected tail head ∅).r v w := by
  have hx (i j k : Fin (n+1)) : (Flows.connected tail head ∅).r (0,j,k) (i,j,k) := by
    induction i using Fin.induction with
    | zero => exact (Flows.connected tail head ∅).refl _
    | succ i ih =>
      exact (Flows.connected tail head ∅).trans ih (Flows.edge_connected tail head ∅ (ex i j k) (by simp))
  have hy (i j k : Fin (n+1)) : (Flows.connected tail head ∅).r (i,0,k) (i,j,k) := by
    induction j using Fin.induction with
    | zero => exact (Flows.connected tail head ∅).refl _
    | succ j ih =>
      exact (Flows.connected tail head ∅).trans ih (Flows.edge_connected tail head ∅ (ey i j k) (by simp))
  have hz (i j k : Fin (n+1)) : (Flows.connected tail head ∅).r (i,j,0) (i,j,k) := by
    induction k using Fin.induction with
    | zero => exact (Flows.connected tail head ∅).refl _
    | succ k ih =>
      exact (Flows.connected tail head ∅).trans ih (Flows.edge_connected tail head ∅ (ez i j k) (by simp))
  have h (v : Vertex n) : (Flows.connected tail head ∅).r (0,0,0) v :=
    (Flows.connected tail head ∅).trans (hx v.1 0 0)
      ((Flows.connected tail head ∅).trans (hy v.1 v.2.1 0) (hz v.1 v.2.1 v.2.2))
  exact (Flows.connected tail head ∅).trans ((Flows.connected tail head ∅).symm (h v)) (h w)

theorem grid_empty_component (v : Vertex n) : Flows.component tail head ∅ v = univ := by
  ext w; simp only [Flows.mem_component,mem_univ,iff_true]; exact grid_connected v w

theorem grid_empty_giant (v : Vertex n) : Flows.IsGiant tail head ∅ v := by
  dsimp [Flows.IsGiant]
  rw [grid_empty_component,card_univ]
  have hpos : 0 < Fintype.card (Vertex n) := Fintype.card_pos_iff.mpr ⟨(0,0,0)⟩
  omega

end





noncomputable section

open Classical Finset

variable {n M : ℕ} {b : Vertex n → Scalar}

theorem AtomShift.config_boundary {t : Edge n → Scalar} {v : Vertex n}
    {s : Configuration b v} {y : Atom b} (h : AtomShift t (.inr ⟨v,s⟩) y) :
    boundary t v = 0 := by
  cases h with
  | config v s h => simpa only [sub_self] using h

theorem quantitative_homogeneity_shift (hn : 1 ≤ n) {J : Type*} [Fintype J] [Nonempty (Atom b)]
    (names : J ↪ Fin M) (a a' : J → Atom b)
    (hw : (7*Fintype.card J:ℝ) + 7*((6*Fintype.card J:ℝ)/boxConstant)^((3:ℝ)/2)+1 ≤ M)
    (ht : Counting.tupleType (analysisStructure b) names a =
      Counting.tupleType (analysisStructure b) names a') :
    ∃ k : Edge n → Scalar, boundary k = 0 ∧ ∀ j, AtomShift k (a j) (a' j) := by
  let : Nonempty (Atom b) := ⟨.inr ⟨(0,0,0),someConfiguration hn b (0,0,0)⟩⟩
  have hp : 0 ≤ ((6*Fintype.card J:ℝ)/boxConstant)^((3:ℝ)/2) := Real.rpow_nonneg (div_nonneg (by positivity) boxConstant_pos.le) _
  have hw' : 7*Fintype.card J < M := by
    have hh : (7*Fintype.card J:ℝ)+1 ≤ M := by linarith
    have hh' : 7*Fintype.card J+1 ≤ M := by exact_mod_cast hh
    omega
  obtain ⟨σ,hσ⟩ := tuple_partial_shift names a a' hw' ht
  let T : Finset (Edge n) := touches univ a
  have hT : T.card ≤ 6*Fintype.card J := by simpa [T] using touches_card univ a
  suffices hs : ∃ k : Edge n → Scalar, Flows.incidence tail head k = 0 ∧ ∀ e ∈ T, k e = σ e by
    obtain ⟨k,hk,he⟩ := hs
    refine ⟨k,hk,fun j => (hσ j).congr ?_⟩
    intro e hej
    exact (he e (touch_subset_touches univ a (mem_univ j) hej)).symm
  by_contra hs
  obtain ⟨C,hbad,hsmall,hcard⟩ := partial_flow_small_obstruction T σ hs
  let U := Flows.cell tail head T C
  have hU : (U.card:ℝ) ≤ ((6*Fintype.card J:ℝ)/boxConstant)^((3:ℝ)/2) := by
    apply hcard.trans
    apply Real.rpow_le_rpow (div_nonneg (Nat.cast_nonneg _) boxConstant_pos.le) _ (by norm_num)
    apply div_le_div_of_nonneg_right _ boxConstant_pos.le
    exact_mod_cast hT
  let conf (v : U) : Atom b := .inr ⟨v.val,someConfiguration hn b v.val⟩
  have hwU : 7*Fintype.card (J ⊕ U) < M := by
    have hh : (7*(Fintype.card J+U.card):ℝ)+1 ≤ M := by linarith
    have hh' : 7*(Fintype.card J+U.card)+1 ≤ M := by exact_mod_cast hh
    simpa only [Fintype.card_sum,Fintype.card_coe] using (show 7*(Fintype.card J+U.card)<M by omega)
  have hcardU : Fintype.card (J ⊕ U) < M := by omega
  obtain ⟨conf',htU⟩ := Counting.tupleType_append (analysisStructure b) (analysisStructure b)
    names (namesBelow hcardU) a a' conf ht
  obtain ⟨d,hd⟩ := tuple_partial_shift (namesBelow hcardU) (Sum.elim a conf) (Sum.elim a' conf') hwU htU
  have hdT : ∀ e ∈ T, d e = σ e := by
    intro e he
    obtain ⟨j,hj,he⟩ := mem_biUnion.mp he
    exact (hd (.inl j)).unique_on_touch (hσ j) e he
  have hdU : ∀ v ∈ U, Flows.incidence tail head d v = 0 := by
    intro v hv
    exact (hd (.inr ⟨v,hv⟩)).config_boundary
  let z : Edge n → Scalar := fun e => if e ∈ T then σ e else 0
  have hd0 : ∀ e ∈ T, (d-z) e = 0 := by intro e he; simp [z,he,hdT e he]
  have hbal := Flows.sum_incidence_component tail head T (d-z) hd0 C.out
  have hsum : (∑ v ∈ U, Flows.incidence tail head d v) = 0 := sum_eq_zero hdU
  change (∑ v ∈ U, Flows.incidence tail head (d-z) v) = 0 at hbal
  simp only [map_sub,Pi.sub_apply,sum_sub_distrib,hsum,zero_sub,neg_eq_zero] at hbal
  exact hbad hbal

end





noncomputable section

open Classical

variable {n M : ℕ} {b : Vertex n → Scalar}

theorem central_atomShift (k : CentralGroup n) (x : Atom b) :
    AtomShift k.toAdd.val x (k • x) := by
  have hk (e : Edge n) : restrictGroup e (Support.H.central flowCycle k.toAdd) =
      Support.H.central (localCycle e) (k.toAdd.val e) := by
    apply Support.H.ext <;> rfl
  cases x with
  | inl x =>
    rcases x with ⟨e,s⟩
    change AtomShift k.toAdd.val (.inl ⟨e,s⟩)
      (.inl ⟨e,restrictGroup e (Support.H.central flowCycle k.toAdd)*s⟩)
    rw [hk]
    exact .edge e s
  | inr x =>
    rcases x with ⟨v,s⟩
    have hb : boundary k.toAdd.val v = b v-b v := by
      simp only [sub_self]
      exact congrFun k.toAdd.property v
    have hs : Support.H.central flowCycle k.toAdd • s = s.shift k.toAdd.val hb := by
      apply Configuration.ext
      intro e
      change restrictGroup e.val (Support.H.central flowCycle k.toAdd)*s.state e = _
      rw [hk]
      rfl
    change AtomShift k.toAdd.val (.inr ⟨v,s⟩) (.inr ⟨v,Support.H.central flowCycle k.toAdd • s⟩)
    rw [hs]
    exact .config v s hb

theorem quantitative_homogeneity (hn : 1 ≤ n) {J : Type*} [Fintype J] [Nonempty (Atom b)]
    (names : J ↪ Fin M) (a a' : J → Atom b)
    (hw : (7*Fintype.card J:ℝ) + 7*((6*Fintype.card J:ℝ)/boxConstant)^((3:ℝ)/2)+1 ≤ M)
    (ht : Counting.tupleType (analysisStructure b) names a =
      Counting.tupleType (analysisStructure b) names a') :
    ∃ k : CentralGroup n, ∀ j, k • a j = a' j := by
  obtain ⟨k,hk,ha⟩ := quantitative_homogeneity_shift hn names a a' hw ht
  let g : CentralGroup n := Multiplicative.ofAdd ⟨k,hk⟩
  exact ⟨g,fun j => (central_atomShift g (a j)).functional (ha j)⟩

theorem empty_homogeneity {J : Type*} [IsEmpty J] (a a' : J → Atom b) :
    ∃ k : CentralGroup n, ∀ j, k • a j = a' j := ⟨1,fun j => isEmptyElim j⟩

end

end WitnessedSeparation.Grid



namespace WitnessedSeparation.Counting.Formula

noncomputable section

open Classical Finset

variable {R I A B : Type*} [DecidableEq I]

theorem eval_of_partialBijectionSystem (S : Structure R A) (T : Structure R B)
    (E : Finset I → (I → A) → (I → B) → Prop)
    (heq : ∀ names v w, E names v w → ∀ i ∈ names, ∀ j ∈ names, (v i = v j ↔ w i = w j))
    (hrel : ∀ names v w, E names v w → ∀ r i, i ∈ names → ∀ j, j ∈ names →
      (S.rel r (v i) (v j) ↔ T.rel r (w i) (w j)))
    (hext : ∀ names v w, E names v w → ∀ i, ∃ e : A ≃ B,
      ∀ a, E (insert i (names.erase i)) (Function.update v i a) (Function.update w i (e a)))
    (φ : Formula R I) : ∀ names v w, E names v w → φ.free ⊆ names →
      (φ.eval S v ↔ φ.eval T w) := by
  induction φ with
  | falsum => intros; rfl
  | equal i j =>
    intro names v w h hf
    exact heq names v w h i (hf (by simp [free])) j (hf (by simp [free]))
  | relation r i j =>
    intro names v w h hf
    exact hrel names v w h r i (hf (by simp [free])) j (hf (by simp [free]))
  | neg φ ih => intro names v w h hf; exact not_congr (ih names v w h hf)
  | and φ ψ ih ih' =>
    intro names v w h hf
    exact and_congr (ih names v w h (subset_union_left.trans hf))
      (ih' names v w h (subset_union_right.trans hf))
  | ex i φ ih =>
    intro names v w h hf
    obtain ⟨e,he⟩ := hext names v w h i
    have hfree : φ.free ⊆ insert i (names.erase i) := by
      intro j hj
      by_cases hji : j = i
      · simp [hji]
      · exact mem_insert_of_mem (mem_erase.mpr ⟨hji,hf (mem_erase.mpr ⟨hji,hj⟩)⟩)
    change (∃ a, φ.eval S (Function.update v i a)) ↔ _
    constructor
    · rintro ⟨a,ha⟩; exact ⟨e a,(ih _ _ _ (he a) hfree).mp ha⟩
    · rintro ⟨b,hb⟩; obtain ⟨a,rfl⟩ := e.surjective b
      exact ⟨a,(ih _ _ _ (he a) hfree).mpr hb⟩
  | exact i n φ ih =>
    intro names v w h hf
    obtain ⟨e,he⟩ := hext names v w h i
    have hfree : φ.free ⊆ insert i (names.erase i) := by
      intro j hj
      by_cases hji : j = i
      · simp [hji]
      · exact mem_insert_of_mem (mem_erase.mpr ⟨hji,hf (mem_erase.mpr ⟨hji,hj⟩)⟩)
    let f : {a : A // φ.eval S (Function.update v i a)} ≃
        {b : B // φ.eval T (Function.update w i b)} :=
      e.subtypeEquiv (fun a => ih _ _ _ (he a) hfree)
    exact ⟨fun ⟨g⟩ => ⟨f.symm.trans g⟩,fun ⟨g⟩ => ⟨f.trans g⟩⟩

end

end WitnessedSeparation.Counting.Formula

end OAI