Operational Monad
Tutorial Video
Text description (accessibility)
This video demonstrates the "Operational Monad" functional Rust example. Difficulty level: Expert. Key concepts covered: Functional Programming. The operational monad is a variant of the free monad that separates the description of primitive instructions from the composition mechanism (bind/sequence). Key difference from OCaml: 1. **Result type tracking**: OCaml's GADT `'a instruction` encodes the result type per instruction; Rust's `Instruction<A>` simulates this with continuations.
Tutorial
The Problem
The operational monad is a variant of the free monad that separates the description of primitive instructions from the composition mechanism (bind/sequence). Instead of encoding both instructions and sequencing in one recursive type, the operational monad has two layers: Instruction<A> (user-defined operations) and Program<A> (sequencing). This reduces boilerplate compared to free monads and makes adding new instructions simpler — only the instruction type changes.
🎯 Learning Outcomes
Program<A> wraps instructions with a universal bind mechanismCode Example
#![allow(clippy::all)]
// Stub — awaiting conversion from OCaml source.Key Differences
'a instruction encodes the result type per instruction; Rust's Instruction<A> simulates this with continuations.Instruction; the Program wrapper and interpreter are unaffected (open-closed principle).OCaml Approach
OCaml's operational monad uses GADTs for the instruction type:
type _ instruction =
| ReadLine : string instruction
| Print : string -> unit instruction
type _ program =
| Done : 'a -> 'a program
| Instr : 'a instruction * ('a -> 'b program) -> 'b program
The GADT instruction type ensures that ReadLine : string instruction — the instruction's result type is tracked. Rust's version uses boxed closures to simulate this.
Full Source
#![allow(clippy::all)]
// Stub — awaiting conversion from OCaml source.Deep Comparison
OCaml vs Rust: Operational Monad
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
Sleep(duration_ms: u64) instruction and implement both a real (actual sleep) and mock (instant) interpreter for it.Print operations into a single buffered write.to_io_actions(program: Program<A>) -> Vec<IoAction> function that serializes a program to a list of actions without executing them.