Church Numerals is a representation of the natural number using lambda notation, defined as follows:
0 ≡ λf.λx. x
1 ≡ λf.λx. f x
2 ≡ λf.λx. f (f x)
3 ≡ λf.λx. f (f (f x))
...
n ≡ λf.λx. fn x
...
That is, n F x == F^n x, where n is a Church numeral. Zero can be represented as follows in Scheme:
(define zero (lambda (f) (lambda (x) x)))
Scheme implementations for calculations with Church Numeral:
- Plus: plus(m, n) = m + n (where m,n are both Church numerals) can be inferred from F^(m+n) (x) == F^m(F^n(x)) == m F (n F x), so in lambda notation, plus function can be expressed as λm.λn.λf.λx. m f (n f x). Scheme implementation:
(define (plus m n) (lambda (f) (lambda (x) ((m f) ((n f) x)))))
- Successor: succ(n) = n + 1 (where n is a Church numeral) can be inferred directly from plus function as plus(n, 1) == F (n F x). That is, two above function are β-equivalent. Scheme implementation:
(define (succ n) (lambda (f) (lambda (x) (f ((n f) x)))))
- Multiply: mult(m, n) = m * n (where m,n are both Church numerals) can be inferred from identical equation F^(m*n) == F^m^n == n (m F), while in lambda notation: λm.λn.λf. n (m f). Scheme implementation:
(define (mult m n) (lambda (f) (n (m f))))
- Exponentiation: exp(m, n) = m^n (where m,n are both Church numerals) comes right from the definition of Church numerals which is, in lambda notation, λm.λn. n m. Scheme implementation:
(define (exp m n) (n m))
- Predecessor: Predecessor function is a bit more complex. pred(n) = n - 1 (where n is a Church numeral and n != 0) works by generating an n-fold composition of functions that each apply their argument g to f. In lambda notation it can be expressed as: λn.λf.λx. n (λg.λh. h (g f)) (λu. x) (λu. u). Scheme implementation:
(define (pred n) (lambda (f) (lambda (x) (((n (lambda (g) (lambda (h) (h (g f))))) (lambda (u) x)) (lambda (u) u)))))
- A Church numeral can be converted to digit by invoking following function:
(define (to-digit n) ((n (lambda (k) (+ k 1))) 0))
Reference:
Church encoding in Wikipedia http://en.wikipedia.org/wiki/Church_encoding