ExamplesBy LevelBy TopicLearning Paths
596 Expert

Church encoding rust

Functional Programming

Tutorial Video

Text description (accessibility)

This video demonstrates the "Church encoding rust" functional Rust example. Difficulty level: Expert. Key concepts covered: Functional Programming. This example explores advanced type theory concepts applied to Rust. Key difference from OCaml: 1. **HKT gap**: Haskell and partly OCaml have higher

Tutorial

The Problem

This example explores advanced type theory concepts applied to Rust. Church encoding, Scott encoding, finally tagless, free monads, and effect systems are all techniques from the typed lambda calculus and category theory research community. They demonstrate how to encode data and control as pure functions, how to build extensible DSLs without committing to a representation, and how to model effects as values rather than side effects. These patterns originate in Haskell and OCaml research and require adapting to Rust's ownership model.

🎯 Learning Outcomes

  • • The theoretical foundation of this encoding/pattern from type theory
  • • How it maps to Rust's type system using traits, generics, and higher-kinded type emulation
  • • The practical limitations and verbosity compared to Haskell/OCaml implementations
  • • When this pattern provides genuine value vs when simpler alternatives suffice
  • • Real systems that use these ideas: compilers, effect libraries, DSL frameworks
  • Code Example

    fn church_true<T>() -> impl Fn(T, T) -> T { |a, _| a }
    fn church_false<T>() -> impl Fn(T, T) -> T { |_, b| b }
    fn to_bool(b: impl Fn(bool, bool) -> bool) -> bool { b(true, false) }

    Key Differences

  • HKT gap: Haskell and partly OCaml have higher-kinded types; Rust uses GATs and trait tricks as approximations — significantly more verbose.
  • Type inference: OCaml's HM inference handles these patterns cleanly; Rust often requires explicit type annotations throughout.
  • Practical value: In Haskell, these patterns are common in production code; in Rust, simpler alternatives (enums + match) usually suffice.
  • Research to practice: These patterns showcase Rust's expressiveness limits and inspire ongoing language design work (GATs, async traits, HKT proposals).
  • OCaml Approach

    OCaml is the natural home for these patterns — they originate in the ML/Haskell research community:

    (* OCaml's polymorphism and first-class modules make these patterns
       more elegant than in Rust. Higher-kinded types are emulated in OCaml
       using functors and first-class modules rather than GATs. *)
    

    Full Source

    #![allow(clippy::all)]
    //! # Church Encoding
    //!
    //! Represent data using only functions (lambda calculus style).
    
    /// Church numeral type - a number is how many times you apply f.
    pub type ChurchNum<T> = Box<dyn Fn(Box<dyn Fn(T) -> T>) -> Box<dyn Fn(T) -> T>>;
    
    /// Church zero - apply f zero times.
    pub fn zero<T: 'static>() -> impl Fn(Box<dyn Fn(T) -> T>) -> Box<dyn Fn(T) -> T> {
        |_f| Box::new(|x| x)
    }
    
    /// Church successor - apply f one more time.
    pub fn succ<T: Clone + 'static>(
        n: impl Fn(Box<dyn Fn(T) -> T>) -> Box<dyn Fn(T) -> T> + 'static,
    ) -> impl Fn(Box<dyn Fn(T) -> T>) -> Box<dyn Fn(T) -> T> {
        move |f: Box<dyn Fn(T) -> T>| {
            let nf = n(Box::new(move |x| f(x)));
            Box::new(move |x| {
                let inner = nf(x);
                inner
            })
        }
    }
    
    /// Convert Church numeral to integer.
    pub fn to_int(n: impl Fn(Box<dyn Fn(i32) -> i32>) -> Box<dyn Fn(i32) -> i32>) -> i32 {
        n(Box::new(|x| x + 1))(0)
    }
    
    /// Church boolean - true.
    pub fn church_true<T>() -> impl Fn(T, T) -> T {
        |a, _b| a
    }
    
    /// Church boolean - false.
    pub fn church_false<T>() -> impl Fn(T, T) -> T {
        |_a, b| b
    }
    
    /// Church boolean to Rust bool.
    pub fn to_bool(b: impl Fn(bool, bool) -> bool) -> bool {
        b(true, false)
    }
    
    /// Church pair constructor.
    pub fn pair<A: Clone + 'static, B: Clone + 'static>(
        a: A,
        b: B,
    ) -> impl Fn(Box<dyn Fn(A, B) -> A>) -> A + Clone {
        move |f| f(a.clone(), b.clone())
    }
    
    /// Church pair first.
    pub fn fst<A: Clone, B>(p: impl Fn(Box<dyn Fn(A, B) -> A>) -> A) -> A {
        p(Box::new(|a, _b| a))
    }
    
    /// Simple demonstration with closures.
    pub fn demo_church_bool() -> (bool, bool) {
        let t = church_true();
        let f = church_false();
        (to_bool(t), to_bool(f))
    }
    
    #[cfg(test)]
    mod tests {
        use super::*;
    
        #[test]
        fn test_church_bool() {
            assert!(to_bool(church_true()));
            assert!(!to_bool(church_false()));
        }
    
        #[test]
        fn test_zero() {
            assert_eq!(to_int(zero()), 0);
        }
    
        #[test]
        fn test_demo() {
            let (t, f) = demo_church_bool();
            assert!(t);
            assert!(!f);
        }
    }
    ✓ Tests Rust test suite
    #[cfg(test)]
    mod tests {
        use super::*;
    
        #[test]
        fn test_church_bool() {
            assert!(to_bool(church_true()));
            assert!(!to_bool(church_false()));
        }
    
        #[test]
        fn test_zero() {
            assert_eq!(to_int(zero()), 0);
        }
    
        #[test]
        fn test_demo() {
            let (t, f) = demo_church_bool();
            assert!(t);
            assert!(!f);
        }
    }

    Deep Comparison

    OCaml vs Rust: Church Encoding

    Church encoding represents data purely with functions.

    Church Booleans

    OCaml

    let church_true  a b = a
    let church_false a b = b
    let to_bool b = b true false
    

    Rust

    fn church_true<T>() -> impl Fn(T, T) -> T { |a, _| a }
    fn church_false<T>() -> impl Fn(T, T) -> T { |_, b| b }
    fn to_bool(b: impl Fn(bool, bool) -> bool) -> bool { b(true, false) }
    

    Church Numerals

    A number N is represented as "apply f N times":

  • • 0 = λf.λx.x
  • • 1 = λf.λx.f x
  • • 2 = λf.λx.f (f x)
  • Key Differences

    AspectOCamlRust
    ClosuresLightweightRequire explicit types
    Higher-rankEasyRequires impl Fn or dyn
    BoxingGC handlesManual Box<dyn Fn>

    Exercises

  • Minimal implementation: Implement the simplest possible version of this pattern for a two-case example (e.g., for Church encoding, for finally tagless).
  • Add an interpreter: If the pattern supports multiple interpretations (like finally tagless), add a second interpreter (e.g., a pretty-printer in addition to an evaluator).
  • Comparison: Implement the same functionality using a plain enum + match — compare the code size, type safety, and extensibility of both approaches.
  • Open Source Repos