Comonad Basics
Tutorial Video
Text description (accessibility)
This video demonstrates the "Comonad Basics" functional Rust example. Difficulty level: Expert. Key concepts covered: Functional Programming. A comonad is the categorical dual of a monad. Key difference from OCaml: 1. **Extract vs. pure**: `extract` always succeeds (comonads are "full" containers with at least one element); `pure` always injects (monads can be empty).
Tutorial
The Problem
A comonad is the categorical dual of a monad. Where a monad has pure: A -> M<A> (inject a value) and bind: (A -> M<B>) -> M<A> -> M<B> (sequence effects), a comonad has extract: W<A> -> A (extract the focused value) and extend: (W<A> -> B) -> W<A> -> W<B> (apply a context-dependent function to every position). Comonads model computations that consume context: cellular automata, image processing, signal filtering, and zipper-based navigation.
🎯 Learning Outcomes
extract, duplicate, and extendNonEmptyVec (with a focused position) as a concrete comonadCode Example
#![allow(clippy::all)]
// Stub — awaiting conversion from OCaml source.Key Differences
extract always succeeds (comonads are "full" containers with at least one element); pure always injects (monads can be empty).extend f w computes f at every position of w, using the whole w as context for each position — this is the "cellular automaton" pattern.extract (extend f w) = f w mirrors bind return = id.extend is used for image convolution (each pixel computed from a neighborhood), sliding window statistics, and breadcrumb navigation.OCaml Approach
OCaml's comonad:
module type COMONAD = sig
type 'a t
val extract : 'a t -> 'a
val extend : ('a t -> 'b) -> 'a t -> 'b t
end
module ZipperComonad : COMONAD with type 'a t = 'a zipper = struct
let extract z = z.focus
let extend f z = map (fun z' -> f z') (all_positions z)
end
The zipper comonad is the standard OCaml comonad example. Comonads appear in OCaml in data-flow programming and attribute grammars.
Full Source
#![allow(clippy::all)]
// Stub — awaiting conversion from OCaml source.Deep Comparison
OCaml vs Rust: Comonad Basics
Overview
See the example.rs and example.ml files for detailed implementations.
Key Differences
| Aspect | OCaml | Rust |
|---|---|---|
| Type system | Hindley-Milner | Ownership + traits |
| Memory | GC | Zero-cost abstractions |
| Mutability | Explicit ref | mut keyword |
| Error handling | Option/Result | Result<T, E> |
See README.md for detailed comparison.
Exercises
duplicate: W<A> -> W<W<A>> in terms of extend and verify it gives all focused sub-contexts.extend on Focused<u8>: each cell's next state depends on itself and its two neighbors.extend: each position's value is the average of the 3 surrounding positions.