Lably
Skip to content

Church encoding

Representation of data of various types in lambda calculus

  • 19 min read
  • 8 chapters

Related on Lably

Through Mathematics, Data type and Value (computer science)

In the untyped lambda calculus the only primitive data type are functions, 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 natively present.

Hence the need arises to have ways to represent the data of these varying types by lambda terms, that is, by functions that are taking functions as their arguments and are returning functions as their results.

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. It can also be extended to represent other data types in the 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.zxyfirst≡λp.p(λxy.x)second≡λp.p(λxy.y)

For example,

first⁡(pair⁡ab)=(λp.p(λxy.x))((λxyz.zxy)ab)=(λp.p(λxy.x))(λz.zab)=(λz.zab)(λxy.x)=(λxy.x)ab=a

Sign in to keep reading

7 more chapters are free with a Lably account.

More in Topics in mathematics