{"unit":"L-meta-33679","title":"Largest anchored one-hot rank with a nonlinear fan-in-two budget for each of two successive layers","level":null,"weight":100,"state":"open","file":"lean/PEqualNP/Outside/L_meta_33679.lean","module":"PEqualNP.Outside.L_meta_33679","imports":["PEqualNP.Defs"],"statement_sha256":"c84c11d9880b1d446f0ac95873ebd36e68046126cfca984fb944d055cbe0cf8c","proved":false,"lean":"import Mathlib\nimport PEqualNP.Defs\n\nnamespace PEqualNP.Outside.L_meta_33679\n\nopen PEqualNP.CircuitLowerBounds PEqualNP.MetaComplexity.NestedCover PEqualNP.Represents\nopen scoped BigOperators\n\nabbrev F := ZMod 2\n\ndef b2z (b : Bool) : F := if b then 1 else 0\n\nlemma f_cases (x : F) : x = 0 ∨ x = 1 := by\n  match x with\n  | ⟨0, _⟩ => exact Or.inl rfl\n  | ⟨1, _⟩ => exact Or.inr rfl\n\nlemma f_add_self (x : F) : x + x = 0 := by\n  rcases f_cases x with rfl | rfl <;> decide\n\ndef nonlinear (g : Gate) : Bool :=\n  (g.inputs.length == 2) &&\n    xor (xor (g.op.eval [false, false]) (g.op.eval [false, true]))\n        (xor (g.op.eval [true, false]) (g.op.eval [true, true]))\n\ndef depthList (w : ℕ) (gs : List Gate) : List ℕ :=\n  gs.foldl (fun (ds : List ℕ) g =>\n    ds ++ [(g.inputs.map (fun h => ds.getD h 0)).foldr max 0 + (nonlinear g).toNat])\n    (List.replicate w 0)\n\ndef wireList (gs : List Gate) (x : List Bool) : List Bool :=\n  gs.foldl (fun w g => w ++ [g.op.eval (g.inputs.map fun i => w.getD i false)]) x\n\nlemma gate_anf_one (op : GateOp) (x : Bool) :\n    b2z (op.eval [x]) = b2z (op.eval [false]) + (b2z (op.eval [false]) + b2z (op.eval [true])) * b2z x := by\n  cases x\n  · simp [b2z]\n  · simp only [b2z, ite_true, mul_one, ← add_assoc, f_add_self, zero_add]\n\nlemma gate_anf_two_aux (f00 f01 f10 f11 x y : Bool) :\n    b2z (match x, y with\n      | false, false => f00\n      | false, true => f01\n      | true, false => f10\n      | true, true => f11) =\n    b2z f00 + (b2z f00 + b2z f10) * b2z x + (b2z f00 + b2z f01) * b2z y +\n      b2z (xor (xor f00 f01) (xor f10 f11)) * (b2z x * b2z y) := by\n  cases x <;> cases y <;> cases f00 <;> cases f01 <;> cases f10 <;> cases f11 <;> decide\n\nlemma gate_anf_two (op : GateOp) (x y : Bool) :\n    b2z (op.eval [x, y]) =\n      b2z (op.eval [false, false]) +\n      (b2z (op.eval [false, false]) + b2z (op.eval [true, false])) * b2z x +\n      (b2z (op.eval [false, false]) + b2z (op.eval [false, true])) * b2z y +\n      b2z (xor (xor (op.eval [false, false]) (op.eval [false, true]))\n               (xor (op.eval [true, false]) (op.eval [true, true]))) * (b2z x * b2z y) := by\n  have h := gate_anf_two_aux (op.eval [false, false]) (op.eval [false, true])\n    (op.eval [true, false]) (op.eval [true, true]) x y\n  cases x <;> cases y <;> exact h\n\nlemma list_getD_append_left {α : Type*} (l r : List α) (i : ℕ) (d : α) (h : i < l.length) :\n    (l ++ r).getD i d = l.getD i d := by\n  simp only [List.getD_eq_getElem?_getD, List.getElem?_append_left h]\n\nlemma list_getD_append_right_singleton {α : Type*} (l : List α) (a d : α) :\n    (l ++ [a]).getD l.length d = a := by\n  simp [List.getD_eq_getElem?_getD]\n\ndef track {α : Type*} (step : List α → Gate → α) (xs : List α) (gs : List Gate) :=\n  gs.foldl (fun l g => l ++ [step l g]) xs\n\nlemma track_len {α : Type*} (step : List α → Gate → α) (xs : List α) (gs : List Gate) :\n    (track step xs gs).length = xs.length + gs.length := by\n  induction gs generalizing xs with\n  | nil => simp [track]\n  | cons g gs ih =>\n    change (track step (xs ++ [step xs g]) gs).length = _\n    rw [ih]\n    simp [List.length_append, Nat.add_assoc, Nat.add_comm, Nat.add_left_comm]\n\nlemma track_prefix {α : Type*} (step : List α → Gate → α)\n    (xs : List α) (gs : List Gate) (i : ℕ) (d : α) (h : i < xs.length) :\n    (track step xs gs).getD i d = xs.getD i d := by\n  induction gs generalizing xs with\n  | nil => rfl\n  | cons g gs ih =>\n    change (track step (xs ++ [step xs g]) gs).getD i d = _\n    rw [ih _ (by simp; omega), list_getD_append_left _ _ _ _ h]\n\nlemma track_gate {α : Type*} (step : List α → Gate → α)\n    (xs : List α) (Q : Circuit) (d : α) (hrb : ReadsBack xs.length Q)\n    (stable : ∀ l l' g, (∀ i ∈ g.inputs, l.getD i d = l'.getD i d) → step l g = step l' g)\n    (j : ℕ) (hj : j < Q.gates.length) :\n    (track step xs Q.gates).getD (xs.length+j) d = step (track step xs Q.gates) Q.gates[j] := by\n  let pre := track step xs (Q.gates.take j)\n  have hl : pre.length = xs.length+j := by simp [pre, track_len, List.length_take_of_le hj.le]\n  have hs : Q.gates = Q.gates.take j ++ (Q.gates[j] :: Q.gates.drop (j+1)) := by\n    conv_rhs => rw [← List.drop_eq_getElem_cons hj, List.take_append_drop]\n  have ht : track step xs Q.gates = track step (pre ++ [step pre Q.gates[j]]) (Q.gates.drop (j+1)) := by\n    conv_lhs => rw [hs]\n    simp only [track, pre, List.foldl_append, List.foldl_cons]\n  rw [ht, ← hl]\n  rw [track_prefix step (pre ++ [step pre Q.gates[j]]) _ pre.length d (by simp),\n    list_getD_append_right_singleton]\n  apply stable\n  intro i hi\n  have hb := hrb j hj i\n  simp only [gateAt, List.getD_eq_getElem?_getD, List.getElem?_eq_getElem hj, Option.getD_some] at hb\n  have hlt := hb hi\n  rw [track_prefix step (pre ++ [step pre Q.gates[j]]) _ i d (by simp; omega),\n    list_getD_append_left _ _ _ _ (by omega)]\n\nlemma depthList_length (w : ℕ) (gs : List Gate) : (depthList w gs).length = w + gs.length := by\n  simpa [depthList, track] using track_len\n    (fun (ds : List ℕ) g => (g.inputs.map (fun i => ds.getD i 0)).foldr max 0 + (nonlinear g).toNat)\n    (List.replicate w 0) gs\n\nlemma wireList_length (gs : List Gate) (x : List Bool) : (wireList gs x).length = x.length + gs.length :=\n  track_len _ _ _\n\nlemma depthList_input (w : ℕ) (gs : List Gate) (k : ℕ) (hk : k < w) :\n    (depthList w gs).getD k 0 = 0 := by\n  let step := fun (ds : List ℕ) (g : Gate) =>\n    (g.inputs.map (fun i => ds.getD i 0)).foldr max 0 + (nonlinear g).toNat\n  change (track step (List.replicate w 0) gs).getD k 0 = 0\n  rw [track_prefix _ _ _ _ _ (by simpa using hk)]\n  simp [List.getD_eq_getElem?_getD, hk]\n\nlemma wireList_input (gs : List Gate) (x : List Bool) (k : ℕ) (hk : k < x.length) :\n    (wireList gs x).getD k false = x.getD k false := track_prefix _ _ _ _ _ hk\n\nlemma depthList_gate (w : ℕ) (Q : Circuit) (hrb : ReadsBack w Q) (j : ℕ) (hj : j < Q.gates.length) :\n    (depthList w Q.gates).getD (w+j) 0 =\n      (Q.gates[j].inputs.map (fun i => (depthList w Q.gates).getD i 0)).foldr max 0 +\n        (nonlinear Q.gates[j]).toNat := by\n  have h := track_gate\n    (fun (ds : List ℕ) g => (g.inputs.map (fun i => ds.getD i 0)).foldr max 0 + (nonlinear g).toNat)\n    (List.replicate w 0) Q 0 (by simpa using hrb)\n    (fun l r g e => by rw [List.map_congr_left e]) j hj\n  simpa only [List.length_replicate, track, depthList] using h\n\nlemma wireList_gate (w : ℕ) (Q : Circuit) (hrb : ReadsBack w Q) (x : List Bool) (hx : x.length = w)\n    (j : ℕ) (hj : j < Q.gates.length) :\n    (wireList Q.gates x).getD (w+j) false =\n      Q.gates[j].op.eval (Q.gates[j].inputs.map (fun i => (wireList Q.gates x).getD i false)) := by\n  subst w\n  exact track_gate _ _ _ _ hrb (fun l r g e => by rw [List.map_congr_left e]) j hj\n\nlemma oneEach_length {a b : ℕ} (u : Fin a) (v : Fin b) : (oneEach u v).length = a + b := by\n  simp [oneEach]\n\nlemma oneEach_getD_left {a b : ℕ} (u : Fin a) (v : Fin b) (k : ℕ) (hk : k < a) :\n    (oneEach u v).getD k false = decide (k = u.val) := by\n  unfold oneEach\n  rw [list_getD_append_left _ _ k false (by simp [hk])]\n  simp [List.getD_eq_getElem?_getD, hk, Fin.ext_iff]\n\nlemma oneEach_getD_right {a b : ℕ} (u : Fin a) (v : Fin b) (k : ℕ) (hk1 : a ≤ k) (hk2 : k < a + b) :\n    (oneEach u v).getD k false = decide (k - a = v.val) := by\n  unfold oneEach\n  have hlen : (List.ofFn (fun i : Fin a => decide (i = u))).length = a := by simp\n  have hge : (List.ofFn (fun i : Fin a => decide (i = u))).length ≤ k := by rw [hlen]; exact hk1\n  rw [List.getD_eq_getElem?_getD, List.getElem?_append_right hge, hlen]\n  have hlt : k - a < b := by omega\n  simp [hlt, Fin.ext_iff]\n\nlemma foldr_max_le_iff (l : List ℕ) (bound : ℕ) :\n    l.foldr max 0 ≤ bound ↔ ∀ x ∈ l, x ≤ bound := by\n  induction l with\n  | nil => simp\n  | cons a l ih =>\n    simp only [List.foldr_cons, List.mem_cons, max_le_iff, ih]\n    constructor\n    · rintro ⟨ha, hl⟩ x (rfl | hx)\n      · exact ha\n      · exact hl x hx\n    · intro h\n      exact ⟨h a (Or.inl rfl), fun x hx => h x (Or.inr hx)⟩\n\ndef wireFn {α β : Type*} (gs : List Gate) (xs : α → β → List Bool) (i : ℕ) :\n    α → β → F := fun a b => b2z ((wireList gs (xs a b)).getD i false)\n\nlemma member_depth_bound {w : ℕ} {gs : List Gate}\n    (h : ∀ d ∈ depthList w gs, d ≤ 1) (i : ℕ) :\n    (depthList w gs).getD i 0 ≤ 1 := by\n  by_cases hi : i < (depthList w gs).length\n  · simpa [List.getD_eq_getElem?_getD, List.getElem?_eq_getElem hi] using\n      h (depthList w gs)[i] (List.getElem_mem hi)\n  · simp [List.getD_eq_getElem?_getD, List.getElem?_eq_none (by omega : (depthList w gs).length ≤ i)]\n\nsection Layer\n\nvariable {α β : Type*}\n\ndef gateFn (g : Gate) (raw : ℕ → α → β → Bool) :\n    α → β → F :=\n  fun a b => b2z (g.op.eval (g.inputs.map fun i => raw i a b))\n\nlemma gate_affine_mem (M : Submodule F (α → β → F)) (h1 : (1 : α → β → F) ∈ M)\n    (g : Gate) (raw : ℕ → α → β → Bool)\n    (hm : ∀ i ∈ g.inputs, (fun a b => b2z (raw i a b)) ∈ M)\n    (hlen : g.inputs.length ≤ 2) (hlin : nonlinear g = false) :\n    gateFn g raw ∈ M := by\n  cases h : g.inputs with\n  | nil =>\n    have he : gateFn g raw =\n        b2z (g.op.eval []) • (1 : α → β → F) := by\n      ext a b; simp [gateFn, h]\n    rw [he]\n    exact M.smul_mem _ h1\n  | cons i tl =>\n    have hi := hm i (by simp [h])\n    cases tl with\n    | nil =>\n      have he : gateFn g raw =\n          b2z (g.op.eval [false]) • (1 : α → β → F) +\n            (b2z (g.op.eval [false]) + b2z (g.op.eval [true])) •\n              (fun a b => b2z (raw i a b)) := by\n        ext a b\n        simpa [gateFn, h] using gate_anf_one g.op (raw i a b)\n      rw [he]\n      exact M.add_mem (M.smul_mem _ h1) (M.smul_mem _ hi)\n    | cons j tl =>\n      cases tl with\n      | cons k tl => simp [h] at hlen\n      | nil =>\n        have hj := hm j (by simp [h])\n        have hp : xor (xor (g.op.eval [false, false]) (g.op.eval [false, true]))\n            (xor (g.op.eval [true, false]) (g.op.eval [true, true])) = false := by\n          simpa [nonlinear, h] using hlin\n        have he : gateFn g raw =\n            b2z (g.op.eval [false, false]) • (1 : α → β → F) +\n              (b2z (g.op.eval [false, false]) + b2z (g.op.eval [true, false])) •\n                (fun a b => b2z (raw i a b)) +\n              (b2z (g.op.eval [false, false]) + b2z (g.op.eval [false, true])) •\n                (fun a b => b2z (raw j a b)) := by\n          ext a b\n          simpa [gateFn, h, hp, show b2z false = 0 from rfl, show b2z true = 1 from rfl] using gate_anf_two g.op (raw i a b) (raw j a b)\n        rw [he]\n        exact M.add_mem (M.add_mem (M.smul_mem _ h1) (M.smul_mem _ hi)) (M.smul_mem _ hj)\n\nlemma gate_quadratic_mem (M : Submodule F (α → β → F)) (h1 : (1 : α → β → F) ∈ M)\n    (g : Gate) (raw : ℕ → α → β → Bool) {i j : ℕ} (h : g.inputs = [i, j])\n    (hm : ∀ t ∈ g.inputs, (fun a b => b2z (raw t a b)) ∈ M)\n    (hn : nonlinear g = true) :\n    ∃ z ∈ M, gateFn g raw =\n      z + (fun a b => b2z (raw i a b)) * (fun a b => b2z (raw j a b)) := by\n  have hi := hm i (by simp [h])\n  have hj := hm j (by simp [h])\n  have hp : xor (xor (g.op.eval [false, false]) (g.op.eval [false, true]))\n      (xor (g.op.eval [true, false]) (g.op.eval [true, true])) = true := by\n    simpa [nonlinear, h] using hn\n  refine ⟨b2z (g.op.eval [false, false]) • 1 +\n    (b2z (g.op.eval [false, false]) + b2z (g.op.eval [true, false])) •\n      (fun a b => b2z (raw i a b)) +\n    (b2z (g.op.eval [false, false]) + b2z (g.op.eval [false, true])) •\n      (fun a b => b2z (raw j a b)),\n    M.add_mem (M.add_mem (M.smul_mem _ h1) (M.smul_mem _ hi)) (M.smul_mem _ hj), ?_⟩\n  ext a b\n  simpa [gateFn, h, hp, show b2z false = 0 from rfl, show b2z true = 1 from rfl] using gate_anf_two g.op (raw i a b) (raw j a b)\n\nlemma nonlinear_length {g : Gate} (hn : nonlinear g = true) : g.inputs.length = 2 := by\n  exact (Bool.and_eq_true_iff.mp hn).1 |> beq_iff_eq.mp\n\nlemma wireFn_at_gate (w : ℕ) (Q : Circuit) (hrb : ReadsBack w Q)\n    (xs : α → β → List Bool) (hx : ∀ a b, (xs a b).length = w) (j : Fin Q.gates.length) :\n    wireFn Q.gates xs (w + j.val) =\n      gateFn Q.gates[j] (fun i a b => (wireList Q.gates (xs a b)).getD i false) := by\n  ext a b\n  simp only [wireFn, gateFn, wireList_gate w Q hrb _ (hx a b) j.val j.isLt]\n  rfl\n\nlemma readsBack_lt (w : ℕ) (Q : Circuit) (hrb : ReadsBack w Q) (j : Fin Q.gates.length)\n    (i : ℕ) (hi : i ∈ Q.gates[j].inputs) : i < w + j.val := by\n  apply hrb j.val j.isLt i\n  simpa [gateAt, List.getD_eq_getElem?_getD, List.getElem?_eq_getElem j.isLt] using hi\n\nlemma input_depth (w : ℕ) (Q : Circuit) (hrb : ReadsBack w Q) (j : Fin Q.gates.length)\n    (i : ℕ) (hi : i ∈ Q.gates[j].inputs) :\n    (depthList w Q.gates).getD i 0 + (nonlinear Q.gates[j]).toNat ≤\n      (depthList w Q.gates).getD (w + j.val) 0 := by\n  rw [depthList_gate w Q hrb j.val j.isLt]\n  apply Nat.add_le_add_right\n  exact (foldr_max_le_iff _ _).mp le_rfl _ (List.mem_map.mpr ⟨i, hi, rfl⟩)\n\nlemma affine_wires (M : Submodule F (α → β → F)) (h1 : (1 : α → β → F) ∈ M)\n    (w : ℕ) (Q : Circuit) (hrb : ReadsBack w Q)\n    (hfan : ∀ g ∈ Q.gates, g.inputs.length ≤ 2)\n    (xs : α → β → List Bool) (hx : ∀ a b, (xs a b).length = w)\n    (hinit : ∀ i < w, wireFn [] xs i ∈ M)\n    (i : ℕ) (hi : i < w + Q.gates.length) (hd : (depthList w Q.gates).getD i 0 = 0) :\n    wireFn Q.gates xs i ∈ M := by\n  induction i using Nat.strong_induction_on with\n  | ind i ih =>\n    by_cases hw : i < w\n    · have he : wireFn Q.gates xs i = wireFn [] xs i := by\n        ext a b\n        unfold wireFn\n        rw [wireList_input _ _ _ (hx a b ▸ hw)]\n        rfl\n      rw [he]\n      exact hinit i hw\n    · let j : Fin Q.gates.length := ⟨i-w, by omega⟩\n      have he : i = w + j.val := by dsimp [j]; omega\n      rw [he, wireFn_at_gate w Q hrb xs hx]\n      have hn : nonlinear Q.gates[j] = false := by\n        have hgate := depthList_gate w Q hrb j.val j.isLt\n        rw [← he, hd] at hgate\n        change 0 = _ + (nonlinear Q.gates[j]).toNat at hgate\n        cases hn : nonlinear Q.gates[j]\n        · rfl\n        · rw [hn, Bool.toNat_true] at hgate\n          omega\n      apply gate_affine_mem M h1\n      · intro t ht\n        have ht' := readsBack_lt w Q hrb j t ht\n        have ht'' := input_depth w Q hrb j t ht\n        rw [← he, hd] at ht''\n        exact ih t (by omega) (by omega) (by omega)\n      · exact hfan _ (List.getElem_mem j.isLt)\n      · exact hn\n\nlemma layer_products (M : Submodule F (α → β → F)) (h1 : (1 : α → β → F) ∈ M)\n    (w : ℕ) (Q : Circuit) (hrb : ReadsBack w Q)\n    (hfan : ∀ g ∈ Q.gates, g.inputs.length ≤ 2)\n    (xs : α → β → List Bool) (hx : ∀ a b, (xs a b).length = w)\n    (hinit : ∀ i < w, wireFn [] xs i ∈ M)\n    (hdepth : ∀ d ∈ depthList w Q.gates, d ≤ 1) :\n    let J := {j : Fin Q.gates.length // nonlinear Q.gates[j] = true}\n    ∃ U V : J → α → β → F,\n      (∀ j, U j ∈ M ∧ V j ∈ M) ∧\n      ∀ i, wireFn Q.gates xs i ∈ M ⊔ Submodule.span F (Set.range fun j => U j * V j) := by\n  classical\n  intro J\n  have hprod : ∀ j : J, ∃ u v : α → β → F,\n      u ∈ M ∧ v ∈ M ∧ ∃ z ∈ M, wireFn Q.gates xs (w + j.val.val) = z + u * v := by\n    intro j\n    obtain ⟨i, l, hil⟩ := List.length_eq_two.mp (nonlinear_length j.property)\n    have hinput : ∀ t ∈ Q.gates[j.val].inputs, wireFn Q.gates xs t ∈ M := by\n      intro t ht\n      have hv := input_depth w Q hrb j.val t ht\n      have hb := member_depth_bound hdepth (w + j.val.val)\n      have hn : nonlinear Q.gates[j.val] = true := j.property\n      rw [hn, Bool.toNat_true] at hv\n      have hlt := readsBack_lt w Q hrb j.val t ht\n      exact affine_wires M h1 w Q hrb hfan xs hx hinit t (by omega) (by omega)\n    refine ⟨wireFn Q.gates xs i, wireFn Q.gates xs l,\n      hinput i (by rw [hil]; simp), hinput l (by rw [hil]; simp), ?_⟩\n    rw [wireFn_at_gate w Q hrb xs hx]\n    exact gate_quadratic_mem M h1 _ _ hil hinput j.property\n  choose U V hU hV z hz he using hprod\n  let M' := M ⊔ Submodule.span F (Set.range fun j => U j * V j)\n  refine ⟨U, V, fun j => ⟨hU j, hV j⟩, ?_⟩\n  intro i\n  change wireFn Q.gates xs i ∈ M'\n  induction i using Nat.strong_induction_on with\n  | ind i ih =>\n    by_cases hbig : w + Q.gates.length ≤ i\n    · have hzero : wireFn Q.gates xs i = 0 := by\n        ext a b\n        have hlen : (wireList Q.gates (xs a b)).length ≤ i := by\n          rwa [wireList_length, hx]\n        simp [wireFn, b2z, List.getD_eq_getElem?_getD, List.getElem?_eq_none hlen]\n      rw [hzero]\n      exact M'.zero_mem\n    · by_cases hw : i < w\n      · exact Submodule.mem_sup_left\n          (affine_wires M h1 w Q hrb hfan xs hx hinit i (by omega) (depthList_input _ _ _ hw))\n      · let j : Fin Q.gates.length := ⟨i-w, by omega⟩\n        have hij : i = w + j.val := by dsimp [j]; omega\n        rw [hij]\n        by_cases hn : nonlinear Q.gates[j] = true\n        · exact he (⟨j, hn⟩ : J) ▸ Submodule.add_mem_sup (hz (⟨j, hn⟩ : J))\n            (Submodule.subset_span ⟨(⟨j, hn⟩ : J), rfl⟩)\n        · rw [wireFn_at_gate w Q hrb xs hx]\n          apply gate_affine_mem M' (Submodule.mem_sup_left h1)\n          · intro t ht\n            exact ih t ((readsBack_lt w Q hrb j t ht).trans_eq hij.symm)\n          · exact hfan _ (List.getElem_mem j.isLt)\n          · exact Bool.eq_false_iff.mpr hn\n\nend Layer\n\nsection Algebra\n\nopen Finset\n\nvariable {α β : Type*}\n\ndef sep (α β : Type*) : Submodule F (α → β → F) where\n  carrier := {f | ∃ r : α → F, ∃ c : β → F, ∀ a b, f a b = r a + c b}\n  zero_mem' := ⟨0, 0, by simp⟩\n  add_mem' := by\n    rintro f g ⟨r, c, hf⟩ ⟨s, d, hg⟩\n    refine ⟨r+s, c+d, fun a b => ?_⟩\n    simp only [Pi.add_apply, hf, hg]\n    abel\n  smul_mem' := by\n    rintro t f ⟨r, c, hf⟩\n    exact ⟨t • r, t • c, fun a b => by\n      simp only [Pi.smul_apply, hf, smul_eq_mul, mul_add]⟩\n\nlemma sep_one : (1 : α → β → F) ∈ sep α β := ⟨1, 0, by simp⟩\n\ndef rect (r : α → F) (c : β → F) : α → β → F := fun a b => r a * c b\n\ndef anchor (a₀ : α) (b₀ : β) : (α → β → F) →ₗ[F] Matrix α β F where\n  toFun f a b := f a b + f a b₀ + f a₀ b + f a₀ b₀\n  map_add' f g := by\n    ext a b\n    change (_ : F) = f a b + f a b₀ + f a₀ b + f a₀ b₀ + (g a b + g a b₀ + g a₀ b + g a₀ b₀)\n    simp only [Pi.add_apply]\n    abel\n  map_smul' t f := by\n    ext a b\n    change (_ : F) = t * (f a b + f a b₀ + f a₀ b + f a₀ b₀)\n    simp only [Pi.smul_apply, smul_eq_mul, mul_add]\n\nlemma anchor_sep (a₀ : α) (b₀ : β) {f : α → β → F} (h : f ∈ sep α β) :\n    anchor a₀ b₀ f = 0 := by\n  obtain ⟨r, c, h⟩ := h\n  ext a b\n  change f a b + f a b₀ + f a₀ b + f a₀ b₀ = 0\n  simp only [h]\n  linear_combination f_add_self (r a) + f_add_self (r a₀) +\n    f_add_self (c b) + f_add_self (c b₀)\n\nlemma anchor_rect (a₀ : α) (b₀ : β) (r : α → F) (c : β → F) :\n    anchor a₀ b₀ (rect r c) =\n      (Matrix.of fun a (_ : Fin 1) => r a + r a₀) *\n        (Matrix.of fun (_ : Fin 1) b => c b + c b₀) := by\n  ext a b\n  change r a * c b + r a * c b₀ + r a₀ * c b + r a₀ * c b₀ = _\n  simp only [Matrix.mul_apply, Matrix.of_apply,\n    Fin.sum_univ_one]\n  ring\n\nlemma rank_rect [Fintype α] [Fintype β] (a₀ : α) (b₀ : β) (r : α → F) (c : β → F) :\n    (anchor a₀ b₀ (rect r c)).rank ≤ 1 := by\n  rw [anchor_rect]\n  apply (Matrix.rank_mul_le_left _ _).trans\n  have h := Matrix.rank_le_card_width (Matrix.of fun (a : α) (_ : Fin 1) => r a + r a₀)\n  rw [Fintype.card_fin] at h\n  exact h\n\nlemma rank_add_le [Fintype β] (A B : Matrix α β F) :\n    (A + B).rank ≤ A.rank + B.rank := by\n  classical\n  have hle : LinearMap.range (A + B).mulVecLin ≤\n      LinearMap.range A.mulVecLin ⊔ LinearMap.range B.mulVecLin := by\n    rintro _ ⟨v, rfl⟩\n    rw [Matrix.mulVecLin_apply, Matrix.add_mulVec]\n    exact Submodule.add_mem_sup ⟨v, rfl⟩ ⟨v, rfl⟩\n  have h1 := Submodule.finrank_mono hle\n  have h2 := Submodule.finrank_sup_add_finrank_inf_eq\n    (LinearMap.range A.mulVecLin) (LinearMap.range B.mulVecLin)\n  change Module.finrank F (LinearMap.range (A + B).mulVecLin) ≤\n    Module.finrank F (LinearMap.range A.mulVecLin) + Module.finrank F (LinearMap.range B.mulVecLin)\n  omega\n\nlemma rank_sum_le [Fintype β] {ι : Type*} (S : Finset ι) (M : ι → Matrix α β F) (d : ℕ)\n    (h : ∀ i ∈ S, (M i).rank ≤ d) : (∑ i ∈ S, M i).rank ≤ d * S.card := by\n  classical\n  induction S using Finset.induction_on with\n  | empty => simp\n  | insert a S ha ih =>\n    rw [sum_insert ha, card_insert_of_notMem ha, Nat.mul_succ]\n    have hS := ih (fun i hi => h i (mem_insert_of_mem hi))\n    have ha' := h a (mem_insert_self a S)\n    exact (rank_add_le _ _).trans (by omega)\n\nabbrev Vec (k : ℕ) := Fin k → Bool → F\n\ndef quad {k : ℕ} (x : Vec k) : F := ∑ l, x l false * x l true\n\ndef polar {k : ℕ} (x y : Vec k) : F :=\n  ∑ l, (x l false * y l true + x l true * y l false)\n\nlemma polar_symm {k : ℕ} (x y : Vec k) : polar x y = polar y x := by\n  unfold polar\n  apply sum_congr rfl\n  intro i _\n  ring\n\nlemma polar_self {k : ℕ} (x : Vec k) : polar x x = 0 := by\n  unfold polar\n  simp_rw [mul_comm (x _ true) (x _ false), f_add_self, sum_const_zero]\n\nlemma polar_add {k : ℕ} (x y z : Vec k) :\n    polar (x + y) z = polar x z + polar y z := by\n  unfold polar\n  rw [← sum_add_distrib]\n  exact sum_congr rfl fun i _ => by simp only [Pi.add_apply]; ring\n\nlemma polar_smul {k : ℕ} (t : F) (x y : Vec k) :\n    polar (t • x) y = t * polar x y := by\n  unfold polar\n  rw [mul_sum]\n  exact sum_congr rfl fun i _ => by simp only [Pi.smul_apply, smul_eq_mul]; ring\n\nlemma polar_zero {k : ℕ} (y : Vec k) : polar 0 y = 0 := by\n  simp [polar]\n\nlemma polar_sum {k : ℕ} {ι : Type*} (S : Finset ι) (x : ι → Vec k) (y : Vec k) :\n    polar (∑ i ∈ S, x i) y = ∑ i ∈ S, polar (x i) y := by\n  classical\n  induction S using Finset.induction_on with\n  | empty => simp [polar_zero]\n  | insert i S hi ih => simp [sum_insert hi, polar_add, ih]\n\nlemma quad_add {k : ℕ} (x y : Vec k) : quad (x+y) = quad x + quad y + polar x y := by\n  unfold quad polar\n  simp only [← sum_add_distrib]\n  exact sum_congr rfl fun i _ => by simp only [Pi.add_apply]; ring\n\nlemma quad_smul {k : ℕ} (t : F) (x : Vec k) : quad (t • x) = t * quad x := by\n  rcases f_cases t with rfl | rfl <;> simp [quad]\n\nlemma quad_sum {k : ℕ} {ι : Type*} [LinearOrder ι]\n    (S : Finset ι) (x : ι → Vec k) :\n    quad (∑ i ∈ S, x i) =\n      (∑ i ∈ S, quad (x i)) +\n        ∑ i ∈ S, ∑ j ∈ S.filter (fun j => j < i), polar (x j) (x i) := by\n  induction S using Finset.induction_on_max with\n  | empty => simp [quad]\n  | insert i S hlt ih =>\n    have hi : i ∉ S := fun hm => (hlt i hm).false\n    have hfilter : ∀ j ∈ S, (insert i S).filter (fun t => t < j) = S.filter (fun t => t < j) := by\n      intro j hj\n      ext t\n      simp only [mem_filter, mem_insert]\n      have hji := hlt j hj\n      constructor\n      · rintro ⟨rfl | h, h'⟩\n        · exact (hji.asymm h').elim\n        · exact ⟨h, h'⟩\n      · exact fun h => ⟨Or.inr h.1, h.2⟩\n    have hwhole : (insert i S).filter (fun t => t < i) = S := by\n      ext j\n      simp only [mem_filter, mem_insert]\n      constructor\n      · rintro ⟨rfl | h, h'⟩\n        · exact (lt_irrefl _ h').elim\n        · exact h\n      · exact fun h => ⟨Or.inr h, hlt j h⟩\n    rw [sum_insert hi, quad_add, ih, polar_symm (x i), polar_sum, sum_insert hi,\n      sum_insert hi, hwhole]\n    have hs : (∑ j ∈ S, ∑ t ∈ (insert i S).filter (fun t => t < j), polar (x t) (x j)) =\n        ∑ j ∈ S, ∑ t ∈ S.filter (fun t => t < j), polar (x t) (x j) :=\n      sum_congr rfl fun j hj => by rw [hfilter j hj]\n    rw [hs]\n    abel\n\nend Algebra\n\nlemma countP_finset_card {α : Type*} (l : List α) (p : α → Bool) :\n    ((Finset.univ : Finset (Fin l.length)).filter (fun i => p l[i] = true)).card = l.countP p := by\n  have h := Finset.natCast_card_filter (R := ℕ) (fun i : Fin l.length => p l[i] = true) Finset.univ\n  simp only [Nat.cast_id] at h\n  rw [h]\n  clear h\n  induction l with\n  | nil => simp\n  | cons a l ih =>\n    rw [List.countP_cons]\n    change (∑ i : Fin (l.length+1), if p (a :: l)[i] = true then 1 else 0) = _\n    rw [Fin.sum_univ_succ]\n    change (if p a = true then 1 else 0) + (∑ i : Fin l.length, if p l[i] = true then 1 else 0) = _\n    rw [ih]\n    cases p a <;> simp [Nat.add_comm]\n\nsection NormalForm\n\nopen Finset\n\nvariable {α β : Type*}\n\ndef mixed {ι : Type*} (A : ι → Bool → α → F) (B : ι → Bool → β → F) (i : ι) :\n    α → β → F := rect (A i false) (B i false) + rect (A i true) (B i true)\n\ndef withPrep {ι : Type*} (W : ι → α → β → F) :\n    Submodule F (α → β → F) := sep α β ⊔ Submodule.span F (Set.range W)\n\nlemma withPrep_decomp {ι : Type*} [Fintype ι] {W : ι → α → β → F}\n    {f : α → β → F} (h : f ∈ withPrep W) :\n    ∃ (r : α → F) (c : β → F) (d : ι → F), ∀ a b, f a b = r a + c b + ∑ j, d j * W j a b := by\n  obtain ⟨g, ⟨r,c,hrc⟩, l, hl, rfl⟩ := Submodule.mem_sup.mp h\n  obtain ⟨d, rfl⟩ := (Submodule.mem_span_range_iff_exists_fun F).mp hl\n  exact ⟨r,c,d,fun a b => by simp [hrc, Finset.sum_apply, smul_eq_mul]⟩\n\nlemma split_readsBack (w : ℕ) (Q : Circuit) (hrb : ReadsBack w Q)\n    (cut : ℕ) (hc : cut ≤ Q.gates.length) :\n    ReadsBack w ⟨Q.gates.take cut,0⟩ ∧\n      ReadsBack (w+cut) ⟨Q.gates.drop cut,0⟩ := by\n  constructor\n  · intro j hj i hi\n    have hj' : j < Q.gates.length := by\n      simp only [List.length_take] at hj\n      omega\n    apply hrb j hj' i\n    simpa [gateAt, List.getD_eq_getElem?_getD, List.getElem?_eq_getElem hj,\n      List.getElem?_eq_getElem hj', List.getElem_take] using hi\n  · intro j hj i hi\n    have hj' : cut+j < Q.gates.length := by\n      simp only [List.length_drop] at hj\n      omega\n    have h := hrb (cut+j) hj' i (by\n      simpa [gateAt, List.getD_eq_getElem?_getD, List.getElem?_eq_getElem hj,\n        List.getElem?_eq_getElem hj', List.getElem_drop] using hi)\n    omega\n\nlemma input_sep {n m : ℕ} (i : ℕ) (hi : i < n+m) :\n    wireFn [] (fun a : Fin n => fun b : Fin m => oneEach a b) i ∈ sep (Fin n) (Fin m) := by\n  by_cases h : i < n\n  · exact ⟨fun a => b2z (decide ((⟨i,h⟩ : Fin n) = a)), 0, fun a b => by\n      change b2z ((oneEach a b).getD i false) = _\n      rw [oneEach_getD_left a b i h]\n      simp only [Pi.zero_apply, add_zero, Fin.ext_iff]⟩\n  · exact ⟨0, fun b => b2z (decide ((⟨i-n,by omega⟩ : Fin m) = b)), fun a b => by\n      change b2z ((oneEach a b).getD i false) = _\n      rw [oneEach_getD_right a b i (by omega) hi]\n      simp only [Pi.zero_apply, zero_add, Fin.ext_iff]⟩\n\nlemma card_nonlinear (gs : List Gate) :\n    Fintype.card {j : Fin gs.length // nonlinear gs[j] = true} = gs.countP nonlinear := by\n  rw [Fintype.card_subtype, countP_finset_card]\n\nlemma sum_reindex_zero {ι κ R : Type*} [Fintype ι] [Fintype κ] [AddCommMonoid R]\n    (e : ι ↪ κ) (g : κ → R) (h : ∀ j, j ∉ Set.range e → g j = 0) :\n    ∑ j, g j = ∑ i, g (e i) := by\n  classical\n  rw [← Finset.sum_map]\n  symm\n  exact Finset.sum_subset (Finset.subset_univ _) fun j _ hj => h j (by simpa using hj)\n\nnoncomputable def pad {ι κ R : Type*} [Zero R] (e : ι ↪ κ) (f : ι → R) : κ → R :=\n  Function.extend e f 0\n\nlemma pad_at {ι κ R : Type*} [Zero R] (e : ι ↪ κ) (f : ι → R) (i : ι) :\n    pad e f (e i) = f i := Function.Injective.extend_apply e.injective _ _ _\n\nlemma pad_off {ι κ R : Type*} [Zero R] (e : ι ↪ κ) (f : ι → R) {j : κ}\n    (hj : j ∉ Set.range e) : pad e f j = 0 := by\n  exact Function.extend_apply' _ _ _ hj\n\nlemma prepare_span {n m s : ℕ} (Q : Circuit) (hback : ReadsBack (n+m) Q)\n    (hfan : ∀ g ∈ Q.gates, g.inputs.length ≤ 2)\n    (hd : ∀ d ∈ depthList (n+m) Q.gates, d ≤ 1) (hs : Q.gates.countP nonlinear ≤ s) :\n    ∃ (A : Fin s → Bool → Fin n → F) (B : Fin s → Bool → Fin m → F),\n      ∀ i, wireFn Q.gates (fun a b => oneEach a b) i ∈ withPrep (mixed A B) := by\n  classical\n  obtain ⟨U,V,hUV,hw⟩ := layer_products (sep (Fin n) (Fin m)) sep_one (n+m) Q hback hfan\n    (fun a b => oneEach a b) oneEach_length input_sep hd\n  let J := {j : Fin Q.gates.length // nonlinear Q.gates[j] = true}\n  have hn : Fintype.card J ≤ s := by rw [show Fintype.card J = _ from card_nonlinear Q.gates]; exact hs\n  let e : J ↪ Fin s := (Fintype.equivFin J).toEmbedding.trans (Fin.castLEEmb hn)\n  have hu : ∀ i : J, ∃ (r : Fin n → F) (c : Fin m → F), ∀ a b, U i a b = r a + c b := fun i => (hUV i).1\n  have hv : ∀ i : J, ∃ (r : Fin n → F) (c : Fin m → F), ∀ a b, V i a b = r a + c b := fun i => (hUV i).2\n  choose r c hr using hu\n  choose p q hp using hv\n  let A : Fin s → Bool → Fin n → F := pad e (fun i t => if t then p i else r i)\n  let B : Fin s → Bool → Fin m → F := pad e (fun i t => if t then c i else q i)\n  refine ⟨A,B,fun i => ?_⟩\n  have hspan : Submodule.span F (Set.range fun j => U j * V j) ≤ withPrep (mixed A B) := by\n    apply Submodule.span_le.mpr\n    rintro g ⟨j,rfl⟩\n    have he : U j * V j =\n        (fun a b => r j a * p j a + c j b * q j b) + mixed A B (e j) := by\n      ext a b\n      simp only [Pi.mul_apply, Pi.add_apply, hr, hp, mixed, rect, A, B, pad_at,\n        Bool.false_eq_true, ite_false, ite_true]\n      ring\n    change U j * V j ∈ _\n    rw [he]\n    exact Submodule.add_mem_sup ⟨_,_,fun _ _ => rfl⟩\n      (Submodule.subset_span ⟨e j,rfl⟩)\n  exact sup_le (le_sup_left : sep (Fin n) (Fin m) ≤ withPrep (mixed A B)) hspan (hw i)\n\nstructure Normal (α β : Type*) (s k : ℕ) (f : α → β → F) where\n  A : Fin s → Bool → α → F\n  B : Fin s → Bool → β → F\n  r : α → F\n  c : β → F\n  lin : Fin s → F\n  x : α → Vec k\n  y : β → Vec k\n  mask : Fin s → Vec k\n  eqn : ∀ a b, f a b =\n    r a + c b + (∑ i, lin i * mixed A B i a b) +\n    quad (x a + y b + ∑ i, mixed A B i a b • mask i)\n\nlemma final_normal {n m s k : ℕ} (Q : Circuit) (cut : ℕ) (hc : cut ≤ Q.gates.length)\n    (hback : ReadsBack (n+m) Q) (hfan : ∀ g ∈ Q.gates, g.inputs.length ≤ 2)\n    (hd₀ : ∀ d ∈ depthList (n+m) (Q.gates.take cut), d ≤ 1)\n    (hd₁ : ∀ d ∈ depthList (n+m+cut) (Q.gates.drop cut), d ≤ 1)\n    (hs : (Q.gates.take cut).countP nonlinear ≤ s)\n    (hk : (Q.gates.drop cut).countP nonlinear ≤ k) :\n    Nonempty (Normal (Fin n) (Fin m) s k (wireFn Q.gates (fun a b => oneEach a b) Q.output)) := by\n  classical\n  obtain ⟨hr₀,hr₁⟩ := split_readsBack (n+m) Q hback cut hc\n  let pre := Q.gates.take cut\n  let post := Q.gates.drop cut\n  have hh₀ : ∀ g ∈ pre, g.inputs.length ≤ 2 := fun g hg => hfan g (List.mem_of_mem_take hg)\n  have hh₁ : ∀ g ∈ post, g.inputs.length ≤ 2 := fun g hg => hfan g (List.mem_of_mem_drop hg)\n  obtain ⟨A,B,hp⟩ := prepare_span ⟨pre,0⟩ hr₀ hh₀ hd₀ hs\n  let W := mixed A B\n  let xs : Fin n → Fin m → List Bool := fun a b => wireList pre (oneEach a b)\n  have hx (a : Fin n) (b : Fin m) : (xs a b).length = n+m+cut := by\n    simp [xs, pre, wireList_length, oneEach_length, min_eq_left hc]\n  obtain ⟨U,V,hUV,hw⟩ := layer_products (withPrep W) (Submodule.mem_sup_left sep_one)\n    (n+m+cut) ⟨post,0⟩ hr₁ hh₁ xs hx (fun i _ => hp i) hd₁\n  have hfull : wireFn Q.gates (fun a b => oneEach a b) Q.output = wireFn post xs Q.output := by\n    have hsplit := List.take_append_drop cut Q.gates\n    ext a b\n    change b2z ((wireList Q.gates _).getD _ _) = b2z ((wireList post (wireList pre _)).getD _ _)\n    rw [← hsplit]\n    simp only [pre, post, wireList, List.foldl_append]\n  rw [hfull]\n  obtain ⟨f,hf,g,hg,he⟩ := Submodule.mem_sup.mp (hw Q.output)\n  obtain ⟨r,c,l,hl⟩ := withPrep_decomp hf\n  obtain ⟨d,hd⟩ := (Submodule.mem_span_range_iff_exists_fun F).mp hg\n  let J := {j : Fin post.length // nonlinear post[j] = true}\n  have hj : Fintype.card J ≤ k := by rw [show Fintype.card J = _ from card_nonlinear post]; exact hk\n  let e : J ↪ Fin k := (Fintype.equivFin J).toEmbedding.trans (Fin.castLEEmb hj)\n  have hU : ∀ j : J, ∃ (r : Fin n → F) (c : Fin m → F) (l : Fin s → F), ∀ a b, (d j • U j) a b =\n      r a + c b + ∑ i, l i * W i a b :=\n    fun j => withPrep_decomp ((withPrep W).smul_mem (d j) (hUV j).1)\n  have hV : ∀ j : J, ∃ (r : Fin n → F) (c : Fin m → F) (l : Fin s → F), ∀ a b, V j a b = r a + c b + ∑ i, l i * W i a b :=\n    fun j => withPrep_decomp (hUV j).2\n  choose rU cU lU eU using hU\n  choose rV cV lV eV using hV\n  let X := pad e (fun j t => if t then rV j else rU j)\n  let Y := pad e (fun j t => if t then cV j else cU j)\n  let L := pad e (fun j t => if t then lV j else lU j)\n  refine ⟨⟨A,B,r,c,l,fun a j t => X j t a,fun b j t => Y j t b,\n    fun i j t => L j t i,fun a b => ?_⟩⟩\n  rw [← he, ← hd]\n  simp only [Pi.add_apply, hl, quad, Finset.sum_apply, Pi.smul_apply, smul_eq_mul, Pi.mul_apply]\n  congr 1\n  rw [sum_reindex_zero e _ (fun j hj => by simp [X,Y,L,pad_off _ _ hj])]\n  apply Finset.sum_congr rfl\n  intro j _\n  simp only [X,Y,L,pad_at, Bool.false_eq_true, ite_false, ite_true]\n  simp only [mul_comm (mixed A B _ _ _) (lU _ _), mul_comm (mixed A B _ _ _) (lV _ _)]\n  change d j * _ = (rU j a + cU j b + ∑ i, lU j i * W i a b) * _\n  rw [← eU j a b, ← eV j a b]\n  simp [smul_eq_mul, mul_assoc]\n\nend NormalForm\n\nsection Upper\n\nopen Finset\n\nvariable {α β : Type*} [Fintype α] [Fintype β]\n\nlemma rank_sum_anchor {ι : Type*} (a₀ : α) (b₀ : β) (S : Finset ι)\n    (f : ι → α → β → F) (d : ℕ) (h : ∀ i ∈ S, (anchor a₀ b₀ (f i)).rank ≤ d) :\n    (anchor a₀ b₀ (∑ i ∈ S, f i)).rank ≤ d * S.card := by\n  rw [map_sum]\n  exact rank_sum_le _ _ _ h\n\nlemma rank_single {ι : Type*} (a₀ : α) (b₀ : β)\n    (A : ι → Bool → α → F) (B : ι → Bool → β → F) (i : ι) (r : α → F) (c : β → F) :\n    (anchor a₀ b₀ (fun a b => (r a + c b) * mixed A B i a b)).rank ≤ 4 := by\n  have e : (fun a b => (r a + c b) * mixed A B i a b) =\n      (rect (r * A i false) (B i false) + rect (A i false) (c * B i false)) +\n      (rect (r * A i true) (B i true) + rect (A i true) (c * B i true)) := by\n    funext a b\n    simp only [mixed, rect, Pi.add_apply, Pi.mul_apply]\n    ring\n  rw [e, map_add, map_add, map_add]\n  have p := rank_add_le (anchor a₀ b₀ (rect (r * A i false) (B i false)))\n    (anchor a₀ b₀ (rect (A i false) (c * B i false)))\n  have q := rank_add_le (anchor a₀ b₀ (rect (r * A i true) (B i true)))\n    (anchor a₀ b₀ (rect (A i true) (c * B i true)))\n  have h1 := rank_rect a₀ b₀ (r * A i false) (B i false)\n  have h2 := rank_rect a₀ b₀ (A i false) (c * B i false)\n  have h3 := rank_rect a₀ b₀ (r * A i true) (B i true)\n  have h4 := rank_rect a₀ b₀ (A i true) (c * B i true)\n  exact (rank_add_le _ _).trans (by omega)\n\nlemma rank_pair {ι : Type*} (a₀ : α) (b₀ : β)\n    (A : ι → Bool → α → F) (B : ι → Bool → β → F) (i j : ι) (t : F) :\n    (anchor a₀ b₀ (fun a b => t * mixed A B i a b * mixed A B j a b)).rank ≤ 4 := by\n  have e : (fun a b => t * mixed A B i a b * mixed A B j a b) =\n      ∑ uv : Bool × Bool, rect (t • (A i uv.1 * A j uv.2)) (B i uv.1 * B j uv.2) := by\n    funext a b\n    simp only [Fintype.sum_prod_type, Finset.sum_apply, Fin.sum_univ_two, Fintype.sum_bool,\n      rect, mixed, Pi.add_apply, Pi.mul_apply, Pi.smul_apply, smul_eq_mul]\n    ring\n  rw [e]\n  exact (rank_sum_anchor a₀ b₀ univ _ 1 (fun _ _ => rank_rect _ _ _ _)).trans (by simp)\n\nlemma rank_polar {k : ℕ} (a₀ : α) (b₀ : β) (x : α → Vec k) (y : β → Vec k) :\n    (anchor a₀ b₀ (fun a b => polar (x a) (y b))).rank ≤ 2*k := by\n  have e : (fun a b => polar (x a) (y b)) =\n      ∑ j : Fin k, (rect (fun a => x a j false) (fun b => y b j true) +\n        rect (fun a => x a j true) (fun b => y b j false)) := by\n    funext a b\n    simp [polar, rect, Finset.sum_apply]\n  rw [e]\n  refine (rank_sum_anchor a₀ b₀ univ _ 2 ?_).trans (by simp)\n  intro j _\n  rw [map_add]\n  exact (rank_add_le _ _).trans (Nat.add_le_add (rank_rect _ _ _ _) (rank_rect _ _ _ _))\n\ndef edges {s k : ℕ} (m : Fin s → Vec k) : Finset (Fin s × Fin s) :=\n  univ.filter (fun ij => ij.1 < ij.2 ∧ polar (m ij.1) (m ij.2) ≠ 0)\n\nlemma quad_expansion {s k : ℕ} (m : Fin s → Vec k) (z : Fin s → F) (x y : Vec k) :\n    quad (x + y + ∑ i, z i • m i) =\n    quad x + quad y + polar x y +\n      (∑ i, (quad (m i) + polar x (m i) + polar y (m i)) * z i) +\n      ∑ ij ∈ edges m, polar (m ij.1) (m ij.2) * z ij.1 * z ij.2 := by\n  classical\n  have hscalar (i j : Fin s) : polar (z i • m i) (z j • m j) =\n      polar (m i) (m j) * z i * z j := by\n    rw [polar_smul, polar_symm (m i), polar_smul, polar_symm (m j)]\n    ring\n  have hlast : (∑ i, ∑ j ∈ univ.filter (fun j => j < i), polar (z j • m j) (z i • m i)) =\n      ∑ ij ∈ edges m, polar (m ij.1) (m ij.2) * z ij.1 * z ij.2 := by\n    simp only [edges, sum_filter, Fintype.sum_prod_type, hscalar]\n    rw [sum_comm]\n    refine sum_congr rfl fun i _ => sum_congr rfl fun j _ => ?_\n    by_cases hl : i < j <;> by_cases hn : polar (m i) (m j) = 0 <;> simp [hl, hn]\n  rw [quad_add, quad_add x y, quad_sum, hlast, polar_symm (x+y), polar_sum]\n  have hp (i : Fin s) : polar (z i • m i) (x+y) =\n      z i * (polar x (m i) + polar y (m i)) := by\n    rw [polar_smul, polar_symm (m i), polar_add]\n  simp only [quad_smul, hp]\n  have he : (∑ i, (z i * quad (m i) + z i * (polar x (m i) + polar y (m i)))) =\n      ∑ i, (quad (m i) + polar x (m i) + polar y (m i)) * z i :=\n    sum_congr rfl fun i _ => by ring\n  rw [← he, sum_add_distrib]\n  abel\n\nlemma normal_upper {s k : ℕ} {f : α → β → F}\n    (N : Normal α β s k f) (a₀ : α) (b₀ : β) :\n    (anchor a₀ b₀ f).rank ≤ 2*k + 4*s + 4*(edges N.mask).card := by\n  let pureF : α → β → F := fun a b => (N.r a + quad (N.x a)) + (N.c b + quad (N.y b))\n  let cross : α → β → F := fun a b => polar (N.x a) (N.y b)\n  let single (i : Fin s) : α → β → F := fun a b =>\n    ((N.lin i + quad (N.mask i) + polar (N.x a) (N.mask i)) + polar (N.y b) (N.mask i)) *\n      mixed N.A N.B i a b\n  let pair (ij : Fin s × Fin s) : α → β → F := fun a b =>\n    polar (N.mask ij.1) (N.mask ij.2) * mixed N.A N.B ij.1 a b * mixed N.A N.B ij.2 a b\n  have he : f = pureF + cross + (∑ i, single i) + ∑ ij ∈ edges N.mask, pair ij := by\n    funext a b\n    simp only [N.eqn a b, quad_expansion, pureF, cross, single, pair, Pi.add_apply, Finset.sum_apply]\n    have hsingle : (∑ i, N.lin i * mixed N.A N.B i a b) +\n        (∑ i, (quad (N.mask i) + polar (N.x a) (N.mask i) + polar (N.y b) (N.mask i)) *\n          mixed N.A N.B i a b) =\n        ∑ i, (N.lin i + quad (N.mask i) + polar (N.x a) (N.mask i) + polar (N.y b) (N.mask i)) *\n          mixed N.A N.B i a b := by\n      rw [← sum_add_distrib]\n      exact sum_congr rfl fun i _ => by ring\n    rw [← hsingle]\n    abel\n  rw [congrArg (fun g => (anchor a₀ b₀ g).rank) he, map_add, map_add, map_add, anchor_sep _ _ (show pureF ∈ sep α β from ⟨_,_,fun _ _ => rfl⟩),\n    zero_add]\n  have h₁ := rank_polar a₀ b₀ N.x N.y\n  have h₂ := rank_sum_anchor a₀ b₀ univ single 4 (fun i _ => rank_single _ _ _ _ _ _ _)\n  have h₃ := rank_sum_anchor a₀ b₀ (edges N.mask) pair 4 (fun ij _ => rank_pair _ _ _ _ _ _ _)\n  have h₄ := rank_add_le (anchor a₀ b₀ cross) (anchor a₀ b₀ (∑ i, single i))\n  have h₅ := rank_add_le (anchor a₀ b₀ cross + anchor a₀ b₀ (∑ i, single i))\n    (anchor a₀ b₀ (∑ ij ∈ edges N.mask, pair ij))\n  simp only [card_univ, Fintype.card_fin] at h₂\n  change (anchor a₀ b₀ cross).rank ≤ _ at h₁\n  omega\n\nlemma normal_upper_zero {s : ℕ} {f : α → β → F}\n    (N : Normal α β s 0 f) (a₀ : α) (b₀ : β) :\n    (anchor a₀ b₀ f).rank ≤ 2*s := by\n  let pureF : α → β → F := fun a b => N.r a + N.c b\n  let f' := fun i => rect (N.lin i • N.A i false) (N.B i false) +\n    rect (N.lin i • N.A i true) (N.B i true)\n  have e : f = pureF + ∑ i, f' i := by\n    funext a b\n    simp only [N.eqn a b, quad, Finset.univ_eq_empty, Finset.sum_empty, add_zero, f', pureF,\n      Pi.add_apply, Finset.sum_apply, rect, mixed, Pi.smul_apply, smul_eq_mul, mul_add, mul_assoc]\n  rw [congrArg (fun g => (anchor a₀ b₀ g).rank) e, map_add, anchor_sep _ _ (show pureF ∈ sep α β from ⟨_,_,fun _ _ => rfl⟩), zero_add]\n  refine (rank_sum_anchor a₀ b₀ univ f' 2 ?_).trans (by simp)\n  intro i _\n  change (anchor a₀ b₀ (_ + _)).rank ≤ _\n  rw [map_add]\n  exact (rank_add_le _ _).trans (Nat.add_le_add (rank_rect _ _ _ _) (rank_rect _ _ _ _))\n\nend Upper\n\nsection Masks\n\nopen Finset\n\ndef maskGraph {s k : ℕ} (m : Fin s → Vec k) : SimpleGraph (Fin s) where\n  Adj i j := polar (m i) (m j) ≠ 0\n  symm := ⟨fun i j h => by rwa [polar_symm]⟩\n  loopless := ⟨fun i => by simp [polar_self]⟩\n\nlemma upper_triangle_card {s : ℕ} (G : SimpleGraph (Fin s)) [DecidableRel G.Adj] :\n    (univ.filter (fun ij : Fin s × Fin s => ij.1 < ij.2 ∧ G.Adj ij.1 ij.2)).card =\n      G.edgeFinset.card := by\n  let S := univ.filter (fun ij : Fin s × Fin s => G.Adj ij.1 ij.2)\n  have h := card_filter_add_card_filter_not (s := S) (fun ij => ij.1 < ij.2)\n  have e : (S.filter fun ij => ¬ij.1 < ij.2).card = (S.filter fun ij => ij.1 < ij.2).card := by\n    apply card_equiv (Equiv.prodComm _ _)\n    intro ⟨a,b⟩\n    simp only [S, mem_filter, mem_univ, true_and, Equiv.prodComm_apply]\n    constructor\n    · rintro ⟨hab, hn⟩\n      exact ⟨hab.symm, lt_of_le_of_ne (le_of_not_gt hn) hab.ne.symm⟩\n    · rintro ⟨hab, hl⟩\n      exact ⟨hab.symm, hl.not_gt⟩\n  rw [e] at h\n  have hd : 2 * G.edgeFinset.card = S.card := by\n    rw [SimpleGraph.two_mul_card_edgeFinset]\n  have hh : S.filter (fun ij => ij.1 < ij.2) =\n      univ.filter (fun ij => ij.1 < ij.2 ∧ G.Adj ij.1 ij.2) := by\n    ext ij\n    simp [S, and_comm]\n  rw [hh] at h\n  omega\n\nlemma mask_clique_free {s k : ℕ} (m : Fin s → Vec k) :\n    (maskGraph m).CliqueFree (2*k+2) := by\n  classical\n  intro S hS\n  let d := 2*k+2\n  let o := S.orderEmbOfFin hS.card_eq\n  let u (i : Fin d) := m (o i)\n  let A : Matrix (Fin d) (Fin d) F := Matrix.of fun i j => polar (u i) (u j)\n  let L : Matrix (Fin d) (Fin k × Bool) F := Matrix.of fun i j => u i j.1 j.2\n  let R : Matrix (Fin k × Bool) (Fin d) F := Matrix.of fun j i => u i j.1 (!j.2)\n  have hfact : A = L * R := by\n    ext i j\n    simp only [A,L,R,Matrix.mul_apply,Matrix.of_apply,polar,Fintype.sum_prod_type,Fintype.sum_bool]\n    exact sum_congr rfl fun _ _ => by simp [add_comm]\n  have hr : A.rank ≤ 2*k := by\n    rw [hfact]\n    exact (Matrix.rank_mul_le_left _ _).trans\n      (by simpa [Nat.mul_comm] using Matrix.rank_le_card_width L)\n  let J : Matrix (Fin d) (Fin d) F := Matrix.of fun _ _ => 1\n  have hf : A = J + 1 := by\n    ext i j\n    by_cases h : i = j\n    · subst j\n      simp [A,J,polar_self,Matrix.one_apply,f_add_self]\n    · have hadj := hS.isClique (S.orderEmbOfFin_mem hS.card_eq i)\n          (S.orderEmbOfFin_mem hS.card_eq j) (fun he => h (o.injective he))\n      change polar (u i) (u j) ≠ 0 at hadj\n      rcases f_cases (polar (u i) (u j)) with h0 | h1\n      · exact (hadj h0).elim\n      · simp [A,J,Matrix.one_apply,h,h1]\n  have hj : J * J = 0 := by\n    ext i j\n    simp only [J,Matrix.mul_apply,Matrix.of_apply,one_mul,sum_const,card_univ,Fintype.card_fin,\n      nsmul_eq_mul,Nat.cast_one,mul_one,Matrix.zero_apply,d,Nat.cast_add,Nat.cast_mul]\n    have hz : (↑(2 : ℕ) : F) = 0 := rfl\n    rw [hz, zero_mul, zero_add]\n  have hsq : A*A = 1 := by\n    rw [hf]\n    simp only [add_mul,mul_add,hj,one_mul,mul_one,zero_add]\n    have hh : J + J = 0 := by ext i j; exact f_add_self _\n    rw [← add_assoc, hh, zero_add]\n  have hrr := Matrix.rank_mul_le_left A A\n  rw [hsq, Matrix.rank_one] at hrr\n  simp only [Fintype.card_fin, d] at hrr\n  omega\n\ndef balancedPairs (s k : ℕ) : Finset (Fin s × Fin s) :=\n  univ.filter (fun ij => ij.1 < ij.2 ∧ ij.1.val % (2*k+1) ≠ ij.2.val % (2*k+1))\n\nlemma edges_le {s k : ℕ} (m : Fin s → Vec k) : (edges m).card ≤ (balancedPairs s k).card := by\n  classical\n  have h : (maskGraph m).edgeFinset.card ≤ (SimpleGraph.turanGraph s (2*k+1)).edgeFinset.card :=\n    (SimpleGraph.isTuranMaximal_turanGraph (by omega)).2 (mask_clique_free m)\n  rw [← upper_triangle_card, ← upper_triangle_card] at h\n  have e₁ : edges m = univ.filter (fun ij : Fin s × Fin s => ij.1 < ij.2 ∧ (maskGraph m).Adj ij.1 ij.2) := by\n    apply Finset.filter_congr\n    intro ij _\n    rfl\n  have e₂ : balancedPairs s k =\n      univ.filter (fun ij : Fin s × Fin s => ij.1 < ij.2 ∧ (SimpleGraph.turanGraph s (2*k+1)).Adj ij.1 ij.2) := by\n    apply Finset.filter_congr\n    intro ij _\n    simp only [SimpleGraph.turanGraph_adj]\n  rw [e₁,e₂]\n  exact h\n\nend Masks\n\n\ndef Fits (s k : ℕ) {n m : ℕ} (G : Fin n → Fin m → Bool) : Prop :=\n  ∃ (C : Circuit) (cut : ℕ), cut ≤ C.gates.length ∧\n    ReadsBack (n+m) C ∧\n    (∀ g ∈ C.gates, g.inputs.length ≤ 2) ∧\n    (C.gates.take cut).countP nonlinear ≤ s ∧ (C.gates.drop cut).countP nonlinear ≤ k ∧\n    (∀ d ∈ depthList (n+m) (C.gates.take cut), d ≤ 1) ∧\n    (∀ d ∈ depthList (n+m+cut) (C.gates.drop cut), d ≤ 1) ∧\n    Represents G C\n\nnoncomputable def anchoredRank {n m : ℕ} (G : Fin n → Fin m → Bool) (u : Fin n) (v : Fin m) : ℕ :=\n  (anchor u v (fun i j => b2z (G i j))).rank\n\ndef budget (s k : ℕ) : ℕ := if k = 0 then 2*s else 2*k+4*s+4*(balancedPairs s k).card\n\ntheorem fits_upper {n m s k : ℕ} {G : Fin n → Fin m → Bool} (h : Fits s k G)\n    (u : Fin n) (v : Fin m) : anchoredRank G u v ≤ budget s k := by\n  obtain ⟨C,cut,hcut,hb,hf,hs,hk,hd₀,hd₁,hrep⟩ := h\n  obtain ⟨N⟩ := final_normal C cut hcut hb hf hd₀ hd₁ hs hk\n  have he : (fun i j => b2z (G i j)) = wireFn C.gates (fun i j => oneEach i j) C.output :=\n    funext fun i => funext fun j => congrArg b2z (hrep i j).symm\n  unfold anchoredRank budget\n  rw [he]\n  split_ifs with hzero\n  · subst k\n    exact normal_upper_zero N u v\n  · exact (normal_upper N u v).trans (by\n      have he' := edges_le N.mask\n      omega)\n\nlemma exists_mask_family (k : ℕ) :\n    ∃ m : Fin (2*k+1) → Vec k, ∀ i j, i ≠ j → polar (m i) (m j) = 1 := by\n  induction k with\n  | zero =>\n    exact ⟨fun _ => 0, fun i j h => (h (Fin.ext (by have := i.isLt; have := j.isLt; omega))).elim⟩\n  | succ k ih =>\n    obtain ⟨m,hm⟩ := ih\n    let old (i : Fin (2*(k+1)+1)) : Vec k :=\n      if h : (i : ℕ) < 2*k+1 then m ⟨i,h⟩ else 0\n    let fresh (i : Fin (2*(k+1)+1)) (t : Bool) : F :=\n      if t then (if (i : ℕ) < 2*k+1 then 0 else 1)\n      else if (i : ℕ) = 2*k+1 then 0 else 1\n    refine ⟨fun i => Fin.lastCases (fresh i) (old i), fun i j hij => ?_⟩\n    unfold polar\n    rw [Fin.sum_univ_castSucc]\n    simp only [Fin.lastCases_castSucc, Fin.lastCases_last]\n    change polar (old i) (old j) +\n      (fresh i false * fresh j true + fresh i true * fresh j false) = 1\n    have hright (u : Vec k) : polar u 0 = 0 := (polar_symm _ _).trans (polar_zero u)\n    by_cases hi : i.val < 2*k+1 <;> by_cases hj : j.val < 2*k+1\n    · have h' : (⟨i.val,hi⟩ : Fin (2*k+1)) ≠ ⟨j.val,hj⟩ :=\n        fun h => hij (Fin.ext (congrArg (fun t : Fin (2*k+1) => t.val) h))\n      have ei : i.val ≠ 2*k+1 := by omega\n      have ej : j.val ≠ 2*k+1 := by omega\n      simp only [old,fresh,hi,hj,ei,ej,dite_true,Bool.false_eq_true,ite_false,ite_true,\n        hm _ _ h',mul_zero,zero_mul,add_zero]\n    · have ei : i.val ≠ 2*k+1 := by omega\n      simp only [old,fresh,hi,hj,ei,dite_true,dite_false,Bool.false_eq_true,ite_false,ite_true,\n        hright,one_mul,zero_mul,add_zero,zero_add]\n    · have ej : j.val ≠ 2*k+1 := by omega\n      simp only [old,fresh,hi,hj,ej,dite_true,dite_false,Bool.false_eq_true,ite_false,ite_true,\n        polar_zero,mul_zero,mul_one,add_zero,zero_add]\n    · have hval : i.val ≠ j.val := fun h => hij (Fin.ext h)\n      by_cases ei : i.val = 2*k+1 <;> by_cases ej : j.val = 2*k+1\n      · omega\n      · simp only [old,fresh,hi,hj,ei,ej,dite_false,Bool.false_eq_true,ite_false,ite_true,\n          polar_zero,mul_zero,zero_mul,one_mul,add_zero,zero_add,lt_irrefl,mul_one,hright]\n      · simp only [old,fresh,hi,hj,ei,ej,dite_false,Bool.false_eq_true,ite_false,ite_true,\n          polar_zero,mul_zero,zero_mul,one_mul,add_zero,zero_add,lt_irrefl,mul_one,hright]\n      · have hii := i.isLt\n        have hjj := j.isLt\n        omega\n\nlemma balanced_masks (s k : ℕ) (hk : k ≠ 0) :\n    ∃ m : Fin s → Vec k, (∀ i, m i ≠ 0) ∧ edges m = balancedPairs s k := by\n  obtain ⟨m,hm⟩ := exists_mask_family k\n  let p (i : Fin s) : Fin (2*k+1) := ⟨i.val % (2*k+1),Nat.mod_lt _ (by omega)⟩\n  refine ⟨m ∘ p, ?_, ?_⟩\n  · intro i hi\n    let j : Fin (2*k+1) := if p i = 0 then ⟨1,by omega⟩ else 0\n    have hne : p i ≠ j := by\n      unfold j\n      split_ifs with h\n      · rw [h]\n        intro hh\n        have he : (0 : ℕ) = 1 := congrArg Fin.val hh\n        omega\n      · exact h\n    have e := hm (p i) j hne\n    change m (p i) = 0 at hi\n    rw [hi,polar_zero] at e\n    exact zero_ne_one e\n  · ext ij\n    simp only [edges,balancedPairs,Finset.mem_filter,Finset.mem_univ,true_and,Function.comp_apply]\n    by_cases h : p ij.1 = p ij.2\n    · have hv := congrArg Fin.val h\n      change ij.1.val % (2*k+1) = ij.2.val % (2*k+1) at hv\n      simp [h,hv,polar_self]\n    · have hv : ij.1.val % (2*k+1) ≠ ij.2.val % (2*k+1) := fun e => h (Fin.ext e)\n      simp [hv,hm _ _ h]\n\n\nsection Sharpness\n\nopen Finset\n\nabbrev Coords (n : ℕ) := Fin n × Bool\nabbrev Cube (s k : ℕ) := (Coords k → F) × (Coords s → F)\nabbrev EdgeSlot {s k : ℕ} (m : Fin s → Vec k) := {ij // ij ∈ edges m} × (Bool × Bool)\nabbrev Slot {s k : ℕ} (m : Fin s → Vec k) :=\n  Coords k ⊕ ((Coords s ⊕ Coords s) ⊕ EdgeSlot m)\n\ndef fromCoords {k : ℕ} (x : Coords k → F) : Vec k := fun i t => x (i,t)\n\ndef feat {s k : ℕ} (m : Fin s → Vec k) (x : Cube s k) : Slot m → F\n  | .inl p => x.1 p\n  | .inr (.inl (.inl p)) => x.2 p\n  | .inr (.inl (.inr p)) => polar (fromCoords x.1) (m p.1) * x.2 p\n  | .inr (.inr e) => x.2 (e.1.val.1,e.2.1) * x.2 (e.1.val.2,e.2.2)\n\nlemma polar_coord {k : ℕ} (l : Coords k) (v : Vec k) :\n    polar (fromCoords (Pi.single l 1)) v = v l.1 (!l.2) := by\n  classical\n  rcases l with ⟨i,t⟩\n  unfold polar\n  rw [sum_eq_single i]\n  · cases t <;> simp [fromCoords, Pi.single_apply]\n  · intro j _ hj\n    simp [fromCoords, Pi.single_apply, hj]\n  · simp\n\nlemma isolated_edge {s k : ℕ} (m : Fin s → Vec k) (a b : EdgeSlot m) :\n    let p := (a.1.val.1,a.2.1)\n    let q := (a.1.val.2,a.2.2)\n    let v : Coords s → F := fun j => if j = p ∨ j = q then 1 else 0\n    v (b.1.val.1,b.2.1) * v (b.1.val.2,b.2.2) = if b = a then 1 else 0 := by\n  classical\n  intro p q v\n  by_cases e : b = a\n  · subst b\n    simp [v,p,q]\n  · have ha := (mem_filter.mp a.1.property).2.1\n    have hb := (mem_filter.mp b.1.property).2.1\n    have hn : ¬ (((b.1.val.1,b.2.1) = p ∨ (b.1.val.1,b.2.1) = q) ∧\n        ((b.1.val.2,b.2.2) = p ∨ (b.1.val.2,b.2.2) = q)) := by\n      rintro ⟨h₁ | h₁,h₂ | h₂⟩\n      all_goals simp only [p,q,Prod.mk.injEq] at h₁ h₂\n      · exact hb.ne (h₁.1.trans h₂.1.symm)\n      · apply e\n        exact Prod.ext (Subtype.ext (Prod.ext h₁.1 h₂.1)) (Prod.ext h₁.2 h₂.2)\n      · rw [h₁.1,h₂.1] at hb\n        exact ha.asymm hb\n      · exact hb.ne (h₁.1.trans h₂.1.symm)\n    simp only [v,e,ite_false]\n    split_ifs with h₁ h₂ <;> simp_all\n\nlemma feat_independent {s k : ℕ} (m : Fin s → Vec k) (hm : ∀ i, m i ≠ 0) :\n    LinearIndependent F (fun j : Slot m => fun x : Cube s k => feat m x j) := by\n  classical\n  rw [Fintype.linearIndependent_iff]\n  intro g hg\n  let a := g ∘ Sum.inl\n  let b := g ∘ Sum.inr ∘ Sum.inl ∘ Sum.inl\n  let c := g ∘ Sum.inr ∘ Sum.inl ∘ Sum.inr\n  let e := g ∘ Sum.inr ∘ Sum.inr\n  have H (x : Coords k → F) (y : Coords s → F) :\n      (∑ p, a p * x p) + ((∑ p, b p * y p) + (∑ p, c p * (polar (fromCoords x) (m p.1) * y p))) +\n        ∑ j : EdgeSlot m, e j * (y (j.1.val.1,j.2.1) * y (j.1.val.2,j.2.2)) = 0 := by\n    have hh := congrFun hg (x,y)\n    simp only [Fintype.sum_sum_type, Finset.sum_apply, Pi.smul_apply, smul_eq_mul, Pi.zero_apply, feat] at hh\n    simpa only [a,b,c,e,Function.comp_apply,add_assoc] using hh\n  have ha (p : Coords k) : a p = 0 := by simpa [Pi.single_apply, mul_ite] using H (Pi.single p 1) 0\n  have he0 (p : Coords s) (j : EdgeSlot m) :\n      (Pi.single p (1 : F) : Coords s → F) (j.1.val.1,j.2.1) * (Pi.single p (1 : F) : Coords s → F) (j.1.val.2,j.2.2) = 0 := by\n    have hlt := (mem_filter.mp j.1.property).2.1\n    simp only [Pi.single_apply]\n    split_ifs with h₁ h₂ <;> simp_all\n    exact (hlt.ne (congrArg Prod.fst (h₁.trans h₂.symm))).elim\n  have hb (p : Coords s) : b p = 0 := by\n    have hz : fromCoords (0 : Coords k → F) = 0 := rfl\n    have he := H 0 (Pi.single p 1)\n    simp only [he0] at he\n    simpa [hz, polar_zero, Pi.single_apply, mul_ite] using he\n  have hc (p : Coords s) : c p = 0 := by\n    obtain ⟨l,hl⟩ : ∃ l : Coords k, m p.1 l.1 (!l.2) = 1 := by\n      by_contra! h\n      apply hm p.1\n      funext i t\n      rcases f_cases (m p.1 i t) with hz | ho\n      · exact hz\n      · exact (h (i,!t) (by simpa using ho)).elim\n    have he := H (Pi.single l 1) (Pi.single p 1)\n    simp only [he0] at he\n    simpa [ha,hb,polar_coord,Pi.single_apply,mul_ite,hl] using he\n  have he (j : EdgeSlot m) : e j = 0 := by\n    let y : Coords s → F := fun p => if p = (j.1.val.1,j.2.1) ∨ p = (j.1.val.2,j.2.2) then 1 else 0\n    have hh := H 0 y\n    simp only [ha,hb,hc,zero_mul,sum_const_zero,add_zero,zero_add,y,isolated_edge] at hh\n    simpa using hh\n  rintro (p | ((p | p) | p))\n  · exact ha p\n  · exact hb p\n  · exact hc p\n  · exact he p\n\n\ndef swapSlot {s k : ℕ} {m : Fin s → Vec k} : Slot m → Slot m\n  | .inl (i,t) => .inl (i,!t)\n  | .inr (.inl (.inl p)) => .inr (.inl (.inr p))\n  | .inr (.inl (.inr p)) => .inr (.inl (.inl p))\n  | .inr (.inr e) => .inr (.inr e)\n\nlemma swapSlot_involutive {s k : ℕ} {m : Fin s → Vec k} :\n    Function.Involutive (@swapSlot s k m) := by\n  rintro (⟨i,t⟩ | ((p | p) | p))\n  · cases t <;> rfl\n  · rfl\n  · rfl\n  · rfl\n\ndef modelMatrix {s k : ℕ} (m : Fin s → Vec k) : Matrix (Cube s k) (Cube s k) F :=\n  Matrix.of fun x y => ∑ i : Slot m, feat m x i * feat m y (swapSlot i)\n\nlemma model_rank {s k : ℕ} (m : Fin s → Vec k) (hm : ∀ i, m i ≠ 0) :\n    (modelMatrix m).rank = 2*k + 4*s + 4*(edges m).card := by\n  classical\n  let A : Matrix (Cube s k) (Slot m) F := Matrix.of (feat m)\n  let B : Matrix (Slot m) (Cube s k) F := Matrix.of fun i y => feat m y (swapSlot i)\n  have hmul : modelMatrix m = A*B := by ext x y; rfl\n  have hA : A.rank = Fintype.card (Slot m) :=\n    Matrix.vecMul_surjective_iff_rank_eq_card.mp (feat_independent m hm).vecMul_surjective\n  have hB : B.rank = Fintype.card (Slot m) := by\n    apply Matrix.mulVec_surjective_iff_rank_eq_card.mp\n    exact ((feat_independent m hm).comp _ swapSlot_involutive.injective).mulVec_surjective\n  have h1 := Matrix.rank_mul_ge A B\n  have h2 := Matrix.rank_mul_le_left A B\n  have he : (A*B).rank = Fintype.card (Slot m) := by omega\n  rw [hmul,he]\n  simp only [Slot, Coords, EdgeSlot, Fintype.card_sum, Fintype.card_prod, Fintype.card_fin,\n    Fintype.card_bool, Fintype.card_coe]\n  omega\n\ndef witnessZ {s k : ℕ} (i : Fin s) (x y : Cube s k) : F :=\n  x.2 (i,false) * y.2 (i,false) + x.2 (i,true) * y.2 (i,true)\n\ndef witnessFun {s k : ℕ} (m : Fin s → Vec k) (x y : Cube s k) : F :=\n  (∑ i, quad (m i) * witnessZ i x y) +\n    quad (fromCoords x.1 + fromCoords y.1 + ∑ i, witnessZ i x y • m i)\n\nlemma witness_expanded {s k : ℕ} (m : Fin s → Vec k) (x y : Cube s k) :\n    witnessFun m x y =\n      quad (fromCoords x.1) + quad (fromCoords y.1) + modelMatrix m x y := by\n  unfold witnessFun\n  rw [quad_expansion]\n  have hsum : (∑ i, quad (m i) * witnessZ i x y) +\n      (∑ i, (quad (m i) + polar (fromCoords x.1) (m i) + polar (fromCoords y.1) (m i)) *\n        witnessZ i x y) =\n      ∑ i, (polar (fromCoords x.1) (m i) + polar (fromCoords y.1) (m i)) * witnessZ i x y := by\n    rw [← sum_add_distrib]\n    refine sum_congr rfl fun i _ => ?_\n    linear_combination (witnessZ i x y) * f_add_self (quad (m i))\n  rw [modelMatrix, Matrix.of_apply]\n  simp only [Fintype.sum_sum_type, feat, swapSlot]\n  have hbase : (∑ i : Coords k, x.1 i * y.1 (i.1,!i.2)) = polar (fromCoords x.1) (fromCoords y.1) := by\n    simp [polar, fromCoords, Fintype.sum_prod_type, Fintype.sum_bool,add_comm]\n  have hprep : ((∑ i : Coords s, x.2 i * (polar (fromCoords y.1) (m i.1) * y.2 i)) +\n      (∑ i : Coords s, (polar (fromCoords x.1) (m i.1) * x.2 i) * y.2 i)) =\n      ∑ i, (polar (fromCoords x.1) (m i) + polar (fromCoords y.1) (m i)) * witnessZ i x y := by\n    simp only [Fintype.sum_prod_type, Fintype.sum_bool]\n    rw [← sum_add_distrib]\n    exact sum_congr rfl fun i _ => by unfold witnessZ; ring\n  have hpairs : (∑ i : EdgeSlot m,\n      x.2 (i.1.val.1,i.2.1) * x.2 (i.1.val.2,i.2.2) *\n        (y.2 (i.1.val.1,i.2.1) * y.2 (i.1.val.2,i.2.2))) =\n      ∑ ij ∈ edges m, polar (m ij.1) (m ij.2) * witnessZ ij.1 x y * witnessZ ij.2 x y := by\n    rw [Fintype.sum_prod_type]\n    rw [← Finset.sum_attach (edges m) (fun ij => polar (m ij.1) (m ij.2) * witnessZ ij.1 x y * witnessZ ij.2 x y)]\n    refine sum_congr rfl fun i _ => ?_\n    have h := (mem_filter.mp i.property).2.2\n    rcases f_cases (polar (m i.val.1) (m i.val.2)) with hz | ho\n    · exact (h hz).elim\n    · rw [ho]\n      simp only [Fintype.sum_prod_type, Fintype.sum_bool, one_mul, witnessZ]\n      ring\n  rw [hbase,hprep,hpairs,← hsum]\n  abel\n\nlemma model_zero {s k : ℕ} (m : Fin s → Vec k) (x : Cube s k) :\n    modelMatrix m (0,0) x = 0 ∧ modelMatrix m x (0,0) = 0 := by\n  have h : ∀ i, feat m (0,0) i = 0 := by\n    rintro (p | ((p | p) | p)) <;> simp [feat]\n  simp [modelMatrix,h]\n\nlemma witness_rank {s k : ℕ} (m : Fin s → Vec k) (hm : ∀ i, m i ≠ 0) :\n    (anchor (0,0) (0,0) (witnessFun m)).rank = 2*k+4*s+4*(edges m).card := by\n  have he : anchor (0,0) (0,0) (witnessFun m) = modelMatrix m := by\n    have hp : (fun x y : Cube s k => quad (fromCoords x.1) + quad (fromCoords y.1)) ∈\n        sep (Cube s k) (Cube s k) := ⟨_,_,fun _ _ => rfl⟩\n    have he' : witnessFun m =\n        (fun x y : Cube s k => quad (fromCoords x.1) + quad (fromCoords y.1)) +\n        (fun x y => modelMatrix m x y) :=\n      funext fun x => funext fun y => witness_expanded m x y\n    rw [he', map_add, anchor_sep _ _ hp,zero_add]\n    ext x y\n    change modelMatrix m x y + modelMatrix m x (0,0) +\n      modelMatrix m (0,0) y + modelMatrix m (0,0) (0,0) = _\n    rw [(model_zero m x).2,(model_zero m y).1,(model_zero m (0,0)).2]\n    simp\n  rw [he, model_rank m hm]\n\nend Sharpness\n\nend PEqualNP.Outside.L_meta_33679\n","lean_sha256":"29262784f69aae228e76a4db3408bdbd449ab654a9523ad0bcb753595ee805b0","build":{"command":"lake build PEqualNP.Outside.L_meta_33679","exit":0,"seconds":16.65},"axioms":["propext","Classical.choice","Quot.sound"],"upstream_commit":"32d0fff5a2b223f305f5e23d96b07f6d18c39545","approach":"The checked partial proves fits_upper for the row’s predicates, including the sharper 2*s bound when k=0. A separate scratch theorem checked the upper-bound statement directly against the row text. I used the binary truth-table expansion and a span invariant for each layer. A preparation product contributes two row-column products after removing single-bank terms. The final quadratic expansion leaves at most four rank-one terms per preparation and per nonzero mask pair. A clique of 2*k+2 masks would have a Gram matrix J+I with square I, contradicting its rank bound 2*k. The pinned Mathlib Turán theorem then gives the residue-class pair count. The checked balanced_masks lemma constructs the masks inductively by adding one coordinate pair. witness_rank proves the algebraic witness attains the bound for k>0 by isolating coefficients on zero, single-coordinate and two-coordinate cube inputs. Concrete readback circuits on all one-hot pairs gave ranks 0, 2, 4 for s=0,1,2 at k=0, 2, 6, 14, 26, 38 for s=0,…,4 at k=1, and 4, 8, 16 for s=0,1,2 at k=2. Exhaustive mask multisets also matched the pair count for k=1,2 and s≤6. I tried a tape-extension construction that retains earlier wires when appending gates. General synthesis from the rank witness, including formal attainment for k=0, remains unfinished. The build and axiom audit cover the partial lemmas only. The local build peaked at about 7.5 GB RSS, so the 6 GB limit was not verified. The row omitted level.","reason":"missing_lemma","skipped":[{"id":"L-meta-32790","state":"pr_open"},{"id":"L-meta-32784","state":"pr_open"},{"id":"L-meta-32702","state":"pr_open"}]}
