ArticleslgStudy

mathematics

Church encoding

Church encoding is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Church encoding rather than just read about it. In short: 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.

Key takeaways

  • Church encoding belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Church encoding to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Church encoding from memory before moving on to harder problems.

Reference excerpt

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⁡(pair⁡a b)false≡λa.λb.b =λa.λb.second⁡(pair⁡a 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.pfalse⁡true=λpab .p b axor=λpq.p (not⁡ q) q=λpqab.p (q b a) (q a b)nand=λpq.not⁡ (and⁡p q)=λpqab.p (q b a) aimplies=λpq.or⁡ (not⁡p) q=λpqab.p (q a b) a Some examples:

and⁡ true⁡ false=(λp.λq.p q p) true⁡ false=true⁡false⁡true=(λa.λb.a)false⁡true=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 val⁡v. 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.

Worked examples

Example 1 — a first encounter with Church encoding

Start with the simplest possible case. Write down what Church encoding claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Church encoding before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Church encoding ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Church encoding

In research
Church encoding appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Church encoding in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Church encoding is common in secondary-school and first-year university syllabi. It links to neighbouring topics Lambda calculus, so understanding it makes those chapters shorter.
In everyday life
Look for Church encoding outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Church encoding” →

How to study Church encoding in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Church encoding means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Church encoding out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Church encoding in simple terms?

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.

Why does Church encoding matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Church encoding?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Church encoding.

Tags

  • Lambda calculus

Keep exploring