|
In mathematics, Church encoding is a means of embedding data and operators into the lambda calculus, the most familiar form being the Church numerals, 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; from the Church-Turing thesis we know that any computable operator (and its operands) can be represented under Church encoding. Many students of mathematics are familiar with Gödel numbering members of a set; Church encoding is an equivalent operation defined on lambda abstractions instead of natural numbers.
[edit] Church numeralsChurch 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 other function f to its n-fold composition. In simpler terms, the "value" of the numeral is equivalent to the number of times the function encapsulates x. [edit] DefinitionChurch numerals 0, 1, 2, ..., are defined as follows in the lambda calculus:
That is, the natural number n is represented by the Church numeral n, which has property that for any lambda-terms
[edit] Computation with Church numeralsIn the lambda calculus, numeric functions are representable by corresponding functions on Church numerals. These functions can be implemented in most functional programming languages (subject to type constraints) by direct translation of lambda terms. The addition function
The successor function
The multiplication function
The exponentiation function
The predecessor function
The subtraction function can be written based on the predecessor function.
[edit] Translation with other representationsMost real-world languages have support for machine-native integers; the church and unchurch functions (given here in Haskell) convert between nonnegative integers and their corresponding church numerals. Implementations of these conversions in other languages are similar.
[edit] Church booleansChurch booleans are the Church encoding of the boolean values true and false. Some programming languages use these as an implementation model for boolean arithmetic; examples are Smalltalk and Pico. The boolean values are represented as functions of two values that evaluate to one or the other of their arguments. Formal definition in lambda calculus:
Note that this definition allows predicates (i.e. functions returning logical values) to directly act as if-clauses, e.g. if predicate is a unary predicate,
evaluates to then-clause if predicate x evaluates to true, and to else-clause if predicate x evaluates to false. Functions of boolean arithmetic can be derived for Church booleans:
Some examples:
[edit] Church pairs
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. Formal definition in lambda calculus:
An example:
[edit] List encondingsAn encoding of (immutable) lists of varying length must define a constructor for creating an empty list (nil), an operation testing whether or not a list is empty (isnil), an operation to prepend a given value to a (possibly empty) list (cons), and two operations to determine the first element and the list of the remaining elements of a nonempty list (head and tail). [edit] Church pairsA nonempty list can basically be encoded by a Church pair with the head of the list stored in the first component of the pair and the tail of the list in the second component. However, special care is needed to unambiguously encode the empty list. This can be achieved by encapsulating any individual list node with another pair with the second component containing the list node and the first component containing a Church boolean which is true for the empty list and false otherwise, similarly to a tagged union. Using this idea the basic list operations can be defined like this:[1]
The second component of the pair enconding nil is never used provided that head and tail are only applied to nonempty lists. [edit] Higher-order-functionAs 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))).
[edit] See also
[edit] References
offerte voli | hoteles | precios | voli | die verzeichnis | annuarie web | stop smoking london |