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, byfunctions that are taking functions as their arguments and are returning functions as their results.
TheChurch numerals are a representation of the natural numbers using lambda notation. The method is named forAlonzo 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 thealternative syntax for lambda abstraction terms, where λx.λy.λz.N is abbreviated as λxyz.N, as well as the two standard combinators, and, as needed.
Church pairs are the Church encoding of thepair (two-tuple) type. The pair is 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, theselect first element and theselect second element functions inlambda calculus are,
Church Booleans encode the Boolean valuestrue andfalse. Some programming languages use these as an implementation model for Boolean arithmetic; examples areSmalltalk andPico.
Boolean logic embodies achoice betweentwo alternatives. Thus the Church encodings oftrue andfalse are functions of two parameters:
true chooses the first parameter ;
false chooses the second parameter.
The two definitions in lambda calculus are:
These definitions allow predicates (i.e. functions returninglogical values) to directly act asif-test clauses, so thatif operator is just an identity function, and thus can be omitted. Each logical value already acts as anif, performing a choice between its two arguments. A Boolean value applied to two values returns either the first or the second value. The expression
Because logical values liketrue andfalse 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):
Church numerals are the representations ofnatural numbers under Church encoding. Thehigher-order function that represents natural numbern is a function that maps any function to itsn-foldcomposition. 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:
Church encoding is thus aunary encoding of natural numbers,[1] corresponding to simplecounting. Each Church numeral achieves this by construction.
All Church numerals are functions that take two parameters. Church numerals0,1,2, ..., are defined as follows in thelambda calculus:
Starting with0not applying the function at all, proceed with1applying the function once,2 applying the function twice in a row,3applying the function three times in a row, etc.:
The Church numeral3 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 asuccessor function). The function itself, and not its end result, is the Church numeral3. The Church numeral3 means simply to do something three times. It is anostensive demonstration of what is meant by "three times".
Arithmetic operations on numbers produce numbers as their results. In Church encoding, these operations are represented bylambda abstractions which, when applied to Church numerals representing the operands, beta-reduce to the Church numerals representing the results.
Church representation of addition,, uses the identity:
The successor operation,, is obtained byβ-reducing the expression "":
Multiplication,, uses the identity:
Thus and, and so by the virtue of Church encoding expressing then-fold composition, the exponentiation operation is given by
The predecessor operation is a little bit more involved. We need to devise an operation that when repeated times will result in applications of the given function. This is achieved by using the identity function instead, one time only, and then switching back to:
As previously mentioned, is the identity function,. See below for a detailed explanation. This suggests implementing e.g. halving and factorial in the similar fashion,
For example, beta-reduces to, beta-reduces to, and beta-reduces to.
Subtraction,, is expressed by repeated application of the predecessor operation a given number of times, just like addition can be expressed by repeated application of the successor operation a given number of times, etc.:
is thetetration operation,. Similarly to the factorial definition above, it can also be defined by using the intrinsic properties of the Church encoding, creating the "code" expression for it and letting the Church numerals themselves do the rest:
The idea here is as follows. The only thing known to the Church numeral is the numeral itself. Given two arguments and, as usual, the only thing it can do is to apply that numeral to the two arguments, somehow modified so that then-long chain of applications thus created will have one (specifically, leftmost) in the chain replaced by the identity function:
Here is the modified, and is the modified. Since itself can not be changed, its behavior can only be modified through an additional argument,.
The goal is achieved, then, by passing that additional argument alongfrom the outside in, while modifying it as necessary, with the definitions
Which is exactly what we have in the definition's lambda expression.
Now it is easy enough to see that
i.e. by eta-contraction and then by induction, it holds that
The identityabove may be coded with the explicit use of pairs. It can be done in several ways, for instance,
The expansion for is:
This is a simpler definition to devise but leads to a more complex lambda expression,
Pairs in the lambda calculus are essentially just extra arguments, whether passing them inside out like here, or from the outside in as in the original definition. Another encoding follows the second variant of the predecessor identity directly,
This way it is already quite close to the original, "outside-in" definition, also creating the chain ofs like it does, only in a bit more wasteful way still. But it is very much less wasteful than the previous, definition here. Indeed if we trace its execution we arrive at the new, even more streamlined, yet fully equivalent, definition
which makes it fully clear and apparent that this is all about just argument modification and passing. Its reduction proceeds as
clearly showing what is going on. Still, the original is much preferable since it's working in the top-down manner and is thus able to stop right away if the user-supplied function is short-circuiting. The top-down approach is also used with other definitions like
Division of natural numbers may be implemented by,[2]
Calculating with takes many beta reductions. Unless doing the reduction by hand, this doesn't matter that much, but it is preferable to not have to do this calculation twice (unless the direct subtraction definition is used, see above). The simplest predicate for testing numbers isIsZero so consider the condition.
But this condition is equivalent to, not. If this expression is used then the mathematical definition of division given above is translated into function on Church numerals as,
As desired, this definition has a single call to. However the result is that this formula gives the value of.
This problem may be corrected by adding 1 ton before callingdivide. The definition ofdivide is then,
divide1 is a recursive definition. TheY combinator may be used to implement the recursion. Create a new function calleddiv by;
In the left hand side
In the right hand side
to get,
Then,
where,
Gives,
Or as text, using \ forλ,
divide = (\n.((\f.(\x.x x) (\x.f (x x))) (\c.\n.\m.\f.\x.(\d.(\n.n (\x.(\a.\b.b)) (\a.\b.a)) d ((\f.\x.x) f x) (f (c d m f x))) ((\m.\n.n (\n.\f.\x.n (\g.\h.h (g f)) (\u.x) (\u.u)) m) n m))) ((\n.\f.\x. f (n f x)) n))
Apredicate is a function that returns a Boolean value. The most fundamental predicate on Church numerals is, which returns if its argument is the Church numeral, and otherwise:
The following predicate tests whether the first argument is less-than-or-equal-to the second:
Most real-world languages have support for machine-native integers; thechurch andunchurch functions convert between nonnegative integers and their corresponding Church numerals. The functions are given here inHaskell, where the\ corresponds to the λ of Lambda calculus. Implementations in other languages are similar.
One simple approach for extending Church Numerals tosigned numbers is to use a Church pair, containing Church numerals representing a positive and a negative value.[3] The integer value is the difference between the two Church numerals.
A natural number is converted to a signed number by,
Negation is performed by swapping the values.
The integer value is more naturally represented if one of the pair is zero. TheOneZero function achieves this condition,
The recursion may be implemented using the Y combinator,
The last expression is translated into lambda calculus as,
A similar definition is given here for division, except in this definition, one value in each pair must be zero (seeOneZero above). ThedivZ function allows us to ignore the value that has a zero component.
divZ is then used in the following formula, which is the same as for multiplication, but withmult replaced bydivZ.
Rational andcomputable real numbers may also be encoded in lambda calculus. Rational numbers may be encoded as a pair of signed numbers. Computable real numbers may be encoded by a limiting process that guarantees that the difference from the real value differs by a number which may be made as small as we need.[4][5] The references given describe software that could, in theory, be translated into lambda calculus. Once real numbers are defined, complex numbers are naturally encoded as a pair of real numbers.
The data types and functions described above demonstrate that any data type or calculation may be encoded in lambda calculus. This is theChurch–Turing thesis.
Alist contains some items in order. The basicoperations on lists are:
Function
Description
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
singleton
Create a list containing one given element
append
Append two lists together
fold
Fold the list with the given "plus" and "zero"
A representation of lists should provide ways to implement these operations.
The archetypal lambda calculus representation of lists is Church List encoding. It represents lists asright folds, as functions to return the results of folding over the list with user-supplied arguments.
It follows the paradigm of "a thing is the result of its observation". No matter the concrete implementation, folding a given list of values results in the same result. This provides an abstract view at what a list is. Church List encoding is such a mechanism.
On the other hand, seen more concretely, lists can be represented as a sequence oflinked listnodes.
What follows are four different representations of lists:
This is the original Church encoding for lists. A list is represented by a binary function, that, when supplied with two arguments, – a "combining function" and a "sentinel value", – will perform theright fold of the encoded list using those two arguments.
For an empty list the sentinel value is returned as the folding's result. The result of folding a non-empty list withhead h andtail t is the result of combining, by the supplied function, of thehead h with theresult of folding the tail t with the supplied two arguments. Thus the combining function's two arguments are, conceptually, thecurrent element and theresult of folding therest of the list.
For example, a list of three elements x, y and z is represented by a term that when applied to c and n returns c x (c y (c z n)). Equivalently, it is an application of the chain offunctional compositions ( ) of partial applications, ((c x) (c y) (c z)) n.
These definitions follow the following logic: the equations
fold c n [ ] = n fold c n [x ] = c x n fold c n [x,y,z] = c x (c y (c z n))
mean that
where denotes the Church List representation of the list.
Since Church encoded list is its own folding function, folding it just means applying that function to the supplied arguments.
This list representation can be given type inSystem F.
The evident correspondence to Church numerals is non-coincidental, as that can be seen as a unary encoding, with natural numbers represented by lists of unit (i.e. non-important) values, e.g. [() () ()], with the list's length serving as the representation of the natural number. Right folding over such lists uses functions which necessarily ignore the element's value, and is equivalent to the chained functional composition, i.e. ( (c ()) (c ()) (c ()) ) n = (f f f) n, as is used in Church numerals.
A nonempty list can be represented by a Church pair, where
first contains the list's head
second contains the list's tail
However this does not give a representation of the empty list, because there is no "null" pointer. To represent null, the pair can be wrapped in another pair, giving three values:
first - the null list indicator (a Boolean).
first of second contains the head (car).
second of second contains the tail (cdr).
Using this idea the basic list operations can be defined like this:[6]
Expression
Description
The first element of the pair istrue meaning the list is null.
Retrieve the null (or empty list) indicator.
Create a list node, which is not null, and give it a headh and a tailt.
second.first is the head.
second.second is the tail.
In anil nodesecond is never accessed, provided thathead andtail are only applied to nonempty lists.
where the definitions like the last one all follow the same general pattern for the safe use of a list, with and referring to the list's head and tail, and being discarded, as an artificial device:
Scott encoding for data types follows their surface syntax without regard to recursion. In the disjunction of conjunctions a.k.a. sum of products style of data type definitions, it represents a datum as a function which expects as many arguments as there are alternatives in its data type definition, where each such argument is expected to be a "handler" function that must be able to handle a given number of data arguments that will correspond to the data fields for that alternative.
For lists, it means the data type definition of
and lists being represented as
Recursive operations on Scott lists typically require explicit use of recursion, e.g. using combinator, or explicit self-application definitions. One such example isfold, unlike the no-op that it is under Church encoding. Buttail is immediately available, so its definition is much simpler here, in comparison. SeeScott encoding for more.
Scott encoding can be seen as using the idea ofcontinuations, which can lead to simpler code[8]. In this approach, we use the fact that lists can be observed usingpattern matching expression. For example, usingScala notation, iflist denotes a value of typeList with empty listNil and constructorCons(h, t) we can inspect the list and computenilCode in case the list is empty andconsCode(h, t) when the list is not empty:
Thelist is given by how it acts uponnilCode andconsCode. We therefore define a list as a function that accepts suchnilCode andconsCode as arguments, so that instead of the above pattern match we may simply write:
Let us denote byn the parameter corresponding tonilCode and byc the parameter corresponding toconsCode.The empty list is the one that returns the nil argument:
The non-empty list with headh and tailt is given by
More generally, analgebraic data type with alternatives becomes a function with parameters. When theth constructor has arguments, the corresponding parameter of the encoding takes arguments as well.
Scott encoding can be done in untyped lambda calculus, whereas its use with types requires a type system with recursion and type polymorphism. A list with element type E in this representation that is used to compute values of type C would have the following recursive type definition, where '=>' denotesfunction type:
typeList=C=>// nil argument(E=>List=>C)=>// cons argumentC// result of pattern matching
A list that can be used to compute arbitrary types would have a type that quantifies overC. A list generic[clarification needed] inE would also takeE as the type argument.
A straightforward implementation of Church encoding slows some access operations from to, where is the size of thedata structure, making Church encoding impractical.[9] Research has shown that this can be addressed by targeted optimizations, but mostfunctional programming languages instead expand their intermediate representations to containalgebraic data types.[10] Nonetheless Church encoding is often used in theoretical arguments, as it is a natural representation for partial evaluation and theorem proving.[9] Operations can be typed usinghigher-ranked types,[11] and primitive recursion is easily accessible.[9] The assumption that functions are the only primitive data types streamlines many proofs.
Church encoding is complete but only representationally. 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 areextensionally equal due to theundecidability of equivalence fromChurch'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 usingintensional equality. There arepotential problems with the interpretation of results because of the difference between the intensional and extensional definition of equality.
^Jansen, Jan Martin (2013), "Programming in the λ-calculus: from Church to Scott and back",The Beauty of Functional Code, Lecture Notes in Computer Science, vol. 8106, Springer-Verlag, pp. 168–180,doi:10.1007/978-3-642-40355-2_12,ISBN978-3-642-40354-5.
^Jansen, Jan Martin (2013). "Programming in the λ-Calculus: From Church to Scott and Back". In Achten, Peter; Koopman, Pieter W. M. (eds.).The Beauty of Functional Code - Essays Dedicated to Rinus Plasmeijer on the Occasion of His 61st Birthday. Lecture Notes in Computer Science. Vol. 8106. Springer. pp. 168–180.doi:10.1007/978-3-642-40355-2_12.ISBN978-3-642-40354-5.
^abcTrancón y Widemann, Baltasar; Parnas, David Lorge (2008). "Tabular Expressions and Total Functional Programming". In Olaf Chitil; Zoltán Horváth; Viktória Zsók (eds.).Implementation and Application of Functional Languages. 19th International Workshop, IFL 2007, Freiburg, Germany, September 27–29, 2007 Revised Selected Papers. Lecture Notes in Computer Science. Vol. 5083. pp. 228–229.doi:10.1007/978-3-540-85373-2_13.ISBN978-3-540-85372-5.
^Jansen, Jan Martin; Koopman, Pieter W. M.; Plasmeijer, Marinus J. (2006). "Efficient interpretation by transforming data types and patterns to functions". In Nilsson, Henrik (ed.).Trends in functional programming. Volume 7. Bristol: Intellect. pp. 73–90.CiteSeerX10.1.1.73.9841.ISBN978-1-84150-188-8.
Kemp, Colin (2007)."§2.4.1 Church Naturals, §2.4.2 Church Booleans, Ch. 5 Derivation techniques for TFP".Theoretical Foundations for Practical 'Totally Functional Programming' (PhD). School of Information Technology and Electrical Engineering, The University of Queensland. pp. 14–17,93–145.CiteSeerX10.1.1.149.3505. All about Church and other similar encodings, including how to derive them and operations on them, from first principles