GNNTheory: A Lean 4 Formalization of Graph Neural Network Theory

1.1 Vertex-Labelled Graphs, Isomorphisms, and Invariants

Definition 1.1 Vertex-labelled graph
✓

A vertex-labelled graph \(G = (V, E, \ell )\) consists of a simple undirected graph \((V, E)\) on a vertex set \(V\) together with a vertex labelling function \(\ell : V \to L\) into a label space \(L\). When \(V\) is finite, \(G\) is a finite vertex-labelled graph. (Grohe, arXiv:2104.14624, §2; Morris et al., arXiv:1810.02244, §3.1.)

Lean 4 Spec · GNNTheory/Spec/Graph.lean:L19–L34Pending AuditApprove SpecDispute
structure LabelledGraph (V L : Type) where
  graph : SimpleGraph V
  label : V → L

def Adj {V L : Type} (G : LabelledGraph V L) (x y : V) : Prop :=
  G.graph.Adj x y

noncomputable def neighborFinset {V L : Type} [Fintype V]
    (G : LabelledGraph V L) (v : V) : Finset V := by
  classical
  exact G.graph.neighborFinset v
Definition 1.2 Isomorphism of vertex-labelled graphs
✓
#

An isomorphism \(f : G \simeq _{\ell } H\) between two \(L\)-labelled graphs \(G = (V, E_G, \ell _G)\) and \(H = (W, E_H, \ell _H)\) is a bijection \(f : V \to W\) such that \(uv \in E_G \iff f(u)f(v) \in E_H\) for all \(u, v \in V\), and \(\ell _H(f(v)) = \ell _G(v)\) for all \(v \in V\). (Grohe, arXiv:2104.14624, §2.)

Lean 4 Spec · GNNTheory/Spec/Graph.lean:L38–L40Pending AuditApprove SpecDispute
structure Iso {V W L : Type} (G : LabelledGraph V L) (H : LabelledGraph W L) where
  toGraphIso : G.graph ≃g H.graph
  map_label' : ∀ v : V, H.label (toGraphIso v) = G.label v
Definition 1.3 Graph invariant
✓

A graph invariant (or \(0\)-ary invariant) on \(L\)-labelled finite graphs with codomain \(Y\) is a function \(\xi \) assigning to each finite \(L\)-labelled graph \(G\) a value \(\xi (G) \in Y\) such that \(\xi (G) = \xi (H)\) whenever \(G \simeq _{\ell } H\). (Grohe, arXiv:2104.14624, §4.)

Lean 4 Spec · GNNTheory/Spec/Graph.lean:L54–L58Pending AuditApprove SpecDispute
def IsGraphInvariant {L Y : Type}
    (ξ : ∀ {V : Type} [Fintype V], LabelledGraph V L → Y) : Prop :=
  ∀ {V W : Type} [Fintype V] [Fintype W]
    (G : LabelledGraph V L) (H : LabelledGraph W L),
    (G ≃ₗ H) → ξ G = ξ H
Definition 1.4 Vertex invariant (equivariance)
✓

A vertex invariant (or equivariant node mapping) on \(L\)-labelled finite graphs with codomain \(C\) associates with every finite \(L\)-labelled graph \(G\) a map \(\xi (G, \cdot ) : V(G) \to C\) such that for all isomorphic \(G \simeq _{\ell } H\) via \(f\) and all \(v \in V(G)\), \(\xi (G, v) = \xi (H, f(v))\). (Grohe, arXiv:2104.14624, §4.)

Lean 4 Spec · GNNTheory/Spec/Graph.lean:L62–L67Pending AuditApprove SpecDispute
def IsVertexInvariant {L C : Type}
    (ξ : ∀ {V : Type} [Fintype V], LabelledGraph V L → V → C) : Prop :=
  ∀ {V W : Type} [Fintype V] [Fintype W]
    (G : LabelledGraph V L) (H : LabelledGraph W L)
    (e : G ≃ₗ H) (v : V),
    ξ G v = ξ H (e v)
Definition 1.5 Multiset lift of a vertex invariant
✓
#

Given a vertex mapping \(\xi (G, \cdot ) : V(G) \to C\), its derived graph-level multiset readout is \(\hat{\xi }(G) := \bigl\{ \! \! \bigl\{ \xi (G, v) \mid v \in V(G)\bigr\} \! \! \bigr\} \). (Grohe, arXiv:2104.14624, §4.)

Lean 4 Spec · GNNTheory/Spec/Graph.lean:L71–L74Pending AuditApprove SpecDispute
def liftVertexInvariant {L C : Type}
    (ξ : ∀ {V : Type} [Fintype V], LabelledGraph V L → V → C)
    {V : Type} [Fintype V] (G : LabelledGraph V L) : Multiset C :=
  Finset.univ.val.map (ξ G)
Definition 1.6 Colouring refinement
✓
#

A colouring \(\chi _1 : \alpha \to C_1\) refines a colouring \(\chi _2 : \alpha \to C_2\) (written \(\chi _1 \preceq \chi _2\)) if for all \(x, y \in \alpha \), \(\chi _1(x) = \chi _1(y) \implies \chi _2(x) = \chi _2(y)\). (Grohe, arXiv:2104.14624, §4; Morris et al., arXiv:1810.02244, §4.)

Lean 4 Spec · GNNTheory/Spec/Graph.lean:L78–L79Pending AuditApprove SpecDispute
def Refines {α C₁ C₂ : Type} (χ₁ : α → C₁) (χ₂ : α → C₂) : Prop :=
  ∀ x y : α, χ₁ x = χ₁ y → χ₂ x = χ₂ y
Definition 1.7 Colouring equivalence
✓
#

Two colourings \(\chi _1 : \alpha \to C_1\) and \(\chi _2 : \alpha \to C_2\) are equivalent (\(\chi _1 \equiv \chi _2\)) if \(\chi _1 \preceq \chi _2\) and \(\chi _2 \preceq \chi _1\). (Grohe, arXiv:2104.14624, §4.)

Lean 4 Spec · GNNTheory/Spec/Graph.lean:L83–L84Pending AuditApprove SpecDispute
def ColorEquiv {α C₁ C₂ : Type} (χ₁ : α → C₁) (χ₂ : α → C₂) : Prop :=
  Refines χ₁ χ₂ ∧ Refines χ₂ χ₁
Definition 1.8 Cross-graph colouring refinement
✓
#

Given colourings \(\chi _1 : \alpha \to C_1\), \(\chi '_1 : \beta \to C_1\) and \(\chi _2 : \alpha \to C_2\), \(\chi '_2 : \beta \to C_2\), we say \((\chi _1, \chi '_1)\) cross-refines \((\chi _2, \chi '_2)\) if for all \(x \in \alpha \) and \(y \in \beta \), \(\chi _1(x) = \chi '_1(y) \implies \chi _2(x) = \chi '_2(y)\). (Grohe, arXiv:2104.14624, Theorem 8.1.)

Lean 4 Spec · GNNTheory/Spec/Graph.lean:L88–L90Pending AuditApprove SpecDispute
def CrossRefines {α β C₁ C₂ : Type}
    (χ₁ : α → C₁) (χ₁' : β → C₁) (χ₂ : α → C₂) (χ₂' : β → C₂) : Prop :=
  ∀ (x : α) (y : β), χ₁ x = χ₁' y → χ₂ x = χ₂' y