Church coding

from Wikipedia, the free encyclopedia

Under church encoding means the embedding data and operators in the lambda calculus . The best known form are the Church numerals , which represent the natural numbers . They are named after Alonzo Church , who was the first to model data in this way.

Church numerals

definition

The basic idea for coding is based on the Peano axioms , according to which the natural numbers are defined by a starting value - the 0 - and a successor function. Accordingly, the Church numerals are defined as follows:

0λf.λx. x
1λf.λx. f x
2λf.λx. f (f x)
3λf.λx. f (f (f x))
...
nλf.λx. fn x

Calculating with Church numbers

In the lambda calculus, arithmetic functions can be represented by corresponding functions using Church numerals. These functions can be implemented in functional programming languages directly by transferring the lambda expressions.

The successor function is defined as follows:

succλn.λf.λx. f (n f x)

The addition of two numbers and is the application of the successor function to :

plusλm.λn.λf.λx. m f (n f x)

The multiplication of two numbers and is the application of the addition to :

multλm.λn.λf.λx. m (n f) x

The predecessor function:

predλn.λf.λx. n (λg.λh. h (g f)) (λu. x) (λu. u)

Boolean expressions

Analogous to the natural numbers, (two-valued) truth values can also be modeled in the lambda calculus.

trueλx.λy. x
falseλx.λy. y

A simple control structure ( IF THEN ELSE ) can also be derived from this:

ifthenelseλb.λx.λy.b x y

The variable is to be understood as a condition, as "THEN" and as "ELSE".