Church encoding is a means of embedding data and operators into the lambda calculus, the most familiar form being the church numerals, a representation of the natural numbers using lambda notation. The method is named for Alonzo Church, who first encoded data in the lambda calculus this way.
Terms that are usually considered primitive in other notations (such as integers, booleans, pairs, lists, and tagged unions) are mapped to higher-order functions under Church encoding; from the Church-Turing thesis we know that any computable operator (and its operands) can be represented under Church encoding.
Many students of mathematics are familiar with Gödel numbering members of a set; Church encoding is an equivalent operation defined on lambda abstractions instead of natural numbers.
Church numerals are the representations of natural numbers under Church encoding. The higher-order function that represents natural number is a function that maps any other function to its n-fold composition .
λf.λx. x
λf.λx. f x
λf.λx. f (f x)
λf.λx. f (f (f x))
λf.λx. fn x
That is, the natural number is represented by the Church numeral n, which has property that for any lambda-terms F and X,
F X =β Fn X
The addition function uses the identity .
λm.λn.λf.λx. m f (n f x)
The successor function is β-equivalent to (plus 1).
λn.λf.λx. f (n f x)
The multiplication function uses the identity .
λm.λn.λf. n (m f)
The exponentiation function is straightforward given our definition of church numerals.
λm.λn. n m
The predecessor function works by generating an -fold composition of functions that each apply their argument g to f; the base case discards its copy of f and returns x.
λn.λf.λx. n (λg.λh. h (g f)) (λu. x) (λu. u)
type Church a = (a -> a) -> a -> a
church :: Integer -> Church a
church 0 = \f -> \x -> x
church n = \f -> \x -> f (church (n-1) f x)
unchurch :: Church Integer -> Integer
unchurch n = n (\x -> x + 1) 0
Church booleans are the Church encoding of the boolean values true and false. Some programming languages use these as implementation model for boolean arithmetic; examples are Smalltalk and Pico. The boolean values are represented as functions of two values that evaluate to one or the other of their arguments.
Formal definition in lambda calculus:
λa.λb. a
λa.λb. b
Functions of boolean arithmetic can be derived for Church booleans:
λm.λn.λa.λb. m (n a b) b
λm.λn.λa.λb. m a (n a b)
λm.λa.λb. m b a
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Church encoding".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world