File size: 22,825 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
import Mathlib

namespace OAI

section

open Finset

namespace SmoothObstruction.SymplecticSampling

lemma bit_cases (a : ZMod 2) : a = 0 ∨ a = 1 := by
  have h : ∀ a : ZMod 2, a = 0 ∨ a = 1 := by decide
  exact h a

lemma bit_one {a : ZMod 2} (h : a ≠ 0) : a = 1 := (bit_cases a).resolve_left h

lemma bit_neg (a : ZMod 2) : -a = a := by
  rcases bit_cases a with rfl | rfl <;> decide

variable {V : Type*} [AddCommGroup V] [Module (ZMod 2) V]

lemma add_self (v : V) : v + v = 0 := by
  rw [← two_smul (ZMod 2), show (2 : ZMod 2) = 0 by decide, zero_smul]

lemma alt_symm {B : LinearMap.BilinForm (ZMod 2) V} (hB : B.IsAlt) (x y : V) :
    B x y = B y x := by
  rw [← hB.neg_eq x y, bit_neg]

lemma transvection_preserves {B : LinearMap.BilinForm (ZMod 2) V}
    (hB : B.IsAlt) (v x y : V) :
    B (LinearEquiv.transvection (hB v) x) (LinearEquiv.transvection (hB v) y) = B x y := by
  simp only [LinearEquiv.transvection.apply, map_add, map_smul, LinearMap.add_apply,
    LinearMap.smul_apply, smul_eq_mul, hB v, mul_zero, add_zero]
  rw [← alt_symm hB x v]
  rcases bit_cases (B v x) with hx | hx <;>
    rcases bit_cases (B v y) with hy | hy <;>
    simp [hx, hy, alt_symm hB x v, add_assoc,
      show (1 : ZMod 2) + 1 = 0 by decide]

lemma transvection_moves {B : LinearMap.BilinForm (ZMod 2) V} (hB : B.IsAlt)
    {v w : V} (hvw : B v w = 1) :
    LinearEquiv.transvection (hB (v + w)) v = w := by
  rw [LinearEquiv.transvection.apply, map_add, LinearMap.add_apply, hB v,
    alt_symm hB w v, hvw, zero_add, one_smul, ← add_assoc, add_self, zero_add]

lemma exists_pairing_one {B : LinearMap.BilinForm (ZMod 2) V}
    (hB : B.Nondegenerate) {v : V} (hv : v ≠ 0) : ∃ w, B v w = 1 := by
  have h : ¬ ∀ w, B v w = 0 := fun h => hv (hB.1 v h)
  obtain ⟨w, hw⟩ := not_forall.mp h
  exact ⟨w, bit_one hw⟩

lemma common_pairing_one {B : LinearMap.BilinForm (ZMod 2) V}
    (hB : B.Nondegenerate) {v w : V} (hv : v ≠ 0) (hw : w ≠ 0) :
    ∃ z, B v z = 1 ∧ B w z = 1 := by
  obtain ⟨a, ha⟩ := exists_pairing_one hB hv
  obtain ⟨b, hb⟩ := exists_pairing_one hB hw
  rcases bit_cases (B w a) with hwa | hwa
  · rcases bit_cases (B v b) with hvb | hvb
    · exact ⟨a + b, by simp [ha, hvb], by simp [hwa, hb]⟩
    · exact ⟨b, hvb, hb⟩
  · exact ⟨a, ha, hwa⟩

/-- Explicit symplectic transitivity in characteristic two, using at most two
transvections. This proves the uniform one-label marginal input to the actual
sequential word sampling, without assuming a symplectic-basis theorem. -/
theorem symplectic_transitive {B : LinearMap.BilinForm (ZMod 2) V}
    (hB : B.IsAlt) (hnd : B.Nondegenerate) {v w : V} (hv : v ≠ 0) (hw : w ≠ 0) :
    ∃ e : V ≃ₗ[ZMod 2] V, e v = w ∧ ∀ x y, B (e x) (e y) = B x y := by
  obtain ⟨z, hvz, hwz⟩ := common_pairing_one hnd hv hw
  let e₁ := LinearEquiv.transvection (hB (v + z))
  let e₂ := LinearEquiv.transvection (hB (z + w))
  refine ⟨e₁.trans e₂, ?_, ?_⟩
  · change e₂ (e₁ v) = w
    rw [show e₁ v = z from transvection_moves hB hvz]
    exact transvection_moves hB ((alt_symm hB z w).trans hwz)
  · intro x y
    exact (transvection_preserves hB (z + w) (e₁ x) (e₁ y)).trans
      (transvection_preserves hB (v + z) x y)

section Cardinality
variable [Fintype V] [DecidableEq V]
noncomputable local instance {k : ℕ} (f : V →ₗ[ZMod 2] (Fin k → ZMod 2)) :
    Fintype f.ker := Fintype.ofFinite _

noncomputable def nonzeroKerEquiv {k : ℕ} (f : V →ₗ[ZMod 2] (Fin k → ZMod 2)) :
    {u : f.ker // u ≠ 0} ≃ {v : V // v ≠ 0 ∧ f v = 0} where
  toFun u := ⟨u.val.val, by
    constructor
    · intro h
      exact u.property (Subtype.ext h)
    · exact u.val.property⟩
  invFun v := ⟨⟨v.val, v.property.2⟩, by
    intro h
    exact v.property.1 (congrArg Subtype.val h)⟩
  left_inv u := rfl
  right_inv v := rfl

lemma card_nonzero_ker {k : ℕ} (f : V →ₗ[ZMod 2] (Fin k → ZMod 2)) :
    Fintype.card f.ker = Fintype.card {v : V // v ≠ 0 ∧ f v = 0} + 1 := by
  have h := Fintype.card_congr (Equiv.optionSubtypeNe (0 : f.ker))
  rw [Fintype.card_option, Fintype.card_congr (nonzeroKerEquiv f)] at h
  exact h.symm

/-- At most k linear equations remove at most k binary dimensions, even when
they are dependent. The zero vector is then removed exactly once. -/
theorem common_kernel_lower {k : ℕ} (f : V →ₗ[ZMod 2] (Fin k → ZMod 2)) :
    2 ^ (Module.finrank (ZMod 2) V - k) ≤
      Fintype.card {v : V // v ≠ 0 ∧ f v = 0} + 1 := by
  have hr := f.finrank_range_add_finrank_ker
  have hk : Module.finrank (ZMod 2) f.range ≤ k := by
    simpa using (Submodule.finrank_le f.range)
  have hdim : Module.finrank (ZMod 2) V - k ≤ Module.finrank (ZMod 2) f.ker := by omega
  calc
    _ ≤ 2 ^ Module.finrank (ZMod 2) f.ker := Nat.pow_le_pow_right (by decide) hdim
    _ = Fintype.card f.ker := by rw [Module.card_eq_pow_finrank (K := ZMod 2), ZMod.card]
    _ = _ := card_nonzero_ker f

/-- The quantitative bound actually used in source (word-choices), with the
original nonzero alphabet and dependent equations allowed. -/
theorem common_kernel_choices {k : ℕ} (f : V →ₗ[ZMod 2] (Fin k → ZMod 2))
    (hk : 1 ≤ k) (hd : k + 1 ≤ Module.finrank (ZMod 2) V) :
    Fintype.card {v : V // v ≠ 0} ≤
      2 ^ (2 * k) * Fintype.card {v : V // v ≠ 0 ∧ f v = 0} := by
  let d := Module.finrank (ZMod 2) V
  let c := Fintype.card {v : V // v ≠ 0 ∧ f v = 0}
  have hlo : 2 ^ (d - k) ≤ c + 1 := common_kernel_lower f
  have hexp : d - k = (d - k - 1) + 1 := by dsimp [d]; omega
  have hpow : 2 ^ (d - k - 1) ≤ c := by
    rw [hexp, pow_succ] at hlo
    have hn : 1 ≤ 2 ^ (d - k - 1) := Nat.one_le_pow _ _ (by decide)
    omega
  have hdim : d ≤ 2 * k + (d - k - 1) := by dsimp [d]; omega
  have hfull : Fintype.card V ≤ 2 ^ (2 * k) * c := calc
    _ = 2 ^ d := by rw [Module.card_eq_pow_finrank (K := ZMod 2), ZMod.card]
    _ ≤ 2 ^ (2 * k + (d - k - 1)) := Nat.pow_le_pow_right (by decide) hdim
    _ = 2 ^ (2 * k) * 2 ^ (d - k - 1) := pow_add _ _ _
    _ ≤ _ := Nat.mul_le_mul_left _ hpow
  exact (Fintype.card_subtype_le _).trans hfull

variable (B : LinearMap.BilinForm (ZMod 2) V)

/-- The exact nonzero choices, not an independent-equations approximation. -/
noncomputable def allowed {k : ℕ} (u : Fin k → V) : Finset {v : V // v ≠ 0} :=
  univ.filter (fun v => ∀ j, B (u j) v.val = 0)

noncomputable def equationMap {k : ℕ} (u : Fin k → V) : V →ₗ[ZMod 2] (Fin k → ZMod 2) :=
  LinearMap.pi (fun j => B (u j))

lemma mem_allowed {k : ℕ} (u : Fin k → V) (v : {v : V // v ≠ 0}) :
    v ∈ allowed B u ↔ ∀ j, B (u j) v.val = 0 := by
  classical
  simp [allowed]

lemma allowed_card {k : ℕ} (u : Fin k → V) :
    (allowed B u).card = Fintype.card {v : V // v ≠ 0 ∧ equationMap B u v = 0} := by
  classical
  let e : {v // v ∈ allowed B u} ≃ {v : V // v ≠ 0 ∧ equationMap B u v = 0} :=
    { toFun := fun v => ⟨v.val.val, v.val.property, by
        ext j
        exact (mem_allowed B u v.val).mp v.property j⟩
      invFun := fun v => ⟨⟨v.val, v.property.1⟩, (mem_allowed B u _).mpr (fun j =>
        congrFun v.property.2 j)⟩
      left_inv := fun _ => rfl
      right_inv := fun _ => rfl }
  exact (Fintype.card_coe _).symm.trans (Fintype.card_congr e)

lemma allowed_nonempty {k : ℕ} (u : Fin k → V)
    (hd : k + 1 ≤ Module.finrank (ZMod 2) V) : (allowed B u).Nonempty := by
  have h := common_kernel_lower (equationMap B u)
  rw [← allowed_card B u] at h
  have hpow : 2 ≤ 2 ^ (Module.finrank (ZMod 2) V - k) := by
    simpa using Nat.pow_le_pow_right (n := 2) (by decide) (show 1 ≤
      Module.finrank (ZMod 2) V - k by omega)
  exact card_pos.mp (by omega)

/-- Uniform sampling among all allowed nonzero labels. -/
noncomputable def kernel {k : ℕ} (u : Fin k → V) (v : {v : V // v ≠ 0}) : ℝ :=
  if v ∈ allowed B u then ((allowed B u).card : ℝ)⁻¹ else 0

lemma kernel_nonneg {k : ℕ} (u : Fin k → V) (v : {v : V // v ≠ 0}) :
    0 ≤ kernel B u v := by
  unfold kernel
  split_ifs <;> positivity

lemma kernel_sum {k : ℕ} (u : Fin k → V)
    (hd : k + 1 ≤ Module.finrank (ZMod 2) V) : ∑ v, kernel B u v = 1 := by
  classical
  have hc : ((allowed B u).card : ℝ) ≠ 0 := Nat.cast_ne_zero.mpr
    (Finset.card_ne_zero.mpr (allowed_nonempty B u hd))
  simp only [kernel, sum_ite_mem, univ_inter, sum_const, nsmul_eq_mul]
  exact mul_inv_cancel₀ hc

lemma kernel_upper {k : ℕ} (u : Fin k → V) (v : {v : V // v ≠ 0})
    (hk : 1 ≤ k) (hd : k + 1 ≤ Module.finrank (ZMod 2) V) :
    kernel B u v ≤ (2 : ℝ) ^ (2 * k) / Fintype.card {v : V // v ≠ 0} := by
  have hN : 0 < (Fintype.card {v : V // v ≠ 0} : ℝ) := by
    have : Nonempty {v : V // v ≠ 0} := (allowed_nonempty B u hd).nonempty
    exact_mod_cast Fintype.card_pos
  have hc : 0 < ((allowed B u).card : ℝ) := by
    exact_mod_cast Finset.card_pos.mpr (allowed_nonempty B u hd)
  have hb : (Fintype.card {v : V // v ≠ 0} : ℝ) ≤
      (2 : ℝ) ^ (2 * k) * (allowed B u).card := by
    have h := common_kernel_choices (equationMap B u) hk hd
    rw [← allowed_card B u] at h
    exact_mod_cast h
  unfold kernel
  split_ifs
  · apply (le_div_iff₀ hN).mpr
    apply (inv_mul_le_iff₀ hc).mpr
    simpa only [mul_comm] using hb
  · positivity

noncomputable def nonzeroEquiv (e : V ≃ₗ[ZMod 2] V) :
    {v : V // v ≠ 0} ≃ {v : V // v ≠ 0} where
  toFun v := ⟨e v.val, fun h => v.property (e.injective (h.trans e.map_zero.symm))⟩
  invFun v := ⟨e.symm v.val, fun h => v.property (e.symm.injective (h.trans e.symm.map_zero.symm))⟩
  left_inv v := Subtype.ext (e.symm_apply_apply v.val)
  right_inv v := Subtype.ext (e.apply_symm_apply v.val)

lemma allowed_equivariant {k : ℕ} (u : Fin k → V) (e : V ≃ₗ[ZMod 2] V)
    (he : ∀ x y, B (e x) (e y) = B x y) (v : {v : V // v ≠ 0}) :
    nonzeroEquiv e v ∈ allowed B (e ∘ u) ↔ v ∈ allowed B u := by
  simp only [mem_allowed, nonzeroEquiv, Equiv.coe_fn_mk, Function.comp_apply, he]

lemma allowed_card_equivariant {k : ℕ} (u : Fin k → V) (e : V ≃ₗ[ZMod 2] V)
    (he : ∀ x y, B (e x) (e y) = B x y) :
    (allowed B (e ∘ u)).card = (allowed B u).card := by
  classical
  have h := Fintype.card_congr ((nonzeroEquiv e).subtypeEquiv
    (fun v => (allowed_equivariant B u e he v).symm))
  simpa only [Fintype.card_coe] using h.symm

lemma kernel_equivariant {k : ℕ} (u : Fin k → V) (e : V ≃ₗ[ZMod 2] V)
    (he : ∀ x y, B (e x) (e y) = B x y) (v : {v : V // v ≠ 0}) :
    kernel B (e ∘ u) (nonzeroEquiv e v) = kernel B u v := by
  simp only [kernel, allowed_equivariant B u e he, allowed_card_equivariant B u e he]

end Cardinality

end SmoothObstruction.SymplecticSampling

namespace SmoothObstruction.SequentialLaw

variable {A : Type*}

/-- A finite left-to-right sampling law. The kernel at time k may depend on
all k preceding labels; no independence inside a group is assumed. -/
def weight (K : (k : ℕ) → (Fin k → A) → A → ℝ) : (n : ℕ) → (Fin n → A) → ℝ
  | 0, _ => 1
  | n + 1, w => weight K n (Fin.init w) * K n (Fin.init w) (w (Fin.last n))

@[simp] lemma weight_zero (K : (k : ℕ) → (Fin k → A) → A → ℝ) (w : Fin 0 → A) :
    weight K 0 w = 1 := rfl

@[simp] lemma weight_snoc (K : (k : ℕ) → (Fin k → A) → A → ℝ)
    {n : ℕ} (w : Fin n → A) (a : A) :
    weight K (n + 1) (Fin.snoc w a) = weight K n w * K n w a := by
  simp [weight]

lemma weight_nonneg {K : (k : ℕ) → (Fin k → A) → A → ℝ}
    (hK : ∀ k w a, 0 ≤ K k w a) : ∀ n w, 0 ≤ weight K n w
  | 0, _ => by simp
  | n + 1, w => mul_nonneg (weight_nonneg hK n _) (hK n _ _)

variable [Fintype A]

omit [Fintype A] in
lemma snoc_equiv_apply {n : ℕ} (p : A × (Fin n → A)) :
    (Fin.snocEquiv (fun _ : Fin (n + 1) => A)) p = Fin.snoc p.2 p.1 := rfl

lemma sum_weight {K : (k : ℕ) → (Fin k → A) → A → ℝ}
    (hK : ∀ k w, ∑ a, K k w a = 1) : ∀ n, ∑ w, weight K n w = 1 := by
  intro n
  induction n with
  | zero => simp
  | succ n ih =>
    rw [← (Fin.snocEquiv (fun _ : Fin (n + 1) => A)).sum_comp]
    simp only [snoc_equiv_apply, weight_snoc, Fintype.sum_prod_type]
    rw [sum_comm]
    simp only [← mul_sum, hK, mul_one]
    exact ih

variable [DecidableEq A]

def mark (m : Option A) (a : A) : ℝ :=
  match m with
  | none => 1
  | some b => if a = b then 1 else 0

def test {n : ℕ} (m : Fin n → Option A) (w : Fin n → A) : ℝ :=
  ∏ i, mark (m i) (w i)

omit [Fintype A] in
lemma mark_nonneg (m : Option A) (a : A) : 0 ≤ mark m a := by
  cases m with
  | none => norm_num [mark]
  | some b => change 0 ≤ (if _ = b then (1 : ℝ) else 0); split_ifs <;> norm_num

omit [Fintype A] in
lemma test_nonneg {n : ℕ} (m : Fin n → Option A) (w : Fin n → A) : 0 ≤ test m w :=
  prod_nonneg (fun i _ => mark_nonneg (m i) (w i))

omit [Fintype A] in
@[simp] lemma test_snoc {n : ℕ} (m : Fin (n + 1) → Option A) (w : Fin n → A) (a : A) :
    test m (Fin.snoc w a) = test (Fin.init m) w * mark (m (Fin.last n)) a := by
  unfold test
  rw [Fin.prod_univ_castSucc]
  simp only [Fin.snoc_castSucc, Fin.snoc_last]
  rfl

lemma sum_test_succ (K : (k : ℕ) → (Fin k → A) → A → ℝ)
    {n : ℕ} (m : Fin (n + 1) → Option A) :
    (∑ w, weight K (n + 1) w * test m w) =
      ∑ w, (weight K n w * test (Fin.init m) w) *
        (∑ a, K n w a * mark (m (Fin.last n)) a) := by
  rw [← (Fin.snocEquiv (fun _ : Fin (n + 1) => A)).sum_comp]
  simp only [snoc_equiv_apply, weight_snoc, test_snoc, Fintype.sum_prod_type]
  rw [sum_comm]
  apply sum_congr rfl
  intro w _
  rw [mul_sum]
  apply sum_congr rfl
  intro a _
  ring

/-- Any list of specified coordinates has probability at most the product
of the pointwise conditional upper bounds. Unspecified labels are integrated
out, exactly as in (word-raw-sublist). -/
theorem cylinder_bound {K : (k : ℕ) → (Fin k → A) → A → ℝ} {ζ : ℝ}
    (h0 : ∀ k w a, 0 ≤ K k w a) (h1 : ∀ k w, ∑ a, K k w a = 1)
    (hζ : 0 ≤ ζ) (hK : ∀ k w a, K k w a ≤ ζ) :
    ∀ n (m : Fin n → Option A), (∑ w, weight K n w * test m w) ≤
      ∏ i, (if (m i).isSome then ζ else 1) := by
  intro n
  induction n with
  | zero => intro m; simp [test]
  | succ n ih =>
    intro m
    rw [sum_test_succ, Fin.prod_univ_castSucc]
    have hstep (w : Fin n → A) : (∑ a, K n w a * mark (m (Fin.last n)) a) ≤
        if (m (Fin.last n)).isSome then ζ else 1 := by
      cases hm : m (Fin.last n) with
      | none => simp [mark, h1]
      | some a => simpa [mark] using hK n w a
    calc
      _ ≤ ∑ w, (weight K n w * test (Fin.init m) w) *
          (if (m (Fin.last n)).isSome then ζ else 1) :=
        sum_le_sum (fun w _ => mul_le_mul_of_nonneg_left (hstep w)
          (mul_nonneg (weight_nonneg h0 n w) (test_nonneg _ _)))
      _ = (∑ w, weight K n w * test (Fin.init m) w) *
          (if (m (Fin.last n)).isSome then ζ else 1) := by rw [sum_mul]
      _ ≤ _ := by
        apply mul_le_mul_of_nonneg_right (ih (Fin.init m))
        split_ifs <;> positivity

omit [Fintype A] [DecidableEq A] in
lemma weight_equivariant (K : (k : ℕ) → (Fin k → A) → A → ℝ) (e : A ≃ A)
    (hK : ∀ k w a, K k (e ∘ w) (e a) = K k w a) :
    ∀ n w, weight K n (e ∘ w) = weight K n w := by
  intro n
  induction n with
  | zero => intro w; rfl
  | succ n ih =>
    intro w
    simp only [weight]
    have hi : Fin.init (e ∘ w) = e ∘ Fin.init w := rfl
    rw [hi, ih, Function.comp_apply, hK]

lemma test_eq_indicator {n : ℕ} (m : Fin n → Option A) (w : Fin n → A) :
    test m w = if ∀ i a, m i = some a → w i = a then 1 else 0 := by
  classical
  have hm (i : Fin n) : mark (m i) (w i) =
      if ∀ a, m i = some a → w i = a then 1 else 0 := by
    cases h : m i <;> simp [mark]
  simp only [test, hm, Finset.prod_boole, Finset.mem_univ, forall_const]

lemma test_upper_pow {K : (k : ℕ) → (Fin k → A) → A → ℝ} {ζ : ℝ}
    (h0 : ∀ k w a, 0 ≤ K k w a) (h1 : ∀ k w, ∑ a, K k w a = 1)
    (hζ : 0 ≤ ζ) (hK : ∀ k w a, K k w a ≤ ζ)
    (n : ℕ) (m : Fin n → Option A) :
    (∑ w, weight K n w * test m w) ≤
      ζ ^ (univ.filter (fun i => (m i).isSome)).card := by
  simpa only [Finset.prod_ite, Finset.prod_const, one_pow, mul_one]
    using cylinder_bound h0 h1 hζ hK n m

/-- The unconditioned one-coordinate law; the complete path is summed out. -/
def marginal (K : (k : ℕ) → (Fin k → A) → A → ℝ) {n : ℕ} (i : Fin n) (a : A) : ℝ :=
  ∑ w, if w i = a then weight K n w else 0

lemma sum_marginal {K : (k : ℕ) → (Fin k → A) → A → ℝ}
    (hK : ∀ k w, ∑ a, K k w a = 1) {n : ℕ} (i : Fin n) :
    ∑ a, marginal K i a = 1 := by
  unfold marginal
  rw [sum_comm]
  simp only [sum_ite_eq, mem_univ, ite_true]
  exact sum_weight hK n

lemma marginal_equivariant (K : (k : ℕ) → (Fin k → A) → A → ℝ) (e : A ≃ A)
    (hK : ∀ k w a, K k (e ∘ w) (e a) = K k w a) {n : ℕ} (i : Fin n) (a : A) :
    marginal K i (e a) = marginal K i a := by
  unfold marginal
  let E : (Fin n → A) ≃ (Fin n → A) := Equiv.piCongrRight (fun _ => e)
  rw [← E.sum_comp]
  apply sum_congr rfl
  intro w _
  change (if e (w i) = e a then weight K n (e ∘ w) else 0) = _
  simp only [e.injective.eq_iff, weight_equivariant K e hK]

/-- Equivariance under a transitive collection of label permutations gives
exactly uniform marginals, despite arbitrary dependencies between positions. -/
theorem uniform_marginal [Nonempty A]
    {K : (k : ℕ) → (Fin k → A) → A → ℝ}
    (h1 : ∀ k w, ∑ a, K k w a = 1)
    (htrans : ∀ a b : A, ∃ e : A ≃ A, e a = b ∧
      ∀ k w x, K k (e ∘ w) (e x) = K k w x)
    {n : ℕ} (i : Fin n) (a : A) : marginal K i a = 1 / Fintype.card A := by
  have heq (b : A) : marginal K i b = marginal K i a := by
    obtain ⟨e, hab, he⟩ := htrans a b
    rw [← hab]
    exact marginal_equivariant K e he i a
  have hsum := sum_marginal h1 i
  simp_rw [heq] at hsum
  simp only [sum_const, card_univ, nsmul_eq_mul] at hsum
  have hc : (Fintype.card A : ℝ) ≠ 0 := Nat.cast_ne_zero.mpr Fintype.card_ne_zero
  exact (eq_div_iff hc).mpr (by simpa only [mul_comm] using hsum)

end SmoothObstruction.SequentialLaw

namespace SmoothObstruction.GroupSampler
open Finset SymplecticSampling SequentialLaw

variable {V : Type*} [AddCommGroup V] [Module (ZMod 2) V]
  [Fintype V] [DecidableEq V]
variable (B : LinearMap.BilinForm (ZMod 2) V) {K : ℕ}

/-- At time t, at most K earlier positions are constrained. None represents
an absent constraint, whose equation is identically zero. -/
def previous (back : (t : ℕ) → Fin K → Option (Fin t))
    (t : ℕ) (w : Fin t → {v : V // v ≠ 0}) (j : Fin K) : V :=
  (back t j).elim 0 (fun r => (w r).val)

noncomputable def step (back : (t : ℕ) → Fin K → Option (Fin t))
    (t : ℕ) (w : Fin t → {v : V // v ≠ 0}) (a : {v : V // v ≠ 0}) : ℝ :=
  kernel B (previous back t w) a

lemma step_sum (back : (t : ℕ) → Fin K → Option (Fin t))
    (hd : K + 1 ≤ Module.finrank (ZMod 2) V) (t : ℕ) (w : Fin t → {v : V // v ≠ 0}) :
    ∑ a, step B back t w a = 1 := kernel_sum B _ hd

omit [Fintype V] [DecidableEq V] in
lemma previous_equivariant (back : (t : ℕ) → Fin K → Option (Fin t))
    (e : V ≃ₗ[ZMod 2] V) (t : ℕ) (w : Fin t → {v : V // v ≠ 0}) :
    previous back t (nonzeroEquiv e ∘ w) = e ∘ previous back t w := by
  funext j
  cases h : back t j <;> simp [previous, h, nonzeroEquiv]

lemma step_equivariant (back : (t : ℕ) → Fin K → Option (Fin t))
    (e : V ≃ₗ[ZMod 2] V) (he : ∀ x y, B (e x) (e y) = B x y)
    (t : ℕ) (w : Fin t → {v : V // v ≠ 0}) (a : {v : V // v ≠ 0}) :
    step B back t (nonzeroEquiv e ∘ w) (nonzeroEquiv e a) = step B back t w a := by
  unfold step
  rw [previous_equivariant, kernel_equivariant B _ e he]

/-- The actual sequential sampling has exactly uniform one-position laws.
This is not a product law inside a group. -/
theorem uniform_position (hB : B.IsAlt) (hnd : B.Nondegenerate)
    (back : (t : ℕ) → Fin K → Option (Fin t))
    (hd : K + 1 ≤ Module.finrank (ZMod 2) V)
    {n : ℕ} (i : Fin n) (a : {v : V // v ≠ 0}) :
    marginal (step B back) i a = 1 / Fintype.card {v : V // v ≠ 0} := by
  let : Nonempty {v : V // v ≠ 0} := ⟨a⟩
  apply uniform_marginal (step_sum B back hd)
  intro v w
  obtain ⟨e, he, hpres⟩ := symplectic_transitive hB hnd v.property w.property
  exact ⟨nonzeroEquiv e, Subtype.ext he, step_equivariant B back e hpres⟩

/-- All sublist probabilities have the source bound, even though prior
constraints can be linearly dependent and unobserved labels are integrated out. -/
theorem raw_sublist (back : (t : ℕ) → Fin K → Option (Fin t))
    (hK : 1 ≤ K) (hd : K + 1 ≤ Module.finrank (ZMod 2) V)
    (n : ℕ) (m : Fin n → Option {v : V // v ≠ 0}) :
    (∑ w, weight (step B back) n w * test m w) ≤
      ((2 : ℝ) ^ (2 * K) / Fintype.card {v : V // v ≠ 0}) ^
        (univ.filter (fun i => (m i).isSome)).card := by
  apply test_upper_pow
  · intro t w a; exact kernel_nonneg B _ _
  · exact step_sum B back hd
  · positivity
  · intro t w a; exact kernel_upper B _ _ hK hd

lemma step_positive_constraint (back : (t : ℕ) → Fin K → Option (Fin t))
    (t : ℕ) (w : Fin t → {v : V // v ≠ 0}) (a : {v : V // v ≠ 0})
    (h : 0 < step B back t w a) {j : Fin K} {r : Fin t} (hr : back t j = some r) :
    B (w r).val a.val = 0 := by
  have hm : a ∈ allowed B (previous back t w) := by
    by_contra hc
    simp [step, kernel, hc] at h
  have he := (mem_allowed B _ _).mp hm j
  simpa [previous, hr] using he

/-- Positively weighted paths satisfy every prescribed relation. -/
theorem supported_constraints (back : (t : ℕ) → Fin K → Option (Fin t)) :
    ∀ n (w : Fin n → {v : V // v ≠ 0}), 0 < weight (step B back) n w →
      ∀ (t : Fin n) (j : Fin K) (r : Fin t.val), back t.val j = some r →
        B (w ⟨r.val, lt_trans r.isLt t.isLt⟩).val (w t).val = 0 := by
  intro n
  induction n with
  | zero => intro w h t; exact Fin.elim0 t
  | succ n ih =>
    intro w hw t j r hr
    have hp : 0 < weight (step B back) n (Fin.init w) ∧
        0 < step B back n (Fin.init w) (w (Fin.last n)) := by
      rcases mul_pos_iff.mp hw with h | h
      · exact h
      · exact False.elim ((not_lt_of_ge (kernel_nonneg B _ _)) h.2)
    by_cases ht : t.val < n
    · exact ih (Fin.init w) hp.1 ⟨t.val, ht⟩ j r hr
    · have teq : t = Fin.last n := Fin.ext (by simp; omega)
      subst t
      exact step_positive_constraint B back n (Fin.init w) (w (Fin.last n)) hp.2 hr

end SmoothObstruction.GroupSampler

end

end OAI