AFTD Auto-Formalizing Theoretical Domains

c4_free_hypercube_subgraph_degree_square_sum

theoremverifiedformalization

Let n ≥ 2 and let G be a subgraph of the n-dimensional hypercube Q_n (vertices {0,1}^n, adjacent when at Hamming distance 1) that contains no copy of the 4-cycle C_4. A coordinate square is a vertex set {u : u_k = v_k for all k ∉ {i, j}} for distinct coordinates i ≠ j and a vertex v. Let a_0 be the number of coordinate squares containing no edge of G (an edge being contained when both endpoints lie in the square), and a_opp the number of coordinate squares containing exactly two edges of G that share no vertex. Then, in the integers, Σ_v d_G(v)^2 = 2n·e(G) − 2·C(n,2)·2^(n−2) + 2(a_0 − a_opp).

Statement

open Classical in
theorem c4_free_hypercube_subgraph_degree_square_sum (n : ℕ) (hn : 2 ≤ n)
    (G : SimpleGraph (Fin n → Bool)) (hG : G ≤ hypercube_graph n)
    (hC4 : (SimpleGraph.cycleGraph 4).Free G) :
    let squares : Finset (Finset (Fin n → Bool)) := Finset.univ.filter fun s =>
      ∃ i j : Fin n, i ≠ j ∧ ∃ v : Fin n → Bool,
        s = Finset.univ.filter fun u => ∀ k, k ≠ i → k ≠ j → u k = v k
    let edgesIn : Finset (Fin n → Bool) → Finset (Sym2 (Fin n → Bool)) := fun s =>
      G.edgeFinset.filter fun e => ∀ w ∈ e, w ∈ s
    let a0 : ℕ := (squares.filter fun s => edgesIn s = ∅).card
    let aopp : ℕ := (squares.filter fun s => (edgesIn s).card = 2 ∧
      ∀ e ∈ edgesIn s, ∀ f ∈ edgesIn s, e ≠ f → ∀ w ∈ e, w ∉ f).card
    ∑ v, (G.degree v : ℤ) ^ 2 =
      2 * n * G.edgeFinset.card - 2 * (n.choose 2 : ℤ) * 2 ^ (n - 2) + 2 * ((a0 : ℤ) - aopp)
Formalizes
arXiv:2611.27284 (The four-cycle extremal number of the seven-cube), Lemma 2.3 (square identity), equation (2.1).
Verified
29 Nov 2026
Axioms
Classical.choiceQuot.soundpropext
Built on 5
  • hypercube_graphThe n-dimensional hypercube graph Q_n has vertex set {0, 1}^n, with two vertices adjacent if and only if their Hamming distance is 1 (they…
  • hypercube_coordinate_square_countFor n ≥ 2, the number of coordinate squares of the n-cube {0,1}^n is C(n,2)·2^(n-2), where a coordinate square is a vertex set of the form…
  • LinearOrder.toTotalPreorderEvery LinearOrder is a TotalPreorder.
  • TotalPreorderA total preorder: a preorder where ≤ is total (complete). This is weaker than LinearOrder — it does NOT require antisymmetry or…
  • FThe prime field $\mathrm{GF}(p)$, realized as $\mathbb{Z}/p\mathbb{Z}$ via ZMod.

Read back from the Lean

For every natural number $n$ with $2 \le n$, and every simple graph $G$ on the vertex set $\text{Fin } n \to \text{Bool}$ such that $G$ is a subgraph of the $n$-dimensional hypercube graph hypercube_graph n and $G$ is $C_4$-free (contains no subgraph isomorphic to SimpleGraph.cycleGraph 4): Let: - $\text{squares}$ be the finite set of all subsets $s \subseteq (\text{Fin } n \to \text{Bool})$ for which there exist distinct coordinates $i \ne j$ in $\text{Fin } n$ and a vector $v : \text{Fin } n \to \text{Bool}$ such that $s = \{u : \text{Fin } n \to \text{Bool} \mid \forall k \ne i, j,\; u(k) = v(k)\}$ (the 2-dimensional faces/squares of the hypercube); - $\text{edgesIn}(s)$ be the set of edges of $G$ both of whose endpoints lie in $s$; - $a_0$ be the number of squares $s \in \text{squares}$ containing no edges of $G$ ($\text{edgesIn}(s) = \emptyset$); - $a_{opp}$ be the number of squares $s \in \text{squares}$ containing exactly two edges of $G$ which are disjoint (share no vertices); Then the sum over all vertices $v : \text{Fin } n \to \text{Bool}$ of $(d_G(v))^2$ (in $\mathbb{Z}$) is equal to: $$2n \cdot |E(G)| - 2 \binom{n}{2} 2^{n - 2} + 2(a_0 - a_{opp})$$

Written by a model that saw only the Lean, never the English above. If the two disagree, that is worth a challenge.

The proof, explained

We evaluate the sum of squared degrees in a C4-free subgraph of the hypercube Q_n by decomposing the sum into local contributions on each coordinate square. For each square, C4-freeness allows an algebraic identity expressing the product of directional degrees in terms of total edges, an empty-square indicator, and an opposite-edges indicator. Summing these local identities over all coordinate squares and double-counting the edges yields the theorem.

  1. Properties of updating coordinates

    Evaluate coordinate updates.

    lines 10–19

    theorem sq2_l {w : Fin n → Bool} {l m : Fin n} (h : l ≠ m) (a b : Bool) : sq2 w l m a b l = a := by
      simp [sq2, Function.update_apply, h]
    
    theorem sq2_m (w : Fin n → Bool) (l m : Fin n) (a b : Bool) : sq2 w l m a b m = b := by
      simp [sq2]
    
    theorem sq2_other (w : Fin n → Bool) {l m k : Fin n} (hl : k ≠ l) (hm : k ≠ m) (a b : Bool) :
        sq2 w l m a b k = w k := by
      simp [sq2, Function.update_apply, hl, hm]
    
  2. Flip and coordinate updates

    Flipping coordinate l or m toggles the corresponding bit.

    lines 20–35

    theorem fl_sq2_l (w : Fin n → Bool) {l m : Fin n} (h : l ≠ m) (a b : Bool) :
        fl (sq2 w l m a b) l = sq2 w l m (!a) b := by
      funext k
      simp only [fl, sq2, Function.update_apply]
      by_cases hk : k = l
      · subst hk; simp [h]
      · simp [hk]
    
    theorem fl_sq2_m (w : Fin n → Bool) {l m : Fin n} (a b : Bool) :
        fl (sq2 w l m a b) m = sq2 w l m a (!b) := by
      funext k
      simp only [fl, sq2, Function.update_apply]
      by_cases hk : k = m
      · subst hk; simp
      · simp [hk]
    
  3. Injectivity of coordinate updates

    Different Boolean pairs give distinct vertices.

    lines 36–45

    theorem sq2_ne {w : Fin n → Bool} {l m : Fin n} (h : l ≠ m) {a b a' b' : Bool}
        (hne : (a, b) ≠ (a', b')) : sq2 w l m a b ≠ sq2 w l m a' b' := by
      intro he
      apply hne
      have h1 := congrFun he l
      have h2 := congrFun he m
      rw [sq2_l h, sq2_l h] at h1
      rw [sq2_m, sq2_m] at h2
      rw [h1, h2]
    
  4. Remaining proof

    The rest of the argument.

    lines 46–565

    uses In1264.main

    /-- the vertex set splits along two coordinates -/
    theorem sum_split {l m : Fin n} (h : l ≠ m) (f : (Fin n → Bool) → ℤ) :
        ∑ v, f v = ∑ w ∈ Finset.univ.filter (fun w : Fin n → Bool => w l = false ∧ w m = false),
          ∑ a : Bool, ∑ b : Bool, f (sq2 w l m a b) := by
      rw [← Finset.sum_product', ← Finset.sum_product']
      refine Finset.sum_nbij' (fun v => ((sq2 v l m false false, v l), v m))
        (fun x => sq2 x.1.1 l m x.1.2 x.2) ?_ ?_ ?_ ?_ ?_
      · intro v _
        simp [sq2_l h, sq2_m]
      · intro _ _; exact Finset.mem_univ _
      · intro v _
        funext k
        simp only [sq2, Function.update_apply]
        split_ifs <;> simp_all
      · rintro ⟨⟨w, a⟩, b⟩ hx
        simp only [Finset.mem_product, Finset.mem_filter, Finset.mem_univ, true_and, and_true] at hx
        simp only [sq2_l h, sq2_m, Prod.mk.injEq, and_true]
        funext k
        simp only [sq2, Function.update_apply]
        split_ifs <;> simp_all
      · intro v _
        congr 1
        funext k
        simp only [sq2, Function.update_apply]
        split_ifs <;> simp_all
    
    open Classical in
    theorem quad_facts {α : Type*} [DecidableEq α] {p q r s : α} (hpq : p ≠ q) (hpr : p ≠ r)
        (hps : p ≠ s) (hqr : q ≠ r) (hqs : q ≠ s) (hrs : r ≠ s) (A B C D : Bool)
        (h4 : ¬ (A = true ∧ B = true ∧ C = true ∧ D = true)) :
        let E : Finset (Sym2 α) := (if A then {s(p, q)} else ∅) ∪ (if B then {s(r, s)} else ∅) ∪
          (if C then {s(p, r)} else ∅) ∪ (if D then {s(q, s)} else ∅)
        ((A.toNat + B.toNat : ℤ) * (C.toNat + D.toNat)) =
          (A.toNat + B.toNat + C.toNat + D.toNat : ℤ) - 1 + (if E = ∅ then 1 else 0) -
            (if E.card = 2 ∧ ∀ e ∈ E, ∀ f ∈ E, e ≠ f → ∀ w ∈ e, w ∉ f then 1 else 0) ∧
        (E.card : ℤ) = A.toNat + B.toNat + C.toNat + D.toNat := by
      intro E
      cases A <;> cases B <;> cases C <;> cases D <;>
        simp_all [E, Finset.card_insert_of_notMem, Sym2.eq_iff, hpq.symm, hpr.symm, hps.symm,
          hqr.symm, hqs.symm, hrs.symm]
    
    
    theorem adj_eq_fl (v u : Fin n → Bool) (h : (hypercube_graph n).Adj v u) :
        ∃ l, u = fl v l := by
      have h1 : hammingDist v u = 1 := h
      unfold hammingDist at h1
      obtain ⟨l, hl⟩ := Finset.card_eq_one.1 h1
      refine ⟨l, funext fun m => ?_⟩
      have hm := Finset.ext_iff.1 hl m
      simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_singleton] at hm
      simp only [fl]
      by_cases hml : m = l
      · rw [if_pos hml]
        have := hm.2 hml
        cases hv : v m <;> cases hu : u m <;> simp_all
      · rw [if_neg hml]
        have := mt hm.1 hml
        simp only [ne_eq, not_not] at this
        exact this.symm
    
    theorem fl_injective (v : Fin n → Bool) : Function.Injective (fl v) := by
      intro l l' h
      by_contra hne
      have := congrFun h l
      simp [fl, hne] at this
    
    open Classical in
    theorem degree_eq (G : SimpleGraph (Fin n → Bool)) (hG : G ≤ hypercube_graph n)
        (v : Fin n → Bool) :
        (G.degree v : ℤ) = ∑ l, if G.Adj v (fl v l) then (1 : ℤ) else 0 := by
      have hd : G.degree v = (Finset.univ.filter fun l => G.Adj v (fl v l)).card := by
        rw [← SimpleGraph.card_neighborFinset_eq_degree]
        have : G.neighborFinset v =
            (Finset.univ.filter fun l => G.Adj v (fl v l)).image (fl v) := by
          ext u
          simp only [SimpleGraph.mem_neighborFinset, Finset.mem_image, Finset.mem_filter,
            Finset.mem_univ, true_and]
          constructor
          · intro h
            obtain ⟨l, rfl⟩ := adj_eq_fl v u (hG h)
            exact ⟨l, h, rfl⟩
          · rintro ⟨l, hl, rfl⟩
            exact hl
        rw [this, Finset.card_image_of_injective _ (fl_injective v)]
      rw [hd, Finset.card_filter]
      push_cast
      rfl
    
    /-- squares of 0/1 sums -/
    theorem sq_sum01 (x : Fin n → ℤ) (hx : ∀ l, x l * x l = x l) :
        (∑ l, x l) ^ 2 = ∑ l, x l + 2 * ∑ p : Fin n × Fin n,
          if p.1 < p.2 then x p.1 * x p.2 else 0 := by
      have h1 : (∑ l, x l) ^ 2 = ∑ p : Fin n × Fin n, x p.1 * x p.2 := by
        rw [sq, Finset.sum_mul_sum, ← Finset.sum_product']; rfl
      have h2 : ∀ p : Fin n × Fin n, x p.1 * x p.2 = (if p.1 < p.2 then x p.1 * x p.2 else 0) +
          (if p.1 = p.2 then x p.1 * x p.2 else 0) + (if p.2 < p.1 then x p.1 * x p.2 else 0) := by
        intro p
        rcases lt_trichotomy p.1 p.2 with h | h | h
        · simp [h, h.ne, not_lt.2 h.le]
        · simp [h]
        · simp [h, h.ne', not_lt.2 h.le]
      have h3 : ∑ p : Fin n × Fin n, (if p.2 < p.1 then x p.1 * x p.2 else 0) =
          ∑ p : Fin n × Fin n, (if p.1 < p.2 then x p.1 * x p.2 else 0) := by
        refine Fintype.sum_equiv (Equiv.prodComm _ _) _ _ ?_
        intro p; simp [mul_comm]
      have h4 : ∑ p : Fin n × Fin n, (if p.1 = p.2 then x p.1 * x p.2 else 0) = ∑ l, x l := by
        rw [Fintype.sum_prod_type]
        refine Finset.sum_congr rfl fun l _ => ?_
        rw [Finset.sum_ite_eq]; simp [hx]
      rw [h1, Finset.sum_congr rfl (fun p _ => h2 p), Finset.sum_add_distrib, Finset.sum_add_distrib,
        h3, h4]
      ring
    
    theorem sum_pairs (T : Fin n → ℤ) :
        ∑ p : Fin n × Fin n, (if p.1 < p.2 then T p.1 + T p.2 else 0) = ((n : ℤ) - 1) * ∑ l, T l := by
      have h1 : ∀ p : Fin n × Fin n, (if p.1 < p.2 then T p.1 + T p.2 else 0) =
          (if p.1 < p.2 then T p.1 else 0) + (if p.1 < p.2 then T p.2 else 0) := by
        intro p; by_cases h : p.1 < p.2 <;> simp [h]
      have h2 : ∑ p : Fin n × Fin n, (if p.1 < p.2 then T p.2 else 0) =
          ∑ p : Fin n × Fin n, (if p.2 < p.1 then T p.1 else 0) := by
        refine Fintype.sum_equiv (Equiv.prodComm _ _) _ _ ?_
        intro p; rfl
      have h3 : ∀ p : Fin n × Fin n, (if p.1 < p.2 then T p.1 else 0) +
          (if p.2 < p.1 then T p.1 else 0) = T p.1 - (if p.1 = p.2 then T p.1 else 0) := by
        intro p
        rcases lt_trichotomy p.1 p.2 with h | h | h
        · simp [h, h.ne, not_lt.2 h.le]
        · simp [h]
        · simp [h, h.ne', not_lt.2 h.le]
      rw [Finset.sum_congr rfl (fun p _ => h1 p), Finset.sum_add_distrib, h2,
        ← Finset.sum_add_distrib, Finset.sum_congr rfl (fun p _ => h3 p), Finset.sum_sub_distrib,
        Fintype.sum_prod_type, Fintype.sum_prod_type]
      have h4 : ∀ l : Fin n, ∑ m : Fin n, (if l = m then T l else 0) = T l := by
        intro l; rw [Finset.sum_ite_eq]; simp
      simp only [h4, Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul]
      rw [Finset.mul_sum, ← Finset.sum_sub_distrib]
      refine Finset.sum_congr rfl fun l _ => ?_
      ring
    
    theorem c4adj : ∀ i j : Fin 4, (SimpleGraph.cycleGraph 4).Adj i j →
        (i = 0 ∧ j = 1) ∨ (i = 1 ∧ j = 0) ∨ (i = 1 ∧ j = 2) ∨ (i = 2 ∧ j = 1) ∨
        (i = 2 ∧ j = 3) ∨ (i = 3 ∧ j = 2) ∨ (i = 3 ∧ j = 0) ∨ (i = 0 ∧ j = 3) := by
      intro i j
      rw [SimpleGraph.cycleGraph_adj']
      revert i j; decide
    
    theorem c4_of {α : Type*} {G : SimpleGraph α} {p q r s : α} (hpq : p ≠ q) (hpr : p ≠ r)
        (hps : p ≠ s) (hqr : q ≠ r) (hqs : q ≠ s) (hrs : r ≠ s)
        (h1 : G.Adj p q) (h2 : G.Adj r s) (h3 : G.Adj p r) (h4 : G.Adj q s) :
        ¬ (SimpleGraph.cycleGraph 4).Free G := by
      intro hF
      apply hF
      refine ⟨⟨⟨![p, q, s, r], ?_⟩, ?_⟩⟩
      · intro i j hij
        rcases c4adj i j hij with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ |
          ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ <;> simp <;>
          first | exact h1 | exact h1.symm | exact h2 | exact h2.symm | exact h3 | exact h3.symm | exact h4 | exact h4.symm
      · intro i j hij
        fin_cases i <;> fin_cases j <;> simp_all [eq_comm]
    
    
    section
    variable (G : SimpleGraph (Fin n → Bool))
    
    open Classical in
    noncomputable def xl (v : Fin n → Bool) (l : Fin n) : ℤ := if G.Adj v (fl v l) then 1 else 0
    
    open Classical in
    noncomputable def eA (w : Fin n → Bool) (l m : Fin n) : ℤ :=
      if G.Adj (sq2 w l m false false) (sq2 w l m true false) then 1 else 0
    open Classical in
    noncomputable def eB (w : Fin n → Bool) (l m : Fin n) : ℤ :=
      if G.Adj (sq2 w l m false true) (sq2 w l m true true) then 1 else 0
    open Classical in
    noncomputable def eC (w : Fin n → Bool) (l m : Fin n) : ℤ :=
      if G.Adj (sq2 w l m false false) (sq2 w l m false true) then 1 else 0
    open Classical in
    noncomputable def eD (w : Fin n → Bool) (l m : Fin n) : ℤ :=
      if G.Adj (sq2 w l m true false) (sq2 w l m true true) then 1 else 0
    
    theorem pair_prod {l m : Fin n} (h : l ≠ m) :
        ∑ v, xl G v l * xl G v m = ∑ w ∈ Finset.univ.filter (fun w : Fin n → Bool =>
          w l = false ∧ w m = false), (eA G w l m + eB G w l m) * (eC G w l m + eD G w l m) := by
      rw [sum_split h]
      refine Finset.sum_congr rfl fun w _ => ?_
      simp only [Fintype.sum_bool, xl, eA, eB, eC, eD]
      rw [fl_sq2_l w h true true, fl_sq2_l w h true false, fl_sq2_l w h false true, fl_sq2_l w h false false, fl_sq2_m w (l := l) (m := m) true true, fl_sq2_m w (l := l) (m := m) true false, fl_sq2_m w (l := l) (m := m) false true, fl_sq2_m w (l := l) (m := m) false false, Bool.not_true, Bool.not_false]
      rw [G.adj_comm (sq2 w l m true true) (sq2 w l m false true),
        G.adj_comm (sq2 w l m true true) (sq2 w l m true false),
        G.adj_comm (sq2 w l m true false) (sq2 w l m false false),
        G.adj_comm (sq2 w l m false true) (sq2 w l m false false)]
      split_ifs <;> ring
    
    theorem pair_l {l m : Fin n} (h : l ≠ m) :
        ∑ v, xl G v l = ∑ w ∈ Finset.univ.filter (fun w : Fin n → Bool =>
          w l = false ∧ w m = false), 2 * (eA G w l m + eB G w l m) := by
      rw [sum_split h]
      refine Finset.sum_congr rfl fun w _ => ?_
      simp only [Fintype.sum_bool, xl, eA, eB]
      rw [fl_sq2_l w h true true, fl_sq2_l w h true false, fl_sq2_l w h false true, fl_sq2_l w h false false, Bool.not_true, Bool.not_false,
        G.adj_comm (sq2 w l m true true) (sq2 w l m false true),
        G.adj_comm (sq2 w l m true false) (sq2 w l m false false)]
      split_ifs <;> ring
    
    theorem pair_m {l m : Fin n} (h : l ≠ m) :
        ∑ v, xl G v m = ∑ w ∈ Finset.univ.filter (fun w : Fin n → Bool =>
          w l = false ∧ w m = false), 2 * (eC G w l m + eD G w l m) := by
      rw [sum_split h]
      refine Finset.sum_congr rfl fun w _ => ?_
      simp only [Fintype.sum_bool, xl, eC, eD]
      rw [fl_sq2_m w (l := l) (m := m) true true, fl_sq2_m w (l := l) (m := m) true false, fl_sq2_m w (l := l) (m := m) false true, fl_sq2_m w (l := l) (m := m) false false, Bool.not_true, Bool.not_false,
        G.adj_comm (sq2 w l m true true) (sq2 w l m true false),
        G.adj_comm (sq2 w l m false true) (sq2 w l m false false)]
      split_ifs <;> ring
    
    theorem mem_ite_single {α : Type*} [DecidableEq α] {c : Prop} [Decidable c] {x y : α}
        (hc : c) (h : x = y) : x ∈ (if c then {y} else ∅ : Finset α) := by
      rw [if_pos hc]; exact Finset.mem_singleton.2 h
    
    theorem mem_sq_eq {w u : Fin n → Bool} {l m : Fin n} (h : l ≠ m)
        (hu : ∀ k, k ≠ l → k ≠ m → u k = w k) : u = sq2 w l m (u l) (u m) := by
      funext k
      by_cases hkl : k = l
      · subst hkl; rw [sq2_l h]
      by_cases hkm : k = m
      · subst hkm; rw [sq2_m]
      rw [sq2_other w hkl hkm, hu k hkl hkm]
    
    open Classical in
    theorem edges_sq (hG : G ≤ hypercube_graph n) {l m : Fin n} (h : l ≠ m) (w : Fin n → Bool) :
        G.edgeFinset.filter (fun e => ∀ u ∈ e,
          u ∈ Finset.univ.filter (fun u : Fin n → Bool => ∀ k, k ≠ l → k ≠ m → u k = w k)) =
        (if decide (G.Adj (sq2 w l m false false) (sq2 w l m true false)) then
            {s(sq2 w l m false false, sq2 w l m true false)} else ∅) ∪
          (if decide (G.Adj (sq2 w l m false true) (sq2 w l m true true)) then
            {s(sq2 w l m false true, sq2 w l m true true)} else ∅) ∪
          (if decide (G.Adj (sq2 w l m false false) (sq2 w l m false true)) then
            {s(sq2 w l m false false, sq2 w l m false true)} else ∅) ∪
          (if decide (G.Adj (sq2 w l m true false) (sq2 w l m true true)) then
            {s(sq2 w l m true false, sq2 w l m true true)} else ∅) := by
      have d : ∀ a b a' b' : Bool, (a, b) ≠ (a', b') → sq2 w l m a b ≠ sq2 w l m a' b' :=
        fun a b a' b' hne => sq2_ne h hne
      have d1 := d false false true false (by decide)
      have d2 := d false false false true (by decide)
      have d3 := d false false true true (by decide)
      have d4 := d true false false true (by decide)
      have d5 := d true false true true (by decide)
      have d6 := d false true true true (by decide)
      ext e
      induction e using Sym2.ind with
      | h u u' =>
      simp only [Finset.mem_filter, SimpleGraph.mem_edgeFinset, SimpleGraph.mem_edgeSet,
        Sym2.mem_iff, forall_eq_or_imp, forall_eq, Finset.mem_univ, true_and]
      constructor
      · rintro ⟨hadj, hu, hu'⟩
        obtain ⟨k, rfl⟩ := adj_eq_fl u u' (hG hadj)
        have hk : k = l ∨ k = m := by
          by_contra hk
          push Not at hk
          have h1 := hu' k hk.1 hk.2
          have h2 := hu k hk.1 hk.2
          simp only [fl, if_pos rfl] at h1
          rw [h2] at h1
          cases w k <;> simp at h1
        rw [mem_sq_eq h hu] at hadj ⊢
        generalize u l = a at hadj ⊢
        generalize u m = b at hadj ⊢
        rcases hk with rfl | rfl
        · rw [fl_sq2_l w h] at hadj ⊢
          cases a <;> cases b <;> simp only [Bool.not_true, Bool.not_false] at hadj ⊢ <;>
          simp only [Finset.mem_union] <;>
          (first
          | exact Or.inl (Or.inl (Or.inl (mem_ite_single (by simpa using hadj) (by first | rfl | exact Sym2.eq_swap))))
          | exact Or.inl (Or.inl (Or.inl (mem_ite_single (by simpa using hadj.symm) (by first | rfl | exact Sym2.eq_swap))))
          | exact Or.inl (Or.inl (Or.inr (mem_ite_single (by simpa using hadj) (by first | rfl | exact Sym2.eq_swap))))
          | exact Or.inl (Or.inl (Or.inr (mem_ite_single (by simpa using hadj.symm) (by first | rfl | exact Sym2.eq_swap))))
          | exact Or.inl (Or.inr (mem_ite_single (by simpa using hadj) (by first | rfl | exact Sym2.eq_swap)))
          | exact Or.inl (Or.inr (mem_ite_single (by simpa using hadj.symm) (by first | rfl | exact Sym2.eq_swap)))
          | exact Or.inr (mem_ite_single (by simpa using hadj) (by first | rfl | exact Sym2.eq_swap))
          | exact Or.inr (mem_ite_single (by simpa using hadj.symm) (by first | rfl | exact Sym2.eq_swap)))
        · rw [fl_sq2_m] at hadj ⊢
          cases a <;> cases b <;> simp only [Bool.not_true, Bool.not_false] at hadj ⊢ <;>
          simp only [Finset.mem_union] <;>
          (first
          | exact Or.inl (Or.inl (Or.inl (mem_ite_single (by simpa using hadj) (by first | rfl | exact Sym2.eq_swap))))
          | exact Or.inl (Or.inl (Or.inl (mem_ite_single (by simpa using hadj.symm) (by first | rfl | exact Sym2.eq_swap))))
          | exact Or.inl (Or.inl (Or.inr (mem_ite_single (by simpa using hadj) (by first | rfl | exact Sym2.eq_swap))))
          | exact Or.inl (Or.inl (Or.inr (mem_ite_single (by simpa using hadj.symm) (by first | rfl | exact Sym2.eq_swap))))
          | exact Or.inl (Or.inr (mem_ite_single (by simpa using hadj) (by first | rfl | exact Sym2.eq_swap)))
          | exact Or.inl (Or.inr (mem_ite_single (by simpa using hadj.symm) (by first | rfl | exact Sym2.eq_swap)))
          | exact Or.inr (mem_ite_single (by simpa using hadj) (by first | rfl | exact Sym2.eq_swap))
          | exact Or.inr (mem_ite_single (by simpa using hadj.symm) (by first | rfl | exact Sym2.eq_swap)))
      · intro hmem
        have hin : ∀ a b, ∀ k, k ≠ l → k ≠ m → sq2 w l m a b k = w k :=
          fun a b k h1 h2 => sq2_other w h1 h2 a b
        simp only [Finset.mem_union] at hmem
        rcases hmem with ((hm | hm) | hm) | hm <;>
        · split_ifs at hm with hA
          · rw [Finset.mem_singleton, Sym2.eq_iff] at hm
            simp only [decide_eq_true_eq] at hA
            rcases hm with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩
            · exact ⟨hA, hin _ _, hin _ _⟩
            · exact ⟨hA.symm, hin _ _, hin _ _⟩
          · simp at hm
    
    end
    
    
    theorem ite_toNat (P : Prop) [Decidable P] : (if P then (1 : ℤ) else 0) = ((decide P).toNat : ℤ) := by
      by_cases h : P <;> simp [h]
    
    set_option maxHeartbeats 1000000 in
    open Classical in
    theorem main (n : ℕ) (hn : 2 ≤ n)
        (G : SimpleGraph (Fin n → Bool)) (hG : G ≤ hypercube_graph n)
        (hC4 : (SimpleGraph.cycleGraph 4).Free G) :
        let squares : Finset (Finset (Fin n → Bool)) := Finset.univ.filter fun s =>
          ∃ i j : Fin n, i ≠ j ∧ ∃ v : Fin n → Bool,
            s = Finset.univ.filter fun u => ∀ k, k ≠ i → k ≠ j → u k = v k
        let edgesIn : Finset (Fin n → Bool) → Finset (Sym2 (Fin n → Bool)) := fun s =>
          G.edgeFinset.filter fun e => ∀ w ∈ e, w ∈ s
        let a0 : ℕ := (squares.filter fun s => edgesIn s = ∅).card
        let aopp : ℕ := (squares.filter fun s => (edgesIn s).card = 2 ∧
          ∀ e ∈ edgesIn s, ∀ f ∈ edgesIn s, e ≠ f → ∀ w ∈ e, w ∉ f).card
        ∑ v, (G.degree v : ℤ) ^ 2 =
          2 * n * G.edgeFinset.card - 2 * (n.choose 2 : ℤ) * 2 ^ (n - 2) + 2 * ((a0 : ℤ) - aopp) := by
      intro squares edgesIn a0 aopp
      let P : Finset (Fin n × Fin n) := Finset.univ.filter fun p => p.1 < p.2
      let Z : Fin n × Fin n → Finset (Fin n → Bool) := fun p =>
        Finset.univ.filter fun w => w p.1 = false ∧ w p.2 = false
      let I := P.sigma Z
      let Ψ : (Σ _ : Fin n × Fin n, Fin n → Bool) → Finset (Fin n → Bool) := fun x =>
        Finset.univ.filter fun u => ∀ k, k ≠ x.1.1 → k ≠ x.1.2 → u k = x.2 k
      have hI : ∀ x ∈ I, x.1.1 < x.1.2 ∧ x.2 x.1.1 = false ∧ x.2 x.1.2 = false := by
        intro x hx
        simp only [I, P, Z, Finset.mem_sigma, Finset.mem_filter, Finset.mem_univ, true_and] at hx
        exact ⟨hx.1, hx.2⟩
      -- the index is injective
      have hinj : Set.InjOn Ψ I := by
        rintro ⟨⟨l, m⟩, w⟩ hx ⟨⟨l', m'⟩, w'⟩ hy hxy
        obtain ⟨hlm, hwl, hwm⟩ := hI _ hx
        obtain ⟨hlm', hwl', hwm'⟩ := hI _ hy
        simp only at hlm hwl hwm hlm' hwl' hwm'
        have hmem : ∀ u : Fin n → Bool, (∀ k, k ≠ l → k ≠ m → u k = w k) ↔
            (∀ k, k ≠ l' → k ≠ m' → u k = w' k) := by
          intro u
          have := Finset.ext_iff.1 hxy u
          simpa [Ψ] using this
        have key : ∀ {a b a' b' : Fin n} {x x' : Fin n → Bool},
            (∀ u : Fin n → Bool, (∀ k, k ≠ a → k ≠ b → u k = x k) ↔
              (∀ k, k ≠ a' → k ≠ b' → u k = x' k)) → ∀ j, (j = a ∨ j = b) → (j = a' ∨ j = b') := by
          intro a b a' b' x x' h j hj
          by_contra hj'
          push Not at hj'
          have h1 := (h x).1 (fun _ _ _ => rfl) j hj'.1 hj'.2
          have h2 := (h (fl x j)).1 (by
            intro k hka hkb
            have : k ≠ j := by rintro rfl; rcases hj with rfl | rfl <;> contradiction
            simp [fl, this]) j hj'.1 hj'.2
          simp only [fl, if_pos rfl] at h2
          rw [← h1] at h2
          cases x j <;> simp at h2
        have k1 := key hmem l (Or.inl rfl)
        have k2 := key hmem m (Or.inr rfl)
        have k3 := key (fun u => (hmem u).symm) l' (Or.inl rfl)
        have k4 := key (fun u => (hmem u).symm) m' (Or.inr rfl)
        have hl : l = l' := by
          rcases k1 with h | h
          · exact h
          rcases k3 with h' | h'
          · exact h'.symm
          exfalso
          subst h; subst h'
          exact absurd (hlm.trans hlm') (lt_irrefl _)
        subst hl
        have hm : m = m' := by
          rcases k2 with h | h
          · subst h; exact absurd hlm (lt_irrefl _)
          · exact h
        subst hm
        have hw : w = w' := by
          funext k
          by_cases hkl : k = l
          · subst hkl; rw [hwl, hwl']
          by_cases hkm : k = m
          · subst hkm; rw [hwm, hwm']
          exact (hmem w).1 (fun _ _ _ => rfl) k hkl hkm
        subst hw
        rfl
      have himg : squares = I.image Ψ := by
        ext s
        simp only [squares, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image]
        constructor
        · rintro ⟨i, j, hij, v, rfl⟩
          rcases lt_or_gt_of_ne hij with h | h
          · refine ⟨⟨(i, j), sq2 v i j false false⟩, ?_, ?_⟩
            · simp only [I, P, Z, Finset.mem_sigma, Finset.mem_filter, Finset.mem_univ, true_and]
              exact ⟨h, sq2_l hij _ _, sq2_m _ _ _ _ _⟩
            · ext u
              simp only [Ψ, Finset.mem_filter, Finset.mem_univ, true_and]
              refine forall_congr' fun k => imp_congr_right fun hki => imp_congr_right fun hkj => ?_
              rw [sq2_other v hki hkj]
          · refine ⟨⟨(j, i), sq2 v j i false false⟩, ?_, ?_⟩
            · simp only [I, P, Z, Finset.mem_sigma, Finset.mem_filter, Finset.mem_univ, true_and]
              exact ⟨h, sq2_l hij.symm _ _, sq2_m _ _ _ _ _⟩
            · ext u
              simp only [Ψ, Finset.mem_filter, Finset.mem_univ, true_and]
              constructor
              · intro H k hki hkj
                rw [H k hkj hki, sq2_other v hkj hki]
              · intro H k hkj hki
                rw [H k hki hkj, sq2_other v hkj hki]
        · rintro ⟨x, hx, rfl⟩
          obtain ⟨hlt, _, _⟩ := hI x hx
          exact ⟨x.1.1, x.1.2, hlt.ne, x.2, rfl⟩
      have hcardI : ((I.card : ℕ) : ℤ) = (n.choose 2 : ℤ) * 2 ^ (n - 2) := by
        have h := hypercube_coordinate_square_count n hn
        have h2 : squares.card = I.card := by rw [himg, Finset.card_image_of_injOn hinj]
        rw [← h2]
        exact_mod_cast h
      have hfilt : ∀ Q : Finset (Fin n → Bool) → Prop, ((squares.filter Q).card : ℤ) =
          ∑ x ∈ I, if Q (Ψ x) then (1 : ℤ) else 0 := by
        intro Q
        rw [Finset.sum_boole, himg, Finset.filter_image,
          Finset.card_image_of_injOn (hinj.mono (Finset.coe_subset.2 (Finset.filter_subset _ _)))]
      -- local identity on each square
      have hloc : ∀ x ∈ I,
          (eA G x.2 x.1.1 x.1.2 + eB G x.2 x.1.1 x.1.2) * (eC G x.2 x.1.1 x.1.2 + eD G x.2 x.1.1 x.1.2) =
            (eA G x.2 x.1.1 x.1.2 + eB G x.2 x.1.1 x.1.2 + eC G x.2 x.1.1 x.1.2 +
              eD G x.2 x.1.1 x.1.2) - 1 + (if edgesIn (Ψ x) = ∅ then 1 else 0) -
              (if (edgesIn (Ψ x)).card = 2 ∧ ∀ e ∈ edgesIn (Ψ x), ∀ f ∈ edgesIn (Ψ x), e ≠ f →
                ∀ w ∈ e, w ∉ f then 1 else 0) := by
        rintro ⟨⟨l, m⟩, w⟩ hx
        obtain ⟨hlm, -, -⟩ := hI _ hx
        have h : l ≠ m := hlm.ne
        simp only at h ⊢
        have hE : edgesIn (Ψ ⟨(l, m), w⟩) = _ := edges_sq G hG h w
        have d : ∀ a b a' b' : Bool, (a, b) ≠ (a', b') → sq2 w l m a b ≠ sq2 w l m a' b' :=
          fun a b a' b' hne => sq2_ne h hne
        have h4 : ¬ (decide (G.Adj (sq2 w l m false false) (sq2 w l m true false)) = true ∧
            decide (G.Adj (sq2 w l m false true) (sq2 w l m true true)) = true ∧
            decide (G.Adj (sq2 w l m false false) (sq2 w l m false true)) = true ∧
            decide (G.Adj (sq2 w l m true false) (sq2 w l m true true)) = true) := by
          simp only [decide_eq_true_eq]
          rintro ⟨h1, h2, h3, h4⟩
          exact c4_of (d false false true false (by decide)) (d false false false true (by decide)) (d false false true true (by decide))
            (d true false false true (by decide)) (d true false true true (by decide)) (d false true true true (by decide)) h1 h2 h3 h4 hC4
        have Q := (quad_facts (d false false true false (by decide)) (d false false false true (by decide)) (d false false true true (by decide))
            (d true false false true (by decide)) (d true false true true (by decide)) (d false true true true (by decide)) _ _ _ _ h4).1
        rw [hE]
        simp only [eA, eB, eC, eD]
        rw [ite_toNat (G.Adj (sq2 w l m false false) (sq2 w l m true false)),
          ite_toNat (G.Adj (sq2 w l m false true) (sq2 w l m true true)),
          ite_toNat (G.Adj (sq2 w l m false false) (sq2 w l m false true)),
          ite_toNat (G.Adj (sq2 w l m true false) (sq2 w l m true true))]
        convert Q using 3
      -- global counts
      have hx01 : ∀ v l, xl G v l * xl G v l = xl G v l := by
        intro v l; unfold xl; split_ifs <;> norm_num
      have hdeg : ∀ v, (G.degree v : ℤ) = ∑ l, xl G v l := fun v => degree_eq G hG v
      have hsumdeg : ∑ v, (G.degree v : ℤ) = 2 * G.edgeFinset.card := by
        exact_mod_cast G.sum_degrees_eq_twice_card_edges
      have hsplitI : ∀ F : (Σ _ : Fin n × Fin n, Fin n → Bool) → ℤ,
          ∑ x ∈ I, F x = ∑ p : Fin n × Fin n, if p.1 < p.2 then ∑ w ∈ Z p, F ⟨p, w⟩ else 0 := by
        intro F
        rw [Finset.sum_sigma, Finset.sum_filter]
      have e1 : ∑ v, ∑ l, xl G v l = 2 * (G.edgeFinset.card : ℤ) := by
        rw [← hsumdeg]; exact Finset.sum_congr rfl (fun v _ => (hdeg v).symm)
      have hcher : ∑ v, (G.degree v : ℤ) ^ 2 = 2 * G.edgeFinset.card + 2 * ∑ x ∈ I,
          (eA G x.2 x.1.1 x.1.2 + eB G x.2 x.1.1 x.1.2) * (eC G x.2 x.1.1 x.1.2 + eD G x.2 x.1.1 x.1.2) := by
        have e2 : ∑ v, ∑ p : Fin n × Fin n, (if p.1 < p.2 then xl G v p.1 * xl G v p.2 else 0) =
            ∑ x ∈ I, (eA G x.2 x.1.1 x.1.2 + eB G x.2 x.1.1 x.1.2) *
              (eC G x.2 x.1.1 x.1.2 + eD G x.2 x.1.1 x.1.2) := by
          rw [hsplitI, Finset.sum_comm]
          refine Finset.sum_congr rfl fun p _ => ?_
          by_cases hp : p.1 < p.2
          · simp only [hp, ↓reduceIte]
            exact pair_prod G hp.ne
          · simp [hp]
        rw [Finset.sum_congr rfl (fun v _ => by rw [hdeg v, sq_sum01 (xl G v) (hx01 v)]),
          Finset.sum_add_distrib, ← Finset.mul_sum, e1, e2]
      have hk : 2 * ∑ x ∈ I, (eA G x.2 x.1.1 x.1.2 + eB G x.2 x.1.1 x.1.2 + eC G x.2 x.1.1 x.1.2 +
          eD G x.2 x.1.1 x.1.2) = ((n : ℤ) - 1) * (2 * G.edgeFinset.card) := by
        rw [hsplitI, Finset.mul_sum, ← hsumdeg]
        simp only [hdeg]
        rw [Finset.sum_comm (s := Finset.univ) (t := Finset.univ), ← sum_pairs]
        refine Finset.sum_congr rfl fun p _ => ?_
        split_ifs with hp
        · rw [pair_l G hp.ne, pair_m G hp.ne, ← Finset.sum_add_distrib, Finset.mul_sum]
          refine Finset.sum_congr rfl fun w _ => ?_
          ring
        · simp
      have ha0 : (a0 : ℤ) = ∑ x ∈ I, if edgesIn (Ψ x) = ∅ then (1 : ℤ) else 0 := by
        convert hfilt (fun s => edgesIn s = ∅)
      have haopp : (aopp : ℤ) = ∑ x ∈ I, if (edgesIn (Ψ x)).card = 2 ∧ ∀ e ∈ edgesIn (Ψ x),
          ∀ f ∈ edgesIn (Ψ x), e ≠ f → ∀ w ∈ e, w ∉ f then (1 : ℤ) else 0 := by
        convert hfilt (fun s => (edgesIn s).card = 2 ∧ ∀ e ∈ edgesIn s, ∀ f ∈ edgesIn s, e ≠ f → ∀ w ∈ e, w ∉ f)
      rw [hcher, Finset.sum_congr rfl hloc, Finset.sum_sub_distrib, Finset.sum_add_distrib,
        Finset.sum_sub_distrib, Finset.sum_const, ← ha0, ← haopp]
      simp only [nsmul_eq_mul, mul_one]
      rw [hcardI]
      linarith [hk]
    
    end In1264
    
    open Classical in
    theorem c4_free_hypercube_subgraph_degree_square_sum (n : ℕ) (hn : 2 ≤ n)
        (G : SimpleGraph (Fin n → Bool)) (hG : G ≤ hypercube_graph n)
        (hC4 : (SimpleGraph.cycleGraph 4).Free G) :
        let squares : Finset (Finset (Fin n → Bool)) := Finset.univ.filter fun s =>
          ∃ i j : Fin n, i ≠ j ∧ ∃ v : Fin n → Bool,
            s = Finset.univ.filter fun u => ∀ k, k ≠ i → k ≠ j → u k = v k
        let edgesIn : Finset (Fin n → Bool) → Finset (Sym2 (Fin n → Bool)) := fun s =>
          G.edgeFinset.filter fun e => ∀ w ∈ e, w ∈ s
        let a0 : ℕ := (squares.filter fun s => edgesIn s = ∅).card
        let aopp : ℕ := (squares.filter fun s => (edgesIn s).card = 2 ∧
          ∀ e ∈ edgesIn s, ∀ f ∈ edgesIn s, e ≠ f → ∀ w ∈ e, w ∉ f).card
        ∑ v, (G.degree v : ℤ) ^ 2 =
          2 * n * G.edgeFinset.card - 2 * (n.choose 2 : ℤ) * 2 ^ (n - 2) + 2 * ((a0 : ℤ) - aopp) := by
      exact In1264.main n hn G hG hC4

Written by a model (gemini-3.8-flash) from the Lean proof and the goal states Lean reported at each step. Lean verified the proof; this explanation is a reading aid and is not itself machine-checked.

Lean source view module on GitHub

Lean’s trace was cut to the first 400 of 671 tactics.

namespace In1264variable {n : ℕ}def fl (v : Fin n → Bool) (l : Fin n) : Fin n → Bool := fun k => if k = l then !v k else v kdef sq2 (w : Fin n → Bool) (l m : Fin n) (a b : Bool) : Fin n → Bool :=  Function.update (Function.update w l a) m btheorem sq2_l {w : Fin n → Bool} {l m : Fin n} (h : l ≠ m) (a b : Bool) : sq2 w l m a b l = a := by  simp [sq2, Function.update_apply, h]theorem sq2_m (w : Fin n → Bool) (l m : Fin n) (a b : Bool) : sq2 w l m a b m = b := by  simp [sq2]theorem sq2_other (w : Fin n → Bool) {l m k : Fin n} (hl : k ≠ l) (hm : k ≠ m) (a b : Bool) :    sq2 w l m a b k = w k := by  simp [sq2, Function.update_apply, hl, hm]theorem fl_sq2_l (w : Fin n → Bool) {l m : Fin n} (h : l ≠ m) (a b : Bool) :    fl (sq2 w l m a b) l = sq2 w l m (!a) b := by  funext k  simp only [fl, sq2, Function.update_apply]  by_cases hk : k = l  · subst hk; simp [h]  · simp [hk]theorem fl_sq2_m (w : Fin n → Bool) {l m : Fin n} (a b : Bool) :    fl (sq2 w l m a b) m = sq2 w l m a (!b) := by  funext k  simp only [fl, sq2, Function.update_apply]  by_cases hk : k = m  · subst hk; simp  · simp [hk]theorem sq2_ne {w : Fin n → Bool} {l m : Fin n} (h : l ≠ m) {a b a' b' : Bool}    (hne : (a, b) ≠ (a', b')) : sq2 w l m a b ≠ sq2 w l m a' b' := by  intro he  apply hne  have h1 := congrFun he l  have h2 := congrFun he m  rw [sq2_l h, sq2_l h] at h1  rw [sq2_m, sq2_m] at h2  rw [h1, h2]/-- the vertex set splits along two coordinates -/theorem sum_split {l m : Fin n} (h : l ≠ m) (f : (Fin n → Bool) → ℤ) :    ∑ v, f v = ∑ w ∈ Finset.univ.filter (fun w : Fin n → Bool => w l = false ∧ w m = false),      ∑ a : Bool, ∑ b : Bool, f (sq2 w l m a b) := by  rw [← Finset.sum_product', ← Finset.sum_product']  refine Finset.sum_nbij' (fun v => ((sq2 v l m false false, v l), v m))    (fun x => sq2 x.1.1 l m x.1.2 x.2) ?_ ?_ ?_ ?_ ?_  · intro v _    simp [sq2_l h, sq2_m]  · intro _ _; exact Finset.mem_univ _  · intro v _    funext k    simp only [sq2, Function.update_apply]    split_ifs <;> simp_all  · rintro ⟨⟨w, a⟩, b⟩ hx    simp only [Finset.mem_product, Finset.mem_filter, Finset.mem_univ, true_and, and_true] at hx    simp only [sq2_l h, sq2_m, Prod.mk.injEq, and_true]    funext k    simp only [sq2, Function.update_apply]    split_ifs <;> simp_all  · intro v _    congr 1    funext k    simp only [sq2, Function.update_apply]    split_ifs <;> simp_allopen Classical intheorem quad_facts {α : Type*} [DecidableEq α] {p q r s : α} (hpq : p ≠ q) (hpr : p ≠ r)    (hps : p ≠ s) (hqr : q ≠ r) (hqs : q ≠ s) (hrs : r ≠ s) (A B C D : Bool)    (h4 : ¬ (A = true ∧ B = true ∧ C = true ∧ D = true)) :    let E : Finset (Sym2 α) := (if A then {s(p, q)} else ∅) ∪ (if B then {s(r, s)} else ∅) ∪      (if C then {s(p, r)} else ∅) ∪ (if D then {s(q, s)} else ∅)    ((A.toNat + B.toNat : ℤ) * (C.toNat + D.toNat)) =      (A.toNat + B.toNat + C.toNat + D.toNat : ℤ) - 1 + (if E = ∅ then 1 else 0) -        (if E.card = 2 ∧ ∀ e ∈ E, ∀ f ∈ E, e ≠ f → ∀ w ∈ e, w ∉ f then 1 else 0) ∧    (E.card : ℤ) = A.toNat + B.toNat + C.toNat + D.toNat := by  intro E  cases A <;> cases B <;> cases C <;> cases D <;>    simp_all [E, Finset.card_insert_of_notMem, Sym2.eq_iff, hpq.symm, hpr.symm, hps.symm,      hqr.symm, hqs.symm, hrs.symm]theorem adj_eq_fl (v u : Fin n → Bool) (h : (hypercube_graph n).Adj v u) :    ∃ l, u = fl v l := by  have h1 : hammingDist v u = 1 := h  unfold hammingDist at h1  obtain ⟨l, hl⟩ := Finset.card_eq_one.1 h1  refine ⟨l, funext fun m => ?_⟩  have hm := Finset.ext_iff.1 hl m  simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_singleton] at hm  simp only [fl]  by_cases hml : m = l  · rw [if_pos hml]    have := hm.2 hml    cases hv : v m <;> cases hu : u m <;> simp_all  · rw [if_neg hml]    have := mt hm.1 hml    simp only [ne_eq, not_not] at this    exact this.symmtheorem fl_injective (v : Fin n → Bool) : Function.Injective (fl v) := by  intro l l' h  by_contra hne  have := congrFun h l  simp [fl, hne] at thisopen Classical intheorem degree_eq (G : SimpleGraph (Fin n → Bool)) (hG : G ≤ hypercube_graph n)    (v : Fin n → Bool) :    (G.degree v : ℤ) = ∑ l, if G.Adj v (fl v l) then (1 : ℤ) else 0 := by  have hd : G.degree v = (Finset.univ.filter fun l => G.Adj v (fl v l)).card := by    rw [← SimpleGraph.card_neighborFinset_eq_degree]    have : G.neighborFinset v =        (Finset.univ.filter fun l => G.Adj v (fl v l)).image (fl v) := by      ext u      simp only [SimpleGraph.mem_neighborFinset, Finset.mem_image, Finset.mem_filter,        Finset.mem_univ, true_and]      constructor      · intro h        obtain ⟨l, rfl⟩ := adj_eq_fl v u (hG h)        exact ⟨l, h, rfl⟩      · rintro ⟨l, hl, rfl⟩        exact hl    rw [this, Finset.card_image_of_injective _ (fl_injective v)]  rw [hd, Finset.card_filter]  push_cast  rfl/-- squares of 0/1 sums -/theorem sq_sum01 (x : Fin n → ℤ) (hx : ∀ l, x l * x l = x l) :    (∑ l, x l) ^ 2 = ∑ l, x l + 2 * ∑ p : Fin n × Fin n,      if p.1 < p.2 then x p.1 * x p.2 else 0 := by  have h1 : (∑ l, x l) ^ 2 = ∑ p : Fin n × Fin n, x p.1 * x p.2 := by    rw [sq, Finset.sum_mul_sum, ← Finset.sum_product']; rfl  have h2 : ∀ p : Fin n × Fin n, x p.1 * x p.2 = (if p.1 < p.2 then x p.1 * x p.2 else 0) +      (if p.1 = p.2 then x p.1 * x p.2 else 0) + (if p.2 < p.1 then x p.1 * x p.2 else 0) := by    intro p    rcases lt_trichotomy p.1 p.2 with h | h | h    · simp [h, h.ne, not_lt.2 h.le]    · simp [h]    · simp [h, h.ne', not_lt.2 h.le]  have h3 : ∑ p : Fin n × Fin n, (if p.2 < p.1 then x p.1 * x p.2 else 0) =      ∑ p : Fin n × Fin n, (if p.1 < p.2 then x p.1 * x p.2 else 0) := by    refine Fintype.sum_equiv (Equiv.prodComm _ _) _ _ ?_    intro p; simp [mul_comm]  have h4 : ∑ p : Fin n × Fin n, (if p.1 = p.2 then x p.1 * x p.2 else 0) = ∑ l, x l := by    rw [Fintype.sum_prod_type]    refine Finset.sum_congr rfl fun l _ => ?_    rw [Finset.sum_ite_eq]; simp [hx]  rw [h1, Finset.sum_congr rfl (fun p _ => h2 p), Finset.sum_add_distrib, Finset.sum_add_distrib,    h3, h4]  ringtheorem sum_pairs (T : Fin n → ℤ) :    ∑ p : Fin n × Fin n, (if p.1 < p.2 then T p.1 + T p.2 else 0) = ((n : ℤ) - 1) * ∑ l, T l := by  have h1 : ∀ p : Fin n × Fin n, (if p.1 < p.2 then T p.1 + T p.2 else 0) =      (if p.1 < p.2 then T p.1 else 0) + (if p.1 < p.2 then T p.2 else 0) := by    intro p; by_cases h : p.1 < p.2 <;> simp [h]  have h2 : ∑ p : Fin n × Fin n, (if p.1 < p.2 then T p.2 else 0) =      ∑ p : Fin n × Fin n, (if p.2 < p.1 then T p.1 else 0) := by    refine Fintype.sum_equiv (Equiv.prodComm _ _) _ _ ?_    intro p; rfl  have h3 : ∀ p : Fin n × Fin n, (if p.1 < p.2 then T p.1 else 0) +      (if p.2 < p.1 then T p.1 else 0) = T p.1 - (if p.1 = p.2 then T p.1 else 0) := by    intro p    rcases lt_trichotomy p.1 p.2 with h | h | h    · simp [h, h.ne, not_lt.2 h.le]    · simp [h]    · simp [h, h.ne', not_lt.2 h.le]  rw [Finset.sum_congr rfl (fun p _ => h1 p), Finset.sum_add_distrib, h2,    ← Finset.sum_add_distrib, Finset.sum_congr rfl (fun p _ => h3 p), Finset.sum_sub_distrib,    Fintype.sum_prod_type, Fintype.sum_prod_type]  have h4 : ∀ l : Fin n, ∑ m : Fin n, (if l = m then T l else 0) = T l := by    intro l; rw [Finset.sum_ite_eq]; simp  simp only [h4, Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul]  rw [Finset.mul_sum, ← Finset.sum_sub_distrib]  refine Finset.sum_congr rfl fun l _ => ?_  ringtheorem c4adj : ∀ i j : Fin 4, (SimpleGraph.cycleGraph 4).Adj i j →    (i = 0 ∧ j = 1) ∨ (i = 1 ∧ j = 0) ∨ (i = 1 ∧ j = 2) ∨ (i = 2 ∧ j = 1) ∨    (i = 2 ∧ j = 3) ∨ (i = 3 ∧ j = 2) ∨ (i = 3 ∧ j = 0) ∨ (i = 0 ∧ j = 3) := by  intro i j  rw [SimpleGraph.cycleGraph_adj']  revert i j; decidetheorem c4_of {α : Type*} {G : SimpleGraph α} {p q r s : α} (hpq : p ≠ q) (hpr : p ≠ r)    (hps : p ≠ s) (hqr : q ≠ r) (hqs : q ≠ s) (hrs : r ≠ s)    (h1 : G.Adj p q) (h2 : G.Adj r s) (h3 : G.Adj p r) (h4 : G.Adj q s) :    ¬ (SimpleGraph.cycleGraph 4).Free G := by  intro hF  apply hF  refine ⟨⟨⟨![p, q, s, r], ?_⟩, ?_⟩⟩  · intro i j hij    rcases c4adj i j hij with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ |      ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ <;> simp <;>      first | exact h1 | exact h1.symm | exact h2 | exact h2.symm | exact h3 | exact h3.symm | exact h4 | exact h4.symm  · intro i j hij    fin_cases i <;> fin_cases j <;> simp_all [eq_comm]sectionvariable (G : SimpleGraph (Fin n → Bool))open Classical innoncomputable def xl (v : Fin n → Bool) (l : Fin n) : ℤ := if G.Adj v (fl v l) then 1 else 0open Classical innoncomputable def eA (w : Fin n → Bool) (l m : Fin n) : ℤ :=  if G.Adj (sq2 w l m false false) (sq2 w l m true false) then 1 else 0open Classical innoncomputable def eB (w : Fin n → Bool) (l m : Fin n) : ℤ :=  if G.Adj (sq2 w l m false true) (sq2 w l m true true) then 1 else 0open Classical innoncomputable def eC (w : Fin n → Bool) (l m : Fin n) : ℤ :=  if G.Adj (sq2 w l m false false) (sq2 w l m false true) then 1 else 0open Classical innoncomputable def eD (w : Fin n → Bool) (l m : Fin n) : ℤ :=  if G.Adj (sq2 w l m true false) (sq2 w l m true true) then 1 else 0theorem pair_prod {l m : Fin n} (h : l ≠ m) :    ∑ v, xl G v l * xl G v m = ∑ w ∈ Finset.univ.filter (fun w : Fin n → Bool =>      w l = false ∧ w m = false), (eA G w l m + eB G w l m) * (eC G w l m + eD G w l m) := by  rw [sum_split h]  refine Finset.sum_congr rfl fun w _ => ?_  simp only [Fintype.sum_bool, xl, eA, eB, eC, eD]  rw [fl_sq2_l w h true true, fl_sq2_l w h true false, fl_sq2_l w h false true, fl_sq2_l w h false false, fl_sq2_m w (l := l) (m := m) true true, fl_sq2_m w (l := l) (m := m) true false, fl_sq2_m w (l := l) (m := m) false true, fl_sq2_m w (l := l) (m := m) false false, Bool.not_true, Bool.not_false]  rw [G.adj_comm (sq2 w l m true true) (sq2 w l m false true),    G.adj_comm (sq2 w l m true true) (sq2 w l m true false),    G.adj_comm (sq2 w l m true false) (sq2 w l m false false),    G.adj_comm (sq2 w l m false true) (sq2 w l m false false)]  split_ifs <;> ringtheorem pair_l {l m : Fin n} (h : l ≠ m) :    ∑ v, xl G v l = ∑ w ∈ Finset.univ.filter (fun w : Fin n → Bool =>      w l = false ∧ w m = false), 2 * (eA G w l m + eB G w l m) := by  rw [sum_split h]  refine Finset.sum_congr rfl fun w _ => ?_  simp only [Fintype.sum_bool, xl, eA, eB]  rw [fl_sq2_l w h true true, fl_sq2_l w h true false, fl_sq2_l w h false true, fl_sq2_l w h false false, Bool.not_true, Bool.not_false,    G.adj_comm (sq2 w l m true true) (sq2 w l m false true),    G.adj_comm (sq2 w l m true false) (sq2 w l m false false)]  split_ifs <;> ringtheorem pair_m {l m : Fin n} (h : l ≠ m) :    ∑ v, xl G v m = ∑ w ∈ Finset.univ.filter (fun w : Fin n → Bool =>      w l = false ∧ w m = false), 2 * (eC G w l m + eD G w l m) := by  rw [sum_split h]  refine Finset.sum_congr rfl fun w _ => ?_  simp only [Fintype.sum_bool, xl, eC, eD]  rw [fl_sq2_m w (l := l) (m := m) true true, fl_sq2_m w (l := l) (m := m) true false, fl_sq2_m w (l := l) (m := m) false true, fl_sq2_m w (l := l) (m := m) false false, Bool.not_true, Bool.not_false,    G.adj_comm (sq2 w l m true true) (sq2 w l m true false),    G.adj_comm (sq2 w l m false true) (sq2 w l m false false)]  split_ifs <;> ringtheorem mem_ite_single {α : Type*} [DecidableEq α] {c : Prop} [Decidable c] {x y : α}    (hc : c) (h : x = y) : x ∈ (if c then {y} else ∅ : Finset α) := by  rw [if_pos hc]; exact Finset.mem_singleton.2 htheorem mem_sq_eq {w u : Fin n → Bool} {l m : Fin n} (h : l ≠ m)    (hu : ∀ k, k ≠ l → k ≠ m → u k = w k) : u = sq2 w l m (u l) (u m) := by  funext k  by_cases hkl : k = l  · subst hkl; rw [sq2_l h]  by_cases hkm : k = m  · subst hkm; rw [sq2_m]  rw [sq2_other w hkl hkm, hu k hkl hkm]open Classical intheorem edges_sq (hG : G ≤ hypercube_graph n) {l m : Fin n} (h : l ≠ m) (w : Fin n → Bool) :    G.edgeFinset.filter (fun e => ∀ u ∈ e,      u ∈ Finset.univ.filter (fun u : Fin n → Bool => ∀ k, k ≠ l → k ≠ m → u k = w k)) =    (if decide (G.Adj (sq2 w l m false false) (sq2 w l m true false)) then        {s(sq2 w l m false false, sq2 w l m true false)} else ∅) ∪      (if decide (G.Adj (sq2 w l m false true) (sq2 w l m true true)) then        {s(sq2 w l m false true, sq2 w l m true true)} else ∅) ∪      (if decide (G.Adj (sq2 w l m false false) (sq2 w l m false true)) then        {s(sq2 w l m false false, sq2 w l m false true)} else ∅) ∪      (if decide (G.Adj (sq2 w l m true false) (sq2 w l m true true)) then        {s(sq2 w l m true false, sq2 w l m true true)} else ∅) := by  have d : ∀ a b a' b' : Bool, (a, b) ≠ (a', b') → sq2 w l m a b ≠ sq2 w l m a' b' :=    fun a b a' b' hne => sq2_ne h hne  have d1 := d false false true false (by decide)  have d2 := d false false false true (by decide)  have d3 := d false false true true (by decide)  have d4 := d true false false true (by decide)  have d5 := d true false true true (by decide)  have d6 := d false true true true (by decide)  ext e  induction e using Sym2.ind with  | h u u' =>  simp only [Finset.mem_filter, SimpleGraph.mem_edgeFinset, SimpleGraph.mem_edgeSet,    Sym2.mem_iff, forall_eq_or_imp, forall_eq, Finset.mem_univ, true_and]  constructor  · rintro ⟨hadj, hu, hu'⟩    obtain ⟨k, rfl⟩ := adj_eq_fl u u' (hG hadj)    have hk : k = l ∨ k = m := by      by_contra hk      push Not at hk      have h1 := hu' k hk.1 hk.2      have h2 := hu k hk.1 hk.2      simp only [fl, if_pos rfl] at h1      rw [h2] at h1      cases w k <;> simp at h1    rw [mem_sq_eq h hu] at hadj ⊢    generalize u l = a at hadj ⊢    generalize u m = b at hadj ⊢    rcases hk with rfl | rfl    · rw [fl_sq2_l w h] at hadj ⊢      cases a <;> cases b <;> simp only [Bool.not_true, Bool.not_false] at hadj ⊢ <;>      simp only [Finset.mem_union] <;>      (first      | exact Or.inl (Or.inl (Or.inl (mem_ite_single (by simpa using hadj) (by first | rfl | exact Sym2.eq_swap))))      | exact Or.inl (Or.inl (Or.inl (mem_ite_single (by simpa using hadj.symm) (by first | rfl | exact Sym2.eq_swap))))      | exact Or.inl (Or.inl (Or.inr (mem_ite_single (by simpa using hadj) (by first | rfl | exact Sym2.eq_swap))))      | exact Or.inl (Or.inl (Or.inr (mem_ite_single (by simpa using hadj.symm) (by first | rfl | exact Sym2.eq_swap))))      | exact Or.inl (Or.inr (mem_ite_single (by simpa using hadj) (by first | rfl | exact Sym2.eq_swap)))      | exact Or.inl (Or.inr (mem_ite_single (by simpa using hadj.symm) (by first | rfl | exact Sym2.eq_swap)))      | exact Or.inr (mem_ite_single (by simpa using hadj) (by first | rfl | exact Sym2.eq_swap))      | exact Or.inr (mem_ite_single (by simpa using hadj.symm) (by first | rfl | exact Sym2.eq_swap)))    · rw [fl_sq2_m] at hadj ⊢      cases a <;> cases b <;> simp only [Bool.not_true, Bool.not_false] at hadj ⊢ <;>      simp only [Finset.mem_union] <;>      (first      | exact Or.inl (Or.inl (Or.inl (mem_ite_single (by simpa using hadj) (by first | rfl | exact Sym2.eq_swap))))      | exact Or.inl (Or.inl (Or.inl (mem_ite_single (by simpa using hadj.symm) (by first | rfl | exact Sym2.eq_swap))))      | exact Or.inl (Or.inl (Or.inr (mem_ite_single (by simpa using hadj) (by first | rfl | exact Sym2.eq_swap))))      | exact Or.inl (Or.inl (Or.inr (mem_ite_single (by simpa using hadj.symm) (by first | rfl | exact Sym2.eq_swap))))      | exact Or.inl (Or.inr (mem_ite_single (by simpa using hadj) (by first | rfl | exact Sym2.eq_swap)))      | exact Or.inl (Or.inr (mem_ite_single (by simpa using hadj.symm) (by first | rfl | exact Sym2.eq_swap)))      | exact Or.inr (mem_ite_single (by simpa using hadj) (by first | rfl | exact Sym2.eq_swap))      | exact Or.inr (mem_ite_single (by simpa using hadj.symm) (by first | rfl | exact Sym2.eq_swap)))  · intro hmem    have hin : ∀ a b, ∀ k, k ≠ l → k ≠ m → sq2 w l m a b k = w k :=      fun a b k h1 h2 => sq2_other w h1 h2 a b    simp only [Finset.mem_union] at hmem    rcases hmem with ((hm | hm) | hm) | hm <;>    · split_ifs at hm with hA      · rw [Finset.mem_singleton, Sym2.eq_iff] at hm        simp only [decide_eq_true_eq] at hA        rcases hm with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩        · exact ⟨hA, hin _ _, hin _ _⟩        · exact ⟨hA.symm, hin _ _, hin _ _⟩      · simp at hmendtheorem ite_toNat (P : Prop) [Decidable P] : (if P then (1 : ℤ) else 0) = ((decide P).toNat : ℤ) := by  by_cases h : P <;> simp [h]set_option maxHeartbeats 1000000 inopen Classical intheorem main (n : ℕ) (hn : 2 ≤ n)    (G : SimpleGraph (Fin n → Bool)) (hG : G ≤ hypercube_graph n)    (hC4 : (SimpleGraph.cycleGraph 4).Free G) :    let squares : Finset (Finset (Fin n → Bool)) := Finset.univ.filter fun s =>      ∃ i j : Fin n, i ≠ j ∧ ∃ v : Fin n → Bool,        s = Finset.univ.filter fun u => ∀ k, k ≠ i → k ≠ j → u k = v k    let edgesIn : Finset (Fin n → Bool) → Finset (Sym2 (Fin n → Bool)) := fun s =>      G.edgeFinset.filter fun e => ∀ w ∈ e, w ∈ s    let a0 : ℕ := (squares.filter fun s => edgesIn s = ∅).card    let aopp : ℕ := (squares.filter fun s => (edgesIn s).card = 2 ∧      ∀ e ∈ edgesIn s, ∀ f ∈ edgesIn s, e ≠ f → ∀ w ∈ e, w ∉ f).card    ∑ v, (G.degree v : ℤ) ^ 2 =      2 * n * G.edgeFinset.card - 2 * (n.choose 2 : ℤ) * 2 ^ (n - 2) + 2 * ((a0 : ℤ) - aopp) := by  intro squares edgesIn a0 aopp  let P : Finset (Fin n × Fin n) := Finset.univ.filter fun p => p.1 < p.2  let Z : Fin n × Fin n → Finset (Fin n → Bool) := fun p =>    Finset.univ.filter fun w => w p.1 = false ∧ w p.2 = false  let I := P.sigma Z  let Ψ : (Σ _ : Fin n × Fin n, Fin n → Bool) → Finset (Fin n → Bool) := fun x =>    Finset.univ.filter fun u => ∀ k, k ≠ x.1.1 → k ≠ x.1.2 → u k = x.2 k  have hI : ∀ x ∈ I, x.1.1 < x.1.2 ∧ x.2 x.1.1 = false ∧ x.2 x.1.2 = false := by    intro x hx    simp only [I, P, Z, Finset.mem_sigma, Finset.mem_filter, Finset.mem_univ, true_and] at hx    exact ⟨hx.1, hx.2⟩  -- the index is injective  have hinj : Set.InjOn Ψ I := by    rintro ⟨⟨l, m⟩, w⟩ hx ⟨⟨l', m'⟩, w'⟩ hy hxy    obtain ⟨hlm, hwl, hwm⟩ := hI _ hx    obtain ⟨hlm', hwl', hwm'⟩ := hI _ hy    simp only at hlm hwl hwm hlm' hwl' hwm'    have hmem : ∀ u : Fin n → Bool, (∀ k, k ≠ l → k ≠ m → u k = w k) ↔        (∀ k, k ≠ l' → k ≠ m' → u k = w' k) := by      intro u      have := Finset.ext_iff.1 hxy u      simpa [Ψ] using this    have key : ∀ {a b a' b' : Fin n} {x x' : Fin n → Bool},        (∀ u : Fin n → Bool, (∀ k, k ≠ a → k ≠ b → u k = x k) ↔          (∀ k, k ≠ a' → k ≠ b' → u k = x' k)) → ∀ j, (j = a ∨ j = b) → (j = a' ∨ j = b') := by      intro a b a' b' x x' h j hj      by_contra hj'      push Not at hj'      have h1 := (h x).1 (fun _ _ _ => rfl) j hj'.1 hj'.2      have h2 := (h (fl x j)).1 (by        intro k hka hkb        have : k ≠ j := by rintro rfl; rcases hj with rfl | rfl <;> contradiction        simp [fl, this]) j hj'.1 hj'.2      simp only [fl, if_pos rfl] at h2      rw [← h1] at h2      cases x j <;> simp at h2    have k1 := key hmem l (Or.inl rfl)    have k2 := key hmem m (Or.inr rfl)    have k3 := key (fun u => (hmem u).symm) l' (Or.inl rfl)    have k4 := key (fun u => (hmem u).symm) m' (Or.inr rfl)    have hl : l = l' := by      rcases k1 with h | h      · exact h      rcases k3 with h' | h'      · exact h'.symm      exfalso      subst h; subst h'      exact absurd (hlm.trans hlm') (lt_irrefl _)    subst hl    have hm : m = m' := by      rcases k2 with h | h      · subst h; exact absurd hlm (lt_irrefl _)      · exact h    subst hm    have hw : w = w' := by      funext k      by_cases hkl : k = l      · subst hkl; rw [hwl, hwl']      by_cases hkm : k = m      · subst hkm; rw [hwm, hwm']      exact (hmem w).1 (fun _ _ _ => rfl) k hkl hkm    subst hw    rfl  have himg : squares = I.image Ψ := by    ext s    simp only [squares, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image]    constructor    · rintro ⟨i, j, hij, v, rfl⟩      rcases lt_or_gt_of_ne hij with h | h      · refine ⟨⟨(i, j), sq2 v i j false false⟩, ?_, ?_⟩        · simp only [I, P, Z, Finset.mem_sigma, Finset.mem_filter, Finset.mem_univ, true_and]          exact ⟨h, sq2_l hij _ _, sq2_m _ _ _ _ _⟩        · ext u          simp only [Ψ, Finset.mem_filter, Finset.mem_univ, true_and]          refine forall_congr' fun k => imp_congr_right fun hki => imp_congr_right fun hkj => ?_          rw [sq2_other v hki hkj]      · refine ⟨⟨(j, i), sq2 v j i false false⟩, ?_, ?_⟩        · simp only [I, P, Z, Finset.mem_sigma, Finset.mem_filter, Finset.mem_univ, true_and]          exact ⟨h, sq2_l hij.symm _ _, sq2_m _ _ _ _ _⟩        · ext u          simp only [Ψ, Finset.mem_filter, Finset.mem_univ, true_and]          constructor          · intro H k hki hkj            rw [H k hkj hki, sq2_other v hkj hki]          · intro H k hkj hki            rw [H k hki hkj, sq2_other v hkj hki]    · rintro ⟨x, hx, rfl⟩      obtain ⟨hlt, _, _⟩ := hI x hx      exact ⟨x.1.1, x.1.2, hlt.ne, x.2, rfl⟩  have hcardI : ((I.card : ℕ) : ℤ) = (n.choose 2 : ℤ) * 2 ^ (n - 2) := by    have h := hypercube_coordinate_square_count n hn    have h2 : squares.card = I.card := by rw [himg, Finset.card_image_of_injOn hinj]    rw [← h2]    exact_mod_cast h  have hfilt : ∀ Q : Finset (Fin n → Bool) → Prop, ((squares.filter Q).card : ℤ) =      ∑ x ∈ I, if Q (Ψ x) then (1 : ℤ) else 0 := by    intro Q    rw [Finset.sum_boole, himg, Finset.filter_image,      Finset.card_image_of_injOn (hinj.mono (Finset.coe_subset.2 (Finset.filter_subset _ _)))]  -- local identity on each square  have hloc : ∀ x ∈ I,      (eA G x.2 x.1.1 x.1.2 + eB G x.2 x.1.1 x.1.2) * (eC G x.2 x.1.1 x.1.2 + eD G x.2 x.1.1 x.1.2) =        (eA G x.2 x.1.1 x.1.2 + eB G x.2 x.1.1 x.1.2 + eC G x.2 x.1.1 x.1.2 +          eD G x.2 x.1.1 x.1.2) - 1 + (if edgesIn (Ψ x) = ∅ then 1 else 0) -          (if (edgesIn (Ψ x)).card = 2 ∧ ∀ e ∈ edgesIn (Ψ x), ∀ f ∈ edgesIn (Ψ x), e ≠ f →            ∀ w ∈ e, w ∉ f then 1 else 0) := by    rintro ⟨⟨l, m⟩, w⟩ hx    obtain ⟨hlm, -, -⟩ := hI _ hx    have h : l ≠ m := hlm.ne    simp only at h ⊢    have hE : edgesIn (Ψ ⟨(l, m), w⟩) = _ := edges_sq G hG h w    have d : ∀ a b a' b' : Bool, (a, b) ≠ (a', b') → sq2 w l m a b ≠ sq2 w l m a' b' :=      fun a b a' b' hne => sq2_ne h hne    have h4 : ¬ (decide (G.Adj (sq2 w l m false false) (sq2 w l m true false)) = true ∧        decide (G.Adj (sq2 w l m false true) (sq2 w l m true true)) = true ∧        decide (G.Adj (sq2 w l m false false) (sq2 w l m false true)) = true ∧        decide (G.Adj (sq2 w l m true false) (sq2 w l m true true)) = true) := by      simp only [decide_eq_true_eq]      rintro ⟨h1, h2, h3, h4⟩      exact c4_of (d false false true false (by decide)) (d false false false true (by decide)) (d false false true true (by decide))        (d true false false true (by decide)) (d true false true true (by decide)) (d false true true true (by decide)) h1 h2 h3 h4 hC4    have Q := (quad_facts (d false false true false (by decide)) (d false false false true (by decide)) (d false false true true (by decide))        (d true false false true (by decide)) (d true false true true (by decide)) (d false true true true (by decide)) _ _ _ _ h4).1    rw [hE]    simp only [eA, eB, eC, eD]    rw [ite_toNat (G.Adj (sq2 w l m false false) (sq2 w l m true false)),      ite_toNat (G.Adj (sq2 w l m false true) (sq2 w l m true true)),      ite_toNat (G.Adj (sq2 w l m false false) (sq2 w l m false true)),      ite_toNat (G.Adj (sq2 w l m true false) (sq2 w l m true true))]    convert Q using 3  -- global counts  have hx01 : ∀ v l, xl G v l * xl G v l = xl G v l := by    intro v l; unfold xl; split_ifs <;> norm_num  have hdeg : ∀ v, (G.degree v : ℤ) = ∑ l, xl G v l := fun v => degree_eq G hG v  have hsumdeg : ∑ v, (G.degree v : ℤ) = 2 * G.edgeFinset.card := by    exact_mod_cast G.sum_degrees_eq_twice_card_edges  have hsplitI : ∀ F : (Σ _ : Fin n × Fin n, Fin n → Bool) → ℤ,      ∑ x ∈ I, F x = ∑ p : Fin n × Fin n, if p.1 < p.2 then ∑ w ∈ Z p, F ⟨p, w⟩ else 0 := by    intro F    rw [Finset.sum_sigma, Finset.sum_filter]  have e1 : ∑ v, ∑ l, xl G v l = 2 * (G.edgeFinset.card : ℤ) := by    rw [← hsumdeg]; exact Finset.sum_congr rfl (fun v _ => (hdeg v).symm)  have hcher : ∑ v, (G.degree v : ℤ) ^ 2 = 2 * G.edgeFinset.card + 2 * ∑ x ∈ I,      (eA G x.2 x.1.1 x.1.2 + eB G x.2 x.1.1 x.1.2) * (eC G x.2 x.1.1 x.1.2 + eD G x.2 x.1.1 x.1.2) := by    have e2 : ∑ v, ∑ p : Fin n × Fin n, (if p.1 < p.2 then xl G v p.1 * xl G v p.2 else 0) =        ∑ x ∈ I, (eA G x.2 x.1.1 x.1.2 + eB G x.2 x.1.1 x.1.2) *          (eC G x.2 x.1.1 x.1.2 + eD G x.2 x.1.1 x.1.2) := by      rw [hsplitI, Finset.sum_comm]      refine Finset.sum_congr rfl fun p _ => ?_      by_cases hp : p.1 < p.2      · simp only [hp, ↓reduceIte]        exact pair_prod G hp.ne      · simp [hp]    rw [Finset.sum_congr rfl (fun v _ => by rw [hdeg v, sq_sum01 (xl G v) (hx01 v)]),      Finset.sum_add_distrib, ← Finset.mul_sum, e1, e2]  have hk : 2 * ∑ x ∈ I, (eA G x.2 x.1.1 x.1.2 + eB G x.2 x.1.1 x.1.2 + eC G x.2 x.1.1 x.1.2 +      eD G x.2 x.1.1 x.1.2) = ((n : ℤ) - 1) * (2 * G.edgeFinset.card) := by    rw [hsplitI, Finset.mul_sum, ← hsumdeg]    simp only [hdeg]    rw [Finset.sum_comm (s := Finset.univ) (t := Finset.univ), ← sum_pairs]    refine Finset.sum_congr rfl fun p _ => ?_    split_ifs with hp    · rw [pair_l G hp.ne, pair_m G hp.ne, ← Finset.sum_add_distrib, Finset.mul_sum]      refine Finset.sum_congr rfl fun w _ => ?_      ring    · simp  have ha0 : (a0 : ℤ) = ∑ x ∈ I, if edgesIn (Ψ x) = ∅ then (1 : ℤ) else 0 := by    convert hfilt (fun s => edgesIn s = ∅)  have haopp : (aopp : ℤ) = ∑ x ∈ I, if (edgesIn (Ψ x)).card = 2 ∧ ∀ e ∈ edgesIn (Ψ x),      ∀ f ∈ edgesIn (Ψ x), e ≠ f → ∀ w ∈ e, w ∉ f then (1 : ℤ) else 0 := by    convert hfilt (fun s => (edgesIn s).card = 2 ∧ ∀ e ∈ edgesIn s, ∀ f ∈ edgesIn s, e ≠ f → ∀ w ∈ e, w ∉ f)  rw [hcher, Finset.sum_congr rfl hloc, Finset.sum_sub_distrib, Finset.sum_add_distrib,    Finset.sum_sub_distrib, Finset.sum_const, ← ha0, ← haopp]  simp only [nsmul_eq_mul, mul_one]  rw [hcardI]  linarith [hk]end In1264open Classical intheorem c4_free_hypercube_subgraph_degree_square_sum (n : ℕ) (hn : 2 ≤ n)    (G : SimpleGraph (Fin n → Bool)) (hG : G ≤ hypercube_graph n)    (hC4 : (SimpleGraph.cycleGraph 4).Free G) :    let squares : Finset (Finset (Fin n → Bool)) := Finset.univ.filter fun s =>      ∃ i j : Fin n, i ≠ j ∧ ∃ v : Fin n → Bool,        s = Finset.univ.filter fun u => ∀ k, k ≠ i → k ≠ j → u k = v k    let edgesIn : Finset (Fin n → Bool) → Finset (Sym2 (Fin n → Bool)) := fun s =>      G.edgeFinset.filter fun e => ∀ w ∈ e, w ∈ s    let a0 : ℕ := (squares.filter fun s => edgesIn s = ∅).card    let aopp : ℕ := (squares.filter fun s => (edgesIn s).card = 2 ∧      ∀ e ∈ edgesIn s, ∀ f ∈ edgesIn s, e ≠ f → ∀ w ∈ e, w ∉ f).card    ∑ v, (G.degree v : ℤ) ^ 2 =      2 * n * G.edgeFinset.card - 2 * (n.choose 2 : ℤ) * 2 ^ (n - 2) + 2 * ((a0 : ℤ) - aopp) := by  exact In1264.main n hn G hG hC4
See it in the graphDiscuss this resultChallenge it