File size: 36,511 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
import OAI.Computability.WitnessedChoice.Hereditary

namespace OAI

namespace WitnessedChoice.BGS

noncomputable section

open Classical WitnessedSeparation WitnessedSeparation.Hereditary

variable {A : Type}

theorem iterationLoop_total (p : ℕ) (step : HF A → Result (HF A))
    (ht : ∀ x, ∃ y, step x = some y) (fuel : ℕ) (x : HF A) :
    ∃ y, iterationLoop p step fuel x = some y := by
  induction fuel generalizing x with
  | zero => exact ⟨emptyHF, rfl⟩
  | succ fuel ih =>
    rw [iterationLoop]
    split_ifs with hb
    · obtain ⟨y, hy⟩ := ht x
      rw [hy, Option.bind_some]
      split_ifs
      · exact ⟨x, rfl⟩
      · exact ih y
    · exact ⟨emptyHF, rfl⟩

theorem ordinaryIteration_total (p : ℕ) (step : HF A → Result (HF A))
    (ht : ∀ x, ∃ y, step x = some y) : ∃ y, ordinaryIteration p step = some y :=
  iterationLoop_total p step ht _ _

theorem collect_total (r : HF A) (guard : HF A → Result Bool) (body : HF A → Result (HF A))
    (hg : ∀ x ∈ elements r, ∃ b, guard x = some b)
    (hb : ∀ x ∈ elements r, guard x = some true → ∃ y, body x = some y) :
    ∃ y, collect r guard body = some y := by
  have hg' : ∀ x ∈ elements r, (guard x).isSome := by
    intro x hx
    obtain ⟨b, hb⟩ := hg x hx
    simp [hb]
  have hb' : ∀ x ∈ elements r, guard x = some true → (body x).isSome := by
    intro x hx htrue
    obtain ⟨b, hb⟩ := hb x hx htrue
    simp [hb]
  unfold collect
  rw [ite_eq_left ⟨hg', hb'⟩]
  exact ⟨_, rfl⟩

mutual
  theorem Term.total [Fintype A] (S : Input A) (p : Polynomial ℝ) {n : ℕ}
      (t : Term n) (h : t.wscCount = 0) (env : Fin n → HF A) :
      ∃ x, t.eval S p env = some x := by
    cases t with
    | var i => exact ⟨env i, rfl⟩
    | empty => exact ⟨emptyHF, rfl⟩
    | atoms => exact ⟨_, rfl⟩
    | pair a b =>
      simp only [Term.wscCount, Nat.add_eq_zero_iff] at h
      obtain ⟨x, hx⟩ := a.total S p h.1 env
      obtain ⟨y, hy⟩ := b.total S p h.2 env
      exact ⟨double x y, by simp [Term.eval, lift₂, hx, hy]⟩
    | union a =>
      obtain ⟨x, hx⟩ := a.total S p h env
      exact ⟨unionHF x, by simp [Term.eval, hx]⟩
    | unique a =>
      obtain ⟨x, hx⟩ := a.total S p h env
      exact ⟨uniqueHF x, by simp [Term.eval, hx]⟩
    | card a =>
      obtain ⟨x, hx⟩ := a.total S p h env
      exact ⟨cardHF x, by simp [Term.eval, hx]⟩
    | comprehend body range guard =>
      simp only [Term.wscCount, Nat.add_eq_zero_iff] at h
      obtain ⟨r, hr⟩ := range.total S p h.1.2 env
      simp only [Term.eval, hr, Option.bind_some]
      apply collect_total
      · exact fun x _ => guard.total S p h.2 (Fin.cons x env)
      · exact fun x _ _ => body.total S p h.1.1 (Fin.cons x env)
    | iterate step =>
      exact ordinaryIteration_total _ _ (fun x => step.total S p h (Fin.cons x env))
  theorem Formula.total [Fintype A] (S : Input A) (p : Polynomial ℝ) {n : ℕ}
      (f : Formula n) (h : f.wscCount = 0) (env : Fin n → HF A) :
      ∃ b, f.eval S p env = some b := by
    cases f with
    | equal a b =>
      simp only [Formula.wscCount, Nat.add_eq_zero_iff] at h
      obtain ⟨x, hx⟩ := a.total S p h.1 env
      obtain ⟨y, hy⟩ := b.total S p h.2 env
      exact ⟨decide (x = y), by simp [Formula.eval, lift₂, hx, hy]⟩
    | input r a b =>
      simp only [Formula.wscCount, Nat.add_eq_zero_iff] at h
      obtain ⟨x, hx⟩ := a.total S p h.1 env
      obtain ⟨y, hy⟩ := b.total S p h.2 env
      exact ⟨inputHF S r x y, by simp [Formula.eval, lift₂, hx, hy]⟩
    | neg a =>
      obtain ⟨x, hx⟩ := a.total S p h env
      exact ⟨!x, by simp [Formula.eval, hx]⟩
    | and a b =>
      simp only [Formula.wscCount, Nat.add_eq_zero_iff] at h
      obtain ⟨x, hx⟩ := a.total S p h.1 env
      obtain ⟨y, hy⟩ := b.total S p h.2 env
      exact ⟨x && y, by simp [Formula.eval, lift₂, hx, hy]⟩
    | or a b =>
      simp only [Formula.wscCount, Nat.add_eq_zero_iff] at h
      obtain ⟨x, hx⟩ := a.total S p h.1 env
      obtain ⟨y, hy⟩ := b.total S p h.2 env
      exact ⟨x || y, by simp [Formula.eval, lift₂, hx, hy]⟩
    | wsc step choice witness output =>
      simp only [Formula.wscCount] at h
      omega
end

theorem Sentence.cpt_boolean (φ : Sentence) (h : φ.isCPT) : φ.BooleanOnAllInputs := by
  intro S
  exact φ.formula.total S.input φ.polynomial h Fin.elim0

end

end WitnessedChoice.BGS

namespace WitnessedSeparation

namespace Support

abbrev Scalar := ZMod 3

abbrev Plane := Scalar × Scalar

def omega (u v : Plane) : Scalar := u.1 * v.2 - u.2 * v.1

@[simp] theorem omega_self (u : Plane) : omega u u = 0 := by simp [omega, mul_comm]

@[simp] theorem omega_zero_left (u : Plane) : omega 0 u = 0 := by simp [omega]

@[simp] theorem omega_zero_right (u : Plane) : omega u 0 = 0 := by simp [omega]

@[simp] theorem omega_add_left (u v w : Plane) :
    omega (u + v) w = omega u w + omega v w := by simp [omega]; ring

@[simp] theorem omega_add_right (u v w : Plane) :
    omega u (v + w) = omega u v + omega u w := by simp [omega]; ring

@[simp] theorem omega_neg_left (u v : Plane) : omega (-u) v = -omega u v := by
  simp [omega]; ring

@[simp] theorem omega_neg_right (u v : Plane) : omega u (-v) = -omega u v := by
  simp [omega]; ring

theorem omega_swap (u v : Plane) : omega v u = -omega u v := by
  simp [omega]; ring

@[simp] theorem omega_smul_left (a : Scalar) (u v : Plane) :
    omega (a • u) v = a * omega u v := by simp [omega]; ring

@[simp] theorem omega_smul_right (a : Scalar) (u v : Plane) :
    omega u (a • v) = a * omega u v := by simp [omega]; ring

variable {F K : Type} [Fintype F] [AddCommGroup K] [Module Scalar K]

def correction (cycle : F → K) (a b : F → Plane) : K :=
  (2 : Scalar) • ∑ f, omega (a f) (b f) • cycle f

@[simp] theorem correction_self (cycle : F → K) (a : F → Plane) :
    correction cycle a a = 0 := by simp [correction]

@[simp] theorem correction_zero_left (cycle : F → K) (a : F → Plane) :
    correction cycle 0 a = 0 := by simp [correction]

@[simp] theorem correction_zero_right (cycle : F → K) (a : F → Plane) :
    correction cycle a 0 = 0 := by simp [correction]

@[simp] theorem correction_add_left (cycle : F → K) (a b c : F → Plane) :
    correction cycle (a + b) c = correction cycle a c + correction cycle b c := by
  simp [correction, add_smul, Finset.sum_add_distrib, smul_add]

@[simp] theorem correction_add_right (cycle : F → K) (a b c : F → Plane) :
    correction cycle a (b + c) = correction cycle a b + correction cycle a c := by
  simp [correction, add_smul, Finset.sum_add_distrib, smul_add]

@[simp] theorem correction_neg_left (cycle : F → K) (a b : F → Plane) :
    correction cycle (-a) b = -correction cycle a b := by
  simp [correction, Finset.sum_neg_distrib]

@[simp] theorem correction_neg_right (cycle : F → K) (a b : F → Plane) :
    correction cycle a (-b) = -correction cycle a b := by
  simp [correction, Finset.sum_neg_distrib]

theorem correction_swap (cycle : F → K) (a b : F → Plane) :
    correction cycle b a = -correction cycle a b := by
  unfold correction
  rw [← smul_neg, ← Finset.sum_neg_distrib]
  congr 1
  apply Finset.sum_congr rfl
  intro f _
  rw [omega_swap, neg_smul]

@[ext]
structure H (cycle : F → K) where
  face : F → Plane
  flow : K

namespace H

variable (cycle : F → K)

instance : One (H cycle) := ⟨⟨0, 0⟩⟩

instance : Mul (H cycle) := ⟨fun x y =>
  ⟨x.face + y.face, x.flow + y.flow + correction cycle x.face y.face⟩⟩

instance : Inv (H cycle) := ⟨fun x => ⟨-x.face, -x.flow⟩⟩

omit [Fintype F] [Module Scalar K] in
@[simp] theorem one_face : (1 : H cycle).face = 0 := rfl

omit [Fintype F] [Module Scalar K] in
@[simp] theorem one_flow : (1 : H cycle).flow = 0 := rfl

@[simp] theorem mul_face (x y : H cycle) : (x * y).face = x.face + y.face := rfl

@[simp] theorem mul_flow (x y : H cycle) :
    (x * y).flow = x.flow + y.flow + correction cycle x.face y.face := rfl

omit [Fintype F] [Module Scalar K] in
@[simp] theorem inv_face (x : H cycle) : x⁻¹.face = -x.face := rfl

omit [Fintype F] [Module Scalar K] in
@[simp] theorem inv_flow (x : H cycle) : x⁻¹.flow = -x.flow := rfl

instance : Group (H cycle) := Group.ofLeftAxioms
  (by intro x y z; ext <;> simp [add_assoc] ; abel)
  (by intro x; ext <;> simp)
  (by intro x; ext <;> simp)

def central (k : K) : H cycle := ⟨0,k⟩

@[simp] theorem central_mul (k l : K) : central cycle k * central cycle l = central cycle (k+l) := by
  ext <;> simp [central]

omit [Fintype F] [Module Scalar K] in
@[simp] theorem central_zero : central cycle (0 : K) = 1 := rfl

omit [Fintype F] [Module Scalar K] in
@[simp] theorem central_neg (k : K) : central cycle (-k) = (central cycle k)⁻¹ := by
  apply H.ext <;> simp [central]

theorem central_commute (x : H cycle) (k : K) :
    x * central cycle k = central cycle k * x := by
  ext <;> simp [central, add_comm]

theorem commutator (x y : H cycle) :
    x * y * x⁻¹ * y⁻¹ = central cycle (∑ f, omega (x.face f) (y.face f) • cycle f) := by
  apply H.ext
  · simp only [mul_face, inv_face, central]
    abel
  · simp only [mul_flow, mul_face, inv_flow, inv_face,
      correction_add_left, correction_neg_left,
      correction_neg_right, correction_self,
      zero_add, add_zero, central]
    have hc : correction cycle x.face y.face + correction cycle x.face y.face =
        ∑ f, omega (x.face f) (y.face f) • cycle f := by
      rw [correction, ← add_smul]
      have h : (2 : Scalar) + 2 = 1 := by decide
      rw [h, one_smul]
    have hs := correction_swap cycle x.face y.face
    rw [hs]
    abel_nf at hc ⊢
    exact hc

def projectedAddSubgroup (S : Subgroup (H cycle)) : AddSubgroup (F → Plane) where
  carrier := {a | ∃ h ∈ S, h.face = a}
  zero_mem' := ⟨1, S.one_mem, rfl⟩
  add_mem' := by
    rintro a b ⟨x, hx, rfl⟩ ⟨y, hy, rfl⟩
    exact ⟨x*y, S.mul_mem hx hy, rfl⟩
  neg_mem' := by
    rintro a ⟨x, hx, rfl⟩
    exact ⟨x⁻¹, S.inv_mem hx, rfl⟩

def centralAddSubgroup (S : Subgroup (H cycle)) : AddSubgroup K where
  carrier := {k | central cycle k ∈ S}
  zero_mem' := S.one_mem
  add_mem' := by
    intro k l hk hl
    change central cycle (k+l) ∈ S
    simpa only [← central_mul] using S.mul_mem hk hl
  neg_mem' := by
    intro k hk
    change central cycle (-k) ∈ S
    simpa only [central_neg] using S.inv_mem hk

def projected (S : Subgroup (H cycle)) : Submodule Scalar (F → Plane) :=
  AddSubgroup.toZModSubmodule 3 (projectedAddSubgroup cycle S)

def centralPart (S : Subgroup (H cycle)) : Submodule Scalar K :=
  AddSubgroup.toZModSubmodule 3 (centralAddSubgroup cycle S)

@[simp] theorem mem_projected (S : Subgroup (H cycle)) (a : F → Plane) :
    a ∈ projected cycle S ↔ ∃ h ∈ S, h.face = a := Iff.rfl

@[simp] theorem mem_centralPart (S : Subgroup (H cycle)) (k : K) :
    k ∈ centralPart cycle S ↔ central cycle k ∈ S := Iff.rfl

theorem commutator_mem_centralPart (S : Subgroup (H cycle))
    {a b : F → Plane} (ha : a ∈ projected cycle S) (hb : b ∈ projected cycle S) :
    (∑ f, omega (a f) (b f) • cycle f) ∈ centralPart cycle S := by
  obtain ⟨x, hx, rfl⟩ := ha
  obtain ⟨y, hy, rfl⟩ := hb
  rw [mem_centralPart, ← commutator]
  exact S.mul_mem (S.mul_mem (S.mul_mem hx hy) (S.inv_mem hx)) (S.inv_mem hy)

theorem stabilizer_isotropic (S : Subgroup (H cycle)) (lambda : K →ₗ[Scalar] Scalar)
    (hlambda : ∀ k ∈ centralPart cycle S, lambda k = 0)
    {a b : F → Plane} (ha : a ∈ projected cycle S) (hb : b ∈ projected cycle S) :
    (∑ f, lambda (cycle f) * omega (a f) (b f)) = 0 := by
  have h := hlambda _ (commutator_mem_centralPart cycle S ha hb)
  simpa only [map_sum, map_smul, smul_eq_mul, mul_comm] using h

def productEquiv : H cycle ≃ (F → Plane) × K where
  toFun x := (x.face,x.flow)
  invFun x := ⟨x.1,x.2⟩
  left_inv _ := rfl
  right_inv _ := rfl

instance [Finite K] : Finite (H cycle) :=
  Finite.of_equiv ((F → Plane) × K) (productEquiv cycle).symm

noncomputable def projectedLift (S : Subgroup (H cycle)) (a : projected cycle S) : H cycle :=
  Classical.choose a.property

theorem projectedLift_mem (S : Subgroup (H cycle)) (a : projected cycle S) :
    projectedLift cycle S a ∈ S := (Classical.choose_spec a.property).1

@[simp] theorem projectedLift_face (S : Subgroup (H cycle)) (a : projected cycle S) :
    (projectedLift cycle S a).face = a.val := (Classical.choose_spec a.property).2

theorem mul_inv_of_face_eq (x y : H cycle) (h : x.face = y.face) :
    x * y⁻¹ = central cycle (x.flow - y.flow) := by
  apply H.ext
  · simp [h, central]
  · simp [central, h, sub_eq_add_neg]

noncomputable def subgroupEquiv (S : Subgroup (H cycle)) :
    S ≃ projected cycle S × centralPart cycle S where
  toFun x := by
    let a : projected cycle S := ⟨x.val.face, x.val, x.property, rfl⟩
    refine (a, ⟨x.val.flow - (projectedLift cycle S a).flow, ?_⟩)
    rw [mem_centralPart, ← mul_inv_of_face_eq cycle x.val (projectedLift cycle S a)]
    · exact S.mul_mem x.property (S.inv_mem (projectedLift_mem cycle S a))
    · exact (projectedLift_face cycle S a).symm
  invFun p := ⟨central cycle p.2.val * projectedLift cycle S p.1,
    S.mul_mem ((mem_centralPart cycle S p.2.val).mp p.2.property) (projectedLift_mem cycle S p.1)⟩
  left_inv := by
    intro x
    apply Subtype.ext
    apply H.ext
    · simp [central]
    · simp [central]
  right_inv := by
    rintro ⟨a,k⟩
    apply Prod.ext
    · apply Subtype.ext
      simp [central]
    · apply Subtype.ext
      let a' : projected cycle S := ⟨(central cycle k.val * projectedLift cycle S a).face,
        central cycle k.val * projectedLift cycle S a,
        S.mul_mem ((mem_centralPart cycle S k.val).mp k.property) (projectedLift_mem cycle S a), rfl⟩
      have h : a' = a := by
        apply Subtype.ext
        simp [a', central]
      change (central cycle k.val * projectedLift cycle S a).flow -
        (projectedLift cycle S a').flow = k.val
      rw [h]
      simp [central]

theorem subgroup_card [FiniteDimensional Scalar K] (S : Subgroup (H cycle)) :
    Nat.card S = 3 ^ (Module.finrank Scalar (projected cycle S) +
      Module.finrank Scalar (centralPart cycle S)) := by
  rw [Nat.card_congr (subgroupEquiv cycle S), Nat.card_prod,
    Module.natCard_eq_pow_finrank (K := Scalar) (V := projected cycle S),
    Module.natCard_eq_pow_finrank (K := Scalar) (V := centralPart cycle S)]
  have h : Nat.card Scalar = 3 := by simp [Scalar, Nat.card_eq_fintype_card]
  rw [h, ← pow_add]

theorem ambient_card [FiniteDimensional Scalar K] :
    Nat.card (H cycle) = 3 ^ (Module.finrank Scalar (F → Plane) + Module.finrank Scalar K) := by
  rw [Nat.card_congr (productEquiv cycle), Nat.card_prod,
    Module.natCard_eq_pow_finrank (K := Scalar) (V := F → Plane),
    Module.natCard_eq_pow_finrank (K := Scalar) (V := K)]
  have h : Nat.card Scalar = 3 := by simp [Scalar, Nat.card_eq_fintype_card]
  rw [h, ← pow_add]

theorem subgroup_index [FiniteDimensional Scalar K] (S : Subgroup (H cycle)) :
    S.index = 3 ^ ((Module.finrank Scalar (F → Plane) - Module.finrank Scalar (projected cycle S)) +
      (Module.finrank Scalar K - Module.finrank Scalar (centralPart cycle S))) := by
  have hu := Submodule.finrank_le (projected cycle S)
  have hk := Submodule.finrank_le (centralPart cycle S)
  have hc := S.index_mul_card
  rw [subgroup_card cycle S, ambient_card cycle] at hc
  apply Nat.mul_right_cancel (m := 3 ^ (Module.finrank Scalar (projected cycle S) +
    Module.finrank Scalar (centralPart cycle S))) (by positivity)
  rw [hc, ← pow_add]
  congr 1
  omega

end H

def diagonalForm (weight : F → Scalar) : LinearMap.BilinForm Scalar (F → Plane) where
  toFun a := {
    toFun := fun b => ∑ f, weight f * omega (a f) (b f)
    map_add' := by intro b c; simp [mul_add, Finset.sum_add_distrib]
    map_smul' := by
      intro r b
      simp only [Pi.smul_apply, omega_smul_right, smul_eq_mul, RingHom.id_apply]
      rw [Finset.mul_sum]
      apply Finset.sum_congr rfl
      intro f _
      ring }
  map_add' := by
    intro a b
    ext c
    simp [mul_add, Finset.sum_add_distrib]
  map_smul' := by
    intro r a
    ext b
    simp only [Pi.smul_apply, omega_smul_left, LinearMap.smul_apply,
      smul_eq_mul, RingHom.id_apply]
    change (∑ f, weight f * (r * omega (a f) (b f))) =
      r * (∑ f, weight f * omega (a f) (b f))
    rw [Finset.mul_sum]
    apply Finset.sum_congr rfl
    intro f _
    ring

@[simp] theorem diagonalForm_apply (weight : F → Scalar) (a b : F → Plane) :
    diagonalForm weight a b = ∑ f, weight f * omega (a f) (b f) := rfl

theorem mem_diagonalForm_ker (weight : F → Scalar) (a : F → Plane) :
    a ∈ (diagonalForm weight).ker ↔ ∀ f, weight f ≠ 0 → a f = 0 := by
  classical
  constructor
  · intro ha f hf
    have hzero : ∀ b, diagonalForm weight a b = 0 := by
      intro b
      exact LinearMap.congr_fun (LinearMap.mem_ker.mp ha) b
    have heval (b : Plane) : diagonalForm weight a (Pi.single f b) =
        weight f * omega (a f) b := by
      rw [diagonalForm_apply, Finset.sum_eq_single f]
      · simp
      · intro g _ hg
        simp [hg]
      · simp
    have hfst := hzero (Pi.single f (0,1))
    have hsnd := hzero (Pi.single f (1,0))
    simp only [heval, omega, mul_one, mul_zero, sub_zero, zero_sub, mul_neg, neg_eq_zero] at hfst hsnd
    apply Prod.ext
    · exact (mul_eq_zero.mp hfst).resolve_left hf
    · exact (mul_eq_zero.mp hsnd).resolve_left hf
  · intro ha
    apply LinearMap.mem_ker.mpr
    apply LinearMap.ext
    intro b
    change (∑ f, weight f * omega (a f) (b f)) = 0
    apply Finset.sum_eq_zero
    intro f _
    by_cases hf : weight f = 0
    · simp [hf]
    · simp [ha f hf]

noncomputable def diagonalKerEquiv (weight : F → Scalar) :
    (diagonalForm weight).ker ≃ₗ[Scalar] ({f : F // weight f = 0} → Plane) where
  toFun a f := a.val f.val
  invFun b := by
    classical
    refine ⟨fun f => if hf : weight f = 0 then b ⟨f,hf⟩ else 0, ?_⟩
    rw [mem_diagonalForm_ker]
    intro f hf
    simp [hf]
  left_inv := by
    intro a
    apply Subtype.ext
    funext f
    dsimp
    split_ifs with hf
    · rfl
    · exact ((mem_diagonalForm_ker weight a.val).mp a.property f hf).symm
  right_inv := by intro b; funext f; simp [f.property]
  map_add' := by intro a b; rfl
  map_smul' := by intro r a; rfl

theorem diagonalForm_finrank_ker (weight : F → Scalar) :
    Module.finrank Scalar (diagonalForm weight).ker =
      2 * Fintype.card {f : F // weight f = 0} := by
  classical
  rw [(diagonalKerEquiv weight).finrank_eq, Module.finrank_pi_fintype]
  simp [Plane, Module.finrank_prod, Nat.mul_comm]

theorem nonzero_blocks_le_codim (weight : F → Scalar)
    (U : Submodule Scalar (F → Plane))
    (hisotropic : ∀ a ∈ U, ∀ b ∈ U, diagonalForm weight a b = 0) :
    Fintype.card {f : F // weight f ≠ 0} ≤
      Module.finrank Scalar (F → Plane) - Module.finrank Scalar U := by
  classical
  have hle : U ≤ (diagonalForm weight).orthogonal U := by
    intro a ha
    exact fun b hb => hisotropic b hb a ha
  have hd := Submodule.finrank_mono hle
  have heq := LinearMap.BilinForm.finrank_add_finrank_orthogonal'
    (B := diagonalForm weight) U
  have hker := Submodule.finrank_mono
    (show U ⊓ (diagonalForm weight).ker ≤ (diagonalForm weight).ker from inf_le_right)
  have hcard : Fintype.card {f : F // weight f ≠ 0} =
      Fintype.card F - Fintype.card {f : F // weight f = 0} :=
    Fintype.card_subtype_compl (fun f : F => weight f = 0)
  have hcardle := Fintype.card_subtype_le (fun f : F => weight f = 0)
  have hdimu : Module.finrank Scalar U ≤ Module.finrank Scalar (F → Plane) :=
    Submodule.finrank_le U
  have hdim : Module.finrank Scalar (F → Plane) = 2 * Fintype.card F := by
    rw [Module.finrank_pi_fintype]
    simp [Plane, Module.finrank_prod, Nat.mul_comm]
  rw [diagonalForm_finrank_ker] at hker
  rw [hdim] at heq hdimu ⊢
  omega

end Support

end WitnessedSeparation

namespace WitnessedSeparation.Grid

abbrev Scalar := ZMod 3

abbrev Vertex (n : ℕ) := Fin (n+1) × Fin (n+1) × Fin (n+1)

abbrev EdgeX (n : ℕ) := Fin n × Fin (n+1) × Fin (n+1)

abbrev EdgeY (n : ℕ) := Fin (n+1) × Fin n × Fin (n+1)

abbrev EdgeZ (n : ℕ) := Fin (n+1) × Fin (n+1) × Fin n

abbrev Edge (n : ℕ) := EdgeX n ⊕ EdgeY n ⊕ EdgeZ n

abbrev FaceXY (n : ℕ) := Fin n × Fin n × Fin (n+1)

abbrev FaceXZ (n : ℕ) := Fin n × Fin (n+1) × Fin n

abbrev FaceYZ (n : ℕ) := Fin (n+1) × Fin n × Fin n

abbrev Face (n : ℕ) := FaceXY n ⊕ FaceXZ n ⊕ FaceYZ n

variable {n : ℕ}

def ex (i : Fin n) (j k : Fin (n+1)) : Edge n := .inl (i,j,k)

def ey (i : Fin (n+1)) (j : Fin n) (k : Fin (n+1)) : Edge n := .inr (.inl (i,j,k))

def ez (i j : Fin (n+1)) (k : Fin n) : Edge n := .inr (.inr (i,j,k))

def tail : Edge n → Vertex n
  | .inl (i,j,k) => (i.castSucc,j,k)
  | .inr (.inl (i,j,k)) => (i,j.castSucc,k)
  | .inr (.inr (i,j,k)) => (i,j,k.castSucc)

def head : Edge n → Vertex n
  | .inl (i,j,k) => (i.succ,j,k)
  | .inr (.inl (i,j,k)) => (i,j.succ,k)
  | .inr (.inr (i,j,k)) => (i,j,k.succ)

noncomputable def boundary : (Edge n → Scalar) →ₗ[Scalar] (Vertex n → Scalar) where
  toFun z := ∑ e, z e • (Pi.single (head e) (1 : Scalar) - 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]

@[simp] theorem boundary_single (e : Edge n) (z : Scalar) :
    boundary (Pi.single e z) = Pi.single (head e) z - Pi.single (tail e) z := by
  classical
  change (∑ x, (Pi.single e z : Edge n → 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 cycle : Face n → Edge n → Scalar
  | .inl (i,j,k) => Pi.single (ex i j.castSucc k) 1 + Pi.single (ey i.succ j k) 1 -
      Pi.single (ex i j.succ k) 1 - Pi.single (ey i.castSucc j k) 1
  | .inr (.inl (i,j,k)) => Pi.single (ex i j k.castSucc) 1 + Pi.single (ez i.succ j k) 1 -
      Pi.single (ex i j k.succ) 1 - Pi.single (ez i.castSucc j k) 1
  | .inr (.inr (i,j,k)) => Pi.single (ey i j k.castSucc) 1 + Pi.single (ez i j.succ k) 1 -
      Pi.single (ey i j k.succ) 1 - Pi.single (ez i j.castSucc k) 1

@[simp] theorem boundary_cycle (f : Face n) : boundary (cycle f) = 0 := by
  rcases f with ⟨i,j,k⟩ | (⟨i,j,k⟩ | ⟨i,j,k⟩)
  all_goals
    simp only [cycle, map_sub, map_add, boundary_single, head, tail, ex, ey, ez]
    abel

def gradient (φ : Vertex n → Scalar) (e : Edge n) : Scalar := φ (head e) - φ (tail e)

def curl (l : Edge n → Scalar) (f : Face n) : Scalar := ∑ e, cycle f e * l e

theorem single_mul_sum {ι : Type*} [Fintype ι] [DecidableEq ι]
    (i : ι) (z : Scalar) (l : ι → Scalar) :
    (∑ j, (Pi.single i z : ι → Scalar) j * l j) = z * l i := by
  classical
  simp only [Pi.single_apply, ite_mul, zero_mul]
  simp

@[simp] theorem curl_xy (l : Edge n → Scalar) (i j : Fin n) (k : Fin (n+1)) :
    curl l (.inl (i,j,k)) = l (ex i j.castSucc k) + l (ey i.succ j k) -
      l (ex i j.succ k) - l (ey i.castSucc j k) := by
  classical
  simp only [curl, cycle, Pi.sub_apply, Pi.add_apply, sub_mul, add_mul,
    Finset.sum_sub_distrib, Finset.sum_add_distrib, single_mul_sum, one_mul]

@[simp] theorem curl_xz (l : Edge n → Scalar) (i k : Fin n) (j : Fin (n+1)) :
    curl l (.inr (.inl (i,j,k))) = l (ex i j k.castSucc) + l (ez i.succ j k) -
      l (ex i j k.succ) - l (ez i.castSucc j k) := by
  classical
  simp only [curl, cycle, Pi.sub_apply, Pi.add_apply, sub_mul, add_mul,
    Finset.sum_sub_distrib, Finset.sum_add_distrib, single_mul_sum, one_mul]

@[simp] theorem curl_yz (l : Edge n → Scalar) (j k : Fin n) (i : Fin (n+1)) :
    curl l (.inr (.inr (i,j,k))) = l (ey i j k.castSucc) + l (ez i j.succ k) -
      l (ey i j k.succ) - l (ez i j.castSucc k) := by
  classical
  simp only [curl, cycle, Pi.sub_apply, Pi.add_apply, sub_mul, add_mul,
    Finset.sum_sub_distrib, Finset.sum_add_distrib, single_mul_sum, one_mul]

@[simp] theorem curl_gradient (φ : Vertex n → Scalar) (f : Face n) :
    curl (gradient φ) f = 0 := by
  rcases f with ⟨i,j,k⟩ | (⟨i,j,k⟩ | ⟨i,j,k⟩)
  all_goals simp [gradient, head, tail, ex, ey, ez]

@[simp] theorem curl_sub (l r : Edge n → Scalar) (f : Face n) :
    curl (l-r) f = curl l f - curl r f := by
  simp [curl, mul_sub, Finset.sum_sub_distrib]

def prefixSum (l : Fin n → Scalar) (i : Fin (n+1)) : Scalar :=
  ∑ a : Fin n, if a.val < i.val then l a else 0

@[simp] theorem prefix_zero (l : Fin n → Scalar) : prefixSum l 0 = 0 := by simp [prefixSum]

theorem prefix_step (l : Fin n → Scalar) (i : Fin n) :
    prefixSum l i.succ - prefixSum l i.castSucc = l i := by
  classical
  have h : prefixSum l i.succ = prefixSum l i.castSucc + l i := by
    unfold prefixSum
    have hs : (∑ a : Fin n, if a = i then l a else 0) = l i := by simp
    rw [← hs, ← Finset.sum_add_distrib]
    apply Finset.sum_congr rfl
    intro a _
    simp only [Fin.val_succ, Fin.val_castSucc]
    by_cases hai : a.val < i.val
    · have hne : a ≠ i := by intro h; subst a; omega
      simp [hai, hne, show a.val < i.val+1 by omega]
    · by_cases heq : a = i
      · subst a; simp
      · have hge : i.val + 1 ≤ a.val := by
          have : a.val ≠ i.val := by intro h; exact heq (Fin.ext h)
          omega
        simp [hai, heq, show ¬a.val < i.val+1 by omega]
  rw [h]
  abel

def phi1 (l : Edge n → Scalar) (v : Vertex n) : Scalar :=
  prefixSum (fun a => l (ex a v.2.1 v.2.2)) v.1

def residual1 (l : Edge n → Scalar) : Edge n → Scalar := l - gradient (phi1 l)

def phi2 (l : Edge n → Scalar) (i0 : Fin (n+1)) (v : Vertex n) : Scalar :=
  prefixSum (fun b => residual1 l (ey i0 b v.2.2)) v.2.1

def residual2 (l : Edge n → Scalar) (i0 : Fin (n+1)) : Edge n → Scalar :=
  residual1 l - gradient (phi2 l i0)

def phi3 (l : Edge n → Scalar) (i0 : Fin (n+1)) (v : Vertex n) : Scalar :=
  prefixSum (fun c => residual2 l i0 (ez i0 0 c)) v.2.2

def residual (l : Edge n → Scalar) (i0 : Fin (n+1)) : Edge n → Scalar :=
  residual2 l i0 - gradient (phi3 l i0)

@[simp] theorem residual1_x (l : Edge n → Scalar) (i : Fin n) (j k : Fin (n+1)) :
    residual1 l (ex i j k) = 0 := by
  simp only [residual1, Pi.sub_apply, gradient, head, tail, ex, phi1, prefix_step, sub_self]

@[simp] theorem residual2_x (l : Edge n → Scalar) (i0 : Fin (n+1))
    (i : Fin n) (j k : Fin (n+1)) : residual2 l i0 (ex i j k) = 0 := by
  rw [residual2, Pi.sub_apply, residual1_x]
  simp only [gradient, phi2, head, tail, ex, sub_self]

@[simp] theorem residual2_y (l : Edge n → Scalar) (i0 : Fin (n+1))
    (j : Fin n) (k : Fin (n+1)) : residual2 l i0 (ey i0 j k) = 0 := by
  simp only [residual2, Pi.sub_apply, gradient, phi2, head, tail, ey,
    prefix_step, sub_self]

@[simp] theorem residual_x (l : Edge n → Scalar) (i0 : Fin (n+1))
    (i : Fin n) (j k : Fin (n+1)) : residual l i0 (ex i j k) = 0 := by
  rw [residual, Pi.sub_apply, residual2_x]
  simp only [gradient, phi3, head, tail, ex, sub_self]

@[simp] theorem residual_y (l : Edge n → Scalar) (i0 : Fin (n+1))
    (j : Fin n) (k : Fin (n+1)) : residual l i0 (ey i0 j k) = 0 := by
  rw [residual, Pi.sub_apply, residual2_y]
  simp only [gradient, phi3, head, tail, ey, sub_self]

@[simp] theorem residual_z (l : Edge n → Scalar) (i0 : Fin (n+1))
    (k : Fin n) : residual l i0 (ez i0 0 k) = 0 := by
  simp only [residual, Pi.sub_apply, gradient, phi3, head, tail, ez,
    prefix_step, sub_self]

@[simp] theorem curl_residual (l : Edge n → Scalar) (i0 : Fin (n+1)) (f : Face n) :
    curl (residual l i0) f = curl l f := by
  simp [residual, residual2, residual1]

theorem residual_as_gradient (l : Edge n → Scalar) (i0 : Fin (n+1)) :
    residual l i0 = l - gradient (phi1 l + phi2 l i0 + phi3 l i0) := by
  funext e
  simp only [residual, residual2, residual1, Pi.sub_apply, gradient, Pi.add_apply]
  ring

theorem line_constant {g : Fin (n+1) → Scalar}
    (h : ∀ a : Fin n, g a.succ = g a.castSucc) (i j : Fin (n+1)) : g i = g j := by
  have hz : ∀ a, g a = g 0 := by
    intro a
    induction a using Fin.induction with
    | zero => rfl
    | succ a ha => exact (h a).trans ha
  exact (hz i).trans (hz j).symm

theorem residual_y_witness (l : Edge n → Scalar) (i0 i : Fin (n+1))
    (j : Fin n) (k : Fin (n+1)) (h : residual l i0 (ey i j k) ≠ 0) :
    ∃ a : Fin n, curl l (.inl (a,j,k)) ≠ 0 := by
  by_contra hn
  push Not at hn
  have hd : ∀ a : Fin n, residual l i0 (ey a.succ j k) =
      residual l i0 (ey a.castSucc j k) := by
    intro a
    have hc := curl_residual l i0 (.inl (a,j,k))
    rw [curl_xy, residual_x, residual_x, hn a] at hc
    simpa using sub_eq_zero.mp hc
  have hc := line_constant (g := fun a => residual l i0 (ey a j k)) hd i i0
  exact h (hc.trans (residual_y l i0 j k))

theorem residual_z_witness (l : Edge n → Scalar) (i0 i j : Fin (n+1))
    (k : Fin n) (h : residual l i0 (ez i j k) ≠ 0) :
    (∃ a : Fin n, curl l (.inr (.inl (a,j,k))) ≠ 0) ∨
    (∃ b : Fin n, curl l (.inr (.inr (i0,b,k))) ≠ 0) := by
  by_contra hn
  push Not at hn
  have hx : ∀ a : Fin n, residual l i0 (ez a.succ j k) =
      residual l i0 (ez a.castSucc j k) := by
    intro a
    have hc := curl_residual l i0 (.inr (.inl (a,j,k)))
    rw [curl_xz, residual_x, residual_x, hn.1 a] at hc
    simpa using sub_eq_zero.mp hc
  have hy : ∀ b : Fin n, residual l i0 (ez i0 b.succ k) =
      residual l i0 (ez i0 b.castSucc k) := by
    intro b
    have hc := curl_residual l i0 (.inr (.inr (i0,b,k)))
    rw [curl_yz, residual_y, residual_y, hn.2 b] at hc
    simpa using sub_eq_zero.mp hc
  exact h ((line_constant (g := fun a => residual l i0 (ez a j k)) hx i i0).trans ((line_constant (g := fun b => residual l i0 (ez i0 b k)) hy j 0).trans
    (residual_z l i0 k)))

def nonzeroSet {ι : Type*} [Fintype ι] (f : ι → Scalar) : Finset ι :=
  Finset.univ.filter (fun i => f i ≠ 0)

@[simp] theorem mem_nonzeroSet {ι : Type*} [Fintype ι] (f : ι → Scalar) (i : ι) :
    i ∈ nonzeroSet f ↔ f i ≠ 0 := by simp [nonzeroSet]

theorem card_le_product_of_witness {α β γ : Type*} [DecidableEq α] [Fintype β]
    (s : Finset α) (t : Finset γ) (f : β → γ → α)
    (h : ∀ x ∈ s, ∃ b c, c ∈ t ∧ f b c = x) :
    s.card ≤ Fintype.card β * t.card := by
  classical
  have hs : s ⊆ (Finset.univ ×ˢ t).image (fun p => f p.1 p.2) := by
    intro x hx
    obtain ⟨b,c,hc,hbc⟩ := h x hx
    exact Finset.mem_image.mpr ⟨(b,c), by simp [hc], hbc⟩
  calc
    s.card ≤ ((Finset.univ ×ˢ t).image (fun p => f p.1 p.2)).card := Finset.card_le_card hs
    _ ≤ (Finset.univ ×ˢ t).card := Finset.card_image_le
    _ = Fintype.card β * t.card := by simp

theorem card_le_two_products_of_witness {α β γ δ ε : Type*} [DecidableEq α]
    [Fintype β] [Fintype δ] (s : Finset α) (t : Finset γ) (u : Finset ε)
    (f : β → γ → α) (g : δ → ε → α)
    (h : ∀ x ∈ s, (∃ b c, c ∈ t ∧ f b c = x) ∨ (∃ d e, e ∈ u ∧ g d e = x)) :
    s.card ≤ Fintype.card β * t.card + Fintype.card δ * u.card := by
  classical
  let sf := (Finset.univ ×ˢ t).image (fun p => f p.1 p.2)
  let sg := (Finset.univ ×ˢ u).image (fun p => g p.1 p.2)
  have hs : s ⊆ sf ∪ sg := by
    intro x hx
    rcases h x hx with ⟨b,c,hc,hbc⟩ | ⟨d,e,he,hde⟩
    · exact Finset.mem_union_left _ (Finset.mem_image.mpr ⟨(b,c), by simp [hc], hbc⟩)
    · exact Finset.mem_union_right _ (Finset.mem_image.mpr ⟨(d,e), by simp [he], hde⟩)
  calc
    s.card ≤ (sf ∪ sg).card := Finset.card_le_card hs
    _ ≤ sf.card + sg.card := Finset.card_union_le _ _
    _ ≤ (Finset.univ ×ˢ t).card + (Finset.univ ×ˢ u).card :=
      Nat.add_le_add Finset.card_image_le Finset.card_image_le
    _ = _ := by simp

def badXY (l : Edge n → Scalar) : Finset (FaceXY n) :=
  nonzeroSet (fun f => curl l (.inl f))

def badXZ (l : Edge n → Scalar) : Finset (FaceXZ n) :=
  nonzeroSet (fun f => curl l (.inr (.inl f)))

def badYZ (l : Edge n → Scalar) : Finset (FaceYZ n) :=
  nonzeroSet (fun f => curl l (.inr (.inr f)))

def badSlice (l : Edge n → Scalar) (i : Fin (n+1)) : Finset (Fin n × Fin n) :=
  nonzeroSet (fun f => curl l (.inr (.inr (i,f))))

theorem sum_badSlice (l : Edge n → Scalar) :
    (∑ i : Fin (n+1), (badSlice l i).card) = (badYZ l).card := by
  simp only [badSlice, badYZ, nonzeroSet, Finset.card_filter, Fintype.sum_prod_type]

theorem exists_sparse_slice (l : Edge n → Scalar) :
    ∃ i0 : Fin (n+1), (n+1) * (badSlice l i0).card ≤ (badYZ l).card := by
  obtain ⟨i0, _, hi⟩ := Finset.exists_min_image Finset.univ
    (fun i : Fin (n+1) => (badSlice l i).card) (by simp)
  refine ⟨i0, ?_⟩
  calc
    (n+1) * (badSlice l i0).card = ∑ _ : Fin (n+1), (badSlice l i0).card := by simp
    _ ≤ ∑ i : Fin (n+1), (badSlice l i).card := Finset.sum_le_sum hi
    _ = _ := sum_badSlice l

theorem count_residual_y (l : Edge n → Scalar) (i0 : Fin (n+1)) :
    (nonzeroSet (fun p : EdgeY n => residual l i0 (ey p.1 p.2.1 p.2.2))).card ≤
      (n+1) * (badXY l).card := by
  have h := card_le_product_of_witness (β := Fin (n+1))
    (nonzeroSet (fun p : EdgeY n => residual l i0 (ey p.1 p.2.1 p.2.2)))
    (badXY l) (fun i f => (i,f.2.1,f.2.2)) (by
      rintro ⟨i,j,k⟩ hx
      obtain ⟨a,ha⟩ := residual_y_witness l i0 i j k (by simpa only [mem_nonzeroSet] using hx)
      exact ⟨i,(a,j,k), by simpa [badXY] using ha, rfl⟩)
  simpa only [Fintype.card_fin] using h

theorem count_residual_z (l : Edge n → Scalar) (i0 : Fin (n+1)) :
    (nonzeroSet (fun p : EdgeZ n => residual l i0 (ez p.1 p.2.1 p.2.2))).card ≤
      (n+1) * (badXZ l).card + (n+1)^2 * (badSlice l i0).card := by
  have h := card_le_two_products_of_witness (β := Fin (n+1))
    (δ := Fin (n+1) × Fin (n+1))
    (nonzeroSet (fun p : EdgeZ n => residual l i0 (ez p.1 p.2.1 p.2.2)))
    (badXZ l) (badSlice l i0) (fun i f => (i,f.2.1,f.2.2))
    (fun p f => (p.1,p.2,f.2)) (by
      rintro ⟨i,j,k⟩ hx
      rcases residual_z_witness l i0 i j k (by simpa only [mem_nonzeroSet] using hx) with ⟨a,ha⟩ | ⟨b,hb⟩
      · exact Or.inl ⟨i,(a,j,k), by simpa [badXZ] using ha, rfl⟩
      · exact Or.inr ⟨(i,j),(b,k), by simpa [badSlice] using hb, rfl⟩)
  simpa only [Fintype.card_fin, Fintype.card_prod, pow_two] using h

theorem count_curl (l : Edge n → Scalar) :
    (nonzeroSet (curl l)).card = (badXY l).card + (badXZ l).card + (badYZ l).card := by
  simp only [nonzeroSet, badXY, badXZ, badYZ, Finset.card_filter, Fintype.sum_sum_type]
  omega

theorem count_residual (l : Edge n → Scalar) (i0 : Fin (n+1)) :
    (nonzeroSet (residual l i0)).card ≤
      (n+1) * ((badXY l).card + (badXZ l).card) + (n+1)^2 * (badSlice l i0).card := by
  have heq : (nonzeroSet (residual l i0)).card =
      (nonzeroSet (fun p : EdgeY n => residual l i0 (ey p.1 p.2.1 p.2.2))).card +
      (nonzeroSet (fun p : EdgeZ n => residual l i0 (ez p.1 p.2.1 p.2.2))).card := by
    simp only [nonzeroSet, Finset.card_filter, Fintype.sum_sum_type]
    have hx : (∑ p : EdgeX n, if residual l i0 (.inl p) ≠ 0 then 1 else 0) = (0 : ℕ) := by
      apply Finset.sum_eq_zero
      rintro ⟨i,j,k⟩ _
      change (if residual l i0 (ex i j k) ≠ 0 then 1 else 0) = 0
      simp only [residual_x, ne_eq, not_true_eq_false, ite_false]
    rw [hx, zero_add]
    rfl
  rw [heq, Nat.mul_add]
  have hy := count_residual_y l i0
  have hz := count_residual_z l i0
  omega

theorem sparse_cochain (l : Edge n → Scalar) :
    ∃ φ : Vertex n → Scalar,
      (nonzeroSet (l - gradient φ)).card ≤ (n+1) * (nonzeroSet (curl l)).card := by
  obtain ⟨i0,hi⟩ := exists_sparse_slice l
  refine ⟨phi1 l + phi2 l i0 + phi3 l i0, ?_⟩
  rw [← residual_as_gradient, count_curl]
  calc
    (nonzeroSet (residual l i0)).card ≤
      (n+1) * ((badXY l).card + (badXZ l).card) + (n+1)^2 * (badSlice l i0).card :=
        count_residual l i0
    _ ≤ (n+1) * ((badXY l).card + (badXZ l).card) + (n+1) * (badYZ l).card := by
      apply Nat.add_le_add_left
      simpa only [pow_two, Nat.mul_assoc] using Nat.mul_le_mul_left (n+1) hi
    _ = _ := by ring

end WitnessedSeparation.Grid

end OAI