Church Encoding - 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
not1λm.λa.λb. m b a
not2λm. m (λa.λb. b) (λa.λb. a)
xorλa.λb. a (not2 b) b
ifλm.λa.λb. m 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
not1 true(λm.λa.λb. m b a) (λa.λb. a)(λa.λb. (λa.λb. a) b a)(λa.λb. (λb.b) a)(λa.λb. b)false
not2 true(λm. m (λa.λb. b) (λa.λb. a)) (λa.λb. a)(λa.λb. a) (λa.λb. b) (λa.λb. a)(λb. (λa.λb. b)) (λa.λb. a)λ1.λb. bfalse

Read more about this topic:  Church Encoding

Famous quotes containing the word church:

    A little black thing among the snow
    Crying “’weep, ‘weep,” in notes of woe!
    “Where are thy father & mother? say?”
    “They are both gone up to the church to pray.
    William Blake (1757–1827)