In mathematics, Church encoding is a means of representing data and operators in the lambda calculus. The data and operators form a mathematical structure which is embedded in the lambda calculus. The Church numerals are 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. The Church-Turing thesis asserts that any computable operator (and its operands) can be represented under Church encoding. In the untyped lambda calculus the only primitive data type is the function. The Church-Turing thesis is that lambda calculus is Turing complete.
The Church encoding is not intended as a practical implementation of primitive data types. Its use is to show that other primitives data types are not required to represent any calculation. The completeness is representational. Additional functions are needed to translate the representation into common data types, for display to people. It is not possible in general to decide if two functions are extensionally equal due to the undecidability of equivalence from Church's theorem. The translation may apply the function in some way to retrieve the value it represents, or look up its value as a literal lambda term.
Lambda calculus is usually interpreted as using intensional equality. There are potential problems with the interpretation of results because of the difference between the intensional and extensional definition of equality.
- 1 Church numerals
- 2 Church Booleans
- 3 Predicates
- 4 Church pairs
- 5 List encodings
- 6 Derivation of predecessor function
- 7 See also
- 8 References
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 function to its n-fold composition. In simpler terms, the "value" of the numeral is equivalent to the number of times the function encapsulates its argument.
All Church numerals are functions that take two parameters. Church numerals 0, 1, 2, ..., are defined as follows in the lambda calculus:
Then starting with zero, being not applying the function, then one being applying the function, ...
|Number||Function definition||Lambda expression|
The Church numeral 3 represents the action of applying any given function three times to a value. The supplied function is first applied to a supplied parameter and then successively to its own result. The end result is not the numeral 3 (unless the supplied parameter happens to be 0 and the function is a successor function). The function itself, and not its end result, is the Church numeral 3. The Church numeral 3 means simply to do anything three times. It is an ostensive demonstration of what is meant by "three times".
Calculation with Church numerals
Arithmetic operations on numbers may be represented by functions on Church numerals. These functions may be defined in lambda calculus, or implemented in most functional programming languages (see converting lambda expressions to functions).
The addition function uses the identity .
The successor function is β-equivalent to .
The multiplication function uses the identity .
The exponentiation function is given by the definition of a Church numerals; . In the definition substitute to get and,
which gives the lambda expression,
The function is more difficult to understand.
A Church numeral applies a function n times. The predecessor function must return a function that applies it's parameter n - 1 times. This is achieved by building a container around f and x, which is initialized in a way that omits the application of the function the first time. See predecessor for a more detailed explanation.
The subtraction function can be written based on the predecessor function.
Table of functions on Church numerals
|Function||Algebra||Identity||Function definition||Lambda expressions|
* Note that in the Church encoding,
Translation with other representations
Most real-world languages have support for machine-native integers; the church and unchurch functions convert between nonnegative integers and their corresponding church numerals. The functions are given here in Haskell, where the
\ corresponds to the λ of Lambda calculus. Implementations in other languages are similar.
type Church a = (a -> a) -> (a -> a) church :: Integer -> Church Integer 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
Boolean logic may be considered as a choice. The Church encoding of true and false are functions of two parameters;
- true chooses the first parameter.
- false chooses the second parameter.
The two definitions are known as Church Booleans;
This definition allows predicates (i.e. functions returning logical values) to directly act as if-clauses. A function returning a Boolean, which is then applied to two parameters, returns either the first or the second parameter;
evaluates to then-clause if predicate x evaluates to true, and to else-clause if predicate x evaluates to false.
Because true and false choose the first or second parameter they may be combined to provide logic operators,
- - This is only a correct implementation if the evaluation strategy is applicative order.
- - This is only a correct implementation if the evaluation strategy is normal order.
A predicate is a function that returns a Boolean value. The most fundamental predicate is , which returns if its argument is the Church numeral , and if its argument is any other Church numeral:
The following predicate tests whether the first argument is less-than-or-equal-to the second:
Because of the identity,
The test for equality may be implemented as,
Church pairs are the Church encoding of the pair (two-tuple) type. The pair is represented as a function that takes a function argument. When given its argument it will apply the argument to the two components of the pair. The definition in lambda calculus is,
|nil||Construct an empty list.|
|isnil||Test if list is empty.|
|cons||Prepend a given value to a (possibly empty) list.|
|head||Get the first element of the list.|
|tail||Get the rest of the list.|
Three different representations of lists are given.
- Build each list node from two pairs (to allow for empty lists).
- Build each list node from one pair.
- Represent the list using the right fold function.
Two pairs as a list node
A nonempty list can implemented by a Church pair;
- First contains the head.
- Second contains the tail.
However this does not give a representation of the empty list, because there is no "null" pointer. To represent null, the pair may be wrapped in another pair, giving free values,
- First - Is the null pointer (empty list).
- Second.First contains the head.
- Second.Second contains the tail.
Using this idea the basic list operations can be defined like this:
|The first element of the pair is true meaning the list is null.|
|Retrieve the null (or empty list) indicator.|
|Create a list node, which is not null, and give it a head h and a tail t.|
|second.first is the head.|
|second.second is the tail.|
In a nil node second is never accessed, provided that head and tail are only applied to nonempty lists.
One pair as a list node
where the last definition is a special case of the general
Represent the list using right fold
As an alternative to the encoding using Church pairs, a list can be encoded by identifying it with its right fold function. For example, a list of three elements x, y and z can be encoded by a higher-order function which when applied to a combinator c and a value n returns c x (c y (c z n)).
Derivation of predecessor function
The predecessor function used in the Church Encoding is,
To build the predecessor we need a way of applying the function 1 less time. A numeral n applies the function f n times to x. The predecessor function must use the numeral n to apply the function n-1 times.
Before implementing the predecessor function, here is a scheme that wraps the value in a container function. We will define new functions to use in place of f and x, called inc and init. The container function is called value. The left hand side of the table shows a numeral n applied to inc and init.
|Number||Using init||Using const|
The general recurrence rule is,
If there is also a function to retrieve the value from the container (called extract),
Then extract may be used to define the samenum function as,
The samenum function is not intrinsically useful. What we want is the initial value to skip an application of f. Call this new initial container const. The right hand side of the above table shows the expansions of n inc const. Then by replacing init with const in the expression for the same function we get the predecessor function,
As explained below the functions inc, init, const, value and extract may be defined as,
Which gives the lambda expression for pred as,
The value container applies a function to its value. It is defined by,
The inc function should take a value containing v, and return a new value containing f v.
Letting g be the value container,
The value may be extracted by applying the identity function,
To implement pred the init function is replaced with the const that does not apply f. We need const to satisfy,
Which is satisfied if,
Or as a lambda expression,
- Directly Reﬂective Meta-Programming
- Church numerals and booleans explained by Robert Cartwright at Rice University
- Theoretical Foundations For Practical 'Totally Functional Programming' (Chapters 2 and 5) All about Church and other similar encodings, including how to derive them and operations on them, from first principles
- Some interactive examples of Church numerals
- This formula is the definition of a Church numeral n with f -> m, x -> f.
- Pierce, Benjamin C. (2002). Types and Programming Languages. MIT Press. p. 500. ISBN 978-0-262-16209-8.
- John Tromp, Binary Lambda Calculus and Combinatory Logic, in Randomness And Complexity, from Leibniz To Chaitin, ed. Cristian S. Calude, World Scientific Publishing Company, October 2008. (pdf version)