Formalized Category Theory · Lean 4 Sina Hazratpour · sinhp.github.io ↗

Covariant Yoneda Lemma in Lean 4

Bridging Sina Hazratpour’s 2023 standalone formalization in github.com/sinhp/CovariantYonedaLean4 (CovYoneda/src/yoneda.lean) with Mathlib’s CategoryTheory.Yoneda on an interactive spatial proof canvas.

github.com/sinhp/CovariantYonedaLean4 · CovYoneda/src/yoneda.lean
2023 Standalone Lean 4 Proof (Self-contained Category, Functor, NatTrans & Yoneda Bijection)
View Source ↗
Core Isomorphism:   Nat(HomC(A, −), F) ≅ F(A)   naturally in A ∈ C and F : C → Type.
Part I · Naturality & Formalization
1. Roadmap & CovariantYonedaLean4 Attribution 2. Commutative Naturality Square (Def. I.1) 3. Lean 4 Structures: CovYoneda vs. Mathlib
Part II · Yoneda Proof & Embedding
4. Subslip II.1a: Element-Chasing Bijection 5. Subslip II.1b: Tactic Proof & Goal Infoview 6. Corollary III.1: Full Faithfulness & Global Map
Definition I.1 · Natural Transformation

Commuting Squares Between Functors

Let F, G : C → D be functors. A natural transformation α : F ⇒ G assigns to each object X ∈ C a component αX : F(X) → G(X) such that for every f : X → Y:

G(f) ∘ αX = αY ∘ F(f)

Highlight either path around the naturality square to inspect how components intertwine with functorial transport:

Showing full commuting square: both routes from F(X) to G(Y) agree.
F(X) G(X) F(Y) G(Y) αX G(f) F(f) αY ↺ =
Naturality square in category D for f : X → Y
Lean 4 Formalization · CovYoneda vs. Mathlib

Structures & Representable Functor Hom(A, −)

Click highlighted symbols (⥤, ⟶, ≫, naturality, Hom) to inspect their elaborated types in the Infoview:

-- Standalone definitions from sinhp/CovariantYonedaLean4 (2023)
-- (Replace lines below with verbatim lines from CovYoneda/src/yoneda.lean if desired)
structure Category (Obj : Type u) where
  Hom  : Obj → Obj → Type v
  id   : ∀ X, Hom X X
  comp : ∀ {X Y Z}, Hom X Y → Hom Y Z → Hom X Z

structure NatTrans (F G : C ⥤ D) where
  app : ∀ X : C, F.obj X ⟶ G.obj X
  naturality : ∀ {X Y : C} (f : X ⟶ Y),
    F.map f ≫ app Y = app X ≫ G.map f

def Hom (A : C) : C ⥤ Type v where
  obj X   := A ⟶ X
  map f g := g ≫ f
Lean 4 Infoview · Symbol Inspector Elaborated
Functor (C ⥤ D)
C ⥤ D : Type (max u₁ u₂ v₁ v₂)
Bundles an object map obj : C → D, a morphism map map : (X ⟶ Y) → (obj X ⟶ obj Y), and functoriality axioms map_id and map_comp.
Theorem II.1 · Nested Proof Architecture

The Covariant Yoneda Lemma:  Nat(HomC(A, −), F) ≅ F(A)

Steps 4 and 5 zoom inside Theorem II.1: first chasing idA across the naturality square, then stepping line-by-line through the mechanized Lean 4 proof in CovYoneda/src/yoneda.lean and Mathlib.
Subslip II.1a · Element Chasing

Why α Is Uniquely Determined by αA(idA)

Set X = A and evaluate naturality at f : A → Y applied to the universal element idA ∈ HomC(A, A):

αY(f) = αY(f ∘ idA) = (αY ∘ Hom(A, f))(idA) = F(f)(αA(idA))
Hom(A, A) id_A F(A) u := α_A(id_A) Hom(A, Y) f = f ∘ id_A F(Y) α_Y(f) = F(f)(u) αA f ∘ (−) F(f) αY
Interactive Element Trace:

Start at the universal element id_A in the top-left corner; every morphism f : A → Y is the image of id_A under post-composition.

Subslip II.1b · Lean 4 Tactic Proof

Mechanizing the Yoneda Equivalence

Click any tactic line below to step through the live Lean 4 proof state and goal turnstile (⊢):

-- Standalone Yoneda equivalence (sinhp/CovariantYonedaLean4, 2023)
-- (Paste verbatim lines from CovYoneda/src/yoneda.lean here if customizing)
def yoneda_fwd (α : NatTrans (Hom A) F) : F.obj A := α.app A (𝟙 A)
def yoneda_bwd (u : F.obj A) : NatTrans (Hom A) F := {
  app := fun _ f => F.map f u,
  naturality := fun g => by funext f; exact F.map_comp f g u
}
theorem yoneda_left_inv (α : NatTrans (Hom A) F) :
    yoneda_bwd (yoneda_fwd α) = α := by
  apply NatTrans.ext; funext Y f
  have h := congrFun (α.naturality f) (𝟙 A)
  rw [Category.id_comp] at h
  exact h.symm
Tactic State · Infoview CovYoneda · Line 1: apply NatTrans.ext; funext Y f
C : Type u
inst : Category C
A : C
F : C ⥤ Type v
α : NatTrans (Hom A) F
Y : C
f : A ⟶ Y
⊢ F.map f (α.app A (𝟙 A)) = α.app Y f
Corollary III.1 · Full Faithfulness

The Yoneda Embedding & Full Proof Architecture

Instantiating F := HomC(B, −) in Theorem II.1 yields a canonical bijection between morphisms in C and natural transformations between representable functors:

HomC(B, A) ≅ Nat(HomC(A, −), HomC(B, −))

Hence the contravariant Yoneda embedding よ : Cop ⥤ (C ⥤ Type) is fully faithful: two objects A, B ∈ C are isomorphic if and only if their representable functors are naturally isomorphic.

Yoneda.lean · FullyFaithful Instance ✓ Type-checked in Lean 4
def fullyFaithfulCoyoneda : (coyoneda (C := C)).FullyFaithful where
  preimage {X Y} α := (yonedaEquiv X.unop (coyoneda.obj Y)).toFun α
  map_preimage α   := (yonedaEquiv _ _).right_inv α
  preimage_map f   := by simp [yonedaEquiv]
Explore Full Formalization: github.com/sinhp/CovariantYonedaLean4
CovYoneda/src/yoneda.lean · Hosted on sinhp.github.io
GitHub ↗
Global Spatial Proof Graph (Overview)
The camera frames the complete 6-slip architecture—from Definition I.1 and CovariantYonedaLean4 (top) through the nested Yoneda Lemma subslips (bottom-left) to Corollary III.1 (bottom-right). Click any card to zoom back in.