ExamplesBy LevelBy TopicLearning Paths
235 Expert

Yoneda Lemma

Functional Programming

Tutorial Video

Text description (accessibility)

This video demonstrates the "Yoneda Lemma" functional Rust example. Difficulty level: Expert. Key concepts covered: Functional Programming. The Yoneda lemma is one of the most important results in category theory: `Nat(Hom(A, -), F) ≅ F(A)`. Key difference from OCaml: 1. **Rank

Tutorial

The Problem

The Yoneda lemma is one of the most important results in category theory: Nat(Hom(A, -), F) ≅ F(A). In Rust terms: a natural transformation from fn(A, -) to any functor F is in one-to-one correspondence with a value of F(A). The practical consequence: Yoneda<F, A> — a type holding a natural transformation — is isomorphic to F(A> but enables free map operations without actually running them, accumulating a composition chain that is applied all at once.

🎯 Learning Outcomes

  • • Understand the Yoneda lemma as a correspondence between natural transformations and functor values
  • • Learn how Yoneda<F, A> defers and fuses map operations (Yoneda's performance benefit)
  • • See the proof: to_yoneda(fa)(id) = fa and from_yoneda(y) = y(id)
  • • Connect to free Functor instances: any type with a Yoneda wrapper gets map for free
  • Code Example

    #![allow(clippy::all)]
    // Stub — awaiting conversion from OCaml source.

    Key Differences

  • Rank-2 types: OCaml's 'b. record polymorphism expresses Yoneda directly; Rust requires defunctionalization (GAT markers).
  • Performance benefit: Both languages benefit from Yoneda map fusion; Haskell's library uses it for stream fusion; Rust's iterator fusion is an analogous (compiler-level) optimization.
  • Free Functor: Yoneda provides a free Functor instance for any type constructor — even non-functors can be mapped over via Yoneda lifting.
  • Practical use: Yoneda is primarily educational; the performance benefit is achieved automatically by LLVM in Rust for iterator chains.
  • OCaml Approach

    OCaml's Yoneda:

    type ('f, 'a) yoneda = { run : 'b. ('a -> 'b) -> 'b 'f }
    let to_yoneda fa = { run = fun f -> map f fa }
    let from_yoneda y = y.run Fun.id
    let map_yoneda f y = { run = fun g -> y.run (g >> f) }
    

    map_yoneda fuses the function f into the natural transformation without actually running it. OCaml's rank-2 type 'b. enables the polymorphic run field.

    Full Source

    #![allow(clippy::all)]
    // Stub — awaiting conversion from OCaml source.

    Deep Comparison

    OCaml vs Rust: Yoneda Lemma

    Overview

    See the example.rs and example.ml files for detailed implementations.

    Key Differences

    AspectOCamlRust
    Type systemHindley-MilnerOwnership + traits
    MemoryGCZero-cost abstractions
    MutabilityExplicit refmut keyword
    Error handlingOption/ResultResult<T, E>

    See README.md for detailed comparison.

    Exercises

  • Implement to_yoneda and from_yoneda for Option<A> and verify the round-trip: from_yoneda(to_yoneda(fa)) == fa.
  • Demonstrate map fusion: apply 10 map operations via Yoneda and verify only one actual traversal occurs.
  • Prove the Yoneda isomorphism: to_yoneda(from_yoneda(y)) == y for a specific y.
  • Open Source Repos