Day Convolution
Tutorial Video
Text description (accessibility)
This video demonstrates the "Day Convolution" functional Rust example. Difficulty level: Expert. Key concepts covered: Functional Programming. Day convolution is a monoidal product for functors: given two applicative functors `F` and `G`, `Day<F, G>` is their "tensor product." The construction packages values from both functors with a combining function, enabling them to be applied together. Key difference from OCaml: 1. **Monoidal structure**: Day convolution gives functors a tensor product; the unit of the monoid is the `Identity` functor — this is the categorical structure.
Tutorial
The Problem
Day convolution is a monoidal product for functors: given two applicative functors F and G, Day<F, G> is their "tensor product." The construction packages values from both functors with a combining function, enabling them to be applied together. Day convolution arises in effect systems (combining independent effect types), parser combinators (running two parsers on the same input), and signal processing abstractions. It is the categorical generalization of zip for applicatives.
🎯 Learning Outcomes
Day<F, G, A> = exists B, C. (F<B>, G<C>, B -> C -> A)zip and parallel applicative compositionCode Example
#![allow(clippy::all)]
// Stub — awaiting conversion from OCaml source.Key Differences
Identity functor — this is the categorical structure.B and C); Coyoneda requires one — Day is more complex.adjunctions, comonad-transformers, and lens internals; it is primarily a theoretical/library tool.zip for lists and liftA2 for applicatives are the everyday incarnations of Day convolution in programming.OCaml Approach
OCaml's Day convolution using GADTs:
type ('f, 'g, 'a) day =
Day : 'b 'f * 'c 'g * ('b -> 'c -> 'a) -> ('f, 'g, 'a) day
let run_option (Day (fb, gc, f)) =
match (fb, gc) with
| (Some b, Some c) -> Some (f b c)
| _ -> None
OCaml's GADT hides 'b and 'c cleanly. The run_option interpreter specializes Day to the Option functor.
Full Source
#![allow(clippy::all)]
// Stub — awaiting conversion from OCaml source.Deep Comparison
OCaml vs Rust: Day Convolution
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
run_vec: Day<Vec, Vec, A> -> Vec<A> using zip semantics.day(identity, fa, |(), a| a) is isomorphic to fa — the identity is the unit for Day.ap: Day<F, F, A> -> F<A> for Option<_>.