GNNTheory: A Lean 4 Formalization of Graph Neural Network Theory

1.3 Aggregate–Combine Message-Passing Neural Networks

Definition 1.20 Aggregate-combine MPNN layer
✓
#

An MPNN layer from feature space \(\alpha \) to feature space \(\beta \) consists of an intermediate message space \(M\), an aggregation function \(\operatorname {agg} : \mathcal{M}_{\mathrm{fin}}(\alpha ) \to M\) operating on finite multisets of neighbor features, and a combination function \(\operatorname {comb} : \alpha \times M \to \beta \). (Grohe, arXiv:2104.14624, §7.1; Xu et al., arXiv:1810.00826, §2.)

Lean 4 Spec · GNNTheory/Spec/MPNN.lean:L19–L22Pending AuditApprove SpecDispute
structure MPNNLayer (α β : Type) where
  Msg  : Type
  agg  : Multiset α → Msg
  comb : α → Msg → β
Definition 1.21 Semantics of an MPNN layer
✓

Applied to a finite graph \(G\) and a feature map \(\zeta : V(G) \to \alpha \), an MPNN layer \(\Lambda = (M, \operatorname {agg}, \operatorname {comb})\) produces the feature map \(\eta : V(G) \to \beta \) defined by \(\eta (v) := \operatorname {comb}\! \bigl(\zeta (v), \operatorname {agg}(\{ \! \! \{ \zeta (w) \mid w \in N^G(v)\} \! \! \} )\bigr)\). (Grohe, arXiv:2104.14624, §7.1, Eq. (2).)

Lean 4 Spec · GNNTheory/Spec/MPNN.lean:L26–L28Pending AuditApprove SpecDispute
noncomputable def MPNNLayer.apply {V L α β : Type} [Fintype V]
    (layer : MPNNLayer α β) (G : LabelledGraph V L) (h : V → α) (v : V) : β :=
  layer.comb (h v) (layer.agg ((G.neighborFinset v).val.map h))
Definition 1.22 Composable stack of MPNN layers
✓

A \(d\)-layer MPNN stack from \(\alpha \) to \(\beta \) is a sequence of \(d\) MPNN layers \((L^{(1)}, \dots , L^{(d)})\) with matching intermediate feature spaces \(\alpha = \alpha _0 \to \alpha _1 \to \dots \to \alpha _d = \beta \). (Grohe, arXiv:2104.14624, §7.1.)

Lean 4 Spec · GNNTheory/Spec/MPNN.lean:L32–L41Pending AuditApprove SpecDispute
inductive MPNNLayers : Type → Type → ℕ → Type 1
  | nil {α : Type} : MPNNLayers α α 0
  | snoc {α β γ : Type} {d : ℕ} :
      MPNNLayers α β d → MPNNLayer β γ → MPNNLayers α γ (d + 1)

noncomputable def MPNNLayers.run {V L α β : Type} [Fintype V] {d : ℕ} :
    MPNNLayers α β d → LabelledGraph V L → (V → α) → (V → β)
  | .nil,            _, h => h
  | .snoc prev last, G, h => last.apply G (prev.run G h)
Definition 1.23 Fixed-depth MPNN
✓
#

A \(d\)-layer MPNN on \(L\)-labelled graphs with graph-level output space \(Y\) consists of input and output feature spaces \(\alpha , \beta \), a label-encoding map \(\operatorname {init} : L \to \alpha \), a \(d\)-layer stack \((L^{(1)}, \dots , L^{(d)})\) from \(\alpha \) to \(\beta \), and a permutation-invariant graph readout \(\operatorname {readout} : \mathcal{M}_{\mathrm{fin}}(\beta ) \to Y\). (Grohe, arXiv:2104.14624, §7.1; Xu et al., arXiv:1810.00826, §2.)

Lean 4 Spec · GNNTheory/Spec/MPNN.lean:L45–L50Pending AuditApprove SpecDispute
structure MPNN (L Y : Type) (d : ℕ) where
  InFeat    : Type
  OutFeat   : Type
  init      : L → InFeat
  layers    : MPNNLayers InFeat OutFeat d
  readoutFn : Multiset OutFeat → Y
Definition 1.24 MPNN node embeddings and graph readout
✓

On a finite \(L\)-labelled graph \(G\), a \(d\)-layer MPNN \(N\) computes node embeddings \(\zeta ^{(d)} : V(G) \to \beta \) by applying its \(d\) layers to \(\zeta ^{(0)}(v) := \operatorname {init}(\ell _G(v))\), and graph-level readout \(N(G) := \operatorname {readout}\! \bigl(\{ \! \! \{ \zeta ^{(d)}(v) \mid v \in V(G)\} \! \! \} \bigr)\). (Grohe, arXiv:2104.14624, §7.1; Xu et al., arXiv:1810.00826, §2.)

Lean 4 Spec · GNNTheory/Spec/MPNN.lean:L53–L61Pending AuditApprove SpecDispute
noncomputable def MPNN.nodeEmbed {V L Y : Type} [Fintype V] {d : ℕ}
    (N : MPNN L Y d) (G : LabelledGraph V L) : V → N.OutFeat :=
  N.layers.run G (N.init ∘ G.label)

noncomputable def MPNN.readout {V L Y : Type} [Fintype V] {d : ℕ}
    (N : MPNN L Y d) (G : LabelledGraph V L) : Y :=
  N.readoutFn (liftVertexInvariant (fun G => N.nodeEmbed G) G)

A recurrent MPNN on \(L\)-labelled graphs with output space \(Y\) consists of a single feature space \(\alpha \), an initial encoding \(\operatorname {init} : L \to \alpha \), a single layer \(\Lambda : \alpha \to \alpha \) iterated \(t\) times to obtain \(\zeta ^{(t)}\), and a multiset readout \(\operatorname {readout} : \mathcal{M}_{\mathrm{fin}}(\alpha ) \to Y\). (Grohe, arXiv:2104.14624, §7.1.)

Lean 4 Spec · GNNTheory/Spec/MPNN.lean:L65–L80Pending AuditApprove SpecDispute
structure RecurrentMPNN (L Y : Type) where
  Feat      : Type
  init      : L → Feat
  layer     : MPNNLayer Feat Feat
  readoutFn : Multiset Feat → Y

noncomputable def RecurrentMPNN.nodeEmbed {V L Y : Type} [Fintype V]
    (R : RecurrentMPNN L Y) (G : LabelledGraph V L) : ℕ → V → R.Feat
  | 0     => R.init ∘ G.label
  | t + 1 => R.layer.apply G (R.nodeEmbed G t)

noncomputable def RecurrentMPNN.readout {V L Y : Type} [Fintype V]
    (R : RecurrentMPNN L Y) (G : LabelledGraph V L) (t : ℕ) : Y :=
  R.readoutFn (liftVertexInvariant (fun G => R.nodeEmbed G t) G)
Lemma 1.26 Equivariance of a single MPNN layer
✓

Let \(\Lambda \) be an MPNN layer, \(f : G \simeq _{\ell } H\) an isomorphism of finite labelled graphs, and \(\zeta _G, \zeta _H\) feature maps satisfying \(\zeta _G(v) = \zeta _H(f(v))\) for all \(v \in V(G)\). Then the updated feature maps satisfy \((\Lambda (G, \zeta _G))(v) = (\Lambda (H, \zeta _H))(f(v))\) for all \(v \in V(G)\). (Grohe, arXiv:2104.14624, §7.1.)

Lean 4 Spec · GNNTheory/Spec/MPNN.lean:L86–L92 · Proved in GNNTheory/Proofs/MPNN.lean:L20–L23Pending AuditApprove SpecDispute
def mpnn_layer_equivariant_stmt : Prop :=
  ∀ {V W L α β : Type} [Fintype V] [Fintype W]
    (layer : MPNNLayer α β)
    (G : LabelledGraph V L) (H : LabelledGraph W L)
    (e : G ≃ₗ H) (hG : V → α) (hH : W → α),
    (∀ v : V, hG v = hH (e v)) →
    ∀ v : V, layer.apply G hG v = layer.apply H hH (e v)
Proof ▶

Paper proof: Grohe (arXiv:2104.14624), §7.1. Lean formalization: Direct rewrite of MPNNLayer.apply using Iso.neighborFinset_val_map.

Lean 4 Proof · GNNTheory/Proofs/MPNN.lean:L20–L23Verified ✓
theorem mpnn_layer_equivariant : Spec.mpnn_layer_equivariant_stmt := by
  intro V W L α β _ _ layer G H e hG hH h v
  unfold MPNNLayer.apply
  rw [h v, e.neighborFinset_val_map hG hH h v]

For every \(d\)-layer MPNN \(N\), the node embedding map \(G \mapsto \zeta ^{(d)}_G\) is a vertex invariant (equivariant under isomorphisms of \(L\)-labelled graphs). (Grohe, arXiv:2104.14624, §7.1.)

Lean 4 Spec · GNNTheory/Spec/MPNN.lean:L96–L98 · Proved in GNNTheory/Proofs/MPNN.lean:L41–L45Pending AuditApprove SpecDispute
def mpnn_equivariant_stmt : Prop :=
  ∀ {L Y : Type} {d : ℕ} (N : MPNN L Y d),
    IsVertexInvariant (fun G => N.nodeEmbed G)
Proof ▶

Paper proof: Grohe (arXiv:2104.14624), §7.1. Lean formalization: Structural induction over N.layers (mpnnLayers_equivariant), starting from Iso.map_label on \(\operatorname {init} \circ \ell \).

Lean 4 Proof · GNNTheory/Proofs/MPNN.lean:L41–L45Verified ✓
theorem mpnn_equivariant : Spec.mpnn_equivariant_stmt := by
  intro L Y d N V W _ _ G H e v
  unfold MPNN.nodeEmbed
  exact mpnnLayers_equivariant N.layers G H e (N.init ∘ G.label) (N.init ∘ H.label)
    (fun u => congrArg N.init (e.map_label u).symm) v

For every \(d\)-layer MPNN \(N\), the graph-level readout \(G \mapsto N(G)\) is a graph invariant. (Grohe, arXiv:2104.14624, §7.1.)

Lean 4 Spec · GNNTheory/Spec/MPNN.lean:L102–L104 · Proved in GNNTheory/Proofs/MPNN.lean:L51–L54Pending AuditApprove SpecDispute
def mpnn_readout_invariant_stmt : Prop :=
  ∀ {L Y : Type} {d : ℕ} (N : MPNN L Y d),
    IsGraphInvariant (fun G => N.readout G)
Proof ▶

Paper proof: Grohe (arXiv:2104.14624), §7.1. Lean formalization: Apply Iso.univ_val_map to mpnn_equivariant and compose with N.readoutFn.

Lean 4 Proof · GNNTheory/Proofs/MPNN.lean:L51–L54Verified ✓
theorem mpnn_readout_invariant : Spec.mpnn_readout_invariant_stmt := by
  intro L Y d N V W _ _ G H e
  unfold MPNN.readout liftVertexInvariant
  exact congrArg N.readoutFn (e.univ_val_map (N.nodeEmbed G) (N.nodeEmbed H) (mpnn_equivariant N G H e))