Optics: iso pattern
Tutorial Video
Text description (accessibility)
This video demonstrates the "Optics: iso pattern" functional Rust example. Difficulty level: Fundamental. Key concepts covered: Functional Programming. Optics are composable data accessors originating from Haskell's lens library (Edward Kmett, 2012). Key difference from OCaml: 1. **HKT requirement**: Haskell's van Laarhoven encoding uses Functor/Applicative for optic unification requiring HKT; Rust uses explicit struct types per optic kind.
Tutorial
The Problem
Optics are composable data accessors originating from Haskell's lens library (Edward Kmett, 2012). They solve the deeply-nested update problem in immutable data: updating a field three levels deep requires rebuilding all intermediate values. Optics compose — a lens into a struct field composed with a prism for an enum variant gives a combined accessor that can get, set, and modify deeply nested optional values. The optic hierarchy includes Lens (exactly one focus), Prism (zero or one focus on enum variants), Traversal (zero or more foci), and Iso (lossless bidirectional conversion).
🎯 Learning Outcomes
Code Example
#![allow(clippy::all)]
//! # Isomorphism Pattern
//! Bidirectional conversion between types.
pub struct Iso<A, B> {
pub to: Box<dyn Fn(A) -> B>,
pub from: Box<dyn Fn(B) -> A>,
}
impl<A, B> Iso<A, B> {
pub fn new(to: impl Fn(A) -> B + 'static, from: impl Fn(B) -> A + 'static) -> Self {
Iso {
to: Box::new(to),
from: Box::new(from),
}
}
}
pub fn celsius_fahrenheit() -> Iso<f64, f64> {
Iso::new(|c| c * 9.0 / 5.0 + 32.0, |f| (f - 32.0) * 5.0 / 9.0)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_iso() {
let iso = celsius_fahrenheit();
let f = (iso.to)(100.0);
assert!((f - 212.0).abs() < 0.001);
}
}Key Differences
^., .~, %~ for terse optic use; Rust uses method calls, more verbose but explicit.lens-rs and similar crates provide derive macros for automatic lens generation; OCaml uses ppx_lens for the same.OCaml Approach
OCaml optics use the same record-with-function approach:
type ('s, 'a) lens = { get: 's -> 'a; set: 's -> 'a -> 's }
let name_lens = { get = (fun u -> u.name); set = (fun u n -> { u with name = n }) }
let compose l1 l2 = { get = (fun s -> l2.get (l1.get s)); set = (fun s a -> l1.set s (l2.set (l1.get s) a)) }
Full Source
#![allow(clippy::all)]
//! # Isomorphism Pattern
//! Bidirectional conversion between types.
pub struct Iso<A, B> {
pub to: Box<dyn Fn(A) -> B>,
pub from: Box<dyn Fn(B) -> A>,
}
impl<A, B> Iso<A, B> {
pub fn new(to: impl Fn(A) -> B + 'static, from: impl Fn(B) -> A + 'static) -> Self {
Iso {
to: Box::new(to),
from: Box::new(from),
}
}
}
pub fn celsius_fahrenheit() -> Iso<f64, f64> {
Iso::new(|c| c * 9.0 / 5.0 + 32.0, |f| (f - 32.0) * 5.0 / 9.0)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_iso() {
let iso = celsius_fahrenheit();
let f = (iso.to)(100.0);
assert!((f - 212.0).abs() < 0.001);
}
}#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_iso() {
let iso = celsius_fahrenheit();
let f = (iso.to)(100.0);
assert!((f - 212.0).abs() < 0.001);
}
}
Deep Comparison
Isomorphism
Lossless conversion A <-> B