Singleton Types
Tutorial Video
Text description (accessibility)
This video demonstrates the "Singleton Types" functional Rust example. Difficulty level: Expert. Key concepts covered: Functional Programming. A singleton type has exactly one value — the type and its single inhabitant are isomorphic. Key difference from OCaml: 1. **Pattern matching**: OCaml's GADT singletons can be matched to recover the underlying value; Rust's trait
Tutorial
The Problem
A singleton type has exactly one value — the type and its single inhabitant are isomorphic. Singleton types bridge the gap between values and types, enabling dependent-type-like programming where a runtime value is "promoted" to the type level. This is used in type-safe array indexing (a bound checked once becomes a type-level certificate), in protocol specifications, and in any setting where you want to carry proof of a runtime fact in the type system.
🎯 Learning Outcomes
Singleton trait pattern in Rust: a type with a unique VALUE constantCode Example
#![allow(clippy::all)]
// Stub — awaiting conversion from OCaml source.Key Differences
const VALUE for recovery.VALUE or pattern matching, not stored.OCaml Approach
OCaml can encode singleton types using GADTs where each constructor carries a unique type index:
type 'a sing =
| STrue : bool sing
| SFalse : bool sing
| SZero : int sing
| SSucc : 'n sing -> (int) sing
OCaml's GADTs allow the singleton value to be recovered from the type directly in pattern matches, making the bridge between types and values more natural than in Rust.
Full Source
#![allow(clippy::all)]
// Stub — awaiting conversion from OCaml source.Deep Comparison
OCaml vs Rust: Singleton Types
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
struct True; struct False; as Rust boolean singletons with a BoolSingleton trait and const VALUE: bool.BoundedIndex<const N: usize> type that can only be constructed when the index is less than N, providing a compile-time certificate of validity.to_runtime<S: Singleton<bool>>(s: S) -> bool that recovers the runtime value from the singleton type.