lean/FeedbackChannel/LatticeJoin.lean

View on GitHub

  1import Mathlib.Tactic
  2import Mathlib.Combinatorics.SimpleGraph.Acyclic
  3import Mathlib.Data.Setoid.Basic
  4
  5/-!
  6# Proposition 2c: two category leaks reveal the joint leak iff the intersection graph is a forest
  7
  8Source: `paper/evaluation_feedback_lattice.md` (version 0.6), §2, Proposition 2c; Python
  9`intersection_graph`, `intersection_graph_is_forest`, `join_failure_witness`.
 10
 11Category maps are labelings `γ : ι → R`, `γ' : ι → C` of the items (blocks = nonempty fibres;
 12unused labels are isolated vertices and change nothing). A record is `r : ι → Bool` (`true` =
 13pass). `Φ(γ)` reveals the per-block pass counts; the channel that observes both leaks identifies
 14records with equal row *and* column counts; `Φ(γ ∨ γ')` reveals the pass count of every cell
 15`γ⁻¹ a ∩ γ'⁻¹ b`. The intersection graph `I(γ, γ')` is the bipartite graph on `R ⊕ C` with an edge
 16`a — b` when the cell is nonempty, as a Mathlib `SimpleGraph`, and *acyclic* is Mathlib's
 17`SimpleGraph.IsAcyclic`.
 18
 19The combinatorial core (`margins_determine_iff_acyclic`): on the box `0 ≤ w ≤ cap` of cell
 20weights, row and column margins determine the weights iff the bipartite graph of positive-capacity
 21cells is acyclic. Forest direction: a cut argument across a bridge (every edge of a forest is
 22one). Cycle direction: an alternating `±1` weight along a path closing a non-bridge edge.
 23-/
 24
 25namespace FeedbackChannel.LatticeJoin
 26
 27open SimpleGraph Finset
 28
 29variable {R C : Type*} [Fintype R] [Fintype C] [DecidableEq R] [DecidableEq C]
 30
 31/-- The bipartite graph on `R ⊕ C` with an edge `inl a — inr b` exactly when `E a b`. -/
 32def bip (E : R → C → Prop) : SimpleGraph (R ⊕ C) :=
 33  SimpleGraph.fromRel fun u v => ∃ a b, u = Sum.inl a ∧ v = Sum.inr b ∧ E a b
 34
 35section Core
 36
 37variable {E : R → C → Prop}
 38
 39@[simp] lemma bip_inl_inr (a : R) (b : C) : (bip E).Adj (Sum.inl a) (Sum.inr b) ↔ E a b := by
 40  simp [bip]
 41
 42@[simp] lemma bip_inr_inl (a : R) (b : C) : (bip E).Adj (Sum.inr b) (Sum.inl a) ↔ E a b := by
 43  simp [bip]
 44
 45@[simp] lemma bip_inl_inl (a a' : R) : ¬ (bip E).Adj (Sum.inl a) (Sum.inl a') := by
 46  simp [bip]
 47
 48@[simp] lemma bip_inr_inr (b b' : C) : ¬ (bip E).Adj (Sum.inr b) (Sum.inr b') := by
 49  simp [bip]
 50
 51lemma bip_congr {E E' : R → C → Prop} (h : ∀ a b, E a b ↔ E' a b) : bip E = bip E' := by
 52  ext u v
 53  simp only [bip, fromRel_adj, h]
 54
 55/-- Row margin of an integer cell weight. -/
 56def rowM (w : R → C → ℤ) (a : R) : ℤ := ∑ b, w a b
 57
 58/-- Column margin of an integer cell weight. -/
 59def colM (w : R → C → ℤ) (b : C) : ℤ := ∑ a, w a b
 60
 61/-- **Forest direction.** If the graph is acyclic, a weight supported on its edges with all row
 62and column margins zero is zero. -/
 63theorem acyclic_margins_zero (hG : (bip E).IsAcyclic) (d : R → C → ℤ)
 64    (hsupp : ∀ a b, ¬ E a b → d a b = 0) (hrow : ∀ a, rowM d a = 0) (hcol : ∀ b, colM d b = 0) :
 65    d = 0 := by
 66  classical
 67  funext a b
 68  show d a b = 0
 69  by_cases hab : E a b
 70  swap
 71  · exact hsupp a b hab
 72  have hbr := isAcyclic_iff_forall_adj_isBridge.1 hG ((bip_inl_inr a b).2 hab)
 73  rw [isBridge_iff] at hbr
 74  set G' := (bip E).deleteEdges {s(Sum.inl a, Sum.inr b)} with hG'
 75  let ind : R ⊕ C → ℤ := fun v => if G'.Reachable (Sum.inl a) v then 1 else 0
 76  have hcell : ∀ x y, d x y * (ind (Sum.inl x) - ind (Sum.inr y)) =
 77      if x = a ∧ y = b then d a b else 0 := by
 78    intro x y
 79    by_cases hxy : x = a ∧ y = b
 80    · obtain ⟨rfl, rfl⟩ := hxy
 81      simp [ind, hbr, Reachable.refl]
 82    · rw [if_neg hxy]
 83      by_cases hE : E x y
 84      · have hadj : G'.Adj (Sum.inl x) (Sum.inr y) := by
 85          rw [hG', deleteEdges_adj]
 86          refine ⟨(bip_inl_inr x y).2 hE, ?_⟩
 87          simp only [Set.mem_singleton_iff, Sym2.eq_iff, Sum.inl.injEq, Sum.inr.injEq,
 88            reduceCtorEq, and_false, or_false]
 89          exact hxy
 90        have hiff : G'.Reachable (Sum.inl a) (Sum.inl x) ↔ G'.Reachable (Sum.inl a) (Sum.inr y) :=
 91          ⟨fun h => h.trans hadj.reachable, fun h => h.trans hadj.symm.reachable⟩
 92        simp only [ind, hiff, sub_self, mul_zero]
 93      · rw [hsupp x y hE, zero_mul]
 94  have hsum : ∑ x, ∑ y, d x y * (ind (Sum.inl x) - ind (Sum.inr y)) = d a b := by
 95    simp_rw [hcell, ite_and]
 96    simp
 97  have hzero : ∑ x, ∑ y, d x y * (ind (Sum.inl x) - ind (Sum.inr y)) = 0 := by
 98    have h1 : ∑ x, ∑ y, d x y * ind (Sum.inl x) = ∑ x, rowM d x * ind (Sum.inl x) := by
 99      refine Finset.sum_congr rfl fun x _ => ?_
100      rw [rowM, Finset.sum_mul]
101    have h2 : ∑ x, ∑ y, d x y * ind (Sum.inr y) = ∑ y, colM d y * ind (Sum.inr y) := by
102      rw [Finset.sum_comm]
103      refine Finset.sum_congr rfl fun y _ => ?_
104      rw [colM, Finset.sum_mul]
105    simp only [mul_sub, Finset.sum_sub_distrib, h1, h2, hrow, hcol, zero_mul,
106      Finset.sum_const_zero, sub_zero]
107  rw [← hsum, hzero]
108
109/-- Weight `1` on the cell of the edge `u — v` (zero unless `{u, v} = {inl x, inr y}`). -/
110def eW (u v : R ⊕ C) (x : R) (y : C) : ℤ :=
111  if (u = Sum.inl x ∧ v = Sum.inr y) ∨ (u = Sum.inr y ∧ v = Sum.inl x) then 1 else 0
112
113/-- Alternating `±1` weight along a walk: first edge `+1`, second `-1`, and so on. -/
114def wW {G : SimpleGraph (R ⊕ C)} : {u v : R ⊕ C} → G.Walk u v → R → C → ℤ
115  | _, _, .nil => 0
116  | u, _, .cons (v := w) _ p => eW u w - wW p
117
118/-- Margin at a vertex: row margin at `inl a`, column margin at `inr b`. -/
119def vm (w : R → C → ℤ) : R ⊕ C → ℤ
120  | Sum.inl a => rowM w a
121  | Sum.inr b => colM w b
122
123lemma vm_sub (f g : R → C → ℤ) (z : R ⊕ C) : vm (f - g) z = vm f z - vm g z := by
124  cases z <;> simp [vm, rowM, colM, Finset.sum_sub_distrib]
125
126lemma vm_zero (z : R ⊕ C) : vm (0 : R → C → ℤ) z = 0 := by
127  cases z <;> simp [vm, rowM, colM]
128
129/-- Indicator of a vertex. -/
130def ind (z u : R ⊕ C) : ℤ := if z = u then 1 else 0
131
132lemma eW_inl_inr (a : R) (b : C) (x : R) (y : C) :
133    eW (Sum.inl a) (Sum.inr b) x y = if a = x ∧ b = y then 1 else 0 := by
134  simp [eW]
135
136lemma eW_symm (u v : R ⊕ C) : eW u v = eW v u := by
137  funext x y; unfold eW; congr 1; apply propext; tauto
138
139lemma vm_eW_inl_inr (a : R) (b : C) (z : R ⊕ C) :
140    vm (eW (Sum.inl a) (Sum.inr b)) z = ind z (Sum.inl a) + ind z (Sum.inr b) := by
141  cases z with
142  | inl x =>
143    simp only [vm, rowM, eW_inl_inr, ind, Sum.inl.injEq, reduceCtorEq, if_false, add_zero]
144    by_cases hx : x = a
145    · subst hx; simp
146    · simp [hx, Ne.symm hx]
147  | inr y =>
148    simp only [vm, colM, eW_inl_inr, ind, Sum.inr.injEq, reduceCtorEq, if_false, zero_add]
149    by_cases hy : y = b
150    · subst hy; simp
151    · simp [hy, Ne.symm hy]
152
153lemma vm_eW {u v : R ⊕ C} (h : (bip E).Adj u v) (z : R ⊕ C) : vm (eW u v) z = ind z u + ind z v := by
154  rcases u with a | b <;> rcases v with a' | b'
155  · exact absurd h (bip_inl_inl a a')
156  · exact vm_eW_inl_inr a b' z
157  · rw [eW_symm, vm_eW_inl_inr, add_comm]
158  · exact absurd h (bip_inr_inr b b')
159
160/-- Side of a vertex, `1` for rows and `-1` for columns. -/
161def side : R ⊕ C → ℤ
162  | Sum.inl _ => 1
163  | Sum.inr _ => -1
164
165lemma side_adj {u v : R ⊕ C} (h : (bip E).Adj u v) : side u = - side v := by
166  rcases u with a | b <;> rcases v with a' | b'
167  · exact absurd h (bip_inl_inl a a')
168  · simp [side]
169  · simp [side]
170  · exact absurd h (bip_inr_inr b b')
171
172lemma side_sq (u : R ⊕ C) : side u * side u = 1 := by
173  cases u <;> simp [side]
174
175variable {G : SimpleGraph (R ⊕ C)}
176
177/-- A walk in a subgraph of a bipartite graph changes side at every step. -/
178lemma walk_parity (hGle : G ≤ bip E) {u v : R ⊕ C} (p : G.Walk u v) :
179    (-1 : ℤ) ^ p.length = side u * side v := by
180  induction p with
181  | nil => simp [side_sq]
182  | cons h p ih =>
183    rw [Walk.length_cons, pow_succ, ih, side_adj (hGle h)]
184    ring
185
186/-- The margins of the alternating weight of a walk from `u` to `v` are `δ_u - (-1)^len δ_v`. -/
187lemma vm_wW (hGle : G ≤ bip E) {u v : R ⊕ C} (p : G.Walk u v) (z : R ⊕ C) :
188    vm (wW p) z = ind z u - (-1 : ℤ) ^ p.length * ind z v := by
189  induction p with
190  | nil => simp [wW, vm_zero]
191  | cons h p ih =>
192    simp only [wW, vm_sub, vm_eW (hGle h), ih, Walk.length_cons, pow_succ]
193    ring
194
195lemma eW_ne_zero {u v : R ⊕ C} {x : R} {y : C} (h : eW u v x y ≠ 0) :
196    s(u, v) = s(Sum.inl x, Sum.inr y) := by
197  unfold eW at h
198  split_ifs at h with hc
199  · rcases hc with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩
200    · rfl
201    · exact Sym2.eq_swap
202  · exact absurd rfl h
203
204lemma eW_values (u v : R ⊕ C) (x : R) (y : C) : eW u v x y = 0 ∨ eW u v x y = 1 := by
205  unfold eW; split_ifs <;> simp
206
207/-- The alternating weight lives on the walk's edges. -/
208lemma wW_supp {u v : R ⊕ C} (p : G.Walk u v) (x : R) (y : C) (h : wW p x y ≠ 0) :
209    s(Sum.inl x, Sum.inr y) ∈ p.edges := by
210  induction p with
211  | nil => exact absurd rfl h
212  | @cons u' w' v' h' p ih =>
213    rw [Walk.edges_cons, List.mem_cons]
214    simp only [wW, Pi.sub_apply] at h
215    by_cases he : eW u' w' x y = 0
216    · rw [he, zero_sub, neg_ne_zero] at h
217      exact Or.inr (ih h)
218    · exact Or.inl (eW_ne_zero he).symm
219
220/-- On a trail the alternating weight takes values in `{0, 1, -1}`. -/
221lemma wW_trail {u v : R ⊕ C} (p : G.Walk u v) (hp : p.IsTrail) (x : R) (y : C) :
222    wW p x y = 0 ∨ wW p x y = 1 ∨ wW p x y = -1 := by
223  induction p with
224  | nil => simp [wW]
225  | @cons u' w' v' h' p ih =>
226    rw [Walk.isTrail_cons] at hp
227    simp only [wW, Pi.sub_apply]
228    rcases eW_values u' w' x y with he | he
229    · rw [he, zero_sub]
230      rcases ih hp.1 with h | h | h <;> simp [h]
231    · have hmem := eW_ne_zero (show eW u' w' x y ≠ 0 by rw [he]; norm_num)
232      have h0 : wW p x y = 0 := by
233        by_contra hne
234        exact hp.2 (hmem ▸ wW_supp p x y hne)
235      rw [he, h0]; simp
236
237/-- A graph that is not acyclic has a non-bridge edge `inl a — inr b`. -/
238lemma exists_nonbridge (h : ¬ (bip E).IsAcyclic) :
239    ∃ a b, E a b ∧ ((bip E).deleteEdges {s(Sum.inl a, Sum.inr b)}).Reachable (Sum.inl a) (Sum.inr b) := by
240  rw [isAcyclic_iff_forall_adj_isBridge] at h
241  push Not at h
242  obtain ⟨v, w, hadj, hnb⟩ := h
243  rw [isBridge_iff, not_not] at hnb
244  rcases v with a | b <;> rcases w with a' | b'
245  · exact absurd hadj (bip_inl_inl a a')
246  · exact ⟨a, b', (bip_inl_inr a b').1 hadj, hnb⟩
247  · refine ⟨a', b, (bip_inr_inl a' b).1 hadj, ?_⟩
248    rw [Sym2.eq_swap]; exact hnb.symm
249  · exact absurd hadj (bip_inr_inr b b')
250
251/-- **Cycle direction.** If the graph is not acyclic, there is a nonzero weight with values in
252`{0, 1, -1}`, supported on the edges, with every row and column margin zero. -/
253theorem not_acyclic_witness (h : ¬ (bip E).IsAcyclic) :
254    ∃ d : R → C → ℤ, d ≠ 0 ∧ (∀ a b, ¬ E a b → d a b = 0) ∧
255      (∀ a b, d a b = 0 ∨ d a b = 1 ∨ d a b = -1) ∧ (∀ a, rowM d a = 0) ∧ (∀ b, colM d b = 0) := by
256  classical
257  obtain ⟨a, b, hab, hreach⟩ := exists_nonbridge h
258  set G' := (bip E).deleteEdges {s(Sum.inl a, Sum.inr b)} with hG'
259  have hle : G' ≤ bip E := deleteEdges_le _
260  obtain ⟨p⟩ := hreach
261  set q := p.bypass with hq
262  have hqt : q.IsTrail := (Walk.bypass_isPath p).isTrail
263  have hqE : ∀ e ∈ q.edges, e ∈ G'.edgeSet := fun e he => q.edges_subset_edgeSet he
264  have hab_notin : s(Sum.inl a, Sum.inr b) ∉ q.edges := by
265    intro hm
266    have := hqE _ hm
267    rw [hG', edgeSet_deleteEdges] at this
268    exact this.2 rfl
269  have hq0 : wW q a b = 0 := by
270    by_contra hne; exact hab_notin (wW_supp q a b hne)
271  set d : R → C → ℤ := wW q - eW (Sum.inl a) (Sum.inr b) with hd
272  have heW : ∀ x y, eW (Sum.inl a) (Sum.inr b) x y = if x = a ∧ y = b then 1 else 0 := by
273    intro x y; rw [eW_inl_inr]; simp only [@eq_comm _ a x, @eq_comm _ b y]
274  have hvm : ∀ z, vm d z = 0 := by
275    intro z
276    rw [hd, vm_sub, vm_wW hle, vm_eW ((bip_inl_inr a b).2 hab), walk_parity hle]
277    simp only [side]
278    ring
279  refine ⟨d, ?_, ?_, ?_, fun x => hvm (Sum.inl x), fun y => hvm (Sum.inr y)⟩
280  · intro h0
281    have := congrFun (congrFun h0 a) b
282    simp only [hd, Pi.sub_apply, hq0, heW, and_self, if_true, Pi.zero_apply] at this
283    norm_num at this
284  · intro x y hxy
285    by_cases hx : x = a ∧ y = b
286    · obtain ⟨rfl, rfl⟩ := hx; exact absurd hab hxy
287    · simp only [hd, Pi.sub_apply, heW, if_neg hx, sub_zero]
288      by_contra hne
289      have hm := hqE _ (wW_supp q x y hne)
290      rw [hG', edgeSet_deleteEdges] at hm
291      exact hxy ((bip_inl_inr x y).1 hm.1)
292  · intro x y
293    by_cases hx : x = a ∧ y = b
294    · obtain ⟨rfl, rfl⟩ := hx
295      simp [hd, hq0, heW]
296    · simp only [hd, Pi.sub_apply, heW, if_neg hx, sub_zero]
297      exact wW_trail q hqt x y
298
299/-- Integer form of the core: margins determine every weight supported on the edges iff the graph
300is acyclic. -/
301theorem margins_determine_int_iff :
302    (∀ d : R → C → ℤ, (∀ a b, ¬ E a b → d a b = 0) → (∀ a, rowM d a = 0) → (∀ b, colM d b = 0) →
303      d = 0) ↔ (bip E).IsAcyclic := by
304  constructor
305  · intro h
306    by_contra hG
307    obtain ⟨d, hne, hsupp, -, hrow, hcol⟩ := not_acyclic_witness hG
308    exact hne (h d hsupp hrow hcol)
309  · intro hG d hsupp hrow hcol
310    exact acyclic_margins_zero hG d hsupp hrow hcol
311
312end Core
313
314/-- **Proposition 2c, combinatorial core, on the note's box.** For cell capacities `cap` (the cell
315sizes), row and column margins determine every weight `0 ≤ w ≤ cap` iff the bipartite graph of the
316positive-capacity cells is acyclic. -/
317theorem margins_determine_iff_acyclic (cap : R → C → ℕ) :
318    (∀ w w' : R → C → ℕ, (∀ a b, w a b ≤ cap a b) → (∀ a b, w' a b ≤ cap a b) →
319      (∀ a, ∑ b, w a b = ∑ b, w' a b) → (∀ b, ∑ a, w a b = ∑ a, w' a b) → w = w') ↔
320    (bip fun a b => 0 < cap a b).IsAcyclic := by
321  constructor
322  · intro h
323    by_contra hG
324    obtain ⟨d, hne, hsupp, hval, hrow, hcol⟩ := not_acyclic_witness hG
325    let w : R → C → ℕ := fun x y => if d x y = 1 then 1 else 0
326    let w' : R → C → ℕ := fun x y => if d x y = -1 then 1 else 0
327    have hdiff : ∀ x y, ((w x y : ℕ) : ℤ) - ((w' x y : ℕ) : ℤ) = d x y := by
328      intro x y
329      rcases hval x y with h | h | h <;> simp [w, w', h]
330    have hcap : ∀ x y, d x y ≠ 0 → 1 ≤ cap x y := by
331      intro x y hxy
332      by_contra hlt
333      exact hxy (hsupp x y (by omega))
334    have hw : ∀ x y, w x y ≤ cap x y := by
335      intro x y
336      simp only [w]
337      split_ifs with h1
338      · exact hcap x y (by rw [h1]; norm_num)
339      · exact Nat.zero_le _
340    have hw' : ∀ x y, w' x y ≤ cap x y := by
341      intro x y
342      simp only [w']
343      split_ifs with h1
344      · exact hcap x y (by rw [h1]; norm_num)
345      · exact Nat.zero_le _
346    have hr : ∀ x, ∑ y, w x y = ∑ y, w' x y := by
347      intro x
348      have : ((∑ y, w x y : ℕ) : ℤ) - ((∑ y, w' x y : ℕ) : ℤ) = 0 := by
349        push_cast
350        rw [← Finset.sum_sub_distrib]
351        simp only [hdiff]
352        exact hrow x
353      omega
354    have hc : ∀ y, ∑ x, w x y = ∑ x, w' x y := by
355      intro y
356      have : ((∑ x, w x y : ℕ) : ℤ) - ((∑ x, w' x y : ℕ) : ℤ) = 0 := by
357        push_cast
358        rw [← Finset.sum_sub_distrib]
359        simp only [hdiff]
360        exact hcol y
361      omega
362    have heq := h w w' hw hw' hr hc
363    apply hne
364    funext x y
365    have := hdiff x y
366    rw [heq, sub_self] at this
367    exact this.symm
368  · intro hG w w' hw hw' hr hc
369    have hzero := acyclic_margins_zero hG (fun x y => (w x y : ℤ) - w' x y)
370      (by
371        intro x y hxy
372        have h1 := hw x y; have h2 := hw' x y
373        have : cap x y = 0 := by omega
374        show ((w x y : ℕ) : ℤ) - ((w' x y : ℕ) : ℤ) = 0
375        omega)
376      (by
377        intro x
378        simp only [rowM, Finset.sum_sub_distrib]
379        exact_mod_cast sub_eq_zero.2 (congrArg (Nat.cast (R := ℤ)) (hr x)))
380      (by
381        intro y
382        simp only [colM, Finset.sum_sub_distrib]
383        exact_mod_cast sub_eq_zero.2 (congrArg (Nat.cast (R := ℤ)) (hc y)))
384    funext x y
385    have := congrFun (congrFun hzero x) y
386    simp only [Pi.zero_apply] at this
387    omega
388
389/-! ### The channel-level statement -/
390
391section Channel
392
393variable {ι : Type*} [Fintype ι] (γ : ι → R) (γ' : ι → C)
394
395/-- The intersection graph `I(γ, γ')`: an edge between a block of `γ` and a block of `γ'` when
396they intersect. -/
397def interGraph : SimpleGraph (R ⊕ C) := bip fun a b => ∃ i, γ i = a ∧ γ' i = b
398
399/-- `Φ(γ)`: pass count per block of `γ`. -/
400def rowCount (r : ι → Bool) (a : R) : ℕ := (univ.filter fun i => γ i = a ∧ r i = true).card
401
402/-- `Φ(γ')`: pass count per block of `γ'`. -/
403def colCount (r : ι → Bool) (b : C) : ℕ := (univ.filter fun i => γ' i = b ∧ r i = true).card
404
405/-- `Φ(γ ∨ γ')`: pass count per cell of the common refinement. -/
406def cellCount (r : ι → Bool) (a : R) (b : C) : ℕ :=
407  (univ.filter fun i => γ i = a ∧ γ' i = b ∧ r i = true).card
408
409/-- Cell sizes, the capacities of the box. -/
410def cellSize (a : R) (b : C) : ℕ := (univ.filter fun i => γ i = a ∧ γ' i = b).card
411
412lemma rowCount_eq_sum (r : ι → Bool) (a : R) : rowCount γ r a = ∑ b, cellCount γ γ' r a b := by
413  classical
414  unfold rowCount cellCount
415  rw [card_eq_sum_card_fiberwise (f := γ') (t := univ) (by intro i _; simp)]
416  refine Finset.sum_congr rfl fun b _ => ?_
417  congr 1
418  ext i
419  simp only [mem_filter, mem_univ, true_and]
420  tauto
421
422lemma colCount_eq_sum (r : ι → Bool) (b : C) : colCount γ' r b = ∑ a, cellCount γ γ' r a b := by
423  classical
424  unfold colCount cellCount
425  rw [card_eq_sum_card_fiberwise (f := γ) (t := univ) (by intro i _; simp)]
426  refine Finset.sum_congr rfl fun a _ => ?_
427  congr 1
428  ext i
429  simp only [mem_filter, mem_univ, true_and]
430  tauto
431
432lemma cellCount_le (r : ι → Bool) (a : R) (b : C) : cellCount γ γ' r a b ≤ cellSize γ γ' a b := by
433  unfold cellCount cellSize
434  apply card_le_card
435  intro i
436  simp only [mem_filter, mem_univ, true_and]
437  tauto
438
439lemma cellSize_pos_iff (a : R) (b : C) : 0 < cellSize γ γ' a b ↔ ∃ i, γ i = a ∧ γ' i = b := by
440  unfold cellSize
441  rw [card_pos, filter_nonempty_iff]
442  simp
443
444/-- Every cell-count vector in the box is the cell-count vector of some record. -/
445lemma realize (w : R → C → ℕ) (hw : ∀ a b, w a b ≤ cellSize γ γ' a b) :
446    ∃ r : ι → Bool, cellCount γ γ' r = w := by
447  classical
448  have := fun a b => exists_subset_card_eq (hw a b)
449  choose t ht hcard using this
450  refine ⟨fun i => decide (i ∈ t (γ i) (γ' i)), ?_⟩
451  funext a b
452  unfold cellCount
453  rw [← hcard a b]
454  congr 1
455  ext i
456  simp only [mem_filter, mem_univ, true_and, decide_eq_true_eq]
457  constructor
458  · rintro ⟨rfl, rfl, h⟩; exact h
459  · intro h
460    have := ht a b h
461    simp only [cellSize, mem_filter, mem_univ, true_and] at this
462    obtain ⟨rfl, rfl⟩ := this
463    exact ⟨rfl, rfl, h⟩
464
465/-- Cell counts always determine the two margins: `Φ(γ ∨ γ')` is at least as fine as
466`Φ(γ) ∨ Φ(γ')`. -/
467theorem cells_determine_margins (r s : ι → Bool) (h : cellCount γ γ' r = cellCount γ γ' s) :
468    rowCount γ r = rowCount γ s ∧ colCount γ' r = colCount γ' s := by
469  refine ⟨funext fun a => ?_, funext fun b => ?_⟩
470  · rw [rowCount_eq_sum γ γ', rowCount_eq_sum γ γ', h]
471  · rw [colCount_eq_sum γ γ', colCount_eq_sum γ γ', h]
472
473/-- **Proposition 2c.** Observing the two category leaks separately identifies exactly the same
474records as observing the leak of the common refinement — for all records `r, s`, equal row and
475column counts iff equal cell counts — iff the intersection graph `I(γ, γ')` is acyclic. -/
476theorem prop2c :
477    (∀ r s : ι → Bool, (rowCount γ r = rowCount γ s ∧ colCount γ' r = colCount γ' s) ↔
478      cellCount γ γ' r = cellCount γ γ' s) ↔ (interGraph γ γ').IsAcyclic := by
479  have hgraph : interGraph γ γ' = bip fun a b => 0 < cellSize γ γ' a b :=
480    bip_congr fun a b => (cellSize_pos_iff γ γ' a b).symm
481  rw [hgraph, ← margins_determine_iff_acyclic]
482  constructor
483  · intro h w w' hw hw' hr hc
484    obtain ⟨r, rfl⟩ := realize γ γ' w hw
485    obtain ⟨s, rfl⟩ := realize γ γ' w' hw'
486    apply (h r s).1
487    refine ⟨funext fun a => ?_, funext fun b => ?_⟩
488    · rw [rowCount_eq_sum γ γ', rowCount_eq_sum γ γ', hr a]
489    · rw [colCount_eq_sum γ γ', colCount_eq_sum γ γ', hc b]
490  · intro h r s
491    refine ⟨fun hm => ?_, cells_determine_margins γ γ' r s⟩
492    apply h _ _ (cellCount_le γ γ' r) (cellCount_le γ γ' s)
493    · intro a
494      rw [← rowCount_eq_sum γ γ', ← rowCount_eq_sum γ γ', hm.1]
495    · intro b
496      rw [← colCount_eq_sum γ γ', ← colCount_eq_sum γ γ', hm.2]
497
498/-- **Proposition 2c in lattice form.** As partitions of the records (Mathlib's `Setoid` order,
499in which the note's join `∨`, the common refinement, is `⊓`):
500`ker Φ(γ) ⊓ ker Φ(γ') = ker Φ(γ ∨ γ')` iff `I(γ, γ')` is acyclic. -/
501theorem prop2c_setoid :
502    Setoid.ker (rowCount γ) ⊓ Setoid.ker (colCount γ') = Setoid.ker (cellCount γ γ') ↔
503      (interGraph γ γ').IsAcyclic := by
504  rw [← prop2c γ γ', Setoid.ext_iff]
505  simp only [Setoid.inf_iff_and, Setoid.ker_def]
506
507end Channel
508
509/-- The note's witness: rows `{0,1}|{2,3}`, columns `{0,2}|{1,3}` (a 4-cycle); the records `1001`
510and `0110` have equal row and column counts and different cell counts. -/
511theorem prop2c_four_cycle_witness :
512    let γ : Fin 4 → Fin 2 := ![0, 0, 1, 1]
513    let γ' : Fin 4 → Fin 2 := ![0, 1, 0, 1]
514    let r : Fin 4 → Bool := ![true, false, false, true]
515    let s : Fin 4 → Bool := ![false, true, true, false]
516    rowCount γ r = rowCount γ s ∧ colCount γ' r = colCount γ' s ∧
517      cellCount γ γ' r ≠ cellCount γ γ' s ∧ ¬ (interGraph γ γ').IsAcyclic := by
518  intro γ γ' r s
519  have hr : rowCount γ r = rowCount γ s := by funext a; fin_cases a <;> decide
520  have hc : colCount γ' r = colCount γ' s := by funext b; fin_cases b <;> decide
521  have hne : cellCount γ γ' r ≠ cellCount γ γ' s := by
522    intro h
523    have := congrFun (congrFun h 0) 0
524    revert this; decide
525  refine ⟨hr, hc, hne, fun hG => hne ?_⟩
526  exact ((prop2c γ γ').2 hG r s).1 ⟨hr, hc⟩
527
528end FeedbackChannel.LatticeJoin