ebooksgratis.com

See also ebooksgratis.com: no banners, no cookies, totally FREE.

CLASSICISTRANIERI HOME PAGE - YOUTUBE CHANNEL
Privacy Policy Cookie Policy Terms and Conditions
Church encoding - Wikipedia, the free encyclopedia

Church encoding

From Wikipedia, the free encyclopedia

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.

Contents

[edit] 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 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.

f^n = f \circ f \circ \cdots \circ f.\,

[edit] Definition

Church numerals 0, 1, 2, ..., are defined as follows in the lambda calculus:

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
...

That is, the natural number n is represented by the Church numeral n, which has property that for any lambda-terms F and X,

n F X =β Fn X

[edit] Computation with Church numerals

In 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 \scriptstyle\operatorname{plus}(m,n)=m+n uses the identity f(m + n)(x) = fm(fn(x)).

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

The successor function \scriptstyle\operatorname{succ}(n)=n+1 is β-equivalent to (plus 1).

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

The multiplication function \scriptstyle\operatorname{mult}(m,n)=m*n uses the identity f(m * n) = (fm)n.

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

The exponentiation function \scriptstyle\exp(m,n)=m^n is straightforward given our definition of church numerals.

expλm.λn. n m

The predecessor function \operatorname{pred}(n) = \begin{cases} 0 & \mbox{if }n=0, \\ n-1 & \mbox{otherwise}\end{cases} works by generating an n-fold composition of functions that each apply their argument g to f; the base case discards its copy of f and returns x.

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

The subtraction function can be written based on the predecessor function.

subλm.λn. (m pred) n

[edit] Translation with other representations

Most 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.

type Church a = (a -> a) -> a -> a
church :: Integer -> Church a
church 0 = \f -> \x -> x
church n = \f -> \x -> f (church (n-1) f x)
unchurch :: Church Integer -> Integer
unchurch n = n (\x -> x + 1) 0

The same code as above, here given in F#

let rec church n = fun f x -> match n with
| 0 -> x
| n -> f (church (n-1) f x)


let unchurch f = f (fun x -> x + 1) 0

In the case of church, the keyword rec is required to allow recursion in F#.

[edit] Church booleans

Church 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:

trueλa.λb. a
falseλa.λb. b

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,

predicate x then-clause else-clause

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:

andλm.λn. m n m
orλm.λn. m m n
notλm.λa.λb. m b a
xorλm.λn.λa.λb. m (n b a) (n a b)

Some examples:

and true false(λm.λn. m n m) (λa.λb. a) (λa.λb. b) ≡ (λa.λb. a) (λa.λb. b) (λa.λb. a)(λa.λb. b)false
or true false(λm.λn. m m n) (λa.λb. a) (λa.λb. b)(λa.λb. a) (λa.λb. a) (λa.λb. b)(λa.λb. a)true
not true(λm.λa.λb. m b a) (λa.λb. a)(λa.λb. (λa.λb. a) b a)(λa.λb. b)false


[edit] Church pairs

See also: Cons

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:

pairλx.λy.λz.z x y
fstλp.p (λx.λy.x)
sndλp.p (λx.λy.y)

An example:

fst (pair a b) ≡ λp.p (λx.λy.x) ((λx.λy.λz.z x y) a b) ≡ λp.p (λx.λy.x) (λz.z a b) ≡ (λz.z a b) (λx.λy.x) ≡ (λx.λy.x) a b ≡ a

[edit] See also

[edit] References


aa - ab - af - ak - als - am - an - ang - ar - arc - as - ast - av - ay - az - ba - bar - bat_smg - bcl - be - be_x_old - bg - bh - bi - bm - bn - bo - bpy - br - bs - bug - bxr - ca - cbk_zam - cdo - ce - ceb - ch - cho - chr - chy - co - cr - crh - cs - csb - cu - cv - cy - da - de - diq - dsb - dv - dz - ee - el - eml - en - eo - es - et - eu - ext - fa - ff - fi - fiu_vro - fj - fo - fr - frp - fur - fy - ga - gan - gd - gl - glk - gn - got - gu - gv - ha - hak - haw - he - hi - hif - ho - hr - hsb - ht - hu - hy - hz - ia - id - ie - ig - ii - ik - ilo - io - is - it - iu - ja - jbo - jv - ka - kaa - kab - kg - ki - kj - kk - kl - km - kn - ko - kr - ks - ksh - ku - kv - kw - ky - la - lad - lb - lbe - lg - li - lij - lmo - ln - lo - lt - lv - map_bms - mdf - mg - mh - mi - mk - ml - mn - mo - mr - mt - mus - my - myv - mzn - na - nah - nap - nds - nds_nl - ne - new - ng - nl - nn - no - nov - nrm - nv - ny - oc - om - or - os - pa - pag - pam - pap - pdc - pi - pih - pl - pms - ps - pt - qu - quality - rm - rmy - rn - ro - roa_rup - roa_tara - ru - rw - sa - sah - sc - scn - sco - sd - se - sg - sh - si - simple - sk - sl - sm - sn - so - sr - srn - ss - st - stq - su - sv - sw - szl - ta - te - tet - tg - th - ti - tk - tl - tlh - tn - to - tpi - tr - ts - tt - tum - tw - ty - udm - ug - uk - ur - uz - ve - vec - vi - vls - vo - wa - war - wo - wuu - xal - xh - yi - yo - za - zea - zh - zh_classical - zh_min_nan - zh_yue - zu -