In mathematics, Church encoding is a method of representing various types of data in the lambda calculus. In the untyped lambda calculus the only primitive data type is the function, represented by lambda abstraction terms. Types that are usually considered primitive in other notations (such as integers, Booleans, pairs, lists, and tagged unions) are not present. As such, they must be constructed from lambda terms. Conceptually, they are constructed from higher-order functions. The Church numerals are a representation of the natural numbers using lambda notation. The method is named after Alonzo Church, who first encoded data in the lambda calculus this way. It can also be extended to represent other data types in a similar spirit. This article makes occasional use of the alternative syntax for lambda abstraction terms, where λx.λy.λz.N is abbreviated as λxyz.N, as well as the two standard combinators, I≡λx.x and K≡λxy.x, as needed.
Church pairs
Church pairs are the Church encoding of the pair (two-tuple) type. To have two things is to be able to supply them to any observer expecting of two things. The pair is thus represented as a function that takes a function argument. The pair itself does not decide what to do with the elements of the tuple. When given its argument it will apply the argument to the two components of the pair. The definition of the pair constructor, the select first element and the select second element functions in lambda calculus are,
pair≡λxy.λz.z x yfirst≡λp.p (λxy.x)second≡λp.p (λxy.y) For example,
first (pair a b)= (λp.p (λxy.x)) ((λxyz.z x y) a b)= (λp.p (λxy.x)) (λz.z a b)= (λz.z a b) (λxy.x)= (λxy.x) a b= a
Church Booleans Church Booleans encode the Boolean values true and false. Some programming languages use these as an implementation model for Boolean arithmetic; examples are Smalltalk and Pico. Boolean logic embodies a choice between two alternatives. Thus the Church encodings of true and false are functions of two parameters:
true chooses the first parameter; false chooses the second parameter. The two definitions in lambda calculus are:
true≡λa.λb.a =λa.λb.first(paira b)false≡λa.λb.b =λa.λb.second(paira b) These definitions allow predicates (i.e. functions returning logical values) to directly act as if-test clauses, so that if operator is just an identity function, and thus can be omitted. Each logical value already acts as an if, performing a choice between its two arguments. A Boolean value applied to two values returns either the first or the second value. The expression
test-clause then-clause else-clause returns then-clause if test-clause is true, and else-clause if test-clause is false. Because logical values like true and false choose their first or second argument, they can be combined to provide logical operators. Several implementations are usually possible, whether by directly manipulating parameters or by reducing to the more basic logical values. Here are the definitions, using the shortened notation as mentioned at the start of the article (p,q are predicates; a,b are general values):
if=λpab.p a b and=λpq.p q p=λpqab.p (q a b) bor=λpq.p p q=λpqab.p a (q a b)not=λp.pfalsetrue=λpab .p b axor=λpq.p (not q) q=λpqab.p (q b a) (q a b)nand=λpq.not (andp q)=λpqab.p (q b a) aimplies=λpq.or (notp) q=λpqab.p (q a b) a Some examples:
and true false=(λp.λq.p q p) true false=truefalsetrue=(λa.λb.a)falsetrue=false not true=(λp.λa.λb.p b a) (λa.λb.a)=λa.λb.(λa.λb.a) b a=λa.λb.(λc.b) a=λa.λb.b=false
Optional values An Optional value is represented as
none ≡ λf.λs. fval ≡ λv. λf.λs. s v Using such a value means supplying it with two arguments – one, f, for the "failure" case, i.e. lack of a value, and the other, s, in case of "success," is a handler function which is presented with that value. The optional value itself "knows" which case it is, and selects which argument to use, accordingly. It "knows" this by virtue of being created that way – either it was created as none, or as valv. The user of an optional value has no way of knowing which case it is except by supplying it with the two arguments, one for each of the two possible cases.
Church numerals Church numerals are the representations of natural numbers under Church encoding. The higher-order function that represents natural number n is a function that maps any function f to its n-fold composition. In simpler terms, a numeral represents the number by applying any given function that number of times in sequence, starting from any given starting value:
n:f↦f∘n f∘n(x)=(f∘f∘…∘f⏟n times)(x)=f(f(…(f⏟n times(x))…)) Church encoding is thus a unary encoding of natural numbers, corresponding to simple counting. Each Church numeral achieves this by construction. All Church numerals are functions that take two parameters. Church numerals 0, 1, 2, ..., are defined as follows in the lambda calculus:
Starting with 0 not applying the function at all, proceed with 1 applying the function once, 2 applying the function twice in a row, 3 applying the function three times in a row, etc.: NumberFunction definitionLambda expression00 f x=x0=λf.λx.x11 f x=f x1=λf.λx.f x22 f x=f (f x)2=λf.λx.f (f x)33 f x=f (f (f x))3=λf.λx.f (f (f x))⋮⋮⋮nn f x=f∘n xn=λf.λx.f∘n x The Church numeral 3 is a chain of three applications of any given function in sequence, starting from some value. The supplied function is first applied to a supplied argument and then successively to its own result. The end result is not the number 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 something three times. It is an ostensive demonstration of what is meant by "three times".
Calculation with Church numerals Arithmetic operations on numbers produce numbers as their results. In Church encoding, these operations are represented by lambda abstractions which, when applied to Church numerals representing the operands, beta-reduce to the Church numerals representing the results. Church representation of addition, plus(m,n)=m+n, uses the identity f∘(m+n)(x)=(f∘m∘f∘n)(x)=f∘m(f∘n(x)):
plus≡λmn.λfx.m f (n f x) The successor operation, succ(n)=n+1, is obtained by β-reducing the expression "plus 1":
succ≡λn.λfx.f (n f x) Multiplication, mult(m,n)=m∗n, uses the identity f∘(m∗n)(x)=(f∘n)∘m(x):
… excerpt ends here. Continue reading the full article.