Church Encoding
Tutorial Video
Text description (accessibility)
This video demonstrates the "Church Encoding" functional Rust example. Difficulty level: Expert. Key concepts covered: Functional Programming. Church encoding represents data and operations as pure functions (lambdas). Key difference from OCaml: 1. **Type verbosity**: OCaml infers Church numeral types; Rust requires explicit `Box<dyn Fn(...)>` types that are hard to write.
Tutorial
The Problem
Church encoding represents data and operations as pure functions (lambdas). Church numerals encode natural numbers: zero is λf.λx.x, one is λf.λx.f x, two is λf.λx.f (f x). Booleans, pairs, and lists can all be encoded as functions. This demonstrates that the lambda calculus is computationally complete — data and control flow are the same thing. Understanding Church encoding illuminates the foundation of functional programming and type theory.
🎯 Learning Outcomes
Code Example
#![allow(clippy::all)]
// Stub — awaiting conversion from OCaml source.Key Differences
Box<dyn Fn(...)> types that are hard to write.OCaml Approach
OCaml's Church encoding is more concise:
let zero f x = x
let succ n f x = f (n f x)
let add m n f x = m f (n f x)
let to_int n = n (fun x -> x + 1) 0
OCaml's type inference handles the complex function types automatically. The encoding is idiomatic OCaml and is used in courses on the lambda calculus and type theory.
Full Source
#![allow(clippy::all)]
// Stub — awaiting conversion from OCaml source.Deep Comparison
OCaml vs Rust: Church Encoding
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
true_ = |t| |f| t, false_ = |t| |f| f, and if_ = |cond| |then_| |else_| cond(then_)(else_).pair = |x| |y| |f| f(x)(y), fst = |p| p(|x| |_| x), snd = |p| p(|_| |y| y).