GNNTheory: A Lean 4 Formalization of Graph Neural Network Theory

1.2 1-WL (Colour Refinement)

Following the machine learning literature (Morris et al., arXiv:1810.02244, §3.2; Xu et al., arXiv:1810.00826, §2), we refer to iterative neighborhood-multiset colour refinement as 1-WL. In Grohe (arXiv:2104.14624, §5), this procedure is called colour refinement (\(\operatorname {cr}\)) and distinguished from the version of 1-WL that counts both neighbors and non-neighbors; Proposition 1.19 below records their equivalence at the graph level.

Definition 1.9 Canonical 1-WL colour space
✓
#

For a label space \(L\) and round \(t \in \mathbb {N}\), the canonical \(1\)-WL colour space \(C_t(L)\) is defined recursively by \(C_0(L) := L\) and \(C_{t+1}(L) := C_t(L) \times \mathcal{M}_{\mathrm{fin}}(C_t(L))\), where \(\mathcal{M}_{\mathrm{fin}}(X)\) denotes finite multisets over \(X\). (Grohe, arXiv:2104.14624, §5.)

Lean 4 Spec · GNNTheory/Spec/WL.lean:L17–L19Pending AuditApprove SpecDispute
def WLColor (L : Type) : ℕ → Type
  | 0     => L
  | t + 1 => WLColor L t × Multiset (WLColor L t)
Definition 1.10 1-WL (colour refinement) iteration
✓
#

For a finite \(L\)-labelled graph \(G\), the round-\(t\) \(1\)-WL (colour refinement) colouring \(\operatorname {cr}^{(t)}(G, \cdot ) : V(G) \to C_t(L)\) is defined by \(\operatorname {cr}^{(0)}(G, v) := \ell _G(v)\) and \(\operatorname {cr}^{(t+1)}(G, v) := \bigl(\operatorname {cr}^{(t)}(G, v), \bigl\{ \! \! \bigl\{ \operatorname {cr}^{(t)}(G, w) \mid w \in N^G(v)\bigr\} \! \! \bigr\} \bigr)\). (Grohe, arXiv:2104.14624, §5; Morris et al., arXiv:1810.02244, §3.2.)

Lean 4 Spec · GNNTheory/Spec/WL.lean:L25–L28Pending AuditApprove SpecDispute
noncomputable def wlColor {V L : Type} [Fintype V]
    (G : LabelledGraph V L) : (t : ℕ) → V → WLColor L t
  | 0,     v => G.label v
  | t + 1, v => (wlColor G t v, (G.neighborFinset v).val.map (wlColor G t))
Definition 1.11 1-WL colour histogram
✓

The round-\(t\) \(1\)-WL colour histogram of a finite \(L\)-labelled graph \(G\) is the multiset \(\widehat{\operatorname {cr}}^{(t)}(G) := \bigl\{ \! \! \bigl\{ \operatorname {cr}^{(t)}(G, v) \mid v \in V(G)\bigr\} \! \! \bigr\} \). (Grohe, arXiv:2104.14624, §4–§5.)

Lean 4 Spec · GNNTheory/Spec/WL.lean:L32–L34Pending AuditApprove SpecDispute
noncomputable def wlHist {V L : Type} [Fintype V]
    (G : LabelledGraph V L) (t : ℕ) : Multiset (WLColor L t) :=
  liftVertexInvariant (fun G => wlColor G t) G
Definition 1.12 Round-\(t\) 1-WL equivalence
✓

Two finite \(L\)-labelled graphs \(G\) and \(H\) are 1-WL equivalent at round \(t\) (written \(G \equiv _{\mathrm{WL}}^{(t)} H\)) if \(\widehat{\operatorname {cr}}^{(t)}(G) = \widehat{\operatorname {cr}}^{(t)}(H)\). (Grohe, arXiv:2104.14624, §5.)

Lean 4 Spec · GNNTheory/Spec/WL.lean:L38–L40Pending AuditApprove SpecDispute
def WLEquivAt {V W L : Type} [Fintype V] [Fintype W]
    (t : ℕ) (G : LabelledGraph V L) (H : LabelledGraph W L) : Prop :=
  wlHist G t = wlHist H t
Definition 1.13 1-WL graph equivalence
✓
#

Two finite \(L\)-labelled graphs \(G\) and \(H\) are 1-WL equivalent (\(G \equiv _{\mathrm{WL}} H\)) if \(\widehat{\operatorname {cr}}^{(t)}(G) = \widehat{\operatorname {cr}}^{(t)}(H)\) for all \(t \in \mathbb {N}\). (Grohe, arXiv:2104.14624, §5; Xu et al., arXiv:1810.00826, §2.)

Lean 4 Spec · GNNTheory/Spec/WL.lean:L44–L46Pending AuditApprove SpecDispute
def WLEquiv {V W L : Type} [Fintype V] [Fintype W]
    (G : LabelledGraph V L) (H : LabelledGraph W L) : Prop :=
  ∀ t : ℕ, WLEquivAt t G H

For every round \(t \in \mathbb {N}\), the \(1\)-WL colouring \(\operatorname {cr}^{(t)}\) is a vertex invariant (equivariant under isomorphisms of \(L\)-labelled graphs), and the colour histogram \(\widehat{\operatorname {cr}}^{(t)}\) is a graph invariant. (Grohe, arXiv:2104.14624, §5.)

Lean 4 Spec · GNNTheory/Spec/WL.lean:L52–L55 · Proved in GNNTheory/Proofs/WL.lean:L31–L37Pending AuditApprove SpecDispute
def wl_equivariant_stmt : Prop :=
  ∀ {L : Type} (t : ℕ),
    IsVertexInvariant (fun G => wlColor (L := L) G t) ∧
    IsGraphInvariant (fun G => wlHist (L := L) G t)
Proof ▶

Paper proof: Grohe (arXiv:2104.14624), §5. Lean formalization: By induction on \(t\), using preservation of neighbor multisets under graph isomorphisms (Iso.neighborFinset_val_map), and lifting vertex equivariance to the full vertex multiset (Iso.univ_val_map).

Lean 4 Proof · GNNTheory/Proofs/WL.lean:L31–L37Verified ✓
theorem wl_equivariant : Spec.wl_equivariant_stmt := by
  intro L t
  refine ⟨?_, ?_⟩
  · intro V W _ _ G H e v
    exact wlColor_equivariant_aux G H e t v
  · intro V W _ _ G H e
    exact e.univ_val_map (wlColor G t) (wlColor H t) (wlColor_equivariant_aux G H e t)
Lemma 1.15 Monotonicity of 1-WL refinement
✓

For all finite \(L\)-labelled graphs \(G, H\) and every round \(t \in \mathbb {N}\), \((\operatorname {cr}^{(t+1)}(G), \operatorname {cr}^{(t+1)}(H))\) cross-refines \((\operatorname {cr}^{(t)}(G), \operatorname {cr}^{(t)}(H))\). In particular, \(\operatorname {cr}^{(t+1)}(G) \preceq \operatorname {cr}^{(t)}(G)\). (Grohe, arXiv:2104.14624, §5.)

Lean 4 Spec · GNNTheory/Spec/WL.lean:L59–L62 · Proved in GNNTheory/Proofs/WL.lean:L43–L45Pending AuditApprove SpecDispute
def wl_refines_prev_stmt : Prop :=
  ∀ {V W L : Type} [Fintype V] [Fintype W]
    (G : LabelledGraph V L) (H : LabelledGraph W L) (t : ℕ),
    CrossRefines (wlColor G (t + 1)) (wlColor H (t + 1)) (wlColor G t) (wlColor H t)
Proof ▶

Paper proof: Grohe (arXiv:2104.14624), §5. Lean formalization: Immediate by first-coordinate projection (Prod.fst) from the canonical recursive colour pair.

Lean 4 Proof · GNNTheory/Proofs/WL.lean:L43–L45Verified ✓
theorem wl_refines_prev : Spec.wl_refines_prev_stmt := by
  intro V W L _ _ G H t v w h
  exact congrArg Prod.fst h

For every finite nonempty \(L\)-labelled graph \(G\), there exists \(t {\lt} |V(G)|\) such that \(\operatorname {cr}^{(t+1)}(G) \equiv \operatorname {cr}^{(t)}(G)\). Moreover, whenever \(\operatorname {cr}^{(t+1)}(G) \equiv \operatorname {cr}^{(t)}(G)\), we have \(\operatorname {cr}^{(s)}(G) \equiv \operatorname {cr}^{(t)}(G)\) for all \(s \ge t\). (Grohe, arXiv:2104.14624, §5; Morris et al., arXiv:1810.02244, §3.2.)

Lean 4 Spec · GNNTheory/Spec/WL.lean:L67–L71 · Proved in GNNTheory/Proofs/WL.lean:L55–L103Pending AuditApprove SpecDispute
def wl_stabilizes_stmt : Prop :=
  ∀ {V L : Type} [Fintype V] [Nonempty V] (G : LabelledGraph V L),
    (∃ t : ℕ, t < Fintype.card V ∧ ColorEquiv (wlColor G (t + 1)) (wlColor G t)) ∧
    (∀ t : ℕ, ColorEquiv (wlColor G (t + 1)) (wlColor G t) →
      ∀ s : ℕ, t ≤ s → ColorEquiv (wlColor G s) (wlColor G t))
Proof ▶

Paper proof: Grohe (arXiv:2104.14624), §5; Morris et al. (arXiv:1810.02244), §3.2. Lean formalization: Pigeonhole on the non-decreasing number of distinct colours (wlNumColors, bounded between \(1\) and \(|V(G)|\)) yields a round \(t {\lt} |V(G)|\) with equal colour counts, hence equivalence; permanence for \(s \ge t\) follows by induction via wl_refines_step.

Lean 4 Proof · GNNTheory/Proofs/WL.lean:L55–L103Verified ✓
theorem wl_stabilizes : Spec.wl_stabilizes_stmt := by
  intro V L _ _ G
  refine ⟨?_, ?_⟩
  · -- Part 1: pigeonhole on the number of distinct colours `wlNumColors G t ≤ |V|`
    by_contra hno
    push Not at hno
    have hlt : ∀ t < Fintype.card V, wlNumColors G t < wlNumColors G (t + 1) := by
      intro t ht
      have hle := wlNumColors_le_succ G t
      refine lt_of_le_of_ne hle ?_
      intro heq
      have h1 : Refines (wlColor G (t + 1)) (wlColor G t) :=
        fun u v h => congrArg Prod.fst h
      have h2 : Refines (wlColor G t) (wlColor G (t + 1)) :=
        wl_refines_of_numColors_eq G heq
      exact hno t ht ⟨h1, h2⟩
    have hbound : ∀ t ≤ Fintype.card V, t + 1 ≤ wlNumColors G t := by
      intro t
      induction t with
      | zero =>
        intro _
        exact one_le_wlNumColors_zero G
      | succ t ih =>
        intro ht
        have ht' : t < Fintype.card V := Nat.lt_of_succ_le ht
        have h1 := ih (le_of_lt ht')
        have h2 := hlt t ht'
        omega
    have htop := hbound (Fintype.card V) le_rfl
    have hle := wlNumColors_le_card G (Fintype.card V)
    omega
  · -- Part 2: permanence for all `s ≥ t` once stabilized at `t`
    intro t hstab s hts
    obtain ⟨k, rfl⟩ := Nat.exists_eq_add_of_le hts
    clear hts
    induction k with
    | zero =>
      exact ⟨fun _ _ h => h, fun _ _ h => h⟩
    | succ k ih =>
      have hstep : ∀ n : ℕ, Refines (wlColor G (t + n)) (wlColor G (t + n + 1)) := by
        intro n
        induction n with
        | zero => exact hstab.2
        | succ n ihn => exact wl_refines_step G ihn
      refine ⟨?_, ?_⟩
      · intro u v h
        exact ih.1 u v (congrArg Prod.fst h)
      · intro u v h
        exact hstep k u v (ih.2 u v h)

Two finite \(L\)-labelled graphs \(G\) and \(H\) are \(1\)-WL equivalent (\(G \equiv _{\mathrm{WL}} H\)) if and only if they have the same colour histogram at round \(|V(G)|\): \(G \equiv _{\mathrm{WL}}^{(|V(G)|)} H\). (Grohe, arXiv:2104.14624, §5; Morris et al., arXiv:1810.02244, §3.2.)

Lean 4 Spec · GNNTheory/Spec/WL.lean:L75–L78 · Proved in GNNTheory/Proofs/WL.lean:L113–L151Pending AuditApprove SpecDispute
def wl_equiv_iff_card_stmt : Prop :=
  ∀ {V W L : Type} [Fintype V] [Fintype W]
    (G : LabelledGraph V L) (H : LabelledGraph W L),
    WLEquiv G H ↔ WLEquivAt (Fintype.card V) G H
Proof ▶

Paper proof: Grohe (arXiv:2104.14624), §5; Morris et al. (arXiv:1810.02244), §3.2. Lean formalization: Empty graphs are handled via histogram cardinality. For nonempty \(V(G)\), pick a stabilization round \(t {\lt} |V(G)|\) of \(G\) via wl_stabilizes, project equality at round \(|V(G)|\) down to \(t + 1\) (wlEquivAt_of_le), transfer stabilization across \(G\) and \(H\) (wl_crossRefines_of_stabilized), and induct upward via wl_crossRefines_and_hist_step.

Lean 4 Proof · GNNTheory/Proofs/WL.lean:L113–L151Verified ✓
theorem wl_equiv_iff_card : Spec.wl_equiv_iff_card_stmt := by
  intro V W L _ _ G H
  constructor
  · intro h
    exact h (Fintype.card V)
  · intro hcard s
    by_cases hne : Nonempty V
    · -- Nonempty case: stabilize `G` at some `t < |V|`, transfer across `H`, induct upward
      obtain ⟨⟨t, ht, hstab⟩, _⟩ := wl_stabilizes G
      have ht1 : WLEquivAt (t + 1) G H := wlEquivAt_of_le (Nat.succ_le_of_lt ht) hcard
      have hcr0 : CrossRefines (wlColor G t) (wlColor H t)
          (wlColor G (t + 1)) (wlColor H (t + 1)) :=
        wl_crossRefines_of_stabilized hstab.2 ht1
      have hup : ∀ k : ℕ,
          CrossRefines (wlColor G (t + k)) (wlColor H (t + k))
            (wlColor G (t + k + 1)) (wlColor H (t + k + 1)) ∧
          WLEquivAt (t + k + 1) G H := by
        intro k
        induction k with
        | zero => exact ⟨hcr0, ht1⟩
        | succ k ih => exact wl_crossRefines_and_hist_step ih.1 ih.2
      by_cases hs : s ≤ t + 1
      · exact wlEquivAt_of_le hs ht1
      · have hge : t + 1 ≤ s := Nat.le_of_not_ge hs
        obtain ⟨k, rfl⟩ := Nat.exists_eq_add_of_le hge
        have : t + 1 + k = t + k + 1 := by omega
        rw [this]
        exact (hup k).2
    · -- Empty case: equal histogram cardinality forces `W` to be empty as well
      have : IsEmpty V := not_nonempty_iff.mp hne
      have h0 : WLEquivAt 0 G H := wlEquivAt_of_le (Nat.zero_le _) hcard
      have hcardW : Fintype.card W = 0 := by
        have hlen := congrArg Multiset.card h0
        unfold wlHist liftVertexInvariant at hlen
        simp only [Multiset.card_map, Finset.card_val, Finset.card_univ, Fintype.card_eq_zero] at hlen
        exact hlen.symm
      have : IsEmpty W := Fintype.card_eq_zero_iff.mp hcardW
      unfold WLEquivAt wlHist liftVertexInvariant
      simp [Finset.univ_eq_empty]
Lemma 1.18 Countability of 1-WL colour spaces
✓
#

If the initial label space \(L\) is countable, then the round-\(t\) canonical colour space \(C_t(L)\) is countable for every \(t \in \mathbb {N}\). (Xu et al., arXiv:1810.00826, Lemma 4 / §3.)

Lean 4 Spec · GNNTheory/Spec/WL.lean:L82–L83 · Proved in GNNTheory/Proofs/WL.lean:L157–L164Pending AuditApprove SpecDispute
def wl_color_countable_stmt : Prop :=
  ∀ {L : Type} [Countable L] (t : ℕ), Countable (WLColor L t)
Proof ▶

Paper proof: Xu et al. (arXiv:1810.00826), Appendix C (Proof of Lemma 4). Lean formalization: Induction on \(t\) using countability of finite multisets (multiset_countable, via surjection from List) and products (instCountableProd).

Lean 4 Proof · GNNTheory/Proofs/WL.lean:L157–L164Verified ✓
theorem wl_color_countable : Spec.wl_color_countable_stmt := by
  intro L _ t
  induction t with
  | zero => exact ‹Countable L›
  | succ t ih =>
    have : Countable (WLColor L t) := ih
    have : Countable (Multiset (WLColor L t)) := multiset_countable
    exact instCountableProd
Proposition 1.19 Colour refinement vs. Grohe’s 1-WL

Let \(\operatorname {wl}_{1}^{(t)}\) denote the variant of \(1\)-WL that counts both neighbors and non-neighbors in each colour class (Grohe, arXiv:2104.14624, §5, Proposition 5.1 / prop:cr1wl). Then for all \(t \in \mathbb {N}\) and all finite \(L\)-labelled graphs \(G, H\), \(G\) and \(H\) are distinguished by \(\operatorname {cr}^{(t)}\) if and only if they are distinguished by \(\operatorname {wl}_{1}^{(t)}\). Moreover, within any single graph \(G\), \(\operatorname {cr}^{(t)}(G) \equiv \operatorname {wl}_{1}^{(t)}(G)\).

Proof ▶

Induct on \(t\): if \(\operatorname {cr}^{(t)}(G)\) and \(\operatorname {wl}_{1}^{(t)}(G)\) are in bijection on their ranges and corresponding colour classes have equal sizes in \(G\) and \(H\), then the number of non-neighbors of \(v\) in a colour class \(C\) equals \(|C|\) minus the number of neighbors of \(v\) in \(C\) (and minus \(1\) if \(v \in C\)).